1181834Sroberto
2290001Sglebius/** \file version.c
3181834Sroberto *
4181834Sroberto *  This module implements the default usage procedure for
5181834Sroberto *  Automated Options.  It may be overridden, of course.
6290001Sglebius *
7290001Sglebius * @addtogroup autoopts
8290001Sglebius * @{
9181834Sroberto */
10290001Sglebius/*
11290001Sglebius *  This file is part of AutoOpts, a companion to AutoGen.
12290001Sglebius *  AutoOpts is free software.
13290001Sglebius *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14181834Sroberto *
15290001Sglebius *  AutoOpts is available under any one of two licenses.  The license
16290001Sglebius *  in use must be one of these two and the choice is under the control
17290001Sglebius *  of the user of the license.
18181834Sroberto *
19290001Sglebius *   The GNU Lesser General Public License, version 3 or later
20290001Sglebius *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21181834Sroberto *
22290001Sglebius *   The Modified Berkeley Software Distribution License
23290001Sglebius *      See the file "COPYING.mbsd"
24181834Sroberto *
25290001Sglebius *  These files have the following sha256 sums:
26181834Sroberto *
27290001Sglebius *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28290001Sglebius *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29290001Sglebius *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30181834Sroberto */
31181834Sroberto
32181834Sroberto/*=export_func  optionVersion
33181834Sroberto *
34181834Sroberto * what:     return the compiled AutoOpts version number
35290001Sglebius * 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=*/
41290001Sglebiuschar const *
42290001SglebiusoptionVersion(void)
43181834Sroberto{
44290001Sglebius    static char const ver[] = OPTIONS_DOTTED_VERSION;
45290001Sglebius    return ver;
46290001Sglebius}
47181834Sroberto
48290001Sglebiusstatic void
49290001Sglebiusemit_first_line(
50290001Sglebius    FILE * fp, char const * alt1, char const * alt2, char const * alt3)
51290001Sglebius{
52290001Sglebius    char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
53290001Sglebius    char const * e;
54290001Sglebius    if (p == NULL)
55290001Sglebius        return;
56290001Sglebius    e = strchr(p, NL);
57290001Sglebius    if (e == NULL)
58290001Sglebius        fputs(p, fp);
59290001Sglebius    else
60290001Sglebius        fwrite(p, 1, (e - p), fp);
61290001Sglebius    fputc(NL, fp);
62181834Sroberto}
63181834Sroberto
64290001Sglebius/**
65290001Sglebius * Select among various ways to emit version information.
66290001Sglebius *
67290001Sglebius * @param[in] o   the option descriptor
68290001Sglebius * @param[in] fp  the output stream
69290001Sglebius */
70290001Sglebiusstatic void
71290001Sglebiusemit_simple_ver(tOptions * o, FILE * fp)
72290001Sglebius{
73290001Sglebius    emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
74290001Sglebius}
75181834Sroberto
76290001Sglebius/**
77290001Sglebius * print the version with a copyright notice.
78290001Sglebius *
79290001Sglebius * @param[in] o   the option descriptor
80290001Sglebius * @param[in] fp  the output stream
81290001Sglebius */
82181834Srobertostatic void
83290001Sglebiusemit_copy_full(tOptions * o, FILE * fp)
84181834Sroberto{
85290001Sglebius    if (o->pzCopyright != NULL)
86290001Sglebius        fputs(o->pzCopyright, fp);
87181834Sroberto
88290001Sglebius    else if (o->pzFullVersion != NULL)
89290001Sglebius        fputs(o->pzFullVersion, fp);
90181834Sroberto
91290001Sglebius    else
92290001Sglebius        emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
93290001Sglebius
94290001Sglebius    if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
95290001Sglebius        fputc(NL, fp);
96290001Sglebius        fputs(o->pzPackager, fp);
97181834Sroberto
98290001Sglebius    } else if (o->pzBugAddr != NULL) {
99290001Sglebius        fputc(NL, fp);
100290001Sglebius        fprintf(fp, zPlsSendBugs, o->pzBugAddr);
101181834Sroberto    }
102290001Sglebius}
103181834Sroberto
104290001Sglebius/**
105290001Sglebius * print the version and any copyright notice.
106290001Sglebius * The version with a full copyright and additional notes.
107290001Sglebius *
108290001Sglebius * @param[in] opts  the option descriptor
109290001Sglebius * @param[in] fp    the output stream
110290001Sglebius */
111290001Sglebiusstatic void
112290001Sglebiusemit_copy_note(tOptions * opts, FILE * fp)
113290001Sglebius{
114290001Sglebius    if (opts->pzCopyright != NULL)
115290001Sglebius        fputs(opts->pzCopyright, fp);
116181834Sroberto
117290001Sglebius    if (opts->pzCopyNotice != NULL)
118290001Sglebius        fputs(opts->pzCopyNotice, fp);
119181834Sroberto
120290001Sglebius    fputc(NL, fp);
121290001Sglebius    fprintf(fp, zao_ver_fmt, optionVersion());
122290001Sglebius
123290001Sglebius    if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
124290001Sglebius        fputc(NL, fp);
125290001Sglebius        fputs(opts->pzPackager, fp);
126181834Sroberto
127290001Sglebius    } else if (opts->pzBugAddr != NULL) {
128290001Sglebius        fputc(NL, fp);
129290001Sglebius        fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
130290001Sglebius    }
131290001Sglebius}
132181834Sroberto
133290001Sglebius/**
134290001Sglebius * Handle the version printing.  We must see how much information
135290001Sglebius * is being requested and select the correct printing routine.
136290001Sglebius */
137290001Sglebiusstatic void
138290001Sglebiusprint_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
139290001Sglebius{
140290001Sglebius    char ch;
141181834Sroberto
142290001Sglebius    if (opts <= OPTPROC_EMIT_LIMIT)
143290001Sglebius        return;
144290001Sglebius
145290001Sglebius    /*
146290001Sglebius     *  IF we have an argument for this option, use it
147290001Sglebius     *  Otherwise, default to version only or copyright note,
148290001Sglebius     *  depending on whether the layout is GNU standard form or not.
149290001Sglebius     */
150290001Sglebius    if (  (od->fOptState & OPTST_ARG_OPTIONAL)
151290001Sglebius       && (od->optArg.argString != NULL)
152290001Sglebius       && (od->optArg.argString[0] != NUL))
153290001Sglebius
154290001Sglebius        ch = od->optArg.argString[0];
155290001Sglebius
156290001Sglebius    else {
157290001Sglebius        set_usage_flags(opts, NULL);
158290001Sglebius        ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
159290001Sglebius    }
160290001Sglebius
161290001Sglebius    switch (ch) {
162290001Sglebius    case NUL: /* arg provided, but empty */
163290001Sglebius    case 'v': case 'V': emit_simple_ver(opts, fp); break;
164290001Sglebius    case 'c': case 'C': emit_copy_full( opts, fp); break;
165290001Sglebius    case 'n': case 'N': emit_copy_note( opts, fp); break;
166290001Sglebius
167181834Sroberto    default:
168290001Sglebius        fprintf(stderr, zBadVerArg, ch);
169290001Sglebius        option_exits(EXIT_FAILURE);
170181834Sroberto    }
171181834Sroberto
172290001Sglebius    fflush(fp);
173290001Sglebius    if (ferror(fp))
174290001Sglebius        fserr_exit(opts->pzProgName, zwriting,
175290001Sglebius                   (fp == stdout) ? zstdout_name : zstderr_name);
176290001Sglebius
177290001Sglebius    if (call_exit)
178290001Sglebius        option_exits(EXIT_SUCCESS);
179181834Sroberto}
180181834Sroberto
181181834Sroberto/*=export_func  optionPrintVersion
182181834Sroberto *
183181834Sroberto * what:  Print the program version
184290001Sglebius * arg:   + tOptions * + opts + program options descriptor +
185290001Sglebius * arg:   + tOptDesc * + od   + the descriptor for this arg +
186181834Sroberto *
187181834Sroberto * doc:
188181834Sroberto *  This routine will print the version to stdout.
189181834Sroberto=*/
190181834Srobertovoid
191290001SglebiusoptionPrintVersion(tOptions * opts, tOptDesc * od)
192181834Sroberto{
193290001Sglebius    print_ver(opts, od, print_exit ? stderr : stdout, true);
194181834Sroberto}
195181834Sroberto
196290001Sglebius/*=export_func  optionPrintVersionAndReturn
197290001Sglebius *
198290001Sglebius * what:  Print the program version
199290001Sglebius * arg:   + tOptions * + opts + program options descriptor +
200290001Sglebius * arg:   + tOptDesc * + od   + the descriptor for this arg +
201290001Sglebius *
202290001Sglebius * doc:
203290001Sglebius *  This routine will print the version to stdout and return
204290001Sglebius *  instead of exiting.  Please see the source for the
205290001Sglebius *  @code{print_ver} funtion for details on selecting how
206290001Sglebius *  verbose to be after this function returns.
207290001Sglebius=*/
208290001Sglebiusvoid
209290001SglebiusoptionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
210290001Sglebius{
211290001Sglebius    print_ver(opts, od, print_exit ? stderr : stdout, false);
212290001Sglebius}
213290001Sglebius
214181834Sroberto/*=export_func  optionVersionStderr
215181834Sroberto * private:
216181834Sroberto *
217181834Sroberto * what:  Print the program version to stderr
218290001Sglebius * arg:   + tOptions * + opts + program options descriptor +
219290001Sglebius * arg:   + tOptDesc * + od   + the descriptor for this arg +
220181834Sroberto *
221181834Sroberto * doc:
222181834Sroberto *  This routine will print the version to stderr.
223181834Sroberto=*/
224181834Srobertovoid
225290001SglebiusoptionVersionStderr(tOptions * opts, tOptDesc * od)
226181834Sroberto{
227290001Sglebius    print_ver(opts, od, stderr, true);
228181834Sroberto}
229181834Sroberto
230290001Sglebius/** @}
231290001Sglebius *
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