pgusage.c 3.9 KB

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