Check each format string in tabgen.c

Silence warning from clang at `if (ch2 = ...)`

Delete `|| rm %{outs}` in build.lua, because it hid the exit status of
tabgen, so if tabgen failed, the build continued and failed later.
This commit is contained in:
George Koehler 2017-11-13 20:40:43 -05:00
parent 974fa93491
commit fbff3a4790
2 changed files with 58 additions and 34 deletions

View file

@ -18,7 +18,7 @@ definerule("tabgen",
}, },
outleaves = { symname..".c" }, outleaves = { symname..".c" },
commands = { commands = {
"%{ins[1]} -f%{ins[2]} > %{outs} || rm %{outs}" "%{ins[1]} -f%{ins[2]} > %{outs}"
} }
} }
end end

View file

@ -9,8 +9,9 @@
Many mods by Ceriel Jacobs Many mods by Ceriel Jacobs
*/ */
#include <stdlib.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef NORCSID #ifndef NORCSID
@ -30,8 +31,18 @@ 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 */
main(argc, argv) void option(char *);
char *argv[]; void option_F(char *);
void InitTable(char *);
void PrintTable(void);
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[])
{ {
ProgCall = *argv++; ProgCall = *argv++;
@ -51,23 +62,19 @@ main(argc, argv)
} }
char * char *
Salloc(s) Salloc(char *s)
char *s;
{ {
char *ns = malloc((unsigned)strlen(s) + 1); char *ns = strdup(s);
if (ns) { if (!ns) {
strcpy(ns, s);
}
else {
fprintf(stderr, "%s: out of memory\n", ProgCall); fprintf(stderr, "%s: out of memory\n", ProgCall);
exit(1); exit(1);
} }
return ns; return ns;
} }
option(str) void
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).
@ -89,7 +96,7 @@ option(str)
DoFile(str); DoFile(str);
break; break;
case 'F': /* new output format string */ case 'F': /* new output format string */
sprintf(OutputForm, "%s\n", ++str); option_F(++str);
break; break;
case 'T': /* insert text literally */ case 'T': /* insert text literally */
printf("%s\n", ++str); printf("%s\n", ++str);
@ -124,8 +131,31 @@ option(str)
} }
} }
InitTable(ival) void
char *ival; option_F(char *form)
{
int len;
char *cp;
/*
* 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);
}
void
InitTable(char *ival)
{ {
int i; int i;
@ -138,7 +168,8 @@ InitTable(ival)
} }
} }
PrintTable() void
PrintTable(void)
{ {
int i; int i;
@ -156,8 +187,7 @@ PrintTable()
} }
int int
process(str, format) process(char *str, int format)
char *str;
{ {
char *cstr = str; char *cstr = str;
char *Name = cstr; /* overwrite original string! */ char *Name = cstr; /* overwrite original string! */
@ -189,12 +219,10 @@ process(str, format)
return 0; return 0;
} }
c_proc(str, Name) int
char *str; c_proc(char *str, char *Name)
char *Name;
{ {
int ch, ch2; int ch, ch2;
int quoted();
char *name = Salloc(Name); char *name = Salloc(Name);
while (*str) { while (*str) {
@ -209,8 +237,8 @@ c_proc(str, Name)
ch2 = quoted(&str); ch2 = quoted(&str);
} }
else { else {
if (ch2 = (*str++ & 0377)); ch2 = (*str++ & 0377);
else str--; if (!ch2) str--;
} }
if (ch > ch2) { if (ch > ch2) {
fprintf(stderr, "%s: bad range\n", ProgCall); fprintf(stderr, "%s: bad range\n", ProgCall);
@ -229,8 +257,7 @@ c_proc(str, Name)
} }
int int
setval(ch, nm) setval(int ch, char *nm)
char *nm;
{ {
char **p = &Table[ch]; char **p = &Table[ch];
@ -246,8 +273,7 @@ setval(ch, nm)
} }
int int
quoted(pstr) quoted(char **pstr)
char **pstr;
{ {
int ch; int ch;
int i; int i;
@ -291,9 +317,7 @@ quoted(pstr)
} }
char * char *
getln(s, n, fp) getln(char *s, int n, FILE *fp)
char *s;
FILE *fp;
{ {
int c = getc(fp); int c = getc(fp);
char *str = s; char *str = s;
@ -316,8 +340,8 @@ getln(s, n, fp)
#define BUFSIZE 1024 #define BUFSIZE 1024
DoFile(name) void
char *name; DoFile(char *name)
{ {
char text[BUFSIZE]; char text[BUFSIZE];
FILE *fp; FILE *fp;