Commit | Line | Data |
---|---|---|
93b14937 LM |
1 | /* depend: */ |
2 | /* cflags: */ | |
3 | /* linker: */ | |
4 | ||
5 | #include <assert.h> | |
6 | #include <getopt.h> | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <unistd.h> | |
10 | ||
11 | /* macros */ | |
12 | ||
13 | #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0) | |
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 | ||
41 | void usage (int ret) | |
42 | { | |
43 | FILE *fd = ret ? stderr : stdout; | |
44 | fprintf (fd, "usage: %s\n", progname); | |
45 | fprintf (fd, " -c : number of columns (%d)\n", nbcols); | |
46 | fprintf (fd, " -h : help message\n"); | |
47 | fprintf (fd, " -v : verbose level (%d)\n", verbose); | |
48 | ||
49 | exit (ret); | |
50 | } | |
51 | ||
52 | /* main function */ | |
53 | ||
54 | int main (int argc, char *argv[]) | |
55 | { | |
56 | int i; | |
57 | ||
58 | progname = argv[0]; | |
59 | ||
60 | int c; | |
61 | while ((c = getopt(argc, argv, "c:hv:")) != EOF) { | |
62 | switch (c) { | |
63 | case 'c': | |
64 | nbcols = atoi (optarg); | |
65 | break; | |
66 | case 'v': | |
67 | verbose = atoi (optarg); | |
68 | break; | |
69 | case 'h': | |
70 | default: | |
71 | usage (c != 'h'); | |
72 | } | |
73 | } | |
74 | if (argc - optind != 0) { | |
75 | fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]); | |
76 | usage (1); | |
77 | } | |
78 | ||
79 | for (i = 0; i < 256; i++) { | |
80 | char line[] = " : : x"; | |
81 | ||
82 | if (i > 99) line[0] = '0' + i / 100; | |
83 | if (i > 9) line[1] = '0' + (i / 10) % 10; | |
84 | line[2] = '0' + i % 10; | |
85 | line[4] = '0' + i / 16; | |
86 | if (line[4] > '9') line[4] += 'a' - '0' - 10; | |
87 | line[5] = '0' + i % 16; | |
88 | if (line[5] > '9') line[5] += 'a' - '0' - 10; | |
89 | ||
90 | if (tablechars[i][0] != '\0') { | |
91 | int j = 0; | |
92 | while (tablechars[i][j] != '\0') { | |
93 | line[8 + j] = tablechars[i][j]; | |
94 | j++; | |
95 | } | |
96 | } else { | |
97 | line[8] = i; | |
98 | } | |
99 | line[11] = (((i + 1) % nbcols == 0) || (i == 255)) ? '\n' : ' '; | |
100 | ||
101 | write (STDOUT_FILENO, line, 12); | |
102 | } | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | // test: ascii.exe -h | |
108 | // test: ascii.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' | |
109 | // test: ascii.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' | |
110 | // test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' | |
111 | ||
112 | /* vim: set ts=4 sw=4 et: */ |