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
|
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "util/option.h"
static const char *usage_string =
"Gebruik: %s [-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) {
const struct option_spec spec[] = {
{'n', OPTION_SETBOOL(&opts->nonewline)},
{'e', OPTION_SETBOOL(&opts->unescape)},
{'h', OPTION_HELPUSAGE(usage_string)},
{'V', OPTION_VERSION()},
OPTION_SPEC_END
};
return option_parse(argc, argv, spec);
}
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;
}
|