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