xref: /freebsd/contrib/ntp/sntp/libopts/version.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
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-2015 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 {
157         set_usage_flags(opts, NULL);
158         ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
159     }
160 
161     switch (ch) {
162     case NUL: /* arg provided, but empty */
163     case 'v': case 'V': emit_simple_ver(opts, fp); break;
164     case 'c': case 'C': emit_copy_full( opts, fp); break;
165     case 'n': case 'N': emit_copy_note( opts, fp); break;
166 
167     default:
168         fprintf(stderr, zBadVerArg, ch);
169         option_exits(EXIT_FAILURE);
170     }
171 
172     fflush(fp);
173     if (ferror(fp))
174         fserr_exit(opts->pzProgName, zwriting,
175                    (fp == stdout) ? zstdout_name : zstderr_name);
176 
177     if (call_exit)
178         option_exits(EXIT_SUCCESS);
179 }
180 
181 /*=export_func  optionPrintVersion
182  *
183  * what:  Print the program version
184  * arg:   + tOptions * + opts + program options descriptor +
185  * arg:   + tOptDesc * + od   + the descriptor for this arg +
186  *
187  * doc:
188  *  This routine will print the version to stdout.
189 =*/
190 void
191 optionPrintVersion(tOptions * opts, tOptDesc * od)
192 {
193     print_ver(opts, od, print_exit ? stderr : stdout, true);
194 }
195 
196 /*=export_func  optionPrintVersionAndReturn
197  *
198  * what:  Print the program version
199  * arg:   + tOptions * + opts + program options descriptor +
200  * arg:   + tOptDesc * + od   + the descriptor for this arg +
201  *
202  * doc:
203  *  This routine will print the version to stdout and return
204  *  instead of exiting.  Please see the source for the
205  *  @code{print_ver} funtion for details on selecting how
206  *  verbose to be after this function returns.
207 =*/
208 void
209 optionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
210 {
211     print_ver(opts, od, print_exit ? stderr : stdout, false);
212 }
213 
214 /*=export_func  optionVersionStderr
215  * private:
216  *
217  * what:  Print the program version to stderr
218  * arg:   + tOptions * + opts + program options descriptor +
219  * arg:   + tOptDesc * + od   + the descriptor for this arg +
220  *
221  * doc:
222  *  This routine will print the version to stderr.
223 =*/
224 void
225 optionVersionStderr(tOptions * opts, tOptDesc * od)
226 {
227     print_ver(opts, od, stderr, true);
228 }
229 
230 /** @}
231  *
232  * Local Variables:
233  * mode: C
234  * c-file-style: "stroustrup"
235  * indent-tabs-mode: nil
236  * End:
237  * end of autoopts/version.c */
238