static int Breadnumber(Biobuf *b, char *buf) { int i; int c; int havedigits; havedigits = 0; for(i=0; i<22; i++){ if((c = Bgetc(b)) == Beof) return -1; if('0' <= c && c <= '9'){ *buf++ = c; havedigits = 1; }else if(c == ' '){ if(havedigits){ while((c = Bgetc(b)) == ' ') ; if(c != Beof) Bungetc(b); break; } }else{ werrstr("bad character %.2x", c); return -1; } } *buf = 0; return 0; }
static int readline(Biobuf *bp, char *buf) { int c; char *p, *e; p = buf; e = p + MAXLINELEN-1; do { c = Bgetc(bp); if (c < 0) { if (p == buf) return -1; break; } if (c == '\n') break; *p++ = c; } while (p < e); *p = 0; if (c != '\n' && c >= 0) { do c = Bgetc(bp); while (c >= 0 && c != '\n'); } return p - buf; }
long Bgetrune(Biobuf *bp) { int c, i; Rune rune; char str[UTFmax]; c = Bgetc(bp); if(c < Runeself) { /* one char */ bp->runesize = 1; return c; } str[0] = c; for(i=1;;) { c = Bgetc(bp); if(c < 0) return c; str[i++] = c; if(fullrune(str, i)) { bp->runesize = chartorune(&rune, str); while(i > bp->runesize) { Bungetc(bp); i--; } return rune; } } }
static char * pass1(State *s, char *buf, int *alloc) { int c, t, used, state; enum { Lost, String, Cplusplus, Comment }; used = 0; state = Lost; while((c = Bgetc(s->bp)) != -1){ if(c == '\n'){ s->line++; if(state == Cplusplus) state = Lost; } if(c == '/'){ t = Bgetc(s->bp); if(t == '*' && state == Lost) state = Comment; else if(t == '/' && state == Lost) state = Cplusplus; else Bungetc(s->bp); } if(c == '*'){ t = Bgetc(s->bp); if(t == '/' && state == Comment){ state = Lost; c = ' '; } else Bungetc(s->bp); } if(c == '\\'){ t = Bgetc(s->bp); if(t == '\n') c = ' '; else Bungetc(s->bp); } if(c == '\n' && state == Lost) break; if(state != Comment && state != Cplusplus){ if(used >= *alloc-1){ *alloc += Memchunk; buf = erealloc(buf, *alloc); } buf[used++] = c; } } if(c == -1) return nil; buf[used] = 0; return buf; }
int Bgetle2(Biobuf *bp) { int l, h; l = Bgetc(bp); h = Bgetc(bp); return l|(h<<8); }
int _read5(Biobuf *bp, Prog *p) { int as, n; Addr a; as = Bgetc(bp); /* as */ if(as < 0) return 0; p->kind = aNone; p->sig = 0; if(as == ANAME || as == ASIGNAME){ if(as == ASIGNAME){ Bread(bp, &p->sig, 4); p->sig = leswal(p->sig); } p->kind = aName; p->type = type2char(Bgetc(bp)); /* type */ p->sym = Bgetc(bp); /* sym */ n = 0; for(;;) { as = Bgetc(bp); if(as < 0) return 0; n++; if(as == 0) break; } p->id = malloc(n); if(p->id == 0) return 0; Bseek(bp, -n, 1); if(Bread(bp, p->id, n) != n) return 0; return 1; } if(as == ATEXT) p->kind = aText; else if(as == AGLOBL) p->kind = aData; skip(bp, 6); /* scond(1), reg(1), lineno(4) */ a = addr(bp); addr(bp); if(a.type != D_OREG || a.name != D_STATIC && a.name != D_EXTERN) p->kind = aNone; p->sym = a.sym; return 1; }
void varread(void) /* read into variable */ { Datum d; extern Biobuf *bin; Symbol *var = (Symbol *) *pc++; int c; Again: do c = Bgetc(bin); while(c==' ' || c=='\t' || c=='\n'); if(c == Beof){ Iseof: if(moreinput()) goto Again; d.val = var->u.val = 0.0; goto Return; } if(strchr("+-.0123456789", c) == 0) execerror("non-number read into", var->name); Bungetc(bin); if(Bgetd(bin, &var->u.val) == Beof) goto Iseof; else d.val = 1.0; Return: var->type = VAR; push(d); }
int readc(void) { loop: if((readptr != &readstk[0]) && (*readptr != 0)) { if(sfeof(*readptr) == 0) return(lastchar = sgetc(*readptr)); release(*readptr); readptr--; goto loop; } lastchar = Bgetc(curfile); if(lastchar != -1) return(lastchar); if(readptr != &readptr[0]) { readptr--; if(*readptr == 0) curfile = &bin; goto loop; } if(curfile != &bin) { Bterm(curfile); curfile = &bin; goto loop; } exits(0); return 0; /* shut up ken */ }
int32_t pcollgnextoff(int32_t fromoff) { int c, state = 0, defoff = -1; if(Bseek(bdict, fromoff, 0) < 0) return -1; while((c = Bgetc(bdict)) >= 0){ if(c == '\r') defoff = Boffset(bdict); switch(state){ case 0: if(c == 0x05) state = 1; break; case 1: if(c == 'h') state = 2; else state = 0; break; case 2: if(c == 0x06) return (Boffset(bdict)-3); else state = 0; break; } } return defoff; }
static int getpassword(char *buf, char *e) { char *p; int c; int consctl, rv = 0; consctl = open("/dev/consctl", OWRITE); if(consctl >= 0) write(consctl, "rawon", 5); print("Password: "******"\n"); out: if(consctl >= 0) close(consctl); return rv; }
int escchar(char c) { int n; char buf[32]; if(c >= '0' && c <= '9') { n = 1; buf[0] = c; for(;;) { c = Bgetc(bin); if(c == Eof) fatal("%d: <eof> in escape sequence", line); if(strchr("0123456789xX", c) == 0) { Bungetc(bin); break; } buf[n++] = c; } buf[n] = '\0'; return strtol(buf, 0, 0); } n = cmap[c]; if(n == 0) return c; return n-1; }
/* * get a line that ends in crnl or cr, turn terminating crnl into a nl * * return 0 on EOF */ static int getcrnl(char *buf, int n) { int c; char *ep; char *bp; Biobuf *fp = ∈ Bflush(&out); bp = buf; ep = bp + n - 1; while(bp != ep){ c = Bgetc(fp); if(debug) { seek(2, 0, 2); fprint(2, "%c", c); } switch(c){ case -1: *bp = 0; if(bp==buf) return 0; else return bp-buf; case '\r': c = Bgetc(fp); if(c == '\n'){ if(debug) { seek(2, 0, 2); fprint(2, "%c", c); } *bp = 0; return bp-buf; } Bungetc(fp); c = '\r'; break; case '\n': *bp = 0; return bp-buf; } *bp++ = c; } *bp = 0; return bp-buf; }
int peek(void) { if (ugtop > 0) return ugbuf[ugtop-1]; ugbuf[ugtop] = Bgetc(bdf); return ugbuf[ugtop++]; }
/* * Return offset into bdict where next webster entry after fromoff starts. * Webster entries start with <p><hw> */ long pgwnextoff(long fromoff) { long a, n; int c; a = Bseek(bdict, fromoff, 0); if(a != fromoff) return -1; n = 0; for(;;) { c = Bgetc(bdict); if(c < 0) break; if(c == '<' && Bgetc(bdict) == 'p' && Bgetc(bdict) == '>') { c = Bgetc(bdict); if(c == '<') { if (Bgetc(bdict) == 'h' && Bgetc(bdict) == 'w' && Bgetc(bdict) == '>') n = 7; } else if (c == '{') n = 4; if(n) break; } } return (Boffset(bdict)-n); }
char * getnext(int must){ int c; char *beg; int prect,nlct; prect = nlct = 0; if(tptr > lastplace){ tptr = lastplace = temp; err = 0; inswitch = 0; } tp = lastplace; if(inswitch && tptr <= lastplace) if (isalnum((uchar)*lastplace)||ispunct((uchar)*lastplace)||isop((uchar)*lastplace))return(lastplace); space: while(isspace(c=Bgetc(input)))puttmp(c,1); beg = tp; puttmp(c,1); if(c == '/'){ if(puttmp(Bgetc(input),1) == '*'){ cont: while((c=Bgetc(input)) != '*'){ puttmp(c,0); if(must == 0 && c == '\n') if(nlct++ > 2)goto done; } puttmp(c,1); star: if(puttmp((c=Bgetc(input)),1) == '/'){ beg = tp; puttmp((c=Bgetc(input)),1); } else if(c == '*')goto star; else goto cont; } else goto done; } if(isspace(c))goto space; if(c == '#' && tp > temp+1 && *(tp-2) == '\n'){ if(prect++ > 2)goto done; while(puttmp((c=Bgetc(input)),1) != '\n') if(c == '\\')puttmp(Bgetc(input),1); goto space; } if(isalnum(c)){ while(isalnum(c = Bgetc(input)))puttmp(c,1); Bungetc(input); } done: puttmp('\0',1); lastplace = tp-1; inswitch = 1; return(beg); }
/* Append an input line to a String. * * Empty lines and leading whitespace are removed. */ static char * rdline(Biobuf *fp, String *to) { int c; int len = 0; c = Bgetc(fp); /* eat leading white */ while(c==' ' || c=='\t' || c=='\n' || c=='\r') c = Bgetc(fp); if(c < 0) return 0; for(;;){ switch(c) { case -1: goto out; case '\\': c = Bgetc(fp); if (c != '\n') { s_putc(to, '\\'); s_putc(to, c); len += 2; } break; case '\r': break; case '\n': if(len != 0) goto out; break; default: s_putc(to, c); len++; break; } c = Bgetc(fp); } out: s_terminate(to); return to->ptr - len; }
static Sym* zsym(char *pn, Biobuf *f, Sym *h[]) { int o; o = Bgetc(f); if(o < 0 || o >= NSYM || h[o] == nil) mangle(pn); return h[o]; }
static int get1(Biobuf *b) { int c; c = Bgetc(b); if(c < 0) sysfatal("unexpected eof"); return c; }
static int get1(Biobuf *b) { int c; c = Bgetc(b); if(c < 0) error("unexpected eof reading file information"); return c; }
static int GETC(void) { int c; c = Bgetc(&fin); if(c == '\n') lineno++; return c; }
void eatstring(void) { String *s; int esc, c, cnt; esc = 0; for(cnt = 0;;) { c = Bgetc(bin); switch(c) { case Eof: fatal("%d: <eof> in string constant", line); case '\n': line++; diag("newline in string constant"); goto done; case '\\': if(esc) { if(cnt >= bufmax) resizebuf(); strbuf[cnt++] = c; esc = 0; break; } esc = 1; break; case '"': if(esc == 0) goto done; /* Fall through */ default: if(esc) { c = escchar(c); esc = 0; } if(cnt >= bufmax) resizebuf(); strbuf[cnt++] = c; break; } } done: if(cnt >= bufmax) resizebuf(); strbuf[cnt] = '\0'; s = malloc(sizeof(String)); s->len = cnt+1; s->string = malloc(s->len); memmove(s->string, strbuf, s->len); yylval.string = s; }
/* * get a line that ends in crnl or cr, turn terminating crnl into a nl * * return 0 on EOF */ static int getcrnl(String *s, Biobuf *fp) { int c; for(;;){ c = Bgetc(fp); if(debug) { seek(2, 0, 2); fprint(2, "%c", c); } switch(c){ case 0: break; case -1: goto out; case '\r': c = Bgetc(fp); if(c == '\n'){ if(debug) { seek(2, 0, 2); fprint(2, "%c", c); stamp(); } s_putc(s, '\n'); goto out; } Bungetc(fp); s_putc(s, '\r'); break; case '\n': s_putc(s, c); goto out; default: s_putc(s, c); break; } } out: s_terminate(s); return s_len(s); }
/* * Assemble a line skipping blank lines, comments, and eliding * escaped newlines */ int assline(Biobuf *bp, Bufblock *buf) { int c; int lastc; buf->current=buf->start; while ((c = nextrune(bp, 1)) >= 0){ switch(c) { case '\r': /* consumes CRs for Win95 */ continue; case '\n': if (buf->current != buf->start) { insert(buf, 0); return 1; } break; /* skip empty lines */ case '\\': case '\'': case '"': rinsert(buf, c); if (escapetoken(bp, buf, 1, c) == 0) Exit(); break; case '`': if (bquote(bp, buf) == 0) Exit(); break; case '#': lastc = '#'; while ((c = Bgetc(bp)) != '\n') { if (c < 0) goto eof; if(c != '\r') lastc = c; } mkinline++; if (lastc == '\\') break; /* propagate escaped newlines??*/ if (buf->current != buf->start) { insert(buf, 0); return 1; } break; default: rinsert(buf, c); break; } } eof: insert(buf, 0); return *buf->start != 0; }
static int Bgetdf(void *vp) { int c; struct bgetd *bg = vp; c = Bgetc(bg->b); if(c == Beof) bg->eof = 1; return c; }
void Bgetline(Biobuf *bp, char *s) /* get a line, including newline */ { int c; while ((c = Bgetc(bp)) >= 0) { *s++ = c; if (c == '\n') break; } *s = 0; }
int getc(void) { if(input == nil) return Beof; if(input->fd) return Bgetc(input->fd); if(input->s < input->end) return *(input->s)++; return -1; }
/* * Get a line including a crnl into a string. Convert crnl into nl. */ char * getcrnl(String *s) { int c; int count; count = 0; for(;;){ c = Bgetc(&bin); if(debug) Bputc(&berr, c); switch(c){ case -1: s_append(s, "connection closed unexpectedly by remote system"); s_terminate(s); return 0; case '\r': c = Bgetc(&bin); if(c == '\n'){ case '\n': s_putc(s, c); if(debug) Bputc(&berr, c); count++; s_terminate(s); return s->ptr - count; } Bungetc(&bin); s_putc(s, '\r'); if(debug) Bputc(&berr, '\r'); count++; break; default: s_putc(s, c); count++; break; } } }
void Bgetstr(Biobuf *bp, char *s) /* get a string */ { int c; while ((c = Bgetc(bp)) >= 0) { if (c == ' ' || c == '\t' || c == '\n') { Bungetc(bp); break; } *s++ = c; } *s = 0; }
int getch(void) { int c; if (ugtop > 0) { if (ugbuf[--ugtop] == '\n') yyline++; return ugbuf[ugtop]; } c = Bgetc(bdf); if (c == '\n') yyline++; return c; }
static void dodecode(Biobuf *infile, Pix *a, int nx, int ny, uint8_t *nbitplanes) { int nel, nx2, ny2, bits, mask; Pix *aend, px; nel = nx*ny; nx2 = (nx+1)/2; ny2 = (ny+1)/2; memset(a, 0, nel*sizeof(*a)); /* * Initialize bit input */ start_inputing_bits(); /* * read bit planes for each quadrant */ qtree_decode(infile, &a[0], ny, nx2, ny2, nbitplanes[0]); qtree_decode(infile, &a[ny2], ny, nx2, ny/2, nbitplanes[1]); qtree_decode(infile, &a[ny*nx2], ny, nx/2, ny2, nbitplanes[1]); qtree_decode(infile, &a[ny*nx2+ny2], ny, nx/2, ny/2, nbitplanes[2]); /* * make sure there is an EOF symbol (nybble=0) at end */ if(input_nybble(infile) != 0){ fprint(2, "dodecode: bad bit plane values\n"); exits("format"); } /* * get the sign bits */ aend = &a[nel]; mask = 0; bits = 0;; for(; a<aend; a++) { if(px = *a) { if(mask == 0) { mask = 0x80; bits = Bgetc(infile); } if(mask & bits) *a = -px; mask >>= 1; } } }