/* general purpose menu driver */ /* alternatively there is an equivalent shell script, menudriver.sh */ /************************************************************************** * Copyright (C) Research Software Limited 1985-90. All rights reserved. * * The Miranda system is distributed as free software under the terms in * * the file "COPYING" which is included in the distribution. * * * * Revised to C11 standard and made 64bit compatible, January 2020 * *------------------------------------------------------------------------*/ #include #include #include #include #include #include #include #include #include #include #ifndef PATH_MAX #ifdef _XOPEN_PATH_MAX #define PATH_MAX _XOPEN_PATH_MAX #else #define PATH_MAX _POSIX_PATH_MAX #endif #endif #define VIEWERPAUSESATEND . /* this modifies default behaviour of menudriver to return straight to menu after displaying section, to avoid two layers of prompt; choice can be overriden by environment variable RETURNTOMENU=YES/NO */ #ifdef VIEWERPAUSESATEND int fastback = 1; #else int fastback = 0; #endif void callshell(char[]); void menudrive(const char *, const char *, char *); void pushlast(char *); void poplast(char *); void settings(const char *, const char *, int); int subdir(void); static void clear_screen(void) { system("tput clear"); } static int read_next(size_t n, char buf[static n]) { int c; do { c = getchar(); } while (c == ' ' || c == '\t'); ungetc(c, stdin); if (fgets(buf, n, stdin) == NULL) { return 0; } for (char *end = strchr(buf, '\n'); isspace(*end); end--) { *end = '\0'; } return 1; } static void wait_for_return(void) { printf("[Hit return to continue]"); fflush(stdout); while (getchar() != '\n') { /* keep reading until new-line */ } } static void view_menu(const char *viewer, const char *filename) { if (viewer) { char cmd[PATH_MAX]; snprintf(cmd, sizeof(cmd), "%s %s", viewer, filename); system(cmd); 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); } 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; } clear_screen(); 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); } else { snprintf(cmd, sizeof(cmd), "%s %s", viewer, filename); } system(cmd); if (!fastback) { wait_for_return(); } } int main(int argc, char *argv[]) { char *viewer = getenv("VIEWER"); char *fb = getenv("RETURNTOMENU"); char *menuviewer = getenv("MENUVIEWER"); char *dir = "."; if (argc > 2) { fprintf(stderr, "menudriver: wrong number of args\n"); exit(1); } else if (argc == 2) { dir = argv[1]; } if (!viewer) { viewer = "less"; } if (fb) { fastback = !(*fb == 'N' || *fb == 'n'); } menudrive(menuviewer, viewer, dir); } /* checks if last is a number (and if so leaves value in val) */ int lastval(char *last) { /* special case, have just entered subdir */ int val; if (strcmp(last, ".") == 0 && subdir()) { poplast(last); if (sscanf(last, "%d", &val) == 1) { chdir(".."); return (1); } pushlast(last); return 0; } return (sscanf(last, "%d", &val) == 1); } void menudrive(const char *menuviewer, const char *viewer, char *dir) { char next[40] = ""; char last[40] = "."; int bad = 0; if (chdir(dir) == -1) { view_file(viewer, dir); exit(0); } struct stat st; while (stat("contents", &st) == 0) { if (next[0] == '\0' || bad) { clear_screen(); /* invalid selection notified here, after clearing screen */ if (bad) { if (strcmp(next, ".") == 0) { printf("no previous selection to substitute for \".\"\n"); } else { printf("selection \"%s\" not valid\n", next); } bad = 0; } view_menu(menuviewer, "contents"); printf("::please type selection number (or return to exit):"); if (!read_next(sizeof(next), next)) { exit(0); } } if (next[0] == '\0') { chdir(".."); poplast(last); continue; } if (strcmp(next, ".") == 0) { strcpy(next, last); /* repeat last option */ } else if (strcmp(next, "+") == 0 && lastval(last)) { (void)sprintf(next, "%d", atoi(last) + 1); } else if (strcmp(next, "-") == 0 && lastval(last)) { (void)sprintf(next, "%d", atoi(last) - 1); } if (stat(next, &st) == 0) { if (strcmp(next, ".") == 0 || strcmp(next, "..") == 0 || strchr(next, '/')) { bad = 1; continue; } /* no pathnames - see below */ if (S_ISDIR(st.st_mode)) { /* directory */ char hold[PATH_MAX]; if (!getcwd(hold, sizeof(hold))) { fprintf(stderr, "panic: cwd too long\n"); exit(1); } if (chdir(next) == -1 || stat("contents", &st) == -1) { bad = 1; chdir(hold); } else { strcpy(last, next); pushlast(last); next[0] = '\0'; } } else if (S_ISREG(st.st_mode)) { view_file(viewer, next); strcpy(last, next); if (fastback) { next[0] = '\0'; } else { printf("::next selection (or return to go back to menu, or q to quit):"); if (!read_next(sizeof(next), next)) { exit(0); } } } } else if (strcmp(next, "???") == 0) { /* ask to see menudriver settings */ settings(viewer, menuviewer, fastback); wait_for_return(); next[0] = '\0'; } else if (strcmp(next, "q") == 0 || strcmp(next, "/q") == 0) { exit(0); } else if (next[0] == '!') { /* shell escape - handy for editing manual! */ static char syscm[80]; if (next[1] == '\0' || next[1] == '!') { if (syscm[0]) { if (next[1] == '!') { strcat(syscm, next + 2); } printf("!%s\n", syscm); } else { printf("no previous shell command to substitute for \"!\"\n"); } } else { strcpy(syscm, next + 1); } if (syscm[0]) { callshell(syscm); /* `system' always gets /bin/sh */ } wait_for_return(); next[0] = '\0'; } else { bad = 1; } } } /* possibly a bug - can retreat above original dir, if parent contains a "contents" file - difficult to detect this in a general way, however */ /* pathnames banned because (i) upward pathname will not return correctly if a directory (see above) (ii) direct selection of a grandchild directory leads to (a) returns via child, instead of directly (b) meaning of "." is screwed up while in (a) */ /* could fix all this by - rewrite handling of subdirectory to hold=getwd() and exit by chdir(hold) instead of chdir("..") - will need to make this recursive, or else have stack of holdwd's */ void callshell(char v[]) { static char *shell = NULL; if (!shell) { shell = getenv("SHELL"); if (!shell) { shell = "/bin/sh"; } } void (*oldsig)(int) = signal(SIGINT, SIG_IGN); switch (fork()) { case -1: perror("UNIX error - cannot create process"); return; case 0: execl(shell, shell, "-c", v, (char *)0); fprintf(stderr, "%s\n", strerror(errno)); exit(1); default: wait(0); (void)signal(SIGINT, oldsig); } } 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 ? 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\ VIEWER is the program used to display individual sections\n\n\ MENUVIEWER is the program used to display contents pages\n\n\ RETURNTOMENU=NO/YES causes a second prompt to be given/not given after\n\ displaying section (ie before returning to contents page). It should be\n\ `YES' if VIEWER is a program that pauses for input at end of file, or\n\ `NO' if VIEWER is a program that quits silently at end of file.\n\n"); } /* Symptoms of a wrong setting are (i) bottom part of manual section\n\ disappears from screen before you have had a chance to read it - to cure\n\ this set RETURNTOMENU=NO; or (ii) having to quit through two layers of\n\ prompt at bottom of a manual section before getting back to the contents\n\ page - to cure this set RETURNTOMENU=YES;\n\n"); */ char lastvec[100]; char *lastp = lastvec + 1; int giveup = 0; int subdir(void) { return (lastp > lastvec + 1); } void pushlast(char *last) { if (!strcmp(last, ".")) { return; } else if (!strcmp(last, "..")) { poplast(last); return; } size_t n = strlen(last); if (lastp + n > lastvec + 100) { /* overflow */ giveup = 1; return; } strcpy(lastp, last); lastp += n + 1; strcpy(last, "."); } void poplast(char *last) { strcpy(lastp, last); /* just in case we come back immediately */ lastp--; if (giveup || lastp <= lastvec) { return; /* underflow */ } while (*--lastp) { /* what does this empty loop do? */ } strcpy(last, ++lastp); }