#include #include #include #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; }