1 /*===========================================================================
2 *
3 *                            PUBLIC DOMAIN NOTICE
4 *               National Center for Biotechnology Information
5 *
6 *  This software/database is a "United States Government Work" under the
7 *  terms of the United States Copyright Act.  It was written as part of
8 *  the author's official duties as a United States Government employee and
9 *  thus cannot be copyrighted.  This software/database is freely available
10 *  to the public for use. The National Library of Medicine and the U.S.
11 *  Government have not placed any restriction on its use or reproduction.
12 *
13 *  Although all reasonable efforts have been taken to ensure the accuracy
14 *  and reliability of the software and data, the NLM and the U.S.
15 *  Government do not and cannot warrant the performance or results that
16 *  may be obtained by using this software or data. The NLM and the U.S.
17 *  Government disclaim all warranties, express or implied, including
18 *  warranties of performance, merchantability or fitness for any particular
19 *  purpose.
20 *
21 *  Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26 
27 #include <kapp/main.h>
28 #include <kapp/args.h>
29 #include <klib/log.h>
30 #include <klib/out.h>
31 #include <klib/status.h>
32 #include <klib/rc.h>
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <assert.h>
38 
39 
40 
41 /*--------------------------------------------------------------------------
42  * KMain
43  *  invoked by platform specific "main" entrypoint
44  */
45 const char UsageDefaultName[] = "rcexplain";
46 
47 
UsageSummary(const char * progname)48 rc_t CC UsageSummary (const char * progname)
49 {
50     return KOutMsg ("\n"
51                     "Usage:\n"
52                     "  %s [Options] rc [rc ...]\n"
53                     "\n"
54                     "Summary:\n"
55                     "  Prints out error string to stdout for one or more return codes.\n",
56                     progname);
57 }
58 
Usage(const Args * args)59 rc_t CC Usage (const Args * args)
60 {
61     const char * progname = UsageDefaultName;
62     const char * fullpath = UsageDefaultName;
63     rc_t rc;
64 
65     if (args == NULL)
66         rc = RC (rcApp, rcArgv, rcAccessing, rcSelf, rcNull);
67     else
68         rc = ArgsProgram (args, &fullpath, &progname);
69 
70     UsageSummary (progname);
71 
72     KOutMsg ("Options:\n");
73 
74     HelpOptionsStandard();
75 
76     HelpVersion (fullpath, KAppVersion());
77 
78     return rc;
79 }
80 
81 
KMain(int argc,char * argv[])82 rc_t CC KMain ( int argc, char *argv [] )
83 {
84     rc_t rc = 0;
85     Args * args;
86 
87     rc = ArgsMakeAndHandle (&args, argc, argv, 0);
88     if (rc == 0)
89     {
90         do
91         {
92             uint32_t pcount;
93 
94             rc = ArgsParamCount (args, &pcount);
95             if (rc)
96                 break;
97 
98             if (pcount == 0) {
99                 MiniUsage(args);
100                 rc = RC(rcExe, rcNoTarg, rcAllocating, rcParam, rcInvalid);
101             }
102             else
103             {
104                 const char * pc;
105                 rc_t exp;
106                 uint32_t ix;
107 
108                 /* over rule any set log level */
109                 rc = KLogLevelSet (klogInfo);
110 
111                 for (ix = 0; ix < pcount; ++ix)
112                 {
113                     rc = ArgsParamValue (args, ix, (const void **)&pc);
114                     if (rc)
115                         break;
116 
117                     exp = AsciiToU32 (pc, NULL, NULL);
118                     rc = LOGERR (klogInfo, exp, pc);
119                 }
120             }
121 
122         } while (0);
123 
124         ArgsWhack (args);
125     }
126     return rc;
127 }
128