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