commit 4b54ffd37c6b25177383a928b855b6515a15e8da
parent d020696a19792b64fbb997ec8b55df1f9c92320d
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Thu, 24 Dec 2020 19:15:54 -0800
Disallow multiple filters
The filters as they currently work are not really useful together.
Perhaps this will be added in again in a better way.
Diffstat:
M | main.c | | | 146 | +++++++++++++++++++++++++++++++++++++------------------------------------------ |
1 file changed, 69 insertions(+), 77 deletions(-)
diff --git a/main.c b/main.c
@@ -40,7 +40,7 @@ enum gphitem { GI_INFO, GI_PATH, GI_HOST, GI_PORT, GI_PLUS, GI_NULL };
enum status { ERROR = -1, OK, UNWIND };
typedef int (item_command)(int, const char **, int, const char **);
-typedef int (filter)(int, const char **, unsigned int, const char **);
+typedef int (filter)(int, const char **, unsigned int, const char **, item_command *);
typedef int (command)(int, const char **, int, const char **);
int gawk(const char **);
@@ -313,33 +313,22 @@ gopher(const char **addr, const char *path)
}
int
-splitrun(filter **filters, int argc, const char **argv, unsigned int index, char *s, item_command *func)
+gphsplit(char **item, int size, char *buf)
{
- int i, n;
- char *item[GI_NULL + 2];
+ int n;
- n = argsplit(item, LEN(item), s, "\t\r", 0) - 1;
+ n = argsplit(item, size, buf, "\t\r", 0) - 1;
if (n != 4 && (n < 4 || n > 5 || strcmp(item[GI_PLUS], "+") != 0))
- warnx("%d: Invalid gopher-item.", index);
-
- if (filters != NULL) {
- for (i = 0; filters[i] != NULL; ++i) {
- n = filters[i](argc, argv, index, (const char **)item);
- if (n <= 0)
- return n;
- argc -= n;
- argv += n;
- }
- }
-
- return func(argc, argv, index, (const char **)item);
+ return ERROR;
+ return 0;
}
int
-run_filters(const char *cache, filter **filters, int argc, const char **argv, item_command *func)
+run_command(const char *cache, filter *filter, int argc, const char **argv, item_command *command)
{
FILE *fp;
- char item[MY_LINE_MAX];
+ char buf[MY_LINE_MAX];
+ char *item[GI_NULL + 2];
int error;
unsigned int i;
@@ -348,62 +337,73 @@ run_filters(const char *cache, filter **filters, int argc, const char **argv, it
return UNWIND;
error = 0;
- for (i = 0; !error && fgets(item, sizeof(item), fp) && *item != '.'; ++i)
- error = splitrun(filters, argc, argv, i, item, func);
-
+ for (i = 0; !error && fgets(buf, sizeof(buf), fp) && *buf != '.'; ++i) {
+ if (gphsplit(item, sizeof(item), buf) == 0) {
+ if (filter)
+ error = filter(argc, argv, i, (const char **)item, command);
+ else
+ error = command(argc, argv, i, (const char **)item);
+ }
+ }
wfclose(fp);
return error;
}
int
-findex(int argc, const char **argv, unsigned int index, const char **item)
+findex(int argc, const char **argv, unsigned int index, const char **item,
+ item_command *command)
{
unsigned int n;
- if (warg(1, -1, argc, argv))
- return -1;
- if (strtorange(&n, 0, UINT_MAX, argv[0]))
- return -1;
+ if (warg(1, -1, argc, argv) || strtorange(&n, 0, UINT_MAX, argv[0]))
+ return ERROR;
if (index == n)
- return 1;
+ return command(argc - 1, argv + 1, index, item);
+ else if (index > n)
+ return ERROR;
return 0;
}
/* NOTE: inclusive */
int
-frange(int argc, const char **argv, unsigned int index, const char **item)
+frange(int argc, const char **argv, unsigned int index, const char **item,
+ item_command *command)
{
- unsigned int range[2];
int i;
+ unsigned int range[2];
if (warg(2, -1, argc, argv))
- return -1;
-
+ return ERROR;
for (i = 0; i < 2; ++i)
if (strtorange(&range[i], 0, UINT_MAX, argv[i]))
- return -1;
- if (index >= range[0] && index <= range[1])
- return 1;
+ return ERROR;
+
+ if (index > range[1])
+ return ERROR;
+ else if (index >= range[0])
+ return command(argc - 2, argv + 2, index, item);
return 0;
}
int
-fstring(int argc, const char **argv, unsigned int index, const char **item)
+fstring(int argc, const char **argv, unsigned int index, const char **item,
+ item_command *command)
{
if (warg(1, -1, argc, argv))
- return -1;
+ return ERROR;
if (strcasestr(item[GI_INFO], argv[0]) != NULL)
- return 1;
+ return command(argc - 1, argv + 1, index, item);
return 0;
}
int
-ftype(int argc, const char **argv, unsigned int index, const char **item)
+ftype(int argc, const char **argv, unsigned int index, const char **item,
+ item_command *command)
{
if (warg(1, -1, argc, argv))
- return -1;
+ return ERROR;
if (strchr(argv[0], *item[GI_INFO]) != NULL)
- return 1;
+ return command(argc - 1, argv + 1, index, item);
return 0;
}
@@ -467,7 +467,7 @@ cprint(int argc, const char **argv, int i, const char **s)
int
cprintn(int argc, const char **argv, int i, const char **s)
-{
+{
if (**s != 'i')
printf("%4d", i);
else printf(" ");
@@ -545,6 +545,7 @@ cextern(int argc, const char **argv, int index, const char **item)
int
cgoto(int argc, const char **argv, int index, const char **item)
{
+ warg(0, 0, argc, argv);
/* TODO: implement 7 type support */
if (**item == '1')
return gawk(itemtoaddr(item));
@@ -553,7 +554,7 @@ cgoto(int argc, const char **argv, int index, const char **item)
}
item_command *
-getcommand(int c)
+get_ic(int c)
{
switch (c) {
case 'p':
@@ -572,7 +573,7 @@ getcommand(int c)
}
filter *
-getfilter(int c)
+get_filter(int c)
{
switch (c) {
case 't':
@@ -589,7 +590,7 @@ getfilter(int c)
}
command *
-getstackc(int c)
+get_rc(int c)
{
switch (c) {
case 'u':
@@ -607,43 +608,34 @@ int
execute(int argc, const char **argv, int depth, const char *cache,
const char **addr)
{
- command *cmd;
- filter *filters[MY_FILTER_MAX];
- item_command *itemc;
- unsigned int i;
+ command *rc;
+ filter *filter;
+ item_command *ic;
if (argc == 0 || argv[0][0] == '\0')
return ERROR;
- filters[0] = NULL;
- 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]);
- return ERROR;
- }
- }
- filters[i] = NULL;
- if (argv[0][i + 1] != '\0') {
- warnx("Too many filters");
+ filter = NULL;
+ switch (strlen(*argv)) {
+ case 1:
+ if ((rc = get_rc(**argv)))
+ return rc(argc - 1, argv + 1, depth, addr);
+ ic = get_ic(**argv);
+ break;
+ case 2:
+ if ((filter = get_filter(**argv)) == NULL)
+ goto invalid;
+ ic = get_ic(argv[0][1]);
+ break;
+ default:
+invalid:
+ warnx("'%s': invalid statement.", *argv);
return ERROR;
}
- 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 ERROR;
- }
- }
-
- --argc;
- ++argv;
- if (cmd != NULL)
- return cmd(argc, argv, depth, addr);
- return run_filters(cache, filters, argc, argv, itemc);
+ if (ic == NULL)
+ goto invalid;
+ return run_command(cache, filter, argc - 1, argv + 1, ic);
}
void
@@ -674,7 +666,7 @@ gawk(const char **addr)
mycache = 1;
if (gopher(addr, cache) == 1)
return 0; /* let the user handle it */
- done = run_filters(cache, NULL, 0, NULL, cprintn);
+ done = run_command(cache, NULL, 0, NULL, cprintn);
}
/* NOTE: do not return until --depth */