xref: /freebsd/contrib/ntp/sntp/libopts/version.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1ea906c41SOllivier Robert 
22b15cb3dSCy Schubert /** \file version.c
3ea906c41SOllivier Robert  *
4ea906c41SOllivier Robert  *  This module implements the default usage procedure for
5ea906c41SOllivier Robert  *  Automated Options.  It may be overridden, of course.
62b15cb3dSCy Schubert  *
72b15cb3dSCy Schubert  * @addtogroup autoopts
82b15cb3dSCy Schubert  * @{
9ea906c41SOllivier Robert  */
102b15cb3dSCy Schubert /*
112b15cb3dSCy Schubert  *  This file is part of AutoOpts, a companion to AutoGen.
122b15cb3dSCy Schubert  *  AutoOpts is free software.
13*a466cc55SCy Schubert  *  AutoOpts is Copyright (C) 1992-2018 by Bruce Korb - all rights reserved
14ea906c41SOllivier Robert  *
152b15cb3dSCy Schubert  *  AutoOpts is available under any one of two licenses.  The license
162b15cb3dSCy Schubert  *  in use must be one of these two and the choice is under the control
172b15cb3dSCy Schubert  *  of the user of the license.
18ea906c41SOllivier Robert  *
192b15cb3dSCy Schubert  *   The GNU Lesser General Public License, version 3 or later
202b15cb3dSCy Schubert  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21ea906c41SOllivier Robert  *
222b15cb3dSCy Schubert  *   The Modified Berkeley Software Distribution License
232b15cb3dSCy Schubert  *      See the file "COPYING.mbsd"
24ea906c41SOllivier Robert  *
252b15cb3dSCy Schubert  *  These files have the following sha256 sums:
26ea906c41SOllivier Robert  *
272b15cb3dSCy Schubert  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
282b15cb3dSCy Schubert  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
292b15cb3dSCy Schubert  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30ea906c41SOllivier Robert  */
31ea906c41SOllivier Robert 
32ea906c41SOllivier Robert /*=export_func  optionVersion
33ea906c41SOllivier Robert  *
34ea906c41SOllivier Robert  * what:     return the compiled AutoOpts version number
35ea906c41SOllivier Robert  * ret_type: char const *
36ea906c41SOllivier Robert  * ret_desc: the version string in constant memory
37ea906c41SOllivier Robert  * doc:
38ea906c41SOllivier Robert  *  Returns the full version string compiled into the library.
39ea906c41SOllivier Robert  *  The returned string cannot be modified.
40ea906c41SOllivier Robert =*/
41ea906c41SOllivier Robert char const *
optionVersion(void)42ea906c41SOllivier Robert optionVersion(void)
43ea906c41SOllivier Robert {
442b15cb3dSCy Schubert     static char const ver[] = OPTIONS_DOTTED_VERSION;
452b15cb3dSCy Schubert     return ver;
46ea906c41SOllivier Robert }
47ea906c41SOllivier Robert 
48ea906c41SOllivier Robert static void
emit_first_line(FILE * fp,char const * alt1,char const * alt2,char const * alt3)492b15cb3dSCy Schubert emit_first_line(
502b15cb3dSCy Schubert     FILE * fp, char const * alt1, char const * alt2, char const * alt3)
51ea906c41SOllivier Robert {
522b15cb3dSCy Schubert     char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
532b15cb3dSCy Schubert     char const * e;
542b15cb3dSCy Schubert     if (p == NULL)
552b15cb3dSCy Schubert         return;
562b15cb3dSCy Schubert     e = strchr(p, NL);
572b15cb3dSCy Schubert     if (e == NULL)
582b15cb3dSCy Schubert         fputs(p, fp);
592b15cb3dSCy Schubert     else
602b15cb3dSCy Schubert         fwrite(p, 1, (e - p), fp);
612b15cb3dSCy Schubert     fputc(NL, fp);
622b15cb3dSCy Schubert }
632b15cb3dSCy Schubert 
642b15cb3dSCy Schubert /**
652b15cb3dSCy Schubert  * Select among various ways to emit version information.
662b15cb3dSCy Schubert  *
672b15cb3dSCy Schubert  * @param[in] o   the option descriptor
682b15cb3dSCy Schubert  * @param[in] fp  the output stream
692b15cb3dSCy Schubert  */
702b15cb3dSCy Schubert static void
emit_simple_ver(tOptions * o,FILE * fp)712b15cb3dSCy Schubert emit_simple_ver(tOptions * o, FILE * fp)
722b15cb3dSCy Schubert {
732b15cb3dSCy Schubert     emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
742b15cb3dSCy Schubert }
752b15cb3dSCy Schubert 
762b15cb3dSCy Schubert /**
772b15cb3dSCy Schubert  * print the version with a copyright notice.
782b15cb3dSCy Schubert  *
792b15cb3dSCy Schubert  * @param[in] o   the option descriptor
802b15cb3dSCy Schubert  * @param[in] fp  the output stream
812b15cb3dSCy Schubert  */
822b15cb3dSCy Schubert static void
emit_copy_full(tOptions * o,FILE * fp)832b15cb3dSCy Schubert emit_copy_full(tOptions * o, FILE * fp)
842b15cb3dSCy Schubert {
852b15cb3dSCy Schubert     if (o->pzCopyright != NULL)
862b15cb3dSCy Schubert         fputs(o->pzCopyright, fp);
872b15cb3dSCy Schubert 
882b15cb3dSCy Schubert     else if (o->pzFullVersion != NULL)
892b15cb3dSCy Schubert         fputs(o->pzFullVersion, fp);
902b15cb3dSCy Schubert 
912b15cb3dSCy Schubert     else
922b15cb3dSCy Schubert         emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
932b15cb3dSCy Schubert 
942b15cb3dSCy Schubert     if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
952b15cb3dSCy Schubert         fputc(NL, fp);
962b15cb3dSCy Schubert         fputs(o->pzPackager, fp);
972b15cb3dSCy Schubert 
982b15cb3dSCy Schubert     } else if (o->pzBugAddr != NULL) {
992b15cb3dSCy Schubert         fputc(NL, fp);
1002b15cb3dSCy Schubert         fprintf(fp, zPlsSendBugs, o->pzBugAddr);
1012b15cb3dSCy Schubert     }
1022b15cb3dSCy Schubert }
1032b15cb3dSCy Schubert 
1042b15cb3dSCy Schubert /**
1052b15cb3dSCy Schubert  * print the version and any copyright notice.
1062b15cb3dSCy Schubert  * The version with a full copyright and additional notes.
1072b15cb3dSCy Schubert  *
1082b15cb3dSCy Schubert  * @param[in] opts  the option descriptor
1092b15cb3dSCy Schubert  * @param[in] fp    the output stream
1102b15cb3dSCy Schubert  */
1112b15cb3dSCy Schubert static void
emit_copy_note(tOptions * opts,FILE * fp)1122b15cb3dSCy Schubert emit_copy_note(tOptions * opts, FILE * fp)
1132b15cb3dSCy Schubert {
1142b15cb3dSCy Schubert     if (opts->pzCopyright != NULL)
1152b15cb3dSCy Schubert         fputs(opts->pzCopyright, fp);
1162b15cb3dSCy Schubert 
1172b15cb3dSCy Schubert     if (opts->pzCopyNotice != NULL)
1182b15cb3dSCy Schubert         fputs(opts->pzCopyNotice, fp);
1192b15cb3dSCy Schubert 
1202b15cb3dSCy Schubert     fputc(NL, fp);
1212b15cb3dSCy Schubert     fprintf(fp, zao_ver_fmt, optionVersion());
1222b15cb3dSCy Schubert 
1232b15cb3dSCy Schubert     if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
1242b15cb3dSCy Schubert         fputc(NL, fp);
1252b15cb3dSCy Schubert         fputs(opts->pzPackager, fp);
1262b15cb3dSCy Schubert 
1272b15cb3dSCy Schubert     } else if (opts->pzBugAddr != NULL) {
1282b15cb3dSCy Schubert         fputc(NL, fp);
1292b15cb3dSCy Schubert         fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
1302b15cb3dSCy Schubert     }
1312b15cb3dSCy Schubert }
1322b15cb3dSCy Schubert 
1332b15cb3dSCy Schubert /**
1342b15cb3dSCy Schubert  * Handle the version printing.  We must see how much information
1352b15cb3dSCy Schubert  * is being requested and select the correct printing routine.
1362b15cb3dSCy Schubert  */
1372b15cb3dSCy Schubert static void
print_ver(tOptions * opts,tOptDesc * od,FILE * fp,bool call_exit)1382b15cb3dSCy Schubert print_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
1392b15cb3dSCy Schubert {
1402b15cb3dSCy Schubert     char ch;
1412b15cb3dSCy Schubert 
1422b15cb3dSCy Schubert     if (opts <= OPTPROC_EMIT_LIMIT)
1432b15cb3dSCy Schubert         return;
144ea906c41SOllivier Robert 
145ea906c41SOllivier Robert     /*
1462b15cb3dSCy Schubert      *  IF we have an argument for this option, use it
1472b15cb3dSCy Schubert      *  Otherwise, default to version only or copyright note,
1482b15cb3dSCy Schubert      *  depending on whether the layout is GNU standard form or not.
149ea906c41SOllivier Robert      */
1502b15cb3dSCy Schubert     if (  (od->fOptState & OPTST_ARG_OPTIONAL)
1512b15cb3dSCy Schubert        && (od->optArg.argString != NULL)
1522b15cb3dSCy Schubert        && (od->optArg.argString[0] != NUL))
153ea906c41SOllivier Robert 
1542b15cb3dSCy Schubert         ch = od->optArg.argString[0];
155ea906c41SOllivier Robert 
156*a466cc55SCy Schubert     else if (OPTST_GET_ARGTYPE(od->fOptState) == OPARG_TYPE_STATIC) {
157*a466cc55SCy Schubert         ch = od->optArg.argString[0];
158*a466cc55SCy Schubert 
159*a466cc55SCy Schubert     } else {
1602b15cb3dSCy Schubert         set_usage_flags(opts, NULL);
1612b15cb3dSCy Schubert         ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
162ea906c41SOllivier Robert     }
163ea906c41SOllivier Robert 
1642b15cb3dSCy Schubert     switch (ch) {
165ea906c41SOllivier Robert     case NUL: /* arg provided, but empty */
1662b15cb3dSCy Schubert     case 'v': case 'V': emit_simple_ver(opts, fp); break;
1672b15cb3dSCy Schubert     case 'c': case 'C': emit_copy_full( opts, fp); break;
1682b15cb3dSCy Schubert     case 'n': case 'N': emit_copy_note( opts, fp); break;
169ea906c41SOllivier Robert 
170ea906c41SOllivier Robert     default:
1712b15cb3dSCy Schubert         fprintf(stderr, zBadVerArg, ch);
1722b15cb3dSCy Schubert         option_exits(EXIT_FAILURE);
173ea906c41SOllivier Robert     }
174ea906c41SOllivier Robert 
1752b15cb3dSCy Schubert     fflush(fp);
1762b15cb3dSCy Schubert     if (ferror(fp))
1772b15cb3dSCy Schubert         fserr_exit(opts->pzProgName, zwriting,
1782b15cb3dSCy Schubert                    (fp == stdout) ? zstdout_name : zstderr_name);
1792b15cb3dSCy Schubert 
1802b15cb3dSCy Schubert     if (call_exit)
1812b15cb3dSCy Schubert         option_exits(EXIT_SUCCESS);
182ea906c41SOllivier Robert }
183ea906c41SOllivier Robert 
184ea906c41SOllivier Robert /*=export_func  optionPrintVersion
185ea906c41SOllivier Robert  *
186ea906c41SOllivier Robert  * what:  Print the program version
1872b15cb3dSCy Schubert  * arg:   + tOptions * + opts + program options descriptor +
1882b15cb3dSCy Schubert  * arg:   + tOptDesc * + od   + the descriptor for this arg +
189ea906c41SOllivier Robert  *
190ea906c41SOllivier Robert  * doc:
191ea906c41SOllivier Robert  *  This routine will print the version to stdout.
192ea906c41SOllivier Robert =*/
193ea906c41SOllivier Robert void
optionPrintVersion(tOptions * opts,tOptDesc * od)1942b15cb3dSCy Schubert optionPrintVersion(tOptions * opts, tOptDesc * od)
195ea906c41SOllivier Robert {
1962b15cb3dSCy Schubert     print_ver(opts, od, print_exit ? stderr : stdout, true);
1972b15cb3dSCy Schubert }
1982b15cb3dSCy Schubert 
1992b15cb3dSCy Schubert /*=export_func  optionPrintVersionAndReturn
2002b15cb3dSCy Schubert  *
2012b15cb3dSCy Schubert  * what:  Print the program version
2022b15cb3dSCy Schubert  * arg:   + tOptions * + opts + program options descriptor +
2032b15cb3dSCy Schubert  * arg:   + tOptDesc * + od   + the descriptor for this arg +
2042b15cb3dSCy Schubert  *
2052b15cb3dSCy Schubert  * doc:
2062b15cb3dSCy Schubert  *  This routine will print the version to stdout and return
2072b15cb3dSCy Schubert  *  instead of exiting.  Please see the source for the
2082b15cb3dSCy Schubert  *  @code{print_ver} funtion for details on selecting how
2092b15cb3dSCy Schubert  *  verbose to be after this function returns.
2102b15cb3dSCy Schubert =*/
2112b15cb3dSCy Schubert void
optionPrintVersionAndReturn(tOptions * opts,tOptDesc * od)2122b15cb3dSCy Schubert optionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
2132b15cb3dSCy Schubert {
2142b15cb3dSCy Schubert     print_ver(opts, od, print_exit ? stderr : stdout, false);
215ea906c41SOllivier Robert }
216ea906c41SOllivier Robert 
217ea906c41SOllivier Robert /*=export_func  optionVersionStderr
218ea906c41SOllivier Robert  * private:
219ea906c41SOllivier Robert  *
220ea906c41SOllivier Robert  * what:  Print the program version to stderr
2212b15cb3dSCy Schubert  * arg:   + tOptions * + opts + program options descriptor +
2222b15cb3dSCy Schubert  * arg:   + tOptDesc * + od   + the descriptor for this arg +
223ea906c41SOllivier Robert  *
224ea906c41SOllivier Robert  * doc:
225ea906c41SOllivier Robert  *  This routine will print the version to stderr.
226ea906c41SOllivier Robert =*/
227ea906c41SOllivier Robert void
optionVersionStderr(tOptions * opts,tOptDesc * od)2282b15cb3dSCy Schubert optionVersionStderr(tOptions * opts, tOptDesc * od)
229ea906c41SOllivier Robert {
2302b15cb3dSCy Schubert     print_ver(opts, od, stderr, true);
231ea906c41SOllivier Robert }
232ea906c41SOllivier Robert 
2332b15cb3dSCy Schubert /** @}
2342b15cb3dSCy Schubert  *
235ea906c41SOllivier Robert  * Local Variables:
236ea906c41SOllivier Robert  * mode: C
237ea906c41SOllivier Robert  * c-file-style: "stroustrup"
238ea906c41SOllivier Robert  * indent-tabs-mode: nil
239ea906c41SOllivier Robert  * End:
240ea906c41SOllivier Robert  * end of autoopts/version.c */
241