123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651 |
- /*
- * usage.c $Id: usage.c,v 4.7 2005/03/13 19:51:59 bkorb Exp $
- * Time-stamp: "2005-02-20 13:47:52 bkorb"
- *
- * This module implements the default usage procedure for
- * Automated Options. It may be overridden, of course.
- */
- /*
- * Automated Options copyright 1992-2005 Bruce Korb
- *
- * Automated Options is free software.
- * You may redistribute it and/or modify it under the terms of the
- * GNU General Public License, as published by the Free Software
- * Foundation; either version 2, or (at your option) any later version.
- *
- * Automated Options is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Automated Options. See the file "COPYING". If not,
- * write to: The Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * As a special exception, Bruce Korb gives permission for additional
- * uses of the text contained in his release of AutoOpts.
- *
- * The exception is that, if you link the AutoOpts library with other
- * files to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the AutoOpts library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
- *
- * This exception applies only to the code released by Bruce Korb under
- * the name AutoOpts. If you copy code from other sources under the
- * General Public License into a copy of AutoOpts, as the General Public
- * License permits, the exception does not apply to the code that you add
- * in this way. To avoid misleading anyone as to the status of such
- * modified files, you must delete this exception notice from them.
- *
- * If you write modifications of your own for AutoOpts, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.
- */
- #define OPTPROC_L_N_S (OPTPROC_LONGOPT | OPTPROC_SHORTOPT)
- #ifndef tSC
- # define tSC static char
- #endif
- static arg_types_t argTypes;
- FILE* option_usage_fp = NULL;
- static char zOptFmtLine[ 16 ];
- static ag_bool displayEnum;
- /* = = = START-STATIC-FORWARD = = = */
- /* static forward declarations maintained by :mkfwd */
- static void
- printProgramDetails( tOptions* pOptions );
- static void
- printExtendedUsage(
- tOptions* pOptions,
- tOptDesc* pOD,
- arg_types_t* pAT );
- static void
- printBareUsage(
- tOptions* pOptions,
- tOptDesc* pOD,
- arg_types_t* pAT );
- static void
- setStdOptFmts( tOptions* pOpts, tCC** ppT );
- static void
- setGnuOptFmts( tOptions* pOpts, tCC** ppT );
- static void
- printInitList(
- tCC** papz,
- ag_bool* pInitIntro,
- tCC* pzRc,
- tCC* pzPN );
- /* = = = END-STATIC-FORWARD = = = */
- /*=export_func optionUsage
- * private:
- *
- * what: Print usage text
- * arg: + tOptions* + pOpts + program options descriptor +
- * arg: + int + exitCode + exit code for calling exit(3) +
- *
- * doc:
- * This routine will print usage in both GNU-standard and AutoOpts-expanded
- * formats. The descriptor specifies the default, but AUTOOPTS_USAGE will
- * over-ride this, providing the value of it is set to either "gnu" or
- * "autoopts". This routine will @strong{not} return.
- =*/
- void
- optionUsage(
- tOptions* pOptions,
- int exitCode )
- {
- tCC* pOptTitle;
- displayEnum = AG_FALSE;
- /*
- * Paged usage will preset option_usage_fp to an output file.
- * If it hasn't already been set, then set it to standard output
- * on successful exit (help was requested), otherwise error out.
- */
- if (option_usage_fp == NULL)
- option_usage_fp = (exitCode != EXIT_SUCCESS) ? stderr : stdout;
- fprintf( option_usage_fp, pOptions->pzUsageTitle, pOptions->pzProgName );
- do {
- char* pz = getenv( "AUTOOPTS_USAGE" );
- if (pz == NULL) break;
- if (streqvcmp( pz, "gnu" ) == 0) {
- pOptions->fOptSet |= OPTPROC_GNUUSAGE;
- break;
- }
- if (streqvcmp( pz, "autoopts" ) == 0) {
- pOptions->fOptSet &= ~OPTPROC_GNUUSAGE;
- break;
- }
- } while (0);
- /*
- * Determine which header and which option formatting strings to use
- */
- if ((pOptions->fOptSet & OPTPROC_GNUUSAGE) != 0) {
- setGnuOptFmts( pOptions, &pOptTitle );
- fputc( '\n', option_usage_fp );
- }
- else {
- setStdOptFmts( pOptions, &pOptTitle );
- /*
- * When we exit with EXIT_SUCCESS and the first option is a doc option,
- * we do *NOT* want to emit the column headers. Otherwise, we do.
- */
- if ( (exitCode != EXIT_SUCCESS)
- || ((pOptions->pOptDesc->fOptState & OPTST_DOCUMENT) == 0) )
- fputs( pOptTitle, option_usage_fp );
- }
- {
- int ct = pOptions->optCt;
- int optNo = 0;
- tOptDesc* pOD = pOptions->pOptDesc;
- int docCt = 0;
- do {
- if ((pOD->fOptState & OPTST_OMITTED) != 0)
- continue;
- if ((pOD->fOptState & OPTST_DOCUMENT) != 0) {
- if (exitCode == EXIT_SUCCESS) {
- fprintf(option_usage_fp, argTypes.pzBrk, pOD->pzText,
- pOptTitle);
- docCt++;
- }
- continue;
- }
- /*
- * IF this is the first auto-opt maintained option
- * *AND* we are doing a full help
- * *AND* there are documentation options
- * *AND* the last one was not a doc option,
- * THEN document that the remaining options are not user opts
- */
- if ( (pOptions->presetOptCt == optNo)
- && (exitCode == EXIT_SUCCESS)
- && (docCt > 0)
- && ((pOD[-1].fOptState & OPTST_DOCUMENT) == 0) )
- fprintf( option_usage_fp, argTypes.pzBrk, zAuto, pOptTitle );
- printBareUsage( pOptions, pOD, &argTypes );
- /*
- * IF we were invoked because of the --help option,
- * THEN print all the extra info
- */
- if (exitCode == EXIT_SUCCESS)
- printExtendedUsage( pOptions, pOD, &argTypes );
- } while (pOD++, optNo++, (--ct > 0));
- }
- fputc( '\n', option_usage_fp );
- /*
- * Describe the mechanics of denoting the options
- */
- switch (pOptions->fOptSet & OPTPROC_L_N_S) {
- case OPTPROC_L_N_S: fputs( zFlagOkay, option_usage_fp ); break;
- case OPTPROC_SHORTOPT: break;
- case OPTPROC_LONGOPT: fputs( zNoFlags, option_usage_fp ); break;
- case 0: fputs( zOptsOnly, option_usage_fp ); break;
- }
- if ((pOptions->fOptSet & OPTPROC_NUM_OPT) != 0) {
- fputs( zNumberOpt, option_usage_fp );
- }
- if ((pOptions->fOptSet & OPTPROC_REORDER) != 0) {
- fputs( zReorder, option_usage_fp );
- }
- if (pOptions->pzExplain != NULL)
- fputs( pOptions->pzExplain, option_usage_fp );
- /*
- * IF the user is asking for help (thus exiting with SUCCESS),
- * THEN see what additional information we can provide.
- */
- if (exitCode == EXIT_SUCCESS)
- printProgramDetails( pOptions );
- if (pOptions->pzBugAddr != NULL)
- fprintf( option_usage_fp, zPlsSendBugs, pOptions->pzBugAddr );
- fflush( option_usage_fp );
- exit( exitCode );
- }
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- *
- * PROGRAM DETAILS
- */
- static void
- printProgramDetails( tOptions* pOptions )
- {
- ag_bool initIntro = AG_TRUE;
- /*
- * Display all the places we look for config files
- */
- printInitList( pOptions->papzHomeList, &initIntro,
- pOptions->pzRcName, pOptions->pzProgPath );
- /*
- * Let the user know about environment variable settings
- */
- if ((pOptions->fOptSet & OPTPROC_ENVIRON) != 0) {
- if (initIntro)
- fputs( zPresetIntro, option_usage_fp );
- fprintf( option_usage_fp, zExamineFmt, pOptions->pzPROGNAME );
- }
- /*
- * IF we found an enumeration,
- * THEN hunt for it again. Call the handler proc with a NULL
- * option struct pointer. That tells it to display the keywords.
- */
- if (displayEnum) {
- int ct = pOptions->optCt;
- int optNo = 0;
- tOptDesc* pOD = pOptions->pOptDesc;
- fputc( '\n', option_usage_fp );
- fflush( option_usage_fp );
- do {
- switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
- case OPARG_TYPE_ENUMERATION:
- case OPARG_TYPE_MEMBERSHIP:
- (*(pOD->pOptProc))( NULL, pOD );
- }
- } while (pOD++, optNo++, (--ct > 0));
- }
- /*
- * If there is a detail string, now is the time for that.
- */
- if (pOptions->pzDetail != NULL)
- fputs( pOptions->pzDetail, option_usage_fp );
- }
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- *
- * PER OPTION TYPE USAGE INFORMATION
- */
- static void
- printExtendedUsage(
- tOptions* pOptions,
- tOptDesc* pOD,
- arg_types_t* pAT )
- {
- /*
- * IF there are option conflicts or dependencies,
- * THEN print them here.
- */
- if ( (pOD->pOptMust != NULL)
- || (pOD->pOptCant != NULL) ) {
- fputs( zTabHyp, option_usage_fp );
- /*
- * DEPENDENCIES:
- */
- if (pOD->pOptMust != NULL) {
- const int* pOptNo = pOD->pOptMust;
- fputs( zReqThese, option_usage_fp );
- for (;;) {
- fprintf( option_usage_fp, zTabout, pOptions->pOptDesc[
- *pOptNo ].pz_Name );
- if (*++pOptNo == NO_EQUIVALENT)
- break;
- }
- if (pOD->pOptCant != NULL)
- fputs( zTabHypAnd, option_usage_fp );
- }
- /*
- * CONFLICTS:
- */
- if (pOD->pOptCant != NULL) {
- const int* pOptNo = pOD->pOptCant;
- fputs( zProhib, option_usage_fp );
- for (;;) {
- fprintf( option_usage_fp, zTabout, pOptions->pOptDesc[
- *pOptNo ].pz_Name );
- if (*++pOptNo == NO_EQUIVALENT)
- break;
- }
- }
- }
- /*
- * IF there is a disablement string
- * THEN print the disablement info
- */
- if (pOD->pz_DisableName != NULL )
- fprintf( option_usage_fp, zDis, pOD->pz_DisableName );
- /*
- * IF the numeric option has a special callback,
- * THEN call it, requesting the range or other special info
- */
- if ( (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NUMERIC)
- && (pOD->pOptProc != NULL)
- && (pOD->pOptProc != optionNumericVal) ) {
- (*(pOD->pOptProc))( pOptions, NULL );
- }
- /*
- * IF the option defaults to being enabled,
- * THEN print that out
- */
- if (pOD->fOptState & OPTST_INITENABLED)
- fputs( zEnab, option_usage_fp );
- /*
- * IF the option is in an equivalence class
- * AND not the designated lead
- * THEN print equivalence and leave it at that.
- */
- if ( (pOD->optEquivIndex != NO_EQUIVALENT)
- && (pOD->optEquivIndex != pOD->optActualIndex ) ) {
- fprintf( option_usage_fp, zAlt,
- pOptions->pOptDesc[ pOD->optEquivIndex ].pz_Name );
- return;
- }
- /*
- * IF this particular option can NOT be preset
- * AND some form of presetting IS allowed,
- * THEN advise that this option may not be preset.
- */
- if ( ((pOD->fOptState & OPTST_NO_INIT) != 0)
- && ( (pOptions->papzHomeList != NULL)
- || (pOptions->pzPROGNAME != NULL)
- ) )
- fputs( zNoPreset, option_usage_fp );
- /*
- * Print the appearance requirements.
- */
- if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP)
- fputs( zMembers, option_usage_fp );
- else switch (pOD->optMinCt) {
- case 1:
- case 0:
- switch (pOD->optMaxCt) {
- case 0: fputs( zPreset, option_usage_fp ); break;
- case NOLIMIT: fputs( zNoLim, option_usage_fp ); break;
- case 1: break;
- /*
- * IF the max is more than one but limited, print "UP TO" message
- */
- default: fprintf( option_usage_fp, zUpTo, pOD->optMaxCt ); break;
- }
- break;
- default:
- /*
- * More than one is required. Print the range.
- */
- fprintf( option_usage_fp, zMust, pOD->optMinCt, pOD->optMaxCt );
- }
- if ( NAMED_OPTS( pOptions )
- && (pOptions->specOptIdx.default_opt == pOD->optIndex))
- fputs( zDefaultOpt, option_usage_fp );
- }
- static void
- printBareUsage(
- tOptions* pOptions,
- tOptDesc* pOD,
- arg_types_t* pAT )
- {
- /*
- * Flag prefix: IF no flags at all, then omit it. If not printable
- * (not allowed for this option), then blank, else print it.
- * Follow it with a comma if we are doing GNU usage and long
- * opts are to be printed too.
- */
- if ((pOptions->fOptSet & OPTPROC_SHORTOPT) == 0)
- fputs( pAT->pzSpc, option_usage_fp );
- else if (! isgraph( pOD->optValue)) {
- if ( (pOptions->fOptSet & (OPTPROC_GNUUSAGE|OPTPROC_LONGOPT))
- == (OPTPROC_GNUUSAGE|OPTPROC_LONGOPT))
- fputc( ' ', option_usage_fp );
- fputs( pAT->pzNoF, option_usage_fp );
- } else {
- fprintf( option_usage_fp, " -%c", pOD->optValue );
- if ( (pOptions->fOptSet & (OPTPROC_GNUUSAGE|OPTPROC_LONGOPT))
- == (OPTPROC_GNUUSAGE|OPTPROC_LONGOPT))
- fputs( ", ", option_usage_fp );
- }
- {
- char z[ 80 ];
- tCC* pzArgType;
- /*
- * Determine the argument type string first on its usage, then,
- * when the option argument is required, base the type string on the
- * argument type.
- */
- if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_NONE) {
- pzArgType = pAT->pzNo;
- } else if (pOD->fOptState & OPTST_ARG_OPTIONAL) {
- pzArgType = pAT->pzOpt;
- } else switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
- case OPARG_TYPE_ENUMERATION: pzArgType = pAT->pzKey; break;
- case OPARG_TYPE_MEMBERSHIP: pzArgType = pAT->pzKeyL; break;
- case OPARG_TYPE_BOOLEAN: pzArgType = pAT->pzBool; break;
- case OPARG_TYPE_NUMERIC: pzArgType = pAT->pzNum; break;
- case OPARG_TYPE_HIERARCHY: pzArgType = pAT->pzNest; break;
- case OPARG_TYPE_STRING: pzArgType = pAT->pzStr; break;
- default: goto bogus_desc; break;
- }
- snprintf( z, sizeof(z), pAT->pzOptFmt, pzArgType, pOD->pz_Name,
- (pOD->optMinCt != 0) ? pAT->pzReq : pAT->pzOpt );
- fprintf( option_usage_fp, zOptFmtLine, z, pOD->pzText );
- switch (OPTST_GET_ARGTYPE(pOD->fOptState)) {
- case OPARG_TYPE_ENUMERATION:
- case OPARG_TYPE_MEMBERSHIP:
- displayEnum |= (pOD->pOptProc != NULL) ? AG_TRUE : AG_FALSE;
- }
- }
- return;
- bogus_desc:
- fprintf( stderr, zInvalOptDesc, pOD->pz_Name );
- exit( EXIT_FAILURE );
- }
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- *
- * OPTION LINE FORMATTING SETUP
- *
- * The "OptFmt" formats receive three arguments:
- * 1. the type of the option's argument
- * 2. the long name of the option
- * 3. "YES" or "no ", depending on whether or not the option must appear
- * on the command line.
- * These formats are used immediately after the option flag (if used) has
- * been printed.
- */
- static void
- setStdOptFmts( tOptions* pOpts, tCC** ppT )
- {
- int flen = 0;
- argTypes.pzStr = zStdStrArg;
- argTypes.pzReq = zStdReqArg;
- argTypes.pzNum = zStdNumArg;
- argTypes.pzKey = zStdKeyArg;
- argTypes.pzKeyL = zStdKeyLArg;
- argTypes.pzBool = zStdBoolArg;
- argTypes.pzNest = zStdNestArg;
- argTypes.pzOpt = zStdOptArg;
- argTypes.pzNo = zStdNoArg;
- argTypes.pzBrk = zStdBreak;
- argTypes.pzNoF = zFiveSpaces;
- argTypes.pzSpc = zTwoSpaces;
- switch (pOpts->fOptSet & (OPTPROC_NO_REQ_OPT | OPTPROC_SHORTOPT)) {
- case (OPTPROC_NO_REQ_OPT | OPTPROC_SHORTOPT):
- *ppT = zNoRq_ShrtTtl;
- argTypes.pzOptFmt = zNrmOptFmt;
- flen = 19;
- break;
- case OPTPROC_NO_REQ_OPT:
- *ppT = zNoRq_NoShrtTtl;
- argTypes.pzOptFmt = zNrmOptFmt;
- flen = 19;
- break;
- case OPTPROC_SHORTOPT:
- *ppT = zReq_ShrtTtl;
- argTypes.pzOptFmt = zReqOptFmt;
- flen = 24;
- break;
- case 0:
- *ppT = zReq_NoShrtTtl;
- argTypes.pzOptFmt = zReqOptFmt;
- flen = 24;
- }
- sprintf( zOptFmtLine, zFmtFmt, flen );
- }
- static void
- setGnuOptFmts( tOptions* pOpts, tCC** ppT )
- {
- int flen = 22;
- *ppT = zNoRq_ShrtTtl;
- argTypes.pzStr = zGnuStrArg;
- argTypes.pzReq = zOneSpace;
- argTypes.pzNum = zGnuNumArg;
- argTypes.pzKey = zGnuKeyArg;
- argTypes.pzKeyL = zGnuKeyLArg;
- argTypes.pzBool = zGnuBoolArg;
- argTypes.pzNest = zGnuNestArg;
- argTypes.pzOpt = zGnuOptArg;
- argTypes.pzNo = zOneSpace;
- argTypes.pzBrk = zGnuBreak;
- argTypes.pzNoF = zSixSpaces;
- argTypes.pzSpc = zThreeSpaces;
- switch (pOpts->fOptSet & OPTPROC_L_N_S) {
- case OPTPROC_L_N_S: argTypes.pzOptFmt = zGnuOptFmt; break;
- case OPTPROC_LONGOPT: argTypes.pzOptFmt = zGnuOptFmt; break;
- case 0: argTypes.pzOptFmt = zGnuOptFmt + 2; break;
- case OPTPROC_SHORTOPT:
- argTypes.pzOptFmt = zShrtGnuOptFmt;
- zGnuStrArg[0] = zGnuNumArg[0] = zGnuKeyArg[0] = zGnuBoolArg[0] = ' ';
- argTypes.pzOpt = " [arg]";
- flen = 8;
- break;
- }
- sprintf( zOptFmtLine, zFmtFmt, flen );
- }
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- *
- * Figure out where all the initialization files might live.
- * This requires translating some environment variables and
- * testing to see if a name is a directory or a file. It's
- * squishy, but important to tell users how to find these files.
- */
- static void
- printInitList(
- tCC** papz,
- ag_bool* pInitIntro,
- tCC* pzRc,
- tCC* pzPN )
- {
- char zPath[ MAXPATHLEN+1 ];
- if (papz == NULL)
- return;
- fputs( zPresetIntro, option_usage_fp );
- *pInitIntro = AG_FALSE;
- for (;;) {
- const char* pzPath = *(papz++);
- if (pzPath == NULL)
- break;
- if (optionMakePath( zPath, sizeof( zPath ), pzPath, pzPN ))
- pzPath = zPath;
- /*
- * Print the name of the "homerc" file. If the "rcfile" name is
- * not empty, we may or may not print that, too...
- */
- fprintf( option_usage_fp, zPathFmt, pzPath );
- if (*pzRc != NUL) {
- struct stat sb;
- /*
- * IF the "homerc" file is a directory,
- * then append the "rcfile" name.
- */
- if ( (stat( pzPath, &sb ) == 0)
- && S_ISDIR( sb.st_mode ) ) {
- fputc( '/', option_usage_fp );
- fputs( pzRc, option_usage_fp );
- }
- }
- fputc( '\n', option_usage_fp );
- }
- }
- /*
- * Local Variables:
- * mode: C
- * c-file-style: "stroustrup"
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- * end of autoopts/usage.c */
|