File: ascii.c

package info (click to toggle)
ascii 3.11-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 120 kB
  • sloc: ansic: 304; xml: 263; makefile: 36; sh: 17
file content (401 lines) | stat: -rw-r--r-- 10,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * ascii.c -- quick crossreference for ASCII character aliases
 *
 * Tries to interpret arguments as names or aliases of ascii characters
 * and dumps out *all* the aliases of each. Accepts literal characters,
 * standard mnemonics, C-style backslash escapes, caret notation for control
 * characters, numbers in hex/decimal/octal/binary, English names.
 *
 * The slang names used are selected from the 2.2 version of the USENET ascii
 * pronunciation guide.  Some additional ones were merged in from the Jargon
 * File.
 *
 * For license terms, see the file COPYING.
 */

#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

typedef char	*string;

static bool terse = false;
static bool help = false;
static bool line = false;

enum { decimal, hex, octal } mode;

void print_table(unsigned short delivery);

static string cnames[128][16] =
{
    {
#include "nametable.h"
    }
};

static int atob(str)
/* ASCII-to-binary conversion */
char	*str;
{
    int	val;

    for (val = 0; *str != '\0'; str++)
    {
	val *= 2;

	if (*str == '1')
	    val++;
        else if (*str != '0')
	    return(-1);
    }
    return(val);
}

void 
showHelp(FILE *out, char *progname) 
{
  fprintf(out,"Usage: %s [-dxohv] [-t] [char-alias...]\n", progname); 
#define P(s)	(void)fputs(s "\n", out);
#include "splashscreen.h"
#undef P

  exit(0);
}

static void process_options(int argc, char *argv[], char * opt_string) 
{
    int op;

    while( (op=getopt(argc, argv, opt_string)) != -1) {

	switch (op) {
	case 't':
	    terse = true; 
	    break;
	case 's':
	    terse = true; 
	    line = true; 
	    break;
	case 'd':
            print_table(decimal);
            exit(0);
	case 'x':
            print_table(hex);
            exit(0);
	case 'o':
            print_table(octal);
            exit(0);
	case '?':
	case 'h':
	    help = true;
	    break;
	case 'v':
	    printf("ascii %2.1f\n",REVISION); 
	    exit(0);
	default :
	    help = true;
	    break;
        } /*switch*/
    }/*while*/
}

static char *btoa(unsigned int val)
/* binary-to-ASCII conversion */
{
#define BITSPERCHAR	8
    char	*rp;
    static char	rep[BITSPERCHAR + 1];

    /* write out external representation at least one char */
    *(rp = rep + BITSPERCHAR) = '\0';
    do {
	*--rp = (char)('0' + (val & 1)); /* Is '1' == '0' + 1 in EBCDIC? */
       val /= 2;
    } while
	(val != 0 && rp > rep);

#ifndef SHORT_BINARY_REPRESENTATION
    while (rp > rep)
       *--rp = '0';
#endif
 
    return(rp);
}

static void speak(unsigned int ch)
/* list all the names for a given character */
{
    char	**ptr = &cnames[ch][0];

    if (terse) {
        (void) printf("%u/%u   %u   0x%02X   0o%o   %s\n", 
		      ch / 16, ch % 16, ch, ch, ch, btoa(ch));
        return;
    }

    (void) printf(
	"ASCII %u/%u is decimal %03u, hex %02x, octal %03o, bits %s: ",
	ch / 16, ch % 16, ch, ch, ch, btoa(ch));

    /* display high-half characters */
    if (ch & 0x80)
    {
	ch &=~ 0x80;
	if (ch == 0x7f)
	    (void) printf("meta-^?\n");
	else if (isprint((int)ch))
	    (void) printf("meta-%c\n", (char)ch);
	else
	    (void) printf("meta-^%c\n", '@' + (ch & 0x1f));
	return;
    }

    if (isprint((int)ch))
	(void) printf("prints as `%s'\n", *ptr++);
    else if (iscntrl((char)ch) || ch == 0x7F)
    {
	if (ch == 0x7f)
	    (void) printf("called ^?");
	else
	    (void) printf("called ^%c", '@' + (ch & 0x1f));
	for (; strlen(*ptr) < 4 && isupper(**ptr); ptr++)
	    (void) printf(", %s", *ptr);
	(void) putchar('\n');
    }

    (void) printf("Official name: %s\n", *ptr++);

    if (*ptr)
    {
	char	*commentary = (char *)NULL;

	if (**ptr == '\\')
	{
	    (void) printf("C escape: '%s'\n", *ptr);
	    ptr++;
	}

	(void) printf("Other names: ");
	for (; *ptr; ptr++)
	    if (**ptr == '#')
		commentary = *ptr;
	    else
		(void) printf("%s%s ", *ptr, 
			      (ptr[1]!=NULL && *ptr[1] != '#') ? "," : "");
	(void) putchar('\n');
	if (commentary)
	    (void) printf("Note: %s\n", commentary+2);
    }

    (void) putchar('\n');
}

static int stricmp(char	*s, char *t)
/* case-blind string compare */
{
    while (*s!='\0' && tolower(*s) == tolower(*t))
	s++, t++;
    return (int)(*t - *s);
}

static void ascii(char *str)
{
    int	ch, hi, lo;
    char **ptr;
    size_t len = strlen(str);

    /* interpret single characters as themselves */
    if (len == 1) { 
	speak((unsigned int)str[0]); 
	/* also interpret single digits as numeric */
	if(!line && strchr("0123456789ABCDEFabcdef",str[0])) {
	    int hval;
	    (void) sscanf(str, "%x", &hval);
	    speak(hval);
	}	return; 
    }

    /* process multiple letters */ 
    if (line == 1) {	
	for (ch = 0; ch < len; ch ++) {
	    speak((unsigned int)str[ch]);
	}
	return;
    }

    /* interpret ^-escapes as control-character notation */
    if (strcmp(str, "^?") == 0)
    { speak((unsigned int)0x7F); return; } 
    else if (str[0] == '^' && len == 2)
    { speak((unsigned int)(str[1] & 0x1f)); return; }

    /* interpret C-style backslash escapes */
    if (*str == '\\' &&  len == 2 && strchr("abfnrtv", str[1]))
	for (ch = 7; ch < 14; ch++)
	    for (ptr = &cnames[ch][1]; *ptr; ptr++)
		if (**ptr == '\\' && strcmp(str, *ptr) == 0)
		{ speak((unsigned int)ch); return; }

    /* interpret 2 and 3-character ASCII control mnemonics */
    if (len == 2 || len == 3)
    {
	/* first check for standard mnemonics */
	if (stricmp(str, "DEL") == 0)
	{ speak(0x7f); return; }
	if (stricmp(str, "BL") == 0)
	{ speak(' '); return; }
	else if (isalpha(str[0]))
	    for (ch = 0; ch <= 32; ch++)
		if (!stricmp(str,cnames[ch][0]) || !strcmp(str,cnames[ch][1]))
		{ speak(ch); return; }
    }

    /* OK, now try to interpret the string as a numeric */
    if (len > 1 && len < 9)
    {
	int hval, dval, oval, bval, spoken = 0;

	dval = oval = hval = bval = -1;

	/* if it's all numeric it could be in one of three bases */
	if (len <= 2 && strspn(str,"0123456789ABCDEFabcdef") == len)
	    (void) sscanf(str, "%x", &hval);
	if (len <= 3 && strspn(str, "0123456789") == len)
	    (void) sscanf(str, "%d", &dval);
	if (len <= 3 && strspn(str, "01234567") == len)
	    (void) sscanf(str, "%o", &oval);
	if (len <= 9 && strspn(str, "01") == len)
	    bval = atob(str);

	/* accept 0xnn, \xnn, xnn and nnh forms for hex */
	if (hval == -1)
	    if ((str[0]=='0'||str[0]=='\\') && tolower(str[1]) == 'x')
		(void) sscanf(str + 2, "%x", &hval);
	    else if (tolower(str[0]) == 'x')
		(void) sscanf(str + 1, "%x", &hval);
	    else if ((len >= 2) && (len <= 3) &&
		     (strspn(str,"0123456789ABCDEFabcdef") == (len-1)) &&
		     (tolower(str[len - 1]) == 'h'))
		(void) sscanf(str, "%x", &hval);

	/* accept 0onn, \onnn, onnn and \nnn forms for octal */
	if (oval == -1)
	    if ((str[0]=='0'||str[0]=='\\') && tolower(str[1]) == 'o')
		(void) sscanf(str + 2, "%o", &oval);
	    else if (tolower(str[0]) == 'o')
		(void) sscanf(str + 1, "%o", &oval);
	    else if (str[0] == '\\' && strspn(str + 1, "0123456789") == len - 1)
		(void) sscanf(str + 1, "%o", &oval);

	/* accept 0dnnn, \dnnn and dnnn forms for decimal */
	if (dval == -1)
	    if ((str[0]=='0'||str[0]=='\\') && tolower(str[1]) == 'd')
		(void) sscanf(str + 2, "%d", &dval);
	    else if (tolower(str[0]) == 'd')
		(void) sscanf(str + 1, "%d", &dval);

	/* accept 0bnnn, \bnnn and bnnn forms for binary */
	if (bval == -1)
	    if ((str[0]=='0'||str[0]=='\\') && tolower(str[1]) == 'b')
		bval = atob(str + 2);
	    else if (tolower(str[0]) == 'b')
		bval = atob(str + 1);

	/* OK, now output all values */
	if (hval > -1 && hval < 256)
	    speak(hval & 0xff);
	if (dval > -1 && dval < 256)
	    speak(dval & 0xff);
	if (oval > -1 && oval < 256)
	    speak(oval & 0xff);
	if (bval > -1 && bval < 256)
	    speak(bval & 0xff);
	if (!(hval==-1 && dval==-1 && oval==-1 && bval==-1))
	{
	    if (hval > -1 || dval > -1 || oval > -1 || bval > -1)
		return;
	    if (hval < 256 || dval < 256 || oval < 256 || bval < 256)
		return;
	}
    }

    if (sscanf(str, "%d/%d", &hi, &lo) == 2)    /* code table reference?  */
    { speak(hi*16 + lo); return; }
    else if (len > 1 && isalpha(str[0]))	/* try to match long names */
    {
	char	canbuf[BUFSIZ], *ep;
	int	i;

	/* map dashes and other junk to spaces */
	for (i = 0; i <= len; i++)
	    if (str[i] == '-' || isspace(str[i]))
		canbuf[i] = ' ';
	    else
		canbuf[i] = str[i];

	/* strip `sign' or `Sign' suffix */
	ep = canbuf + strlen(canbuf) - 4;
	if (!strcmp(ep, "sign") || !strcmp(ep, "Sign"))
	    *ep = '\0';

	/* remove any trailing whitespace */
	while (canbuf[strlen(canbuf) - 1] == ' ')
	    canbuf[strlen(canbuf) - 1] = '\0';

	/* look through all long names for a match */
	for (ch = 0; ch < 128; ch++)
	    for (ptr = &cnames[ch][1]; *ptr; ptr++)
		if (!stricmp(*ptr, canbuf))
		    speak(ch);
    } /* outer if */
}

void print_table(unsigned short delivery)
{
   static unsigned short i, j, len;
   char   separator[]= "   "; 
   char   *tail     = separator + 3 ;
   char   *space; 
   char   *name ;

   for (i=0;i<16; i++) {
           for (j=0; j<8; j++) {
                  name= *cnames[i+(j*16)];
                  len = strlen(name);
                  space = tail - (len % 3) ; 
                  switch (delivery) {
                      case decimal:  printf("%5d %1s%1s",i+(j*16),name,space);
                                     break;
                      case octal  :  printf("%5o %1s%1s",i+(j*16),name,space);
                                     break;
                      case hex    :  printf("%5X %1s%1s",i+(j*16),name,space);
                                     break;
                  }
           }
           printf("\n");
   }
       
}

int main(int argc, char **argv)
{
    char      *optstring="tshvxod" ;


    process_options(argc, argv, optstring) ;

    if (help || argc == optind)
	showHelp(stdout, argv[0]);
    else 
	while (optind < argc)	    
	    ascii(argv[optind++]);
    exit(0);
}

/******************************************************************/
/* ascii.c ends here */