#include #include #include #define FAIL(var, str, code) if (var == NULL) { \ fprintf(stderr, str); \ return 1; \ } /* struct cmd_t { char *cmd; unsigned short int type; unsigned short int num; unsigned short int next; unsigned short int other_next; }; */ unsigned short int detect_command_type(char *command); unsigned short int detect_next_command(char *command, unsigned short int is_searching_for_second_next); unsigned short int detect_command_number(char *command); /* * argv[1] must be a tape and argv[2] must be commands */ int main(int argc, char *argv[]) { if (argc < 3) return 1; char **stack; /* command stack */ unsigned short int s = 0; /* stack size */ char *_tmp_cell; _tmp_cell = strtok(argv[2], ";"); stack = malloc((s + 1) * sizeof(char*)); FAIL(stack, "error: could not allocate memory for stack variable.", 1) while (_tmp_cell != NULL) { stack = realloc(stack,(s + 1) * sizeof(char*)); FAIL(stack, "error: could not reallocate memory for stack variable.", 1) stack[s] = malloc((strlen(_tmp_cell) + 1) * sizeof(char)); FAIL(stack, "error: could not allocate memory for cell in stack.", 1) strcpy(stack[s], _tmp_cell); s++; _tmp_cell = strtok(NULL, ";"); } for (int i = 0;i < s;i++) printf("\ncommand: %s\ntype: %hd\nnext: %hd\nsecond next: %hd\nnumber: %hd\n", stack[i], detect_command_type(stack[i]), detect_next_command(stack[i], 0), detect_next_command(stack[i], 1), detect_command_number(stack[i])); printf("\n"); free(stack); return 0; } /* * > - 1 * < - 2 * ^ - 3 * | - 4 * ! - 5 * ? - 6 */ unsigned short int detect_command_type(char *command) { for (int i = 0;i < strlen(command);i++) { switch (command[i]) { case '>': return 1; case '<': return 2; case '^': return 3; case '|': return 4; case '!': return 5; case '?': return 6; } } return 0; } unsigned short int detect_next_command(char *command, unsigned short int is_searching_for_second_next) { unsigned short int type = detect_command_type(command); if (type == 5) return 0; unsigned short int number = 0; char *_command; char *_tmp_number; _command = malloc((strlen(command) + 1) * sizeof(char)); FAIL(_command, "error: could not allocate memory for _command variable.", 1) strcpy(_command, command); _tmp_number = strtok(_command, "><^|!?"); _tmp_number = strtok(NULL, "><^|!?"); if (is_searching_for_second_next) { if (type != 6) return 0; char *__tmp_number; char *___tmp_number; ___tmp_number = malloc((strlen(_tmp_number) + 1) * sizeof(char)); FAIL(___tmp_number, "error: could not allocate memory for ___tmp_number variable.", 1) strcpy(___tmp_number, _tmp_number); __tmp_number = strtok(___tmp_number, ","); sscanf(__tmp_number, "%hd", &number); } else { sscanf(_tmp_number, "%hd", &number); } free(_command); return number; } unsigned short int detect_command_number(char *command) { if (detect_command_type(command) == 5) return 0; unsigned short int number = 0; char *_command; char *_number; _command = malloc((strlen(command) + 1) * sizeof(char)); strcpy(_command, command); FAIL(_command, "error: could not allocate memory for _command variable.", 1) _number = strtok(_command, "><^|?!"); sscanf(_number, "%hd", &number); free(_command); return number; }