pgusage.c 5.0 KB

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