* v1.2 March 1995 -- Fixed a core-dump bug
* v1.3 October 1995 -- Fixed a bug that shows up only under ELF.
* v2.0 December 1995 -- Interpret ISO-style code table escapes.
+ * v2.1 August 1997 -- nerge in changes by Ioannes Tambouras.
*
* Tries to interpret arguments as names or aliases of ascii characters
* and dumps out *all* the aliases of each. Accepts literal characters,
* remove the authorship notice and clearly mark your changes. Send me
* your improvements, please.
*/
+
+
+#define REVISION "$Revision$"
+
+#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
+#include <string.h>
typedef char *string;
+static unsigned char terse = 0;
+
static string cnames[128][15] =
{
{"NUL", "Null", (char *)NULL},
"Twiddle", "Enyay", (char *)NULL},
{"DEL", "Delete", (char *)NULL},
};
-
+/******************************************************************/
static int atob(str)
/* ASCII-to-binary conversion */
char *str;
return(val);
}
+/*********************************************************/
+void
+showHelp(FILE *out, char *progname)
+{
+ fprintf(out,"Usage: $ %s [options] char-alias...\n",progname);
+ fprintf(out,"Options:\n");
+ fprintf(out," -t \t\t Script-friendly mode; output is brief and terse\n");
+ fprintf(out," -h \t\t This help screen\n");
+ fprintf(out," -v \t\t Version info\n");
+ fprintf(out,"Description:\n");
+ fprintf(out,"Prints all aliases of a given ASCII character.
+Args may be single chars, C escape sequences, English names
+caret escapes for control characters, ASCII mnemonics,
+or numeric values in hex/octal/decimal/binary.\n");
+
+ exit(0);
+}
+
+/*********************************************************/
+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=1;
+ break;
+ case 'h':
+ showHelp(stdout, argv[0]) ;
+ break;
+ case 'v':
+ printf("%s\n",REVISION); exit(0) ;
+ break;
+ case '?':
+ default :
+ showHelp(stderr, argv[0]) ;
+ break;
+ } /*switch*/
+ }/*while*/
+}
+
+/******************************************************************/
static char *btoa(val)
/* binary-to-ASCII conversion */
{
#define BITSPERCHAR 8
char *rp;
- int i;
static char rep[BITSPERCHAR + 1];
/* write out external representation at least one char */
return(rp);
}
+/******************************************************************/
static void speak(ch)
/* list all the names for a given character */
int ch;
{
char **ptr = &cnames[ch][0];
+ if (terse) {
+ printf("%d/%d %d 0x%02X 0o%o %s\n",
+ ch / 16, ch % 16, ch, ch, ch, btoa(ch));
+ return;
+ }
+
(void) printf(
"ASCII %d/%d is decimal %03d, hex %02x, octal %03o, bits %s: ",
ch / 16, ch % 16, ch, ch, ch, btoa(ch));
(void) putchar('\n');
}
-
+/******************************************************************/
static int stricmp(s, t)
/* case-blind string compare */
register char *s, *t;
s++, t++;
return(*t - *s);
}
-
+/******************************************************************/
static void ascii(str)
char *str;
{
/* interpret single characters as themselves */
if (len == 1)
- speak(str[0]);
+ { speak(str[0]); return; }
/* interpret ^-escapes as control-character notation */
if (strcmp(str, "^?") == 0)
- speak(0x7F);
+ { speak(0x7F); return; }
else if (str[0] == '^' && len == 2)
- speak(str[1] & 0x1f);
+ { speak(str[1] & 0x1f); return; }
/* interpret C-style backslash escapes */
if (*str == '\\' && len == 2 && strchr("abfnrtv", str[1]))
- for (ch = 7; ch < 13; ch++)
+ for (ch = 7; ch < 14; ch++)
for (ptr = &cnames[ch][1]; *ptr; ptr++)
if (**ptr == '\\' && strcmp(str, *ptr) == 0)
- speak(ch);
+ { speak(ch); return; }
/* interpret 2 and 3-character ASCII control mnemonics */
if (len == 2 || len == 3)
{
/* first check for standard mnemonics */
if (strcmp(str, "DEL") == 0)
- speak(0x7f);
+ { speak(0x7f); return; }
if (strcmp(str, "BL") == 0)
- speak(' ');
+ { 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);
+ { speak(ch); return; }
}
/* OK, now try to interpret the string as a numeric */
/* OK, now output only unique values */
if (hval > -1)
- speak(hval & 0xff);
+ { speak(hval & 0xff); return; }
if (dval > -1 && dval != hval)
- speak(dval & 0xff);
+ { speak(dval & 0xff); return; }
if (oval > -1 && oval != dval && oval != hval)
- speak(oval & 0xff);
+ { speak(oval & 0xff); return; }
if (bval > -1 && bval != oval && bval != dval && bval != hval)
- speak(bval & 0xff);
+ { speak(bval & 0xff); return; }
}
if (sscanf(str, "%d/%d", &hi, &lo) == 2) /* code table reference? */
- speak(hi*16 + lo);
- else if (len >= 4 && isalpha(str[0])) /* try to match long names */
+ { speak(hi*16 + lo); return; }
+ else if (len > 1 && isalpha(str[0])) /* try to match long names */
{
char canbuf[BUFSIZ], *ep;
int i;
for (ch = 0; ch < 128; ch++)
for (ptr = &cnames[ch][1]; *ptr; ptr++)
if (!stricmp(*ptr, canbuf))
- speak(ch);
- }
+ { speak(ch); return; }
+ } /* outer if */
}
-
-main(argc, argv)
+/******************************************************************/
+/*----------------------------------------- M A I N --------------*/
+int main(argc, argv)
int argc;
char **argv;
{
- if (argc == 1)
- {
- (void) fprintf(stderr, "usage: ascii char-alias...\n");
- (void) fprintf(stderr,
- "\tPrints all aliases of a given ASCII character.\n\n");
- (void) fprintf(stderr,
- "\tArgs may be single chars, C escape sequences, English names,\n");
- (void) fprintf(stderr,
- "\tcaret escapes for control characters, ASCII mnemonics,\n");
- (void) fprintf(stderr,
- "\tor numeric values in hex/octal/decimal/binary.\n");
+ char *optstring="thv" ;
+
+
+ process_options(argc, argv, optstring) ;
+
+ if (argc == optind ) {
+ showHelp(stderr, argv[0]);
+ }else{
+ for(argc--, argv++ ; argc > 0; argv++, argc--)
+ ascii(*argv) ;
}
- else
- for(; argc > 0; argv++, argc--)
- ascii(*argv);
+ exit(0);
}
-
+/******************************************************************/
/* ascii.c ends here */