Run through clang-format.

This commit is contained in:
David Given 2018-06-23 23:46:44 +02:00
parent fd94e219d4
commit 205c8d0a35

View file

@ -15,44 +15,47 @@
#include <string.h> #include <string.h>
#ifndef NORCSID #ifndef NORCSID
static char *RcsId = "$Id$"; static char* RcsId = "$Id$";
#endif #endif
#define MAXBUF 256 #define MAXBUF 256
#define MAXTAB 10000 #define MAXTAB 10000
#define COMCOM '-' #define COMCOM '-'
#define FILECOM '%' #define FILECOM '%'
int InputForm = 'c'; /* default input format (and, currently, only one) */ int InputForm = 'c'; /* default input format (and, currently, only one) */
char OutputForm[MAXBUF] = "%s,\n"; char OutputForm[MAXBUF] = "%s,\n";
/* format for spitting out a string */ /* format for spitting out a string */
char *Table[MAXTAB]; char* Table[MAXTAB];
char *ProgCall; /* callname of this program */ char* ProgCall; /* callname of this program */
int TabSize = 128; /* default size of generated table */ int TabSize = 128; /* default size of generated table */
char *InitialValue; /* initial value of all table entries */ char* InitialValue; /* initial value of all table entries */
void option(char *); void option(char*);
void option_F(char *); void option_F(char*);
void InitTable(char *); void InitTable(char*);
void PrintTable(void); void PrintTable(void);
int process(char *, int); int process(char*, int);
int c_proc(char *, char *); int c_proc(char*, char*);
int setval(int, char *); int setval(int, char*);
int quoted(char **); int quoted(char**);
void DoFile(char *); void DoFile(char*);
int int main(int argc, char* argv[])
main(int argc, char *argv[])
{ {
ProgCall = *argv++; ProgCall = *argv++;
argc--; argc--;
while (argc-- > 0) { while (argc-- > 0)
if (**argv == COMCOM) { {
if (**argv == COMCOM)
{
option(*argv++); option(*argv++);
} }
else { else
if (! process(*argv++, InputForm)) { {
if (!process(*argv++, InputForm))
{
exit(1); exit(1);
} }
} }
@ -61,81 +64,85 @@ main(int argc, char *argv[])
/*NOTREACHED*/ /*NOTREACHED*/
} }
char * char* Salloc(char* s)
Salloc(char *s)
{ {
char *ns = strdup(s); char* ns = strdup(s);
if (!ns) { if (!ns)
{
fprintf(stderr, "%s: out of memory\n", ProgCall); fprintf(stderr, "%s: out of memory\n", ProgCall);
exit(1); exit(1);
} }
return ns; return ns;
} }
void void option(char* str)
option(char *str)
{ {
/* note that *str indicates the source of the option: /* note that *str indicates the source of the option:
either COMCOM (from command line) or FILECOM (from a file). either COMCOM (from command line) or FILECOM (from a file).
*/ */
switch (*++str) { 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 */
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) { case ' ': /* command */
fprintf(stderr, "%s: size would exceed maximum\n", case '\t':
ProgCall); 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;
} }
else { default:
TabSize = i; fprintf(stderr, "%s: bad option -%s\n", ProgCall, str);
}
break;
}
default:
fprintf(stderr, "%s: bad option -%s\n", ProgCall, str);
} }
} }
void void option_F(char* form)
option_F(char *form)
{ {
int len; int len;
char *cp; char* cp;
/* /*
* The format string must have one '%s' and no other '%'. * The format string must have one '%s' and no other '%'.
@ -154,180 +161,203 @@ bad:
exit(1); exit(1);
} }
void void InitTable(char* ival)
InitTable(char *ival)
{ {
int i; int i;
for (i = 0; i < TabSize; i++) { for (i = 0; i < TabSize; i++)
{
Table[i] = 0; Table[i] = 0;
} }
InitialValue = 0; InitialValue = 0;
if (ival) { if (ival)
{
InitialValue = Salloc(ival); InitialValue = Salloc(ival);
} }
} }
void void PrintTable(void)
PrintTable(void)
{ {
int i; int i;
for (i = 0; i < TabSize; i++) { for (i = 0; i < TabSize; i++)
if (Table[i]) { {
if (Table[i])
{
printf(OutputForm, Table[i]); printf(OutputForm, Table[i]);
} }
else if (InitialValue) { else if (InitialValue)
{
printf(OutputForm, InitialValue); printf(OutputForm, InitialValue);
} }
else { else
{
printf(OutputForm, "0"); printf(OutputForm, "0");
} }
} }
} }
int int process(char* str, int format)
process(char *str, int format)
{ {
char *cstr = str; char* cstr = str;
char *Name = cstr; /* overwrite original string! */ char* Name = cstr; /* overwrite original string! */
/* strip of the entry name /* strip of the entry name
*/ */
while (*str && *str != ':') { while (*str && *str != ':')
if (*str == '\\') { {
if (*str == '\\')
{
++str; ++str;
} }
*cstr++ = *str++; *cstr++ = *str++;
} }
if (*str != ':') { if (*str != ':')
{
fprintf(stderr, "%s: bad specification: \"%s\", ignored\n", fprintf(stderr, "%s: bad specification: \"%s\", ignored\n",
ProgCall, Name); ProgCall, Name);
return 0; return 0;
} }
*cstr = '\0'; *cstr = '\0';
str++; str++;
switch (format) { switch (format)
{
case 'c': case 'c':
return c_proc(str, Name); return c_proc(str, Name);
default: default:
fprintf(stderr, "%s: bad input format\n", ProgCall); fprintf(stderr, "%s: bad input format\n", ProgCall);
} }
return 0; return 0;
} }
int int c_proc(char* str, char* Name)
c_proc(char *str, char *Name)
{ {
int ch, ch2; int ch, ch2;
char *name = Salloc(Name); char* name = Salloc(Name);
while (*str) { while (*str)
if (*str == '\\') { {
if (*str == '\\')
{
ch = quoted(&str); ch = quoted(&str);
} }
else { else
{
ch = *str++ & 0377; ch = *str++ & 0377;
} }
if (*str == '-') { if (*str == '-')
if (*++str == '\\') { {
if (*++str == '\\')
{
ch2 = quoted(&str); ch2 = quoted(&str);
} }
else { else
{
ch2 = (*str++ & 0377); ch2 = (*str++ & 0377);
if (!ch2) str--; if (!ch2)
str--;
} }
if (ch > ch2) { if (ch > ch2)
{
fprintf(stderr, "%s: bad range\n", ProgCall); fprintf(stderr, "%s: bad range\n", ProgCall);
return 0; return 0;
} }
while (ch <= ch2) { while (ch <= ch2)
if (! setval(ch, name)) return 0; {
if (!setval(ch, name))
return 0;
ch++; ch++;
} }
} }
else { else
if (! setval(ch, name)) return 0; {
if (!setval(ch, name))
return 0;
} }
} }
return 1; return 1;
} }
int int setval(int ch, char* nm)
setval(int ch, char *nm)
{ {
char **p = &Table[ch]; char** p = &Table[ch];
if (ch < 0 || ch >= TabSize) { if (ch < 0 || ch >= TabSize)
{
fprintf(stderr, "Illegal index: %d\n", ch); fprintf(stderr, "Illegal index: %d\n", ch);
return 0; return 0;
} }
if (*(p = &Table[ch])) { if (*(p = &Table[ch]))
{
fprintf(stderr, "Warning: redefinition of index %d\n", ch); fprintf(stderr, "Warning: redefinition of index %d\n", ch);
} }
*p = nm; *p = nm;
return 1; return 1;
} }
int int quoted(char** pstr)
quoted(char **pstr)
{ {
int ch; int ch;
int i; int i;
char *str = *pstr; char* str = *pstr;
if ((*++str >= '0') && (*str <= '9')) { if ((*++str >= '0') && (*str <= '9'))
{
ch = 0; ch = 0;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++)
{
ch = 8 * ch + (*str - '0'); ch = 8 * ch + (*str - '0');
if (*++str < '0' || *str > '9') if (*++str < '0' || *str > '9')
break; break;
} }
} }
else { else
switch (*str++) { {
case 'n': switch (*str++)
ch = '\n'; {
break; case 'n':
case 't': ch = '\n';
ch = '\t'; break;
break; case 't':
case 'b': ch = '\t';
ch = '\b'; break;
break; case 'b':
case 'r': ch = '\b';
ch = '\r'; break;
break; case 'r':
case 'f': ch = '\r';
ch = '\f'; break;
break; case 'f':
case 'v': ch = '\f';
ch = 013; break;
break; case 'v':
default : ch = 013;
ch = *(str - 1); break;
break; default:
ch = *(str - 1);
break;
} }
} }
*pstr = str; *pstr = str;
return ch & 0377; return ch & 0377;
} }
char * char* getln(char* s, int n, FILE* fp)
getln(char *s, int n, FILE *fp)
{ {
int c = getc(fp); int c = getc(fp);
char *str = s; char* str = s;
while (n--) { while (n--)
if (c == EOF) { {
if (c == EOF)
{
return NULL; return NULL;
} }
else else if (c == '\n')
if (c == '\n') { {
*str++ = '\0'; *str++ = '\0';
return s; return s;
} }
@ -340,22 +370,26 @@ getln(char *s, int n, FILE *fp)
#define BUFSIZE 1024 #define BUFSIZE 1024
void void DoFile(char* name)
DoFile(char *name)
{ {
char text[BUFSIZE]; char text[BUFSIZE];
FILE *fp; FILE* fp;
if ((fp = fopen(name, "r")) == NULL) { if ((fp = fopen(name, "r")) == NULL)
{
fprintf(stderr, "%s: cannot read file %s\n", ProgCall, name); fprintf(stderr, "%s: cannot read file %s\n", ProgCall, name);
exit(1); exit(1);
} }
while (getln(text, BUFSIZE, fp) != NULL) { while (getln(text, BUFSIZE, fp) != NULL)
if (text[0] == FILECOM) { {
if (text[0] == FILECOM)
{
option(text); option(text);
} }
else { else
if (! process(text, InputForm)) { {
if (!process(text, InputForm))
{
exit(1); exit(1);
} }
} }