5 * Id: da09fc943da953195b243b7697fdbf5127f5b6c2
6 * Time-stamp: "2009-11-01 11:52:51 bkorb"
8 * Automated Options Paged Usage module.
10 * This routine will run run-on options through a pager so the
11 * user may examine, print or edit them at their leisure.
13 * This file is part of AutoOpts, a companion to AutoGen.
14 * AutoOpts is free software.
15 * AutoOpts is copyright (c) 1992-2009 by Bruce Korb - all rights reserved
17 * AutoOpts is available under any one of two licenses. The license
18 * in use must be one of these two and the choice is under the control
19 * of the user of the license.
21 * The GNU Lesser General Public License, version 3 or later
22 * See the files "COPYING.lgplv3" and "COPYING.gplv3"
24 * The Modified Berkeley Software Distribution License
25 * See the file "COPYING.mbsd"
27 * These files have the following md5sums:
29 * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
30 * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
31 * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
34 tePagerState pagerState
= PAGER_STATE_INITIAL
;
36 /*=export_func optionPagedUsage
39 * what: Decipher a boolean value
40 * arg: + tOptions* + pOpts + program options descriptor +
41 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
44 * Run the usage output through a pager.
45 * This is very handy if it is very long.
46 * This is disabled on platforms without a working fork() function.
49 optionPagedUsage( tOptions
* pOptions
, tOptDesc
* pOD
)
51 #if ! defined(HAVE_WORKING_FORK)
52 if ((pOD
->fOptState
& OPTST_RESET
) != 0)
55 (*pOptions
->pUsageProc
)( pOptions
, EXIT_SUCCESS
);
58 char zPageUsage
[ 1024 ];
61 * IF we are being called after the usage proc is done
62 * (and thus has called "exit(2)")
63 * THEN invoke the pager to page through the usage file we created.
66 case PAGER_STATE_INITIAL
:
68 if ((pOD
->fOptState
& OPTST_RESET
) != 0)
73 snprintf(zPageUsage
, sizeof(zPageUsage
), "/tmp/use.%lu", (tAoUL
)my_pid
);
75 sprintf( zPageUsage
, "/tmp/use.%lu", (tAoUL
)my_pid
);
80 * Set usage output to this temporary file
82 option_usage_fp
= fopen( zPageUsage
, "w" FOPEN_BINARY_FLAG
);
83 if (option_usage_fp
== NULL
)
84 _exit( EXIT_FAILURE
);
86 pagerState
= PAGER_STATE_READY
;
89 * Set up so this routine gets called during the exit logic
91 atexit( (void(*)(void))optionPagedUsage
);
94 * The usage procedure will now put the usage information into
95 * the temporary file we created above.
97 (*pOptions
->pUsageProc
)( pOptions
, EXIT_SUCCESS
);
100 _exit( EXIT_FAILURE
);
103 case PAGER_STATE_READY
:
105 tSCC zPage
[] = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu";
106 tCC
* pzPager
= (tCC
*)getenv( "PAGER" );
109 * Use the "more(1)" program if "PAGER" has not been defined
115 * Page the file and remove it when done.
118 snprintf(zPageUsage
, sizeof(zPageUsage
), zPage
, pzPager
, (tAoUL
)my_pid
);
120 sprintf( zPageUsage
, zPage
, pzPager
, (tAoUL
)my_pid
);
123 dup2( STDOUT_FILENO
, STDERR_FILENO
);
125 (void)system( zPageUsage
);
128 case PAGER_STATE_CHILD
:
130 * This is a child process used in creating shell script usage.
140 * c-file-style: "stroustrup"
141 * indent-tabs-mode: nil
143 * end of autoopts/pgusage.c */