void token(char *lin,char *tok[],int *ntok,char *outbuff) { int jj; int tj; char *cp; char *p2 = NULL; *ntok = 0; in_quote = false; if (table_loaded==false) token_init(); cp = lin; cp = find_non_space(cp); while (*cp!=0) { if (*cp==' ' || *cp==' ') { *cp = ' '; cp = find_non_space(cp); } if (*cp == '!') goto endofline; p2 = find_term(cp); jj = p2-cp+1; if (jj==0) goto endofline; add_tok(cp,jj); cp = p2 + 1 ; if (*ntok>280) subscript(); } endofline:; if (*ntok>0) { if ( (*tok[*ntok])=='\n' ) (*ntok)--; if (strcmp(tok[*ntok]," ")==0) (*ntok)--; if (*ntok>0) p2 = tok[*ntok] + strlen(tok[*ntok]) - 1; if (*p2==10) *p2 = 0; } }
void process_token (enum tok_kind kind) { int tok = add_tok (yytext, kind); count++; }
int yylex(void) { int c; while ((c = scanc()) != EOF) { /* special handling for quoted string */ if (instring) { if (escaped) { escaped = 0; /* if newline, just eat and forget it */ if (c == '\n') continue; if (strchr("xXd01234567", c)) { unscanc(c); unscanc(esc_char); return (get_wide()); } yylval.wc = get_escaped(c); return (T_CHAR); } if (c == esc_char) { escaped = 1; continue; } switch (c) { case '<': return (get_symbol()); case '>': /* oops! should generate syntax error */ return (T_GT); case '"': instring = 0; return (T_QUOTE); default: yylval.wc = c; return (T_CHAR); } } /* escaped characters first */ if (escaped) { escaped = 0; if (c == '\n') { /* eat the newline */ continue; } hadtok = 1; if (tokidx) { /* an escape mid-token is nonsense */ return (T_NULL); } /* numeric escapes are treated as wide characters */ if (strchr("xXd01234567", c)) { unscanc(c); unscanc(esc_char); return (get_wide()); } add_tok(get_escaped(c)); continue; } /* if it is the escape charter itself note it */ if (c == esc_char) { escaped = 1; continue; } /* remove from the comment char to end of line */ if (c == com_char) { while (c != '\n') { if ((c = scanc()) == EOF) { /* end of file without newline! */ return (EOF); } } assert(c == '\n'); if (!hadtok) { /* * If there were no tokens on this line, * then just pretend it didn't exist at all. */ continue; } hadtok = 0; return (T_NL); } if (strchr(" \t\n;()<>,\"", c) && (tokidx != 0)) { /* * These are all token delimiters. If there * is a token already in progress, we need to * process it. */ unscanc(c); return (consume_token()); } switch (c) { case '\n': if (!hadtok) { /* * If the line was completely devoid of tokens, * then just ignore it. */ continue; } /* we're starting a new line, reset the token state */ hadtok = 0; return (T_NL); case ',': hadtok = 1; return (T_COMMA); case ';': hadtok = 1; return (T_SEMI); case '(': hadtok = 1; return (T_LPAREN); case ')': hadtok = 1; return (T_RPAREN); case '>': hadtok = 1; return (T_GT); case '<': /* symbol start! */ hadtok = 1; return (get_symbol()); case ' ': case '\t': /* whitespace, just ignore it */ continue; case '"': hadtok = 1; instring = 1; return (T_QUOTE); default: hadtok = 1; add_tok(c); continue; } } return (EOF); }
int get_symbol(void) { int c; while ((c = scanc()) != EOF) { if (escaped) { escaped = 0; if (c == '\n') continue; add_tok(get_escaped(c)); continue; } if (c == esc_char) { escaped = 1; continue; } if (c == '\n') { /* well that's strange! */ yyerror("unterminated symbolic name"); continue; } if (c == '>') { /* end of symbol */ /* * This restarts the token from the beginning * the next time we scan a character. (This * token is complete.) */ if (token == NULL) { yyerror("missing symbolic name"); return (T_NULL); } tokidx = 0; /* * A few symbols are handled as keywords outside * of the normal categories. */ if (category == T_END) { int i; for (i = 0; symwords[i].name != 0; i++) { if (strcmp(token, symwords[i].name) == 0) { last_kw = symwords[i].id; return (last_kw); } } } /* * Contextual rule: Only literal characters are * permitted in CHARMAP. Anywhere else the symbolic * forms are fine. */ if ((category != T_CHARMAP) && (lookup_charmap(token, &yylval.wc)) != -1) { return (T_CHAR); } if ((yylval.collsym = lookup_collsym(token)) != NULL) { return (T_COLLSYM); } if ((yylval.collelem = lookup_collelem(token)) != NULL) { return (T_COLLELEM); } /* its an undefined symbol */ yylval.token = strdup(token); token = NULL; toksz = 0; tokidx = 0; return (T_SYMBOL); } add_tok(c); } yyerror("unterminated symbolic name"); return (EOF); }
static int read_initfile(const char *initfile) { struct initline *p; FILE *f; char *buf = NULL; size_t buf_len = 0; int i,j,k; char *ptr, *getty; #ifdef SPECIAL_CONSOLE_TERM char tty[50]; struct stat stb; #endif char *termenv, *getenv(); termenv = getenv("TERM"); /* set by kernel */ /* termenv = "vt100"; */ i = numcmd; if (!(f = fopen(initfile, "r"))) return 1; while(!feof(f)) { if (i+2 == inittab_size) { /* need to realloc inittab */ inittab_size += NUMCMD; inittab = realloc(inittab, inittab_size * sizeof(struct initline)); if (!inittab) { /* failure case - what do you do if init fails? */ err("malloc failed"); _exit(1); } } if (getline(&buf, &buf_len, f) == -1) break; for(k = 0; k < buf_len && buf[k]; k++) { if(buf[k] == '#') { buf[k] = '\0'; break; } } if(buf[0] == '\0' || buf[0] == '\n') continue; p = inittab + i; init_itab(p); p->line = strdup(buf); p->fullline = strdup(buf); if (!p->line || !p->fullline) { err("Not memory to allocate inittab entry"); clear_itab(p); continue; } ptr = strtok(p->line, ":"); if (!ptr) { err("Missing TTY/ID field in inittab"); clear_itab(p); continue; } strncpy(p->tty, ptr, 9); //p->tty[9] = '\0'; ptr = strtok(NULL, ":"); if (!ptr) { err("Missing TERMTYPE field in inittab"); clear_itab(p); continue; } strncpy(p->termcap, ptr, 29); //p->termcap[29] = '\0'; getty = strtok(NULL, " \t\n"); if (!getty) { err("Missing PROCESS field in inittab"); clear_itab(p); continue; } add_tok(p, getty); j = 1; while((ptr = strtok(NULL, " \t\n"))) add_tok(p, ptr); #ifdef SPECIAL_CONSOLE_TERM /* special-case termcap for the console ttys */ strcpy(tty, "/dev/"); strcat(tty, p->tty); if(!termenv || stat(tty, &stb) < 0) { err("no TERM or cannot stat tty\n"); } else { /* is it a console tty? */ if(major(stb.st_rdev) == 4 && minor(stb.st_rdev) < 64) { strncpy(p->termcap, termenv, 30); p->termcap[29] = 0; } } #endif i++; } if (buf) free(buf); fclose(f); numcmd = i; return 0; }
void read_inittab(void) { int i; /* * free any old data and start again */ for (i = 0; i < numcmd; i++) clear_itab(&inittab[i]); numcmd = 0; /* Fake an inittab entry if boot console defined */ #ifdef CONFIG_USER_INIT_CONSOLE_SH #if LINUX_VERSION_CODE < 0x020100 if (console_device && strcmp(console_device, "/dev/null")) #else if (have_console) #endif { struct initline *p; p = inittab + numcmd++; init_itab(p); p->fullline = strdup("console"); strcpy(p->tty, "console"); strcpy(p->termcap, "linux"); add_tok(p, "-/bin/sh"); } #endif i = 0; if (read_initfile(_PATH_INITTAB) == 0) i++; #ifdef CONFIG_USER_FLATFSD_FLATFSD if (read_initfile(_PATH_CONFIGTAB) == 0) i++; #endif if (i == 0) { err("Failed to open " _PATH_INITTAB #ifdef CONFIG_USER_FLATFSD_FLATFSD " or " _PATH_CONFIGTAB #endif "." ); } #ifdef CONFIG_USER_INIT_CONF load_init_conf(); #endif /* if needed, shrink the array using realloc - * must be done here so that we include the results of all init files * when calculating number of commands */ if ((numcmd + 2) < (inittab_size - NUMCMD)) { /* round up from numcmd to the nearest multiple of NUMCMD */ inittab_size = ((numcmd + 2) / NUMCMD + 1) * NUMCMD; inittab = realloc(inittab, inittab_size * sizeof(struct initline)); if (!inittab) { /* failure case - what do you do if init fails? */ err("malloc failed"); _exit(1); } } if (numcmd == 0) _exit(1); }