Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void parse() {
- arg = malloc(sizeof(char*) * 1);
- if (!arg) { reportError(); return; }
- int pipe, redir;
- if ((pipe = strchr(line, '|')) == NULL && (redir = strchr(line, '>')) == NULL) {
- parseHelper();
- return;
- }
- else if (pipe != NULL) { //piping
- type = 1;
- }
- else if (redir != NULL) { //redirect
- type = 2;
- }
- char* scPtr;
- if (type == 1) {
- scPtr = strchr(line, '|');
- }
- else {
- scPtr = strchr(line, '>');
- }
- //pad
- char padded[strlen(line) + 3];
- strncpy(padded, line, (size_t)(scPtr - line));
- strcat(padded, (t == 1 ? " | " : " > "));
- strcat(padded, scPtr + 1);
- free(line);
- line = realloc(NULL, (strlen(padded) + 1));
- strcpy(line, padded);
- //parse
- char* temp = strtok(line, " \n\t"); //get first arg
- while (temp) {
- arg[totalArgs - 1] = temp;
- totalArgs++; //make it larger, started at 1 for NULL item
- arg = realloc(arg, sizeof(char*) * totalArgs);
- if (!arg) { reportError(); return; }
- temp = strtok(NULL, " \n\t"); //get next
- }
- arg[totalArgs - 1] = (char*)NULL; //setup for execv
- //special helper
- int count = 0;
- for (int i = 0; i < totalArgs-1; i++) { //last arg is always NULL
- if (strchr(arg[i], (type == 1 ? '|' : '>')) != NULL) {
- specArg = i;
- count++;
- }
- }
- if (count > 1) {
- reportError();
- totalArgs = 0; //so it doesn't call run after
- freeAll();
- }
- //TODO: check if more than 1 arg after special character
- }
Add Comment
Please, Sign In to add comment