3 * $Id: pgusage.c,v 4.12 2007/04/28 22:19:23 bkorb Exp $
4 * Time-stamp: "2006-07-16 08:13:26 bkorb"
6 * Automated Options Paged Usage module.
8 * This routine will run run-on options through a pager so the
9 * user may examine, print or edit them at their leisure.
13 * Automated Options copyright 1992-2007 Bruce Korb
15 * Automated Options is free software.
16 * You may redistribute it and/or modify it under the terms of the
17 * GNU General Public License, as published by the Free Software
18 * Foundation; either version 2, or (at your option) any later version.
20 * Automated Options is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with Automated Options. See the file "COPYING". If not,
27 * write to: The Free Software Foundation, Inc.,
28 * 51 Franklin Street, Fifth Floor,
29 * Boston, MA 02110-1301, USA.
31 * As a special exception, Bruce Korb gives permission for additional
32 * uses of the text contained in his release of AutoOpts.
34 * The exception is that, if you link the AutoOpts library with other
35 * files to produce an executable, this does not by itself cause the
36 * resulting executable to be covered by the GNU General Public License.
37 * Your use of that executable is in no way restricted on account of
38 * linking the AutoOpts library code into it.
40 * This exception does not however invalidate any other reasons why
41 * the executable file might be covered by the GNU General Public License.
43 * This exception applies only to the code released by Bruce Korb under
44 * the name AutoOpts. If you copy code from other sources under the
45 * General Public License into a copy of AutoOpts, as the General Public
46 * License permits, the exception does not apply to the code that you add
47 * in this way. To avoid misleading anyone as to the status of such
48 * modified files, you must delete this exception notice from them.
50 * If you write modifications of your own for AutoOpts, it is your choice
51 * whether to permit this exception to apply to your modifications.
52 * If you do not wish that, delete this exception notice.
55 tePagerState pagerState = PAGER_STATE_INITIAL;
57 /*=export_func optionPagedUsage
60 * what: Decipher a boolean value
61 * arg: + tOptions* + pOpts + program options descriptor +
62 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
65 * Run the usage output through a pager.
66 * This is very handy if it is very long.
69 optionPagedUsage( tOptions* pOptions, tOptDesc* pOD )
71 #if defined(__windows__) && !defined(__CYGWIN__)
72 (*pOptions->pUsageProc)( pOptions, EXIT_SUCCESS );
75 char zPageUsage[ 1024 ];
78 * IF we are being called after the usage proc is done
79 * (and thus has called "exit(2)")
80 * THEN invoke the pager to page through the usage file we created.
83 case PAGER_STATE_INITIAL:
87 snprintf(zPageUsage, sizeof(zPageUsage), "/tmp/use.%lu", (tAoUL)my_pid);
89 sprintf( zPageUsage, "/tmp/use.%lu", (tAoUL)my_pid );
94 * Set usage output to this temporary file
96 option_usage_fp = fopen( zPageUsage, "w" FOPEN_BINARY_FLAG );
97 if (option_usage_fp == NULL)
98 _exit( EXIT_FAILURE );
100 pagerState = PAGER_STATE_READY;
103 * Set up so this routine gets called during the exit logic
105 atexit( (void(*)(void))optionPagedUsage );
108 * The usage procedure will now put the usage information into
109 * the temporary file we created above.
111 (*pOptions->pUsageProc)( pOptions, EXIT_SUCCESS );
114 _exit( EXIT_FAILURE );
117 case PAGER_STATE_READY:
119 tSCC zPage[] = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu";
120 tCC* pzPager = (tCC*)getenv( "PAGER" );
123 * Use the "more(1)" program if "PAGER" has not been defined
129 * Page the file and remove it when done.
132 snprintf(zPageUsage, sizeof(zPageUsage), zPage, pzPager, (tAoUL)my_pid);
134 sprintf( zPageUsage, zPage, pzPager, (tAoUL)my_pid );
137 dup2( STDOUT_FILENO, STDERR_FILENO );
139 (void)system( zPageUsage );
142 case PAGER_STATE_CHILD:
144 * This is a child process used in creating shell script usage.
154 * c-file-style: "stroustrup"
155 * indent-tabs-mode: nil
157 * end of autoopts/pgusage.c */