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
|
|
|
|
*/
|
|
|
|
|
2017-11-14 01:40:43 +00:00
|
|
|
#include <ctype.h>
|
1997-05-15 12:03:05 +00:00
|
|
|
#include <stdio.h>
|
2017-11-14 01:40:43 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-18 17:10:29 +00:00
|
|
|
#include <string.h>
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
#ifndef NORCSID
|
2018-06-23 21:46:44 +00:00
|
|
|
static char* RcsId = "$Id$";
|
1988-11-16 09:37:04 +00:00
|
|
|
#endif
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
#define MAXBUF 256
|
1988-11-16 09:37:04 +00:00
|
|
|
#define MAXTAB 10000
|
2018-06-23 21:46:44 +00:00
|
|
|
#define COMCOM '-'
|
|
|
|
#define FILECOM '%'
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
int InputForm = 'c'; /* default input format (and, currently, only one) */
|
1988-11-16 09:37:04 +00:00
|
|
|
char OutputForm[MAXBUF] = "%s,\n";
|
2018-06-23 21:46:44 +00:00
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
void option(char*);
|
|
|
|
void option_F(char*);
|
|
|
|
void InitTable(char*);
|
2017-11-14 01:40:43 +00:00
|
|
|
void PrintTable(void);
|
2018-06-23 21:46:44 +00:00
|
|
|
int process(char*, int);
|
|
|
|
int c_proc(char*, char*);
|
|
|
|
int setval(int, char*);
|
|
|
|
int quoted(char**);
|
|
|
|
void DoFile(char*);
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
ProgCall = *argv++;
|
|
|
|
argc--;
|
2018-06-23 21:46:44 +00:00
|
|
|
while (argc-- > 0)
|
|
|
|
{
|
|
|
|
if (**argv == COMCOM)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
option(*argv++);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!process(*argv++, InputForm))
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
1991-08-27 09:41:49 +00:00
|
|
|
/*NOTREACHED*/
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
char* Salloc(char* s)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
2018-06-23 21:46:44 +00:00
|
|
|
char* ns = strdup(s);
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
if (!ns)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
fprintf(stderr, "%s: out of memory\n", ProgCall);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
void option(char* str)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
|
|
|
/* note that *str indicates the source of the option:
|
|
|
|
either COMCOM (from command line) or FILECOM (from a file).
|
|
|
|
*/
|
2018-06-23 21:46:44 +00:00
|
|
|
switch (*++str)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
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 */
|
|
|
|
option_F(++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':
|
|
|
|
{
|
|
|
|
int i = atoi(++str);
|
|
|
|
|
|
|
|
if (i <= 0 || i > MAXTAB)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: size would exceed maximum\n",
|
|
|
|
ProgCall);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TabSize = i;
|
|
|
|
}
|
|
|
|
break;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: bad option -%s\n", ProgCall, str);
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
void option_F(char* form)
|
2017-11-14 01:40:43 +00:00
|
|
|
{
|
|
|
|
int len;
|
2018-06-23 21:46:44 +00:00
|
|
|
char* cp;
|
2017-11-14 01:40:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The format string must have one '%s' and no other '%'.
|
|
|
|
* Don't allow '%99$s', '%ls', '%n'.
|
|
|
|
*/
|
|
|
|
cp = strchr(form, '%');
|
|
|
|
if (!cp || cp[1] != 's' || strchr(cp + 1, '%'))
|
|
|
|
goto bad;
|
|
|
|
len = snprintf(OutputForm, sizeof(OutputForm), "%s\n", form);
|
|
|
|
if (len < 0 && len >= sizeof(OutputForm))
|
|
|
|
goto bad;
|
|
|
|
return;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
fprintf(stderr, "%s: -F: bad format %s\n", ProgCall, form);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
void InitTable(char* ival)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int i;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
for (i = 0; i < TabSize; i++)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
Table[i] = 0;
|
|
|
|
}
|
|
|
|
InitialValue = 0;
|
2018-06-23 21:46:44 +00:00
|
|
|
if (ival)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
InitialValue = Salloc(ival);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
void PrintTable(void)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int i;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
for (i = 0; i < TabSize; i++)
|
|
|
|
{
|
|
|
|
if (Table[i])
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
printf(OutputForm, Table[i]);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else if (InitialValue)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
printf(OutputForm, InitialValue);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
printf(OutputForm, "0");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
int process(char* str, int format)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
2018-06-23 21:46:44 +00:00
|
|
|
char* cstr = str;
|
|
|
|
char* Name = cstr; /* overwrite original string! */
|
1988-11-16 09:37:04 +00:00
|
|
|
|
|
|
|
/* strip of the entry name
|
|
|
|
*/
|
2018-06-23 21:46:44 +00:00
|
|
|
while (*str && *str != ':')
|
|
|
|
{
|
|
|
|
if (*str == '\\')
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
++str;
|
|
|
|
}
|
|
|
|
*cstr++ = *str++;
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
if (*str != ':')
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
fprintf(stderr, "%s: bad specification: \"%s\", ignored\n",
|
2018-06-23 21:46:44 +00:00
|
|
|
ProgCall, Name);
|
1988-11-16 09:37:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*cstr = '\0';
|
|
|
|
str++;
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
case 'c':
|
|
|
|
return c_proc(str, Name);
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: bad input format\n", ProgCall);
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
int c_proc(char* str, char* Name)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
|
|
|
int ch, ch2;
|
2018-06-23 21:46:44 +00:00
|
|
|
char* name = Salloc(Name);
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (*str == '\\')
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
ch = quoted(&str);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else
|
|
|
|
{
|
1991-08-27 09:41:49 +00:00
|
|
|
ch = *str++ & 0377;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
if (*str == '-')
|
|
|
|
{
|
|
|
|
if (*++str == '\\')
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
ch2 = quoted(&str);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else
|
|
|
|
{
|
2017-11-14 01:40:43 +00:00
|
|
|
ch2 = (*str++ & 0377);
|
2018-06-23 21:46:44 +00:00
|
|
|
if (!ch2)
|
|
|
|
str--;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
if (ch > ch2)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
fprintf(stderr, "%s: bad range\n", ProgCall);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
while (ch <= ch2)
|
|
|
|
{
|
|
|
|
if (!setval(ch, name))
|
|
|
|
return 0;
|
1988-11-16 09:37:04 +00:00
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!setval(ch, name))
|
|
|
|
return 0;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
int setval(int ch, char* nm)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
2018-06-23 21:46:44 +00:00
|
|
|
char** p = &Table[ch];
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
if (ch < 0 || ch >= TabSize)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
fprintf(stderr, "Illegal index: %d\n", ch);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-23 21:46:44 +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;
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
int quoted(char** pstr)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int ch;
|
|
|
|
int i;
|
2018-06-23 21:46:44 +00:00
|
|
|
char* str = *pstr;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
if ((*++str >= '0') && (*str <= '9'))
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
ch = 0;
|
2018-06-23 21:46:44 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
ch = 8 * ch + (*str - '0');
|
|
|
|
if (*++str < '0' || *str > '9')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
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;
|
|
|
|
case 'v':
|
|
|
|
ch = 013;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ch = *(str - 1);
|
|
|
|
break;
|
1988-11-16 09:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*pstr = str;
|
|
|
|
return ch & 0377;
|
|
|
|
}
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
char* getln(char* s, int n, FILE* fp)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
1994-12-06 09:12:21 +00:00
|
|
|
int c = getc(fp);
|
2018-06-23 21:46:44 +00:00
|
|
|
char* str = s;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
while (n--)
|
|
|
|
{
|
|
|
|
if (c == EOF)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else if (c == '\n')
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
*str++ = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
*str++ = c;
|
|
|
|
c = getc(fp);
|
|
|
|
}
|
|
|
|
s[n - 1] = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
void DoFile(char* name)
|
1988-11-16 09:37:04 +00:00
|
|
|
{
|
|
|
|
char text[BUFSIZE];
|
2018-06-23 21:46:44 +00:00
|
|
|
FILE* fp;
|
1988-11-16 09:37:04 +00:00
|
|
|
|
2018-06-23 21:46:44 +00:00
|
|
|
if ((fp = fopen(name, "r")) == NULL)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
fprintf(stderr, "%s: cannot read file %s\n", ProgCall, name);
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
while (getln(text, BUFSIZE, fp) != NULL)
|
|
|
|
{
|
|
|
|
if (text[0] == FILECOM)
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
option(text);
|
|
|
|
}
|
2018-06-23 21:46:44 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!process(text, InputForm))
|
|
|
|
{
|
1988-11-16 09:37:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|