5 * Time-stamp: "2012-01-29 16:09:14 bkorb"
7 * Automated Options Paged Usage module.
9 * This routine will run run-on options through a pager so the
10 * user may examine, print or edit them at their leisure.
12 * This file is part of AutoOpts, a companion to AutoGen.
13 * AutoOpts is free software.
14 * AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
16 * AutoOpts is available under any one of two licenses. The license
17 * in use must be one of these two and the choice is under the control
18 * of the user of the license.
20 * The GNU Lesser General Public License, version 3 or later
21 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
23 * The Modified Berkeley Software Distribution License
24 * See the file "COPYING.mbsd"
26 * These files have the following md5sums:
28 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
29 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
30 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
33 /*=export_func optionPagedUsage
36 * what: Decipher a boolean value
37 * arg: + tOptions* + pOpts + program options descriptor +
38 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
41 * Run the usage output through a pager.
42 * This is very handy if it is very long.
43 * This is disabled on platforms without a working fork() function.
46 optionPagedUsage(tOptions
* pOptions
, tOptDesc
* pOD
)
48 #if ! defined(HAVE_WORKING_FORK)
49 if ((pOD
->fOptState
& OPTST_RESET
) != 0)
52 (*pOptions
->pUsageProc
)(pOptions
, EXIT_SUCCESS
);
55 char zPageUsage
[ 1024 ];
58 * IF we are being called after the usage proc is done
59 * (and thus has called "exit(2)")
60 * THEN invoke the pager to page through the usage file we created.
63 case PAGER_STATE_INITIAL
:
65 if ((pOD
->fOptState
& OPTST_RESET
) != 0)
70 snprintf(zPageUsage
, sizeof(zPageUsage
), TMP_USAGE_FMT
, (tAoUL
)my_pid
);
72 sprintf(zPageUsage
, TMP_USAGE_FMT
, (tAoUL
)my_pid
);
77 * Set usage output to this temporary file
79 option_usage_fp
= fopen(zPageUsage
, "w" FOPEN_BINARY_FLAG
);
80 if (option_usage_fp
== NULL
)
83 pagerState
= PAGER_STATE_READY
;
86 * Set up so this routine gets called during the exit logic
88 atexit((void(*)(void))optionPagedUsage
);
91 * The usage procedure will now put the usage information into
92 * the temporary file we created above.
94 (*pOptions
->pUsageProc
)(pOptions
, EXIT_SUCCESS
);
100 case PAGER_STATE_READY
:
102 tCC
* pzPager
= (tCC
*)getenv(PAGER_NAME
);
105 * Use the "more(1)" program if "PAGER" has not been defined
111 * Page the file and remove it when done.
114 snprintf(zPageUsage
, sizeof(zPageUsage
), PAGE_USAGE_FMT
, pzPager
, (tAoUL
)my_pid
);
116 sprintf(zPageUsage
, PAGE_USAGE_FMT
, pzPager
, (tAoUL
)my_pid
);
119 dup2(STDOUT_FILENO
, STDERR_FILENO
);
121 (void)system(zPageUsage
);
124 case PAGER_STATE_CHILD
:
126 * This is a child process used in creating shell script usage.
136 * c-file-style: "stroustrup"
137 * indent-tabs-mode: nil
139 * end of autoopts/pgusage.c */