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: Try a full (base->local to remote) merge */
27 /* merge = */ (char**)0,
29 /***** -help: show usage information */
31 /***** -version: show program version */
32 /* show_versionP = */ 0,
33 /***** uninterpreted rest of command line */
35 /* argv = */ (char**)0,
36 /***** the original command line concatenated */
42 /***** let LCLint run more smoothly */
47 /******************************************************************/
49 This is a bit tricky. We want to make a difference between overflow
50 and underflow and we want to allow v==Inf or v==-Inf but not
53 We don't use fabs to avoid linkage with -lm.
56 checkFloatConversion(double v
, char *option
, char *arg
)
60 if( (errno
==ERANGE
&& v
!=0.0) /* even double overflowed */
61 || (v
<HUGE_VAL
&& v
>-HUGE_VAL
&& (v
<0.0?-v
:v
)>(double)FLT_MAX
) ) {
63 } else if( (errno
==ERANGE
&& v
==0.0)
64 || (v
!=0.0 && (v
<0.0?-v
:v
)<(double)FLT_MIN
) ) {
69 "%s: parameter `%s' of option `%s' to %s to represent\n",
70 Program
, arg
, option
, err
);
76 getIntOpt(int argc
, char **argv
, int i
, int *value
, int force
)
81 if( ++i
>=argc
) goto nothingFound
;
84 v
= strtol(argv
[i
], &end
, 0);
86 /***** check for conversion error */
87 if( end
==argv
[i
] ) goto nothingFound
;
89 /***** check for surplus non-whitespace */
90 while( isspace((int) *end
) ) end
+=1;
91 if( *end
) goto nothingFound
;
93 /***** check if it fits into an int */
94 if( errno
==ERANGE
|| v
>(long)INT_MAX
|| v
<(long)INT_MIN
) {
96 "%s: parameter `%s' of option `%s' to large to represent\n",
97 Program
, argv
[i
], argv
[i
-1]);
105 if( !force
) return i
-1;
108 "%s: missing or malformed integer value after option `%s'\n",
112 /**********************************************************************/
115 getIntOpts(int argc
, char **argv
, int i
,
119 We want to find at least cmin values and at most cmax values.
120 cmax==-1 then means infinitely many are allowed.
126 if( i
+cmin
>= argc
) {
128 "%s: option `%s' wants at least %d parameters\n",
129 Program
, argv
[i
], cmin
);
134 alloc a bit more than cmin values. It does not hurt to have room
135 for a bit more values than cmax.
138 *values
= (int*)calloc((size_t)alloced
, sizeof(int));
142 "%s: out of memory while parsing option `%s'\n",
147 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
148 if( used
==alloced
) {
150 *values
= (int *) realloc(*values
, alloced
*sizeof(int));
151 if( !*values
) goto outMem
;
155 v
= strtol(argv
[used
+i
+1], &end
, 0);
157 /***** check for conversion error */
158 if( end
==argv
[used
+i
+1] ) break;
160 /***** check for surplus non-whitespace */
161 while( isspace((int) *end
) ) end
+=1;
164 /***** check for overflow */
165 if( errno
==ERANGE
|| v
>(long)INT_MAX
|| v
<(long)INT_MIN
) {
167 "%s: parameter `%s' of option `%s' to large to represent\n",
168 Program
, argv
[i
+used
+1], argv
[i
]);
172 (*values
)[used
] = (int)v
;
178 "%s: parameter `%s' of `%s' should be an "
180 Program
, argv
[i
+used
+1], argv
[i
]);
186 /**********************************************************************/
189 getLongOpt(int argc
, char **argv
, int i
, long *value
, int force
)
193 if( ++i
>=argc
) goto nothingFound
;
196 *value
= strtol(argv
[i
], &end
, 0);
198 /***** check for conversion error */
199 if( end
==argv
[i
] ) goto nothingFound
;
201 /***** check for surplus non-whitespace */
202 while( isspace((int) *end
) ) end
+=1;
203 if( *end
) goto nothingFound
;
205 /***** check for overflow */
206 if( errno
==ERANGE
) {
208 "%s: parameter `%s' of option `%s' to large to represent\n",
209 Program
, argv
[i
], argv
[i
-1]);
215 /***** !force means: this parameter may be missing.*/
216 if( !force
) return i
-1;
219 "%s: missing or malformed value after option `%s'\n",
223 /**********************************************************************/
226 getLongOpts(int argc
, char **argv
, int i
,
230 We want to find at least cmin values and at most cmax values.
231 cmax==-1 then means infinitely many are allowed.
237 if( i
+cmin
>= argc
) {
239 "%s: option `%s' wants at least %d parameters\n",
240 Program
, argv
[i
], cmin
);
245 alloc a bit more than cmin values. It does not hurt to have room
246 for a bit more values than cmax.
249 *values
= (long int *)calloc((size_t)alloced
, sizeof(long));
253 "%s: out of memory while parsing option `%s'\n",
258 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
259 if( used
==alloced
) {
261 *values
= (long int*) realloc(*values
, alloced
*sizeof(long));
262 if( !*values
) goto outMem
;
266 (*values
)[used
] = strtol(argv
[used
+i
+1], &end
, 0);
268 /***** check for conversion error */
269 if( end
==argv
[used
+i
+1] ) break;
271 /***** check for surplus non-whitespace */
272 while( isspace((int) *end
) ) end
+=1;
275 /***** check for overflow */
276 if( errno
==ERANGE
) {
278 "%s: parameter `%s' of option `%s' to large to represent\n",
279 Program
, argv
[i
+used
+1], argv
[i
]);
287 "%s: parameter `%s' of `%s' should be an "
289 Program
, argv
[i
+used
+1], argv
[i
]);
295 /**********************************************************************/
298 getFloatOpt(int argc
, char **argv
, int i
, float *value
, int force
)
303 if( ++i
>=argc
) goto nothingFound
;
306 v
= strtod(argv
[i
], &end
);
308 /***** check for conversion error */
309 if( end
==argv
[i
] ) goto nothingFound
;
311 /***** check for surplus non-whitespace */
312 while( isspace((int) *end
) ) end
+=1;
313 if( *end
) goto nothingFound
;
315 /***** check for overflow */
316 checkFloatConversion(v
, argv
[i
-1], argv
[i
]);
323 if( !force
) return i
-1;
326 "%s: missing or malformed float value after option `%s'\n",
331 /**********************************************************************/
334 getFloatOpts(int argc
, char **argv
, int i
,
338 We want to find at least cmin values and at most cmax values.
339 cmax==-1 then means infinitely many are allowed.
346 if( i
+cmin
>= argc
) {
348 "%s: option `%s' wants at least %d parameters\n",
349 Program
, argv
[i
], cmin
);
354 alloc a bit more than cmin values.
357 *values
= (float*)calloc((size_t)alloced
, sizeof(float));
361 "%s: out of memory while parsing option `%s'\n",
366 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
367 if( used
==alloced
) {
369 *values
= (float *) realloc(*values
, alloced
*sizeof(float));
370 if( !*values
) goto outMem
;
374 v
= strtod(argv
[used
+i
+1], &end
);
376 /***** check for conversion error */
377 if( end
==argv
[used
+i
+1] ) break;
379 /***** check for surplus non-whitespace */
380 while( isspace((int) *end
) ) end
+=1;
383 /***** check for overflow */
384 checkFloatConversion(v
, argv
[i
], argv
[i
+used
+1]);
386 (*values
)[used
] = (float)v
;
391 "%s: parameter `%s' of `%s' should be a "
392 "floating-point value\n",
393 Program
, argv
[i
+used
+1], argv
[i
]);
399 /**********************************************************************/
402 getDoubleOpt(int argc
, char **argv
, int i
, double *value
, int force
)
406 if( ++i
>=argc
) goto nothingFound
;
409 *value
= strtod(argv
[i
], &end
);
411 /***** check for conversion error */
412 if( end
==argv
[i
] ) goto nothingFound
;
414 /***** check for surplus non-whitespace */
415 while( isspace((int) *end
) ) end
+=1;
416 if( *end
) goto nothingFound
;
418 /***** check for overflow */
419 if( errno
==ERANGE
) {
421 "%s: parameter `%s' of option `%s' to %s to represent\n",
422 Program
, argv
[i
], argv
[i
-1],
423 (*value
==0.0 ? "small" : "large"));
430 if( !force
) return i
-1;
433 "%s: missing or malformed value after option `%s'\n",
438 /**********************************************************************/
441 getDoubleOpts(int argc
, char **argv
, int i
,
445 We want to find at least cmin values and at most cmax values.
446 cmax==-1 then means infinitely many are allowed.
452 if( i
+cmin
>= argc
) {
454 "%s: option `%s' wants at least %d parameters\n",
455 Program
, argv
[i
], cmin
);
460 alloc a bit more than cmin values.
463 *values
= (double*)calloc((size_t)alloced
, sizeof(double));
467 "%s: out of memory while parsing option `%s'\n",
472 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
473 if( used
==alloced
) {
475 *values
= (double *) realloc(*values
, alloced
*sizeof(double));
476 if( !*values
) goto outMem
;
480 (*values
)[used
] = strtod(argv
[used
+i
+1], &end
);
482 /***** check for conversion error */
483 if( end
==argv
[used
+i
+1] ) break;
485 /***** check for surplus non-whitespace */
486 while( isspace((int) *end
) ) end
+=1;
489 /***** check for overflow */
490 if( errno
==ERANGE
) {
492 "%s: parameter `%s' of option `%s' to %s to represent\n",
493 Program
, argv
[i
+used
+1], argv
[i
],
494 ((*values
)[used
]==0.0 ? "small" : "large"));
502 "%s: parameter `%s' of `%s' should be a "
504 Program
, argv
[i
+used
+1], argv
[i
]);
510 /**********************************************************************/
513 force will be set if we need at least one argument for the option.
516 getStringOpt(int argc
, char **argv
, int i
, char **value
, int force
)
521 fprintf(stderr
, "%s: missing string after option `%s'\n",
528 if( !force
&& argv
[i
][0] == '-' ) return i
-1;
532 /**********************************************************************/
535 getStringOpts(int argc
, char **argv
, int i
,
539 We want to find at least cmin values and at most cmax values.
540 cmax==-1 then means infinitely many are allowed.
545 if( i
+cmin
>= argc
) {
547 "%s: option `%s' wants at least %d parameters\n",
548 Program
, argv
[i
], cmin
);
554 *values
= (char**)calloc((size_t)alloced
, sizeof(char*));
558 "%s: out of memory during parsing of option `%s'\n",
563 for(used
=0; (cmax
==-1 || used
<cmax
) && used
+i
+1<argc
; used
++) {
564 if( used
==alloced
) {
566 *values
= (char **)realloc(*values
, alloced
*sizeof(char*));
567 if( !*values
) goto outMem
;
570 if( used
>=cmin
&& argv
[used
+i
+1][0]=='-' ) break;
571 (*values
)[used
] = argv
[used
+i
+1];
576 "%s: less than %d parameters for option `%s', only %d found\n",
577 Program
, cmin
, argv
[i
], used
);
583 /**********************************************************************/
586 checkIntLower(char *opt
, int *values
, int count
, int max
)
590 for(i
=0; i
<count
; i
++) {
591 if( values
[i
]<=max
) continue;
593 "%s: parameter %d of option `%s' greater than max=%d\n",
594 Program
, i
+1, opt
, max
);
598 /**********************************************************************/
601 checkIntHigher(char *opt
, int *values
, int count
, int min
)
605 for(i
=0; i
<count
; i
++) {
606 if( values
[i
]>=min
) continue;
608 "%s: parameter %d of option `%s' smaller than min=%d\n",
609 Program
, i
+1, opt
, min
);
613 /**********************************************************************/
616 checkLongLower(char *opt
, long *values
, int count
, long max
)
620 for(i
=0; i
<count
; i
++) {
621 if( values
[i
]<=max
) continue;
623 "%s: parameter %d of option `%s' greater than max=%ld\n",
624 Program
, i
+1, opt
, max
);
628 /**********************************************************************/
631 checkLongHigher(char *opt
, long *values
, int count
, long min
)
635 for(i
=0; i
<count
; i
++) {
636 if( values
[i
]>=min
) continue;
638 "%s: parameter %d of option `%s' smaller than min=%ld\n",
639 Program
, i
+1, opt
, min
);
643 /**********************************************************************/
646 checkFloatLower(char *opt
, float *values
, int count
, float max
)
650 for(i
=0; i
<count
; i
++) {
651 if( values
[i
]<=max
) continue;
653 "%s: parameter %d of option `%s' greater than max=%f\n",
654 Program
, i
+1, opt
, max
);
658 /**********************************************************************/
661 checkFloatHigher(char *opt
, float *values
, int count
, float min
)
665 for(i
=0; i
<count
; i
++) {
666 if( values
[i
]>=min
) continue;
668 "%s: parameter %d of option `%s' smaller than min=%f\n",
669 Program
, i
+1, opt
, min
);
673 /**********************************************************************/
676 checkDoubleLower(char *opt
, double *values
, int count
, double max
)
680 for(i
=0; i
<count
; i
++) {
681 if( values
[i
]<=max
) continue;
683 "%s: parameter %d of option `%s' greater than max=%f\n",
684 Program
, i
+1, opt
, max
);
688 /**********************************************************************/
691 checkDoubleHigher(char *opt
, double *values
, int count
, double min
)
695 for(i
=0; i
<count
; i
++) {
696 if( values
[i
]>=min
) continue;
698 "%s: parameter %d of option `%s' smaller than min=%f\n",
699 Program
, i
+1, opt
, min
);
703 /**********************************************************************/
706 catArgv(int argc
, char **argv
)
712 for(i
=0, l
=0; i
<argc
; i
++) l
+= (1+strlen(argv
[i
]));
713 s
= (char *)malloc(l
);
715 fprintf(stderr
, "%s: out of memory\n", Program
);
720 for(i
=1; i
<argc
; i
++) {
727 /**********************************************************************/
732 fprintf(stderr
,"%s"," [-blr merge] [-help] [-version]\n");
733 fprintf(stderr
,"%s"," merges files looking for similar patterns, and allows several plugins\n");
734 fprintf(stderr
,"%s"," for understanding the syntax of several files \n");
735 fprintf(stderr
,"%s"," -blr: Try a full (base->local to remote) merge\n");
736 fprintf(stderr
,"%s"," 3 char* values\n");
737 fprintf(stderr
,"%s"," -help: show usage information\n");
738 fprintf(stderr
,"%s"," -version: show program version\n");
739 fprintf(stderr
,"%s"," version: 0.1.0\n");
740 fprintf(stderr
,"%s"," ");
743 /**********************************************************************/
745 parseCmdline(int argc
, char **argv
)
750 cmd
.tool
= catArgv(argc
, argv
);
751 for(i
=1, cmd
.argc
=1; i
<argc
; i
++) {
752 if( 0==strcmp("-blr", argv
[i
]) ) {
755 i
= getStringOpts(argc
, argv
, i
, &cmd
.merge
, 3, 3);
760 if( 0==strcmp("-help", argv
[i
]) ) {
765 if( 0==strcmp("-version", argv
[i
]) ) {
766 cmd
.show_versionP
= 1;
770 if( argv
[i
][0]=='-' ) {
771 fprintf(stderr
, "\n%s: unknown option `%s'\n\n",
775 argv
[cmd
.argc
++] = argv
[i
];
785 fprintf(stderr
, "%s: There are %d arguments not associated with any option\n",
789 /*@-compmempass*/ return &cmd
;