version.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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-2018 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 if (OPTST_GET_ARGTYPE(od->fOptState) == OPARG_TYPE_STATIC) {
  138. ch = od->optArg.argString[0];
  139. } else {
  140. set_usage_flags(opts, NULL);
  141. ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
  142. }
  143. switch (ch) {
  144. case NUL: /* arg provided, but empty */
  145. case 'v': case 'V': emit_simple_ver(opts, fp); break;
  146. case 'c': case 'C': emit_copy_full( opts, fp); break;
  147. case 'n': case 'N': emit_copy_note( opts, fp); break;
  148. default:
  149. fprintf(stderr, zBadVerArg, ch);
  150. option_exits(EXIT_FAILURE);
  151. }
  152. fflush(fp);
  153. if (ferror(fp))
  154. fserr_exit(opts->pzProgName, zwriting,
  155. (fp == stdout) ? zstdout_name : zstderr_name);
  156. if (call_exit)
  157. option_exits(EXIT_SUCCESS);
  158. }
  159. /*=export_func optionPrintVersion
  160. *
  161. * what: Print the program version
  162. * arg: + tOptions * + opts + program options descriptor +
  163. * arg: + tOptDesc * + od + the descriptor for this arg +
  164. *
  165. * doc:
  166. * This routine will print the version to stdout.
  167. =*/
  168. void
  169. optionPrintVersion(tOptions * opts, tOptDesc * od)
  170. {
  171. print_ver(opts, od, print_exit ? stderr : stdout, true);
  172. }
  173. /*=export_func optionPrintVersionAndReturn
  174. *
  175. * what: Print the program version
  176. * arg: + tOptions * + opts + program options descriptor +
  177. * arg: + tOptDesc * + od + the descriptor for this arg +
  178. *
  179. * doc:
  180. * This routine will print the version to stdout and return
  181. * instead of exiting. Please see the source for the
  182. * @code{print_ver} funtion for details on selecting how
  183. * verbose to be after this function returns.
  184. =*/
  185. void
  186. optionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
  187. {
  188. print_ver(opts, od, print_exit ? stderr : stdout, false);
  189. }
  190. /*=export_func optionVersionStderr
  191. * private:
  192. *
  193. * what: Print the program version to stderr
  194. * arg: + tOptions * + opts + program options descriptor +
  195. * arg: + tOptDesc * + od + the descriptor for this arg +
  196. *
  197. * doc:
  198. * This routine will print the version to stderr.
  199. =*/
  200. void
  201. optionVersionStderr(tOptions * opts, tOptDesc * od)
  202. {
  203. print_ver(opts, od, stderr, true);
  204. }
  205. /** @}
  206. *
  207. * Local Variables:
  208. * mode: C
  209. * c-file-style: "stroustrup"
  210. * indent-tabs-mode: nil
  211. * End:
  212. * end of autoopts/version.c */