File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / sntp / libopts / pgusage.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:08:38 2012 UTC (12 years, 7 months ago) by misho
Branches: ntp, MAIN
CVS tags: v4_2_6p5p0, v4_2_6p5, HEAD
ntp 4.2.6p5

    1: 
    2: /**
    3:  * \file pgusage.c
    4:  *
    5:  * Time-stamp:      "2011-03-25 17:54:41 bkorb"
    6:  *
    7:  *   Automated Options Paged Usage module.
    8:  *
    9:  *  This routine will run run-on options through a pager so the
   10:  *  user may examine, print or edit them at their leisure.
   11:  *
   12:  *  This file is part of AutoOpts, a companion to AutoGen.
   13:  *  AutoOpts is free software.
   14:  *  AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
   15:  *
   16:  *  AutoOpts is available under any one of two licenses.  The license
   17:  *  in use must be one of these two and the choice is under the control
   18:  *  of the user of the license.
   19:  *
   20:  *   The GNU Lesser General Public License, version 3 or later
   21:  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
   22:  *
   23:  *   The Modified Berkeley Software Distribution License
   24:  *      See the file "COPYING.mbsd"
   25:  *
   26:  *  These files have the following md5sums:
   27:  *
   28:  *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
   29:  *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
   30:  *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
   31:  */
   32: 
   33: /*=export_func  optionPagedUsage
   34:  * private:
   35:  *
   36:  * what:  Decipher a boolean value
   37:  * arg:   + tOptions* + pOpts    + program options descriptor +
   38:  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
   39:  *
   40:  * doc:
   41:  *  Run the usage output through a pager.
   42:  *  This is very handy if it is very long.
   43:  *  This is disabled on platforms without a working fork() function.
   44: =*/
   45: void
   46: optionPagedUsage(tOptions* pOptions, tOptDesc* pOD)
   47: {
   48: #if ! defined(HAVE_WORKING_FORK)
   49:     if ((pOD->fOptState & OPTST_RESET) != 0)
   50:         return;
   51: 
   52:     (*pOptions->pUsageProc)(pOptions, EXIT_SUCCESS);
   53: #else
   54:     static pid_t     my_pid;
   55:     char zPageUsage[ 1024 ];
   56: 
   57:     /*
   58:      *  IF we are being called after the usage proc is done
   59:      *     (and thus has called "exit(2)")
   60:      *  THEN invoke the pager to page through the usage file we created.
   61:      */
   62:     switch (pagerState) {
   63:     case PAGER_STATE_INITIAL:
   64:     {
   65:         if ((pOD->fOptState & OPTST_RESET) != 0)
   66:             return;
   67: 
   68:         my_pid  = getpid();
   69: #ifdef HAVE_SNPRINTF
   70:         snprintf(zPageUsage, sizeof(zPageUsage), "/tmp/use.%lu", (tAoUL)my_pid);
   71: #else
   72:         sprintf(zPageUsage, "/tmp/use.%lu", (tAoUL)my_pid);
   73: #endif
   74:         unlink(zPageUsage);
   75: 
   76:         /*
   77:          *  Set usage output to this temporary file
   78:          */
   79:         option_usage_fp = fopen(zPageUsage, "w" FOPEN_BINARY_FLAG);
   80:         if (option_usage_fp == NULL)
   81:             _exit(EXIT_FAILURE);
   82: 
   83:         pagerState = PAGER_STATE_READY;
   84: 
   85:         /*
   86:          *  Set up so this routine gets called during the exit logic
   87:          */
   88:         atexit((void(*)(void))optionPagedUsage);
   89: 
   90:         /*
   91:          *  The usage procedure will now put the usage information into
   92:          *  the temporary file we created above.
   93:          */
   94:         (*pOptions->pUsageProc)(pOptions, EXIT_SUCCESS);
   95: 
   96:         /* NOTREACHED */
   97:         _exit(EXIT_FAILURE);
   98:     }
   99: 
  100:     case PAGER_STATE_READY:
  101:     {
  102:         tSCC zPage[]  = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu";
  103:         tCC* pzPager  = (tCC*)getenv("PAGER");
  104: 
  105:         /*
  106:          *  Use the "more(1)" program if "PAGER" has not been defined
  107:          */
  108:         if (pzPager == NULL)
  109:             pzPager = "more";
  110: 
  111:         /*
  112:          *  Page the file and remove it when done.
  113:          */
  114: #ifdef HAVE_SNPRINTF
  115:         snprintf(zPageUsage, sizeof(zPageUsage), zPage, pzPager, (tAoUL)my_pid);
  116: #else
  117:         sprintf(zPageUsage, zPage, pzPager, (tAoUL)my_pid);
  118: #endif
  119:         fclose(stderr);
  120:         dup2(STDOUT_FILENO, STDERR_FILENO);
  121: 
  122:         (void)system(zPageUsage);
  123:     }
  124: 
  125:     case PAGER_STATE_CHILD:
  126:         /*
  127:          *  This is a child process used in creating shell script usage.
  128:          */
  129:         break;
  130:     }
  131: #endif
  132: }
  133: 
  134: /*
  135:  * Local Variables:
  136:  * mode: C
  137:  * c-file-style: "stroustrup"
  138:  * indent-tabs-mode: nil
  139:  * End:
  140:  * end of autoopts/pgusage.c */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>