1
0

pgusage.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * $Id: pgusage.c,v 4.4 2005/03/13 19:51:59 bkorb Exp $
  3. * Time-stamp: "2005-02-14 08:22:01 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-2005 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. * 59 Temple Place - Suite 330,
  27. * Boston, MA 02111-1307, 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. static pid_t my_pid;
  68. char zPageUsage[ 1024 ];
  69. /*
  70. * IF we are being called after the usage proc is done
  71. * (and thus has called "exit(2)")
  72. * THEN invoke the pager to page through the usage file we created.
  73. */
  74. switch (pagerState) {
  75. case PAGER_STATE_INITIAL:
  76. {
  77. my_pid = getpid();
  78. #ifdef HAVE_SNPRINTF
  79. snprintf( zPageUsage, sizeof(zPageUsage), "/tmp/use.%lu", (tUL)my_pid );
  80. #else
  81. sprintf( zPageUsage, "/tmp/use.%lu", (tUL)my_pid );
  82. #endif
  83. unlink( zPageUsage );
  84. /*
  85. * Set usage output to this temporary file
  86. */
  87. option_usage_fp = fopen( zPageUsage, "w" FOPEN_BINARY_FLAG );
  88. if (option_usage_fp == NULL)
  89. _exit( EXIT_FAILURE );
  90. pagerState = PAGER_STATE_READY;
  91. /*
  92. * Set up so this routine gets called during the exit logic
  93. */
  94. atexit( (void(*)(void))optionPagedUsage );
  95. /*
  96. * The usage procedure will now put the usage information into
  97. * the temporary file we created above.
  98. */
  99. (*pOptions->pUsageProc)( pOptions, EXIT_SUCCESS );
  100. /*NOTREACHED*/
  101. _exit( EXIT_FAILURE );
  102. }
  103. case PAGER_STATE_READY:
  104. {
  105. tSCC zPage[] = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu";
  106. char* pzPager = getenv( "PAGER" );
  107. /*
  108. * Use the "more(1)" program if "PAGER" has not been defined
  109. */
  110. if (pzPager == NULL)
  111. pzPager = "more";
  112. /*
  113. * Page the file and remove it when done.
  114. */
  115. #ifdef HAVE_SNPRINTF
  116. snprintf( zPageUsage, sizeof(zPageUsage), zPage, pzPager, (tUL)my_pid );
  117. #else
  118. sprintf( zPageUsage, zPage, pzPager, (tUL)my_pid );
  119. #endif
  120. fclose( stderr );
  121. dup2( STDOUT_FILENO, STDERR_FILENO );
  122. system( zPageUsage );
  123. }
  124. case PAGER_STATE_CHILD:
  125. /*
  126. * This is a child process used in creating shell script usage.
  127. */
  128. break;
  129. }
  130. }
  131. /*
  132. * Local Variables:
  133. * mode: C
  134. * c-file-style: "stroustrup"
  135. * tab-width: 4
  136. * indent-tabs-mode: nil
  137. * End:
  138. * end of autoopts/pgusage.c */