/* shchars.c */

/* compile me: cc -o shchars -lcurses shchars.c */

#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <term.h>

static unsigned char stoplist [256];

static void ruler (void);

int main (void)
{
    const char *term, *smacs, *rmacs, *acsc;
    int err, rc, i, j, c, len;

    term= getenv ("TERM");
    if (term) {
        fprintf (stderr, "TERM=%s\n", term);
    } else {
        fprintf (stderr, "TERM variable not set, exiting\n");
	return 4;
    }
    rc= setupterm ((char *)term, 1, &err);
    if (rc!=OK) {
        fprintf (stderr, "setupterm failed err=%d\n", err);
	return 4;
    }
    smacs= tigetstr ("smacs");
    rmacs= tigetstr ("rmacs");
    acsc = tigetstr ("acsc");
    if (smacs==NULL || smacs==(char *)-1 ||
        rmacs==NULL || rmacs==(char *)-1 ||
	acsc ==NULL || acsc ==(char *)-1) {
        fprintf (stderr, "could not get smacs, rmacs or acsc\n");
	return 8;
    }
    /* enable graphical characters */
    for (len= strlen (acsc), i=0; i<len-1; i+=2) {
        stoplist [(unsigned char)acsc[i+1]] &= ~2;
    }
    
    ruler ();
    for (i=0x00; i<=0xf0; i+=0x10) {
        printf ("%02x  ", i);
        for (j=0x0; j<=0xf; j++) {
	    c= i+j;
	    if (stoplist[c] & 1) printf (". ");
	    else                 printf ("%c ", c);
	}
	printf (" %02x  %s", i, smacs);
        for (j=0x0; j<=0xf; j++) {
	    c= i+j;
	    if (stoplist[c] & 2) printf (". ");
	    else                 printf ("%c ", c);
	}
	printf ("%s %02x\n", rmacs, i);
    }
    ruler ();
    return 0;
}

static void ruler (void)
{
    int j;
    
    printf ("    ");
    for (j=0; j<=0xf; ++j) {
        printf ("%x ", j);
    }
    printf ("     ");
    for (j=0; j<=0xf; ++j) {
        printf ("%x ", j);
    }
    printf ("\n");
}

/*  If the output of this program is broken,
 *  you may increase values of this table:
 *  1 means: dont show it as regular character,
 *  2 means: dont show it as graphic character,
 *  3 means: do not show it at all
 */
static unsigned char stoplist [256] = {
/* 00 */ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
/* 10 */ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
/* 20 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 60 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,
/* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 90 */ 0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,
/* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* c0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* d0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* f0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
