FILE_LOCAL void doif(int hash) /* * Process an #if, #ifdef, or #ifndef. The latter two are straightforward, * while #if needs a subroutine of its own to evaluate the expression. * * doif() is called only if compiling is TRUE. If false, compilation * is always supressed, so we don't need to evaluate anything. This * supresses unnecessary warnings. */ { register int c; register int found; if ((c = skipws()) == '\n' || c == EOF_CHAR) { unget(); goto badif; } if (hash == L_if) { unget(); found = (eval() != 0); /* Evaluate expr, != 0 is TRUE */ hash = L_ifdef; /* #if is now like #ifdef */ } else { if (type[c] != LET) /* Next non-blank isn't letter */ goto badif; /* ... is an error */ found = (lookid(c) != NULL); /* Look for it in symbol table */ } if (found == (hash == L_ifdef)) { compiling = TRUE; *ifptr |= TRUE_SEEN; } else { compiling = FALSE; } return; badif: cerror("#if, #ifdef, or #ifndef without an argument", NULLST); #if !OLD_PREPROCESSOR skipnl(); /* Prevent an extra */ unget(); /* Error message */ #endif return; }
/* * Process an #if, #ifdef, or #ifndef. The latter two are straightforward, * while #if needs a subroutine of its own to evaluate the expression. * * doif() is called only if compiling is TRUE. If false, compilation * is always suppressed, so we don't need to evaluate anything. This * suppresses unnecessary warnings. */ FILE_LOCAL void doif(int hash) { int c; int found; if ((c = skipws()) == '\n' || c == EOF_CHAR) { unget(); goto badif; } if (hash == L_if) { unget(); found = (eval() != 0); /* Evaluate expr, != 0 is TRUE */ hash = L_ifdef; /* #if is now like #ifdef */ } else { if (type[c] != LET) /* Next non-blank isn't letter */ goto badif; /* ... is an error */ found = (lookid(c) != NULL); /* Look for it in symbol table */ } if (found == (hash == L_ifdef)) { compiling = TRUE; *ifptr |= TRUE_SEEN; } else { compiling = FALSE; } return; badif: cerror("#if, #ifdef, or #ifndef without an argument", NULLST); skipnl(); /* Prevent an extra */ unget(); /* Error message */ return; }
static Xfid* fsyswalk(Xfid *x, Fid *f) { Fcall t; int c, i, j, id; Qid q; uint8_t type; uint32_t path; Fid *nf; Dirtab *d, *dir; Window *w; char *err; nf = nil; w = nil; if(f->open) return respond(x, &t, "walk of open file"); if(x->fid != x->newfid) { nf = newfid(x->newfid); if(nf->busy) return respond(x, &t, "newfid already in use"); nf->busy = TRUE; nf->open = FALSE; nf->mntdir = f->mntdir; if(f->mntdir) f->mntdir->ref++; nf->dir = f->dir; nf->qid = f->qid; nf->w = f->w; nf->nrpart = 0; /* not open, so must be zero */ if(nf->w) incref(nf->w); f = nf; /* walk f */ } t.nwqid = 0; err = nil; dir = nil; id = WIN(f->qid); q = f->qid; if(x->nwname > 0) { for(i=0; i<x->nwname; i++) { if((q.type & QTDIR) == 0) { err = Enotdir; break; } if(strcmp(x->wname[i], "..") == 0) { type = QTDIR; path = Qdir; id = 0; if(w) { winclose(w); w = nil; } Accept: if(i == MAXWELEM) { err = "name too long"; break; } q.type = type; q.vers = 0; q.path = QID(id, path); t.wqid[t.nwqid++] = q; continue; } /* is it a numeric name? */ for(j=0; (c=x->wname[i][j]); j++) if(c<'0' || '9'<c) goto Regular; /* yes: it's a directory */ if(w) /* name has form 27/23; get out before losing w */ break; id = atoi(x->wname[i]); qlock(&row); w = lookid(id, FALSE); if(w == nil) { qunlock(&row); break; } incref(w); /* we'll drop reference at end if there's an error */ path = Qdir; type = QTDIR; qunlock(&row); dir = dirtabw; goto Accept; Regular: // if(FILE(f->qid) == Qacme) /* empty directory */ // break; if(strcmp(x->wname[i], "new") == 0) { if(w) error("w set in walk to new"); sendp(cnewwindow, nil); /* signal newwindowthread */ w = recvp(cnewwindow); /* receive new window */ incref(w); type = QTDIR; path = QID(w->id, Qdir); id = w->id; dir = dirtabw; goto Accept; } if(id == 0) d = dirtab; else d = dirtabw; d++; /* skip '.' */ for(; d->name; d++) if(strcmp(x->wname[i], d->name) == 0) { path = d->qid; type = d->type; dir = d; goto Accept; } break; /* file not found */ } if(i==0 && err == nil) err = Eexist; } if(err!=nil || t.nwqid<x->nwname) { if(nf) { nf->busy = FALSE; fsysdelid(nf->mntdir); } } else if(t.nwqid == x->nwname) { if(w) { f->w = w; w = nil; /* don't drop the reference */ } if(dir) f->dir = dir; f->qid = q; } if(w != nil) winclose(w); return respond(x, &t, err); }
/* * Called from control when a #define is scanned. This module * parses formal parameters and the replacement string. When * the formal parameter name is encountered in the replacement * string, it is replaced by a character in the range 128 to * 128+NPARAM (this allows up to 32 parameters within the * Dec Multinational range). * * There is some special case code to distinguish * #define foo bar * from #define foo() bar * * Also, we make sure that * #define foo foo * expands to "foo" but doesn't put cpp into an infinite loop. * * A warning message is printed if you redefine a symbol to a * different text. I.e, * #define foo 123 * #define foo 123 * is ok, but * #define foo 123 * #define foo +123 * is not. * * The following subroutines are called from define(): * checkparm called when a token is scanned. It checks through the * array of formal parameters. If a match is found, the * token is replaced by a control byte which will be used * to locate the parameter when the macro is expanded. * textput puts a string in the macro work area (parm[]), updating * parmp to point to the first free byte in parm[]. * textput() tests for work buffer overflow. * charput puts a single character in the macro work area (parm[]) * in a manner analogous to textput(). */ void dodefine() { int c; DEFBUF* dp; /* -> new definition */ int isredefine; /* TRUE if redefined */ char* old = NULL; /* Remember redefined */ if (type[(c = skipws())] != LET) goto bad_define; isredefine = FALSE; /* Set if redefining */ if ((dp = lookid(c)) == NULL) /* If not known now */ dp = defendel(token, FALSE); /* Save the name */ else /* It's known: */ { isredefine = TRUE; /* Remember this fact */ old = dp->repl; /* Remember replacement */ dp->repl = NULL; /* No replacement now */ } parlist[0] = parmp = parm; /* Setup parm buffer */ if ((c = get()) == '(') /* With arguments? */ { nargs = 0; /* Init formals counter */ do /* Collect formal parms */ { if (nargs >= LASTPARM) cfatal("Too many arguments for macro", NULLST); else if ((c = skipws()) == ')') break; /* Got them all */ else if (type[c] != LET) /* Bad formal syntax */ goto bad_define; scanid(c); /* Get the formal param */ parlist[nargs++] = parmp; /* Save its start */ textput(token); /* Save text in parm[] */ } while ((c = skipws()) == ','); /* Get another argument */ if (c != ')') /* Must end at ) */ goto bad_define; c = ' '; /* Will skip to body */ } else { /* * DEF_NOARGS is needed to distinguish between * "#define foo" and "#define foo()". */ nargs = DEF_NOARGS; /* No () parameters */ } if (type[c] == SPA) /* At whitespace? */ c = skipws(); /* Not any more. */ workp = work; /* Replacement put here */ inmacro = TRUE; /* Keep \<newline> now */ while (c != EOF_CHAR && c != '\n') /* Compile macro body */ { if (c == '#') /* Token concatenation? */ { while (workp > work && type[(int)workp[-1]] == SPA) --workp; /* Erase leading spaces */ save(TOK_SEP); /* Stuff a delimiter */ c = skipws(); /* Eat whitespace */ if (type[c] == LET) /* Another token here? */ ; /* Stuff it normally */ else if (type[c] == DIG) /* Digit string after? */ { while (type[c] == DIG) /* Stuff the digits */ { save(c); c = get(); } save(TOK_SEP); /* Delimit 2nd token */ } else { ciwarn("Strange character after # (%d.)", c); } continue; } switch (type[c]) { case LET: checkparm(c, dp); /* Might be a formal */ break; case DIG: /* Number in mac. body */ case DOT: /* Maybe a float number */ scannumber(c, save); /* Scan it off */ break; case QUO: /* String in mac. body */ stparmscan(c); break; case BSH: /* Backslash */ save('\\'); if ((c = get()) == '\n') wrongline = TRUE; save(c); break; case SPA: /* Absorb whitespace */ /* * Note: the "end of comment" marker is passed on * to allow comments to separate tokens. */ if (workp[-1] == ' ') /* Absorb multiple */ break; /* spaces */ else if (c == '\t') c = ' '; /* Normalize tabs */ /* Fall through to store character */ default: /* Other character */ save(c); break; } c = get(); } inmacro = FALSE; /* Stop newline hack */ unget(); /* For control check */ if (workp > work && workp[-1] == ' ') /* Drop trailing blank */ workp--; *workp = EOS; /* Terminate work */ dp->repl = savestring(work); /* Save the string */ dp->nargs = nargs; /* Save arg count */ #if OSL_DEBUG_LEVEL > 1 if (debug) dumpadef("macro definition", dp); else if (bDumpDefs) dumpadef(NULL, dp); #endif if (isredefine) /* Error if redefined */ { if ((old != NULL && dp->repl != NULL && !streq(old, dp->repl)) || (old == NULL && dp->repl != NULL) || (old != NULL && dp->repl == NULL)) { #ifdef STRICT_UNDEF cerror("Redefining defined variable \"%s\"", dp->name); #else cwarn("Redefining defined variable \"%s\"", dp->name); #endif } if (old != NULL) /* We don't need the */ free(old); /* old definition now. */ } return; bad_define: cerror("#define syntax error", NULLST); inmacro = FALSE; /* Stop <newline> hack */ }
FILE_LOCAL ReturnCode doif(struct Global *global, int hash) { /* * Process an #if, #ifdef, or #ifndef. The latter two are straightforward, * while #if needs a subroutine of its own to evaluate the expression. * * doif() is called only if compiling is TRUE. If false, compilation * is always supressed, so we don't need to evaluate anything. This * supresses unnecessary warnings. */ int c; int found; ReturnCode ret; if( (c = skipws( global ) ) == '\n' || c == EOF_CHAR ) { unget( global ); cerror( global, ERROR_MISSING_ARGUMENT ); #if !OLD_PREPROCESSOR skipnl( global ); /* Prevent an extra */ unget( global ); /* Error message */ #endif return(FPP_OK); } if( hash == L_if ) { unget( global ); ret = eval( global, &found ); if( ret ) return( ret ); found = (found != 0); /* Evaluate expr, != 0 is TRUE */ hash = L_ifdef; /* #if is now like #ifdef */ } else { if( type[c] != LET ) { /* Next non-blank isn't letter */ /* ... is an error */ cerror( global, ERROR_MISSING_ARGUMENT ); #if !OLD_PREPROCESSOR skipnl( global ); /* Prevent an extra */ unget( global ); /* Error message */ #endif return(FPP_OK); } found = ( lookid( global, c ) != NULL ); /* Look for it in symbol table */ } if( found == (hash == L_ifdef) ) { compiling = TRUE; *global->ifptr |= TRUE_SEEN; } else compiling = FALSE; return(FPP_OK); }
int rowload(Row *row, char *file, int initing) { int i, j, line, percent, y, nr, nfontr, n, ns, ndumped, dumpid, x, fd; Biobuf *b, *bout; char *buf, *l, *t, *fontname; Rune *r, rune, *fontr; Column *c, *c1, *c2; uint q0, q1; Rectangle r1, r2; Window *w; buf = fbufalloc(); if(file == nil){ if(home == nil){ warning(nil, "can't find file for load: $home not defined\n"); goto Rescue1; } sprint(buf, "%s/acme.dump", home); file = buf; } b = Bopen(file, OREAD); if(b == nil){ warning(nil, "can't open load file %s: %r\n", file); goto Rescue1; } /* current directory */ line = 0; l = rdline(b, &line); if(l == nil) goto Rescue2; l[Blinelen(b)-1] = 0; if(chdir(l) < 0){ warning(nil, "can't chdir %s\n", l); goto Rescue2; } /* global fonts */ for(i=0; i<2; i++){ l = rdline(b, &line); if(l == nil) goto Rescue2; l[Blinelen(b)-1] = 0; if(*l && strcmp(l, fontnames[i])!=0) rfget(i, TRUE, i==0 && initing, l); } if(initing && row->ncol==0) rowinit(row, screen->clipr); l = rdline(b, &line); if(l == nil) goto Rescue2; j = Blinelen(b)/12; if(j<=0 || j>10) goto Rescue2; for(i=0; i<j; i++){ percent = atoi(l+i*12); if(percent<0 || percent>=100) goto Rescue2; x = row->r.min.x+percent*Dx(row->r)/100; if(i < row->ncol){ if(i == 0) continue; c1 = row->col[i-1]; c2 = row->col[i]; r1 = c1->r; r2 = c2->r; r1.max.x = x; r2.min.x = x+Border; if(Dx(r1) < 50 || Dx(r2) < 50) continue; draw(screen, Rpt(r1.min, r2.max), display->white, nil, ZP); colresize(c1, r1); colresize(c2, r2); r2.min.x = x; r2.max.x = x+Border; draw(screen, r2, display->black, nil, ZP); } if(i >= row->ncol) rowadd(row, nil, x); } for(;;){ l = rdline(b, &line); if(l == nil) break; dumpid = 0; switch(l[0]){ case 'e': if(Blinelen(b) < 1+5*12+1) goto Rescue2; l = rdline(b, &line); /* ctl line; ignored */ if(l == nil) goto Rescue2; l = rdline(b, &line); /* directory */ if(l == nil) goto Rescue2; l[Blinelen(b)-1] = 0; if(*l == '\0'){ if(home == nil) r = bytetorune("./", &nr); else{ t = emalloc(strlen(home)+1+1); sprint(t, "%s/", home); r = bytetorune(t, &nr); free(t); } }else r = bytetorune(l, &nr); l = rdline(b, &line); /* command */ if(l == nil) goto Rescue2; t = emalloc(Blinelen(b)+1); memmove(t, l, Blinelen(b)); run(nil, t, r, nr, TRUE, nil, nil, FALSE); /* r is freed in run() */ continue; case 'f': if(Blinelen(b) < 1+5*12+1) goto Rescue2; fontname = l+1+5*12; ndumped = -1; break; case 'F': if(Blinelen(b) < 1+6*12+1) goto Rescue2; fontname = l+1+6*12; ndumped = atoi(l+1+5*12+1); break; case 'x': if(Blinelen(b) < 1+5*12+1) goto Rescue2; fontname = l+1+5*12; ndumped = -1; dumpid = atoi(l+1+1*12); break; default: goto Rescue2; } l[Blinelen(b)-1] = 0; fontr = nil; nfontr = 0; if(*fontname) fontr = bytetorune(fontname, &nfontr); i = atoi(l+1+0*12); j = atoi(l+1+1*12); q0 = atoi(l+1+2*12); q1 = atoi(l+1+3*12); percent = atoi(l+1+4*12); if(i<0 || i>10) goto Rescue2; if(i > row->ncol) i = row->ncol; c = row->col[i]; y = c->r.min.y+(percent*Dy(c->r))/100; if(y<c->r.min.y || y>=c->r.max.y) y = -1; if(dumpid == 0) w = coladd(c, nil, nil, y); else w = coladd(c, nil, lookid(dumpid, TRUE), y); if(w == nil) continue; w->dumpid = j; l = rdline(b, &line); if(l == nil) goto Rescue2; l[Blinelen(b)-1] = 0; r = bytetorune(l+5*12, &nr); ns = -1; for(n=0; n<nr; n++){ if(r[n] == '/') ns = n; if(r[n] == ' ') break; } if(dumpid == 0) winsetname(w, r, n); for(; n<nr; n++) if(r[n] == '|') break; wincleartag(w); textinsert(&w->tag, w->tag.file->nc, r+n+1, nr-(n+1), TRUE); if(ndumped >= 0){ /* simplest thing is to put it in a file and load that */ sprint(buf, "/tmp/d%d.%.4sacme", getpid(), getuser()); fd = create(buf, OWRITE|ORCLOSE, 0600); if(fd < 0){ free(r); warning(nil, "can't create temp file: %r\n"); goto Rescue2; } bout = emalloc(sizeof(Biobuf)); Binit(bout, fd, OWRITE); for(n=0; n<ndumped; n++){ rune = Bgetrune(b); if(rune == '\n') line++; if(rune == (Rune)Beof){ free(r); Bterm(bout); free(bout); close(fd); goto Rescue2; } Bputrune(bout, rune); } Bterm(bout); free(bout); textload(&w->body, 0, buf, 1); close(fd); w->body.file->mod = TRUE; for(n=0; n<w->body.file->ntext; n++) w->body.file->text[n]->w->dirty = TRUE; winsettag(w); }else if(dumpid==0 && r[ns+1]!='+' && r[ns+1]!='-') get(&w->body, nil, nil, FALSE, XXX, nil, 0); if(fontr){ fontx(&w->body, nil, nil, 0, 0, fontr, nfontr); free(fontr); } free(r); if(q0>w->body.file->nc || q1>w->body.file->nc || q0>q1) q0 = q1 = 0; textshow(&w->body, q0, q1, 1); w->maxlines = min(w->body.nlines, max(w->maxlines, w->body.maxlines)); } Bterm(b); fbuffree(buf); return TRUE; Rescue2: warning(nil, "bad load file %s:%d\n", file, line); Bterm(b); Rescue1: fbuffree(buf); return FALSE; }
ReturnCode dodefine(struct Global *global) { /* * Called from control when a #define is scanned. This module * parses formal parameters and the replacement string. When * the formal parameter name is encountered in the replacement * string, it is replaced by a character in the range 128 to * 128+NPARAM (this allows up to 32 parameters within the * Dec Multinational range). If cpp is ported to an EBCDIC * machine, you will have to make other arrangements. * * There is some special case code to distinguish * #define foo bar * from #define foo() bar * * Also, we make sure that * #define foo foo * expands to "foo" but doesn't put cpp into an infinite loop. * * A warning message is printed if you redefine a symbol to a * different text. I.e, * #define foo 123 * #define foo 123 * is ok, but * #define foo 123 * #define foo +123 * is not. * * The following subroutines are called from define(): * checkparm called when a token is scanned. It checks through the * array of formal parameters. If a match is found, the * token is replaced by a control byte which will be used * to locate the parameter when the macro is expanded. * textput puts a string in the macro work area (parm[]), updating * parmp to point to the first free byte in parm[]. * textput() tests for work buffer overflow. * charput puts a single character in the macro work area (parm[]) * in a manner analogous to textput(). */ int c; DEFBUF *dp; /* -> new definition */ int isredefine; /* TRUE if redefined */ char *old = NULL; /* Remember redefined */ ReturnCode ret; #if OK_CONCAT int quoting; /* Remember we saw a # */ #endif if (type[(c = skipws(global))] != LET) { cerror(global, ERROR_DEFINE_SYNTAX); global->inmacro = FALSE; /* Stop <newline> hack */ return(FPP_OK); } isredefine = FALSE; /* Set if redefining */ if ((dp = lookid(global, c)) == NULL) { /* If not known now */ dp = defendel(global, global->tokenbuf, FALSE); /* Save the name */ if(!dp) return(FPP_OUT_OF_MEMORY); } else { /* It's known: */ isredefine = TRUE; /* Remember this fact */ old = dp->repl; /* Remember replacement */ dp->repl = NULL; /* No replacement now */ } global->parlist[0] = global->parmp = global->parm; /* Setup parm buffer */ if ((c = get(global)) == '(') { /* With arguments? */ global->nargs = 0; /* Init formals counter */ do { /* Collect formal parms */ if (global->nargs >= LASTPARM) { cfatal(global, FATAL_TOO_MANY_ARGUMENTS_MACRO); return(FPP_TOO_MANY_ARGUMENTS); } else if ((c = skipws(global)) == ')') break; /* Got them all */ else if (type[c] != LET) { /* Bad formal syntax */ cerror(global, ERROR_DEFINE_SYNTAX); global->inmacro = FALSE; /* Stop <newline> hack */ return(FPP_OK); } scanid(global, c); /* Get the formal param */ global->parlist[global->nargs++] = global->parmp; /* Save its start */ ret=textput(global, global->tokenbuf); /* Save text in parm[] */ if(ret) return(ret); } while ((c = skipws(global)) == ','); /* Get another argument */ if (c != ')') { /* Must end at ) */ cerror(global, ERROR_DEFINE_SYNTAX); global->inmacro = FALSE; /* Stop <newline> hack */ return(FPP_OK); } c = ' '; /* Will skip to body */ } else { /* * DEF_NOARGS is needed to distinguish between * "#define foo" and "#define foo()". */ global->nargs = DEF_NOARGS; /* No () parameters */ } if (type[c] == SPA) /* At whitespace? */ c = skipws(global); /* Not any more. */ global->workp = global->work; /* Replacement put here */ global->inmacro = TRUE; /* Keep \<newline> now */ quoting = 0; /* No # seen yet. */ while (c != EOF_CHAR && c != '\n') { /* Compile macro body */ #if OK_CONCAT if (c == '#') { /* Token concatenation? */ if ((c = get(global)) != '#') { /* No, not really */ quoting = 1; /* Maybe quoting op. */ continue; } while (global->workp > global->work && type[(unsigned)*(global->workp - 1)] == SPA) --global->workp; /* Erase leading spaces */ if((ret=save(global, TOK_SEP))) /* Stuff a delimiter */ return(ret); c = skipws(global); /* Eat whitespace */ continue; } #endif switch (type[c]) { case LET: #if OK_CONCAT ret=checkparm(global, c, dp, quoting); /* Might be a formal */ #else ret=checkparm(c, dp); /* Might be a formal */ #endif if(ret) return(ret); break; case DIG: /* Number in mac. body */ case DOT: /* Maybe a float number */ ret=scannumber(global, c, save); /* Scan it off */ if(ret) return(ret); break; case QUO: /* String in mac. body */ ret=stparmscan(global, c); if(ret) return(ret); break; case BSH: /* Backslash */ ret=save(global, '\\'); if(ret) return(ret); if ((c = get(global)) == '\n') global->wrongline = TRUE; ret=save(global, c); if(ret) return(ret); break; case SPA: /* Absorb whitespace */ /* * Note: the "end of comment" marker is passed on * to allow comments to separate tokens. */ if (global->workp[-1] == ' ') /* Absorb multiple */ break; /* spaces */ else if (c == '\t') c = ' '; /* Normalize tabs */ /* Fall through to store character */ default: /* Other character */ ret=save(global, c); if(ret) return(ret); break; } c = get(global); quoting = 0; /* Only when immediately*/ /* preceding a formal */ } global->inmacro = FALSE; /* Stop newline hack */ unget(global); /* For control check */ if (global->workp > global->work && global->workp[-1] == ' ') /* Drop trailing blank */ global->workp--; *global->workp = EOS; /* Terminate work */ dp->repl = savestring(global, global->work); /* Save the string */ dp->nargs = global->nargs; /* Save arg count */ if (isredefine) { /* Error if redefined */ if ((old != NULL && dp->repl != NULL && !streq(old, dp->repl)) || (old == NULL && dp->repl != NULL) || (old != NULL && dp->repl == NULL)) { cerror(global, ERROR_REDEFINE, dp->name); } if (old != NULL) /* We don't need the */ free(old); /* old definition now. */ } return(FPP_OK); }