Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char** parse_command(char input[]){
- static char* strings[100];
- int x = 0;
- while(strings[x] != NULL){
- strings[x] = NULL;
- x++;
- }
- static char* quotations[3];
- x=0;
- while(quotations[x] != NULL){
- quotations[x] = NULL;
- x++;
- }
- int index = 0;
- int iQuotation = 0;
- char* tknQuotation = strtok(input, "\"");
- while(tknQuotation != NULL){
- quotations[iQuotation++] = tknQuotation;
- tknQuotation = strtok(NULL, "\"");
- }
- if(quotations[1] == NULL){
- char* token = strtok(input, " \n"); // split string by space and newline
- while (token != NULL){
- if (token[0] == '$'){
- char* varName = token + 1;
- for(int i = 0; i < varIndex; i++){
- if(!strcmp(varName, varNames[i])){
- strings[index++] = varValues[i];
- }
- }
- }
- else{
- strings[index++] = token ;
- }
- token = strtok(NULL, " \n");
- }
- strings[index] = NULL;
- }
- else{
- for(int i = 0; i < 3; i++) {
- if(i!=1){
- char* token = strtok(quotations[i], " \n");
- while (token != NULL){
- strings[index++] = token;
- token = strtok(NULL, " \n");
- }
- }
- else{
- char* q[100];
- int j = 0;
- char* tkn = strtok(quotations[i], " \n");
- while(tkn != NULL){
- if(tkn[0] == '$'){
- char* varName = tkn + 1;
- for(int k = 0; k < varIndex; k++){
- if(!strcmp(varName, varNames[k])){
- char* token = strtok(varValues[k], "\"");
- q[j++] = token;
- }
- }
- }
- else {
- q[j++] = tkn;
- }
- tkn = strtok(NULL, " \n");
- }
- strings[index++] = q;
- }
- }
- }
- return strings;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement