From 4d8b177ea1bf21c61df428fcd34f6b05bd07b0e1 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 11 Aug 1997 02:06:36 +0000 Subject: [PATCH] Merged in Ionnes Tambouras's changes. --- Makefile | 13 +++--- ascii.c | 132 ++++++++++++++++++++++++++++++++++++++--------------- ascii.spec | 4 +- ascii.xml | 15 +++++- 4 files changed, 118 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index eb366a3..a2a3c2b 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,8 @@ # ascii -- interactive ASCII reference # Note: When the version changes, you also have to change -# * the name of the containing directory -# * the RPM spec file -V=2.0 +# the RPM spec file +V=2.1 ascii: ascii.c @@ -12,14 +11,16 @@ CFLAGS = -O clean: rm -f ascii ascii.tar* ascii.shar -ascii-$(V).tar: Makefile ascii.c ascii.1 ascii.lsm - cd ..; tar cvf ascii-$(V)/ascii-$(V).tar ascii-$(V)/Makefile ascii-$(V)/ascii.c ascii-$(V)/ascii.1 ascii-$(V)/ascii.lsm ascii-$(V)/ascii.spec +SOURCES = Makefile ascii.c ascii.1 ascii.lsm scii.spec + +ascii-$(V).tar: $(SOURCES) + tar --name-prefix ascii-$(V) -cvf ascii-$(V).tar $(SOURCES) ascii-$(V).tar.gz: ascii-$(V).tar gzip -f ascii-$(V).tar ascii-$(V).shar: - shar Makefile ascii.c ascii.1 ascii.lsm ascii-$(V)-1.spec >ascii-$(V).shar + shar $(SOURCES) >ascii-$(V).shar rpm: ascii-$(V).tar.gz cp ascii-$(V).tar.gz /usr/src/SOURCES diff --git a/ascii.c b/ascii.c index 4aeb911..9a9d1cf 100644 --- a/ascii.c +++ b/ascii.c @@ -6,6 +6,7 @@ * 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, @@ -20,11 +21,19 @@ * remove the authorship notice and clearly mark your changes. Send me * your improvements, please. */ + + +#define REVISION "$Revision$" + +#include #include #include +#include typedef char *string; +static unsigned char terse = 0; + static string cnames[128][15] = { {"NUL", "Null", (char *)NULL}, @@ -244,7 +253,7 @@ static string cnames[128][15] = "Twiddle", "Enyay", (char *)NULL}, {"DEL", "Delete", (char *)NULL}, }; - +/******************************************************************/ static int atob(str) /* ASCII-to-binary conversion */ char *str; @@ -263,12 +272,56 @@ 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 */ @@ -287,12 +340,19 @@ static char *btoa(val) 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)); @@ -348,7 +408,7 @@ int ch; (void) putchar('\n'); } - +/******************************************************************/ static int stricmp(s, t) /* case-blind string compare */ register char *s, *t; @@ -357,7 +417,7 @@ register char *s, *t; s++, t++; return(*t - *s); } - +/******************************************************************/ static void ascii(str) char *str; { @@ -367,33 +427,33 @@ 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 */ @@ -443,18 +503,18 @@ char *str; /* 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; @@ -479,29 +539,27 @@ char *str; 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 */ diff --git a/ascii.spec b/ascii.spec index ac8ce95..d611827 100644 --- a/ascii.spec +++ b/ascii.spec @@ -1,8 +1,8 @@ Description: interactive ASCII name and synonym chart Name: ascii -Version: 2.0 +Version: 2.1 Release: 1 -Source: locke.ccil.org:/pub/esr/ascii-2.0.tar.gz +Source: locke.ccil.org:/pub/esr/ascii-2.1.tar.gz Copyright: distributable Group: Utilities/Text diff --git a/ascii.xml b/ascii.xml index c73eb91..712adb8 100644 --- a/ascii.xml +++ b/ascii.xml @@ -3,6 +3,18 @@ ascii -- report character aliases .SH SYNOPSIS ascii char-alias... +.SH OPTIONS +A summary of options are included below: +.TP +.B \-t +Script-friendly mode, emits only ISO/decimal/hex/octal/binary +encodings of the character. +.TP +.B \-h +Show summary of options. +.TP +.B \-v +Show version of program. .SH DESCRIPTION Characters in the ASCII set can have many aliases, depending on context. A character's possible names include: @@ -53,4 +65,5 @@ case-insensitive, and dashes are mapped to spaces. Eric S. Raymond November 1990 (home page at http://www.ccil.org/~esr/home.html). Reproduce, use, and modify as you like as long as you don't remove this authorship notice. - +Ioannis E. Tambouras added command options and minor +enhancements. -- 2.43.0