Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / sntp / libopts / pgusage.c
blob45bfeb00cab658ca923522a83e7b9a380244efb8
1 /* $NetBSD$ */
4 /*
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
60 * private:
62 * what: Decipher a boolean value
63 * arg: + tOptions* + pOpts + program options descriptor +
64 * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
66 * doc:
67 * Run the usage output through a pager.
68 * This is very handy if it is very long.
69 =*/
70 void
71 optionPagedUsage( tOptions* pOptions, tOptDesc* pOD )
73 #if defined(__windows__) && !defined(__CYGWIN__)
74 (*pOptions->pUsageProc)( pOptions, EXIT_SUCCESS );
75 #else
76 static pid_t my_pid;
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.
84 switch (pagerState) {
85 case PAGER_STATE_INITIAL:
87 my_pid = getpid();
88 #ifdef HAVE_SNPRINTF
89 snprintf(zPageUsage, sizeof(zPageUsage), "/tmp/use.%lu", (tAoUL)my_pid);
90 #else
91 sprintf( zPageUsage, "/tmp/use.%lu", (tAoUL)my_pid );
92 #endif
93 unlink( zPageUsage );
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 );
115 /*NOTREACHED*/
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
127 if (pzPager == NULL)
128 pzPager = "more";
131 * Page the file and remove it when done.
133 #ifdef HAVE_SNPRINTF
134 snprintf(zPageUsage, sizeof(zPageUsage), zPage, pzPager, (tAoUL)my_pid);
135 #else
136 sprintf( zPageUsage, zPage, pzPager, (tAoUL)my_pid );
137 #endif
138 fclose( stderr );
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.
148 break;
150 #endif
154 * Local Variables:
155 * mode: C
156 * c-file-style: "stroustrup"
157 * indent-tabs-mode: nil
158 * End:
159 * end of autoopts/pgusage.c */