2 command line parser -- generated by clig
3 (http://wsd.iitb.fhg.de/~kir/clighome/)
5 The command line parser `clig':
6 (C) 1995-2004 Harald Kirsch (clig@geggus.net)
24 static Cmdline cmd
= {
25 /***** --blr: <base-filename> <local-filename> <remote-filename>
26 Try a full (base->local to remote) merge */
27 /* do_blrmergeP = */ 0,
28 /* do_blrmerge = */ (char**)0,
29 /* do_blrmergeC = */ 0,
30 /***** --hashcmp: <line1> <line2> ... <lineN>
31 Do a hash compare between 4 or more lines. */
32 /* do_hashcmpP = */ 0,
33 /* do_hashcmp = */ (char**)0,
34 /* do_hashcmpC = */ 0,
35 /***** --test-plugin: try to load the split-lines plugin */
36 /* test_pluginP = */ 0,
37 /***** --help: show usage information */
39 /***** uninterpreted rest of command line */
41 /* argv = */ (char**)0,
42 /***** the original command line concatenated */
48 /***** let LCLint run more smoothly */
53 /******************************************************************/
55 This is a bit tricky. We want to make a difference between overflow
56 and underflow and we want to allow v==Inf or v==-Inf but not
59 We don't use fabs to avoid linkage with -lm.
62 checkFloatConversion(double v
, char *option
, char *arg
)
66 if( (errno
==ERANGE
&& v
!=0.0) /* even double overflowed */
67 || (v
<HUGE_VAL
&& v
>-HUGE_VAL
&& (v
<0.0?-v
:v
)>(double)FLT_MAX
) ) {
69 } else if( (errno
==ERANGE
&& v
==0.0)
70 || (v
!=0.0 && (v
<0.0?-v
:v
)<(double)FLT_MIN
) ) {
75 "%s: parameter `%s' of option `%s' to %s to represent\n",
76 Program
, arg
, option
, err
);
82 getIntOpt(int argc
, char **argv
, int i
, int *value
, int force
)
87 if( ++i
>=argc
) goto nothingFound
;
90 v
= strtol(argv
[i
], &end
, 0);
92 /***** check for conversion error */
93 if( end
==argv
[i
] ) goto nothingFound
;
95 /***** check for surplus non-whitespace */
96 while( isspace((int) *end
) ) end
+=1;
97 if( *end
) goto nothingFound
;
99 /***** check if it fits into an int */
100 if( errno
==ERANGE
|| v
>(long)INT_MAX
|| v
<(long)INT_MIN
) {
102 "%s: parameter `%s' of option `%s' to large to represent\n",
103 Program
, argv
[i
], argv
[i
-1]);
111 if( !force
) return i
-1;
114 "%s: missing or malformed integer value after option `%s'\n",
118 /**********************************************************************/
121 getIntOpts(int argc
, char **argv
, int i
,
125 We want to find at least cmin values and at most cmax values.
126 cmax==-1 then means infinitely many are allowed.
132 if( i
+cmin
>= argc
) {
134 "%s: option `%s' wants at least %d parameters\n",
135 Program
, argv
[i
], cmin
);
140 alloc a bit more than cmin values. It does not hurt to have room
141 for a bit more values than cmax.
144 *values
= (int*)calloc((size_t)alloced
, sizeof(int));
148 "%s: out of memory while parsing option `%s'\n",
153 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
154 if( used
==alloced
) {
156 *values
= (int *) realloc(*values
, alloced
*sizeof(int));
157 if( !*values
) goto outMem
;
161 v
= strtol(argv
[used
+i
+1], &end
, 0);
163 /***** check for conversion error */
164 if( end
==argv
[used
+i
+1] ) break;
166 /***** check for surplus non-whitespace */
167 while( isspace((int) *end
) ) end
+=1;
170 /***** check for overflow */
171 if( errno
==ERANGE
|| v
>(long)INT_MAX
|| v
<(long)INT_MIN
) {
173 "%s: parameter `%s' of option `%s' to large to represent\n",
174 Program
, argv
[i
+used
+1], argv
[i
]);
178 (*values
)[used
] = (int)v
;
184 "%s: parameter `%s' of `%s' should be an "
186 Program
, argv
[i
+used
+1], argv
[i
]);
192 /**********************************************************************/
195 getLongOpt(int argc
, char **argv
, int i
, long *value
, int force
)
199 if( ++i
>=argc
) goto nothingFound
;
202 *value
= strtol(argv
[i
], &end
, 0);
204 /***** check for conversion error */
205 if( end
==argv
[i
] ) goto nothingFound
;
207 /***** check for surplus non-whitespace */
208 while( isspace((int) *end
) ) end
+=1;
209 if( *end
) goto nothingFound
;
211 /***** check for overflow */
212 if( errno
==ERANGE
) {
214 "%s: parameter `%s' of option `%s' to large to represent\n",
215 Program
, argv
[i
], argv
[i
-1]);
221 /***** !force means: this parameter may be missing.*/
222 if( !force
) return i
-1;
225 "%s: missing or malformed value after option `%s'\n",
229 /**********************************************************************/
232 getLongOpts(int argc
, char **argv
, int i
,
236 We want to find at least cmin values and at most cmax values.
237 cmax==-1 then means infinitely many are allowed.
243 if( i
+cmin
>= argc
) {
245 "%s: option `%s' wants at least %d parameters\n",
246 Program
, argv
[i
], cmin
);
251 alloc a bit more than cmin values. It does not hurt to have room
252 for a bit more values than cmax.
255 *values
= (long int *)calloc((size_t)alloced
, sizeof(long));
259 "%s: out of memory while parsing option `%s'\n",
264 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
265 if( used
==alloced
) {
267 *values
= (long int*) realloc(*values
, alloced
*sizeof(long));
268 if( !*values
) goto outMem
;
272 (*values
)[used
] = strtol(argv
[used
+i
+1], &end
, 0);
274 /***** check for conversion error */
275 if( end
==argv
[used
+i
+1] ) break;
277 /***** check for surplus non-whitespace */
278 while( isspace((int) *end
) ) end
+=1;
281 /***** check for overflow */
282 if( errno
==ERANGE
) {
284 "%s: parameter `%s' of option `%s' to large to represent\n",
285 Program
, argv
[i
+used
+1], argv
[i
]);
293 "%s: parameter `%s' of `%s' should be an "
295 Program
, argv
[i
+used
+1], argv
[i
]);
301 /**********************************************************************/
304 getFloatOpt(int argc
, char **argv
, int i
, float *value
, int force
)
309 if( ++i
>=argc
) goto nothingFound
;
312 v
= strtod(argv
[i
], &end
);
314 /***** check for conversion error */
315 if( end
==argv
[i
] ) goto nothingFound
;
317 /***** check for surplus non-whitespace */
318 while( isspace((int) *end
) ) end
+=1;
319 if( *end
) goto nothingFound
;
321 /***** check for overflow */
322 checkFloatConversion(v
, argv
[i
-1], argv
[i
]);
329 if( !force
) return i
-1;
332 "%s: missing or malformed float value after option `%s'\n",
337 /**********************************************************************/
340 getFloatOpts(int argc
, char **argv
, int i
,
344 We want to find at least cmin values and at most cmax values.
345 cmax==-1 then means infinitely many are allowed.
352 if( i
+cmin
>= argc
) {
354 "%s: option `%s' wants at least %d parameters\n",
355 Program
, argv
[i
], cmin
);
360 alloc a bit more than cmin values.
363 *values
= (float*)calloc((size_t)alloced
, sizeof(float));
367 "%s: out of memory while parsing option `%s'\n",
372 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
373 if( used
==alloced
) {
375 *values
= (float *) realloc(*values
, alloced
*sizeof(float));
376 if( !*values
) goto outMem
;
380 v
= strtod(argv
[used
+i
+1], &end
);
382 /***** check for conversion error */
383 if( end
==argv
[used
+i
+1] ) break;
385 /***** check for surplus non-whitespace */
386 while( isspace((int) *end
) ) end
+=1;
389 /***** check for overflow */
390 checkFloatConversion(v
, argv
[i
], argv
[i
+used
+1]);
392 (*values
)[used
] = (float)v
;
397 "%s: parameter `%s' of `%s' should be a "
398 "floating-point value\n",
399 Program
, argv
[i
+used
+1], argv
[i
]);
405 /**********************************************************************/
408 getDoubleOpt(int argc
, char **argv
, int i
, double *value
, int force
)
412 if( ++i
>=argc
) goto nothingFound
;
415 *value
= strtod(argv
[i
], &end
);
417 /***** check for conversion error */
418 if( end
==argv
[i
] ) goto nothingFound
;
420 /***** check for surplus non-whitespace */
421 while( isspace((int) *end
) ) end
+=1;
422 if( *end
) goto nothingFound
;
424 /***** check for overflow */
425 if( errno
==ERANGE
) {
427 "%s: parameter `%s' of option `%s' to %s to represent\n",
428 Program
, argv
[i
], argv
[i
-1],
429 (*value
==0.0 ? "small" : "large"));
436 if( !force
) return i
-1;
439 "%s: missing or malformed value after option `%s'\n",
444 /**********************************************************************/
447 getDoubleOpts(int argc
, char **argv
, int i
,
451 We want to find at least cmin values and at most cmax values.
452 cmax==-1 then means infinitely many are allowed.
458 if( i
+cmin
>= argc
) {
460 "%s: option `%s' wants at least %d parameters\n",
461 Program
, argv
[i
], cmin
);
466 alloc a bit more than cmin values.
469 *values
= (double*)calloc((size_t)alloced
, sizeof(double));
473 "%s: out of memory while parsing option `%s'\n",
478 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
479 if( used
==alloced
) {
481 *values
= (double *) realloc(*values
, alloced
*sizeof(double));
482 if( !*values
) goto outMem
;
486 (*values
)[used
] = strtod(argv
[used
+i
+1], &end
);
488 /***** check for conversion error */
489 if( end
==argv
[used
+i
+1] ) break;
491 /***** check for surplus non-whitespace */
492 while( isspace((int) *end
) ) end
+=1;
495 /***** check for overflow */
496 if( errno
==ERANGE
) {
498 "%s: parameter `%s' of option `%s' to %s to represent\n",
499 Program
, argv
[i
+used
+1], argv
[i
],
500 ((*values
)[used
]==0.0 ? "small" : "large"));
508 "%s: parameter `%s' of `%s' should be a "
510 Program
, argv
[i
+used
+1], argv
[i
]);
516 /**********************************************************************/
519 force will be set if we need at least one argument for the option.
522 getStringOpt(int argc
, char **argv
, int i
, char **value
, int force
)
527 fprintf(stderr
, "%s: missing string after option `%s'\n",
534 if( !force
&& argv
[i
][0] == '-' ) return i
-1;
538 /**********************************************************************/
541 getStringOpts(int argc
, char **argv
, int i
,
545 We want to find at least cmin values and at most cmax values.
546 cmax==-1 then means infinitely many are allowed.
551 if( i
+cmin
>= argc
) {
553 "%s: option `%s' wants at least %d parameters\n",
554 Program
, argv
[i
], cmin
);
560 *values
= (char**)calloc((size_t)alloced
, sizeof(char*));
564 "%s: out of memory during parsing of option `%s'\n",
569 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
570 if( used
==alloced
) {
572 *values
= (char **)realloc(*values
, alloced
*sizeof(char*));
573 if( !*values
) goto outMem
;
576 if( used
>=cmin
&& argv
[used
+i
+1][0]=='-' ) break;
577 (*values
)[used
] = argv
[used
+i
+1];
582 "%s: less than %d parameters for option `%s', only %d found\n",
583 Program
, cmin
, argv
[i
], used
);
589 /**********************************************************************/
592 checkIntLower(char *opt
, int *values
, int count
, int max
)
596 for(i
=0; i
<count
; i
++) {
597 if( values
[i
]<=max
) continue;
599 "%s: parameter %d of option `%s' greater than max=%d\n",
600 Program
, i
+1, opt
, max
);
604 /**********************************************************************/
607 checkIntHigher(char *opt
, int *values
, int count
, int min
)
611 for(i
=0; i
<count
; i
++) {
612 if( values
[i
]>=min
) continue;
614 "%s: parameter %d of option `%s' smaller than min=%d\n",
615 Program
, i
+1, opt
, min
);
619 /**********************************************************************/
622 checkLongLower(char *opt
, long *values
, int count
, long max
)
626 for(i
=0; i
<count
; i
++) {
627 if( values
[i
]<=max
) continue;
629 "%s: parameter %d of option `%s' greater than max=%ld\n",
630 Program
, i
+1, opt
, max
);
634 /**********************************************************************/
637 checkLongHigher(char *opt
, long *values
, int count
, long min
)
641 for(i
=0; i
<count
; i
++) {
642 if( values
[i
]>=min
) continue;
644 "%s: parameter %d of option `%s' smaller than min=%ld\n",
645 Program
, i
+1, opt
, min
);
649 /**********************************************************************/
652 checkFloatLower(char *opt
, float *values
, int count
, float max
)
656 for(i
=0; i
<count
; i
++) {
657 if( values
[i
]<=max
) continue;
659 "%s: parameter %d of option `%s' greater than max=%f\n",
660 Program
, i
+1, opt
, max
);
664 /**********************************************************************/
667 checkFloatHigher(char *opt
, float *values
, int count
, float min
)
671 for(i
=0; i
<count
; i
++) {
672 if( values
[i
]>=min
) continue;
674 "%s: parameter %d of option `%s' smaller than min=%f\n",
675 Program
, i
+1, opt
, min
);
679 /**********************************************************************/
682 checkDoubleLower(char *opt
, double *values
, int count
, double max
)
686 for(i
=0; i
<count
; i
++) {
687 if( values
[i
]<=max
) continue;
689 "%s: parameter %d of option `%s' greater than max=%f\n",
690 Program
, i
+1, opt
, max
);
694 /**********************************************************************/
697 checkDoubleHigher(char *opt
, double *values
, int count
, double min
)
701 for(i
=0; i
<count
; i
++) {
702 if( values
[i
]>=min
) continue;
704 "%s: parameter %d of option `%s' smaller than min=%f\n",
705 Program
, i
+1, opt
, min
);
709 /**********************************************************************/
712 catArgv(int argc
, char **argv
)
718 for(i
=0, l
=0; i
<argc
; i
++) l
+= (1+strlen(argv
[i
]));
719 s
= (char *)malloc(l
);
721 fprintf(stderr
, "%s: out of memory\n", Program
);
726 for(i
=1; i
<argc
; i
++) {
733 /**********************************************************************/
738 fprintf(stderr
,"%s"," [--blr do_blrmerge] [--hashcmp do_hashcmp] [--test-plugin] [--help]\n");
739 fprintf(stderr
,"%s"," merges files looking for similar patterns, and allows several plugins\n");
740 fprintf(stderr
,"%s"," for understanding the syntax of several files \n");
741 fprintf(stderr
,"%s"," --blr: <base-filename> <local-filename> <remote-filename>\n");
742 fprintf(stderr
,"%s"," Try a full (base->local to remote) merge\n");
743 fprintf(stderr
,"%s"," 3 char* values\n");
744 fprintf(stderr
,"%s"," --hashcmp: <line1> <line2> ... <lineN>\n");
745 fprintf(stderr
,"%s"," Do a hash compare between 4 or more lines.\n");
746 fprintf(stderr
,"%s"," 4 or more char* values\n");
747 fprintf(stderr
,"%s"," --test-plugin: try to load the split-lines plugin\n");
748 fprintf(stderr
,"%s"," --help: show usage information\n");
749 fprintf(stderr
,"%s"," version: 0.1.1\n");
750 fprintf(stderr
,"%s"," ");
753 /**********************************************************************/
755 parseCmdline(int argc
, char **argv
)
760 cmd
.tool
= catArgv(argc
, argv
);
761 for(i
=1, cmd
.argc
=1; i
<argc
; i
++) {
762 if( 0==strcmp("--blr", argv
[i
]) ) {
764 cmd
.do_blrmergeP
= 1;
765 i
= getStringOpts(argc
, argv
, i
, &cmd
.do_blrmerge
, 3, 3);
766 cmd
.do_blrmergeC
= i
-keep
;
770 if( 0==strcmp("--hashcmp", argv
[i
]) ) {
773 i
= getStringOpts(argc
, argv
, i
, &cmd
.do_hashcmp
, 4, -1);
774 cmd
.do_hashcmpC
= i
-keep
;
778 if( 0==strcmp("--test-plugin", argv
[i
]) ) {
779 cmd
.test_pluginP
= 1;
783 if( 0==strcmp("--help", argv
[i
]) ) {
788 if( argv
[i
][0]=='-' ) {
789 fprintf(stderr
, "\n%s: unknown option `%s'\n\n",
793 argv
[cmd
.argc
++] = argv
[i
];
803 fprintf(stderr
, "%s: There are %d arguments not associated with any option\n",
807 /*@-compmempass*/ return &cmd
;