Exemplo n.º 1
0
int setconfig(wchar** arg,int cl)
{
	int rv=0;
	for(;;)
	{
		if (cl<1)
		{
			printf("-config : invalid number of parameters, format is section:key=value\n");
			return rv;
		}
		
		wchar* sep=strstr(arg[1],":");
		if (sep==0)
		{
			printf("-config : invalid parameter %s, format is section:key=value\n",arg[1]);
			return rv;
		}
		wchar* value=strstr(sep+1,"=");
		if (value==0)
		{
			printf("-config : invalid parameter %s, format is section:key=value\n",arg[1]);
			return rv;
		}

		*sep++=0;
		*value++=0;

		wchar* sect=trim_ws(arg[1]);
		wchar* key=trim_ws(sep);
		value=trim_ws(value);

		if (sect==0 || key==0)
		{
			printf("-config : invalid parameter, format is section:key=value\n");
			return rv;
		}

		if (value==0)
			value="";
			
		printf("Virtual cfg %s:%s=%s\n",sect,key,value);

		cfgSetVitual(sect,key,value);
		rv++;

		if (cl>=3 && stricmp(arg[2],",")==0)
		{
			cl-=2;
			arg+=2;
			rv++;
			continue;
		}
		else
			break;
	}
	return rv;
}
Exemplo n.º 2
0
void ConfigFile::ParseFile(FILE* file)
{
	wchar line[512];
	wchar cur_sect[512]={0};
	int cline=0;
	while(file && !feof(file))
	{
		fgets(line,512,file);
		if (feof(file))
			break;

		cline++;

		if (strlen(line)<3)
			continue;
		if (line[strlen(line)-1]=='\r' || line[strlen(line)-1]=='\n')
			line[strlen(line)-1]=0;

		wchar* tl=trim_ws(line);
		if (tl[0]=='[' && tl[strlen(tl)-1]==']')
		{
			tl[strlen(tl)-1]=0;
			strcpy(cur_sect,tl+1);
			trim_ws(cur_sect);
		}
		else
		{
			if (cur_sect[0]==0)
				continue;//no open section
			wchar* str1=strstr(tl,"=");
			if (!str1)
			{
				printf("Malformed entry on config - ignoring @ %d(%s)\n",cline,tl);
				continue;
			}
			*str1=0;
			str1++;
			wchar* v=trim_ws(str1);
			wchar* k=trim_ws(tl);
			if (v && k)
			{
				ConfigSection*cs=this->GetEntry(cur_sect);
				
				//if (!cs->FindEntry(k))
				cs->SetEntry(k,v,CEM_SAVE|CEM_LOAD);
			}
			else
			{
				printf("Malformed entry on config - ignoring @ %d(%s)\n",cline,tl);
			}
		}
	}
}
Exemplo n.º 3
0
	int Tokenizer::tokenize( const string &raw_sent, V2_STR &data, const size_t init_offset ) 
	{
		string	trimmed_sent = trim_ws( raw_sent );		

		if( trimmed_sent == "") {
			return 0;
		}else {
			int	n_tokens = splitter( trimmed_sent, data );
			mark_pos( raw_sent, data, init_offset );

			return	n_tokens;
		}
	}
Exemplo n.º 4
0
void filter_set::parse_filter_file()
{
    //map<string,string> filters;
    list<string>::iterator iter;
    size_t idx;
    string key, val;

    for( iter = filter_file.begin(); iter != filter_file.end(); ++iter ){
        trim_ws( * iter);
        if( (! iter->empty()) && (iter->at(0) != '#') ){
            //cout << "[" << * iter << "]\n";
            //find the first "=" and split there
            idx = iter->find('=');
            if(idx != string::npos){
                key = iter->substr(0,idx);
                val = iter->substr(idx+1,iter->size()-1);
                trim_ws(key);
                trim_ws(val);
                //cout << "key[" << key << "] val[" << val << "]\n";
                filters.insert( pair<string,string>(key,val) );
            }
        }
    }
}
Exemplo n.º 5
0
static int def_load_bio(CONF *conf, BIO *in, long *line)
{
/* The macro BUFSIZE conflicts with a system macro in VxWorks */
#define CONFBUFSIZE     512
    int bufnum = 0, i, ii;
    BUF_MEM *buff = NULL;
    char *s, *p, *end;
    int again;
    long eline = 0;
    char btmp[DECIMAL_SIZE(eline) + 1];
    CONF_VALUE *v = NULL, *tv;
    CONF_VALUE *sv = NULL;
    char *section = NULL, *buf;
    char *start, *psection, *pname;
    void *h = (void *)(conf->data);
    STACK_OF(BIO) *biosk = NULL;
#ifndef OPENSSL_NO_POSIX_IO
    char *dirpath = NULL;
    OPENSSL_DIR_CTX *dirctx = NULL;
#endif

    if ((buff = BUF_MEM_new()) == NULL) {
        CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
        goto err;
    }

    section = OPENSSL_strdup("default");
    if (section == NULL) {
        CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
        goto err;
    }

    if (_CONF_new_data(conf) == 0) {
        CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
        goto err;
    }

    sv = _CONF_new_section(conf, section);
    if (sv == NULL) {
        CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
        goto err;
    }

    bufnum = 0;
    again = 0;
    for (;;) {
        if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
            CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
            goto err;
        }
        p = &(buff->data[bufnum]);
        *p = '\0';
 read_retry:
        BIO_gets(in, p, CONFBUFSIZE - 1);
        p[CONFBUFSIZE - 1] = '\0';
        ii = i = strlen(p);
        if (i == 0 && !again) {
            /* the currently processed BIO is at EOF */
            BIO *parent;

#ifndef OPENSSL_NO_POSIX_IO
            /* continue processing with the next file from directory */
            if (dirctx != NULL) {
                BIO *next;

                if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
                    BIO_vfree(in);
                    in = next;
                    goto read_retry;
                } else {
                    OPENSSL_free(dirpath);
                    dirpath = NULL;
                }
            }
#endif
            /* no more files in directory, continue with processing parent */
            if ((parent = sk_BIO_pop(biosk)) == NULL) {
                /* everything processed get out of the loop */
                break;
            } else {
                BIO_vfree(in);
                in = parent;
                goto read_retry;
            }
        }
        again = 0;
        while (i > 0) {
            if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
                break;
            else
                i--;
        }
        /*
         * we removed some trailing stuff so there is a new line on the end.
         */
        if (ii && i == ii)
            again = 1;          /* long line */
        else {
            p[i] = '\0';
            eline++;            /* another input line */
        }

        /* we now have a line with trailing \r\n removed */

        /* i is the number of bytes */
        bufnum += i;

        v = NULL;
        /* check for line continuation */
        if (bufnum >= 1) {
            /*
             * If we have bytes and the last char '\\' and second last char
             * is not '\\'
             */
            p = &(buff->data[bufnum - 1]);
            if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
                bufnum--;
                again = 1;
            }
        }
        if (again)
            continue;
        bufnum = 0;
        buf = buff->data;

        clear_comments(conf, buf);
        s = eat_ws(conf, buf);
        if (IS_EOF(conf, *s))
            continue;           /* blank line */
        if (*s == '[') {
            char *ss;

            s++;
            start = eat_ws(conf, s);
            ss = start;
 again:
            end = eat_alpha_numeric(conf, ss);
            p = eat_ws(conf, end);
            if (*p != ']') {
                if (*p != '\0' && ss != p) {
                    ss = p;
                    goto again;
                }
                CONFerr(CONF_F_DEF_LOAD_BIO,
                        CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
                goto err;
            }
            *end = '\0';
            if (!str_copy(conf, NULL, &section, start))
                goto err;
            if ((sv = _CONF_get_section(conf, section)) == NULL)
                sv = _CONF_new_section(conf, section);
            if (sv == NULL) {
                CONFerr(CONF_F_DEF_LOAD_BIO,
                        CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
                goto err;
            }
            continue;
        } else {
            pname = s;
            end = eat_alpha_numeric(conf, s);
            if ((end[0] == ':') && (end[1] == ':')) {
                *end = '\0';
                end += 2;
                psection = pname;
                pname = end;
                end = eat_alpha_numeric(conf, end);
            } else {
                psection = section;
            }
            p = eat_ws(conf, end);
            if (strncmp(pname, ".include", 8) == 0
                && (p != pname + 8 || *p == '=')) {
                char *include = NULL;
                BIO *next;

                if (*p == '=') {
                    p++;
                    p = eat_ws(conf, p);
                }
                trim_ws(conf, p);
                if (!str_copy(conf, psection, &include, p))
                    goto err;
                /* get the BIO of the included file */
#ifndef OPENSSL_NO_POSIX_IO
                next = process_include(include, &dirctx, &dirpath);
                if (include != dirpath) {
                    /* dirpath will contain include in case of a directory */
                    OPENSSL_free(include);
                }
#else
                next = BIO_new_file(include, "r");
                OPENSSL_free(include);
#endif
                if (next != NULL) {
                    /* push the currently processing BIO onto stack */
                    if (biosk == NULL) {
                        if ((biosk = sk_BIO_new_null()) == NULL) {
                            CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
                            goto err;
                        }
                    }
                    if (!sk_BIO_push(biosk, in)) {
                        CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
                        goto err;
                    }
                    /* continue with reading from the included BIO */
                    in = next;
                }
                continue;
            } else if (*p != '=') {
                CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
                goto err;
            }
            *end = '\0';
            p++;
            start = eat_ws(conf, p);
            trim_ws(conf, start);

            if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
                CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
                goto err;
            }
            v->name = OPENSSL_strdup(pname);
            v->value = NULL;
            if (v->name == NULL) {
                CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
                goto err;
            }
            if (!str_copy(conf, psection, &(v->value), start))
                goto err;

            if (strcmp(psection, section) != 0) {
                if ((tv = _CONF_get_section(conf, psection))
                    == NULL)
                    tv = _CONF_new_section(conf, psection);
                if (tv == NULL) {
                    CONFerr(CONF_F_DEF_LOAD_BIO,
                            CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
                    goto err;
                }
            } else
                tv = sv;
            if (_CONF_add_string(conf, tv, v) == 0) {
                CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
                goto err;
            }
            v = NULL;
        }
    }
    BUF_MEM_free(buff);
    OPENSSL_free(section);
    /*
     * No need to pop, since we only get here if the stack is empty.
     * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
     */
    sk_BIO_free(biosk);
    return 1;
 err:
    BUF_MEM_free(buff);
    OPENSSL_free(section);
    /*
     * Since |in| is the first element of the stack and should NOT be freed
     * here, we cannot use sk_BIO_pop_free().  Instead, we pop and free one
     * BIO at a time, making sure that the last one popped isn't.
     */
    while (sk_BIO_num(biosk) > 0) {
        BIO *popped = sk_BIO_pop(biosk);
        BIO_vfree(in);
        in = popped;
    }
    sk_BIO_free(biosk);
#ifndef OPENSSL_NO_POSIX_IO
    OPENSSL_free(dirpath);
    if (dirctx != NULL)
        OPENSSL_DIR_end(&dirctx);
#endif
    if (line != NULL)
        *line = eline;
    BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
    ERR_add_error_data(2, "line ", btmp);
    if (h != conf->data) {
        CONF_free(conf->data);
        conf->data = NULL;
    }
    if (v != NULL) {
        OPENSSL_free(v->name);
        OPENSSL_free(v->value);
        OPENSSL_free(v);
    }
    return 0;
}
Exemplo n.º 6
0
int main(int argc, char** argv)
{
	int c;
	char* sock_name;
	int sock_type;
	int s;
	struct binrpc_cmd cmd;
	struct id_list* sock_id;
	char* format;
	char* line;
	char* l;

	quit=0;
	format=0;
	line=0;
	s=-1;
	sock_name=0;
	sock_type=UNIXS_SOCK;
	opterr=0;
	while((c=getopt(argc, argv, "UVhs:D:R:vf:"))!=-1){
		switch(c){
			case 'V':
				printf("version: %s\n", version);
				printf("%s compiled on %s \n", __FILE__,
						compiled);
				exit(0);
				break;
			case 'h':
				printf("version: %s\n", version);
				printf("%s", help_msg);
				exit(0);
				break;
			case 's':
				sock_name=optarg;
				break;
			case 'R':
				reply_socket=optarg;
				break;
			case 'D':
				sock_dir=optarg;
				break;
			case 'U':
				sock_type=UDP_SOCK;
				break;
			case 'v':
				verbose++;
				break;
			case 'f':
				format=str_escape(optarg);
				if (format==0){
					fprintf(stderr, "ERROR: memory allocation failure\n");
					goto error;
				}
				break;
			case '?':
				if (isprint(optopt))
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				else
					fprintf(stderr,
							"Unknown option character `\\x%x'.\n",
							optopt);
				goto error;
			case ':':
				fprintf(stderr,
						"Option `-%c' requires an argument.\n",
						optopt);
				goto error;
			default:
				abort();
		}
	}
	if (sock_name==0){
		sock_name=DEFAULT_CTL_SOCKET;
	}

	/* init the random number generator */
	srand(getpid()+time(0)); /* we don't need very strong random numbers */

	if (sock_name==0){
		fprintf(stderr, "ERROR: no server socket address specified\n");
		goto error;
	}
	sock_id=parse_listen_id(sock_name, strlen(sock_name), sock_type);
	if (sock_id==0){
		fprintf(stderr, "ERROR: error parsing server socket address %s\n", sock_name);
		goto error;
	}

	switch(sock_id->proto){
		case UDP_SOCK:
		case TCP_SOCK:
			if (sock_id->port==0){
				sock_id->port=DEFAULT_CTL_PORT;
				/*
				fprintf(stderr, "ERROR: no port specified: %s:<port>\n",
								sock_name);
				goto error;
				*/
			}
			if ((s=connect_tcpudp_socket(sock_id->name, sock_id->port,
						(sock_id->proto==UDP_SOCK)?SOCK_DGRAM:
													SOCK_STREAM))<0){
				goto error;
			}
			break;
		case UNIXS_SOCK:
		case UNIXD_SOCK:
			if ((s=connect_unix_sock(sock_id->name,
						(sock_id->proto==UNIXD_SOCK)?SOCK_DGRAM:
													SOCK_STREAM))<0)
				goto error;
			break;
		case UNKNOWN_SOCK:
			fprintf(stderr, "ERROR: Bad socket type for %s\n", sock_name);
			goto error;
	}
	free(sock_id); /* not needed anymore */
	sock_id=0;

	if (optind>=argc){
			/*fprintf(stderr, "ERROR: no command specified\n");
			goto error; */
	}else{
		if (parse_cmd(&cmd, &argv[optind], argc-optind)<0)
			goto error;
		if (run_cmd(s, &cmd, format)<0)
			goto error;
		goto end;
	}
	/* interactive mode */
	if (get_kamcmd_list(s)==0){
	#ifdef USE_CFG_VARS
		get_cfgvars_list(s);
	#endif /* USE_CFG_VARS */
	#ifdef USE_COUNTERS
		get_counters_list(s);
	#endif /* USE_COUNTERS */
	}
	/* banners */
	printf("%s %s\n", NAME, VERSION);
	printf("%s\n", COPYRIGHT);
	printf("%s\n", DISCLAIMER);
#ifdef USE_READLINE

	/* initialize readline */
	/* allow conditional parsing of the ~/.inputrc file*/
	rl_readline_name=NAME;
	rl_completion_entry_function=kamcmd_generator;
	rl_attempted_completion_function=kamcmd_completion;

	while(!quit){
		line=readline(NAME "> ");
		if (line==0) /* EOF */
			break;
		l=trim_ws(line); /* trim whitespace */
		if (*l){
			add_history(l);
			run_line(s, l, format);
		}
		free(line);
		line=0;
	}
#else
	line=malloc(MAX_LINE_SIZE);
	if (line==0){
		fprintf(stderr, "memory allocation error\n");
		goto error;
	}
	printf(NAME "> "); fflush(stdout); /* prompt */
	while(!quit && fgets(line, MAX_LINE_SIZE, stdin)){
		l=trim_ws(line);
		if (*l){
			run_line(s, l, format);
		}
		printf(NAME "> "); fflush(stdout); /* prompt */
	};
	free(line);
	line=0;
#endif /* USE_READLINE */
end:
	/* normal exit */
	if (line)
		free(line);
	if (format)
		free(format);
	if (rpc_array)
		free_rpc_array(rpc_array, rpc_no);
#ifdef USE_CFG_VARS
	if (cfg_grp_lst)
		free_cfg_grp_lst();
	if (cfg_vars_array){
		free_rpc_array(cfg_vars_array, cfg_vars_no);
		cfg_vars_array=0;
		cfg_vars_no=0;
	}
#endif /* USE_CFG_VARS */
#ifdef USE_COUNTERS
	if (cnt_grp_lst)
		free_cnt_grp_lst();
	if (cnt_grps_array){
		free_rpc_array(cnt_grps_array, cnt_grps_no);
		cnt_grps_array=0;
		cnt_grps_no=0;
	}
#endif /* USE_COUNTERS */
	cleanup();
	exit(0);
error:
	if (line)
		free(line);
	if (format)
		free(format);
	if (rpc_array)
		free_rpc_array(rpc_array, rpc_no);
#ifdef USE_CFG_VARS
	if (cfg_grp_lst)
		free_cfg_grp_lst();
	if (cfg_vars_array){
		free_rpc_array(cfg_vars_array, cfg_vars_no);
		cfg_vars_array=0;
		cfg_vars_no=0;
	}
#endif /* USE_CFG_VARS */
#ifdef USE_COUNTERS
	if (cnt_grp_lst)
		free_cnt_grp_lst();
	if (cnt_grps_array){
		free_rpc_array(cnt_grps_array, cnt_grps_no);
		cnt_grps_array=0;
		cnt_grps_no=0;
	}
#endif /* USE_COUNTERS */
	cleanup();
	exit(-1);
}
Exemplo n.º 7
0
bool cfgOpen()
{
	wchar * tmpPath = GetEmuPath(L"");
	wcscpy(appPath, tmpPath);
	free(tmpPath);

	if (cfgPath[0]==0)
		swprintf(cfgPath,L"%snullDC.cfg", appPath);

	swprintf(dataPath,L"%sdata\\", appPath);
	swprintf(pluginPath,L"%splugins\\", appPath);

	ConfigSection* cs= cfgdb.GetEntry(L"emu");

	cs->SetEntry(L"AppPath",appPath,CEM_VIRTUAL | CEM_READONLY);
	cs->SetEntry(L"PluginPath",pluginPath,CEM_VIRTUAL | CEM_READONLY);
	cs->SetEntry(L"DataPath",dataPath,CEM_VIRTUAL | CEM_READONLY);
	cs->SetEntry(L"FullName",VER_FULLNAME,CEM_VIRTUAL | CEM_READONLY);
	cs->SetEntry(L"ShortName",VER_SHORTNAME,CEM_VIRTUAL | CEM_READONLY);
	cs->SetEntry(L"Name",VER_EMUNAME,CEM_VIRTUAL | CEM_READONLY);

	FILE* cfgfile = _tfopen(cfgPath,L"r");
	if(!cfgfile) {
		cfgfile = _tfopen(cfgPath,L"wt");
		if(!cfgfile) 
			log("Unable to open the config file for reading or writing\nfile : %s\n",cfgPath);
		else
		{
			fprintf(cfgfile,";; nullDC cfg file ;;\n\n");
			fseek(cfgfile,0,SEEK_SET);
			fclose(cfgfile);
			cfgfile = _tfopen(cfgPath,L"r");
			if(!cfgfile) 
				log("Unable to open the config file for reading\nfile : %s\n",cfgPath);
		}
	}

	wchar line[512];
	wchar cur_sect[512]={0};
	int cline=0;
	while(cfgfile && !feof(cfgfile))
	{
		cline++;
		fgetws(line,512,cfgfile);
		if (wcslen(line)<3)
			continue;
		if (line[wcslen(line)-1]=='\r' || line[wcslen(line)-1]=='\n')
			line[wcslen(line)-1]=0;

		wchar* tl=trim_ws(line);
		if (tl[0]=='[' && tl[wcslen(tl)-1]==']')
		{
			tl[wcslen(tl)-1]=0;
			wcscpy(cur_sect,tl+1);
			trim_ws(cur_sect);
		}
		else
		{
			if (cur_sect[0]==0)
				continue;//no open section
			wchar* str1=wcsstr(tl,L"=");
			if (!str1)
			{
				wprintf(L"Malformed entry on cfg,  ignoring @ %d(%s)\n",cline,tl);
				continue;
			}
			*str1=0;
			str1++;
			wchar* v=trim_ws(str1);
			wchar* k=trim_ws(tl);
			if (v && k)
			{
				ConfigSection*cs=cfgdb.GetEntry(cur_sect);
				
				//if (!cs->FindEntry(k))
				cs->SetEntry(k,v,CEM_SAVE|CEM_LOAD);
			}
			else
			{
				wprintf(L"Malformed entry on cfg,  ignoring @ %d(%s)\n",cline,tl);
			}
		}
	}

	for (size_t i=0;i<vlist.size();i++)
	{
		cfgdb.GetEntry(vlist[i].s)->SetEntry(vlist[i].n,vlist[i].v,CEM_VIRTUAL);
	}
	if (cfgfile)
	{
		cfgdb.SaveFile(cfgfile);
		fclose(cfgfile);
	}
	return true;
}