-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_handler.c
More file actions
116 lines (90 loc) · 3.27 KB
/
input_handler.c
File metadata and controls
116 lines (90 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mymat.h"
#define SPACE_STR " "
#define COMMA_STR ","
#define COMMA_CHAR ','
int inject_parameters(command_params *parameters, char *token, char command[BUFFER_MAX]);
void command_execution(command_params *parameters, char command_string[BUFFER_MAX]);
/* Register command with function name. minimum arguments and maximum arguments */
static command cmd[] = {{"read_mat", read_mat, 2, 100}, {"print_mat", print_mat, 1, 1},
{"add_mat", add_mat, 3, 3}, {"sub_mat", sub_mat, 3, 3},
{"mul_mat", mul_mat, 3, 3}, {"mul_scalar", mul_scalar, 3, 3},
{"trans_mat", trans_mat, 2, 2}, {"stop", stop, 0, 0}};
/* Handle all the user's input and forward it to the functions that handle the commands */
void handleInput(char buffer[BUFFER_MAX]) {
int valid;
/* Loop while there's input */
while (fgets(buffer, BUFFER_MAX, stdin) != NULL) {
command_params parameters;
char command[BUFFER_MAX];
char *buffer_paramaters;
char *token;
buffer[strcspn(buffer, "\n")] = 0;
puts(buffer);
if (buffer[strlen(buffer)-1] == COMMA_CHAR) {
printf("Extraneous text after end of command\n");
continue;
}
strcpy(command, buffer);
token = strtok(command, SPACE_STR);
/* Check for extra spaces between arguments instead of comma */
if (command[strlen(command)-1] == COMMA_CHAR) {
printf("Illegal comma\n");
continue;
}
buffer_paramaters = buffer + strlen(command) + 1;
token = strtok(buffer_paramaters, COMMA_STR);
valid = inject_parameters(¶meters, token, command);
/* Check if the parameters entered correctly adn execute the command */
if (valid) {
command_execution(¶meters, command);
}
}
}
int inject_parameters(command_params *parameters, char *token, char command[BUFFER_MAX]) {
int parameter_index;
/* Loop throught arguments, normalize data and validation */
for (parameter_index = 0; (token != NULL && parameter_index <= MAX_PARAMAETERS); parameter_index++) {
token = strstrip(token);
if (strcmp(token, "") == 0) {
printf("Multiple consecutive commas\n");
return FALSE;
}
if (strchr(token, ' ') != NULL) {
printf("Missing comma\n");
return FALSE;
}
(*parameters).parameters[parameter_index] = token;
token = strtok(NULL, COMMA_STR);
}
(*parameters).command = command;
(*parameters).parameters_length = parameter_index;
return TRUE;
}
void command_execution(command_params *parameters, char command_string[BUFFER_MAX]) {
int i;
int parameters_length = (*parameters).parameters_length;
for (i = 0; cmd[i].func != NULL; i++) {
if (strcmp(command_string, cmd[i].name) == 0) {
break;
}
}
if (cmd[i].func == NULL) {
printf("Undefined command name\n");
} else {
command com = cmd[i];
if (com.min_arguments > parameters_length) {
printf("Missing argument\n");
return;
}
if (com.max_arguments < parameters_length) {
printf("Extraneous text after end of command\n");
return;
}
/* Invoke the matching function with the command's parameters */
(*(cmd[i].func))(parameters);
}
}