c15d71bc1021bcbcb5c3bbc253a00f07584e0636
[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 ? _fd_stderr : _fd_stdout;
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 progname = argv[0];
59 while (progname[i] != '\0') {
60 if ((progname[i] == '/') || (progname[i] == '\\')) {
61 progname += i + 1;
62 i = 0;
63 } else {
64 i++;
65 }
66 }
67
68 while (argc-- > 1) {
69 char *arg = *(++argv);
70 if (arg[0] != '-') {
71 PRINTERR ("%s: invalid option -- %s\n", progname, arg);
72 return usage (1);
73 }
74 char c = arg[1];
75 switch (c) {
76 case 'c':
77 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
78 if (arg == NULL) {
79 PRINTERR ("%s: missing number of columns\n", progname);
80 return usage (1);
81 }
82 nbcols = atoi (arg);
83 break;
84 case 'v':
85 PRINTOUT ("%s: version %s\n", progname, VERSION);
86 return 0;
87 break;
88 case 'h':
89 default:
90 return usage (c != 'h');
91 }
92 }
93
94 /* main */
95
96 for (i = 0; i < 256; i++) {
97 char line[] = " [ ] x";
98
99 if (i > 99) line[0] = '0' + i / 100;
100 if (i > 9) line[1] = '0' + (i / 10) % 10;
101 line[2] = '0' + i % 10;
102 line[4] = '0' + i / 16;
103 if (line[4] > '9') line[4] += 'a' - '0' - 10;
104 line[5] = '0' + i % 16;
105 if (line[5] > '9') line[5] += 'a' - '0' - 10;
106
107 if (tablechars[i][0] != '\0') {
108 int j = 0;
109 while (tablechars[i][j] != '\0') {
110 line[8 + j] = tablechars[i][j];
111 j++;
112 }
113 } else {
114 line[8] = i;
115 }
116 line[11] = (((i + 1) % nbcols == 0) || (i == 255)) ? '\n' : ' ';
117
118 fdprintf (_fd_stdout, "%s", line);
119 }
120
121 return 0;
122 }
123
124 // test: ascii.exe -h
125 // test: ascii.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
126 // test: ascii.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
127 // test: ascii.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
128
129 /* vim: set ts=4 sw=4 et: */