xref: /netbsd-src/external/bsd/ntp/dist/sntp/libopts/version.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: version.c,v 1.1.1.2 2012/01/31 21:27:46 kardel Exp $	*/
2 
3 
4 /*
5  * Time-stamp:      "2011-04-22 12:54:28 bkorb"
6  *
7  *  This module implements the default usage procedure for
8  *  Automated Options.  It may be overridden, of course.
9  */
10 
11 /*
12  *  This file is part of AutoOpts, a companion to AutoGen.
13  *  AutoOpts is free software.
14  *  AutoOpts is Copyright (c) 1992-2011 by Bruce Korb - all rights reserved
15  *
16  *  AutoOpts is available under any one of two licenses.  The license
17  *  in use must be one of these two and the choice is under the control
18  *  of the user of the license.
19  *
20  *   The GNU Lesser General Public License, version 3 or later
21  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
22  *
23  *   The Modified Berkeley Software Distribution License
24  *      See the file "COPYING.mbsd"
25  *
26  *  These files have the following md5sums:
27  *
28  *  43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
29  *  06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
30  *  66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
31  */
32 
33 /*=export_func  optionVersion
34  *
35  * what:     return the compiled AutoOpts version number
36  * ret_type: char const*
37  * ret_desc: the version string in constant memory
38  * doc:
39  *  Returns the full version string compiled into the library.
40  *  The returned string cannot be modified.
41 =*/
42 char const*
43 optionVersion(void)
44 {
45     static char const zVersion[] =
46         STR(AO_CURRENT.AO_REVISION);
47 
48     return zVersion;
49 }
50 
51 static void
52 emit_simple_ver(tOptions * pOpts, FILE * fp)
53 {
54     if (pOpts->pzFullVersion != NULL)
55         fputs(pOpts->pzFullVersion, fp);
56 
57     else if (pOpts->pzCopyright != NULL) {
58         char const * pe = strchr(pOpts->pzCopyright, '\n');
59         if (pe == NULL)
60             pe = pOpts->pzCopyright + strlen(pOpts->pzCopyright);
61         fwrite(pOpts->pzCopyright, 1, pe - pOpts->pzCopyright, fp);
62     }
63 
64     else {
65         char const * pe = strchr(pOpts->pzUsageTitle, '\n');
66         if (pe == NULL)
67             pe = pOpts->pzUsageTitle + strlen(pOpts->pzUsageTitle);
68         fwrite(pOpts->pzUsageTitle, 1, pe - pOpts->pzCopyright, fp);
69     }
70     fputc('\n', fp);
71 }
72 
73 static void
74 emit_copy_ver(tOptions * pOpts, FILE * fp)
75 {
76     if (pOpts->pzCopyright != NULL)
77         fputs(pOpts->pzCopyright, fp);
78 
79     else if (pOpts->pzFullVersion != NULL)
80         fputs(pOpts->pzFullVersion, fp);
81 
82     else {
83         char const * pe = strchr(pOpts->pzUsageTitle, '\n');
84         if (pe == NULL)
85             pe = pOpts->pzUsageTitle + strlen(pOpts->pzUsageTitle);
86         fwrite(pOpts->pzUsageTitle, 1, pe - pOpts->pzCopyright, fp);
87     }
88 
89     fputc('\n', fp);
90 
91     if (HAS_pzPkgDataDir(pOpts) && (pOpts->pzPackager != NULL))
92         fputs(pOpts->pzPackager, fp);
93 
94     else if (pOpts->pzBugAddr != NULL)
95         fprintf(fp, zPlsSendBugs, pOpts->pzBugAddr);
96 }
97 
98 static void
99 emit_copy_note(tOptions * pOpts, FILE * fp)
100 {
101     if (pOpts->pzCopyright != NULL) {
102         fputs(pOpts->pzCopyright, fp);
103         fputc('\n', fp);
104     }
105 
106     if (pOpts->pzCopyNotice != NULL) {
107         fputs(pOpts->pzCopyNotice, fp);
108         fputc('\n', fp);
109     }
110 
111     fprintf(fp, zAO_Ver, optionVersion());
112 
113     if (HAS_pzPkgDataDir(pOpts) && (pOpts->pzPackager != NULL))
114         fputs(pOpts->pzPackager, fp);
115 
116     else if (pOpts->pzBugAddr != NULL)
117         fprintf(fp, zPlsSendBugs, pOpts->pzBugAddr);
118 }
119 
120 static void
121 print_ver(tOptions * pOpts, tOptDesc * pOD, FILE * fp)
122 {
123     char ch;
124 
125     /*
126      *  IF the optional argument flag is off, or the argument
127      *  is not provided, then just print the version.
128      */
129     if (  ((pOD->fOptState & OPTST_ARG_OPTIONAL) == 0)
130        || (pOD->optArg.argString == NULL))
131          ch = 'v';
132     else ch = pOD->optArg.argString[0];
133 
134     switch (ch) {
135     case NUL: /* arg provided, but empty */
136     case 'v': case 'V': emit_simple_ver(pOpts, fp); break;
137     case 'c': case 'C': emit_copy_ver(pOpts, fp);   break;
138     case 'n': case 'N': emit_copy_note(pOpts, fp);  break;
139 
140     default:
141         fprintf(stderr, zBadVerArg, ch);
142         exit(EXIT_FAILURE);
143     }
144 
145     fflush(fp);
146     if (ferror(fp) != 0) {
147         fputs(zOutputFail, stderr);
148         exit(EXIT_FAILURE);
149     }
150     exit(EXIT_SUCCESS);
151 }
152 
153 /*=export_func  optionPrintVersion
154  * private:
155  *
156  * what:  Print the program version
157  * arg:   + tOptions* + pOpts    + program options descriptor +
158  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
159  *
160  * doc:
161  *  This routine will print the version to stdout.
162 =*/
163 void
164 optionPrintVersion(tOptions * pOpts, tOptDesc * pOD)
165 {
166     print_ver(pOpts, pOD, stdout);
167 }
168 
169 /*=export_func  optionVersionStderr
170  * private:
171  *
172  * what:  Print the program version to stderr
173  * arg:   + tOptions* + pOpts    + program options descriptor +
174  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
175  *
176  * doc:
177  *  This routine will print the version to stderr.
178 =*/
179 void
180 optionVersionStderr(tOptions * pOpts, tOptDesc * pOD)
181 {
182     print_ver(pOpts, pOD, stderr);
183 }
184 
185 /*
186  * Local Variables:
187  * mode: C
188  * c-file-style: "stroustrup"
189  * indent-tabs-mode: nil
190  * End:
191  * end of autoopts/version.c */
192