-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtestprog_utils.cpp
450 lines (391 loc) · 10.5 KB
/
testprog_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//////////////////////////////////////////////////////////////////////
//
// Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Universal Permissive License v 1.0 as shown
// at http://oss.oracle.com/licenses/upl
//
// testprog_utils.cpp: routines shared by all of the TTClasses sample programs
//
//////////////////////////////////////////////////////////////////////
#include "testprog_utils.h"
#include <ttclasses/TTInclude.h>
#ifdef _WIN32
# include <sys/timeb.h>
#else
# include <sys/times.h>
# include <sys/time.h>
# include <stdio.h>
#endif /* _WIN32 */
#include <signal.h>
#include "tt_version.h"
#define DSNNAME DEMODSN
/*----------------------------------------------------------------------------*/
/* signal handling stuff */
/*----------------------------------------------------------------------------*/
static int sigReceived;
static int sigReported;
/*
* Signal handling
*/
void
RequestStop(int sig)
{
sigReceived = sig;
sigReported = 0;
}
void
StopRequestClear(void)
{
sigReceived = 0;
sigReported = 0;
}
int
StopRequested(void)
{
if (sigReceived != 0) {
if (!sigReported) {
fprintf(stderr,
"\n*** Stop request received (signal %d). Terminating. ***\n",
sigReceived);
sigReported = 1; /* only print this message once */
}
return 1;
}
return 0;
}
int
SignalReceived(void)
{
return (sigReceived);
}
#ifdef _WIN32
/*
* WIN32's signal is not the BSD signal(), it's the old SYSV signal().
*
* Since SYSV signal() is not really robust, and since signal() is not
* really a native WIN32 API, we use SetConsoleCtrlHandler instead to do
* things the Windows way.
*/
static BOOL WINAPI
ConCtrlHandler(DWORD ctrlType)
{
switch(ctrlType) {
case CTRL_C_EVENT :
RequestStop (SIGINT) ;
return TRUE; /* This "signal" is handled. */
case CTRL_BREAK_EVENT :
RequestStop (SIGBREAK) ;
return TRUE; /* This "signal" is also handled. */
case CTRL_CLOSE_EVENT :
case CTRL_LOGOFF_EVENT :
case CTRL_SHUTDOWN_EVENT :
default:
return FALSE; /* Let someone else handle this */
}
return FALSE;
}
int HandleSignals(void)
{
int rc;
rc = SetConsoleCtrlHandler(ConCtrlHandler, TRUE);
/* Our rc and theirs are inverted */
if (rc) {
return 0;
}
return 1;
}
#else
/*
* Signal handler
* No printf, no malloc / strdup, no longjump
*/
static void
sigHandler(int sig)
{
RequestStop (sig) ;
}
static int
sigHandlerSet(int sig, handler_t handler)
{
struct sigaction new_action;
struct sigaction old_action;
memset(&new_action, 0, sizeof(struct sigaction));
new_action.sa_handler = handler;
/* Temporarily block all other signals during handler execution */
sigfillset (&new_action.sa_mask);
/* Auto restart syscalls after handler return */
#if !defined(SB_P_OS_LYNX)
new_action.sa_flags = SA_RESTART;
#endif /* Not on Lynx */
if (sigaction(sig, NULL, &old_action) != 0) {
return -1;
}
/* Honor inherited SIG_IGN that's set by some shell's */
if (old_action.sa_handler != SIG_IGN) {
if (sigaction(sig, &new_action, NULL) != 0) {
return -1;
}
}
return 0;
}
int HandleSignals(void)
{
if (sigHandlerSet(SIGINT, (handler_t)sigHandler) != 0) {
perror("Unable to set signal handler for SIGINT");
return 1;
}
if (sigHandlerSet(SIGTERM, (handler_t)sigHandler) != 0) {
perror("Unable to set signal handler for SIGTERM");
return 1;
}
if (sigHandlerSet(SIGHUP, (handler_t)sigHandler) != 0) {
perror("Unable to set signal handler for SIGHUP");
return 1;
}
if (sigHandlerSet(SIGPIPE, (handler_t)sigHandler) != 0) {
perror("Unable to set signal handler for SIGPIPE");
return 1;
}
return 0;
}
#endif /* Handling ctrl-c on Windows and Unix */
// ---------------------------------------------------------------------
// class ParamParser -- parameter parsing stuff
// ---------------------------------------------------------------------
ParamParser::ParamParser(const char * usage_str) : _howmany(0)
{
strncpy(usage_string, usage_str, 10240-1);
for (int i = 0 ; i < MAX_CMDLINE_ARGUMENTS ; i++)
arg_value[i] = NULL ;
}
ParamParser::~ParamParser()
{
for (int i = 0; i < _howmany; i++) {
delete [] arg_value[i];
}
}
void
ParamParser::setArg(const char * arglabel, bool required, int len)
{
if (_howmany == MAX_CMDLINE_ARGUMENTS) {
cerr << "Too many possible arguments specified." << endl ;
cerr << "Increase the value of the MAX_CMDLINE_ARGUMENTS global" << endl ;
cerr << "in file testprog_utils.h and recompile." << endl ;
return;
}
strncpy(label[_howmany], arglabel, MAX_CMDLINE_LABEL_LEN);
reqd [_howmany] = required;
maxlen[_howmany] = len;
twinof[_howmany] = -1;
if (len > 0) {
arg_value[_howmany] = new char [len];
} else {
arg_value[_howmany] = new char [1];
}
*(arg_value[_howmany]) = '\0';
_howmany++;
}
void
ParamParser::setTwinArg(const char * origArg, const char * twinArg)
{
if (_howmany == MAX_CMDLINE_ARGUMENTS) {
cerr << "Too many possible arguments specified." << endl ;
cerr << "Increase the value of the MAX_CMDLINE_ARGUMENTS global" << endl ;
cerr << "in file testprog_utils.h and recompile." << endl ;
return;
}
for (int i = 0 ; i < _howmany; i++)
{
if (!tt_strcasecmp(origArg,label[i])) {
strncpy(label[_howmany], twinArg, MAX_CMDLINE_LABEL_LEN);
reqd [_howmany] = false;
maxlen[_howmany] = 1;
twinof[_howmany] = i ; // says that "twinArg" should be treated same as "origArg"
arg_value[_howmany] = NULL; // delete [] NULL is OK
_howmany++;
return;
}
}
cerr << "*** Bad use of ParamParser::setTwinArg():" << endl ;
cerr << "*** No such arg label registered: '" << origArg << "'" << endl ;
}
void
ParamParser::usage(const char * prog, bool err)
{
const char *progname;
/* Get the name of the program (sans path). */
progname = (const char *)strrchr(prog, '/');
if (progname != NULL) {
++progname;
}
else {
#ifdef _WIN32
progname = strrchr(prog, '\\');
if (progname != NULL)
++progname;
else
#endif /* _WIN32 */
progname = prog;
}
(err ? cerr : cout) << endl <<
"Usage: " << endl <<
" " << progname << " [-h | -help | -?]" << endl <<
" " << progname << " [-V | -version]" << endl <<
usage_string <<
" -h, -help, -? Print this message and exit." << endl <<
" -V, -version Print product version number and exit." << endl <<
" <DSN> DSN to connect to." << endl <<
" -connstr <connstr> Connection string to be used." << endl;
if (err)
exit(-2);
}
void
ParamParser::processArgs(int argc, char * argv[], char * conn_string)
{
int i = 1;
int argnum = -1;
while (i < argc)
{
if (!tt_strcasecmp(argv[i], "-h") || !tt_strcasecmp(argv[i], "-help") ||
!strcmp(argv[i], "?")) {
usage(argv[0], false);
exit(0);
}
else if (!strcmp(argv[i], "-V") || !tt_strcasecmp(argv[i], "-version")) {
cout << TTVERSION_STRING << endl;
exit(0);
}
else if (!tt_strcasecmp(argv[i], "-connStr")) {
if (argc < i+2 ) {
cerr << "*** Parameter '-connStr' specified without argument." << endl ;
usage(argv[0]);
exit(1);
}
strcpy(conn_string, argv[i+1]);
i += 2;
continue;
}
else if (!tt_strcasecmp(argv[i], "-dsn")) {
if (argc < i+2 ) {
cerr << "*** Parameter '-dsn' specified without argument." << endl ;
usage(argv[0]);
exit(1);
}
sprintf(conn_string, "dsn=%s", argv[i+1]);
i += 2;
continue;
}
else if ( (argnum = matchesZeroParamArg(argv[i])) >= 0) {
setArgValue(argnum, argv[i+1]);
i += 1;
}
else if ( (argnum = matchesOneParamArg(argv[i])) >= 0) {
if (argc < i+2 ) {
cerr << "*** Valid parameter '" << argv[i] << "' specified without "
<< "required argument." << endl;
usage(argv[0]);
exit(1);
}
setArgValue(argnum, argv[i+1]);
i += 2;
}
else if ((i == argc-1) && (!*conn_string)) {
// must be a DSN
sprintf(conn_string, "dsn=%s", argv[i]);
i += 2;
continue;
}
else {
cerr << "*** Invalid program argument: '" << argv[i] << "'" << endl ;
usage(argv[0]);
exit(1);
}
}
/* If connection string is not specified, default it */
if (*conn_string == '\0') {
/* Default the DSN and UID */
sprintf(conn_string, "dsn=%s;%s", DSNNAME, UID);
}
/* Verify all required parameters have been supplied */
bool ok_params = true;
for (i = 0 ; i < _howmany; i++)
{
if (reqd[i] && *(arg_value[i]) == '\0') {
cerr << endl << "Required argument '" << label[i] << "' not supplied." << endl;
ok_params = false;
}
}
if (!ok_params) {
// cerr << "Missing some required parameters, aborting." << endl ;
usage(argv[0]) ;
}
}
void
ParamParser::setArgValue(int which, const char * value)
{
if (maxlen[which] > 0)
strncpy(arg_value[which], value, maxlen[which]);
else
*(arg_value[which]) = 'y';
}
int
ParamParser::matchesZeroParamArg(const char * arglabel)
{
int idx = -1;
for (int i = 0 ; i < _howmany; i++)
{
if (!tt_strcasecmp(arglabel,label[i])) {
idx = i;
if (twinof[i] >= 0)
idx = twinof[i];
if (maxlen[idx] == 0) // i.e., no parameter value is supplied with this param
return idx;
}
}
return -1;
}
int
ParamParser::matchesOneParamArg(const char * arglabel)
{
int idx = -1;
for (int i = 0 ; i < _howmany; i++)
{
if (!tt_strcasecmp(arglabel,label[i])) {
idx = i;
if (twinof[i] >= 0)
idx = twinof[i];
if (maxlen[idx] > 0) // i.e., some parameter value is supplied with this param
return idx;
}
}
return -1;
}
bool
ParamParser::argUsed(const char * arglabel)
{
for (int i = 0 ; i < _howmany; i++)
{
if (!tt_strcasecmp(arglabel,label[i])) {
if (*(arg_value[i]) == '\0')
return false;
else
return true;
}
}
cerr << "*** Bad use of ParamParser::argSeen():" << endl ;
cerr << "*** No such arg label registered: '" << arglabel << "'" << endl ;
return false;
}
const char *
ParamParser::getArgValue(const char * arglabel)
{
for (int i = 0 ; i < _howmany; i++)
{
if (!tt_strcasecmp(arglabel,label[i]))
return arg_value[i];
}
cerr << "*** Bad use of ParamParser::getArg():" << endl ;
cerr << "*** No such arg label registered: '" << arglabel << "'" << endl ;
return NULL;
}