1181834Sroberto
2285612Sdelphij/** \file version.c
3181834Sroberto *
4181834Sroberto *  This module implements the default usage procedure for
5181834Sroberto *  Automated Options.  It may be overridden, of course.
6285612Sdelphij *
7285612Sdelphij * @addtogroup autoopts
8285612Sdelphij * @{
9181834Sroberto */
10285612Sdelphij/*
11285612Sdelphij *  This file is part of AutoOpts, a companion to AutoGen.
12285612Sdelphij *  AutoOpts is free software.
13285612Sdelphij *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14181834Sroberto *
15285612Sdelphij *  AutoOpts is available under any one of two licenses.  The license
16285612Sdelphij *  in use must be one of these two and the choice is under the control
17285612Sdelphij *  of the user of the license.
18181834Sroberto *
19285612Sdelphij *   The GNU Lesser General Public License, version 3 or later
20285612Sdelphij *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21181834Sroberto *
22285612Sdelphij *   The Modified Berkeley Software Distribution License
23285612Sdelphij *      See the file "COPYING.mbsd"
24181834Sroberto *
25285612Sdelphij *  These files have the following sha256 sums:
26181834Sroberto *
27285612Sdelphij *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28285612Sdelphij *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29285612Sdelphij *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30181834Sroberto */
31181834Sroberto
32181834Sroberto/*=export_func  optionVersion
33181834Sroberto *
34181834Sroberto * what:     return the compiled AutoOpts version number
35285612Sdelphij * ret_type: char const *
36181834Sroberto * ret_desc: the version string in constant memory
37181834Sroberto * doc:
38181834Sroberto *  Returns the full version string compiled into the library.
39181834Sroberto *  The returned string cannot be modified.
40181834Sroberto=*/
41285612Sdelphijchar const *
42285612SdelphijoptionVersion(void)
43181834Sroberto{
44285612Sdelphij    static char const ver[] = OPTIONS_DOTTED_VERSION;
45285612Sdelphij    return ver;
46285612Sdelphij}
47181834Sroberto
48285612Sdelphijstatic void
49285612Sdelphijemit_first_line(
50285612Sdelphij    FILE * fp, char const * alt1, char const * alt2, char const * alt3)
51285612Sdelphij{
52285612Sdelphij    char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
53285612Sdelphij    char const * e;
54285612Sdelphij    if (p == NULL)
55285612Sdelphij        return;
56285612Sdelphij    e = strchr(p, NL);
57285612Sdelphij    if (e == NULL)
58285612Sdelphij        fputs(p, fp);
59285612Sdelphij    else
60285612Sdelphij        fwrite(p, 1, (e - p), fp);
61285612Sdelphij    fputc(NL, fp);
62181834Sroberto}
63181834Sroberto
64285612Sdelphij/**
65285612Sdelphij * Select among various ways to emit version information.
66285612Sdelphij *
67285612Sdelphij * @param[in] o   the option descriptor
68285612Sdelphij * @param[in] fp  the output stream
69285612Sdelphij */
70285612Sdelphijstatic void
71285612Sdelphijemit_simple_ver(tOptions * o, FILE * fp)
72285612Sdelphij{
73285612Sdelphij    emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
74285612Sdelphij}
75181834Sroberto
76285612Sdelphij/**
77285612Sdelphij * print the version with a copyright notice.
78285612Sdelphij *
79285612Sdelphij * @param[in] o   the option descriptor
80285612Sdelphij * @param[in] fp  the output stream
81285612Sdelphij */
82181834Srobertostatic void
83285612Sdelphijemit_copy_full(tOptions * o, FILE * fp)
84181834Sroberto{
85285612Sdelphij    if (o->pzCopyright != NULL)
86285612Sdelphij        fputs(o->pzCopyright, fp);
87181834Sroberto
88285612Sdelphij    else if (o->pzFullVersion != NULL)
89285612Sdelphij        fputs(o->pzFullVersion, fp);
90181834Sroberto
91285612Sdelphij    else
92285612Sdelphij        emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
93285612Sdelphij
94285612Sdelphij    if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
95285612Sdelphij        fputc(NL, fp);
96285612Sdelphij        fputs(o->pzPackager, fp);
97181834Sroberto
98285612Sdelphij    } else if (o->pzBugAddr != NULL) {
99285612Sdelphij        fputc(NL, fp);
100285612Sdelphij        fprintf(fp, zPlsSendBugs, o->pzBugAddr);
101181834Sroberto    }
102285612Sdelphij}
103181834Sroberto
104285612Sdelphij/**
105285612Sdelphij * print the version and any copyright notice.
106285612Sdelphij * The version with a full copyright and additional notes.
107285612Sdelphij *
108285612Sdelphij * @param[in] opts  the option descriptor
109285612Sdelphij * @param[in] fp    the output stream
110285612Sdelphij */
111285612Sdelphijstatic void
112285612Sdelphijemit_copy_note(tOptions * opts, FILE * fp)
113285612Sdelphij{
114285612Sdelphij    if (opts->pzCopyright != NULL)
115285612Sdelphij        fputs(opts->pzCopyright, fp);
116181834Sroberto
117285612Sdelphij    if (opts->pzCopyNotice != NULL)
118285612Sdelphij        fputs(opts->pzCopyNotice, fp);
119181834Sroberto
120285612Sdelphij    fputc(NL, fp);
121285612Sdelphij    fprintf(fp, zao_ver_fmt, optionVersion());
122285612Sdelphij
123285612Sdelphij    if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
124285612Sdelphij        fputc(NL, fp);
125285612Sdelphij        fputs(opts->pzPackager, fp);
126181834Sroberto
127285612Sdelphij    } else if (opts->pzBugAddr != NULL) {
128285612Sdelphij        fputc(NL, fp);
129285612Sdelphij        fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
130285612Sdelphij    }
131285612Sdelphij}
132181834Sroberto
133285612Sdelphij/**
134285612Sdelphij * Handle the version printing.  We must see how much information
135285612Sdelphij * is being requested and select the correct printing routine.
136285612Sdelphij */
137285612Sdelphijstatic void
138285612Sdelphijprint_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
139285612Sdelphij{
140285612Sdelphij    char ch;
141181834Sroberto
142285612Sdelphij    if (opts <= OPTPROC_EMIT_LIMIT)
143285612Sdelphij        return;
144285612Sdelphij
145285612Sdelphij    /*
146285612Sdelphij     *  IF we have an argument for this option, use it
147285612Sdelphij     *  Otherwise, default to version only or copyright note,
148285612Sdelphij     *  depending on whether the layout is GNU standard form or not.
149285612Sdelphij     */
150285612Sdelphij    if (  (od->fOptState & OPTST_ARG_OPTIONAL)
151285612Sdelphij       && (od->optArg.argString != NULL)
152285612Sdelphij       && (od->optArg.argString[0] != NUL))
153285612Sdelphij
154285612Sdelphij        ch = od->optArg.argString[0];
155285612Sdelphij
156285612Sdelphij    else {
157285612Sdelphij        set_usage_flags(opts, NULL);
158285612Sdelphij        ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
159285612Sdelphij    }
160285612Sdelphij
161285612Sdelphij    switch (ch) {
162285612Sdelphij    case NUL: /* arg provided, but empty */
163285612Sdelphij    case 'v': case 'V': emit_simple_ver(opts, fp); break;
164285612Sdelphij    case 'c': case 'C': emit_copy_full( opts, fp); break;
165285612Sdelphij    case 'n': case 'N': emit_copy_note( opts, fp); break;
166285612Sdelphij
167181834Sroberto    default:
168285612Sdelphij        fprintf(stderr, zBadVerArg, ch);
169285612Sdelphij        option_exits(EXIT_FAILURE);
170181834Sroberto    }
171181834Sroberto
172285612Sdelphij    fflush(fp);
173285612Sdelphij    if (ferror(fp))
174285612Sdelphij        fserr_exit(opts->pzProgName, zwriting,
175285612Sdelphij                   (fp == stdout) ? zstdout_name : zstderr_name);
176285612Sdelphij
177285612Sdelphij    if (call_exit)
178285612Sdelphij        option_exits(EXIT_SUCCESS);
179181834Sroberto}
180181834Sroberto
181181834Sroberto/*=export_func  optionPrintVersion
182181834Sroberto *
183181834Sroberto * what:  Print the program version
184285612Sdelphij * arg:   + tOptions * + opts + program options descriptor +
185285612Sdelphij * arg:   + tOptDesc * + od   + the descriptor for this arg +
186181834Sroberto *
187181834Sroberto * doc:
188181834Sroberto *  This routine will print the version to stdout.
189181834Sroberto=*/
190181834Srobertovoid
191285612SdelphijoptionPrintVersion(tOptions * opts, tOptDesc * od)
192181834Sroberto{
193285612Sdelphij    print_ver(opts, od, print_exit ? stderr : stdout, true);
194181834Sroberto}
195181834Sroberto
196285612Sdelphij/*=export_func  optionPrintVersionAndReturn
197285612Sdelphij *
198285612Sdelphij * what:  Print the program version
199285612Sdelphij * arg:   + tOptions * + opts + program options descriptor +
200285612Sdelphij * arg:   + tOptDesc * + od   + the descriptor for this arg +
201285612Sdelphij *
202285612Sdelphij * doc:
203285612Sdelphij *  This routine will print the version to stdout and return
204285612Sdelphij *  instead of exiting.  Please see the source for the
205285612Sdelphij *  @code{print_ver} funtion for details on selecting how
206285612Sdelphij *  verbose to be after this function returns.
207285612Sdelphij=*/
208285612Sdelphijvoid
209285612SdelphijoptionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
210285612Sdelphij{
211285612Sdelphij    print_ver(opts, od, print_exit ? stderr : stdout, false);
212285612Sdelphij}
213285612Sdelphij
214181834Sroberto/*=export_func  optionVersionStderr
215181834Sroberto * private:
216181834Sroberto *
217181834Sroberto * what:  Print the program version to stderr
218285612Sdelphij * arg:   + tOptions * + opts + program options descriptor +
219285612Sdelphij * arg:   + tOptDesc * + od   + the descriptor for this arg +
220181834Sroberto *
221181834Sroberto * doc:
222181834Sroberto *  This routine will print the version to stderr.
223181834Sroberto=*/
224181834Srobertovoid
225285612SdelphijoptionVersionStderr(tOptions * opts, tOptDesc * od)
226181834Sroberto{
227285612Sdelphij    print_ver(opts, od, stderr, true);
228181834Sroberto}
229181834Sroberto
230285612Sdelphij/** @}
231285612Sdelphij *
232181834Sroberto * Local Variables:
233181834Sroberto * mode: C
234181834Sroberto * c-file-style: "stroustrup"
235181834Sroberto * indent-tabs-mode: nil
236181834Sroberto * End:
237181834Sroberto * end of autoopts/version.c */
238