gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit fdc740cdc0a9b40ecf87297e0c9d9b438520d966
parent 7c166993c74c4bef19553a20e4206659d159cf30
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Thu,  7 Jan 2021 13:08:11 -0800

Organize functions

- Move `argsplit' to `util.c'
- Put helper functions directly above the functions they help.

Also remove redundent includes.

Diffstat:
Mmain.c | 66++++++++++++++++++++++++------------------------------------------
Mutil.c | 19+++++++++++++++++--
Mutil.h | 14+++++++-------
3 files changed, 48 insertions(+), 51 deletions(-)

diff --git a/main.c b/main.c @@ -18,7 +18,6 @@ #include <dirent.h> #include <errno.h> #include <signal.h> -#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> @@ -32,23 +31,6 @@ static char tmpdir[] = "/tmp/gawk-XXXXXXXXXXX"; static int fatal; -int -argsplit(char **argv, int size, char *s, char const *sep, int cat) -{ - int argc; - - for (argc = 0; argc < size && - (argv[argc] = strsep(&s, sep)) != NULL;) { - if (!cat || *argv[argc] != '\0') - ++argc; - } - - if (argc == size && - argv[argc - 1][strcspn(argv[argc - 1], sep)] == '\0') - return -1; - return argc; -} - void putprompt(char const *prompt) { @@ -94,30 +76,6 @@ too_big: } int -gphsplit(char **item, int size, char *buf) -{ - int n; - - n = argsplit(item, size, buf, "\t\r", 0) - 1; - if (n != 4 && (n < 4 || n > 5 || strcmp(item[GI_PLUS], "+") != 0)) - return ERROR; - return 0; -} - -command * -getcom(char const *s, struct bind const *const binds, int size) -{ - int i; - - if (strlen(s) != 1) - return NULL; - for (i = 0; i < size; ++i) - if (*s == binds[i].c) - return binds[i].f; - return NULL; -} - -int runpipe(struct command *cmds, int *indexes, int count, char **item) { int i; @@ -136,6 +94,17 @@ runpipe(struct command *cmds, int *indexes, int count, char **item) } int +gphsplit(char **item, int size, char *buf) +{ + int n; + + n = argsplit(item, size, buf, "\t\r", 0) - 1; + if (n != 4 && (n < 4 || n > 5 || strcmp(item[GI_PLUS], "+") != 0)) + return ERROR; + return 0; +} + +int runpipes(char const *cache, struct command *cmds, int count) { FILE *fp; @@ -158,6 +127,19 @@ runpipes(char const *cache, struct command *cmds, int count) return error; } +command * +getcom(char const *s, struct bind const *const binds, int size) +{ + int i; + + if (strlen(s) != 1) + return NULL; + for (i = 0; i < size; ++i) + if (*s == binds[i].c) + return binds[i].f; + return NULL; +} + int execute(char const *cache, char *input, int depth, char **addr) { diff --git a/util.c b/util.c @@ -18,10 +18,8 @@ #include <sys/stat.h> #include <errno.h> #include <stdarg.h> -#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include "util.h" @@ -37,6 +35,23 @@ wfopen(char const *path, char const *mode) } int +argsplit(char **argv, int size, char *s, char const *sep, int cat) +{ + int argc; + + for (argc = 0; argc < size && + (argv[argc] = strsep(&s, sep)) != NULL;) { + if (!cat || *argv[argc] != '\0') + ++argc; + } + + if (argc == size && + argv[argc - 1][strcspn(argv[argc - 1], sep)] == '\0') + return -1; + return argc; +} + +int exists(char const *path) { struct stat sb; diff --git a/util.h b/util.h @@ -6,14 +6,14 @@ #define die(E, ...) { warn(E, __VA_ARGS__); exit(EXIT_FAILURE); } -FILE * wfopen(char const *, char const *); -int exists(char const *); -int wfclose(FILE *); -int wremove(char const *); -#undef strtonum +FILE *wfopen(char const *, char const *); +int argsplit(char **, int, char *, char const *, int); +int exists(char const *); +int wfclose(FILE *); +int wremove(char const *); long long strtonum(const char *, long long, long long, const char **); long long wstrtonum(const char *, long long, long long, const char **); -void tr(char *, char const *, int, int); -void warn(int, char const *, ...); +void tr(char *, char const *, int, int); +void warn(int, char const *, ...); #endif /* _util_h */