64-bit safe. (Taj Khattra)

This commit is contained in:
rsc 2005-10-31 19:57:47 +00:00
parent d5233ccb3a
commit 0aae8d3d09

View file

@ -6,10 +6,8 @@
#define bsp_max 5000 #define bsp_max 5000
Biobuf *in; Biobuf *in;
#define stdin bstdin Biobuf bstdin;
#define stdout bstdout Biobuf bstdout;
Biobuf stdin;
Biobuf stdout;
char cary[1000]; char cary[1000];
char* cp = { cary }; char* cp = { cary };
char string[1000]; char string[1000];
@ -19,7 +17,7 @@
int bindx = 0; int bindx = 0;
int lev = 0; int lev = 0;
int ln; int ln;
int* ttp; char* ttp;
char* ss = ""; char* ss = "";
int bstack[10] = { 0 }; int bstack[10] = { 0 };
char* numb[15] = char* numb[15] =
@ -28,8 +26,8 @@
" 6", " 7", " 8", " 9", " 10", " 11", " 6", " 7", " 8", " 9", " 10", " 11",
" 12", " 13", " 14" " 12", " 13", " 14"
}; };
int* pre; char* pre;
int* post; char* post;
long peekc = -1; long peekc = -1;
int sargc; int sargc;
@ -61,40 +59,39 @@
"u","v","w","x","y","z" "u","v","w","x","y","z"
}; };
char* dot = { "." }; char* dot = { "." };
int bspace[bsp_max]; char* bspace[bsp_max];
int* bsp_nxt = { bspace }; char** bsp_nxt = bspace;
int bdebug = 0; int bdebug = 0;
int lflag; int lflag;
int cflag; int cflag;
int sflag; int sflag;
int* bundle(int, ...); char* bundle(int, ...);
void conout(int*, char*); void conout(char*, char*);
int cpeek(int, int, int); int cpeek(int, int, int);
int getch(void); int getch(void);
int* geta(char*); char* geta(char*);
int* getf(char*); char* getf(char*);
void getout(void); void getout(void);
void output(int*); void output(char*);
void pp(char*); void pp(char*);
void routput(int*); void routput(char*);
void tp(char*); void tp(char*);
void yyerror(char*, ...); void yyerror(char*, ...);
int yyparse(void); int yyparse(void);
typedef void* pointer; typedef void* pointer;
/* #pragma varargck type "lx" pointer */ #pragma varargck type "lx" pointer
%} %}
%union %union
{ {
int* iptr;
char* cptr; char* cptr;
int cc; int cc;
} }
%type <iptr> pstat stat stat1 def slist dlets e ase nase %type <cptr> pstat stat stat1 def slist dlets e ase nase
%type <iptr> slist re fprefix cargs eora cons constant lora %type <cptr> slist re fprefix cargs eora cons constant lora
%type <cptr> crs %type <cptr> crs
%token <cptr> LETTER EQOP _AUTO DOT %token <cptr> LETTER EQOP _AUTO DOT
@ -124,7 +121,7 @@ stuff:
ttp = bundle(6, pre, $6, post , "0", numb[lev], "Q"); ttp = bundle(6, pre, $6, post , "0", numb[lev], "Q");
conout(ttp, (char*)$1); conout(ttp, (char*)$1);
rcrs = crs; rcrs = crs;
output((int*)""); /* this is horse puk!! */ output("");
lev = bindx = 0; lev = bindx = 0;
} }
@ -550,8 +547,8 @@ def:
_DEFINE LETTER '(' _DEFINE LETTER '('
{ {
$$ = getf($2); $$ = getf($2);
pre = (int*)""; pre = (char*)"";
post = (int*)""; post = (char*)"";
lev = 1; lev = 1;
bindx = 0; bindx = 0;
bstack[bindx] = 0; bstack[bindx] = 0;
@ -793,12 +790,13 @@ loop:
if(ifile > sargc) { if(ifile > sargc) {
if(ifile >= sargc+2) if(ifile >= sargc+2)
getout(); getout();
in = &stdin; in = &bstdin;
Binit(in, 0, OREAD); Binit(in, 0, OREAD);
ln = 0; ln = 0;
goto loop; goto loop;
} }
Bterm(in); if(in)
Bterm(in);
if((in = Bopen(sargv[ifile], OREAD)) != 0){ if((in = Bopen(sargv[ifile], OREAD)) != 0){
ln = 0; ln = 0;
ss = sargv[ifile]; ss = sargv[ifile];
@ -808,57 +806,63 @@ loop:
return 0; /* shut up ken */ return 0; /* shut up ken */
} }
int* char*
bundle(int a, ...) bundle(int a, ...)
{ {
int i, *p, *q; int i;
char **q;
va_list arg;
p = &a; i = a;
i = *p++; va_start(arg, a);
q = bsp_nxt; q = bsp_nxt;
if(bdebug) if(bdebug)
fprint(2, "bundle %d elements at %lx\n", i, q); fprint(2, "bundle %d elements at %lx\n", i, q);
while(i-- > 0) { while(i-- > 0) {
if(bsp_nxt >= &bspace[bsp_max]) if(bsp_nxt >= &bspace[bsp_max])
yyerror("bundling space exceeded"); yyerror("bundling space exceeded");
*bsp_nxt++ = *p++; *bsp_nxt++ = va_arg(arg, char*);
} }
*bsp_nxt++ = 0; *bsp_nxt++ = 0;
yyval.iptr = q; va_end(arg);
return q; yyval.cptr = (char*)q;
return (char*)q;
} }
void void
routput(int *p) routput(char *p)
{ {
char **pp;
if(bdebug) if(bdebug)
fprint(2, "routput(%lx)\n", p); fprint(2, "routput(%lx)\n", p);
if(p >= &bspace[0] && p < &bspace[bsp_max]) { if((char**)p >= &bspace[0] && (char**)p < &bspace[bsp_max]) {
/* part of a bundle */ /* part of a bundle */
while(*p != 0) pp = (char**)p;
routput((int*)(*p++)); while(*pp != 0)
routput(*pp++);
} else } else
Bprint(&stdout, (char*)p); /* character string */ Bprint(&bstdout, p); /* character string */
} }
void void
output(int *p) output(char *p)
{ {
routput(p); routput(p);
bsp_nxt = &bspace[0]; bsp_nxt = &bspace[0];
Bprint(&stdout, "\n"); Bprint(&bstdout, "\n");
Bflush(&stdout); Bflush(&bstdout);
cp = cary; cp = cary;
crs = rcrs; crs = rcrs;
} }
void void
conout(int *p, char *s) conout(char *p, char *s)
{ {
Bprint(&stdout, "["); Bprint(&bstdout, "[");
routput(p); routput(p);
Bprint(&stdout, "]s%s\n", s); Bprint(&bstdout, "]s%s\n", s);
Bflush(&stdout); Bflush(&bstdout);
lev--; lev--;
} }
@ -867,8 +871,8 @@ yyerror(char *s, ...)
{ {
if(ifile > sargc) if(ifile > sargc)
ss = "teletype"; ss = "teletype";
Bprint(&stdout, "c[%s on line %d, %s]pc\n", s, ln+1, ss); Bprint(&bstdout, "c[%s on line %d, %s]pc\n", s, ln+1, ss);
Bflush(&stdout); Bflush(&bstdout);
cp = cary; cp = cary;
crs = rcrs; crs = rcrs;
bindx = 0; bindx = 0;
@ -881,9 +885,9 @@ pp(char *s)
{ {
/* puts the relevant stuff on pre and post for the letter s */ /* puts the relevant stuff on pre and post for the letter s */
bundle(3, "S", s, pre); bundle(3, "S", s, pre);
pre = yyval.iptr; pre = yyval.cptr;
bundle(4, post, "L", s, "s."); bundle(4, post, "L", s, "s.");
post = yyval.iptr; post = yyval.cptr;
} }
void void
@ -891,19 +895,19 @@ tp(char *s)
{ {
/* same as pp, but for temps */ /* same as pp, but for temps */
bundle(3, "0S", s, pre); bundle(3, "0S", s, pre);
pre = yyval.iptr; pre = yyval.cptr;
bundle(4, post, "L", s, "s."); bundle(4, post, "L", s, "s.");
post = yyval.iptr; post = yyval.cptr;
} }
void void
yyinit(int argc, char **argv) yyinit(int argc, char **argv)
{ {
Binit(&stdout, 1, OWRITE); Binit(&bstdout, 1, OWRITE);
sargv = argv; sargv = argv;
sargc = argc - 1; sargc = argc - 1;
if(sargc == 0) { if(sargc == 0) {
in = &stdin; in = &bstdin;
Binit(in, 0, OREAD); Binit(in, 0, OREAD);
} else if((in = Bopen(sargv[1], OREAD)) == 0) } else if((in = Bopen(sargv[1], OREAD)) == 0)
yyerror("cannot open input file"); yyerror("cannot open input file");
@ -915,21 +919,21 @@ yyinit(int argc, char **argv)
void void
getout(void) getout(void)
{ {
Bprint(&stdout, "q"); Bprint(&bstdout, "q");
Bflush(&stdout); Bflush(&bstdout);
exits(0); exits(0);
} }
int* char*
getf(char *p) getf(char *p)
{ {
return (int*)funtab[*p - 'a']; return funtab[*p - 'a'];
} }
int* char*
geta(char *p) geta(char *p)
{ {
return (int*)atab[*p - 'a']; return atab[*p - 'a'];
} }
void void
@ -967,7 +971,7 @@ main(int argc, char **argv)
yyinit(argc, argv); yyinit(argc, argv);
for(;;) for(;;)
yyparse(); yyparse();
/* exits(0); */ exits(0);
} }
pipe(p); pipe(p);
if(fork() == 0) { if(fork() == 0) {
@ -981,5 +985,5 @@ main(int argc, char **argv)
dup(p[0], 0); dup(p[0], 0);
close(p[0]); close(p[0]);
close(p[1]); close(p[1]);
execlp("dc", "dc", (char*)0); execl("dc", "dc", nil);
} }