1988-11-16 09:37:04 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
/*
|
1997-05-15 12:03:05 +00:00
|
|
|
chtabgen - character table generator
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
Author: Erik Baalbergen (..tjalk!erikb)
|
|
|
|
Many mods by Ceriel Jacobs
|
|
|
|
*/
|
|
|
|
|
2006-07-18 17:10:29 +00:00
|
|
|
#include <stdlib.h>
|
1997-05-15 12:03:05 +00:00
|
|
|
#include <stdio.h>
|
2006-07-18 17:10:29 +00:00
|
|
|
#include <string.h>
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
#ifndef NORCSID
|
1994-03-11 10:37:39 +00:00
|
|
|
static char *RcsId = "$Id$";
|
1988-11-16 09:37:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAXBUF 256
|
|
|
|
#define MAXTAB 10000
|
|
|
|
#define COMCOM '-'
|
|
|
|
#define FILECOM '%'
|
|
|
|
|
|
|
|
int InputForm = 'c'; /* default input format (and, currently, only one) */
|
|
|
|
char OutputForm[MAXBUF] = "%s,\n";
|
|
|
|
/* format for spitting out a string */
|
|
|
|
char *Table[MAXTAB];
|
|
|
|
char *ProgCall; /* callname of this program */
|
|
|
|
int TabSize = 128; /* default size of generated table */
|
|
|
|
char *InitialValue; /* initial value of all table entries */
|
|
|
|
|
|
|
|
main(argc, argv)
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
|
|
|
|
ProgCall = *argv++;
|
|
|
|
argc--;
|
|
|
|
while (argc-- > 0) {
|
|
|
|
if (**argv == COMCOM) {
|
|
|
|
option(*argv++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (! process(*argv++, InputForm)) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
1991-08-27 09:41:49 +00:00
|
|
|
/*NOTREACHED*/
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
Salloc(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
char *ns = malloc((unsigned)strlen(s) + 1);
|
|
|
|
|
|
|
|
if (ns) {
|
|
|
|
strcpy(ns, s);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "%s: out of memory\n", ProgCall);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
option(str)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
/* note that *str indicates the source of the option:
|
|
|
|
either COMCOM (from command line) or FILECOM (from a file).
|
|
|
|
*/
|
|
|
|
switch (*++str) {
|
|
|
|
|
|
|
|
case ' ': /* command */
|
|
|
|
case '\t':
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
case 'I': /* for future extension */
|
|
|
|
InputForm = *++str;
|
|
|
|
break;
|
|
|
|
case 'f': /* input from file ... */
|
|
|
|
if (*++str == '\0') {
|
|
|
|
fprintf(stderr, "%s: -f: name expected\n", ProgCall);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
DoFile(str);
|
|
|
|
break;
|
|
|
|
case 'F': /* new output format string */
|
|
|
|
sprintf(OutputForm, "%s\n", ++str);
|
|
|
|
break;
|
|
|
|
case 'T': /* insert text literally */
|
|
|
|
printf("%s\n", ++str);
|
|
|
|
break;
|
|
|
|
case 'p': /* print table */
|
|
|
|
PrintTable();
|
|
|
|
break;
|
|
|
|
case 'C': /* clear table */
|
|
|
|
InitTable((char *)0);
|
|
|
|
break;
|
|
|
|
case 'i': /* initialize table with given value */
|
|
|
|
if (*++str == '\0') {
|
|
|
|
InitTable((char *)0);
|
|
|
|
}
|
|
|
|
else InitTable(str);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int i = atoi(++str);
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
if (i <= 0 || i > MAXTAB) {
|
|
|
|
fprintf(stderr, "%s: size would exceed maximum\n",
|
|
|
|
ProgCall);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TabSize = i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: bad option -%s\n", ProgCall, str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InitTable(ival)
|
|
|
|
char *ival;
|
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int i;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
for (i = 0; i < TabSize; i++) {
|
|
|
|
Table[i] = 0;
|
|
|
|
}
|
|
|
|
InitialValue = 0;
|
|
|
|
if (ival) {
|
|
|
|
InitialValue = Salloc(ival);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintTable()
|
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int i;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
for (i = 0; i < TabSize; i++) {
|
|
|
|
if (Table[i]) {
|
|
|
|
printf(OutputForm, Table[i]);
|
|
|
|
}
|
|
|
|
else if (InitialValue) {
|
|
|
|
printf(OutputForm, InitialValue);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(OutputForm, "0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
process(str, format)
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
char *cstr = str;
|
|
|
|
char *Name = cstr; /* overwrite original string! */
|
|
|
|
|
|
|
|
/* strip of the entry name
|
|
|
|
*/
|
|
|
|
while (*str && *str != ':') {
|
|
|
|
if (*str == '\\') {
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
*cstr++ = *str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*str != ':') {
|
|
|
|
fprintf(stderr, "%s: bad specification: \"%s\", ignored\n",
|
|
|
|
ProgCall, Name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*cstr = '\0';
|
|
|
|
str++;
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
return c_proc(str, Name);
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: bad input format\n", ProgCall);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
c_proc(str, Name)
|
|
|
|
char *str;
|
|
|
|
char *Name;
|
|
|
|
{
|
|
|
|
int ch, ch2;
|
|
|
|
int quoted();
|
|
|
|
char *name = Salloc(Name);
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
if (*str == '\\') {
|
|
|
|
ch = quoted(&str);
|
|
|
|
}
|
|
|
|
else {
|
1991-08-27 09:41:49 +00:00
|
|
|
ch = *str++ & 0377;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
if (*str == '-') {
|
|
|
|
if (*++str == '\\') {
|
|
|
|
ch2 = quoted(&str);
|
|
|
|
}
|
|
|
|
else {
|
1991-08-27 09:41:49 +00:00
|
|
|
if (ch2 = (*str++ & 0377));
|
1988-11-16 09:37:04 +00:00
|
|
|
else str--;
|
|
|
|
}
|
|
|
|
if (ch > ch2) {
|
|
|
|
fprintf(stderr, "%s: bad range\n", ProgCall);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (ch <= ch2) {
|
|
|
|
if (! setval(ch, name)) return 0;
|
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (! setval(ch, name)) return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
setval(ch, nm)
|
|
|
|
char *nm;
|
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
char **p = &Table[ch];
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
if (ch < 0 || ch >= TabSize) {
|
|
|
|
fprintf(stderr, "Illegal index: %d\n", ch);
|
|
|
|
return 0;
|
|
|
|
}
|
1991-08-27 09:41:49 +00:00
|
|
|
if (*(p = &Table[ch])) {
|
1988-11-16 09:37:04 +00:00
|
|
|
fprintf(stderr, "Warning: redefinition of index %d\n", ch);
|
|
|
|
}
|
|
|
|
*p = nm;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
quoted(pstr)
|
|
|
|
char **pstr;
|
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int ch;
|
|
|
|
int i;
|
|
|
|
char *str = *pstr;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
if ((*++str >= '0') && (*str <= '9')) {
|
|
|
|
ch = 0;
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
ch = 8 * ch + (*str - '0');
|
|
|
|
if (*++str < '0' || *str > '9')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (*str++) {
|
|
|
|
case 'n':
|
|
|
|
ch = '\n';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
ch = '\t';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
ch = '\b';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ch = '\r';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
ch = '\f';
|
|
|
|
break;
|
1989-12-06 12:38:18 +00:00
|
|
|
case 'v':
|
1991-11-06 14:36:25 +00:00
|
|
|
ch = 013;
|
1989-12-06 12:38:18 +00:00
|
|
|
break;
|
1988-11-16 09:37:04 +00:00
|
|
|
default :
|
1989-12-06 12:38:18 +00:00
|
|
|
ch = *(str - 1);
|
|
|
|
break;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*pstr = str;
|
|
|
|
return ch & 0377;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
getline(s, n, fp)
|
|
|
|
char *s;
|
|
|
|
FILE *fp;
|
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int c = getc(fp);
|
1988-11-16 09:37:04 +00:00
|
|
|
char *str = s;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
if (c == EOF) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (c == '\n') {
|
|
|
|
*str++ = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
*str++ = c;
|
|
|
|
c = getc(fp);
|
|
|
|
}
|
|
|
|
s[n - 1] = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
|
|
|
DoFile(name)
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
char text[BUFSIZE];
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen(name, "r")) == NULL) {
|
|
|
|
fprintf(stderr, "%s: cannot read file %s\n", ProgCall, name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while (getline(text, BUFSIZE, fp) != NULL) {
|
|
|
|
if (text[0] == FILECOM) {
|
|
|
|
option(text);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (! process(text, InputForm)) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|