ascii is working
[ascii.git] / ascii.c
CommitLineData
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
17char *progname = NULL;
18int verbose = 0;
19int nbcols = 4;
20
21char 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 41int usage (int ret)
93b14937 42{
1968fc94
LM
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");
93b14937 48
1968fc94 49 return ret;
93b14937
LM
50}
51
52/* main function */
53
54int main (int argc, char *argv[])
55{
1968fc94 56 int i = 0;
93b14937
LM
57
58 progname = argv[0];
1968fc94
LM
59 while (progname[i] != '\0') {
60 if ((progname[i] == '/') || (progname[i] == '\\')) {
61 progname += i + 1;
62 i = 0;
63 } else {
64 i++;
65 }
66 }
93b14937 67
1968fc94
LM
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];
93b14937
LM
75 switch (c) {
76 case 'c':
1968fc94
LM
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);
93b14937
LM
83 break;
84 case 'v':
1968fc94
LM
85 PRINTOUT ("%s: version %s\n", progname, VERSION);
86 return 0;
93b14937
LM
87 break;
88 case 'h':
89 default:
1968fc94 90 return usage (c != 'h');
93b14937
LM
91 }
92 }
1968fc94
LM
93
94 /* main */
93b14937
LM
95
96 for (i = 0; i < 256; i++) {
1968fc94 97 char line[] = " [ ] x";
93b14937
LM
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
1968fc94 118 fdprintf (_fd_stdout, "%s", line);
93b14937
LM
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: */