1181834Sroberto
2280849Scy/** \file version.c
3181834Sroberto *
4181834Sroberto *  This module implements the default usage procedure for
5181834Sroberto *  Automated Options.  It may be overridden, of course.
6280849Scy *
7280849Scy * @addtogroup autoopts
8280849Scy * @{
9181834Sroberto */
10280849Scy/*
11280849Scy *  This file is part of AutoOpts, a companion to AutoGen.
12280849Scy *  AutoOpts is free software.
13285169Scy *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14181834Sroberto *
15280849Scy *  AutoOpts is available under any one of two licenses.  The license
16280849Scy *  in use must be one of these two and the choice is under the control
17280849Scy *  of the user of the license.
18181834Sroberto *
19280849Scy *   The GNU Lesser General Public License, version 3 or later
20280849Scy *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21181834Sroberto *
22280849Scy *   The Modified Berkeley Software Distribution License
23280849Scy *      See the file "COPYING.mbsd"
24181834Sroberto *
25280849Scy *  These files have the following sha256 sums:
26181834Sroberto *
27280849Scy *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28280849Scy *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29280849Scy *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30181834Sroberto */
31181834Sroberto
32181834Sroberto/*=export_func  optionVersion
33181834Sroberto *
34181834Sroberto * what:     return the compiled AutoOpts version number
35285169Scy * 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=*/
41285169Scychar const *
42280849ScyoptionVersion(void)
43181834Sroberto{
44280849Scy    static char const ver[] = OPTIONS_DOTTED_VERSION;
45280849Scy    return ver;
46280849Scy}
47181834Sroberto
48280849Scystatic void
49280849Scyemit_first_line(
50280849Scy    FILE * fp, char const * alt1, char const * alt2, char const * alt3)
51280849Scy{
52280849Scy    char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
53280849Scy    char const * e;
54280849Scy    if (p == NULL)
55280849Scy        return;
56280849Scy    e = strchr(p, NL);
57280849Scy    if (e == NULL)
58280849Scy        fputs(p, fp);
59280849Scy    else
60280849Scy        fwrite(p, 1, (e - p), fp);
61280849Scy    fputc(NL, fp);
62181834Sroberto}
63181834Sroberto
64280849Scy/**
65280849Scy * Select among various ways to emit version information.
66280849Scy *
67280849Scy * @param[in] o   the option descriptor
68280849Scy * @param[in] fp  the output stream
69280849Scy */
70280849Scystatic void
71280849Scyemit_simple_ver(tOptions * o, FILE * fp)
72280849Scy{
73280849Scy    emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
74280849Scy}
75181834Sroberto
76280849Scy/**
77280849Scy * print the version with a copyright notice.
78280849Scy *
79280849Scy * @param[in] o   the option descriptor
80280849Scy * @param[in] fp  the output stream
81280849Scy */
82181834Srobertostatic void
83280849Scyemit_copy_full(tOptions * o, FILE * fp)
84181834Sroberto{
85280849Scy    if (o->pzCopyright != NULL)
86280849Scy        fputs(o->pzCopyright, fp);
87181834Sroberto
88280849Scy    else if (o->pzFullVersion != NULL)
89280849Scy        fputs(o->pzFullVersion, fp);
90181834Sroberto
91280849Scy    else
92280849Scy        emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
93280849Scy
94280849Scy    if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
95280849Scy        fputc(NL, fp);
96280849Scy        fputs(o->pzPackager, fp);
97181834Sroberto
98280849Scy    } else if (o->pzBugAddr != NULL) {
99280849Scy        fputc(NL, fp);
100280849Scy        fprintf(fp, zPlsSendBugs, o->pzBugAddr);
101181834Sroberto    }
102280849Scy}
103181834Sroberto
104280849Scy/**
105280849Scy * print the version and any copyright notice.
106280849Scy * The version with a full copyright and additional notes.
107280849Scy *
108280849Scy * @param[in] opts  the option descriptor
109280849Scy * @param[in] fp    the output stream
110280849Scy */
111280849Scystatic void
112280849Scyemit_copy_note(tOptions * opts, FILE * fp)
113280849Scy{
114280849Scy    if (opts->pzCopyright != NULL)
115280849Scy        fputs(opts->pzCopyright, fp);
116181834Sroberto
117280849Scy    if (opts->pzCopyNotice != NULL)
118280849Scy        fputs(opts->pzCopyNotice, fp);
119181834Sroberto
120280849Scy    fputc(NL, fp);
121280849Scy    fprintf(fp, zao_ver_fmt, optionVersion());
122280849Scy
123280849Scy    if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
124280849Scy        fputc(NL, fp);
125280849Scy        fputs(opts->pzPackager, fp);
126181834Sroberto
127280849Scy    } else if (opts->pzBugAddr != NULL) {
128280849Scy        fputc(NL, fp);
129280849Scy        fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
130280849Scy    }
131280849Scy}
132181834Sroberto
133280849Scy/**
134280849Scy * Handle the version printing.  We must see how much information
135280849Scy * is being requested and select the correct printing routine.
136280849Scy */
137280849Scystatic void
138280849Scyprint_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
139280849Scy{
140280849Scy    char ch;
141181834Sroberto
142280849Scy    if (opts <= OPTPROC_EMIT_LIMIT)
143280849Scy        return;
144280849Scy
145280849Scy    /*
146280849Scy     *  IF we have an argument for this option, use it
147280849Scy     *  Otherwise, default to version only or copyright note,
148280849Scy     *  depending on whether the layout is GNU standard form or not.
149280849Scy     */
150280849Scy    if (  (od->fOptState & OPTST_ARG_OPTIONAL)
151280849Scy       && (od->optArg.argString != NULL)
152280849Scy       && (od->optArg.argString[0] != NUL))
153280849Scy
154280849Scy        ch = od->optArg.argString[0];
155280849Scy
156280849Scy    else {
157280849Scy        set_usage_flags(opts, NULL);
158280849Scy        ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
159280849Scy    }
160280849Scy
161280849Scy    switch (ch) {
162280849Scy    case NUL: /* arg provided, but empty */
163280849Scy    case 'v': case 'V': emit_simple_ver(opts, fp); break;
164280849Scy    case 'c': case 'C': emit_copy_full( opts, fp); break;
165280849Scy    case 'n': case 'N': emit_copy_note( opts, fp); break;
166280849Scy
167181834Sroberto    default:
168280849Scy        fprintf(stderr, zBadVerArg, ch);
169280849Scy        option_exits(EXIT_FAILURE);
170181834Sroberto    }
171181834Sroberto
172280849Scy    fflush(fp);
173280849Scy    if (ferror(fp))
174280849Scy        fserr_exit(opts->pzProgName, zwriting,
175280849Scy                   (fp == stdout) ? zstdout_name : zstderr_name);
176280849Scy
177280849Scy    if (call_exit)
178280849Scy        option_exits(EXIT_SUCCESS);
179181834Sroberto}
180181834Sroberto
181181834Sroberto/*=export_func  optionPrintVersion
182181834Sroberto *
183181834Sroberto * what:  Print the program version
184285169Scy * arg:   + tOptions * + opts + program options descriptor +
185285169Scy * arg:   + tOptDesc * + od   + the descriptor for this arg +
186181834Sroberto *
187181834Sroberto * doc:
188181834Sroberto *  This routine will print the version to stdout.
189181834Sroberto=*/
190181834Srobertovoid
191280849ScyoptionPrintVersion(tOptions * opts, tOptDesc * od)
192181834Sroberto{
193280849Scy    print_ver(opts, od, print_exit ? stderr : stdout, true);
194181834Sroberto}
195181834Sroberto
196280849Scy/*=export_func  optionPrintVersionAndReturn
197280849Scy *
198280849Scy * what:  Print the program version
199285169Scy * arg:   + tOptions * + opts + program options descriptor +
200285169Scy * arg:   + tOptDesc * + od   + the descriptor for this arg +
201280849Scy *
202280849Scy * doc:
203280849Scy *  This routine will print the version to stdout and return
204280849Scy *  instead of exiting.  Please see the source for the
205280849Scy *  @code{print_ver} funtion for details on selecting how
206280849Scy *  verbose to be after this function returns.
207280849Scy=*/
208280849Scyvoid
209280849ScyoptionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
210280849Scy{
211280849Scy    print_ver(opts, od, print_exit ? stderr : stdout, false);
212280849Scy}
213280849Scy
214181834Sroberto/*=export_func  optionVersionStderr
215181834Sroberto * private:
216181834Sroberto *
217181834Sroberto * what:  Print the program version to stderr
218285169Scy * arg:   + tOptions * + opts + program options descriptor +
219285169Scy * arg:   + tOptDesc * + od   + the descriptor for this arg +
220181834Sroberto *
221181834Sroberto * doc:
222181834Sroberto *  This routine will print the version to stderr.
223181834Sroberto=*/
224181834Srobertovoid
225280849ScyoptionVersionStderr(tOptions * opts, tOptDesc * od)
226181834Sroberto{
227280849Scy    print_ver(opts, od, stderr, true);
228181834Sroberto}
229181834Sroberto
230280849Scy/** @}
231280849Scy *
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