Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/sntp/libopts/version.c
Line
Count
Source (jump to first uncovered line)
1
2
/** \file version.c
3
 *
4
 *  This module implements the default usage procedure for
5
 *  Automated Options.  It may be overridden, of course.
6
 *
7
 * @addtogroup autoopts
8
 * @{
9
 */
10
/*
11
 *  This file is part of AutoOpts, a companion to AutoGen.
12
 *  AutoOpts is free software.
13
 *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
14
 *
15
 *  AutoOpts is available under any one of two licenses.  The license
16
 *  in use must be one of these two and the choice is under the control
17
 *  of the user of the license.
18
 *
19
 *   The GNU Lesser General Public License, version 3 or later
20
 *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
21
 *
22
 *   The Modified Berkeley Software Distribution License
23
 *      See the file "COPYING.mbsd"
24
 *
25
 *  These files have the following sha256 sums:
26
 *
27
 *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
28
 *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
29
 *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
30
 */
31
32
/*=export_func  optionVersion
33
 *
34
 * what:     return the compiled AutoOpts version number
35
 * ret_type: char const *
36
 * ret_desc: the version string in constant memory
37
 * doc:
38
 *  Returns the full version string compiled into the library.
39
 *  The returned string cannot be modified.
40
=*/
41
char const *
42
optionVersion(void)
43
0
{
44
0
    static char const ver[] = OPTIONS_DOTTED_VERSION;
45
0
    return ver;
46
0
}
47
48
static void
49
emit_first_line(
50
    FILE * fp, char const * alt1, char const * alt2, char const * alt3)
51
0
{
52
0
    char const * p = (alt1 != NULL) ? alt1 : ((alt2 != NULL) ? alt2 : alt3);
53
0
    char const * e;
54
0
    if (p == NULL)
55
0
        return;
56
0
    e = strchr(p, NL);
57
0
    if (e == NULL)
58
0
        fputs(p, fp);
59
0
    else
60
0
        fwrite(p, 1, (e - p), fp);
61
0
    fputc(NL, fp);
62
0
}
63
64
/**
65
 * Select among various ways to emit version information.
66
 *
67
 * @param[in] o   the option descriptor
68
 * @param[in] fp  the output stream
69
 */
70
static void
71
emit_simple_ver(tOptions * o, FILE * fp)
72
0
{
73
0
    emit_first_line(fp, o->pzFullVersion, o->pzCopyright, o->pzUsageTitle);
74
0
}
75
76
/**
77
 * print the version with a copyright notice.
78
 *
79
 * @param[in] o   the option descriptor
80
 * @param[in] fp  the output stream
81
 */
82
static void
83
emit_copy_full(tOptions * o, FILE * fp)
84
0
{
85
0
    if (o->pzCopyright != NULL)
86
0
        fputs(o->pzCopyright, fp);
87
88
0
    else if (o->pzFullVersion != NULL)
89
0
        fputs(o->pzFullVersion, fp);
90
91
0
    else
92
0
        emit_first_line(fp, o->pzUsageTitle, NULL, NULL);
93
    
94
0
    if (HAS_pzPkgDataDir(o) && (o->pzPackager != NULL)) {
95
0
        fputc(NL, fp);
96
0
        fputs(o->pzPackager, fp);
97
98
0
    } else if (o->pzBugAddr != NULL) {
99
0
        fputc(NL, fp);
100
0
        fprintf(fp, zPlsSendBugs, o->pzBugAddr);
101
0
    }
102
0
}
103
104
/**
105
 * print the version and any copyright notice.
106
 * The version with a full copyright and additional notes.
107
 *
108
 * @param[in] opts  the option descriptor
109
 * @param[in] fp    the output stream
110
 */
111
static void
112
emit_copy_note(tOptions * opts, FILE * fp)
113
0
{
114
0
    if (opts->pzCopyright != NULL)
115
0
        fputs(opts->pzCopyright, fp);
116
117
0
    if (opts->pzCopyNotice != NULL)
118
0
        fputs(opts->pzCopyNotice, fp);
119
120
0
    fputc(NL, fp);
121
0
    fprintf(fp, zao_ver_fmt, optionVersion());
122
    
123
0
    if (HAS_pzPkgDataDir(opts) && (opts->pzPackager != NULL)) {
124
0
        fputc(NL, fp);
125
0
        fputs(opts->pzPackager, fp);
126
127
0
    } else if (opts->pzBugAddr != NULL) {
128
0
        fputc(NL, fp);
129
0
        fprintf(fp, zPlsSendBugs, opts->pzBugAddr);
130
0
    }
131
0
}
132
133
/**
134
 * Handle the version printing.  We must see how much information
135
 * is being requested and select the correct printing routine.
136
 */
137
static void
138
print_ver(tOptions * opts, tOptDesc * od, FILE * fp, bool call_exit)
139
0
{
140
0
    char ch;
141
142
0
    if (opts <= OPTPROC_EMIT_LIMIT)
143
0
        return;
144
145
    /*
146
     *  IF we have an argument for this option, use it
147
     *  Otherwise, default to version only or copyright note,
148
     *  depending on whether the layout is GNU standard form or not.
149
     */
150
0
    if (  (od->fOptState & OPTST_ARG_OPTIONAL)
151
0
       && (od->optArg.argString != NULL)
152
0
       && (od->optArg.argString[0] != NUL))
153
154
0
        ch = od->optArg.argString[0];
155
156
0
    else {
157
0
        set_usage_flags(opts, NULL);
158
0
        ch = (opts->fOptSet & OPTPROC_GNUUSAGE) ? 'c' : 'v';
159
0
    }
160
161
0
    switch (ch) {
162
0
    case NUL: /* arg provided, but empty */
163
0
    case 'v': case 'V': emit_simple_ver(opts, fp); break;
164
0
    case 'c': case 'C': emit_copy_full( opts, fp); break;
165
0
    case 'n': case 'N': emit_copy_note( opts, fp); break;
166
167
0
    default:
168
0
        fprintf(stderr, zBadVerArg, ch);
169
0
        option_exits(EXIT_FAILURE);
170
0
    }
171
172
0
    fflush(fp);
173
0
    if (ferror(fp))
174
0
        fserr_exit(opts->pzProgName, zwriting,
175
0
                   (fp == stdout) ? zstdout_name : zstderr_name);
176
177
0
    if (call_exit)
178
0
        option_exits(EXIT_SUCCESS);
179
0
}
180
181
/*=export_func  optionPrintVersion
182
 *
183
 * what:  Print the program version
184
 * arg:   + tOptions * + opts + program options descriptor +
185
 * arg:   + tOptDesc * + od   + the descriptor for this arg +
186
 *
187
 * doc:
188
 *  This routine will print the version to stdout.
189
=*/
190
void
191
optionPrintVersion(tOptions * opts, tOptDesc * od)
192
0
{
193
0
    print_ver(opts, od, print_exit ? stderr : stdout, true);
194
0
}
195
196
/*=export_func  optionPrintVersionAndReturn
197
 *
198
 * what:  Print the program version
199
 * arg:   + tOptions * + opts + program options descriptor +
200
 * arg:   + tOptDesc * + od   + the descriptor for this arg +
201
 *
202
 * doc:
203
 *  This routine will print the version to stdout and return
204
 *  instead of exiting.  Please see the source for the
205
 *  @code{print_ver} funtion for details on selecting how
206
 *  verbose to be after this function returns.
207
=*/
208
void
209
optionPrintVersionAndReturn(tOptions * opts, tOptDesc * od)
210
0
{
211
0
    print_ver(opts, od, print_exit ? stderr : stdout, false);
212
0
}
213
214
/*=export_func  optionVersionStderr
215
 * private:
216
 *
217
 * what:  Print the program version to stderr
218
 * arg:   + tOptions * + opts + program options descriptor +
219
 * arg:   + tOptDesc * + od   + the descriptor for this arg +
220
 *
221
 * doc:
222
 *  This routine will print the version to stderr.
223
=*/
224
void
225
optionVersionStderr(tOptions * opts, tOptDesc * od)
226
0
{
227
0
    print_ver(opts, od, stderr, true);
228
0
}
229
230
/** @}
231
 *
232
 * Local Variables:
233
 * mode: C
234
 * c-file-style: "stroustrup"
235
 * indent-tabs-mode: nil
236
 * End:
237
 * end of autoopts/version.c */