/* * Receive a packet. */ static int kgdb_recv(u_char *bp, int maxlen) { u_char *p; int c, csum, tmpcsum; int len; DPRINTF(("kgdb_recv: ")); do { p = bp; csum = len = 0; while ((c = GETC()) != KGDB_START) DPRINTF(("%c",c)); DPRINTF(("%c Start ",c)); while ((c = GETC()) != KGDB_END && len < maxlen) { DPRINTF(("%c",c)); c &= 0x7f; csum += c; *p++ = c; len++; } csum &= 0xff; *p = '\0'; DPRINTF(("%c End ", c)); if (len >= maxlen) { DPRINTF(("Long- ")); PUTC(KGDB_BADP); continue; } tmpcsum = csum; c = GETC(); DPRINTF(("%c",c)); csum -= digit2i(c) * 16; c = GETC(); DPRINTF(("%c",c)); csum -= digit2i(c); if (csum == 0) { DPRINTF(("Good+ ")); PUTC(KGDB_GOODP); /* Sequence present? */ if (bp[2] == ':') { DPRINTF(("Seq %c%c ", bp[0], bp[1])); PUTC(bp[0]); PUTC(bp[1]); len -= 3; kgdb_copy(bp + 3, bp, len); } break; } DPRINTF((" Bad(wanted %x, off by %d)- ", tmpcsum, csum)); __USE(tmpcsum); PUTC(KGDB_BADP); } while (1); DPRINTF(("kgdb_recv: %s\n", bp)); return (len); }
static int getnum(int *chp, ftnlen *val) { register int ch, sign; register ftnlen x; while(GETC(ch) <= ' ' && ch >= 0); if (ch == '-') { sign = 1; GETC(ch); } else { sign = 0; if (ch == '+') GETC(ch); } x = ch - '0'; if (x < 0 || x > 9) return 115; while(GETC(ch) >= '0' && ch <= '9') x = 10*x + ch - '0'; while(ch <= ' ' && ch >= 0) GETC(ch); if (ch == EOF) return EOF; *val = sign ? -x : x; *chp = ch; return 0; }
/* lifted from img.c Should be put somewhere common? JPNP*/ static char * getline_alloc(FILE *fh, size_t ilen) { int ch; size_t i = 0; size_t len = ilen; char *buf = xosmalloc(len); if (!buf) return NULL; ch = GETC(fh); while (ch != '\n' && ch != '\r' && ch != EOF) { buf[i++] = ch; if (i == len - 1) { char *p; len += len; p = xosrealloc(buf, len); if (!p) { osfree(buf); return NULL; } buf = p; } ch = GETC(fh); } if (ch == '\n' || ch == '\r') { int otherone = ch ^ ('\n' ^ '\r'); ch = GETC(fh); /* if it's not the other eol character, put it back */ if (ch != otherone) ungetc(ch, fh); } buf[i++] = '\0'; return buf; }
int l_C() { int ch; if(lcount>0) return(0); ltype=0; for(GETC(ch);isblnk(ch);GETC(ch)); if(ch==',') { lcount=1; return(0); } if(ch=='/') { lquit=1; return(0); } if(ch!='(') { if(fscanf(cf,"%d",&lcount)!=1) { if(!feof(cf)) err(elist->cierr,112,"no rep") else err(elist->cierr,(EOF),"lread"); } if(GETC(ch)!='*') { ungetc(ch,cf); if(!feof(cf)) err(elist->cierr,112,"no star") else err(elist->cierr,(EOF),"lread"); } if(GETC(ch)!='(') { ungetc(ch,cf); return(0); } }
static int l_L(Void) { int ch, rv, sawdot; if(f__lcount>0) return(0); f__lcount = 1; f__ltype=0; GETC(ch); if(isdigit(ch)) { rd_count(ch); if(GETC(ch)!='*') { if(!f__cf || !feof(f__cf)) { errfl(f__elist->cierr,112,"no star"); } else { err(f__elist->cierr,(EOF),"lread"); } } GETC(ch); } sawdot = 0; if(ch == '.') { sawdot = 1; GETC(ch); } switch(ch) { case 't': case 'T': if (nml_read && Lfinish(ch, sawdot, &rv)) return rv; f__lx=1; break; case 'f': case 'F': if (nml_read && Lfinish(ch, sawdot, &rv)) return rv; f__lx=0; break; default: if(isblnk(ch) || issep(ch) || ch==EOF) { (void) Ungetc(ch,f__cf); return(0); } if (nml_read > 1) { Ungetc(ch,f__cf); f__lquit = 2; return 0; } errfl(f__elist->cierr,112,"logical"); } f__ltype=TYLONG; while(!issep(GETC(ch)) && ch!=EOF); Ungetc(ch, f__cf); return(0); }
/* * getC to read skipping the comments from the file. * * Each comment returns a newline or EOF * * Reading strings normal getc is used! */ static int getC(tpLspObject pLSP, FILE *f){ int ch; if( (ch=GETC(f)) == ';' ) while( (ch=GETC(f)) != '\n' && ch != EOF ) ; return ch; }
int l_R() { double a,b,c,d; int i,ch,sign=0,da,db,dc; a=b=c=d=0; da=db=dc=0; if(lcount>0) return(0); ltype=0; for(GETC(ch);isblnk(ch);GETC(ch)); if(ch==',') { lcount=1; return(0); } if(ch=='/') { lquit=1; return(0); } ungetc(ch,cf); da=rd_int(&a); if(da== -1) sign=da; if(GETC(ch)!='*') { ungetc(ch,cf); db=1; b=a; a=1; } else db=rd_int(&b); if(GETC(ch)!='.') { dc=c=0; ungetc(ch,cf); } else dc=rd_int(&c); if(isexp(GETC(ch))) db=rd_int(&d); else { ungetc(ch,cf); d=0; } lcount=a; if(!db && !dc) return(0); if(db && b<0) { sign=1; b = -b; } for(i=0;i<dc;i++) c/=10; b=b+c; for(i=0;i<d;i++) b *= 10; for(i=0;i< -d;i++) b /= 10; if(sign) b = -b; ltype=TYLONG; lx=b; return(0); }
/* * Receive a packet. */ static int kgdb_recv(u_char *bp, int maxlen) { u_char *p; int c, csum; int len; do { p = bp; csum = len = 0; while ((c = GETC()) != KGDB_START) ; while ((c = GETC()) != KGDB_END && len < maxlen) { c &= 0x7f; csum += c; *p++ = c; len++; } csum &= 0xff; *p = '\0'; if (len >= maxlen) { PUTC(KGDB_BADP); continue; } csum -= digit2i(GETC()) * 16; csum -= digit2i(GETC()); if (csum == 0) { PUTC(KGDB_GOODP); /* Sequence present? */ if (bp[2] == ':') { PUTC(bp[0]); PUTC(bp[1]); len -= 3; kgdb_copy(bp + 3, bp, len); } break; } PUTC(KGDB_BADP); } while (1); #ifdef DEBUG_KGDB printf("kgdb_recv: %s\n", bp); #endif return (len); }
static ZZJSON *parse_value(ZZJSON_CONFIG *config) { ZZJSON *retval = NULL; int c; SKIPWS(); c = GETC(); UNGETC(c); switch (c) { case '"': retval = parse_string2(config); break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': retval = parse_number(config); break; case '{': retval = parse_object(config); break; case '[': retval = parse_array(config); break; case 't': retval = parse_true(config); break; case 'f': retval = parse_false(config); break; case 'n': retval = parse_null(config); break; } if (!retval) { ERROR("value: invalid value"); return retval; } return retval; }
void wordcount(FILE_TYPE file, long *result) { int c; long linec, wordc, charc; int inword = 0; linec = wordc = charc = 0; while ((c = GETC(file)) != EOF) { charc++; if (c == '\n') linec++; if (isspace(c)) { if (inword) inword = 0; } else { if (!inword) { inword = 1; wordc++; } } } result[0] = linec; result[1] = wordc; result[2] = charc; }
Lfinish(int ch, int dot, int *rvp) #endif { char *s, *se; static char what[] = "namelist input"; s = nmLbuf + 2; se = nmLbuf + sizeof(nmLbuf) - 1; *s++ = ch; while(!issep(GETC(ch)) && ch!=EOF) { if (s >= se) { nmLbuf_ovfl: return *rvp = err__fl(f__elist->cierr,131,what); } *s++ = ch; if (ch != '=') continue; if (dot) return *rvp = err__fl(f__elist->cierr,112,what); got_eq: *s = 0; nmL_getc_save = l_getc; l_getc = nmL_getc; nmL_ungetc_save = l_ungetc; l_ungetc = nmL_ungetc; nmLbuf[1] = *(nmL_next = nmLbuf) = ','; *rvp = f__lcount = 0; return 1; } if (dot) goto done; for(;;) { if (s >= se) goto nmLbuf_ovfl; *s++ = ch; if (!isblnk(ch)) break; if (GETC(ch) == EOF) goto done; } if (ch == '=') goto got_eq; done: Ungetc(ch, f__cf); return 0; }
/* scanerror -- called for lexical errors */ static void scanerror(char *s) { int c; /* TODO: check previous character? rc's last hack? */ while ((c = GETC()) != '\n' && c != EOF) ; goterror = TRUE; yyerror(s); }
int rd_int(double *x) { int ch,sign=0,i; double y; i=0; y=0; if(GETC(ch)=='-') sign = -1; else if(ch=='+') sign=0; else ungetc(ch,cf); while(isdigit(GETC(ch))) { i++; y=10*y+ch-'0'; } ungetc(ch,cf); if(sign) y = -y; *x = y; return(y!=0?i:sign); }
static Boolean getfds(int fd[2], int c, int default0, int default1) { int n; fd[0] = default0; fd[1] = default1; if (c != '[') { UNGETC(c); return TRUE; } if ((unsigned int) (n = GETC() - '0') > 9) { scanerror("expected digit after '['"); return FALSE; } while ((unsigned int) (c = GETC() - '0') <= 9) n = n * 10 + c; fd[0] = n; switch (c += '0') { case '=': if ((unsigned int) (n = GETC() - '0') > 9) { if (n != ']' - '0') { scanerror("expected digit or ']' after '='"); return FALSE; } fd[1] = CLOSED; } else { while ((unsigned int) (c = GETC() - '0') <= 9) n = n * 10 + c; if (c != ']' - '0') { scanerror("expected ']' after digit"); return FALSE; } fd[1] = n; } break; case ']': break; default: scanerror("expected '=' or ']' after digit"); return FALSE; } return TRUE; }
int getpw(uid_t uid, char buf[]) { int n, c; char *bp; FILE *fp; rmutex_t *lk; if (pwf == NULL) { fp = fopen(PASSWD, "rF"); lmutex_lock(&_pwlock); if (pwf == NULL) { if ((pwf = fp) == NULL) { lmutex_unlock(&_pwlock); return (1); } fp = NULL; } lmutex_unlock(&_pwlock); if (fp != NULL) /* someone beat us to it */ (void) fclose(fp); } FLOCKFILE(lk, pwf); _rewind_unlocked(pwf); for (;;) { bp = buf; while ((c = GETC(pwf)) != '\n') { if (c == EOF) { FUNLOCKFILE(lk); return (1); } *bp++ = (char)c; } *bp = '\0'; bp = buf; n = 3; while (--n) while ((c = *bp++) != ':') if (c == '\n') { FUNLOCKFILE(lk); return (1); } while ((c = *bp++) != ':') if (isdigit(c)) n = n*10+c-'0'; else continue; if (n == uid) { FUNLOCKFILE(lk); return (0); } } }
static int getname(register char *s, int slen) { register char *se = s + slen - 1; register int ch; GETC(ch); if (!(*s++ = Alpha[ch & 0xff])) { if (ch != EOF) ch = 115; errfl(f__elist->cierr, ch, "namelist read"); } while(*s = Alphanum[GETC(ch) & 0xff]) if (s < se) s++; if (ch == EOF) err(f__elist->cierr, EOF, "namelist read"); if (ch > ' ') Ungetc(ch,f__cf); return *s = 0; }
rd_count(register int ch) #endif { if (ch < '0' || ch > '9') return 1; f__lcount = ch - '0'; while(GETC(ch) >= '0' && ch <= '9') f__lcount = 10*f__lcount + ch - '0'; Ungetc(ch,f__cf); return f__lcount <= 0; }
static int fail(int st) { int c; while((c=GETC()) >= 0 && c != '\n') ; if(c == '\n') UNGETC(c); setstate(st); return Terror; }
void process (struct iop *io) { while (1) { int c = GETC (io); if (c == EOF) break; usleep (3000); PUTC (io, c); FLUSH (io); } if (io->fd == -1) { PUTC (io, '\n'); } }
static int skip(char *s) { int n, c; n = 0; while((c=GETC()) >= 0 && strchr(s, c)) n++; if(c != Beof) UNGETC(c); return n; }
int read_some(char *buf, int n, int start_ch, int copy) /* if copy is not 0 then copy characters to stdout while scanning to find start_ch. When you find it, read n characters into buf, return the number of characters read into buf, but these characters MUST be free of start_ch. */ { int ch; int prev = 0; while (1) { ch =GETC(stdin); if (ch == EOF) return -1; if (copy) {putc(ch,stdout); if (prev == '\n' && ch == '{') { fprintf(stderr,"Error(at char %d):found \\n{ inside section to copy\n",pos) ; exit(1);} prev = ch; } AGAIN: if (ch == start_ch) { int i = 0; while (i < n) { ch = GETC(stdin); if (ch == EOF) return i; if (copy) {putc(ch,stdout); if (prev == '\n' && ch == '{') { fprintf(stderr,"Error(at char %d):found \\n{ inside section to copy",pos) ; exit(1);} prev = ch; } if (ch == start_ch) goto AGAIN; buf[i++] = ch; } return i; }}}
ZZJSON *zzjson_parse(ZZJSON_CONFIG *config) { ZZJSON *retval; int c; SKIPWS(); c = GETC(); UNGETC(c); if (c == '[') retval = parse_array(config); else if (c == '{') retval = parse_object(config); else { ERROR("expected '[' or '{'"); return NULL; } if (!retval) return NULL; SKIPWS(); c = GETC(); if (c >= 0 && !ALLOW_GARBAGE_AT_END) { ERROR("parse: garbage at end of file"); zzjson_free(config, retval); return NULL; } return retval; }
void process (struct iop *io) { while (1) { int d; int c = GETC (io); if (c == EOF) break; if (c >= '!' && c <= '~') { c = ('!' + '~') - c; } PUTC (io, c); } if (io->fd == -1) { PUTC (io, '\n'); } }
static int getcc(char *c, char *s) /* returning inline cmd or 0, c as a text */ { int c1, c2; c1 = GETC(); if(c1 == Beof || c1 == '\n') return c1; if(strchr(s, c1) == nil){ *c = c1; return 0; } c2 = GETC(); if(c2 == c1){ *c = c1; return 0; } UNGETC(c2); return c1; }
static char * __getpass(const char *prompt, int size) { struct termio ttyb; unsigned short flags; char *p; int c; FILE *fi; char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL); struct sigaction act, osigint, osigtstp; if (pbuf == NULL || (fi = fopen("/dev/tty", "r+F")) == NULL) return (NULL); setbuf(fi, NULL); intrupt = 0; act.sa_flags = 0; act.sa_handler = catch; (void) sigemptyset(&act.sa_mask); (void) sigaction(SIGINT, &act, &osigint); /* trap interrupt */ act.sa_handler = SIG_IGN; (void) sigaction(SIGTSTP, &act, &osigtstp); /* ignore stop */ (void) ioctl(fileno(fi), TCGETA, &ttyb); flags = ttyb.c_lflag; ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); (void) ioctl(fileno(fi), TCSETAF, &ttyb); (void) fputs(prompt, fi); p = pbuf; while (!intrupt && (c = GETC(fi)) != '\n' && c != '\r' && c != EOF) { if (p < &pbuf[ size ]) *p++ = (char)c; } *p = '\0'; (void) PUTC('\n', fi); ttyb.c_lflag = flags; (void) ioctl(fileno(fi), TCSETAW, &ttyb); (void) sigaction(SIGINT, &osigint, NULL); (void) sigaction(SIGTSTP, &osigtstp, NULL); (void) fclose(fi); if (intrupt) { /* if interrupted erase the input */ pbuf[0] = '\0'; (void) kill(getpid(), SIGINT); } return (pbuf); }
/* getherevar -- read a variable from a here doc */ extern Tree *getherevar(void) { int c; char *s; Buffer *buf = openbuffer(0); while (!dnw[c = GETC()]) buf = bufputc(buf, c); s = sealcountedbuffer(buf); if (buf->len == 0) { yyerror("null variable name in here document"); return NULL; } if (c != '^') UNGETC(c); return flatten(mk(nVar, mk(nWord, s)), " "); }
/* * Send a packet. */ static void kgdb_send(const u_char *bp) { const u_char *p; u_char csum, c; DPRINTF(("kgdb_send: %s\n", bp)); do { p = bp; PUTC(KGDB_START); for (csum = 0; (c = *p); p++) { PUTC(c); csum += c; } PUTC(KGDB_END); PUTC(i2digit(csum >> 4)); PUTC(i2digit(csum)); } while ((c = GETC() & 0x7f) == KGDB_BADP); }
static int rreadstr(char *delim, char *s) /* if s != 0, skip this chars before reading */ { int c; s_reset(sbuf); if(s) skip(s); while((c=GETC()) >= 0){ if(strchr(delim, c)){ UNGETC(c); s_terminate(sbuf); return Tstring; } s_putc(sbuf, c); } if(c == Beof) yyerror("eof in string"); return Terror; }
static ZZJSON *parse_literal(ZZJSON_CONFIG *config, char *s, ZZJSON_TYPE t) { char b[strlen(s)+1]; unsigned int i; for (i=0; i<strlen(s); i++) b[i] = GETC(); b[i] = 0; if (!strcmp(b,s)) { ZZJSON *zzjson; zzjson = config->calloc(1, sizeof(ZZJSON)); if (!zzjson) { MEMERROR(); return NULL; } zzjson->type = t; return zzjson; } ERROR("literal: expected %s", s); return NULL; }
static char * __getpass(const char *prompt, int size) { struct termio ttyb; unsigned short flags; char *p; int c; FILE *fi; char *pbuf = tsdalloc(_T_GETPASS, MAXPASSWD + 1, NULL); void (*sig)(int); rmutex_t *lk; if (pbuf == NULL || (fi = fopen("/dev/tty", "rF")) == NULL) return (NULL); setbuf(fi, NULL); sig = signal(SIGINT, catch); intrupt = 0; (void) ioctl(FILENO(fi), TCGETA, &ttyb); flags = ttyb.c_lflag; ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); (void) ioctl(FILENO(fi), TCSETAF, &ttyb); FLOCKFILE(lk, stderr); (void) fputs(prompt, stderr); p = pbuf; while (!intrupt && (c = GETC(fi)) != '\n' && c != '\r' && c != EOF) { if (p < &pbuf[ size ]) *p++ = (char)c; } *p = '\0'; ttyb.c_lflag = flags; (void) ioctl(FILENO(fi), TCSETAW, &ttyb); (void) PUTC('\n', stderr); FUNLOCKFILE(lk); (void) signal(SIGINT, sig); (void) fclose(fi); if (intrupt) (void) kill(getpid(), SIGINT); return (pbuf); }