ANSI C conversion.

This commit is contained in:
carl 2019-05-11 01:14:01 +08:00
parent dfefbcae86
commit fa2859df79
4 changed files with 157 additions and 137 deletions

View file

@ -10,17 +10,30 @@
#include <stdio.h> #include <stdio.h>
extern CheckId(); extern void BeginOfProgram(void);
extern DoOption(); extern void EndOfProgram(void);
extern BeginOfProgram(), EndOfProgram(); extern void DoOption(char*);
extern void CheckId(char *, int);
#define MAX_ID_LEN 256 #define MAX_ID_LEN 256
char *ProgName; char *ProgName;
int GCcopy; int GCcopy;
main(argc, argv)
char *argv[]; void DoFile(FILE *);
void SkipString(FILE *, int);
void SkipComment(FILE *);
void DoIdent(FILE *, int);
int StartId(int);
int InId(int);
int StartNum(int);
void DoNum(register FILE *, int);
int main(int argc, char *argv[])
{ {
char **nargv; char **nargv;
int nargc = 0; int nargc = 0;
@ -55,13 +68,12 @@ main(argc, argv)
DoFile(stdin); DoFile(stdin);
} }
EndOfProgram(); EndOfProgram();
exit(0); return 0;
} }
DoFile(fp) void DoFile(FILE *fp)
FILE *fp;
{ {
register c; register int c;
while ((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
switch (c) { switch (c) {
@ -98,10 +110,9 @@ DoFile(fp)
fclose(fp); fclose(fp);
} }
SkipString(fp, stopc) void SkipString(FILE *fp, int stopc)
FILE *fp;
{ {
register c; register int c;
while ((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
if (GCcopy) putchar(c); if (GCcopy) putchar(c);
@ -116,10 +127,9 @@ SkipString(fp, stopc)
} }
} }
SkipComment(fp) void SkipComment(FILE *fp)
FILE *fp;
{ {
register c; register int c;
while ((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
if (GCcopy) putchar(c); if (GCcopy) putchar(c);
@ -133,12 +143,11 @@ SkipComment(fp)
} }
} }
DoIdent(fp, s) void DoIdent(FILE *fp, int s)
FILE *fp;
{ {
char id_buf[MAX_ID_LEN]; char id_buf[MAX_ID_LEN];
register cnt = 1; register int cnt = 1;
register c; register int c;
id_buf[0] = s; id_buf[0] = s;
@ -155,7 +164,7 @@ DoIdent(fp, s)
} }
} }
StartId(c) int StartId(int c)
{ {
switch (c) { switch (c) {
@ -179,7 +188,7 @@ StartId(c)
} }
} }
InId(c) int InId(int c)
{ {
switch (c) { switch (c) {
@ -205,7 +214,7 @@ InId(c)
} }
} }
StartNum(c) int StartNum(int c)
{ {
switch(c) { switch(c) {
case '0': case '1': case '2': case '3': case '4': case '0': case '1': case '2': case '3': case '4':
@ -223,8 +232,7 @@ StartNum(c)
#define getoct(c, fp) do { c = getc((fp)); if (GCcopy) putchar(c);} while (isoct(c)) #define getoct(c, fp) do { c = getc((fp)); if (GCcopy) putchar(c);} while (isoct(c))
#define gethex(c, fp) do { c = getc((fp)); if (GCcopy) putchar(c);} while (ishex(c)) #define gethex(c, fp) do { c = getc((fp)); if (GCcopy) putchar(c);} while (ishex(c))
DoNum(fp, c) void DoNum(register FILE *fp, int c)
register FILE *fp;
{ {
if (c != '0') { if (c != '0') {
getdec(c, fp); getdec(c, fp);

View file

@ -30,6 +30,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#define DEF_LENGTH 8 #define DEF_LENGTH 8
@ -84,20 +85,32 @@ char * keywords[] = {
0 0
}; };
void InsertId(char *,int);
char *Malloc(unsigned int);
char *Salloc(char *);
int EnHash(char*);
void EndOfProgram(void);
void DoOption(char*);
void CheckId(char *, int);
void saveline(register struct idf *);
void mapline(char *);
struct idf *maplist = 0; struct idf *maplist = 0;
DefineKeys() void DefineKeys(void)
{ {
register char **pkey = &keywords[0]; register char **pkey = &keywords[0];
register char *id; register char *id;
while (id = *pkey++) while ((id = *pkey++))
if (strlen(id) >= maxlen) if (strlen(id) >= maxlen)
InsertId(id, 1); InsertId(id, 1);
} }
DoOption(str) void DoOption(char *str)
char *str;
{ {
switch (str[1]) { switch (str[1]) {
@ -129,8 +142,7 @@ struct idf *hash_tab[HASHSIZE];
char *Malloc(), *Salloc(); char *Malloc(), *Salloc();
InsertId(id, key) void InsertId(char *id, int key)
char *id;
{ {
int hash_val = EnHash(id); int hash_val = EnHash(id);
register struct idf *idp = hash_tab[hash_val]; register struct idf *idp = hash_tab[hash_val];
@ -168,11 +180,9 @@ InsertId(id, key)
p->id_key = key; p->id_key = key;
} }
char * char *Malloc(unsigned n)
Malloc(n)
unsigned n;
{ {
char *mem, *malloc(); char *mem;
if ((mem = malloc(n)) == 0) { if ((mem = malloc(n)) == 0) {
fprintf(stderr, "%s: out of memory\n", ProgName); fprintf(stderr, "%s: out of memory\n", ProgName);
@ -181,9 +191,7 @@ Malloc(n)
return mem; return mem;
} }
char * char *Salloc(char *str)
Salloc(str)
char *str;
{ {
if (str == 0) if (str == 0)
str = ""; str = "";
@ -191,11 +199,10 @@ Salloc(str)
return strcpy(Malloc((unsigned)strlen(str) + 1), str); return strcpy(Malloc((unsigned)strlen(str) + 1), str);
} }
EnHash(id) int EnHash(char *id)
char *id;
{ {
register unsigned hash_val = 0; register unsigned hash_val = 0;
register n = maxlen; register int n = maxlen;
while (n-- && *id) while (n-- && *id)
hash_val = 31 * hash_val + *id++; hash_val = 31 * hash_val + *id++;
@ -203,9 +210,9 @@ EnHash(id)
return hash_val % (unsigned) HASHSIZE; return hash_val % (unsigned) HASHSIZE;
} }
BeginOfProgram() { DefineKeys(); } void BeginOfProgram(void) { DefineKeys(); }
EndOfProgram() void EndOfProgram(void)
{ {
register int i; register int i;
register struct idf *idp, *p; register struct idf *idp, *p;
@ -216,12 +223,12 @@ EndOfProgram()
continue; continue;
switch (action) { switch (action) {
register n; register int n;
case ACT_LISTONLY: case ACT_LISTONLY:
n = 0; n = 0;
if (idp->id_key == 0) { if (idp->id_key == 0) {
printf(idp->id_name); printf("%s",idp->id_name);
n++; n++;
} }
for (p = idp->id_same; p; p = p->id_same) for (p = idp->id_same; p; p = p->id_same)
@ -260,8 +267,7 @@ EndOfProgram()
} }
} }
saveline(p) void saveline(register struct idf *p)
register struct idf *p;
{ {
register struct idf *idp = maplist, *idp1 = 0; register struct idf *idp = maplist, *idp1 = 0;
@ -278,10 +284,9 @@ saveline(p)
} }
} }
mapline(nm) void mapline(char *nm)
char *nm;
{ {
static map_count = 0; static int map_count = 0;
switch (action) { switch (action) {
@ -295,8 +300,7 @@ mapline(nm)
} }
} }
CheckId(id, s) void CheckId(char *id, int s)
char *id;
{ {
if (s >= maxlen) if (s >= maxlen)
InsertId(id, 0); InsertId(id, 0);

View file

@ -15,6 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#ifndef DEF_LENGTH #ifndef DEF_LENGTH
#define DEF_LENGTH 8 #define DEF_LENGTH 8
@ -32,13 +33,22 @@ struct idf {
struct idf *hash_tab[HASHSIZE]; struct idf *hash_tab[HASHSIZE];
char *Malloc(), *Salloc(); char *Malloc(unsigned int);
struct idf *FindId(); char *Salloc(char *);
int EnHash(char*);
void EndOfProgram(void);
void DoOption(char*);
void CheckId(char *, int);
void DoMacro(char *);
void GetMacros(char *);
void InsertMacro(char *, char *);
struct idf *FindId(char *);
extern char *ProgName; extern char *ProgName;
DoOption(str) void DoOption(char* str)
char *str;
{ {
switch (str[1]) { switch (str[1]) {
@ -57,8 +67,7 @@ DoOption(str)
} }
/*ARGSUSED*/ /*ARGSUSED*/
CheckId(id, len) void CheckId(char *id, int len)
char *id;
{ {
struct idf *idp = FindId(id); struct idf *idp = FindId(id);
@ -70,8 +79,7 @@ CheckId(id, len)
} }
} }
DoMacro(str) void DoMacro(char *str)
char *str;
{ {
char *id, *text; char *id, *text;
@ -92,11 +100,10 @@ DoMacro(str)
InsertMacro(id, text); InsertMacro(id, text);
} }
GetMacros(fn) void GetMacros(char *fn)
char *fn;
{ {
FILE *fp; FILE *fp;
register c; register int c;
char buf[LINE_LEN]; char buf[LINE_LEN];
char *bufp = &buf[0]; char *bufp = &buf[0];
@ -118,8 +125,7 @@ GetMacros(fn)
fclose(fp); fclose(fp);
} }
InsertMacro(id, text) void InsertMacro(char *id, char *text)
char *id, *text;
{ {
int hash_val = EnHash(id); int hash_val = EnHash(id);
struct idf *idp = hash_tab[hash_val]; struct idf *idp = hash_tab[hash_val];
@ -143,11 +149,9 @@ InsertMacro(id, text)
hash_tab[hash_val] = idp; hash_tab[hash_val] = idp;
} }
char * char *Malloc(unsigned int n)
Malloc(n)
unsigned n;
{ {
char *mem, *malloc(); char *mem;
if ((mem = malloc(n)) == 0) { if ((mem = malloc(n)) == 0) {
fprintf(stderr, "%s: out of memory\n", ProgName); fprintf(stderr, "%s: out of memory\n", ProgName);
@ -156,9 +160,7 @@ Malloc(n)
return mem; return mem;
} }
char * char *Salloc(char *str)
Salloc(str)
char *str;
{ {
if (str == 0) { if (str == 0) {
str = ""; str = "";
@ -166,11 +168,9 @@ Salloc(str)
return strcpy(Malloc((unsigned)strlen(str) + 1), str); return strcpy(Malloc((unsigned)strlen(str) + 1), str);
} }
struct idf * struct idf *FindId(char *id)
FindId(id)
char *id;
{ {
register hash_val = EnHash(id); register int hash_val = EnHash(id);
register struct idf *idp = hash_tab[hash_val]; register struct idf *idp = hash_tab[hash_val];
while (idp) { while (idp) {
@ -182,8 +182,7 @@ FindId(id)
return 0; return 0;
} }
EnHash(id) int EnHash(char *id)
char *id;
{ {
register unsigned hash_val = 0; register unsigned hash_val = 0;
@ -196,11 +195,11 @@ EnHash(id)
extern int GCcopy; extern int GCcopy;
BeginOfProgram() void BeginOfProgram(void)
{ {
GCcopy = 1; GCcopy = 1;
} }
EndOfProgram() void EndOfProgram(void)
{ {
} }

View file

@ -10,14 +10,16 @@
*/ */
#include <stdio.h> #include <stdio.h>
#incoude <string.h> #include <string.h>
#include <stdlib.h>
#define BSIZ 1024 #define BSIZ 1024
char *prog; char *prog;
int dflag = 0; /* suppress "file.c :" */ int dflag = 0; /* suppress "file.c :" */
struct namelist { struct namelist
{
struct namelist *next; struct namelist *next;
char *name; char *name;
}; };
@ -26,13 +28,19 @@ struct namelist *freelist;
struct namelist *new_namelist(); struct namelist *new_namelist();
struct namelist *nl = 0; struct namelist *nl = 0;
char *Malloc(unsigned int);
char *include_line(char *);
int dofile(char *);
char *Malloc(u) char *Malloc(u)
unsigned u; unsigned u;
{ {
char *sp, *malloc(); char *sp;
if ((sp = malloc(u)) == 0) { if ((sp = malloc(u)) == 0)
fprintf(stderr, "%s: out of space\n"); {
fprintf(stderr, "%s:", " out of space\n");
exit(1); exit(1);
} }
return sp; return sp;
@ -43,7 +51,8 @@ new_namelist()
{ {
register struct namelist *nlp = freelist; register struct namelist *nlp = freelist;
if (nlp) { if (nlp)
{
freelist = nlp->next; freelist = nlp->next;
return nlp; return nlp;
} }
@ -51,23 +60,23 @@ new_namelist()
return (struct namelist *) Malloc(sizeof(struct namelist)); return (struct namelist *) Malloc(sizeof(struct namelist));
} }
free_namelist(nlp) void free_namelist(struct namelist *nlp)
struct namelist *nlp; {
if (nlp)
{ {
if (nlp) {
free_namelist(nlp->next); free_namelist(nlp->next);
nlp->next = freelist; nlp->next = freelist;
freelist = nlp; freelist = nlp;
} }
} }
add_name(nm) void add_name(char *nm)
char *nm;
{ {
struct namelist *nlp = nl, *lnlp = 0, *nnlp; struct namelist *nlp = nl, *lnlp = 0, *nnlp;
while (nlp) { while (nlp)
register i = strcmp(nm, nlp->name); {
register int i = strcmp(nm, nlp->name);
if (i < 0) if (i < 0)
break; break;
if (i == 0) /* already present */ if (i == 0) /* already present */
@ -76,23 +85,25 @@ add_name(nm)
nlp = nlp->next; nlp = nlp->next;
} }
(nnlp = new_namelist())->name = strcpy(Malloc((unsigned)strlen(nm) + 1), nm); (nnlp = new_namelist())->name = strcpy(Malloc((unsigned) strlen(nm) + 1),
nm);
if (lnlp) { if (lnlp)
{
nnlp->next = lnlp->next; nnlp->next = lnlp->next;
lnlp->next = nnlp; lnlp->next = nnlp;
} }
else { else
{
nnlp->next = nl; nnlp->next = nl;
nl = nnlp; nl = nnlp;
} }
} }
print_namelist(nm, nlp) void print_namelist(char *nm, struct namelist *nlp)
char *nm; {
struct namelist *nlp; while (nlp)
{ {
while (nlp) {
if (!dflag) if (!dflag)
printf("%s: ", nm); printf("%s: ", nm);
printf("%s\n", nlp->name); printf("%s\n", nlp->name);
@ -101,23 +112,25 @@ print_namelist(nm, nlp)
} }
/*ARGSUSED*/ /*ARGSUSED*/
main(argc, argv) int main(int argc, char *argv[])
char *argv[];
{ {
int err = 0; int err = 0;
prog = *argv++; prog = *argv++;
if (*argv && **argv == '-') { if (*argv && **argv == '-')
{
char *opt = &(*argv++)[1]; char *opt = &(*argv++)[1];
if (*opt++ != 'd' || *opt) { if (*opt++ != 'd' || *opt)
{
fprintf(stderr, "use: %s [-d] [file ...]\n", prog); fprintf(stderr, "use: %s [-d] [file ...]\n", prog);
exit(1); exit(1);
} }
dflag = 1; dflag = 1;
} }
while (*argv) { while (*argv)
{
free_namelist(nl); free_namelist(nl);
nl = 0; nl = 0;
if (dofile(*argv) == 0) if (dofile(*argv) == 0)
@ -127,67 +140,63 @@ main(argc, argv)
exit(err ? 1 : 0); exit(err ? 1 : 0);
} }
int int contains_slash(register char *s)
contains_slash(s)
register char *s;
{ {
while (*s) while (*s)
if (*s++ == '/') return 1; if (*s++ == '/')
return 1;
return 0; return 0;
} }
extern char *fgets(); int dofile(char *fn)
dofile(fn)
char *fn;
{ {
char buf[BSIZ]; char buf[BSIZ];
FILE *fp; FILE *fp;
char *nm, *include_line(); char *nm;
if ((fp = fopen(fn, "r")) == 0) { if ((fp = fopen(fn, "r")) == 0)
{
fprintf(stderr, "%s: cannot read %s\n", prog, fn); fprintf(stderr, "%s: cannot read %s\n", prog, fn);
return 0; return 0;
} }
if (contains_slash(fn)) { if (contains_slash(fn))
fprintf(stderr, "%s: (warning) %s not in current directory; not checked\n", prog, fn); {
fprintf(stderr,
"%s: (warning) %s not in current directory; not checked\n",
prog, fn);
fclose(fp); fclose(fp);
return 1; return 1;
} }
while (fgets(buf, BSIZ, fp) != NULL) while (fgets(buf, BSIZ, fp) != NULL)
if (nm = include_line(buf)) { if ((nm = include_line(buf)))
{
add_name(nm); add_name(nm);
if (dofile(nm)) ; if (dofile(nm))
;
} }
fclose(fp); fclose(fp);
return 1; return 1;
} }
char * char *include_line(char *s)
include_line(s)
char *s;
{ {
while ((*s == '\t') || (*s == ' ')) while ((*s == '\t') || (*s == ' '))
s++; s++;
if (*s++ == '#') { if (*s++ == '#')
{
while ((*s == '\t') || (*s == ' ')) while ((*s == '\t') || (*s == ' '))
s++; s++;
if ( if ((*s++ == 'i') && (*s++ == 'n') && (*s++ == 'c') && (*s++ == 'l')
(*s++ == 'i') && && (*s++ == 'u') && (*s++ == 'd') && (*s++ == 'e'))
(*s++ == 'n') && {
(*s++ == 'c') &&
(*s++ == 'l') &&
(*s++ == 'u') &&
(*s++ == 'd') &&
(*s++ == 'e')
) {
while ((*s == '\t') || (*s == ' ')) while ((*s == '\t') || (*s == ' '))
s++; s++;
if (*s++ == '"') { if (*s++ == '"')
{
char *nm = s; char *nm = s;
while (*s != 0 && *s != '"') while (*s != 0 && *s != '"')