1
0

pgusage.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * $Id: pgusage.c,v 4.14 2007/07/04 21:36:38 bkorb Exp $
  3. * Time-stamp: "2007-07-04 11:34:23 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-2007 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. * 239588c55c22c60ffe159946a760a33e pkg/libopts/COPYING.gplv3
  27. * fa82ca978890795162346e661b47161a 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. (*pOptions->pUsageProc)( pOptions, EXIT_SUCCESS );
  47. #else
  48. static pid_t my_pid;
  49. char zPageUsage[ 1024 ];
  50. /*
  51. * IF we are being called after the usage proc is done
  52. * (and thus has called "exit(2)")
  53. * THEN invoke the pager to page through the usage file we created.
  54. */
  55. switch (pagerState) {
  56. case PAGER_STATE_INITIAL:
  57. {
  58. my_pid = getpid();
  59. #ifdef HAVE_SNPRINTF
  60. snprintf(zPageUsage, sizeof(zPageUsage), "/tmp/use.%lu", (tAoUL)my_pid);
  61. #else
  62. sprintf( zPageUsage, "/tmp/use.%lu", (tAoUL)my_pid );
  63. #endif
  64. unlink( zPageUsage );
  65. /*
  66. * Set usage output to this temporary file
  67. */
  68. option_usage_fp = fopen( zPageUsage, "w" FOPEN_BINARY_FLAG );
  69. if (option_usage_fp == NULL)
  70. _exit( EXIT_FAILURE );
  71. pagerState = PAGER_STATE_READY;
  72. /*
  73. * Set up so this routine gets called during the exit logic
  74. */
  75. atexit( (void(*)(void))optionPagedUsage );
  76. /*
  77. * The usage procedure will now put the usage information into
  78. * the temporary file we created above.
  79. */
  80. (*pOptions->pUsageProc)( pOptions, EXIT_SUCCESS );
  81. /*NOTREACHED*/
  82. _exit( EXIT_FAILURE );
  83. }
  84. case PAGER_STATE_READY:
  85. {
  86. tSCC zPage[] = "%1$s /tmp/use.%2$lu ; rm -f /tmp/use.%2$lu";
  87. tCC* pzPager = (tCC*)getenv( "PAGER" );
  88. /*
  89. * Use the "more(1)" program if "PAGER" has not been defined
  90. */
  91. if (pzPager == NULL)
  92. pzPager = "more";
  93. /*
  94. * Page the file and remove it when done.
  95. */
  96. #ifdef HAVE_SNPRINTF
  97. snprintf(zPageUsage, sizeof(zPageUsage), zPage, pzPager, (tAoUL)my_pid);
  98. #else
  99. sprintf( zPageUsage, zPage, pzPager, (tAoUL)my_pid );
  100. #endif
  101. fclose( stderr );
  102. dup2( STDOUT_FILENO, STDERR_FILENO );
  103. (void)system( zPageUsage );
  104. }
  105. case PAGER_STATE_CHILD:
  106. /*
  107. * This is a child process used in creating shell script usage.
  108. */
  109. break;
  110. }
  111. #endif
  112. }
  113. /*
  114. * Local Variables:
  115. * mode: C
  116. * c-file-style: "stroustrup"
  117. * indent-tabs-mode: nil
  118. * End:
  119. * end of autoopts/pgusage.c */