Ejemplo n.º 1
0
/**
 * Runs computations and stores matrices based on given input
 */
void compute_engine(void) {

    g_entries = calloc(MAX_ENTRIES, sizeof(entry*));

    while (true) {
        printf("> ");

        char line[MAX_BUFFER];
        if (fgets(line, MAX_BUFFER, stdin) == NULL) {
            command_bye();
        }

        char command[MAX_BUFFER];
        if (sscanf(line, "%s", command) != 1) {
            printf("\n");
            continue;
        }

        if (strcasecmp(command, "bye") == 0) {
            command_bye();
        } else if (strcasecmp(command, "help") == 0) {
            command_help();
        } else if (strcasecmp(command, "set") == 0) {
            command_set(line);
        } else if (strcasecmp(command, "show") == 0) {
            command_show(line);
        } else if (strcasecmp(command, "compute") == 0) {
            command_compute(line);
        } else {
            puts("invalid command");
        }

        printf("\n");
    }
}
Ejemplo n.º 2
0
int main(void) {

	char line[MAX_LINE];
	entry *entryHead;       //head of entry list
	snapshot *snapHead;		//head of snapshot list
	entryHead = NULL;  
	snapHead = NULL;
	
	while (true) {
		printf("> ");

		if (fgets(line, MAX_LINE, stdin) == NULL) {
			printf("\n");
			command_bye(entryHead, snapHead);
			return 0;
		}
		
		//get command from the input line as well as a second parameter if it exists
		char *ptr = strtok(line, " ");
		char firstToken[MAX_COMMAND];    //stores a command
		strcpy(firstToken, ptr);
		ptr = strtok(NULL, " ");
		char secondToken[MAX_KEY];  	 //stores either a key or a second part of the command
		secondToken[0] = '\0';
		
		//get rid of a new line character if it exists in the last valid token entered
		char *newLine;
		if (ptr != NULL) {		
			strcpy(secondToken, ptr);
			newLine = strchr(secondToken, '\n');
		}
		else {
			newLine = strchr(firstToken, '\n');
		}
		if (newLine) {
			*newLine = 0;	
		}
		
		//identify the command and call the right function to execute the command entered
		if (strcasecmp(firstToken, "BYE") == 0) {
			command_bye(entryHead, snapHead);
			return 0;
		}
		else if (strcasecmp(firstToken, "HELP") == 0) {
			command_help();
		}
		else if (strcasecmp(firstToken, "LIST") == 0) {
			if (strcasecmp(secondToken, "ENTRIES") == 0) {
				list_entries(entryHead);	
			}
			else if (strcasecmp(secondToken, "KEYS") == 0) {
				list_keys(entryHead);
			}
			else if(strcasecmp(secondToken, "SNAPSHOTS") == 0) {
				list_snapshots(snapHead);
			}
			else {
				printf("unknown\n");	
			}
		}
		else if (strcasecmp(firstToken, "GET") == 0) {
			get(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "DEL") == 0) {
			entryHead = del(entryHead, secondToken);
		}
		else if (strcasecmp(firstToken, "PURGE") == 0) {
			entryHead = purge(secondToken, entryHead, snapHead);
		}
		else if (strcasecmp(firstToken, "SET") == 0) {
			if (ptr != NULL && secondToken[0] != '\0') {
				entryHead = set(ptr, secondToken, entryHead);
			}
			else {
				printf("invalid input\n");	
			}
		}
		else if (strcasecmp(firstToken, "PUSH") == 0 || strcasecmp(firstToken, "APPEND") == 0) {
			push_append(ptr, secondToken, entryHead, firstToken);
		}
		else if (strcasecmp(firstToken, "PICK") == 0) {
			ptr = strtok(NULL, " ");
			int index = atoi(ptr);
			pick(index, secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "PLUCK") == 0) {
			ptr = strtok(NULL, " ");
			int index = atoi(ptr);
			pluck(index, secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "POP") == 0) {
			pluck(1, secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "DROP") == 0) {
			snapHead = drop(snapHead, atoi(secondToken));
		}
		else if (strcasecmp(firstToken, "ROLLBACK") == 0) {
			entryHead = checkout(atoi(secondToken), entryHead, snapHead);
			snapHead = remove_snapshots(atoi(secondToken), snapHead);
		}
		else if (strcasecmp(firstToken, "CHECKOUT") == 0) {
			entryHead = checkout(atoi(secondToken), entryHead, snapHead);
		}
		else if (strcasecmp(firstToken, "SNAPSHOT") == 0) {
			snapHead = take_snapshot(entryHead, snapHead);
		}
		else if (strcasecmp(firstToken, "MIN") == 0) {
			min(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "MAX") == 0) {
			max(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "SUM") == 0) {
			sum(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "LEN") == 0) {
			len(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "REV") == 0) {
			reverse(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "UNIQ") == 0) {
			unique(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "SORT") == 0) {
			sort(secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "DIFF") == 0) {
			diff(ptr, secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "INTER") == 0) {
			inter(ptr, secondToken, entryHead);
		}
		else if (strcasecmp(firstToken, "UNION") == 0) {
			union_oper(ptr, secondToken, entryHead);
		}
		else {
			printf("unknown\n");	
		}
		
		printf("\n");
  	}

	return 0;
}