-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathttgetopt.c
2429 lines (2160 loc) · 61 KB
/
ttgetopt.c
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999, 2021, 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
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "ttgetopt.h"
#include "tt_version.h"
#define TTC_VERSION_MAJOR1 TTMAJOR1
#define TTC_VERSION_MAJOR2 TTMAJOR2
#define TTC_VERSION_MAJOR3 TTMAJOR3
#define TTC_VERSION_PATCH TTPATCH
#define TTC_VERSION_PORTPATCH TTPORTPATCH
#ifdef _WIN32
# define WINDOWS 1
#endif
#ifdef WINDOWS
# include <windows.h>
# define strncasecmp _strnicmp
# define strcasecmp _stricmp
# define snprintf _snprintf
# define vsnprintf _vsnprintf
#else
# include <unistd.h>
#endif
/* Info about options stored in list of the following structures. */
typedef struct _Option {
char* option; /* the option string */
char* option_orig; /* the original option string (before lower-case) */
int argType; /* i for int, s for string, 0 for nothing, ... */
void* argPtr; /* where to store option values */
int argLen; /* length of buffer or array */
int numArgs; /* number in array so far */
int* numArgs_P; /* ptr to user's number in array so far */
int strlen; /* length of opt */
void* callback_arg; /* option to be sent to the callback function */
struct _Option *next; /* ptr to next option in list */
} Option;
typedef struct {
Option *head, *tail; /* ptrs to first/last options */
Ttc_getopt_options options; /* mask of options */
char* errmsgbuf; /* error messages stored here */
int errmsgbuflen; /* size of errmsgbuf */
const char* cmdname; /* from argv[0] or filename */
Option **sorted; /* indexes sorted by option name,
* normal options only */
int n_sorted; /* number of items in sorted */
Option* dsn_opt; /* set if see <DSN> */
Option* connstr_opt; /* set if see <CONNSTR> */
int got_dsn; /* set if got -dsn */
int got_connstr; /* set if got -connstr */
int (*str_eq)(const char*,const char*);
int (*str_cmp)(const char*,const char*);
} OptionList;
static int getOptions(const int argc, char* const argv[],
Ttc_getopt_options options,
char* errmsgbuf, int errmsgbuflen,
const char* cmdname,
va_list ap);
static void init_errmsg(char* errmsgbuf, int errmsgbuflen);
static int getArgsFromFile(const char *fileName,
int *pargc, char ***pargv,
char* errmsgbuf, int errmsgbuflen, int do_print);
static int getArgsFromStr(const char *str, const char* name,
int *pargc, char ***pargv,
char* errmsgbuf, int errmsgbuflen, int do_print);
static int str_equal(const char* a, const char* b);
static int str_equal_ignore(const char* a, const char* b);
static int looks_like_dsn(const char* opt);
static int looks_like_connstr(const char* opt);
/****************************************************************/
/****************************************************************/
/**** Entry point functions. ****/
/****************************************************************/
/****************************************************************/
/**
* Extract the command name from argv0 (argv[0]),
* removing any leading path components,
* the leading underscore,
* and any trailing 'Cmd' or '.exe'.
*
* @param argv0 argv[0]
* @param buf Destination buffer.
* @param buflen Size of destination buffer.
*
* @return pointer to buf.
*/
TTC_DllExport const char*
ttc_getcmdname(const char* argv0, char* buf, int buflen)
{
const char* cmd;
int len;
char* trailer;
int trailer_len;
/* try to get the basename */
#ifdef WINDOWS
cmd = strrchr(argv0, '\\');
if (cmd == NULL) {
cmd = strrchr(argv0, '/');
}
#else
cmd = strrchr(argv0, '/');
#endif
if (cmd == NULL) {
cmd = argv0;
} else {
++cmd;
}
if (*cmd == '_')
++cmd;
len = strlen(cmd);
/* strip off trailing .exe or Cmd */
#ifdef WINDOWS
trailer = ".exe";
#else
trailer = "Cmd";
#endif
trailer_len = strlen(trailer);
if ( (len > trailer_len) &&
str_equal_ignore(cmd+len-trailer_len, trailer) ) {
if (buflen >= len-(trailer_len-1)) {
strncpy(buf, cmd, len-trailer_len);
buf[len-trailer_len] = '\0';
cmd = buf;
} else {
cmd = argv0;
}
} else {
if (buflen > len) {
strcpy (buf, cmd);
cmd = buf;
} else {
cmd = argv0;
}
}
return cmd;
} /* ttc_getcmdname */
#ifndef PRODUCT_NAME
# define PRODUCT_NAME "TimesTen"
#endif
/**
* Return a consistent form of the version name,
* looking like 'TimesTen Release 5.6.7'.
* Fills the specified buffer up to the specified length.
*
* @param buf Destination buffer.
* @param buflen Size of destination buffer.
*
* @return pointer to buf.
*/
TTC_DllExport const char*
ttc_get_version_name(char* buf, int buflen)
{
snprintf(buf, buflen,
"%s Release %d.%d.%d.%d.%d",
PRODUCT_NAME,
TTC_VERSION_MAJOR1, TTC_VERSION_MAJOR2, TTC_VERSION_MAJOR3,
TTC_VERSION_PATCH, TTC_VERSION_PORTPATCH);
return buf;
} /* ttc_get_version_name */
/**
* Parse the given argv list according to the given specifications.
*
* @param argc Argument count.
* @param argv Argument list.
* @param options Mask of TTC_OPT_ values.
* @param errmsgbuf Buffer for error messages.
* @param errmsgbuflen Size of errmsgbuf.
*
* @return Index of the next argument after the options arguments,
* or -1 for errors.
*
* @see ttc_vgetoptions()
* @see ttc_filegetoptions()
* @see ttc_strgetoptions()
*/
TTC_DllExport int
ttc_getoptions(int argc, char* const argv[],
Ttc_getopt_options options,
char* errmsgbuf, int errmsgbuflen,
...)
{
va_list ap;
int n;
char cmdbuf[80];
const char* cmd = ttc_getcmdname(argv[0], cmdbuf, sizeof(cmdbuf));
char* tthome;
va_start(ap, errmsgbuflen);
tthome = getenv("TIMESTEN_HOME");
if (tthome == NULL) {
strcpy(errmsgbuf, "TIMESTEN_HOME environment variable not set");
return -1;
}
init_errmsg(errmsgbuf, errmsgbuflen);
n = getOptions(argc, argv, options, errmsgbuf, errmsgbuflen, cmd, ap);
va_end(ap);
return n;
} /* ttc_getoptions */
/**
* Parse the given argv list according to the given specifications.
* This version takes a va_list for the specifications.
*
* @param argc Argument count.
* @param argv Argument list.
* @param options Mask of TTC_OPT_ values.
* @param errmsgbuf Buffer for error messages.
* @param errmsgbuflen Size of errmsgbuf.
* @param ap va_list of specifications.
*
* @return Index of the next argument after the options arguments,
* or -1 for errors.
* @see ttc_getoptions()
*/
TTC_DllExport int
ttc_vgetoptions(int argc, char* const argv[],
Ttc_getopt_options options,
char* errmsgbuf, int errmsgbuflen,
va_list ap)
{
int n;
char cmdbuf[80];
const char* cmd = ttc_getcmdname(argv[0], cmdbuf, sizeof(cmdbuf));
init_errmsg(errmsgbuf, errmsgbuflen);
n = getOptions(argc, argv, options, errmsgbuf, errmsgbuflen, cmd, ap);
return n;
} /* ttc_vgetoptions */
/**
* Parse the given argv list according to the given specifications.
* This version takes the argv list from a file,
* for instance, ttendaemon.options.
* The routine allocates an argv list, which the user
* should maintain during argument handling. The user
* should call ttc_filegetoptions_free() when the argv list
* is no longer needed.
*
* @param filename Name of input file. If NULL, stdin is used.
* @param pargc Argument count (output parameter).
* @param pargv Argument list (output parameter).
* @param options Mask of TTC_OPT_ values.
* @param errmsgbuf Buffer for error messages.
* @param errmsgbuflen Size of errmsgbuf.
*
* @return Index of the next argument after the options arguments,
* or -1 for errors.
* @see ttc_getoptions()
* @see ttc_filegetoptions_free()
*/
TTC_DllExport int
ttc_filegetoptions(const char* filename,
int* pargc, char*** pargv,
Ttc_getopt_options options,
char* errmsgbuf, int errmsgbuflen,
...)
{
va_list ap;
int n;
int argc;
char** argv;
init_errmsg(errmsgbuf, errmsgbuflen);
if (! getArgsFromFile(filename, &argc, &argv, errmsgbuf, errmsgbuflen,
options & TTC_OPT_PRINT_ERRS)) {
return -1;
}
va_start(ap, errmsgbuflen);
n = getOptions(argc, argv, options, errmsgbuf, errmsgbuflen,
filename ? filename : "<stdin>",
ap);
va_end(ap);
if (pargc != NULL) *pargc = argc;
if (pargv == NULL) {
/* free argv, don't need it anymore */
ttc_filegetoptions_free(argv);
} else {
*pargv = argv;
}
return n;
} /* ttc_filegetoptions */
/**
* Parse the given argv list according to the given specifications.
* This version takes the argv list from a string,
* for instance, ttendaemon.options.
* The routine allocates an argv list, which the user
* should maintain during argument handling. The user
* should call ttc_strgetoptions_free() when the argv list
* is no longer needed.
*
* @param str String containing options.
* @param name Name to print as cmd name in error msgs.
* @param pargc Argument count (output parameter).
* @param pargv Argument list (output parameter).
* @param options Mask of TTC_OPT_ values.
* @param errmsgbuf Buffer for error messages.
* @param errmsgbuflen Size of errmsgbuf.
*
* @return Index of the next argument after the options arguments,
* or -1 for errors.
* @see ttc_getoptions()
* @see ttc_strgetoptions_free()
*/
TTC_DllExport int
ttc_strgetoptions(const char* str, const char* name,
int* pargc, char*** pargv,
Ttc_getopt_options options,
char* errmsgbuf, int errmsgbuflen,
...)
{
va_list ap;
int n;
int argc;
char** argv;
init_errmsg(errmsgbuf, errmsgbuflen);
if (! getArgsFromStr(str, name, &argc, &argv, errmsgbuf, errmsgbuflen,
options & TTC_OPT_PRINT_ERRS)) {
return -1;
}
va_start(ap, errmsgbuflen);
n = getOptions(argc, argv, options, errmsgbuf, errmsgbuflen, name, ap);
va_end(ap);
if (pargc != NULL) *pargc = argc;
if (pargv == NULL) {
/* free argv, don't need it anymore */
ttc_filegetoptions_free(argv);
} else {
*pargv = argv;
}
return n;
} /* ttc_strgetoptions */
/**
* Free any storage allocated by ttc_filegetoptions().
* The caller must be sure that they don't access
* any of this storage again after this.
*
* @param argv The argument list allocated by ttc_filegetoptions().
* @see ttc_filegetoptions()
*/
TTC_DllExport void
ttc_filegetoptions_free(char** argv)
{
if (argv != NULL) {
char** av;
for (av = argv; *av != NULL; ++av) {
free((void*)*av);
}
free((void*)argv);
}
} /* ttc_filegetoptions_free */
/**
* Free any storage allocated by ttc_strgetoptions().
* The caller must be sure that they don't access
* any of this storage again after this.
*
* @param argv The argument list allocated by ttc_strgetoptions().
* @see ttc_strgetoptions()
*/
TTC_DllExport void
ttc_strgetoptions_free(char** argv)
{
if (argv != NULL) {
/* argv[0] string we copied */
free((void*)argv[0]);
/* also free the argv list itself */
free((void*)argv);
}
} /* ttc_strgetoptions_free */
/**
* Return a true value if the argument looks like a DSN.
*
* @param str String to check.
*/
TTC_DllExport int
ttc_opt_looks_like_dsn(const char* str)
{
return looks_like_dsn(str);
} /* ttc_opt_looks_like_dsn */
/**
* Return a true value if the argument looks like a connection string.
*
* @param str String to check.
*/
TTC_DllExport int
ttc_opt_looks_like_connstr(const char* str)
{
return looks_like_connstr(str);
} /* ttc_opt_looks_like_connstr */
/****************************************************************/
/****************************************************************/
/**** General utility routines. ****/
/****************************************************************/
/****************************************************************/
/* Initially clear the error message buffer. */
static void
init_errmsg(char* errmsgbuf, int errmsgbuflen)
{
if ( (errmsgbuf != NULL) && (errmsgbuflen > 0) ) {
*errmsgbuf = '\0';
}
} /* init_errmsg */
/* Put error message into the given buffer. */
static void
pr_error(char* errmsgbuf, int errmsgbuflen, int do_print,
const char* fmt, va_list ap)
{
if (errmsgbuf != NULL) {
int len;
len = strlen(errmsgbuf);
if (len > 0) {
snprintf(errmsgbuf + len, errmsgbuflen - len, "\n");
len = strlen(errmsgbuf);
}
vsnprintf(errmsgbuf + len, errmsgbuflen - len, fmt, ap);
if (do_print) {
fprintf(stderr, "%s", errmsgbuf + len); /* ap no longer valid at this point */
fputs("\n", stderr);
}
}
else {
if (do_print) {
vfprintf(stderr, fmt, ap);
fputs("\n", stderr);
}
}
} /* pr_error */
/* Put error message into the given buffer. */
static void
msgbuf_error(char* errmsgbuf, int errmsgbuflen, int do_print,
const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pr_error(errmsgbuf, errmsgbuflen, do_print, fmt, ap);
va_end(ap);
} /* msgbuf_error */
/*
* Stick an error message into the buffer the user specified
* for that purpose. Return false so the function can be called
* like: return opt_error(ol, ...)
*/
static int
opt_error(OptionList* ol, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pr_error(ol->errmsgbuf, ol->errmsgbuflen, ol->options & TTC_OPT_PRINT_ERRS, fmt, ap);
va_end(ap);
return 0;
} /* opt_error */
/*
* str_equal - return true value if both pointers are non-null
* and they point to identical strings.
*/
static int
str_equal(const char* a, const char* b)
{
return (a != NULL) && (b != NULL) && (strcmp(a,b) == 0);
} /* str_equal */
/*
* str_equal_ignore - return true value if both pointers are non-null
* and they point to identical strings, ignoring case.
*/
static int
str_equal_ignore(const char* a, const char* b)
{
return (a != NULL) && (b != NULL) && (strcasecmp(a,b) == 0);
} /* str_equal_ignore */
/****************************************************************/
/****************************************************************/
/**** Option list functions. ****/
/****************************************************************/
/****************************************************************/
/* Initialize the list of options. */
static void
init_option_list(OptionList *ol, Ttc_getopt_options options,
char* errmsgbuf, int errmsgbuflen,
const char* cmdname)
{
ol->head = ol->tail = NULL;
ol->options = options;
ol->errmsgbuf = errmsgbuf;
ol->errmsgbuflen = errmsgbuflen;
ol->cmdname = cmdname;
ol->sorted = NULL;
ol->str_eq = (options & TTC_OPT_CASE_SENSITIVE) ? str_equal : str_equal_ignore;
ol->str_cmp = (options & TTC_OPT_CASE_SENSITIVE) ? strcmp : strcasecmp;
ol->dsn_opt = ol->connstr_opt = NULL;
ol->got_dsn = ol->got_connstr = 0;
} /* init_option_list */
/* Make lower-case duplicate of string. */
static char*
str_lc_dup(const char* src)
{
const char* p;
char* q;
char* dst = (char*)malloc(strlen(src)+1);
if (dst == NULL) {
return NULL;
}
p = src;
q = dst;
while (*p) {
if (isupper(*p)) {
*q++ = tolower(*p++);
} else {
*q++ = *p++;
}
}
*q++ = '\0';
return dst;
} /* str_lc_dup */
/*
* Insert new option info into list.
* Must be inserted in order, so precedence is kept straight.
*/
static int
add_option_to_list(OptionList* ol,
char* opt,
int argType,
void* arg,
int len,
int* num_P,
void* callback_arg)
{
Option* newOpt;
for (newOpt = ol->head; newOpt != NULL; newOpt = newOpt->next) {
if (str_equal(newOpt->option, opt)) {
opt_error(ol, "%s: option '%s' is a duplicate",
ol->cmdname, opt);
free(opt);
return 0;
}
}
/*
* MALLOC - it will be freed in free_opt_list.
*/
newOpt = (Option*)malloc(sizeof(Option));
if (newOpt == NULL) {
return opt_error(ol, "%s: Out of memory allocating %d bytes.", ol->cmdname, sizeof(Option));
}
newOpt->option_orig = opt;
if ( (ol->options & TTC_OPT_CASE_SENSITIVE) || (strlen(opt) == 1) ) {
newOpt->option = newOpt->option_orig;
} else {
newOpt->option = str_lc_dup(opt);
if (newOpt->option == NULL) {
opt_error(ol, "%s: Out of memory copying %d bytes.",
ol->cmdname, strlen(opt));
free(newOpt); /* Parfait-detected memory leak */
return 0;
}
}
newOpt->argType = argType;
newOpt->argPtr = arg;
newOpt->argLen = len;
newOpt->numArgs = 0;
newOpt->numArgs_P = num_P;
if (num_P != NULL) *num_P = 0;
newOpt->callback_arg = callback_arg;
newOpt->strlen = strlen(opt);
newOpt->next = NULL;
if (ol->tail) {
ol->tail->next = newOpt;
}
ol->tail = newOpt;
if (ol->head == NULL) {
ol->head = newOpt;
}
if (str_equal(opt, "<DSN>")) ol->dsn_opt = newOpt;
else if (str_equal(opt, "<CONNSTR>")) ol->connstr_opt = newOpt;
return 1;
} /* add_option_to_list */
/* Separate optionname/type and add to list. */
static int
add_option(OptionList* ol,
char* opt,
void* arg,
char* sep,
int len,
int* num_P,
void* callback_arg)
{
char arg_type;
if (sep == NULL) {
if (str_equal(opt, "<DSN>") || str_equal(opt, "<CONNSTR>")) {
/* implicit =s for DSN and CONNSTR */
arg_type = 's';
} else if (str_equal(opt, "<HELPFUNC>") || str_equal(opt, "<VERSFUNC>")) {
/* implicit =x for HELPFUNC and VERSFUNC */
arg_type = 'x';
} else {
int slen = strlen(opt);
if (opt[slen-1] == '!') {
arg_type = '!';
opt[slen-1] = '\0';
} else {
arg_type = '\0';
}
}
} else {
arg_type = *(sep+1);
*sep = '\0';
}
return add_option_to_list (ol, opt, arg_type, arg, len, num_P, callback_arg);
} /* add_option */
/* case-sensitive compare routine for qsort */
static int
sortcmp_sens(const void* a, const void* b)
{
/* a and b are actually Option** */
Option* oa = *(Option**)a;
Option* ob = *(Option**)b;
return strcmp(oa->option, ob->option);
} /* sortcmp_sens */
/*
* Make sorted list for use when matching abbreviated options.
*/
static int
make_sorted(OptionList* ol)
{
int n;
Option* opt;
/* see how many there are */
for (opt = ol->head, n = 0; opt != NULL; opt = opt->next) {
if (*(opt->option) != '<') {
++n;
}
}
/* Initialize sorted array */
ol->n_sorted = n;
if (n == 0) {
n = 1; /* some platforms (aix) don't like malloc of 0 bytes */
}
ol->sorted = (Option**)malloc(n * sizeof(Option*));
if (ol->sorted == NULL) {
return opt_error(ol, "%s: Out of memory allocating %d*%d bytes.",
ol->cmdname, n, sizeof(Option*));
}
for (opt = ol->head, n = 0; opt != NULL; opt = opt->next) {
if (*(opt->option) != '<') {
ol->sorted[n++] = opt;
}
}
/* sort them */
qsort(ol->sorted, n, sizeof(Option*), sortcmp_sens);
return 1;
} /* make_sorted */
/*
* Add all given option specifiers to the options list.
*/
static int
add_options(OptionList* ol, va_list ap)
{
for (;;) {
char* next_opt; /* next option in the va_list */
char* opt; /* copy of next option */
char* arg; /* usually a ptr to variable */
int len = 0; /* for string type, length of buffer,
* for array type, length of array */
char* sep; /* location of the '=' */
int* num_P = NULL; /* where to put number of array vals stored */
void* callback_arg = NULL; /* argument for some callbacks */
next_opt = (char*)va_arg(ap, char*);
if (next_opt == NULL) {
/* reached the end */
break;
}
/*
* MALLOC
* Some options may come from variables, others are string constants,
* others might be stack local storage.
* Since we don't know which it is, and we want to be able to clean
* up everything later, we malloc a copy here. It will be freed
* in free_opt_list.
*/
opt = strdup(next_opt);
if (opt == NULL) {
return opt_error(ol, "%s: Out of memory duping '%s'.", ol->cmdname, next_opt);
}
arg = (void*)va_arg(ap, void*);
sep = strchr(opt, '=');
if (sep != NULL) {
char* p = sep + 1;
/* string and array options take a length */
if (str_equal(p, "I") || str_equal(p, "D") || str_equal(p, "s") || str_equal(p, "S") || str_equal(p, "z")) {
len = (int)va_arg(ap, int);
}
/* array options take a ptr to a var to hold number of values */
/* 'z' type also gets a pointer to an int argument */
if (str_equal(p, "I") || str_equal(p, "D") || str_equal(p, "S") || str_equal(p, "z")) {
num_P = (int*)va_arg(ap, int*);
}
/* 't' type gets a value to be passed along */
if (str_equal(p, "t")) {
callback_arg = (void*)va_arg(ap, void*);
}
} else if (str_equal(opt, "<DSN>") || str_equal(opt, "<CONNSTR>")) {
/* <DSN> and <CONNSTR> have an implicit =s */
len = (int)va_arg(ap, int);
}
if (! add_option(ol, opt, arg, sep, len, num_P, callback_arg)) {
return 0;
}
}
if (! make_sorted(ol)) {
return 0;
}
return 1;
} /* add_options */
/* Free storage for the option list. */
static void
free_opt_list(OptionList *ol)
{
Option *o, *next;
for (o=ol->head; o != NULL; o = next) {
next = o->next;
if (o->option) {
if (o->option_orig != o->option) {
free(o->option_orig);
}
free(o->option);
}
free((void*)o);
}
ol->head = ol->tail = NULL;
if (ol->sorted != NULL) {
free((void*)ol->sorted);
}
} /* free_opt_list */
/*
* Make sure it looked numeric (based on the strto* output),
* and has no trailing junk. Return true value if OK.
*/
static int
chk_numeric_arg(OptionList* ol, char* p, const char* optstr, const char* opt,
int quiet)
{
#ifndef _WIN32
if (errno == ERANGE) {
if (! quiet) {
opt_error(ol, "%s: option %s: numeric argument '%s' out of range\n",
ol->cmdname, opt, optstr);
}
return 0;
}
#endif /* not for Windows */
if (p == optstr) {
if (! quiet) {
opt_error(ol, "%s: option %s: bad numeric argument '%s'\n",
ol->cmdname, opt, optstr);
}
return 0;
}
if (*p != '\0') {
if (! quiet) {
opt_error(ol, "%s: option %s: trailing garbage in numeric argument '%s'\n",
ol->cmdname, opt, optstr);
}
return 0;
}
return 1;
} /* chk_numeric_arg */
/*
* Handle a numeric option.
*/
static int
get_numeric_arg(OptionList* ol, Option* o, const char* opt, int* idxP,
int argc, char* const * argv, const char* optarg)
{
const char* optstr;
char* p;
if ( (*idxP >= argc) && !optarg) {
return opt_error(ol, "%s: option %s missing argument\n", ol->cmdname, opt);
}
optstr = optarg ? optarg : argv[*idxP];
switch (o->argType) {
case 'i':
{
errno = 0;
int i = (int)strtol(optstr, &p, 0);
if (! chk_numeric_arg(ol, p, optstr, opt, 0)) return 0;
if (o->argPtr != NULL) {
*(int*)o->argPtr = i;
}
break;
}
case 'u':
{
unsigned int u;
errno = 0;
u = (unsigned int)strtoul(optstr, &p, 0);
if (! chk_numeric_arg(ol, p, optstr, opt, 0)) return 0;
if (o->argPtr != NULL) {
*(unsigned int*)o->argPtr = u;
}
break;
}
case 'l':
{
long i;
errno = 0;
i = strtol(optstr, &p, 0);
if (! chk_numeric_arg(ol, p, optstr, opt, 0)) return 0;
if (o->argPtr != NULL) {
*(long*)o->argPtr = i;
}
break;
}
case 'd':
{
double d;
errno = 0;
d = strtod(optstr, &p);
if (! chk_numeric_arg(ol, p, optstr, opt, 0)) return 0;
if (o->argPtr != NULL) {
*(double*)o->argPtr = d;
}
break;
}
default:
return opt_error(ol, "%s: bad numeric option type '%c'", ol->cmdname, o->argType);
}
if (optarg == NULL) {
++*idxP;
}
return 1;
} /* get_numeric_arg */
/*
* Handle an int (=I) array option.
*/
static int
get_int_array_arg(OptionList* ol, Option* o, const char* opt, int* idxP,
int argc, char* const * argv)
{
const char* optstr;
char* p;
int n_added = 0;
int n;
int rc;
if (*idxP >= argc) {
return opt_error(ol, "%s: option %s missing argument\n", ol->cmdname, opt);
}
for (;;) {
if (*idxP >= argc) {
/* at least one must have been found */
rc = 1;
break;
}
optstr = argv[*idxP];
errno = 0;
n = (int)strtol(optstr, &p, 0);
if (! chk_numeric_arg(ol, p, optstr, opt, 1)) {
/* If none have been found, it is an error. */
if (n_added == 0) {
opt_error(ol, "%s: No arguments to %s", ol->cmdname, o->option_orig);
rc = 0;
} else {
rc = 1;
}
rc = (n_added > 0);
break;
}
/* got another numeric arg, add it */
if (o->argPtr != NULL) {
int* arr;
/*
* o->argPtr points to an array of ints,
* o->argLen is the size of the array
* o->numArgs is the number there so far
*/
if (o->numArgs >= o->argLen) {
opt_error(ol, "%s: too many %s options specified",