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/rc.h>
32
33 #include <assert.h>
34 #include <stdio.h>
35
36 // TODO: provide short ones
37 #define ALIAS_AMD64 NULL
38 #define OPTION_AMD64 "amd64"
39 static const char* USAGE_AMD64[] = { "require tool to be run under 64-bit environment", NULL };
40
41 #define ALIAS_RAM NULL
42 #define OPTION_RAM "ram"
43 static const char* USAGE_RAM[] = { "require system RAM to be at least B", NULL };
44
45 OptDef Options[] =
46 { /* needs_value, required */
47 { OPTION_AMD64, ALIAS_AMD64, NULL, USAGE_AMD64, 1, false, false }
48 , { OPTION_RAM , ALIAS_RAM , NULL, USAGE_RAM , 1, true , false }
49 };
50
KAppVersion(void)51 ver_t CC KAppVersion ( void )
52 {
53 return 0;
54 }
55
56 const char UsageDefaultName[] = "test-env";
57
UsageSummary(const char * progname)58 rc_t CC UsageSummary ( const char *progname )
59 {
60 return KOutMsg ( "\n"
61 "Usage:\n"
62 " %s [Options]\n"
63 "\n"
64 "Summary:\n"
65 " Test system environment.\n"
66 , progname
67 );
68 }
69
Usage(const Args * args)70 rc_t CC Usage ( const Args *args )
71 {
72 const char * progname = UsageDefaultName;
73 const char * fullpath = UsageDefaultName;
74 rc_t rc;
75
76 if (args == NULL)
77 rc = RC (rcApp, rcArgv, rcAccessing, rcSelf, rcNull);
78 else
79 rc = ArgsProgram (args, &fullpath, &progname);
80 if (rc)
81 progname = fullpath = UsageDefaultName;
82
83 UsageSummary (progname);
84
85 KOutMsg ("Options:\n");
86
87 HelpOptionLine (ALIAS_AMD64, OPTION_AMD64, NULL, USAGE_AMD64);
88 HelpOptionLine (ALIAS_RAM , OPTION_RAM , NULL, USAGE_RAM);
89 KOutMsg ("\n");
90
91 HelpOptionsStandard ();
92 HelpVersion ( fullpath, KAppVersion () );
93
94 return rc;
95 }
96
97
KMain(int argc,char * argv[])98 rc_t CC KMain ( int argc, char *argv [] )
99 {
100 Args *args;
101 rc_t rc;
102 do {
103 uint32_t paramc;
104 bool requireAmd64 = false;
105 uint64_t requireRam = 0;
106
107 rc = ArgsMakeAndHandle ( & args, argc, argv, 1,
108 Options, sizeof Options / sizeof (OptDef) );
109 if ( rc != 0 )
110 {
111 LogErr ( klogErr, rc, "failed to parse arguments" );
112 break;
113 }
114
115 // TODO: cehck if we need it
116 rc = ArgsParamCount ( args, & paramc );
117 if ( rc != 0 ) {
118 LogErr ( klogInt, rc, "failed to obtain param count" );
119 break;
120 }
121
122 { // OPTION_AMD64
123 rc = ArgsOptionCount( args, OPTION_AMD64, & paramc );
124 if ( rc ) {
125 LOGERR( klogErr, rc, "Failure to get '" OPTION_AMD64 "' argument" );
126 break;
127 }
128 if ( paramc ) {
129 requireAmd64 = true;
130 }
131 }
132
133 {
134 // OPTION_RAM
135 rc = ArgsOptionCount ( args, OPTION_RAM, & paramc );
136 if ( rc ) {
137 LOGERR ( klogErr, rc, "Failure to get '" OPTION_RAM "' argument" );
138 break;
139 }
140 if ( paramc ) {
141 long long unsigned int sscanf_param;
142
143 const char* dummy = NULL;
144 rc = ArgsOptionValue(args, OPTION_RAM, 0, (const void **)&dummy);
145 if ( rc ) {
146 LOGERR(klogErr, rc, "Failure to get '" OPTION_RAM "' argument");
147 break;
148 }
149
150 rc = sscanf(dummy, "%llu", &sscanf_param);
151 requireRam = ( uint64_t ) sscanf_param;
152 if ( rc != 1)
153 {
154 LOGMSG(klogErr, "Failure to parse '" OPTION_RAM "' argument value");
155 break;
156 }
157 }
158
159 }
160
161 {
162 rc = KAppCheckEnvironment(requireAmd64, requireRam);
163 if (rc != 0 )
164 printf("Invalid environment\n");
165 else
166 printf("Enviroment is fine!\n");
167 }
168 } while (false);
169
170 ArgsWhack ( args );
171
172 return rc;
173 }
174