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