5 * Id: pgusage.c,v 4.12 2007/04/28 22:19:23 bkorb Exp
6 * Time-stamp: "2006-07-16 08:13:26 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.
15 * Automated Options copyright 1992-2007 Bruce Korb
17 * Automated Options is free software.
18 * You may redistribute it and/or modify it under the terms of the
19 * GNU General Public License, as published by the Free Software
20 * Foundation; either version 2, or (at your option) any later version.
22 * Automated Options is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with Automated Options. See the file "COPYING". If not,
29 * write to: The Free Software Foundation, Inc.,
30 * 51 Franklin Street, Fifth Floor,
31 * Boston, MA 02110-1301, USA.
33 * As a special exception, Bruce Korb gives permission for additional
34 * uses of the text contained in his release of AutoOpts.
36 * The exception is that, if you link the AutoOpts library with other
37 * files to produce an executable, this does not by itself cause the
38 * resulting executable to be covered by the GNU General Public License.
39 * Your use of that executable is in no way restricted on account of
40 * linking the AutoOpts library code into it.
42 * This exception does not however invalidate any other reasons why
43 * the executable file might be covered by the GNU General Public License.
45 * This exception applies only to the code released by Bruce Korb under
46 * the name AutoOpts. If you copy code from other sources under the
47 * General Public License into a copy of AutoOpts, as the General Public
48 * License permits, the exception does not apply to the code that you add
49 * in this way. To avoid misleading anyone as to the status of such
50 * modified files, you must delete this exception notice from them.
52 * If you write modifications of your own for AutoOpts, it is your choice
53 * whether to permit this exception to apply to your modifications.
54 * If you do not wish that, delete this exception notice.
57 tePagerState pagerState
= PAGER_STATE_INITIAL
;
59 /*=export_func optionPagedUsage
62 * what: Decipher a boolean value
63 * arg: + tOptions* + pOpts + program options descriptor +
64 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
67 * Run the usage output through a pager.
68 * This is very handy if it is very long.
71 optionPagedUsage( tOptions
* pOptions
, tOptDesc
* pOD
)
73 #if defined(__windows__) && !defined(__CYGWIN__)
74 (*pOptions
->pUsageProc
)( pOptions
, EXIT_SUCCESS
);
77 char zPageUsage
[ 1024 ];
80 * IF we are being called after the usage proc is done
81 * (and thus has called "exit(2)")
82 * THEN invoke the pager to page through the usage file we created.
85 case PAGER_STATE_INITIAL
:
89 snprintf(zPageUsage
, sizeof(zPageUsage
), "/tmp/use.%lu", (tAoUL
)my_pid
);
91 sprintf( zPageUsage
, "/tmp/use.%lu", (tAoUL
)my_pid
);
96 * Set usage output to this temporary file
98 option_usage_fp
= fopen( zPageUsage
, "w" FOPEN_BINARY_FLAG
);
99 if (option_usage_fp
== NULL
)
100 _exit( EXIT_FAILURE
);
102 pagerState
= PAGER_STATE_READY
;
105 * Set up so this routine gets called during the exit logic
107 atexit( (void(*)(void))optionPagedUsage
);
110 * The usage procedure will now put the usage information into
111 * the temporary file we created above.
113 (*pOptions
->pUsageProc
)( pOptions
, EXIT_SUCCESS
);
116 _exit( EXIT_FAILURE
);
119 case PAGER_STATE_READY
:
121 tSCC zPage
[] = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu";
122 tCC
* pzPager
= (tCC
*)getenv( "PAGER" );
125 * Use the "more(1)" program if "PAGER" has not been defined
131 * Page the file and remove it when done.
134 snprintf(zPageUsage
, sizeof(zPageUsage
), zPage
, pzPager
, (tAoUL
)my_pid
);
136 sprintf( zPageUsage
, zPage
, pzPager
, (tAoUL
)my_pid
);
139 dup2( STDOUT_FILENO
, STDERR_FILENO
);
141 (void)system( zPageUsage
);
144 case PAGER_STATE_CHILD
:
146 * This is a child process used in creating shell script usage.
156 * c-file-style: "stroustrup"
157 * indent-tabs-mode: nil
159 * end of autoopts/pgusage.c */