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