xref: /freebsd/contrib/ntp/sntp/libopts/version.c (revision ea906c4152774dff300bb26fbfc1e4188351c89a)
1ea906c41SOllivier Robert 
2ea906c41SOllivier Robert /*  $Id: version.c,v 4.10 2007/04/28 22:19:23 bkorb Exp $
3ea906c41SOllivier Robert  * Time-stamp:      "2007-04-28 10:08:34 bkorb"
4ea906c41SOllivier Robert  *
5ea906c41SOllivier Robert  *  This module implements the default usage procedure for
6ea906c41SOllivier Robert  *  Automated Options.  It may be overridden, of course.
7ea906c41SOllivier Robert  */
8ea906c41SOllivier Robert 
9ea906c41SOllivier Robert static char const zAOV[] =
10ea906c41SOllivier Robert     "Automated Options version %s, copyright (c) 1999-2007 Bruce Korb\n";
11ea906c41SOllivier Robert 
12ea906c41SOllivier Robert /*  Automated Options is free software.
13ea906c41SOllivier Robert  *  You may redistribute it and/or modify it under the terms of the
14ea906c41SOllivier Robert  *  GNU General Public License, as published by the Free Software
15ea906c41SOllivier Robert  *  Foundation; either version 2, or (at your option) any later version.
16ea906c41SOllivier Robert  *
17ea906c41SOllivier Robert  *  Automated Options is distributed in the hope that it will be useful,
18ea906c41SOllivier Robert  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19ea906c41SOllivier Robert  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20ea906c41SOllivier Robert  *  GNU General Public License for more details.
21ea906c41SOllivier Robert  *
22ea906c41SOllivier Robert  *  You should have received a copy of the GNU General Public License
23ea906c41SOllivier Robert  *  along with Automated Options.  See the file "COPYING".  If not,
24ea906c41SOllivier Robert  *  write to:  The Free Software Foundation, Inc.,
25ea906c41SOllivier Robert  *             51 Franklin Street, Fifth Floor,
26ea906c41SOllivier Robert  *             Boston, MA  02110-1301, USA.
27ea906c41SOllivier Robert  *
28ea906c41SOllivier Robert  * As a special exception, Bruce Korb gives permission for additional
29ea906c41SOllivier Robert  * uses of the text contained in his release of AutoOpts.
30ea906c41SOllivier Robert  *
31ea906c41SOllivier Robert  * The exception is that, if you link the AutoOpts library with other
32ea906c41SOllivier Robert  * files to produce an executable, this does not by itself cause the
33ea906c41SOllivier Robert  * resulting executable to be covered by the GNU General Public License.
34ea906c41SOllivier Robert  * Your use of that executable is in no way restricted on account of
35ea906c41SOllivier Robert  * linking the AutoOpts library code into it.
36ea906c41SOllivier Robert  *
37ea906c41SOllivier Robert  * This exception does not however invalidate any other reasons why
38ea906c41SOllivier Robert  * the executable file might be covered by the GNU General Public License.
39ea906c41SOllivier Robert  *
40ea906c41SOllivier Robert  * This exception applies only to the code released by Bruce Korb under
41ea906c41SOllivier Robert  * the name AutoOpts.  If you copy code from other sources under the
42ea906c41SOllivier Robert  * General Public License into a copy of AutoOpts, as the General Public
43ea906c41SOllivier Robert  * License permits, the exception does not apply to the code that you add
44ea906c41SOllivier Robert  * in this way.  To avoid misleading anyone as to the status of such
45ea906c41SOllivier Robert  * modified files, you must delete this exception notice from them.
46ea906c41SOllivier Robert  *
47ea906c41SOllivier Robert  * If you write modifications of your own for AutoOpts, it is your choice
48ea906c41SOllivier Robert  * whether to permit this exception to apply to your modifications.
49ea906c41SOllivier Robert  * If you do not wish that, delete this exception notice.
50ea906c41SOllivier Robert  */
51ea906c41SOllivier Robert 
52ea906c41SOllivier Robert /* = = = START-STATIC-FORWARD = = = */
53ea906c41SOllivier Robert /* static forward declarations maintained by :mkfwd */
54ea906c41SOllivier Robert static void
55ea906c41SOllivier Robert printVersion( tOptions* pOpts, tOptDesc* pOD, FILE* fp );
56ea906c41SOllivier Robert /* = = = END-STATIC-FORWARD = = = */
57ea906c41SOllivier Robert 
58ea906c41SOllivier Robert /*=export_func  optionVersion
59ea906c41SOllivier Robert  *
60ea906c41SOllivier Robert  * what:     return the compiled AutoOpts version number
61ea906c41SOllivier Robert  * ret_type: char const*
62ea906c41SOllivier Robert  * ret_desc: the version string in constant memory
63ea906c41SOllivier Robert  * doc:
64ea906c41SOllivier Robert  *  Returns the full version string compiled into the library.
65ea906c41SOllivier Robert  *  The returned string cannot be modified.
66ea906c41SOllivier Robert =*/
67ea906c41SOllivier Robert char const*
68ea906c41SOllivier Robert optionVersion( void )
69ea906c41SOllivier Robert {
70ea906c41SOllivier Robert     static char const zVersion[] =
71ea906c41SOllivier Robert         STR( AO_CURRENT.AO_REVISION );
72ea906c41SOllivier Robert 
73ea906c41SOllivier Robert     return zVersion;
74ea906c41SOllivier Robert }
75ea906c41SOllivier Robert 
76ea906c41SOllivier Robert 
77ea906c41SOllivier Robert static void
78ea906c41SOllivier Robert printVersion( tOptions* pOpts, tOptDesc* pOD, FILE* fp )
79ea906c41SOllivier Robert {
80ea906c41SOllivier Robert     char swCh;
81ea906c41SOllivier Robert 
82ea906c41SOllivier Robert     /*
83ea906c41SOllivier Robert      *  IF the optional argument flag is off, or the argument is not provided,
84ea906c41SOllivier Robert      *  then just print the version.
85ea906c41SOllivier Robert      */
86ea906c41SOllivier Robert     if (  ((pOD->fOptState & OPTST_ARG_OPTIONAL) == 0)
87ea906c41SOllivier Robert        || (pOD->optArg.argString == NULL))
88ea906c41SOllivier Robert          swCh = 'v';
89ea906c41SOllivier Robert     else swCh = tolower(pOD->optArg.argString[0]);
90ea906c41SOllivier Robert 
91ea906c41SOllivier Robert     if (pOpts->pzFullVersion != NULL) {
92ea906c41SOllivier Robert         fputs( pOpts->pzFullVersion, fp );
93ea906c41SOllivier Robert         fputc( '\n', fp );
94ea906c41SOllivier Robert 
95ea906c41SOllivier Robert     } else {
96ea906c41SOllivier Robert         char const *pz = pOpts->pzUsageTitle;
97ea906c41SOllivier Robert         do { fputc(*pz, fp); } while (*(pz++) != '\n');
98ea906c41SOllivier Robert     }
99ea906c41SOllivier Robert 
100ea906c41SOllivier Robert     switch (swCh) {
101ea906c41SOllivier Robert     case NUL: /* arg provided, but empty */
102ea906c41SOllivier Robert     case 'v':
103ea906c41SOllivier Robert         break;
104ea906c41SOllivier Robert 
105ea906c41SOllivier Robert     case 'c':
106ea906c41SOllivier Robert         if (pOpts->pzCopyright != NULL) {
107ea906c41SOllivier Robert             fputs( pOpts->pzCopyright, fp );
108ea906c41SOllivier Robert             fputc( '\n', fp );
109ea906c41SOllivier Robert         }
110ea906c41SOllivier Robert         fprintf( fp, zAOV, optionVersion() );
111ea906c41SOllivier Robert         if (pOpts->pzBugAddr != NULL)
112ea906c41SOllivier Robert             fprintf( fp, zPlsSendBugs, pOpts->pzBugAddr );
113ea906c41SOllivier Robert         break;
114ea906c41SOllivier Robert 
115ea906c41SOllivier Robert     case 'n':
116ea906c41SOllivier Robert         if (pOpts->pzCopyright != NULL) {
117ea906c41SOllivier Robert             fputs( pOpts->pzCopyright, fp );
118ea906c41SOllivier Robert             fputc( '\n', fp );
119ea906c41SOllivier Robert             fputc( '\n', fp );
120ea906c41SOllivier Robert         }
121ea906c41SOllivier Robert 
122ea906c41SOllivier Robert         if (pOpts->pzCopyNotice != NULL) {
123ea906c41SOllivier Robert             fputs( pOpts->pzCopyNotice, fp );
124ea906c41SOllivier Robert             fputc( '\n', fp );
125ea906c41SOllivier Robert         }
126ea906c41SOllivier Robert 
127ea906c41SOllivier Robert         fprintf( fp, zAOV, optionVersion() );
128ea906c41SOllivier Robert         if (pOpts->pzBugAddr != NULL)
129ea906c41SOllivier Robert             fprintf( fp, zPlsSendBugs, pOpts->pzBugAddr );
130ea906c41SOllivier Robert         break;
131ea906c41SOllivier Robert 
132ea906c41SOllivier Robert     default:
133ea906c41SOllivier Robert         fprintf( stderr, zBadVerArg, swCh );
134ea906c41SOllivier Robert         exit( EXIT_FAILURE );
135ea906c41SOllivier Robert     }
136ea906c41SOllivier Robert 
137ea906c41SOllivier Robert     exit( EXIT_SUCCESS );
138ea906c41SOllivier Robert }
139ea906c41SOllivier Robert 
140ea906c41SOllivier Robert /*=export_func  optionPrintVersion
141ea906c41SOllivier Robert  * private:
142ea906c41SOllivier Robert  *
143ea906c41SOllivier Robert  * what:  Print the program version
144ea906c41SOllivier Robert  * arg:   + tOptions* + pOpts    + program options descriptor +
145ea906c41SOllivier Robert  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
146ea906c41SOllivier Robert  *
147ea906c41SOllivier Robert  * doc:
148ea906c41SOllivier Robert  *  This routine will print the version to stdout.
149ea906c41SOllivier Robert =*/
150ea906c41SOllivier Robert void
151ea906c41SOllivier Robert optionPrintVersion( tOptions*  pOpts, tOptDesc*  pOD )
152ea906c41SOllivier Robert {
153ea906c41SOllivier Robert     printVersion( pOpts, pOD, stdout );
154ea906c41SOllivier Robert }
155ea906c41SOllivier Robert 
156ea906c41SOllivier Robert /*=export_func  optionVersionStderr
157ea906c41SOllivier Robert  * private:
158ea906c41SOllivier Robert  *
159ea906c41SOllivier Robert  * what:  Print the program version to stderr
160ea906c41SOllivier Robert  * arg:   + tOptions* + pOpts    + program options descriptor +
161ea906c41SOllivier Robert  * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
162ea906c41SOllivier Robert  *
163ea906c41SOllivier Robert  * doc:
164ea906c41SOllivier Robert  *  This routine will print the version to stderr.
165ea906c41SOllivier Robert =*/
166ea906c41SOllivier Robert void
167ea906c41SOllivier Robert optionVersionStderr( tOptions*  pOpts, tOptDesc*  pOD )
168ea906c41SOllivier Robert {
169ea906c41SOllivier Robert     printVersion( pOpts, pOD, stderr );
170ea906c41SOllivier Robert }
171ea906c41SOllivier Robert 
172ea906c41SOllivier Robert /*
173ea906c41SOllivier Robert  * Local Variables:
174ea906c41SOllivier Robert  * mode: C
175ea906c41SOllivier Robert  * c-file-style: "stroustrup"
176ea906c41SOllivier Robert  * indent-tabs-mode: nil
177ea906c41SOllivier Robert  * End:
178ea906c41SOllivier Robert  * end of autoopts/version.c */
179