/* * $Id: //websites/unixwiz/newroot/tools/achart.c#2 $ * * written by: Stephen J. Friedl * Tustin, CA 92780 * steve@unixwiz.net * * This is a self-contained ASCII chart program. It takes a base as * an argument and prints a chart with the lower 128 ASCII chars. * The default base is decimal, but an alternate base of ( 8, 10 or * 16 ) can be specified as well. If the indicated base is prefixed * with a minus sign, the lower control characters are displayed with * their mnemonics (NUL, EOT, etc.) instead of their control-character * representation. */ #include #include /*------------------------------------------------------------------------ * this little table contains the representations of all the characters * in the lower part of the set. For the time being, we don't care about * the chars >= 128. */ static const char *prtchar[] = { "^@", "^A", "^B", "^C", "^D", "^E", "^F", "^G", "^H", "^I", "^J", "^K", "^L", "^M", "^N", "^O", "^P", "^Q", "^R", "^S", "^T", "^U", "^V", "^W", "^X", "^Y", "^Z", "^[", "^\\", "^]", "^^", "^_", "SP", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "^?" }; static void add_mnemonics(void); int main(int argc, char **argv) { char *base_name; /* printable name of the base */ int i, ccol = 0, /* current column counter */ maxcols = 8, /* total number of columns allowed */ base = 10; /* current base of interest */ /*---------------------------------------------------------------- * if we have a base specified, pick it apart. Note that we don't * check for excess args here... */ if (argc > 1) base = atoi(argv[1]); /*---------------------------------------------------------------- * if the base is less than zero, use the alternate display format. */ if (base < 0) add_mnemonics(), base = -base; /*---------------------------------------------------------------- * validate the base and make sure we have something valid. We * also determine the base string. */ switch (base) { case 10: base_name = "decimal"; break; case 16: base_name = "hex"; break; case 8: base_name = "octal"; break; default: fprintf(stderr, "%s: base must be 10, 16, or 8\n", argv[0]); return EXIT_FAILURE; } /*---------------------------------------------------------------- * print a header to remind the user what we are doing */ printf("\nASCII chart, %s representation\n\n", base_name); /*---------------------------------------------------------------- * now run through the chart. We do this in 16 rows of 8 chars each. */ for (i = 0; i < 128; i++) { /*-------------------------------------------------------- * if this is not the first column, we need the separator * between it and the previosu column. */ if (ccol++ > 0) printf(" | "); /*-------------------------------------------------------- * now actualy do the output of the character's index */ switch (base) { case 10: printf("%3d", i); break; case 8: printf("%03o", i); break; case 16: printf("%02X", i); break; } /*-------------------------------------------------------- * now put the name of the character */ printf(" %3s", prtchar[i]); /*-------------------------------------------------------- * If we are at the end of a line, kick out a newline. */ if (ccol >= maxcols) putchar('\n'), ccol = 0; } putchar('\n'); return EXIT_SUCCESS; } /* * add_mnemonics() * * Run through the prtchar[] table and stuff in the ASCII control * code abbreviations. */ static void add_mnemonics(void) { prtchar[ 0] = "NUL"; prtchar[ 1] = "SOH"; prtchar[ 2] = "STX"; prtchar[ 3] = "ETX"; prtchar[ 4] = "EOT"; prtchar[ 5] = "ENQ"; prtchar[ 6] = "ACK"; prtchar[ 7] = "BEL"; prtchar[ 8] = "BS"; prtchar[ 9] = "HT"; prtchar[ 10] = "LF"; prtchar[ 11] = "VT"; prtchar[ 12] = "FF"; prtchar[ 13] = "CR"; prtchar[ 14] = "SO"; prtchar[ 15] = "SI"; prtchar[ 16] = "DLE"; prtchar[ 17] = "DC1"; prtchar[ 18] = "DC2"; prtchar[ 19] = "DC3"; prtchar[ 20] = "DC4"; prtchar[ 21] = "NAK"; prtchar[ 22] = "SYN"; prtchar[ 23] = "ETB"; prtchar[ 24] = "CAN"; prtchar[ 25] = "EM"; prtchar[ 26] = "SUB"; prtchar[ 27] = "ESC"; prtchar[ 28] = "FS"; prtchar[ 29] = "GS"; prtchar[ 30] = "RS"; prtchar[ 31] = "US"; prtchar[127] = "DEL"; }