diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 10:24:52 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 10:24:52 -0400 |
commit | 16b13691833e65f6e0cb77d27bccf87ef7da3435 (patch) | |
tree | 0c7200920ff0f66a71d7bc2fd88958d3f2ec76bc | |
parent | 0701bc4cf61fd4a58889b5b68c308621263de5d1 (diff) |
replace singleton() and file viewing in menudrive() with view_file()
-rw-r--r-- | menudriver.c | 102 |
1 files changed, 44 insertions, 58 deletions
diff --git a/menudriver.c b/menudriver.c index aa6d65c..17e2272 100644 --- a/menudriver.c +++ b/menudriver.c @@ -27,8 +27,6 @@ #endif #endif -char *viewer = "less"; - #define VIEWERPAUSESATEND . /* this modifies default behaviour of menudriver to return straight to menu after displaying section, to avoid two layers of prompt; @@ -42,11 +40,10 @@ int fastback = 0; void callshell(char[]); void clrscr(void); -void menudrive(const char *, char *); +void menudrive(const char *, const char *, char *); void pushlast(void); void poplast(void); void settings(const char *, const char *, int); -void singleton(char *); int subdir(void); char next[40] = "", cmd[80], last[40] = "."; @@ -84,9 +81,39 @@ static void view_menu(const char *command, const char *filename) fclose(f); } +static void view_file(const char *viewer, const char *filename) +{ + struct stat st; + if (!(stat(filename, &st) == 0 && S_ISREG(st.st_mode))) { + fprintf(stderr, "menudriver: cannot access \"%s\"\n", filename), exit(1); + return; + } + + clrscr(); + + char cmd[PATH_MAX]; + +#ifdef UWIN /* does UWIN even exist anymore? */ + if (strcmp(filename, "99") == 0) { +#else + if (st.st_mode & S_IXUSR) { +#endif + snprintf(cmd, sizeof(cmd), "./%s", filename); + fastback = 0; + } else { + snprintf(cmd, sizeof(cmd), "%s %s", viewer, filename); + } + + system(cmd); + + if (!fastback) { + wait_for_return(); + } +} + int main(int argc, char *argv[]) { - char *v = getenv("VIEWER"); + char *viewer = getenv("VIEWER"); char *fb = getenv("RETURNTOMENU"); char *menuviewer = getenv("MENUVIEWER"); @@ -95,15 +122,15 @@ int main(int argc, char *argv[]) exit(1); } - if (v) { - viewer = v; + if (!viewer) { + viewer = "less"; } if (fb) { fastback = !(*fb == 'N' || *fb == 'n'); } - menudrive(menuviewer, argc == 1 ? "." : argv[1]); + menudrive(menuviewer, viewer, argc == 1 ? "." : argv[1]); } /* checks if last is a number (and if so leaves value in val) */ @@ -125,13 +152,15 @@ int lastval(void) return (sscanf(last, "%d", &val) == 1); } -void menudrive(const char *menuviewer, char *dir) +void menudrive(const char *menuviewer, const char *viewer, char *dir) { char *np; int c; int bad = 0; + if (chdir(dir) == -1) { - singleton(dir); /* apparently not a directory */ + view_file(viewer, dir); + exit(0); } struct stat st; @@ -207,7 +236,8 @@ void menudrive(const char *menuviewer, char *dir) } if (chdir(next) == -1 || stat("contents", &st)) { - bad = 1, chdir(hold); + bad = 1; + chdir(hold); } else { strcpy(last, next); pushlast(); @@ -215,25 +245,7 @@ void menudrive(const char *menuviewer, char *dir) } } else if (S_ISREG(st.st_mode)) { - /* regular file */ - clrscr(); - -#ifndef UWIN - if (st.st_mode & S_IXUSR) /* executable (by owner) */ -#else - if (strcmp(next, "99") == 0) -#endif - { - snprintf(cmd, sizeof(cmd), "./%s", next); - system(cmd); - if (fastback) { - wait_for_return(); - } - - } else { - snprintf(cmd, sizeof(cmd), "%s %s", viewer, next); - system(cmd); - } + view_file(viewer, next); if (fastback) { strcpy(last, next); @@ -251,7 +263,8 @@ void menudrive(const char *menuviewer, char *dir) } while (c != '\n' && c != EOF) { - *np++ = c, c = getchar(); + *np++ = c; + c = getchar(); } if (c == EOF) { @@ -317,32 +330,6 @@ void menudrive(const char *menuviewer, char *dir) and exit by chdir(hold) instead of chdir("..") - will need to make this recursive, or else have stack of holdwd's */ -void singleton(char *fil) -{ - struct stat st; - if (!(stat(fil, &st) == 0 && S_ISREG(st.st_mode))) { - fprintf(stderr, "menudriver: cannot access \"%s\"\n", fil), exit(1); - return; - } - - clrscr(); - - snprintf(cmd, sizeof(cmd), "%s %s", viewer, fil); -#ifndef UWIN - if (st.st_mode & S_IXUSR) { /* executable (by owner) */ - snprintf(cmd, sizeof(cmd), "./%s", fil); - fastback = 0; - } -#endif - system(cmd); - - if (!fastback) { - wait_for_return(); - } - - exit(0); -} - void callshell(char v[]) { static char *shell = NULL; @@ -448,4 +435,3 @@ void clrscr(void) { system("tput clear"); } - |