Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8.  
  9. #define BUF_SIZE 1024
  10. #define MAX_ARGS 32
  11.  
  12. char* readline(char* buf, size_t size) {
  13.    
  14.     int length;
  15.    
  16.     if (fgets(buf, BUF_SIZE, stdin) != NULL) {
  17.            
  18.         length = strlen(buf);
  19.         while (buf[length - 1] == '\n' && length != 0) {
  20.             buf[length - 1] = '\0';
  21.             length -= 1;
  22.         }
  23.        
  24.         // ignoring whitespace check for now
  25.         if (length == 0) {
  26.             return NULL;
  27.         }
  28.        
  29.     } else {
  30.         return NULL;
  31.     }
  32.        
  33.     return buf;
  34. }
  35.  
  36. bool speechmarkArg(char* str, char** args, int* index, int* argCount) {
  37.    
  38.     static const char key[] = {"\""};
  39.     int length = strlen(str);
  40.    
  41.     // increment index to point to the first character of the argument
  42.     *index += 1;
  43.    
  44.     // get the length of the argument
  45.     int argLength = strcspn(str + (*index), key);
  46.    
  47.     // no closing speechmark was found, return an error
  48.     if ((*index + argLength) == length) {
  49.         return false;
  50.     }
  51.    
  52.     // store the new argument in the array of arguments
  53.     args[*argCount] = malloc((argLength + 1) * sizeof(char));
  54.     memcpy(args[*argCount], str + (*index), argLength);
  55.     strcat(args[*argCount], "\0");
  56.    
  57.     // update index to point to the closing speechmark. Upon returning
  58.     // to the splut function, the for loop will immediately cycle past
  59.     *argCount += 1;
  60.     *index += argLength;
  61.    
  62.     // return true on success
  63.     return true;
  64. }
  65.  
  66. bool whitespaceArg(char* str, char** args, int* index, int* argCount) {
  67.    
  68.     static const char key[] = {"\" "};
  69.     char delim;
  70.    
  71.     // get the length of the argument
  72.     int argLength = strcspn(str + (*index), key);
  73.    
  74.     // store the delimiting character
  75.     delim = str[*index + argLength];
  76.    
  77.     // store the new argument in the array of arguments
  78.     args[*argCount] = malloc((argLength + 1) * sizeof(char));
  79.     memcpy(args[*argCount], str + (*index), argLength);
  80.     strcat(args[*argCount], "\0");
  81.    
  82.     // update the index and argcount
  83.     *index += argLength;
  84.     *argCount += 1;
  85.    
  86.     // either return to the split function immediatley or
  87.     // create add another argument based on the delimiting
  88.     // character which was found
  89.    
  90.     if (delim == ' ' ||  delim == '\0') {
  91.         return true;
  92.     } else {
  93.         if (!speechmarkArg(str, args, index, argCount)) {
  94.             return false;
  95.         }
  96.     }
  97.    
  98.     // return true on success
  99.     return true;
  100. }
  101.  
  102.  
  103. void split(char* buf, char** args, size_t max) {
  104.    
  105.     int length   = strlen(buf);
  106.     int argCount = 0;
  107.     for (int i = 0; i < length; i += 1) {
  108.         if (buf[i] == ' ') {
  109.             continue;
  110.         } else if (buf[i] == '\"') {
  111.             if (!speechmarkArg(buf, args, &i, &argCount)) {
  112.                 puts("error: expected closing quotation mark");
  113.                 break;
  114.             }
  115.         } else {
  116.             if (!whitespaceArg(buf, args, &i, &argCount)) {
  117.                 puts("error: expected closing quotation mark");
  118.                 break;
  119.             }
  120.         }
  121.     }
  122.     args[argCount] = NULL;
  123.    
  124. }
  125.  
  126. int main() {
  127.    
  128.     char buffer[BUF_SIZE];
  129.     char** args = malloc(MAX_ARGS * sizeof(char*));
  130.    
  131.     while (true) {
  132.        
  133.         if (readline(buffer, BUF_SIZE) != NULL) {
  134.             split(buffer, args, MAX_ARGS);
  135.         }
  136.        
  137.         for(int i = 0;; i += 1) {
  138.             if (args[i] == NULL) {
  139.                 break;
  140.             }
  141.             puts(args[i]);
  142.         }
  143.        
  144.     }
  145.  
  146.     free (args);                
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement