pgusage.c 3.8 KB

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