diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 10:07:08 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 10:07:08 -0400 |
commit | 0701bc4cf61fd4a58889b5b68c308621263de5d1 (patch) | |
tree | 3656b1482f4bef8728b96a4219f7a29efecd88b0 | |
parent | 7d6cc262f55a581d7db59a5df3ee6f7a23ca3f52 (diff) |
add internal menuviewer
-rw-r--r-- | menudriver.c | 59 |
1 files changed, 37 insertions, 22 deletions
diff --git a/menudriver.c b/menudriver.c index 9043ee9..aa6d65c 100644 --- a/menudriver.c +++ b/menudriver.c @@ -13,10 +13,10 @@ #include <limits.h> #include <stdio.h> #include <stdlib.h> -#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <sys/wait.h> +#include <string.h> #include <signal.h> #ifndef PATH_MAX @@ -27,7 +27,6 @@ #endif #endif -char *menuviewer; char *viewer = "less"; #define VIEWERPAUSESATEND . @@ -43,20 +42,18 @@ int fastback = 0; void callshell(char[]); void clrscr(void); -void menudrive(char *); +void menudrive(const char *, char *); void pushlast(void); void poplast(void); -void settings(void); +void settings(const char *, const char *, int); void singleton(char *); int subdir(void); char next[40] = "", cmd[80], last[40] = "."; int val; -#include <string.h> -#define index(s,c) strchr(s,c) - -static void wait_for_return(void) { +static void wait_for_return(void) +{ printf("[Hit return to continue]"); fflush(stdout); while (getchar() != '\n') { @@ -64,21 +61,40 @@ static void wait_for_return(void) { } } +static void view_menu(const char *command, const char *filename) +{ + if (command) { + char buf[PATH_MAX]; + snprintf(buf, sizeof(buf), "%s %s", command, filename); + system(buf); + return; + } + + FILE *f = fopen(filename, "r"); + if (!f) { + fprintf(stderr, "menudriver: %s: %s\n", filename, strerror(errno)); + return; + } + + int c; + while ((c = fgetc(f)) != EOF) { + putchar(c); + } + + fclose(f); +} + int main(int argc, char *argv[]) { char *v = getenv("VIEWER"); char *fb = getenv("RETURNTOMENU"); - menuviewer = getenv("MENUVIEWER"); + char *menuviewer = getenv("MENUVIEWER"); if (argc > 2) { fprintf(stderr, "menudriver: wrong number of args\n"); exit(1); } - if (!menuviewer) { - menuviewer = "cat"; - } - if (v) { viewer = v; } @@ -87,7 +103,7 @@ int main(int argc, char *argv[]) fastback = !(*fb == 'N' || *fb == 'n'); } - menudrive(argc == 1 ? "." : argv[1]); + menudrive(menuviewer, argc == 1 ? "." : argv[1]); } /* checks if last is a number (and if so leaves value in val) */ @@ -109,7 +125,7 @@ int lastval(void) return (sscanf(last, "%d", &val) == 1); } -void menudrive(char *dir) +void menudrive(const char *menuviewer, char *dir) { char *np; int c; @@ -133,8 +149,7 @@ void menudrive(char *dir) bad = 0; } - snprintf(cmd, sizeof(cmd), "%s contents", menuviewer); - system(cmd); + view_menu(menuviewer, "contents"); printf("::please type selection number (or return to exit):"); /* read remainder of line into next, less leading white space */ @@ -180,7 +195,7 @@ void menudrive(char *dir) } if (stat(next, &st) == 0) { - if (strcmp(next, ".") == 0 || strcmp(next, "..") == 0 || index(next, '/')) { + if (strcmp(next, ".") == 0 || strcmp(next, "..") == 0 || strchr(next, '/')) { bad = 1; continue; } /* no pathnames - see below */ @@ -255,7 +270,7 @@ void menudrive(char *dir) } else if (strcmp(next, "???") == 0) { /* ask to see menudriver settings */ - settings(); + settings(viewer, menuviewer, fastback); wait_for_return(); next[0] = '\0'; @@ -356,11 +371,11 @@ void callshell(char v[]) } } -void settings(void) +void settings(const char *viewer, const char *menuviewer, int fastback) { printf("current values of menudriver internal variables are\n\n"); - printf(" VIEWER=%s\n", viewer); - printf(" MENUVIEWER=%s\n", menuviewer); + printf(" VIEWER=%s\n", viewer ? viewer : "(internal)"); + printf(" MENUVIEWER=%s\n", menuviewer ? : "(internal)"); printf(" RETURNTOMENU=%s\n", fastback ? "YES" : "NO"); printf("\n\ These can be modified by setting environment variables of the same names\n\n\ |