/** * Runs the lash program. The given parameters allow the user to specify the * limits the program should operate within. * * @param command The maximum amount of commands to allow * @param args The maximum number of arguments within each command * @param arglength The maximum length of each argument * @param promptlength The maximum length of the prompt on the command line */ void runLash(int command, int args, int arglength, int promptlength){ maxcommands = command; maxargs = args; maxarglength = arglength; maxpromptlength = promptlength; sprintf(prompt, "%s LaSH %% ", getenv("USER")); // ignore all signals that should be passed to jobs signal (SIGINT, sighandler); signal (SIGQUIT, SIG_IGN); signal (SIGTSTP, SIG_IGN); signal (SIGTTIN, SIG_IGN); signal (SIGTTOU, SIG_IGN); // signal (SIGCHLD, sighandler); stifle_history(100); struct LashParser *lash = newLashParser(maxcommands, maxargs, maxarglength); // Loop forever. This will be broken if exit is run int cont = 1; int status; while(cont){ acceptInterrupt = 0; char *input = readline(prompt); if(strcmp(input, "") != 0){ add_history(input); } status = buildCommand(lash, input); if(status == VALID) cont = executeCommand(lash); else if(status == QUOTE_MISMATCH) printf("Error: Quote Mismatch\n"); free(input); clearParser(lash); } free(lash->commands); free(lash); exit(0); }
/* returns * 0 if not enough commands remain or * 1 if commands remain in buffer */ int checkClientBufOnCommand() { int len = (cli.cbuf)->count; char * buf = flushBuffer(cli.cbuf); char * c; Task * t; int stParse; if (len == 0) { debugl(9, "checkClientBufOnCommand: No string to analyze\n"); free(buf); return 0; } debugl(6, "checkClientBufOnCommand: Analyzing string with len=%d: ", len); for (c = buf; (c - buf) < len; c++) debugl(6, "%hhu ", *c); debugl(6, "\n"); debugl(9, "checkClientBufOnCommand: Attempting to get string: "); c = buf; while (*c != '\012' && (c - buf) < len) { debugl(9, "%c(%hhu) ", *c, *c); c++; } debugl(9, "end:%c(%hhu) ss=%d\n", *c, *c, c - buf); if (*c != '\012' || (c - buf) == len) { addnStr(cli.cbuf, buf, len); free(buf); return 0; } else if ((c - buf + 1) < len) { addnStr(cli.cbuf, c + 1, len - (c - buf + 1)); } debugl(9, "checkClientBufOnCommand: String got successfully!\n"); delTask(&t); initParserByString(buf); stParse = parse(&t); clearParser(); if (stParse == PS_OK) { defineTaskType(t); if (t->type != TASK_UNKNOWN) { if (t->type & TASKT_SERVER) { t->type &= ~TASKT_SERVER; sendCommand(t); } else if (t->type &TASKT_CLIENT) { t->type &= ~TASKT_CLIENT; execCommand(t); } } else error("Unknown command!\n"); } else if (stParse == PS_ERROR) echoParserError(); if ((c - buf + 1) == len) return 0; else return 1; }