int definedirective() { char word[MAXWORD]; int c1,c2,c3; /* read define*/ c1 = readword(word); if (c1 == EOF) { printf("#%s", word); return EOF; } else if (str_cmp(word, "define") == 0) { /* #define */ c2 = readword(word); /* search word */ if (c2 == EOF) { printf("#define%c%s", c1, word); return c2; } else if (isidentifier(word)) { char def[MAXWORD]; c3 = readword(def); if (strlen(def) == 0) printf("#define%c%s%c%s", c1, word, c2, def); else { install(word, def); } if (c3 != EOF) putchar(c3); return c3; } else { printf("#define%c%s", c1, word); return c2; } } else { printf("#%s%c", word, c1); return readprintline(); } }
int parse (char *buf, Command *c) { int n; Pgm *cmd0; char *t = buf; char *tok; init(); c->rstdin = NULL; c->rstdout = NULL; c->rstderr = NULL; c->bakground = 0; /* false */ c->pgm = NULL; newcmd: if ((n = acmd(t, &cmd0)) <= 0) return -1; t += n; cmd0->next = c->pgm; c->pgm = cmd0; newtoken: n = nexttoken(t, &tok); if (n == 0) { return 1; } t += n; switch(*tok) { case PIPE: goto newcmd; break; case BG: n = nexttoken(t, &tok); if (n == 0) { c->bakground = 1; return 1; } else { fprintf(stderr, "illegal bakgrounding\n"); return -1; } break; case RIN: if (c->rstdin != NULL) { fprintf(stderr, "duplicate redirection of stdin\n"); return -1; } if ((n = nexttoken(t, &(c->rstdin))) < 0) return -1; if (!isidentifier(c->rstdin)) { fprintf(stderr, "Illegal filename: \"%s\"\n", c->rstdin); return -1; } t += n; goto newtoken; break; case RUT: if (c->rstdout != NULL) { fprintf(stderr, "duplicate redirection of stdout\n"); return -1; } if ((n = nexttoken(t, &(c->rstdout))) < 0) return -1; if (!isidentifier(c->rstdout)) { fprintf(stderr, "Illegal filename: \"%s\"\n", c->rstdout); return -1; } t += n; goto newtoken; break; default: return -1; } goto newcmd; }
/* --- parse the commandline and build shell commmand structure --- */ int parsecommand(char *cmdline, Shellcmd *shellcmd) { int i, n; Cmd *cmd0; char *t = cmdline; char *tok; // Initialize list for (i = 0; i < COMMANDMAX-1; i++) cmdbuf[i].next = &cmdbuf[i+1]; cmdbuf[COMMANDMAX-1].next = NULL; cmds = cmdbuf; cp = cbuf; pp = pbuf; shellcmd->rd_stdin = NULL; shellcmd->rd_stdout = NULL; shellcmd->rd_stderr = NULL; shellcmd->background = 0; // false shellcmd->the_cmds = NULL; do { if ((n = acmd(t, &cmd0)) <= 0) return -1; t += n; cmd0->next = shellcmd->the_cmds; shellcmd->the_cmds = cmd0; int newtoken = 1; while (newtoken) { n = nexttoken(t, &tok); if (n == 0) { return 1; } t += n; switch(*tok) { case PIPE: newtoken = 0; break; case BG: n = nexttoken(t, &tok); if (n == 0) { shellcmd->background = 1; return 1; } else { fprintf(stderr, "illegal bakgrounding\n"); return -1; } newtoken = 0; break; case RIN: if (shellcmd->rd_stdin != NULL) { fprintf(stderr, "duplicate redirection of stdin\n"); return -1; } if ((n = nexttoken(t, &(shellcmd->rd_stdin))) < 0) return -1; if (!isidentifier(shellcmd->rd_stdin)) { fprintf(stderr, "Illegal filename: \"%s\"\n", shellcmd->rd_stdin); return -1; } t += n; break; case RUT: if (shellcmd->rd_stdout != NULL) { fprintf(stderr, "duplicate redirection of stdout\n"); return -1; } if ((n = nexttoken(t, &(shellcmd->rd_stdout))) < 0) return -1; if (!isidentifier(shellcmd->rd_stdout)) { fprintf(stderr, "Illegal filename: \"%s\"\n", shellcmd->rd_stdout); return -1; } t += n; break; default: return -1; } } } while (1); return 0; }
static void write_identifier(io::stream& e, const char* value) { if(isidentifier(value)) e << value; else e << "'" << value << "'"; }