5 * Automated Options Paged Usage module.
11 * This routine will run run-on options through a pager so the
12 * user may examine, print or edit them at their leisure.
14 * This file is part of AutoOpts, a companion to AutoGen.
15 * AutoOpts is free software.
16 * AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
18 * AutoOpts is available under any one of two licenses. The license
19 * in use must be one of these two and the choice is under the control
20 * of the user of the license.
22 * The GNU Lesser General Public License, version 3 or later
23 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
25 * The Modified Berkeley Software Distribution License
26 * See the file "COPYING.mbsd"
28 * These files have the following sha256 sums:
30 * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
31 * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
32 * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
35 #if defined(HAVE_WORKING_FORK)
37 open_tmp_usage(char ** buf)
43 unsigned int my_pid = (unsigned int)getpid();
44 char const * tmpdir = getenv(TMPDIR);
47 bfsz = TMP_FILE_FMT_LEN + strlen(tmpdir) + 10;
48 bf = AGALOC(bfsz, "tmp fil");
49 snprintf(bf, bfsz, TMP_FILE_FMT, tmpdir, my_pid);
53 static mode_t const cmask = S_IRWXO | S_IRWXG;
54 mode_t svmsk = umask(cmask);
63 return fdopen(fd, "w");
68 mk_pager_cmd(char const * fname)
71 * Page the file and remove it when done. For shell script processing,
72 * we must redirect the output to the current stderr, otherwise stdout.
74 fclose(option_usage_fp);
75 option_usage_fp = NULL;
78 char const * pager = (char const *)getenv(PAGER_NAME);
83 * Use the "more(1)" program if "PAGER" has not been defined
88 bfsz = 2 * strlen(fname) + strlen(pager) + PAGE_USAGE_FMT_LEN;
89 res = AGALOC(bfsz, "more cmd");
90 snprintf(res, bfsz, PAGE_USAGE_FMT, pager, fname);
97 /*=export_func optionPagedUsage
100 * what: emit help text and pass through a pager program.
101 * arg: + tOptions * + opts + program options descriptor +
102 * arg: + tOptDesc * + od + the descriptor for this arg +
105 * Run the usage output through a pager.
106 * This is very handy if it is very long.
107 * This is disabled on platforms without a working fork() function.
110 optionPagedUsage(tOptions * opts, tOptDesc * od)
112 #if ! defined(HAVE_WORKING_FORK)
113 if ((od->fOptState & OPTST_RESET) != 0)
116 (*opts->pUsageProc)(opts, EXIT_SUCCESS);
118 static bool sv_print_exit = false;
119 static char * fil_name = NULL;
122 * IF we are being called after the usage proc is done
123 * (and thus has called "exit(2)")
124 * THEN invoke the pager to page through the usage file we created.
126 switch (pagerState) {
127 case PAGER_STATE_INITIAL:
129 if ((od->fOptState & OPTST_RESET) != 0)
131 option_usage_fp = open_tmp_usage(&fil_name);
132 if (option_usage_fp == NULL)
133 (*opts->pUsageProc)(opts, EXIT_SUCCESS);
135 pagerState = PAGER_STATE_READY;
136 sv_print_exit = print_exit;
139 * Set up so this routine gets called during the exit logic
141 atexit((void(*)(void))optionPagedUsage);
144 * The usage procedure will now put the usage information into
145 * the temporary file we created above. Keep any shell commands
149 (*opts->pUsageProc)(opts, EXIT_SUCCESS);
155 case PAGER_STATE_READY:
156 fil_name = mk_pager_cmd(fil_name);
159 fputs("\nexit 0\n", stdout);
161 dup2(STDERR_FILENO, STDOUT_FILENO);
165 dup2(STDOUT_FILENO, STDERR_FILENO);
168 ignore_val( system( fil_name));
171 case PAGER_STATE_CHILD:
173 * This is a child process used in creating shell script usage.
184 * c-file-style: "stroustrup"
185 * indent-tabs-mode: nil
187 * end of autoopts/pgusage.c */