summaryrefslogtreecommitdiff
path: root/src/weerklank.c
blob: ad033891f07c22a6f3f26306a1a5e6d5449cf570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "util/versie.h"

static void usage(FILE *f) {
  fprintf(f,
      "Gebruik: weerklank [-nehV] [touwtje]...\n"
      "\n"
      "Exporteer de touwtjes.\n"
      "\n"
      "  -n    Voeg geen nieuwe regel toe\n"
      "  -e    Ontleed terugslagontsnappingen\n"
      "  -h    Toon deze hulptekst\n"
      "  -V    Toon versienummer\n");
}

struct options {
  bool nonewline;
  bool unescape;
};

// Returns pointer to argument array containing the file names
static char** parse_options(int argc, char **argv, struct options *opts) {
  int opt;
  while ((opt = getopt(argc, argv, "nehV")) != -1) {
    switch (opt) {
      case 'n':
        opts->nonewline = true;
        break;

      case 'e':
        opts->unescape = true;
        break;

      case 'h':
        usage(stdout);
        exit(0);

      case 'V':
        drukkedoos_print_versie(stdout, "weerklank");
        exit(0);

      case '?':
        fprintf(stderr, "weerklank: Ongeldige optie: -%c\n", optopt);
        usage(stderr);
        exit(1);
    }
  }

  return argv + optind;
}

static const char lowercase_escapes[] = "\a\b\0\0\e\f\0\0\0\0\0\0\0\n\0\0\0\r\0\t\0\v\0\0\0\0";

static int fromhexdigit(char c) {
  if ('0' <= c && c <= '9') return c - '0';
  if ('a' <= c && c <= 'f') return c - 'a' + 10;
  if ('A' <= c && c <= 'F') return c - 'A' + 10;
  return -1;
}

int entry_weerklank(int argc, char **argv) {
  struct options opts = {0};

  char **args = parse_options(argc, argv, &opts);

  for (int i = 0; args[i]; i++) {
    if (i != 0) putchar(' ');

    if (!opts.unescape) fputs(args[i], stdout);
    else for (int j = 0; args[i][j]; j++) {
      if (args[i][j] != '\\') putchar(args[i][j]);
      else if ('a' <= args[i][j+1] && args[i][j+1] <= 'z'
                 && lowercase_escapes[args[i][j+1] - 'a'] != '\0') {
        putchar(lowercase_escapes[args[i][j+1] - 'a']);
        j++;
      } else if (args[i][j+1] == '0') {
        char n = 0;
        int len = 0;
        while (isdigit(args[i][j+1 + len+1]) && len < 3)
          n = 8 * n + (args[i][j+1 + ++len] - '0');
        putchar(n);
        j += 1 + len;
      } else if (args[i][j+1] == 'x') {
        int n1, n2;
        if ((n1 = fromhexdigit(args[i][j+2])) == -1) fputs("\\x", stdout);
        else if ((n2 = fromhexdigit(args[i][j+3])) == -1) { putchar(n1); j += 2; }
        else { putchar(16 * n1 + n2); j += 3; }
      } else putchar('\\');
    }
  }

  if (!opts.nonewline) putchar('\n');

  return 0;
}