/* chars.c - Display the character set. Display characters with or without their numbers, using decimal, hex or both. Accepts only one argument: d: use decimal numbers (default) x: use hexadecimal numbers l: both dec and hex numbers, characters 0 to 127 h: both dec and hex numbers, characters 128 to 255 a: both dec and hex numbers, all characters c: characters only s: characters only, space in-between Jason Hood, 30 October, 1998. Public Domain. 14 & 15 October, 2003, v1.00: recognise current background color; use normal output if redirected. */ #define PVERS "1.00" #define PDATE "15 October, 2003" #include #include void disp_char( int chr, int attr ); int get_bkgd( void ); void help( char* program ); #define LOW 1 #define HIGH 2 #define ALL 3 #define CHARS 4 #define SPACE 5 #define FIRST 15 // Display characters in alternating colors: #define SECOND 10 // bright white and bright green #define SWITCH (FIRST ^ SECOND) int redir; // Non-zero if output has been redirected int main( int argc, char* argv[] ) { int dec = 0, hex = 0, fmt = 0, both; int beg, end, wid; int color = FIRST; int j, k; if (argc > 1) { j = 0; if (argv[1][0] == '-' || argv[1][0] == '/') ++j; switch (argv[1][j] | 0x20) { case 'd': dec = 1; break; case 'x': hex = 1; break; case 'l': fmt = LOW; break; case 'h': fmt = HIGH; break; case 'a': fmt = ALL; break; case 'c': fmt = CHARS; break; case 's': fmt = SPACE; break; default: help( argv[0] ); return 0; } } if (!dec && !hex && !fmt) dec = 1; both = (fmt == LOW || fmt == HIGH || fmt == ALL); if (both) dec = hex = 1; beg = (fmt == HIGH) ? 128 : 0; end = (fmt == LOW) ? 127 : 255; wid = (fmt == 0) ? 13 : (both) ? 8 : 32; redir = !isatty( 1 ); if (!redir) { putchar( '\n' ); color |= get_bkgd(); } else if (beg == 0) beg = 32; k = 0; for (j = beg; j <= end; ++j) { if (dec) printf( "%3d ", j ); if (hex) { if (!dec) putchar( ' ' ); printf( "%02x ", j ); } disp_char( j, color ); color ^= SWITCH; if (++k == wid) { k = 0; putchar( '\n' ); if (fmt != 0) color ^= SWITCH; } else if (fmt != CHARS) { putchar( ' ' ); if (both) putchar( ' ' ); } } if (fmt == 0) putchar( '\n' ); return 0; } // Display a character using Video BIOS. // Increases cursor position, but does not wrap. void disp_char( int chr, int attr ) { if (redir) putchar( chr ); else asm { mov bh, 0 // I'll assume page 0 mov ah, 3 // Get Cursor Position int 0x10 mov ah, 0x09 // Display Character mov al, byte ptr chr // Get the character mov bl, byte ptr attr // and attribute mov cx, 1 // Just the one int 0x10 inc dl // Next column mov ah, 2 // Set Cursor Position int 0x10 } } // Determine the background color. int get_bkgd( void ) { fputs( " \b", stdout ); asm { mov bh, 0 // I'll assume page 0 mov ah, 8 // Get Char and Attr at Cursor Position int 0x10 and ah, 0xf0 // Strip off foreground } return _AH; } void help( char* program ) { printf( "Characters by Jason Hood .\n" "Version "PVERS" ("PDATE"). Public Domain.\n" "http://misc.adoxa.cjb.net/\n" "\n" "Display the current character set, with or without character codes.\n" "\n" "Usage: %s [-|/][d | x | l | h | a | c | s]\n" "\n" " d: use decimal numbers (default)\n" " x: use hexadecimal numbers\n" " l: both dec and hex numbers, characters 0 to 127\n" " h: both dec and hex numbers, characters 128 to 255\n" " a: both dec and hex numbers, all characters\n" " c: characters only\n" " s: characters only, space in-between\n" , program ); }