config_cmdline.c 9.69 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
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
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file common/config/cmdline/config_libconfig.c
 * \brief configuration module, command line parsing implementation 
 * \author Francois TABURET
 * \date 2017
 * \version 0.1
 * \company NOKIA BellLabs France
 * \email: francois.taburet@nokia-bell-labs.com
 * \note
 * \warning
 */
32
#define _GNU_SOURCE
33 34 35
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
36
#include <ctype.h>
37
#include <errno.h>
38
#include <platform_types.h>
39 40
#include "config_userapi.h"

41

42
int parse_stringlist(paramdef_t *cfgoptions, char *val)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
char *atoken;
char *tokctx;
char *tmpval=strdup(val);
int   numelt=0;

   cfgoptions->numelt=0;

   atoken=strtok_r(tmpval, ",",&tokctx);
   while(atoken != NULL) {
     numelt++ ;
     atoken=strtok_r(NULL, ",",&tokctx);
   }
   free(tmpval);
   config_check_valptr(cfgoptions,(char **)&(cfgoptions->strlistptr), sizeof(char *) * numelt);
   cfgoptions->numelt=numelt;

   atoken=strtok_r(val, ",",&tokctx);
   for( int i=0; i<cfgoptions->numelt && atoken != NULL ; i++) {
      config_check_valptr(cfgoptions,&(cfgoptions->strlistptr[i]),strlen(atoken)+1);
      sprintf(cfgoptions->strlistptr[i],"%s",atoken);
      printf_params("[LIBCONFIG] %s[%i]: %s\n", cfgoptions->optname,i,cfgoptions->strlistptr[i]);
      atoken=strtok_r(NULL, ",",&tokctx);
   }
67
   return (cfgoptions->numelt > 0); 
68 69
}
 
70 71
int processoption(paramdef_t *cfgoptions, char *value)
{
oai's avatar
oai committed
72
char *tmpval = value;
oai's avatar
oai committed
73
int optisset=0;
oai's avatar
oai committed
74
char defbool[2]="1";
oai's avatar
oai committed
75

76
     
77 78
     if ( value == NULL) {
        if( (cfgoptions->paramflags &PARAMFLAG_BOOL) == 0 ) { /* not a boolean, argument required */
79 80
	    fprintf(stderr,"[CONFIG] command line, option %s requires an argument\n",cfgoptions->optname);
	    return 0;
81
        } else {        /* boolean value option without argument, set value to true*/
oai's avatar
oai committed
82
            tmpval = defbool;
83
        }
oai's avatar
oai committed
84
     }
85 86 87
     switch(cfgoptions->type)
       {
       	case TYPE_STRING:
88 89 90 91 92 93
           if (cfgoptions->numelt == 0 ) {
              config_check_valptr(cfgoptions, cfgoptions->strptr, strlen(tmpval)+1);
              sprintf(*(cfgoptions->strptr), "%s",tmpval);
            } else {
              sprintf( (char *)(cfgoptions->strptr), "%s",tmpval);              
           }
oai's avatar
oai committed
94
           printf_cmdl("[CONFIG] %s set to  %s from command line\n", cfgoptions->optname, tmpval);
95 96
	   optisset=1;
        break;
97
	
98
        case TYPE_STRINGLIST:
99
           optisset=parse_stringlist(cfgoptions,tmpval); 
100 101 102 103 104 105 106 107
        break;
        case TYPE_UINT32:
       	case TYPE_INT32:
        case TYPE_UINT16:
       	case TYPE_INT16:
	case TYPE_UINT8:
       	case TYPE_INT8:	
           config_check_valptr(cfgoptions, (char **)&(cfgoptions->iptr),sizeof(int32_t));
oai's avatar
oai committed
108
	   config_assign_int(cfgoptions,cfgoptions->optname,(int32_t)strtol(tmpval,NULL,0));  
109 110
	   optisset=1;
        break;  	
111 112
       	case TYPE_UINT64:
       	case TYPE_INT64:
113
           config_check_valptr(cfgoptions, (char **)&(cfgoptions->i64ptr),sizeof(uint64_t));
oai's avatar
oai committed
114
	   *(cfgoptions->i64ptr)=strtoll(tmpval,NULL,0);  
115 116
           printf_cmdl("[CONFIG] %s set to  %lli from command line\n", cfgoptions->optname, (long long)*(cfgoptions->i64ptr));
	   optisset=1;
117 118 119 120 121
        break;        
       	case TYPE_UINTARRAY:
       	case TYPE_INTARRAY:

        break;
122 123
        case TYPE_DOUBLE:
           config_check_valptr(cfgoptions, (char **)&(cfgoptions->dblptr),sizeof(double)); 
oai's avatar
oai committed
124
           *(cfgoptions->dblptr) = strtof(tmpval,NULL);  
125 126 127 128
           printf_cmdl("[CONFIG] %s set to  %lf from command line\n", cfgoptions->optname, *(cfgoptions->dblptr));
	   optisset=1; 
        break; 

129 130 131 132 133
       	case TYPE_IPV4ADDR:

        break;

       default:
134
            fprintf(stderr,"[CONFIG] command line, %s type %i  not supported\n",cfgoptions->optname, cfgoptions->type);
135 136
       break;
       } /* switch on param type */
137 138 139
       if (optisset == 1) {
          cfgoptions->paramflags = cfgoptions->paramflags |  PARAMFLAG_PARAMSET;
       }
oai's avatar
oai committed
140
    return optisset;
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
int config_check_cmdlineopt(char *prefix)
{
int unknowndetected=0;
char testprefix[CONFIG_MAXOPTLENGTH]="";
int finalcheck = 0; 

    if (prefix != NULL) {
       if (strcmp(prefix,CONFIG_CHECKALLSECTIONS) == 0) 
         finalcheck = 1;
       else if (strlen(prefix) > 0) {
         sprintf(testprefix,"--%s.",prefix);
       }
    } 

    for (int i=1; i<config_get_if()->argc ; i++) {
      if ( !finalcheck && strstr(config_get_if()->argv[i],testprefix) == NULL ) continue;
      if ( !finalcheck && testprefix[0]==0 && index(config_get_if()->argv[i],'.') != NULL) continue;
      if ( !finalcheck && config_get_if()->argv[i][0] == '-' && isdigit(config_get_if()->argv[i][1])) continue;
      if ( (config_get_if()->argv_info[i] & CONFIG_CMDLINEOPT_PROCESSED) == 0 ) {
        fprintf(stderr,"[CONFIG] unknown option: %s\n",
                config_get_if()->argv[i] );
        unknowndetected++; 
      }  
    }

 
  printf_cmdl("[CONFIG] %i unknown option(s) in command line starting with %s (section %s)\n",
              unknowndetected,testprefix,((prefix==NULL)?"":prefix));
  return unknowndetected;            
}  /* parse_cmdline*/

174 175
int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
{
Cedric Roux's avatar
Cedric Roux committed
176 177 178
  int c = config_get_if()->argc;
  int i,j;
  char *pp;
179
  char cfgpath[CONFIG_MAXOPTLENGTH];
180
 
181 182 183 184
  j = 0;
  i = 0;
    while (c > 0 ) {
        char *oneargv = strdup(config_get_if()->argv[i]);          /* we use strtok_r which modifies its string paramater, and we don't want argv to be modified */
Cedric Roux's avatar
Cedric Roux committed
185
        /* first check help options, either --help, -h or --help_<section> */
186 187 188
        if (strncmp(oneargv, "-h",2) == 0 || strncmp(oneargv, "--help",6) == 0 ) {
            char *tokctx;
            pp=strtok_r(oneargv, "_",&tokctx);
189
            config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
190 191 192 193 194 195 196
            if (pp == NULL || strcasecmp(pp,config_get_if()->argv[i] ) == 0 ) {
                if( prefix == NULL) {
                  config_printhelp(cfgoptions,numoptions);
                  if ( ! ( CONFIG_ISFLAGSET(CONFIG_NOEXITONHELP)))
                     exit_fun("[CONFIG] Exiting after displaying help\n");
                }
            } else {
197
                pp=strtok_r(NULL, " ",&tokctx);
198 199 200 201 202 203 204 205 206
                if ( prefix != NULL && pp != NULL && strncasecmp(prefix,pp,strlen(pp)) == 0 ) { 
                   printf ("Help for %s section:\n",prefix);               
                   config_printhelp(cfgoptions,numoptions);
                   if ( ! (CONFIG_ISFLAGSET(CONFIG_NOEXITONHELP))) {
                      fprintf(stderr,"[CONFIG] %s %i section %s:", __FILE__, __LINE__, prefix);
                      exit_fun(" Exiting after displaying help\n");
                   }
                }
            } 
207
        }
208

Cedric Roux's avatar
Cedric Roux committed
209
        /* now, check for non help options */
210 211 212
        if (oneargv[0] == '-') {        
    	    for(int n=0;n<numoptions;n++) {
    		if ( ( cfgoptions[n].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
oai's avatar
oai committed
213 214 215
    		  continue;
    		 }
    		if (prefix != NULL) {
216
    		   sprintf(cfgpath,"%s.%s",prefix,cfgoptions[n].optname);
oai's avatar
oai committed
217
    		} else {
218
    		   sprintf(cfgpath,"%s",cfgoptions[n].optname);
oai's avatar
oai committed
219
    		}
220 221 222 223
    		if ( ((strlen(oneargv) == 2) && (strcmp(oneargv + 1,cfgpath) == 0))  || /* short option, one "-" */
    		     ((strlen(oneargv) > 2) && (strcmp(oneargv + 2,cfgpath ) == 0 )) ) {
                   char *valptr=NULL;
                   int ret;
224
                   config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
225 226 227 228 229 230 231 232 233 234 235
                   if (c > 0) {
    		      pp = config_get_if()->argv[i+1];
                      if (pp != NULL ) {                      
                         ret = strlen(pp);
                         if (ret > 0 ) {
                             if (pp[0] != '-')
                                valptr=pp;
                             else if ( ret > 1 && pp[0] == '-' && isdigit(pp[1]) )
                                valptr=pp;
                         }
                     }
236
                   }
237
                   j += processoption(&(cfgoptions[n]), valptr);
238 239
    		   if (  valptr != NULL ) {
                      i++;
240
                      config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
241 242
                      c--;
    		   } 
243
                   break;
oai's avatar
oai committed
244
    		}
245 246 247 248
    	     } /* for n... */
         } /* if (oneargv[0] == '-') */  	     
         free(oneargv);
         i++;
249 250
         c--;  
    }   /* fin du while */
251
  printf_cmdl("[CONFIG] %s %i options set from command line\n",((prefix == NULL) ? "(root)":prefix),j);
252 253 254 255 256 257 258 259
  if ( !(CONFIG_ISFLAGSET( CONFIG_NOCHECKUNKOPT )) ) {
      i=config_check_cmdlineopt(prefix);
      if (i > 0) {
         fprintf(stderr,"[CONFIG] %i unknown options for section %s detected in command line\n",
                 i,((prefix==NULL)?"\"root section\"":prefix));
         exit_fun(" Exiting after detecting errors in command line \n");
      }
  }
260 261
  return j;            
}  /* parse_cmdline*/