pgusage.c revision 1.1.1.2
1117397Skan/* $NetBSD: pgusage.c,v 1.1.1.2 2012/01/31 21:27:52 kardel Exp $ */ 2117397Skan 3117397Skan 4117397Skan/** 5117397Skan * \file pgusage.c 6117397Skan * 7117397Skan * Time-stamp: "2011-03-25 17:54:41 bkorb" 8117397Skan * 9117397Skan * Automated Options Paged Usage module. 10117397Skan * 11117397Skan * This routine will run run-on options through a pager so the 12117397Skan * user may examine, print or edit them at their leisure. 13117397Skan * 14117397Skan * This file is part of AutoOpts, a companion to AutoGen. 15117397Skan * AutoOpts is free software. 16117397Skan * AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved 17117397Skan * 18169691Skan * AutoOpts is available under any one of two licenses. The license 19117397Skan * in use must be one of these two and the choice is under the control 20117397Skan * of the user of the license. 21117397Skan * 22117397Skan * The GNU Lesser General Public License, version 3 or later 23117397Skan * See the files "COPYING.lgplv3" and "COPYING.gplv3" 24117397Skan * 25117397Skan * The Modified Berkeley Software Distribution License 26117397Skan * See the file "COPYING.mbsd" 27117397Skan * 28117397Skan * These files have the following md5sums: 29169691Skan * 30169691Skan * 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3 31169691Skan * 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3 32169691Skan * 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd 33169691Skan */ 34117397Skan 35117397Skan/*=export_func optionPagedUsage 36117397Skan * private: 37117397Skan * 38169691Skan * what: Decipher a boolean value 39117397Skan * arg: + tOptions* + pOpts + program options descriptor + 40117397Skan * arg: + tOptDesc* + pOptDesc + the descriptor for this arg + 41117397Skan * 42169691Skan * doc: 43169691Skan * Run the usage output through a pager. 44117397Skan * This is very handy if it is very long. 45117397Skan * This is disabled on platforms without a working fork() function. 46117397Skan=*/ 47117397Skanvoid 48117397SkanoptionPagedUsage(tOptions* pOptions, tOptDesc* pOD) 49117397Skan{ 50117397Skan#if ! defined(HAVE_WORKING_FORK) 51117397Skan if ((pOD->fOptState & OPTST_RESET) != 0) 52117397Skan return; 53117397Skan 54117397Skan (*pOptions->pUsageProc)(pOptions, EXIT_SUCCESS); 55117397Skan#else 56117397Skan static pid_t my_pid; 57117397Skan char zPageUsage[ 1024 ]; 58117397Skan 59117397Skan /* 60117397Skan * IF we are being called after the usage proc is done 61117397Skan * (and thus has called "exit(2)") 62117397Skan * THEN invoke the pager to page through the usage file we created. 63117397Skan */ 64117397Skan switch (pagerState) { 65117397Skan case PAGER_STATE_INITIAL: 66117397Skan { 67117397Skan if ((pOD->fOptState & OPTST_RESET) != 0) 68117397Skan return; 69117397Skan 70117397Skan my_pid = getpid(); 71117397Skan#ifdef HAVE_SNPRINTF 72117397Skan snprintf(zPageUsage, sizeof(zPageUsage), "/tmp/use.%lu", (tAoUL)my_pid); 73117397Skan#else 74117397Skan sprintf(zPageUsage, "/tmp/use.%lu", (tAoUL)my_pid); 75117397Skan#endif 76117397Skan unlink(zPageUsage); 77169691Skan 78169691Skan /* 79 * Set usage output to this temporary file 80 */ 81 option_usage_fp = fopen(zPageUsage, "w" FOPEN_BINARY_FLAG); 82 if (option_usage_fp == NULL) 83 _exit(EXIT_FAILURE); 84 85 pagerState = PAGER_STATE_READY; 86 87 /* 88 * Set up so this routine gets called during the exit logic 89 */ 90 atexit((void(*)(void))optionPagedUsage); 91 92 /* 93 * The usage procedure will now put the usage information into 94 * the temporary file we created above. 95 */ 96 (*pOptions->pUsageProc)(pOptions, EXIT_SUCCESS); 97 98 /* NOTREACHED */ 99 _exit(EXIT_FAILURE); 100 } 101 102 case PAGER_STATE_READY: 103 { 104 tSCC zPage[] = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu"; 105 tCC* pzPager = (tCC*)getenv("PAGER"); 106 107 /* 108 * Use the "more(1)" program if "PAGER" has not been defined 109 */ 110 if (pzPager == NULL) 111 pzPager = "more"; 112 113 /* 114 * Page the file and remove it when done. 115 */ 116#ifdef HAVE_SNPRINTF 117 snprintf(zPageUsage, sizeof(zPageUsage), zPage, pzPager, (tAoUL)my_pid); 118#else 119 sprintf(zPageUsage, zPage, pzPager, (tAoUL)my_pid); 120#endif 121 fclose(stderr); 122 dup2(STDOUT_FILENO, STDERR_FILENO); 123 124 (void)system(zPageUsage); 125 } 126 127 case PAGER_STATE_CHILD: 128 /* 129 * This is a child process used in creating shell script usage. 130 */ 131 break; 132 } 133#endif 134} 135 136/* 137 * Local Variables: 138 * mode: C 139 * c-file-style: "stroustrup" 140 * indent-tabs-mode: nil 141 * End: 142 * end of autoopts/pgusage.c */ 143