forked from matheusmoreira/ite-829x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.c
175 lines (136 loc) · 4.25 KB
/
cmd.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright © 2019 Matheus Afonso Martins Moreira
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
* Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define DIV_CEIL(x, y) ((((x) - 1) / (y)) + 1)
typedef unsigned int (*command_function)(size_t count, const char **arguments, void *context);
struct command {
const char *name;
command_function function;
};
struct commands {
void *context;
struct command *list;
};
static int command_empty_or_null(struct command *command)
{
return command == NULL || command->name == NULL || command->function == NULL;
}
static int commands_empty_or_null(struct commands *commands)
{
return commands == NULL || command_empty_or_null(commands->list);
}
static int string_empty_or_null(const char *string)
{
return string == NULL || *string == '\0';
}
static int vector_empty_or_null(const char **vector)
{
return vector == NULL || string_empty_or_null(*vector);
}
static struct command *find(struct command *commands, const char *line)
{
for (; commands->name != NULL; ++commands) {
size_t command_length = strlen(commands->name);
if (strncmp(commands->name, line, command_length) == 0)
return commands;
}
return NULL;
}
int process_command_vector(struct commands *commands, const char **arguments)
{
if (vector_empty_or_null(arguments) || commands_empty_or_null(commands))
return 0; // NULL or empty inputs
struct command *command = find(commands->list, *arguments++);
if (command == NULL)
return -1; // command not found
size_t count = 0;
for (const char **p = arguments; *p != NULL; ++p, ++count);
return command->function(count, arguments, commands->context);
}
static int process_command_line_copy(struct commands *commands, char *copy, size_t length)
{
int result = 0;
// Strings will be in the form of interleaved space/non-space characters
// For example: "a b c d"
// For a string of length N, at least ceil(N / 2) pointers are required
// to point at all possible tokens, plus one for the trailing NULL
const char **arguments = calloc(DIV_CEIL(length, 2) + 1, sizeof(*arguments));
if (arguments == NULL) {
result = -2; // memory allocation error
goto free_and_exit;
}
copy = strtok(copy, " \t\n");
if (copy == NULL) {
result = 0; // empty line
goto free_and_exit;
}
struct command *command = find(commands->list, copy);
if (command == NULL) {
result = -1; // command not found
goto free_and_exit;
}
size_t i = 0;
for (char *argument = copy; argument != NULL; ++i)
arguments[i] = argument = strtok(NULL, " \t\n");
arguments[i] = NULL;
result = command->function(i - 1, arguments, commands->context);
free_and_exit:
free(arguments);
return result;
}
int process_command_line(struct commands *commands, const char *line)
{
if (string_empty_or_null(line) || commands_empty_or_null(commands))
return 0; // NULL or empty inputs
int result = 0;
// strtok will modify the string so make a copy first
size_t length = strlen(line);
char *copy = malloc(length + 1);
if (copy == NULL) {
result = -2; // memory allocation error
goto free_and_exit;
}
strcpy(copy, line);
result = process_command_line_copy(commands, copy, length);
free_and_exit:
free(copy);
return result;
}
int process_command_file(struct commands *commands, FILE *input)
{
int result = 0;
while (1) {
char *line = NULL;
size_t length = 0;
errno = 0;
ssize_t read = getline(&line, &length, input);
if (read == -1) {
if (errno)
result = -2; // memory allocation error
goto free;
}
result = process_command_line(commands, line);
free:
free(line);
if (result || read == -1)
break;
}
return result;
}