remove skeleton file
[ascii.git] / ascii.c
1 /* depend: */
2 /* cflags: */
3 /* linker: atoi.o fdprintf.o */
4
5 #include <stddef.h>
6
7 #include "atoi.h"
8 #include "fdprintf.h"
9
10 /* macros */
11
12 #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
13 #define VERSION "0.9"
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 int usage (int ret)
42 {
43 int fd = ret ? stdfderr : stdfdout;
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");
48
49 return ret;
50 }
51
52 /* main function */
53
54 int main (int argc, char *argv[])
55 {
56 int i = 0;
57
58 /* programm name */
59
60 progname = argv[0];
61 while (progname[i] != '\0') {
62 if ((progname[i] == '/') || (progname[i] == '\\')) {
63 progname += i + 1;
64 i = 0;
65 } else {
66 i++;
67 }
68 }
69
70 /* argument processing */
71
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];
79 switch (c) {
80 case 'c':
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);
87 break;
88 case 'v':
89 PRINTOUT ("%s: version %s\n", progname, VERSION);
90 return 0;
91 break;
92 case 'h':
93 default:
94 return usage (c != 'h');
95 }
96 }
97
98 /* main */
99
100 int n = (256 + nbcols - 1) / nbcols;
101 for (i = 0; i < n * nbcols; i++) {
102 char line[] = " [ ] x";
103 int j = i / nbcols + (i % nbcols) * n;
104
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;
114 if (line[4] > '9') line[4] += 'a' - '0' - 10;
115 line[5] = '0' + j % 16;
116 if (line[5] > '9') line[5] += 'a' - '0' - 10;
117
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++;
123 }
124 } else {
125 line[8] = j;
126 }
127 line[11] = ((i + 1) % nbcols == 0) ? '\n' : ' ';
128
129 PRINTOUT ("%s", line);
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) }'
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'
139 // test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
140
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) }'
143 // test: ascii.exe -v | grep -q version
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) }'
147 // test: ascii.exe -c 2>&1 | grep -q 'missing number of columns'
148
149 /* vim: set ts=4 sw=4 et: */