diff options
Diffstat (limited to 'menudriver.c')
-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\ |