config_cmdline.c 5.53 KB
Newer Older
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
/*
 * 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
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * 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
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "config_userapi.h"

38 39
extern char *emul_iface;

40 41
int processoption(paramdef_t *cfgoptions, char *value)
{
oai's avatar
oai committed
42
char *tmpval = value;
oai's avatar
oai committed
43
int optisset=0;
oai's avatar
oai committed
44
char defbool[2]="1";
oai's avatar
oai committed
45

46 47
     if ( value == NULL) {
        if( (cfgoptions->paramflags &PARAMFLAG_BOOL) == 0 ) { /* not a boolean, argument required */
48 49
	    fprintf(stderr,"[CONFIG] command line, option %s requires an argument\n",cfgoptions->optname);
	    return 0;
50
        } else {        /* boolean value option without argument, set value to true*/
oai's avatar
oai committed
51
            tmpval = defbool;
52
        }
oai's avatar
oai committed
53
     }
54 55 56
     switch(cfgoptions->type)
       {
       	case TYPE_STRING:
57
           config_check_valptr(cfgoptions, (char **)(cfgoptions->strptr), sizeof(char *));
oai's avatar
oai committed
58
           config_check_valptr(cfgoptions, cfgoptions->strptr, strlen(tmpval+1));
59
	   printf("cfgoptions->strptr %p (&emul_iface %p, emul_iface %p)\n",cfgoptions->strptr,&emul_iface,emul_iface);
oai's avatar
oai committed
60
           sprintf(*(cfgoptions->strptr), "%s",tmpval);
61
           printf("[CONFIG] %s set to  %s from command line\n", cfgoptions->optname, tmpval);
62 63
	   optisset=1;
        break;
64
	
65 66 67 68 69 70 71 72 73
        case TYPE_STRINGLIST:
        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
74
	   config_assign_int(cfgoptions,cfgoptions->optname,(int32_t)strtol(tmpval,NULL,0));  
75 76
	   optisset=1;
        break;  	
77 78
       	case TYPE_UINT64:
       	case TYPE_INT64:
79
           config_check_valptr(cfgoptions, (char **)&(cfgoptions->i64ptr),sizeof(uint64_t));
oai's avatar
oai committed
80
	   *(cfgoptions->i64ptr)=strtoll(tmpval,NULL,0);  
81 82
           printf_cmdl("[CONFIG] %s set to  %lli from command line\n", cfgoptions->optname, (long long)*(cfgoptions->i64ptr));
	   optisset=1;
83 84 85 86 87
        break;        
       	case TYPE_UINTARRAY:
       	case TYPE_INTARRAY:

        break;
88 89
        case TYPE_DOUBLE:
           config_check_valptr(cfgoptions, (char **)&(cfgoptions->dblptr),sizeof(double)); 
oai's avatar
oai committed
90
           *(cfgoptions->dblptr) = strtof(tmpval,NULL);  
91 92 93 94
           printf_cmdl("[CONFIG] %s set to  %lf from command line\n", cfgoptions->optname, *(cfgoptions->dblptr));
	   optisset=1; 
        break; 

95 96 97 98 99
       	case TYPE_IPV4ADDR:

        break;

       default:
100
            fprintf(stderr,"[CONFIG] command line, %s type %i  not supported\n",cfgoptions->optname, cfgoptions->type);
101 102
       break;
       } /* switch on param type */
103 104 105 106
       if (optisset == 1) {
          cfgoptions->paramflags = cfgoptions->paramflags |  PARAMFLAG_PARAMSET;
       }
       
oai's avatar
oai committed
107
    return optisset;
108 109 110 111 112 113 114
}

int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix)
{
char **p = config_get_if()->argv;
int c = config_get_if()->argc;
int j;
oai's avatar
oai committed
115
char *pp;
116 117
char *cfgpath; 
 
118

119 120 121 122 123 124 125 126 127 128
  j = (prefix ==NULL) ? 0 : strlen(prefix); 
  cfgpath = malloc( j + MAX_OPTNAME_SIZE +1);
  if (cfgpath == NULL) {
     fprintf(stderr,"[CONFIG] %s %i malloc error,  %s\n", __FILE__, __LINE__,strerror(errno));
     return -1;
  }

  j=0;
  p++;
  c--;
oai's avatar
oai committed
129
    while (c > 0 && *p != NULL) {
130 131 132
        if (strcmp(*p, "-h") == 0 || strcmp(*p, "--help") == 0 ) {
            config_printhelp(cfgoptions,numoptions);
        }
133 134

        if (*p[0] == '-') {        
oai's avatar
oai committed
135 136 137 138 139 140 141 142 143
    	    for(int i=0;i<numoptions;i++) {
    		if ( ( cfgoptions[i].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
    		  continue;
    		 }
    		if (prefix != NULL) {
    		   sprintf(cfgpath,"%s.%s",prefix,cfgoptions[i].optname);
    		} else {
    		   sprintf(cfgpath,"%s",cfgoptions[i].optname);
    		}
144

oai's avatar
oai committed
145 146 147
    		if ( ((strlen(*p) == 2) && (strcmp(*p + 1,cfgpath) == 0))  || 
    		     ((strlen(*p) > 2) && (strcmp(*p + 2,cfgpath ) == 0 )) ) {
    		   pp = *(p+1);
148 149
    		   if ( ( pp != NULL ) && (c>1) &&  (pp[0]!= '-') ) {
    		
oai's avatar
oai committed
150 151 152 153
    		      j += processoption(&(cfgoptions[i]), pp);
    		   } else {
    		      j += processoption(&(cfgoptions[i]), NULL);
    		   }
154
                   break;
oai's avatar
oai committed
155 156 157
    		}
    	     } /* for */
         } /* if (*p[0] == '-') */  	     
158 159 160 161 162 163 164 165 166 167
   	 p++;
         c--;  
    }   /* fin du while */
  printf_cmdl("[CONFIG] %s %i options set from command line\n",((prefix == NULL) ? "":prefix),j);
  free(cfgpath);
  return j;            
}  /* parse_cmdline*/