xref: /netbsd/external/bsd/ntp/dist/sntp/libopts/version.c (revision 9034ec65)
1*9034ec65Schristos /*	$NetBSD: version.c,v 1.5 2020/05/25 20:47:35 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel 
42b3787f6Schristos /** \file version.c
5abb0f93cSkardel  *
6abb0f93cSkardel  *  This module implements the default usage procedure for
7abb0f93cSkardel  *  Automated Options.  It may be overridden, of course.
82b3787f6Schristos  *
92b3787f6Schristos  * @addtogroup autoopts
102b3787f6Schristos  * @{
11abb0f93cSkardel  */
12abb0f93cSkardel /*
13abb0f93cSkardel  *  This file is part of AutoOpts, a companion to AutoGen.
14abb0f93cSkardel  *  AutoOpts is free software.
154e3b3909Schristos  *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
16abb0f93cSkardel  *
17abb0f93cSkardel  *  AutoOpts is available under any one of two licenses.  The license
18abb0f93cSkardel  *  in use must be one of these two and the choice is under the control
19abb0f93cSkardel  *  of the user of the license.
20abb0f93cSkardel  *
21abb0f93cSkardel  *   The GNU Lesser General Public License, version 3 or later
22abb0f93cSkardel  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
23abb0f93cSkardel  *
24abb0f93cSkardel  *   The Modified Berkeley Software Distribution License
25abb0f93cSkardel  *      See the file "COPYING.mbsd"
26abb0f93cSkardel  *
272b3787f6Schristos  *  These files have the following sha256 sums:
28abb0f93cSkardel  *
292b3787f6Schristos  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
302b3787f6Schristos  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
312b3787f6Schristos  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
32abb0f93cSkardel  */
33abb0f93cSkardel 
34abb0f93cSkardel /*=export_func  optionVersion
35abb0f93cSkardel  *
36abb0f93cSkardel  * what:     return the compiled AutoOpts version number
37abb0f93cSkardel  * ret_type: char const *
38abb0f93cSkardel  * ret_desc: the version string in constant memory
39abb0f93cSkardel  * doc:
40abb0f93cSkardel  *  Returns the full version string compiled into the library.
41abb0f93cSkardel  *  The returned string cannot be modified.
42abb0f93cSkardel =*/
43abb0f93cSkardel char const *
optionVersion(void)44abb0f93cSkardel optionVersion(void)
45abb0f93cSkardel {
462b3787f6Schristos     static char const ver[] = OPTIONS_DOTTED_VERSION;
472b3787f6Schristos     return ver;
48abb0f93cSkardel }
49abb0f93cSkardel 
504305584aSkardel static void
emit_first_line(FILE * fp,char const * alt1,char const * alt2,char const * alt3)512b3787f6Schristos emit_first_line(
522b3787f6Schristos     FILE * fp, char const * alt1, char const * alt2, char const * alt3)
534305584aSkardel {
542b3787f6Schristos     char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
552b3787f6Schristos     char const * e;
562b3787f6Schristos     if (p == NULL)
572b3787f6Schristos         return;
582b3787f6Schristos     e = strchr(p, NL);
592b3787f6Schristos     if (e == NULL)
602b3787f6Schristos         fputs(p, fp);
612b3787f6Schristos     else
622b3787f6Schristos         fwrite(p, 1, (e - p), fp);
632b3787f6Schristos     fputc(NL, fp);
644305584aSkardel }
654305584aSkardel 
662b3787f6Schristos /**
672b3787f6Schristos  * Select among various ways to emit version information.
682b3787f6Schristos  *
692b3787f6Schristos  * @param[in] o   the option descriptor
702b3787f6Schristos  * @param[in] fp  the output stream
712b3787f6Schristos  */
72abb0f93cSkardel static void
emit_simple_ver(tOptions * o,FILE * fp)732b3787f6Schristos emit_simple_ver(tOptions * o, FILE * fp)
74abb0f93cSkardel {
752b3787f6Schristos     emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
76abb0f93cSkardel }
77abb0f93cSkardel 
782b3787f6Schristos /**
792b3787f6Schristos  * print the version with a copyright notice.
802b3787f6Schristos  *
812b3787f6Schristos  * @param[in] o   the option descriptor
822b3787f6Schristos  * @param[in] fp  the output stream
832b3787f6Schristos  */
844305584aSkardel static void
emit_copy_full(tOptions * o,FILE * fp)852b3787f6Schristos emit_copy_full(tOptions * o, FILE * fp)
864305584aSkardel {
872b3787f6Schristos     if (o->pzCopyright != NULL)
882b3787f6Schristos         fputs(o->pzCopyright, fp);
892b3787f6Schristos 
902b3787f6Schristos     else if (o->pzFullVersion != NULL)
912b3787f6Schristos         fputs(o->pzFullVersion, fp);
922b3787f6Schristos 
932b3787f6Schristos     else
942b3787f6Schristos         emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
952b3787f6Schristos 
962b3787f6Schristos     if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
972b3787f6Schristos         fputc(NL, fp);
982b3787f6Schristos         fputs(o->pzPackager, fp);
992b3787f6Schristos 
1002b3787f6Schristos     } else if (o->pzBugAddr != NULL) {
1012b3787f6Schristos         fputc(NL, fp);
1022b3787f6Schristos         fprintf(fp, zPlsSendBugs, o->pzBugAddr);
1032b3787f6Schristos     }
104abb0f93cSkardel }
105abb0f93cSkardel 
1062b3787f6Schristos /**
1072b3787f6Schristos  * print the version and any copyright notice.
1082b3787f6Schristos  * The version with a full copyright and additional notes.
1092b3787f6Schristos  *
1102b3787f6Schristos  * @param[in] opts  the option descriptor
1112b3787f6Schristos  * @param[in] fp    the output stream
1122b3787f6Schristos  */
1134305584aSkardel static void
emit_copy_note(tOptions * opts,FILE * fp)1142b3787f6Schristos emit_copy_note(tOptions * opts, FILE * fp)
1152b3787f6Schristos {
1162b3787f6Schristos     if (opts->pzCopyright != NULL)
1172b3787f6Schristos         fputs(opts->pzCopyright, fp);
1182b3787f6Schristos 
1192b3787f6Schristos     if (opts->pzCopyNotice != NULL)
1202b3787f6Schristos         fputs(opts->pzCopyNotice, fp);
1212b3787f6Schristos 
1222b3787f6Schristos     fputc(NL, fp);
1232b3787f6Schristos     fprintf(fp, zao_ver_fmt, optionVersion());
1242b3787f6Schristos 
1252b3787f6Schristos     if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
1262b3787f6Schristos         fputc(NL, fp);
1272b3787f6Schristos         fputs(opts->pzPackager, fp);
1282b3787f6Schristos 
1292b3787f6Schristos     } else if (opts->pzBugAddr != NULL) {
1302b3787f6Schristos         fputc(NL, fp);
1312b3787f6Schristos         fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
1322b3787f6Schristos     }
1332b3787f6Schristos }
1342b3787f6Schristos 
1352b3787f6Schristos /**
1362b3787f6Schristos  * Handle the version printing.  We must see how much information
1372b3787f6Schristos  * is being requested and select the correct printing routine.
1382b3787f6Schristos  */
1392b3787f6Schristos static void
print_ver(tOptions * opts,tOptDesc * od,FILE * fp,bool call_exit)1401b6f2cd4Schristos print_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
1414305584aSkardel {
1424305584aSkardel     char ch;
1434305584aSkardel 
1442b3787f6Schristos     if (opts <= OPTPROC_EMIT_LIMIT)
1452b3787f6Schristos         return;
1462b3787f6Schristos 
1474305584aSkardel     /*
1482b3787f6Schristos      *  IF we have an argument for this option, use it
1492b3787f6Schristos      *  Otherwise, default to version only or copyright note,
1502b3787f6Schristos      *  depending on whether the layout is GNU standard form or not.
1514305584aSkardel      */
1522b3787f6Schristos     if (  (od->fOptState & OPTST_ARG_OPTIONAL)
1532b3787f6Schristos        && (od->optArg.argString != NULL)
1542b3787f6Schristos        && (od->optArg.argString[0] != NUL))
1552b3787f6Schristos 
1562b3787f6Schristos         ch = od->optArg.argString[0];
1572b3787f6Schristos 
1582b3787f6Schristos     else {
1592b3787f6Schristos         set_usage_flags(opts, NULL);
1602b3787f6Schristos         ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
1612b3787f6Schristos     }
1624305584aSkardel 
1634305584aSkardel     switch (ch) {
1644305584aSkardel     case NUL: /* arg provided, but empty */
1652b3787f6Schristos     case 'v': case 'V': emit_simple_ver(opts, fp); break;
1662b3787f6Schristos     case 'c': case 'C': emit_copy_full( opts, fp); break;
1672b3787f6Schristos     case 'n': case 'N': emit_copy_note( opts, fp); break;
168abb0f93cSkardel 
169abb0f93cSkardel     default:
1704305584aSkardel         fprintf(stderr, zBadVerArg, ch);
1712b3787f6Schristos         option_exits(EXIT_FAILURE);
172abb0f93cSkardel     }
173abb0f93cSkardel 
1744305584aSkardel     fflush(fp);
1752b3787f6Schristos     if (ferror(fp))
1762b3787f6Schristos         fserr_exit(opts->pzProgName, zwriting,
1772b3787f6Schristos                    (fp == stdout) ? zstdout_name : zstderr_name);
1782b3787f6Schristos 
1791b6f2cd4Schristos     if (call_exit)
1802b3787f6Schristos         option_exits(EXIT_SUCCESS);
181abb0f93cSkardel }
182abb0f93cSkardel 
183abb0f93cSkardel /*=export_func  optionPrintVersion
184abb0f93cSkardel  *
185abb0f93cSkardel  * what:  Print the program version
1862b3787f6Schristos  * arg:   + tOptions * + opts + program options descriptor +
1872b3787f6Schristos  * arg:   + tOptDesc * + od   + the descriptor for this arg +
188abb0f93cSkardel  *
189abb0f93cSkardel  * doc:
190abb0f93cSkardel  *  This routine will print the version to stdout.
191abb0f93cSkardel =*/
192abb0f93cSkardel void
optionPrintVersion(tOptions * opts,tOptDesc * od)1932b3787f6Schristos optionPrintVersion(tOptions * opts, tOptDesc * od)
194abb0f93cSkardel {
1951b6f2cd4Schristos     print_ver(opts, od, print_exit ? stderr : stdout, true);
1961b6f2cd4Schristos }
1971b6f2cd4Schristos 
1981b6f2cd4Schristos /*=export_func  optionPrintVersionAndReturn
1991b6f2cd4Schristos  *
2001b6f2cd4Schristos  * what:  Print the program version
2011b6f2cd4Schristos  * arg:   + tOptions * + opts + program options descriptor +
2021b6f2cd4Schristos  * arg:   + tOptDesc * + od   + the descriptor for this arg +
2031b6f2cd4Schristos  *
2041b6f2cd4Schristos  * doc:
2051b6f2cd4Schristos  *  This routine will print the version to stdout and return
2061b6f2cd4Schristos  *  instead of exiting.  Please see the source for the
2071b6f2cd4Schristos  *  @code{print_ver} funtion for details on selecting how
2081b6f2cd4Schristos  *  verbose to be after this function returns.
2091b6f2cd4Schristos =*/
2101b6f2cd4Schristos void
optionPrintVersionAndReturn(tOptions * opts,tOptDesc * od)2111b6f2cd4Schristos optionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
2121b6f2cd4Schristos {
2131b6f2cd4Schristos     print_ver(opts, od, print_exit ? stderr : stdout, false);
214abb0f93cSkardel }
215abb0f93cSkardel 
216abb0f93cSkardel /*=export_func  optionVersionStderr
217abb0f93cSkardel  * private:
218abb0f93cSkardel  *
219abb0f93cSkardel  * what:  Print the program version to stderr
2202b3787f6Schristos  * arg:   + tOptions * + opts + program options descriptor +
2212b3787f6Schristos  * arg:   + tOptDesc * + od   + the descriptor for this arg +
222abb0f93cSkardel  *
223abb0f93cSkardel  * doc:
224abb0f93cSkardel  *  This routine will print the version to stderr.
225abb0f93cSkardel =*/
226abb0f93cSkardel void
optionVersionStderr(tOptions * opts,tOptDesc * od)2272b3787f6Schristos optionVersionStderr(tOptions * opts, tOptDesc * od)
228abb0f93cSkardel {
2291b6f2cd4Schristos     print_ver(opts, od, stderr, true);
230abb0f93cSkardel }
231abb0f93cSkardel 
2322b3787f6Schristos /** @}
2332b3787f6Schristos  *
234abb0f93cSkardel  * Local Variables:
235abb0f93cSkardel  * mode: C
236abb0f93cSkardel  * c-file-style: "stroustrup"
237abb0f93cSkardel  * indent-tabs-mode: nil
238abb0f93cSkardel  * End:
239abb0f93cSkardel  * end of autoopts/version.c */
240