commit 335acd2a4c30a0d488101c23eead25c603beee3c
parent 5681ea43c71a8e1a89ddf420d32b659b5f5ca33d
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 23 Dec 2020 14:00:48 -0800
Rename command types and revise execute()
Rename command to item_command because they take gopher items and
rename stack_commands to commands because it's now free and it's
misleading with the addition of cgoto().
Note that function names were kept the same as I have a fickle mind
and these might change again.
Diffstat:
M | main.c | | | 50 | ++++++++++++++++++++++++-------------------------- |
1 file changed, 24 insertions(+), 26 deletions(-)
diff --git a/main.c b/main.c
@@ -47,9 +47,9 @@ enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_NULL };
/* NOTE: before changing see reqtoaddr() */
enum address { AR_PATH, AR_HOST, AR_PORT, AR_NULL };
-typedef int (command)(int, const char **, int, const char **);
+typedef int (item_command)(int, const char **, int, const char **);
typedef int (filter)(int, const char **, unsigned int, const char **);
-typedef int (stack_command)(int, const char **, int, const char **);
+typedef int (command)(int, const char **, int, const char **);
int gawk(const char **);
@@ -313,7 +313,7 @@ gph_write(const char **addr, const char *path)
/* Returns -1 on fatal error, 0 on no match, and 1 on match. */
int
-splitrun(filter **filters, int argc, const char **argv, unsigned int index, char *s, command *func)
+splitrun(filter **filters, int argc, const char **argv, unsigned int index, char *s, item_command *func)
{
int i, n;
char *item[6];
@@ -339,7 +339,7 @@ splitrun(filter **filters, int argc, const char **argv, unsigned int index, char
}
int
-run_filters(const char *cache, filter **filters, int argc, const char **argv, command *func)
+run_filters(const char *cache, filter **filters, int argc, const char **argv, item_command *func)
{
FILE *fp;
char item[MY_LINE_MAX];
@@ -530,7 +530,7 @@ cgoto(int argc, const char **argv, int index, const char **item)
return gawk(reqtoaddr(item));
}
-command *
+item_command *
getcommand(int c)
{
switch (c) {
@@ -566,7 +566,7 @@ getfilter(int c)
}
}
-stack_command *
+command *
getstackc(int c)
{
switch (c) {
@@ -585,22 +585,16 @@ int
execute(int argc, const char **argv, int depth, const char *cache,
const char **addr)
{
- unsigned int i;
+ command *cmd;
filter *filters[MY_FILTER_MAX];
- command *command;
- stack_command *stackc;
+ item_command *itemc;
+ unsigned int i;
- if (argc == 0 && argv[0][0] == '\0')
+ if (argc == 0 || argv[0][0] == '\0')
return 0;
filters[0] = NULL;
- stackc = 0;
-
- for (i = 0; argv[0][i + 1] != '\0'; ++i) {
- if (i >= LEN(filters)) {
- warnx("Too many filters");
- return 0;
- }
+ for (i = 0; argv[0][i + 1] != '\0' && i < LEN(filters) - 1; ++i) {
filters[i] = getfilter(argv[0][i]);
if (filters[i] == NULL) {
warnx("'%c': Not a filter.", argv[0][i]);
@@ -608,22 +602,26 @@ execute(int argc, const char **argv, int depth, const char *cache,
}
}
filters[i] = NULL;
- command = getcommand(argv[0][i]);
- if (command == NULL) {
- stackc = getstackc(argv[0][i]);
- if (stackc == NULL) {
+ if (argv[0][i + 1] != '\0') {
+ warnx("Too many filters");
+ return 0;
+ }
+
+ cmd = NULL;
+ itemc = getcommand(argv[0][i]);
+ if (itemc == NULL) {
+ cmd = getstackc(argv[0][i]);
+ if (cmd == NULL) {
warnx("'%c': Not a command.", argv[0][i]);
return 0;
}
}
- /* TODO: combine stack commands and regular commands */
--argc;
++argv;
- if (stackc != NULL)
- return stackc(argc, argv, depth, addr);
- else
- run_filters(cache, filters, argc, argv, command);
+ if (cmd != NULL)
+ return cmd(argc, argv, depth, addr);
+ run_filters(cache, filters, argc, argv, itemc);
return 0;
}