Guest User

Untitled

a guest
Feb 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. static void parse() {
  2. arg = malloc(sizeof(char*) * 1);
  3. if (!arg) { reportError(); return; }
  4. int pipe, redir;
  5.  
  6. if ((pipe = strchr(line, '|')) == NULL && (redir = strchr(line, '>')) == NULL) {
  7. parseHelper();
  8. return;
  9. }
  10. else if (pipe != NULL) { //piping
  11. type = 1;
  12. }
  13. else if (redir != NULL) { //redirect
  14. type = 2;
  15. }
  16.  
  17. char* scPtr;
  18. if (type == 1) {
  19. scPtr = strchr(line, '|');
  20. }
  21. else {
  22. scPtr = strchr(line, '>');
  23. }
  24.  
  25. //pad
  26. char padded[strlen(line) + 3];
  27. strncpy(padded, line, (size_t)(scPtr - line));
  28. strcat(padded, (t == 1 ? " | " : " > "));
  29. strcat(padded, scPtr + 1);
  30. free(line);
  31. line = realloc(NULL, (strlen(padded) + 1));
  32. strcpy(line, padded);
  33.  
  34. //parse
  35. char* temp = strtok(line, " \n\t"); //get first arg
  36.  
  37. while (temp) {
  38. arg[totalArgs - 1] = temp;
  39.  
  40. totalArgs++; //make it larger, started at 1 for NULL item
  41. arg = realloc(arg, sizeof(char*) * totalArgs);
  42. if (!arg) { reportError(); return; }
  43.  
  44. temp = strtok(NULL, " \n\t"); //get next
  45. }
  46. arg[totalArgs - 1] = (char*)NULL; //setup for execv
  47.  
  48. //special helper
  49. int count = 0;
  50. for (int i = 0; i < totalArgs-1; i++) { //last arg is always NULL
  51. if (strchr(arg[i], (type == 1 ? '|' : '>')) != NULL) {
  52. specArg = i;
  53. count++;
  54. }
  55. }
  56. if (count > 1) {
  57. reportError();
  58. totalArgs = 0; //so it doesn't call run after
  59. freeAll();
  60. }
  61. //TODO: check if more than 1 arg after special character
  62.  
  63. }
Add Comment
Please, Sign In to add comment