Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/io/std_io/parse_arg.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00010 /* ======================================================= */
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <alloc.h>
00014 #include <data_io.h>
00015 #include <soft_version.h>
00016 #include <gendef.h>
00017 
00018 #define OPTION_MAXSIZE 25
00019 #define DEFVALUE_MAXSIZE 2048
00020 
00021 int DEBUG = 0;
00022 int VERBOSE = 1;
00023 int TK = 0;
00024 int PROGRESS = 20;
00025 int ASK = 1;
00026 int ASK_BACK = 0;
00027 
00028 char **pt_ArgLabels = NULL;
00029 char **pt_ArgValues = NULL;
00030 
00031 char StandardArg_List[][OPTION_MAXSIZE] = {
00032         "-h",
00033         "-help",
00034         "-inputformat",
00035         "-outputformat",
00036         "-version",
00037         "-debug",
00038         "-tk",
00039         "-quiet",
00040         "-noask"
00041 };                                                              /* list of standard options           */
00042 int  StandardList_Length=9;             /* length of previous list            */
00043 
00044 char **Arg_List;                                /* saved list of user'allowed options */
00045 char **DefVal_List;                             /* saved list of user'allowed options */
00046 int  List_Length;                               /* list length                        */
00047 
00048 char *General_Purpose = NULL;
00049 
00050 extern char soft_version[];
00051 
00052 /* --- Doxygen Function Comment -------------------------- */
00056                 /* @{ ***** Starting group32 ***** */
00066 /* ------------------------------------------------------- */
00067 
00068 int 
00069 set_arglist(char *arg_label)
00070 {
00071         char *pt_buf;
00072         char optlist[2048];
00073         char tmp_string[2048];
00074 
00075         List_Length = 0;
00076 
00077         strcpy(optlist,arg_label);
00078         pt_buf = optlist;
00079         Arg_List = (char **)malloc(sizeof(char *));
00080         DefVal_List = (char **)malloc(sizeof(char *));
00081         if ((Arg_List == (char **)0) || (Arg_List == (char **)0)) {
00082                 Handle_Error("set_arglist : Unable to allocate memory",-1);
00083                 return (-1);
00084         }
00085 
00086         while ( *pt_buf != '\0' ) {
00087                         DefVal_List[List_Length] =
00088                                 (char *)malloc(DEFVALUE_MAXSIZE*sizeof(char));
00089                         if (DefVal_List[List_Length] == (char *)0) {
00090                                 Handle_Error("set_arglist : Unable to allocate memory",-1);
00091                                 return (-1);
00092                         }
00093                                                                                         /* save option label */
00094                         DefVal_List[List_Length][0] = '\0';
00095                         if (sscanf(pt_buf," -%s",tmp_string) != 1) {
00096                 Handle_Error("set_arglist : Syntax error",-1);
00097                                 return(-1);
00098                         }
00099                         Arg_List[List_Length] = 
00100                                 (char *)malloc((strlen(tmp_string)+2)*sizeof(char));
00101                         if (Arg_List[List_Length] == (char *)0) {
00102                                 Handle_Error("set_arglist : Unable to allocate memory",-1);
00103                                 return (-1);
00104                         }
00105                         sprintf(Arg_List[List_Length],"-%s",tmp_string);
00106                         pt_buf = strpbrk(pt_buf,Arg_List[List_Length])
00107                                         + strlen(tmp_string);
00108                         if (*pt_buf != '\0') pt_buf++;
00109 
00110                                                                         /* scan default value until next option */
00111                         while (sscanf(pt_buf," %s",tmp_string) == 1 ) { 
00112                                                                                         
00113                                 if ((tmp_string[0] == '-') && !is_num(tmp_string[1]))
00114                                         break;
00115                                                                                         /* decode default value */
00116                                 strcat(DefVal_List[List_Length],tmp_string);
00117                                 pt_buf = strpbrk(pt_buf,DefVal_List[List_Length])
00118                                         + strlen(tmp_string);
00119                                 if (*pt_buf != '\0') pt_buf++;
00120                                 strcat(DefVal_List[List_Length]," ");
00121                         }
00122                         if (strlen(DefVal_List[List_Length]) != 0)
00123                                 DefVal_List[List_Length][strlen(DefVal_List[List_Length])-1] = '\0';
00124                         else
00125                                 strcpy(DefVal_List[List_Length],"false");
00126                         List_Length++;
00127                         Arg_List = (char **)realloc(Arg_List,
00128                                 (List_Length+1)*sizeof(char *));
00129                         DefVal_List = (char **)realloc(DefVal_List,
00130                                 (List_Length+1)*sizeof(char *));
00131                 }
00132                 return (0);
00133 }
00134 
00135 /* --- Doxygen Function Comment -------------------------- */
00141 /* ------------------------------------------------------- */
00142 
00143 int set_purpose(char *text) {
00144 
00145         General_Purpose = (char *)malloc((strlen(text)+1)*sizeof(char));
00146         strcpy(General_Purpose,text);
00147         return(0);
00148 }
00149 
00150 #define set_arglabel(arglabel) \
00151         strcpy(pt_ArgLabels[j],arglabel);
00152 
00153 #define set_argval(argvalue) \
00154         argvalue[0] = '\0'; \
00155         if (i+1 != argno) { \
00156         if (argv[i+1][0] != '-') {\
00157                         strcpy(argvalue,argv[i+1]); \
00158                         i++; \
00159                 } \
00160                 else { \
00161                         if (is_num(argv[i+1][1])) {\
00162                                 strcpy(argvalue,argv[i+1]); \
00163                                 i++; \
00164                         } \
00165                 } \
00166         }
00167 
00168 extern char Calling_Prog[];
00169 
00170 /* --- Doxygen Function Comment -------------------------- */
00182 /* ------------------------------------------------------- */
00183 
00184 int 
00185 parse_arg(char **argv, int argno, char ***pt_arglabel, char ***pt_argval)
00186 {
00187         int  i, j, k, nb_arg, status;
00188         char format[256], err_text[512],
00189                 *pt_delim, *pt_arg, tmp_arg[DEFVALUE_MAXSIZE];
00190         int set_inputformat(char *), set_outputformat(char *);
00191 
00192         DEBUG = 0;
00193         TK = 0;
00194 
00195         nb_arg = List_Length;
00196         if (nb_arg !=0) {
00197                 alloc2d(pt_arglabel,nb_arg,DEFVALUE_MAXSIZE,CHAR);
00198                 alloc2d(pt_argval,nb_arg,DEFVALUE_MAXSIZE,CHAR);
00199         }
00200         pt_ArgLabels = *pt_arglabel;
00201         pt_ArgValues = *pt_argval;
00202 
00203         for (i=0; i<List_Length;i++) {           /* copy default values */
00204                 strcpy(pt_ArgValues[i],DefVal_List[i]);
00205                 strcpy(pt_ArgLabels[i],Arg_List[i]);
00206         }
00207 
00208         for (i=1; i<argno;i++) {                /* special care for -tk */
00209                 if (strcmp(argv[i],"-tk") == 0) {       /* option specified */
00210                         TK = 1;
00211                 }
00212         }
00213         for (i=1; i<argno;i++) {
00214                 if (argv[i][0] == '-')  {       /* option specified */
00215                                                                         /* is it a standard option ? */
00216                         for (j=0;j<StandardList_Length && 
00217                                 strcmp(StandardArg_List[j],argv[i]) != 0;j++);
00218 
00219                         if (j == StandardList_Length) {         /* non standard option      */
00220                                                                                                 /* is this option allowed ? */
00221                                 for (j=0;j<List_Length;j++) {
00222                                                                                                 /* multiple options */
00223                                         if ((pt_delim = strchr(Arg_List[j],'|')) != NULL) {
00224                                                 pt_arg = &(Arg_List[j][1]);
00225                                                 while (pt_delim != NULL) {
00226                                                         strncpy(tmp_arg,pt_arg,(pt_delim - pt_arg));
00227                                                         tmp_arg[pt_delim - pt_arg] ='\0';
00228                                                         if (strcmp(tmp_arg,&(argv[i][1])) == 0) break;
00229                                                         pt_arg = pt_delim+1;
00230                                                         pt_delim = strchr(pt_arg,'|');
00231                                                 }
00232                                                 if (pt_delim == NULL) {
00233                                                         if (strcmp(pt_arg,&(argv[i][1])) == 0) break;
00234                                                 }
00235                                                 else
00236                                                         break;
00237                                         }
00238                                         else
00239                                                 if (strcmp(Arg_List[j],argv[i]) == 0) break;
00240                                 }
00241                                 if (j == List_Length) {         /* unknown option     */
00242                                         sprintf(err_text,"parse_arg : option %s ",argv[i]);
00243                                         Handle_Error(err_text,-1);
00244                                         return(-1);                             
00245                                 }
00246                                 set_arglabel(argv[i]);                  /* got label */
00247                                 set_argval(pt_ArgValues[j]);            /* got value */
00248                                 if (pt_ArgValues[j][0] == '\0') {
00249                                         if (strcmp(DefVal_List[j],"false") == 0)
00250                                                 strcpy(pt_ArgValues[j],"true");
00251                                         if (strcmp(DefVal_List[j],"none") == 0)
00252                                                 strcpy(pt_ArgValues[j],DefVal_List[j]);
00253                                 }
00254                         }
00255                         else {                                          /* handle standard option */
00256                                 switch (j) {
00257                                 case 0 :                                /* display help */
00258                                 case 1 :                                /* display help */
00259                                         print_msg("available options :");
00260                                         err_text[0] = '\0';
00261                                         for (k=0;k<List_Length; k++) {
00262                                                 if (strcmp(DefVal_List[k],"none") == 0) {
00263                                                         strcat(err_text,Arg_List[k]);
00264                                                         strcat(err_text," nodefault ");
00265                                                 }
00266                                                 else {
00267                                                         strcat(err_text,"[");
00268                                                         strcat(err_text,Arg_List[k]);
00269                                                         if (strcmp(DefVal_List[k],"false") != 0) {
00270                                                                 strcat(err_text," ");
00271                                                                 strcat(err_text,DefVal_List[k]);
00272                                                                 strcat(err_text," ");
00273                                                         }
00274                                                         strcat(err_text,"] ");
00275                                                 }
00276                                         }
00277                                         for (k=0;k<StandardList_Length; k++) {
00278                                                 strcat(err_text,"[");
00279                                                 strcat(err_text,StandardArg_List[k]);
00280                                                 strcat(err_text,"] ");
00281                                         }
00282                                         print_msg(err_text);
00283 
00284                                         if (General_Purpose != NULL)
00285                                                 print_msg("\nPurpose :\n%s\n",General_Purpose);
00286 
00287                                         exit(0);                        /* only want's help */
00288                                         break;
00289 
00290                                 case 2 :                                /* set input format */
00291                                         set_argval(format);
00292                                         status = set_inputformat(format);
00293                                         if (status) {
00294                                                 return(status);
00295                                         };
00296                                         break;
00297 
00298                                 case 3 :                                /* set output format */
00299                                         set_argval(format);
00300                                         status = set_outputformat(format);
00301                                         if (status) {
00302                                                 return(status);
00303                                         };
00304                                         break;
00305 
00306                                 case 4 :                                /* print software version */
00307                                         sprintf(err_text,"%s-%s",soft_version,LIBRARY_VERSION);
00308                                         print_msg(err_text);
00309                                         exit(0);                        /* only want's version */
00310                                         break;
00311 
00312                                 case 5 :                                /* set debug mode */
00313                                         DEBUG = 1;
00314                                         break;
00315 
00316                                 case 6 :                                /* set TK mode */
00317                                         TK = 1;
00318                                         break;
00319 
00320                                 case 7 :                                /* set quiet mode */
00321                                         VERBOSE = 0;
00322                                         strcpy(Calling_Prog,Quiet_Mode);
00323                                         break;
00324 
00325                                 case 8 :
00326                                         ASK = 0;
00327                                         break;
00328 
00329                                 default :
00330                                         sprintf(err_text,"parse_arg : Unknown option %s",argv[i]);
00331                                         Handle_Error(err_text,-1);
00332                                         return(-1);
00333                                         break;
00334                                 }
00335                         }
00336                 }
00337         }
00338         for (i=0; i<List_Length;i++) {
00339                 free(Arg_List[i]);
00340                 free(DefVal_List[i]);
00341         }
00342         free(Arg_List);
00343         free(DefVal_List);
00344 
00345         for (i=0; i<List_Length;i++) 
00346                 if (strcmp(pt_ArgValues[i],"none") == 0) {      
00347                         pt_ArgValues[i][0] = '\0';
00348                         sprintf(err_text,"parse_arg : option %s : value must be specified ",
00349                                         pt_ArgLabels[i]);
00350                         Handle_Error(err_text,ERR_NB_PARAM);
00351                         return(-1);                                     /* unknown option     */
00352                 }
00353         return(0);
00354 }
00355 
00356 /* --- Doxygen Function Comment -------------------------- */
00366 /* ------------------------------------------------------- */
00367 void get_argval(int i, char *format, void *value)
00368 {
00369         char errtext[132];
00370         int exit_session(int);
00371 
00372     if (sscanf(pt_ArgValues[i],format,value) != 1) {
00373         sprintf(errtext,"Inconsistent value found for %s. Got %s",
00374             pt_ArgLabels[i],pt_ArgValues[i]); 
00375         print_error(errtext);
00376         exit_session(ERR_BAD_PARAM);
00377     }
00378 }
00379 
00380 /*-----------------------------------------------------------------------------
00381                        functions defined in macro.h
00382 !
00383 !.func                             is_true()
00384 !
00385 !.purp       returns true if specified option is set, false if not 
00386 !.desc
00387 ! boolean is_true(option_val)
00388 !
00389 ! char *option_val;     option value returned by parse_arg()
00390 !.ed
00391 !
00392 !.func                             is_false()
00393 !
00394 !.purp       returns false if specified option is set, true if not 
00395 !.desc
00396 ! boolean is_false(option_val)
00397 !
00398 ! char *option_val;     option value returned by parse_arg()
00399 !.ed
00400 !
00401 !.func                             is_set()
00402 !
00403 !.purp   returns true if the option is set (i.e. its value is not NULL)
00404 !.desc
00405 ! boolean is_set(option_val)
00406 !
00407 ! char *option_val;     option value returned by parse_arg()
00408 !.ed
00409 !
00410 !.func                             is_unset()
00411 !
00412 !.purp   returns true if the option is not set (i.e. its value is NULL)
00413 !.desc
00414 ! boolean is_unset(option_val)
00415 !
00416 ! char *option_val;     option value returned by parse_arg()
00417 !.ed
00418 ------------------------------------------------------------------------------*/

Generated on Tue Jul 1 17:35:45 2003 for IFU_C_iolibs by doxygen1.3