1
0

version.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /** \file version.c
  2. *
  3. * This module implements the default usage procedure for
  4. * Automated Options. It may be overridden, of course.
  5. *
  6. * @addtogroup autoopts
  7. * @{
  8. */
  9. /*
  10. * This file is part of AutoOpts, a companion to AutoGen.
  11. * AutoOpts is free software.
  12. * AutoOpts is Copyright (C) 1992-2015 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 sha256 sums:
  25. *
  26. * 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
  27. * 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
  28. * 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
  29. */
  30. /*=export_func optionVersion
  31. *
  32. * what: return the compiled AutoOpts version number
  33. * ret_type: char const *
  34. * ret_desc: the version string in constant memory
  35. * doc:
  36. * Returns the full version string compiled into the library.
  37. * The returned string cannot be modified.
  38. =*/
  39. char const *
  40. optionVersion(void)
  41. {
  42. static char const ver[] = OPTIONS_DOTTED_VERSION;
  43. return ver;
  44. }
  45. static void
  46. emit_first_line(
  47. FILE * fp, char const * alt1, char const * alt2, char const * alt3)
  48. {
  49. char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
  50. char const * e;
  51. if (p == NULL)
  52. return;
  53. e = strchr(p, NL);
  54. if (e == NULL)
  55. fputs(p, fp);
  56. else
  57. fwrite(p, 1, (e - p), fp);
  58. fputc(NL, fp);
  59. }
  60. /**
  61. * Select among various ways to emit version information.
  62. *
  63. * @param[in] o the option descriptor
  64. * @param[in] fp the output stream
  65. */
  66. static void
  67. emit_simple_ver(tOptions * o, FILE * fp)
  68. {
  69. emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
  70. }
  71. /**
  72. * print the version with a copyright notice.
  73. *
  74. * @param[in] o the option descriptor
  75. * @param[in] fp the output stream
  76. */
  77. static void
  78. emit_copy_full(tOptions * o, FILE * fp)
  79. {
  80. if (o->pzCopyright != NULL)
  81. fputs(o->pzCopyright, fp);
  82. else if (o->pzFullVersion != NULL)
  83. fputs(o->pzFullVersion, fp);
  84. else
  85. emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
  86. if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
  87. fputc(NL, fp);
  88. fputs(o->pzPackager, fp);
  89. } else if (o->pzBugAddr != NULL) {
  90. fputc(NL, fp);
  91. fprintf(fp, zPlsSendBugs, o->pzBugAddr);
  92. }
  93. }
  94. /**
  95. * print the version and any copyright notice.
  96. * The version with a full copyright and additional notes.
  97. *
  98. * @param[in] opts the option descriptor
  99. * @param[in] fp the output stream
  100. */
  101. static void
  102. emit_copy_note(tOptions * opts, FILE * fp)
  103. {
  104. if (opts->pzCopyright != NULL)
  105. fputs(opts->pzCopyright, fp);
  106. if (opts->pzCopyNotice != NULL)
  107. fputs(opts->pzCopyNotice, fp);
  108. fputc(NL, fp);
  109. fprintf(fp, zao_ver_fmt, optionVersion());
  110. if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
  111. fputc(NL, fp);
  112. fputs(opts->pzPackager, fp);
  113. } else if (opts->pzBugAddr != NULL) {
  114. fputc(NL, fp);
  115. fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
  116. }
  117. }
  118. /**
  119. * Handle the version printing. We must see how much information
  120. * is being requested and select the correct printing routine.
  121. */
  122. static void
  123. print_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
  124. {
  125. char ch;
  126. if (opts <= OPTPROC_EMIT_LIMIT)
  127. return;
  128. /*
  129. * IF we have an argument for this option, use it
  130. * Otherwise, default to version only or copyright note,
  131. * depending on whether the layout is GNU standard form or not.
  132. */
  133. if ( (od->fOptState & OPTST_ARG_OPTIONAL)
  134. && (od->optArg.argString != NULL)
  135. && (od->optArg.argString[0] != NUL))
  136. ch = od->optArg.argString[0];
  137. else {
  138. set_usage_flags(opts, NULL);
  139. ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
  140. }
  141. switch (ch) {
  142. case NUL: /* arg provided, but empty */
  143. case 'v': case 'V': emit_simple_ver(opts, fp); break;
  144. case 'c': case 'C': emit_copy_full( opts, fp); break;
  145. case 'n': case 'N': emit_copy_note( opts, fp); break;
  146. default:
  147. fprintf(stderr, zBadVerArg, ch);
  148. option_exits(EXIT_FAILURE);
  149. }
  150. fflush(fp);
  151. if (ferror(fp))
  152. fserr_exit(opts->pzProgName, zwriting,
  153. (fp == stdout) ? zstdout_name : zstderr_name);
  154. if (call_exit)
  155. option_exits(EXIT_SUCCESS);
  156. }
  157. /*=export_func optionPrintVersion
  158. *
  159. * what: Print the program version
  160. * arg: + tOptions * + opts + program options descriptor +
  161. * arg: + tOptDesc * + od + the descriptor for this arg +
  162. *
  163. * doc:
  164. * This routine will print the version to stdout.
  165. =*/
  166. void
  167. optionPrintVersion(tOptions * opts, tOptDesc * od)
  168. {
  169. print_ver(opts, od, print_exit ? stderr : stdout, true);
  170. }
  171. /*=export_func optionPrintVersionAndReturn
  172. *
  173. * what: Print the program version
  174. * arg: + tOptions * + opts + program options descriptor +
  175. * arg: + tOptDesc * + od + the descriptor for this arg +
  176. *
  177. * doc:
  178. * This routine will print the version to stdout and return
  179. * instead of exiting. Please see the source for the
  180. * @code{print_ver} funtion for details on selecting how
  181. * verbose to be after this function returns.
  182. =*/
  183. void
  184. optionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
  185. {
  186. print_ver(opts, od, print_exit ? stderr : stdout, false);
  187. }
  188. /*=export_func optionVersionStderr
  189. * private:
  190. *
  191. * what: Print the program version to stderr
  192. * arg: + tOptions * + opts + program options descriptor +
  193. * arg: + tOptDesc * + od + the descriptor for this arg +
  194. *
  195. * doc:
  196. * This routine will print the version to stderr.
  197. =*/
  198. void
  199. optionVersionStderr(tOptions * opts, tOptDesc * od)
  200. {
  201. print_ver(opts, od, stderr, true);
  202. }
  203. /** @}
  204. *
  205. * Local Variables:
  206. * mode: C
  207. * c-file-style: "stroustrup"
  208. * indent-tabs-mode: nil
  209. * End:
  210. * end of autoopts/version.c */