Commit | Line | Data |
---|---|---|
93b14937 LM |
1 | /* depend: */ |
2 | /* cflags: */ | |
1968fc94 | 3 | /* linker: atoi.o fdprintf.o */ |
93b14937 | 4 | |
1968fc94 LM |
5 | #include <stddef.h> |
6 | ||
7 | #include "atoi.h" | |
8 | #include "fdprintf.h" | |
93b14937 LM |
9 | |
10 | /* macros */ | |
11 | ||
12 | #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0) | |
1968fc94 | 13 | #define VERSION "0.9" |
93b14937 LM |
14 | |
15 | /* gobal variables */ | |
16 | ||
17 | char *progname = NULL; | |
18 | int verbose = 0; | |
19 | int nbcols = 4; | |
20 | ||
21 | char tablechars[256][4] = { | |
22 | "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "DEL", | |
23 | "DS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", | |
24 | "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", | |
25 | "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US", | |
26 | "SP", "", "", "", "", "", "", "", | |
27 | "", "", "", "", "", "", "", "", | |
28 | "", "", "", "", "", "", "", "", | |
29 | "", "", "", "", "", "", "", "", | |
30 | "", "", "", "", "", "", "", "", | |
31 | "", "", "", "", "", "", "", "", | |
32 | "", "", "", "", "", "", "", "", | |
33 | "", "", "", "", "", "", "", "", | |
34 | "", "", "", "", "", "", "", "", | |
35 | "", "", "", "", "", "", "", "", | |
36 | "", "", "", "", "", "", "", "", | |
37 | "", "", "", "", "", "", "", "DEL"}; | |
38 | ||
39 | /* help function */ | |
40 | ||
1968fc94 | 41 | int usage (int ret) |
93b14937 | 42 | { |
a8476deb | 43 | int fd = ret ? stdfderr : stdfdout; |
1968fc94 LM |
44 | fdprintf (fd, "usage: %s\n", progname); |
45 | fdprintf (fd, " -c : number of columns (%d)\n", nbcols); | |
46 | fdprintf (fd, " -h : help message\n"); | |
47 | fdprintf (fd, " -v : show version\n"); | |
93b14937 | 48 | |
1968fc94 | 49 | return ret; |
93b14937 LM |
50 | } |
51 | ||
52 | /* main function */ | |
53 | ||
54 | int main (int argc, char *argv[]) | |
55 | { | |
1968fc94 | 56 | int i = 0; |
93b14937 | 57 | |
a8476deb LM |
58 | /* programm name */ |
59 | ||
93b14937 | 60 | progname = argv[0]; |
1968fc94 LM |
61 | while (progname[i] != '\0') { |
62 | if ((progname[i] == '/') || (progname[i] == '\\')) { | |
63 | progname += i + 1; | |
64 | i = 0; | |
65 | } else { | |
66 | i++; | |
67 | } | |
68 | } | |
93b14937 | 69 | |
a8476deb LM |
70 | /* argument processing */ |
71 | ||
1968fc94 LM |
72 | while (argc-- > 1) { |
73 | char *arg = *(++argv); | |
74 | if (arg[0] != '-') { | |
75 | PRINTERR ("%s: invalid option -- %s\n", progname, arg); | |
76 | return usage (1); | |
77 | } | |
78 | char c = arg[1]; | |
93b14937 LM |
79 | switch (c) { |
80 | case 'c': | |
1968fc94 LM |
81 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
82 | if (arg == NULL) { | |
83 | PRINTERR ("%s: missing number of columns\n", progname); | |
84 | return usage (1); | |
85 | } | |
86 | nbcols = atoi (arg); | |
93b14937 LM |
87 | break; |
88 | case 'v': | |
1968fc94 LM |
89 | PRINTOUT ("%s: version %s\n", progname, VERSION); |
90 | return 0; | |
93b14937 LM |
91 | break; |
92 | case 'h': | |
93 | default: | |
1968fc94 | 94 | return usage (c != 'h'); |
93b14937 LM |
95 | } |
96 | } | |
1968fc94 LM |
97 | |
98 | /* main */ | |
93b14937 | 99 | |
a8476deb LM |
100 | int n = (256 + nbcols - 1) / nbcols; |
101 | for (i = 0; i < n * nbcols; i++) { | |
1968fc94 | 102 | char line[] = " [ ] x"; |
a8476deb | 103 | int j = i / nbcols + (i % nbcols) * n; |
93b14937 | 104 | |
a8476deb LM |
105 | if (j > 255) { |
106 | PRINTOUT ("\n"); | |
107 | continue; | |
108 | } | |
109 | ||
110 | if (j > 99) line[0] = '0' + j / 100; | |
111 | if (j > 9) line[1] = '0' + (j / 10) % 10; | |
112 | line[2] = '0' + j % 10; | |
113 | line[4] = '0' + j / 16; | |
93b14937 | 114 | if (line[4] > '9') line[4] += 'a' - '0' - 10; |
a8476deb | 115 | line[5] = '0' + j % 16; |
93b14937 LM |
116 | if (line[5] > '9') line[5] += 'a' - '0' - 10; |
117 | ||
a8476deb LM |
118 | if (tablechars[j][0] != '\0') { |
119 | int k = 0; | |
120 | while (tablechars[j][k] != '\0') { | |
121 | line[8 + k] = tablechars[j][k]; | |
122 | k++; | |
93b14937 LM |
123 | } |
124 | } else { | |
a8476deb | 125 | line[8] = j; |
93b14937 | 126 | } |
a8476deb | 127 | line[11] = ((i + 1) % nbcols == 0) ? '\n' : ' '; |
93b14937 | 128 | |
a8476deb | 129 | PRINTOUT ("%s", line); |
93b14937 LM |
130 | } |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
135 | // test: ascii.exe -h | |
136 | // test: ascii.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' | |
e9b24fd1 LM |
137 | // test: ascii.exe -_ 2>/dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' |
138 | // test: ascii.exe error 2>&1 | grep -q 'invalid option' | |
93b14937 LM |
139 | // test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' |
140 | ||
a8476deb LM |
141 | // test: ascii.exe | awk '/ 64\[40\] @/ { rc=1 } END { exit (1-rc) }' |
142 | // test: ascii.exe | awk '/127\[7f\] DEL/ { rc=1 } END { exit (1-rc) }' | |
e9b24fd1 | 143 | // test: ascii.exe -v | grep -q version |
a8476deb LM |
144 | // test: ascii.exe -c 4 | tail -1 | awk '/63/ { rc=1 } END { exit (1-rc) }' |
145 | // test: ascii.exe -c 5 | tail -1 | awk '/51/ { rc=1 } END { exit (1-rc) }' | |
146 | // test: ascii.exe -c 6 | tail -1 | awk '/42/ { rc=1 } END { exit (1-rc) }' | |
e9b24fd1 | 147 | // test: ascii.exe -c 2>&1 | grep -q 'missing number of columns' |
a8476deb | 148 | |
93b14937 | 149 | /* vim: set ts=4 sw=4 et: */ |