Example #1
0
int			ft_putenv(char *name, char *value)
{
	char	**env;
	char	*to_put;
	int		i;
	int		change;

	env = get_env();
	name = ft_strjoin(name, "=");
	to_put = ft_strjoin(name, value);
	change = 0;
	i = 0;
	while (env[i] && !change)
	{
		if (ft_strnequ(name, env[i], ft_strlen(name)))
		{
			env[i] = ft_strdup(to_put);
			change = 1;
		}
		i++;
	}
	if (!change)
		env = ft_ctabaddtoindex(env, setctab(to_put), i, 0);
	create_env(env);
	return (-1);
}
Example #2
0
File: main.c Project: vlehuger/42sh
int				main(int ac, char **av, char **env)
{
	t_sh		*sh;
	int			stop;

	create_env(env);
	env = add_builtin_pid(env);
	set_signal();
	if (ac > 1)
		(void)av;
	else
	{
		stop = 0;
		sh = get_sh(&stop);
		set_terminal(sh);
		while (!stop)
		{
			sh->ft_stdin = dup(STDIN_FILENO);
			sh->ft_stdout = dup(STDOUT_FILENO);
			start_sh(sh);
			ft_putenv("PID_EXECV", NULL);
		}
	}
	return (0);
}
Example #3
0
void	ft_setenv(char **av, t_env *env)
{
	int		i;
	int		j;

	i = 0;
	j = 0;
	while (av[1][i] != '=' && av[1][i] != '\0')
		i++;
	if (av[1][i] == '\0')
	{
		ft_puterr("setenv : usage VAR=VALEUR\n");
		return ;
	}
	while (env->environ[j] && ft_strncmp(env->environ[j], av[1], i))
		j++;
	if (env->environ[j])
	{
		free(env->environ[j]);
		env->environ[j] = ft_strdelc(av[1], '"');
	}
	else
		create_env(env, j, ft_strdelc(av[1], '"'));
	parse_environ(env);
}
Example #4
0
int				ft_unsetenv(int ac, char **av, int i, int j)
{
	char		*tmp;
	char		**env;

	env = get_env();
	if (!ac)
	{
		ft_putendl_fd("unsetenv: Too few arguments.", 2);
		return (EXIT_FAILURE);
	}
	while (i < ac)
	{
		j = 0;
		while (env[j])
		{
			tmp = ft_strjoin(av[i], "=");
			if (ft_strnequ(tmp, env[j], ft_strlen(tmp)))
				env = ft_ctabdel(env, j);
			j++;
		}
		i++;
	}
	create_env(env);
	return (EXIT_SUCCESS);
}
int _svm_struct_learn (int argc, char* argv[])
{  
  char trainfile[200];           /* file with training examples */
  char modelfile[200];           /* file for resulting classifier */
  SAMPLE sample;  /* training sample */
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  STRUCT_LEARN_PARM struct_parm;
  STRUCTMODEL structmodel;
  int alg_type;

  HIDEO_ENV *hideo_env=create_env();

  svm_struct_learn_api_init(argc,argv);

  svm_struct_main_read_input_parameters(argc,argv,trainfile,modelfile,&verbosity,
			&struct_verbosity,&struct_parm,&learn_parm,
			&kernel_parm,&alg_type);

  if(struct_verbosity>=1) {
    printf("Reading training examples..."); fflush(stdout);
  }
  /* read the training examples */
  sample=read_struct_examples(trainfile,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n"); fflush(stdout);
  }
  
  /* Do the learning and return structmodel. */
  if(alg_type == 1)
    svm_learn_struct(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,hideo_env);
  else if(alg_type == 2)
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,PRIMAL_ALG,hideo_env);
  else if(alg_type == 3)
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_ALG,hideo_env);
  else if(alg_type == 4)
    svm_learn_struct_joint(sample,&struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_CACHE_ALG,hideo_env);
  else
    exit(1);

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  if(struct_verbosity>=1) {
    printf("Writing learned model...");fflush(stdout);
  }
  write_struct_model(modelfile,&structmodel,&struct_parm);
  if(struct_verbosity>=1) {
    printf("done\n");fflush(stdout);
  }

  free_struct_sample(sample);
  free_struct_model(structmodel);

  svm_struct_learn_api_exit();

  return 0;
}
Example #6
0
/* This function doesn't actually open the environment, so it doesn't
   have to look in the cache.  Callers are supposed to own an
   exclusive lock on the filesystem anyway. */
svn_error_t *
svn_fs_bdb__remove(const char *path, apr_pool_t *pool)
{
  bdb_env_t *bdb;

  SVN_ERR(create_env(&bdb, path, pool));
  return convert_bdb_error
    (bdb, bdb->env->remove(bdb->env, bdb->path_bdb, DB_FORCE));
}
Example #7
0
LMDBFileIndex::LMDBFileIndex(bool no_sync)
	: _has_error(false), txn(NULL), map_size(c_initial_map_size), it_cursor(NULL), no_sync(no_sync)
{
	IScopedWriteLock lock(mutex);

	if(!create_env())
	{
		Server->Log("LMDB error creating env", LL_ERROR);
		_has_error=true;
	}
}
Example #8
0
void db_print(char *db_path){

    DB *dbp=NULL;
    DB_ENV *bayes_env;
    //u_int32_t db_flags;
    DBC *db_cursor;
    DBT key,data;

    if(create_env(&bayes_env,BAYES_LEARN_ENV_PATH)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not create environment\n");
        exit(EXIT_FAILURE);
    }

    /* If the database can't be open.*/
    if(create_db_conexion(&dbp, bayes_env, db_path,  DB_CREATE)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open database\n");
        exit(EXIT_FAILURE);
    }
    /*
    if(db_create(&dbp,NULL,0)!=0){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not create database\n");
        exit(1);
    }

    db_flags= DB_CREATE;

    if (dbp->open(dbp,NULL, db_path, NULL, DB_HASH, db_flags,0) !=0 ){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open database\n");
        exit(1);
    }
    */
    dbp->cursor(dbp,NULL,&db_cursor,0);

    memset(&key,0,sizeof(DBT));
    memset(&data,0,sizeof(DBT));

    long i=0;
    /* For each token is stored its number of spam and ham messages and its probability.*/
    while (db_cursor->get(db_cursor,&key,&data,DB_NEXT)==0){
        printf("  KEY %s\n",(char *)key.data);
        printf("  SPAM: %ld\n",((tokendata *)data.data)->spam_count);
        printf("  HAM: %ld\n",((tokendata *)data.data)->ham_count);
        printf("  Probability: %2.2f\n\n",((tokendata *)data.data)->probability);
        i++;
    }

    (db_cursor!=NULL)?(db_cursor->c_close(db_cursor)):(1);

    (dbp!=NULL)?(close_db_conexion(&dbp, db_path)):(1);
    bayes_env->close(bayes_env,0);

    wblprintf(LOG_INFO,"LEARN_BAYES","Total %l records\n",i);

}
Example #9
0
Create( void *data, LWChannelID chan, LWError *err )
{
   ETInst *inst;

   if ( inst = calloc( 1, sizeof( ETInst ))) {
      inst->chan = chan;
      inst->env = create_env( chan );
   }
   else
      *err = "Instance allocation failed.";

   return inst;
}
Example #10
0
int
main(int argc, char *argv[])
{
    extern char *optarg;
    DB_ENV *dbenv;
    const char *home;
    char ch;
    int ret;

    dbenv = NULL;

    ret = 0;
    home = NULL;

    /* Create and configure the environment handle. */
    if ((ret = create_env(progname, &dbenv)) != 0)
	goto err;

    /* Collect the command line options. */
    while ((ch = getopt(argc, argv, "h:")) != EOF)
	switch (ch) {
	case 'h':
	    home = optarg;
	    break;
	case '?':
	default:
	    usage();
	}

    /* Error check command line. */
    if (home == NULL)
	usage();

    /* Open the environment. */
    if ((ret = env_init(dbenv, home)) != 0)
      goto err;

    if ((ret = doloop(dbenv)) != 0) {
	dbenv->err(dbenv, ret, "Application failed");
	goto err;
    }

err: if (dbenv != NULL)
	(void)dbenv->close(dbenv, 0);

    return (ret);
}
Example #11
0
/**
 * Set an ENV. If it value is empty, delete it.
 * If not find it in ENV table, then create it.
 *
 * @param key ENV name
 * @param value ENV value
 *
 * @return result
 */
FlashErrCode flash_set_env(const char *key, const char *value) {
    FlashErrCode result = FLASH_NO_ERR;

    /* if ENV value is empty, delete it */
    if (*value == NULL) {
        result = flash_del_env(key);
    } else {
        /* if find this ENV, then delete it and recreate it  */
        if (find_env(key)) {
            result = flash_del_env(key);
        }
        if (result == FLASH_NO_ERR) {
            result = create_env(key, value);
        }
    }
    return result;
}
Example #12
0
File: ft_env.c Project: yytailor/42
t_env		*insert_env(t_env *list, char *name, char *value)
{
	t_env	*tmp;
	t_env	*new_element;

	tmp = list;
	new_element = create_env(name, value);
	if (list == NULL)
		return (new_element);
	else
	{
		while (tmp->next != NULL)
			tmp = tmp->next;
		tmp->next = new_element;
		new_element->prev = tmp;
	}
	return (list);
}
Example #13
0
/**
 * ENV set default.
 *
 * @return result
 */
FlashErrCode flash_env_set_default(void){
    FlashErrCode result = FLASH_NO_ERR;
    size_t i;

    FLASH_ASSERT(default_env_set);
    FLASH_ASSERT(default_env_set_size);

    /* set ENV detail part end address is at ENV detail part start address */
    set_env_detail_end_addr(get_env_detail_addr());

    /* create default ENV */
    for (i = 0; i < default_env_set_size; i++) {
        create_env(default_env_set[i].key, default_env_set[i].value);
    }

    flash_save_env();

    return result;
}
Example #14
0
int     eval_expr(char *str)
{
	int		i;
	t_env	*env;

	env = create_env();
	if (!env)
		return (0);
	i = 0;
	while (str[i])
        read_token(env, str, &i); //transform to reverse-polish notation
	while (env->stack)
	{
		add_to_queue(env, env->stack->data);
		pop_from_stack(env);
	}
	env->stack = NULL;
	i = calc_expr(env); //calculate result from reverse-polish notation
	free(env);
	return (i);
}
Example #15
0
File: main.c Project: rle-mino/21sh
int		main(int ac, char **av, char **env)
{
	struct termios		reset;
	t_le				le;
	t_env				*data_env;

	if (ac && av)
		;
	if (tcgetattr(0, &reset) == -1)
		message_handling();
	g_name_prog = NULL;
	signal(SIGWINCH, update_size);
	data_env = create_env(env);
	data_env->reset = reset;
	fill_env(&data_env, env);
	if (init_term(data_env) == 0)
		message_handling();
	init_env(&le, data_env);
	data_env->le = le;
	loop_prompt(data_env);
	free_env(&data_env);
	reset_term(reset);
	return (0);
}
Example #16
0
static svn_error_t *
svn_fs_bdb__open_internal(bdb_env_baton_t **bdb_batonp,
                          const char *path,
                          u_int32_t flags, int mode,
                          apr_pool_t *pool)
{
  bdb_env_key_t key;
  bdb_env_t *bdb;
  svn_boolean_t panic;

  /* We can safely discard the open DB_CONFIG file handle.  If the
     environment descriptor is in the cache, the key's immutability is
     guaranteed.  If it's not, we don't care if the key changes,
     between here and the actual insertion of the newly-created
     environment into the cache, because no other thread can touch the
     cache in the meantime. */
  SVN_ERR(bdb_cache_key(&key, NULL, path, pool));

  bdb = bdb_cache_get(&key, &panic);
  if (panic)
    return svn_error_create(SVN_ERR_FS_BERKELEY_DB, NULL,
                            db_strerror(DB_RUNRECOVERY));

  /* Make sure that the environment's open flags haven't changed. */
  if (bdb && bdb->flags != flags)
    {
      /* Handle changes to the DB_PRIVATE flag specially */
      if ((flags ^ bdb->flags) & DB_PRIVATE)
        {
          if (flags & DB_PRIVATE)
            return svn_error_create(SVN_ERR_FS_BERKELEY_DB, NULL,
                                    "Reopening a public Berkeley DB"
                                    " environment with private attributes");
          else
            return svn_error_create(SVN_ERR_FS_BERKELEY_DB, NULL,
                                    "Reopening a private Berkeley DB"
                                    " environment with public attributes");
        }

      /* Otherwise return a generic "flags-mismatch" error. */
      return svn_error_create(SVN_ERR_FS_BERKELEY_DB, NULL,
                              "Reopening a Berkeley DB environment"
                              " with different attributes");
    }

  if (!bdb)
    {
      svn_error_t *err;

      SVN_ERR(create_env(&bdb, path, svn_pool_create(bdb_cache_pool)));
      err = bdb_open(bdb, flags, mode);
      if (err)
        {
          /* Clean up, and we can't do anything about returned errors. */
          svn_error_clear(bdb_close(bdb));
          return svn_error_trace(err);
        }

      apr_hash_set(bdb_cache, &bdb->key, sizeof bdb->key, bdb);
      bdb->flags = flags;
      bdb->refcount = 1;
    }
  else
    {
      ++bdb->refcount;
    }

  *bdb_batonp = apr_palloc(pool, sizeof **bdb_batonp);
  (*bdb_batonp)->env = bdb->env;
  (*bdb_batonp)->bdb = bdb;
  (*bdb_batonp)->error_info = get_error_info(bdb);
  ++(*bdb_batonp)->error_info->refcount;
  apr_pool_cleanup_register(pool, *bdb_batonp, cleanup_env_baton,
                            apr_pool_cleanup_null);

  return SVN_NO_ERROR;
}
Example #17
0
void LMDBFileIndex::commit_transaction_internal(bool handle_enosp)
{
	int rc = mdb_txn_commit(txn);
	
	
	if(rc==MDB_MAP_FULL && handle_enosp)
	{
		if(_has_error)
		{
			Server->Log("LMDB had error during increase (on commit). Aborting...", LL_ERROR);
			start_transaction();
			return;
		}

		{
			read_transaction_lock.reset();

			IScopedWriteLock lock(mutex);

			destroy_env();

			map_size*=2;

			Server->Log("Increased LMDB database size to "+PrettyPrintBytes(map_size)+" (on commit)", LL_DEBUG);

			if(!create_env())
			{
				Server->Log("Error creating env after database file size increase", LL_ERROR);
				_has_error=true;
				start_transaction();
				return;
			}
			
		}

		start_transaction();

		replay_transaction_log();
		
		commit_transaction_internal(false);
	}
	else if(rc==MDB_BAD_TXN && handle_enosp)
	{
		if(_has_error)
		{
			Server->Log("LMDB had error on BAD_TXN (on commit). Aborting...", LL_ERROR);
			start_transaction();
			return;
		}
		
		start_transaction();
		
		replay_transaction_log();
		
		commit_transaction_internal(false);
	}
	else if(rc)
	{
		Server->Log("LMDB: Failed to commit transaction ("+(std::string)mdb_strerror(rc)+")", LL_ERROR);
		_has_error=true;
	}

	read_transaction_lock.reset();
	transaction_log.clear();
}
Example #18
0
/**
 * Loads the emails contained in a path and stores its tokens in a berkeley database.
 */
void load_directory_mail(char *directory, char *db_path, int type){

    filelist *mails_directory=list_files(directory,"eml");
    int i=0;
    DB *dbp=NULL;
    DB_ENV *bayes_env;
    map_t tokenbody;
    char *email;
    int count=0;

    //bayes_env=malloc(sizeof(DB_ENV *));
    /* If the path is incorrect. */
    if(mails_directory==NULL){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Directory not found");
        exit(EXIT_FAILURE);
    }

    /* If the directory not contains any e-mail.*/
    if (count_files_filelist(mails_directory)==0){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Directory empty\n");
        exit(EXIT_FAILURE);
    }

    /*If the environment can't be created.*/
    if(create_env(&bayes_env,BAYES_SPAM_LEARN_ENV_PATH)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not create environment\n");
        exit(EXIT_FAILURE);
    }

    /* If the database can't be open.*/
    if(create_db_conexion(&dbp, bayes_env, db_path,  DB_CREATE)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open database\n");
        exit(EXIT_FAILURE);
    }

    /*If the e-mail is a spam message, its tokens will be stored in the database as spam tokens.*/
    if(type==OPT_SPAM)
        for(;i<count_files_filelist(mails_directory);i++){
            wblprintf(LOG_INFO,"LEARN_BAYES","Spam file name: %s\n",get_file_at(mails_directory,i));
            email=loademail(get_file_at(mails_directory,i));
            if((tokenbody=tokenizebody(email))!=NULL){
                count++;
                store_mail(dbp,tokenbody,OPT_SPAM);
                free_tokenize(tokenbody);
            }
            else{}
            free(email);
    }/*If the e-mail is a ham message, its tokens will be stored in the database as ham tokens.*/
    else
        for(;i<count_files_filelist(mails_directory);i++){
            wblprintf(LOG_INFO,"LEARN_BAYES","Ham file name: %s\n",get_file_at(mails_directory,i));
            email=loademail(get_file_at(mails_directory,i));
            if((tokenbody=tokenizebody(email))!=NULL){
                count++;
                store_mail(dbp,tokenbody,OPT_HAM);
                free_tokenize(tokenbody);
            }
            free(email);
        }
    free_filelist(mails_directory);
    close_db_conexion(&dbp, db_path);
    bayes_env->close(bayes_env,0);
    //free(bayes_env);
    freeEMLParser();
    printf("Summary %d messages\n",i);
    printf("  %d messages inserted\n",count);
    printf("  %d messages wrong\n",i-count);
}
Example #19
0
static void test_eval( LWChannelID chan )
{
   static char ftype[] = "GBA";
   static char stype[] = "THBLSZ";
   static char sep1[] = "------------+-----------+---+-----------+-----------+-----------+-----------+-----------+-----------+-----------";
   static char sep2[] = "-----------+-----------+----------------------+----------------------+----------------------";

   LWEnvelopeID lwenv;
   Envelope *env[ 3 ];
   Key *key[ 3 ];
   FILE *fp;
   float lo, hi, tlen, t, tp, a, b;
   int i, j;


   env[ 0 ] = create_env( chan );
   env[ 1 ] = read_envb( chan );
   env[ 2 ] = read_enva( chan );
   lwenv = chinfo->channelEnvelope( chan );

   if ( !env[ 0 ] || !env[ 1 ] || !env[ 2 ] || !lwenv ) {
      msgf->error( "Couldn't get the envelope.", NULL );
      return;
   }

   write_mot57( env[ 1 ], "test57.mot" );

   fp = fopen( TEST_OUT, "w" );
   if ( !fp ) {
      msgf->error( "Couldn't open " TEST_OUT, NULL );
      return;
   }

   fprintf( fp,
      "(G)lobal:  values from LWEnvelopeFuncs->keyGet()\n"
      "(B)inary:  values from LWEnvelopeFuncs->save() LWIO_BINARY\n"
      "(A)SCII:   values from LWEnvelopeFuncs->save() LWIO_ASCII\n\n" );

   for ( j = 0; j < 3; j++ ) {
      fprintf( fp, "%c   %d keys   Pre %d  Post %d\n", ftype[ j ],
         env[ j ]->nkeys, env[ j ]->behavior[ 0 ], env[ j ]->behavior[ 1 ] );
      key[ j ] = env[ j ]->key;
   }
   fprintf( fp, "\n" );

   fprintf( fp, "%s\n", sep1 );
   fprintf( fp, "        Time|      Value|Typ|    Tension| Continuity|       Bias|    Param 0|    Param 1|    Param 2|    Param 3\n" );
   fprintf( fp, "%s\n", sep1 );

   for ( i = 0; i < env[ 0 ]->nkeys; i++ ) {
      for ( j = 0; j < 3; j++ ) {
         fprintf( fp, "%c%11.6f|%11.6f| %c |%11.6f|%11.6f|%11.6f|%11.6f|%11.6f|%11.6f|%11.6f\n",
            ftype[ j ],
            key[ j ]->time,
            key[ j ]->value,
            stype[ key[ j ]->shape ],
            key[ j ]->tension,
            key[ j ]->continuity,
            key[ j ]->bias,
            key[ j ]->param[ 0 ],
            key[ j ]->param[ 1 ],
            key[ j ]->param[ 2 ],
            key[ j ]->param[ 3 ] );
         key[ j ] = key[ j ]->next;
      }
      fprintf( fp, "%s\n", sep1 );
   }
   fprintf( fp, "\n\n" );

   key[ 0 ] = env[ 0 ]->key;
   lo = key[ 0 ]->time;
   while ( key[ 0 ]->next ) key[ 0 ] = key[ 0 ]->next;
   hi = key[ 0 ]->time;
   tlen = hi - lo;
   lo -= tlen * 2;
   hi += tlen * 2;
//   tlen *= 4.9f;
   tlen = hi - lo;

   fprintf( fp, "%s\n", sep2 );
   fprintf( fp, "       Time|      Value|    G Value      Error|    B Value      Error|    A Value      Error\n" );
   fprintf( fp, "%s\n", sep2 );

   t = lo;
   for ( i = 0; i < 100; i++ ) {
      a = ( float ) envf->evaluate( lwenv, ( double ) t );
      fprintf( fp, "%11.6f|%11.6f", t, a );
      for ( j = 0; j < 3; j++ ) {
         b = evalEnvelope( env[ j ], t );
         fprintf( fp, "|%11.6f%11.6f", b, b - a );
      }
      fprintf( fp, "\n" );

      tp = t;
      t = lo + tlen * ( i + 1 ) / 100.0f;
      if ( tp < env[ 0 ]->key->time && t >= env[ 0 ]->key->time )
         fprintf( fp, "%s\n", sep2 );
      if ( tp < key[ 0 ]->time && t >= key[ 0 ]->time )
         fprintf( fp, "%s\n", sep2 );
   }

   fclose( fp );
   for ( j = 0; j < 3; j++ )
      if ( env[ j ] ) free_env( env[ j ] );
}
Example #20
0
int
main(int argc, char *argv[])
{
    DB_ENV *dbenv;
    DB_SITE *dbsite;
    extern char *optarg;
    const char *home;
    char ch, *host, *portstr;
    int local_is_set, ret, is_group_creator;
    u_int16_t port;
    /* Used to track whether this is a replica or a master. */
    APP_DATA my_app_data;

    dbenv = NULL;
    ret = local_is_set = is_group_creator = 0;
    home = NULL;

    my_app_data.is_master = 0;  /* Assume that we start as a replica */

    if ((ret = create_env(progname, &dbenv)) != 0)
	goto err;

    /* Make APP_DATA available through the environment handle. */
    dbenv->app_private = &my_app_data;

    /* Default priority is 100. */
    dbenv->rep_set_priority(dbenv, 100);
    /* Permanent messages require at least one ack. */
    dbenv->repmgr_set_ack_policy(dbenv, DB_REPMGR_ACKS_ONE);
    /* Give 500 microseconds to receive the ack. */
    dbenv->rep_set_timeout(dbenv, DB_REP_ACK_TIMEOUT, 500);

    /* Collect the command line options. */
    while ((ch = getopt(argc, argv, "h:l:L:p:r:")) != EOF)
	switch (ch) {
	case 'h':
	    home = optarg;
	    break;
	/* Set the host and port used by this environment. */
	case 'L':
	    is_group_creator = 1; /* FALLTHROUGH */
	case 'l':
	    host = strtok(optarg, ":");
	    if ((portstr = strtok(NULL, ":")) == NULL) {
		fprintf(stderr, "Bad host specification.\n");
		goto err;
	    }
	    port = (unsigned short)atoi(portstr);
	    if ((ret =
	      dbenv->repmgr_site(dbenv, host, port, &dbsite, 0)) != 0){
		fprintf(stderr, "Could not set local address %s:%d.\n",
		  host, port);
		goto err;
	    }
	    dbsite->set_config(dbsite, DB_LOCAL_SITE, 1);
	    if (is_group_creator)
		dbsite->set_config(dbsite, DB_GROUP_CREATOR, 1);

	    if ((ret = dbsite->close(dbsite)) != 0) {
		dbenv->err(dbenv, ret, "DB_SITE->close");
		goto err;
	}
	    local_is_set = 1;
	    break;
	/* Set this replica's election priority. */
	case 'p':
	    dbenv->rep_set_priority(dbenv, atoi(optarg));
	    break;
	/* Identify another site in the replication group. */
	case 'r':
	    host = strtok(optarg, ":");
	    if ((portstr = strtok(NULL, ":")) == NULL) {
		fprintf(stderr, "Bad host specification.\n");
		goto err;
	    }
	    port = (unsigned short)atoi(portstr);
	    if ((ret = dbenv->repmgr_site(dbenv, host, port, &dbsite, 0)) != 0) {
		dbenv->err(dbenv, ret, "DB_ENV->repmgr_site");
		goto err;
	    }
	    dbsite->set_config(dbsite, DB_BOOTSTRAP_HELPER, 1);
	    if ((ret = dbsite->close(dbsite)) != 0) {
		dbenv->err(dbenv, ret, "DB_SITE->close");
		goto err;
	    }
	    break;
	case '?':
	default:
	    usage();
	}

    /* Error check command line. */
    if (home == NULL || !local_is_set)
	usage();

    if ((ret = env_init(dbenv, home)) != 0)
	goto err;

    if ((ret = dbenv->repmgr_start(dbenv, 3, DB_REP_ELECTION)) != 0)
	goto err;

    if ((ret = doloop(dbenv)) != 0) {
	dbenv->err(dbenv, ret, "Application failed");
	goto err;
    }

err: if (dbenv != NULL)
	(void)dbenv->close(dbenv, 0);

    return (ret);

}
Example #21
0
int process_logline(request * req)
{
	char *stop, *stop2;
	static char *SIMPLE_HTTP_VERSION = "HTTP/0.9";
	
	req->logline = req->header_line;
	req->method = request_type(req);
	if (req->method == M_INVALID || req->method == M_SHORT) {
#ifdef BOA_TIME_LOG
		log_error_time();
		fprintf(stderr, "malformed request: \"%s\"\n", req->logline);
#endif
		syslog(LOG_ERR, "malformed request: \"%s\" from %s\n", req->logline, req->remote_ip_addr);
		send_r_bad_request(req);
		return 0;
	}
	
	/* Guaranteed to find ' ' since we matched a method above */
	stop = req->logline + 3;
	if (*stop != ' ')
		++stop;
	
	/* scan to start of non-whitespace */
	while (*(++stop) == ' ');

	stop2 = stop;

	/* scan to end of non-whitespace */
	while (*stop2 != '\0' && *stop2 != ' ')
		++stop2;

	if (stop2 - stop > MAX_HEADER_LENGTH) {
#ifdef BOA_TIME_LOG
		log_error_time();
		fprintf(stderr, "URI too long %d: \"%s\"\n", MAX_HEADER_LENGTH,
				req->logline);
#endif
		syslog(LOG_ERR, "URI too long %d: \"%s\" from %s\n", MAX_HEADER_LENGTH,
				req->logline, req->remote_ip_addr);
		send_r_bad_request(req);
		return 0;
	}
	memcpy(req->request_uri, stop, stop2 - stop);
	req->request_uri[stop2 - stop] = '\0';

	if (*stop2 == ' ') {		
		/* if found, we should get an HTTP/x.x */
		int p1, p2;

		if (sscanf(++stop2, "HTTP/%d.%d", &p1, &p2) == 2 && p1 >= 1) {
			req->http_version = stop2;
			req->simple = 0;
		} else {
#ifdef BOA_TIME_LOG
			log_error_time();
			fprintf(stderr, "bogus HTTP version: \"%s\"\n", stop2);
#endif
			syslog(LOG_ERR, "bogus HTTP version: \"%s\" from %s\n", stop2, req->remote_ip_addr);
			send_r_bad_request(req);
			return 0;
		}
		
	} else {
		req->http_version = SIMPLE_HTTP_VERSION;
		req->simple = 1;
	}
	
	if (req->method == M_HEAD && req->simple) {
		syslog(LOG_ERR, "Simple HEAD request not allowed from %s\n", req->remote_ip_addr);
		send_r_bad_request(req);
		return 0;
	}
	create_env(req);    /* create cgi env[], we don't know if url is cgi */
	return 1;
}
Example #22
0
ast_t* eval_call(env_t* env, ast_t* ast) {
  ast_t* func = NULL;
  ast_t* result = NULL;
  char* fn = NULL;  /* function name */
  switch(ast->data.call.call_type) {
    case ct_anonymous:
      fn = "<anonymous>";
      func = ast->data.call.function.function;
      break;
    case ct_named:
      fn = ast->data.call.function.id;
      func = get_ast_by_id(env, fn);
      if(func == NULL) {
        error_id(NULL, fn);
      };
      break;
  }
  switch(func->type) {
    case at_function:{
      size_t i;
      if(ast->data.call.callargs->data.callargs.count != func->data.function.params->data.params.count) {
        error_paramcount(NULL, fn, func->data.function.params->data.params.count, ast->data.call.callargs->data.callargs.count);
      }
      env_t* inner = create_env();
      inner->parent =  env;
      for(i = 0; i < func->data.function.params->data.params.count; i++) {
        set_ast_to_id(
            inner,
            func->data.function.params->data.params.params[i],
            eval_expression(env,ast->data.call.callargs->data.callargs.callargs[i])
        );
      }
      /* execute the function */
      exec_statements(inner, func->data.function.statements);
      /* get the result */
      inner->parent = NULL; /* must be NULL, get_ast_by_id() also searches the parent environment */
      result = get_ast_by_id(inner, "@");
      free_env(inner);
      break;
    }
    case at_builtin:
      if(ast->data.call.callargs->data.callargs.count != func->data.builtin.paramcount) {
        error_paramcount(NULL, fn, func->data.function.params->data.params.count, ast->data.call.callargs->data.callargs.count);
      }
      switch(func->data.builtin.paramcount) {
        case 0:
          result = func->data.builtin.function.builtin_0();
          break;
        case 1: {
          ast_t* p = eval_expression(env,ast->data.call.callargs->data.callargs.callargs[0]);
          result = func->data.builtin.function.builtin_1(p);
          dec_ref(p);
          break;
        }
        case 2: {
          ast_t* p1 = eval_expression(env,ast->data.call.callargs->data.callargs.callargs[0]);
          ast_t* p2 = eval_expression(env,ast->data.call.callargs->data.callargs.callargs[1]);
          result = func->data.builtin.function.builtin_2(p1,p2);
          dec_ref(p1);
          dec_ref(p2);
          break;
        }
        case 3: {
          ast_t* p1 = eval_expression(env, ast->data.call.callargs->data.callargs.callargs[0]);
          ast_t* p2 = eval_expression(env, ast->data.call.callargs->data.callargs.callargs[1]);
          ast_t* p3 = eval_expression(env, ast->data.call.callargs->data.callargs.callargs[2]);
          result = func->data.builtin.function.builtin_3(p1,p2,p3);
          dec_ref(p1);
          dec_ref(p2);
          dec_ref(p3);
          break;
        }
        default:
          printf("\n\n*** HINT TO DEVELOPER ***\nimplement builtincall in vm.c\n\n");
          exit(1);
          /* if you create a builtin function with more parameters then you have to add a case here */
          break;
      }
      break;
    default:
      error_expected(NULL, get_ast_type_name(at_function), get_ast_type_name(func->type));
      break;
  }
  return result;
}
Example #23
0
int _svm_learn (int argc, char* argv[])
{  
  char docfile[200];           /* file with training examples */
  char modelfile[200];         /* file for resulting classifier */
  char restartfile[200];       /* file with initial alphas */
  DOC **docs;  /* training examples */
  long totwords,totdoc,i;
  double *target;
  double *alpha_in=NULL;
  KERNEL_CACHE *kernel_cache;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  MODEL *model=(MODEL *)my_malloc(sizeof(MODEL));

  HIDEO_ENV *hideo_env=create_env();

  model->td_pred=NULL;
  model->n_td_pred=0;

  _read_input_parameters(argc,argv,docfile,modelfile,restartfile,&verbosity,
			&learn_parm,&kernel_parm);
  read_documents(docfile,&docs,&target,&totwords,&totdoc);
  if(restartfile[0]) alpha_in=read_alphas(restartfile,totdoc);

  if(kernel_parm.kernel_type == LINEAR) { /* don't need the cache */
    kernel_cache=NULL;
  }
  else {
    /* Always get a new kernel cache. It is not possible to use the
       same cache for two different training runs */
    kernel_cache=kernel_cache_init(totdoc,learn_parm.kernel_cache_size);
  }

  if(learn_parm.type == CLASSIFICATION) {
    svm_learn_classification(docs,target,totdoc,totwords,&learn_parm,
			     &kernel_parm,kernel_cache,model,alpha_in,hideo_env);
  }
  else if(learn_parm.type == REGRESSION) {
    svm_learn_regression(docs,target,totdoc,totwords,&learn_parm,
			 &kernel_parm,&kernel_cache,model,hideo_env);
  }
  else if(learn_parm.type == RANKING) {
    svm_learn_ranking(docs,target,totdoc,totwords,&learn_parm,
		      &kernel_parm,&kernel_cache,model,hideo_env);
  }
  else if(learn_parm.type == OPTIMIZATION) {
    svm_learn_optimization(docs,target,totdoc,totwords,&learn_parm,
			   &kernel_parm,kernel_cache,model,alpha_in,hideo_env);
  }

  if(kernel_cache) {
    /* Free the memory used for the cache. */
    kernel_cache_cleanup(kernel_cache);
  }

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  /* deep_copy_of_model=copy_model(model); */
  write_model(modelfile,model);

  free(alpha_in);
  free_model(model,0);
  for(i=0;i<totdoc;i++) 
    free_example(docs[i],1);
  free(docs);
  free(target);
  free_env(hideo_env);

  return(0);
}
Example #24
0
XCALL_( static LWError ) Copy( ETInst *to, ETInst *from )
{
   to->chan = from->chan;
   to->env = create_env( from->chan );
   return NULL;
}
Example #25
0
static lisp_obj *apply(lisp_expr_application *app, lisp_env *env, lisp_err *err)
{
    lisp_obj *callable = FORCE_VALUE(app->proc, env, err);
    if (! callable){
        return NULL;
    }

    lisp_obj *res = NIL;

    /* Internal procedure */
    if (callable->type == PROC){
        /* Eval args */
        lisp_obj **args = calloc(app->nparams, sizeof(lisp_obj*));
        for (size_t i=0; i<app->nparams; i++){
            lisp_obj *arg = FORCE_VALUE(app->params[i], env, err);
            if (! arg){
                for (size_t j=0; j<i; j++){
                    release(args[j]);
                }
                free(args);
                return NULL;
            }
            args[i] = arg;
        }

        /* Eval internal */
        res = callable->value.p(app->nparams, args);
        
        /* Free args */
        for (size_t i=0; i<app->nparams; i++){
            release(args[i]);
        }
        free(args);
    }

    /* Lisp func */
    else if (callable->type == LAMBDA){
        lisp_lambda *lambda = &(callable->value.l);
        lisp_expr_lambda *lambda_expr = &(lambda->declaration->value.mklambda);

        /* Check arity */
        if (app->nparams != lambda_expr->nparams){
            raise_error(err, WRONG_ARITY, "Arity error ! Expected %d params, got %d",
                lambda_expr->nparams, app->nparams);
            return NULL;
        }

        /* Extend env */
        lisp_env *locals = create_env(lambda->context);
        for (size_t i=0; i<lambda_expr->nparams; i++){
            lisp_obj *param = eval_expression(app->params[i], env, err);
            if (! param){
                release_env(locals);
                return NULL;
            }
            DEBUG("Extend env with %s", lambda_expr->param_names[i]);
            release(set_env(locals, lambda_expr->param_names[i], param));
        }

        if (enable_debug){
            printf("\033[1mCALL\033[0m ");
            dump_expr(lambda_expr->body);
            printf(" with env\n");
            dump_env(locals);
        }

        /* Wrap in thunk for trampoline */
        res = make_thunk(lambda_expr->body, locals);
        release_env(locals);
    }
    else {
        lisp_print(callable);
        raise_error(err, NOT_CALLABLE, "CANNOT CALL obj %p", callable);
        return NULL;
    }

    release(callable);
    return res;
}
Example #26
0
sync_env( ETInst *inst )
{
   if ( inst->env ) free_env( inst->env );
   inst->env = create_env( inst->chan );
   return NULL;
}
Example #27
0
int svm_struct_learn (int argc, char* argv[], SAMPLE *sample, STRUCTMODEL *model, STRUCT_LEARN_PARM *struct_parm)
{  
  char trainfile[200];           /* file with training examples */
  char modelfile[200];           /* file for resulting classifier */
  STRUCTMODEL structmodel;
  LEARN_PARM learn_parm;
  KERNEL_PARM kernel_parm;
  int alg_type;
  //SAMPLE _sample;
  //int i, j;

  HIDEO_ENV *hideo_env=create_env();

  svm_struct_learn_api_init(argc,argv);

  svm_struct_read_input_parameters(argc,argv,trainfile,modelfile,&verbosity,
			&struct_verbosity,struct_parm,&learn_parm,
			&kernel_parm,&alg_type);

//  _sample=read_struct_examples("C:\\NewWork\\SvmLightLib\\SvmLightLibDemo\\Examples\\Multiclass\\train.dat",struct_parm);
//  // compare _sample to *sample
//  assert(_sample.n == sample->n);
//  for (i = 0; i < _sample.n; i++)
//  {
//	  SVECTOR *vec_1 = _sample.examples[i].x.doc->fvec;
//	  SVECTOR *vec_2 = sample->examples[i].x.doc->fvec;
//	  assert(vec_1->factor == vec_2->factor);
//	  assert(vec_1->kernel_id == vec_2->kernel_id);
//	  assert(vec_1->next == vec_2->next);
//	  assert(vec_1->twonorm_sq == vec_2->twonorm_sq);
//	  assert(strcmp(vec_1->userdefined, vec_2->userdefined) == 0);
//	  j = 0;
//	  while (vec_1->words[j].wnum > 0 || vec_2->words[j].wnum > 0)
//	  {
//		  //printf("%d %d\n", vec_1->words[j].wnum, vec_2->words[j].wnum);
//		  //printf("%f %f\n", vec_1->words[j].weight, vec_2->words[j].weight);
//		  assert(vec_1->words[j].wnum == vec_2->words[j].wnum);
//		  assert(vec_1->words[j].weight == vec_2->words[j].weight);
//		  j++;
//	  }
//  }
//  //sample=&_sample;
  
  /* Do the learning and return structmodel. */
  if(alg_type == 1)
    svm_learn_struct(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,hideo_env);
  else if(alg_type == 2)
    svm_learn_struct_joint(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,PRIMAL_ALG,hideo_env);
  else if(alg_type == 3)
    svm_learn_struct_joint(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_ALG,hideo_env);
  else if(alg_type == 4)
    svm_learn_struct_joint(*sample,struct_parm,&learn_parm,&kernel_parm,&structmodel,DUAL_CACHE_ALG,hideo_env);
  else
    exit(1);

  /* Warning: The model contains references to the original data 'docs'.
     If you want to free the original data, and only keep the model, you 
     have to make a deep copy of 'model'. */
  model->sizePsi=structmodel.sizePsi;
  model->svm_model=copy_model(structmodel.svm_model);
  model->w=model->svm_model->lin_weights;
 
  free_struct_model(structmodel);
  
  svm_struct_learn_api_exit();

  return 0;
}
Example #28
0
int sysinit (cfg_opts *options)
{
    struct t_os2process *proc; // sysinit's PTDA/proc
    //l4events_ch_t event_ch = L4EVENTS_EXIT_CHANNEL;
    //l4events_nr_t event_nr = L4EVENTS_NO_NR;
    //l4events_event_t event;
    char   *env;
    APIRET rc;

    // create global environment
    create_env(&env);

    // Create the sysinit process PTDA structure (pid == ppid == 0)
    proc = PrcCreate(0,         // ppid
                     "sysinit", // pPrg
                     NULL,      // pArg
                     env);      // pEnv

    /* set task number */
    sysinit_id = l4_myself();
    proc->task = sysinit_id;

    if (!names_register("os2srv.sysinit"))
        io_log("error registering on the name server\n");

    /* Start servers */
    exec_runserver(proc->pid);

    /* Start run=/call= */
    exec_run_call(proc->pid);

    // Check PROTSHELL statement value
    if (!options->protshell || !*(options->protshell))
    {
        io_log("No PROTSHELL statement in CONFIG.SYS\n");
        rc = ERROR_INVALID_PARAMETER; /*ERROR_INVALID_PARAMETER 87; Not defined for Windows*/
    } else {
        exec_protshell(options);
        rc = 0; /* NO_ERROR */
    }

    io_log("sem wait\n");

    if (!rc) // wait until child process (protshell) terminates (this will unblock us)
        l4semaphore_down(&proc->term_sem);

    io_log("done waiting\n");

    /* if (use_events) // use events server
    {
      // terminate by sending an exit event
      event.len = sizeof(l4_threadid_t);
      *(l4_threadid_t*)event.str = l4_myself();
      // send exit event
      l4events_send(event_ch, &event, &event_nr, L4EVENTS_SEND_ACK);
      // get acknowledge
      l4events_get_ack(&event_nr, L4_IPC_NEVER);
    } */

    io_log("event notification sent\n");

    // unregister at names
    if (!names_unregister_task(sysinit_id))
        io_log("Cannot unregister at name server!\n");

    // destroy proc/PTDA
    PrcDestroy(proc);

    // destroy global environment
    destroy_env(env);

    io_log("OS/2 Server ended\n");
    // terminate OS/2 Server
    exit(rc);

    return rc;
}
Example #29
0
/**
 * Exports a berkeley database to a file.
 */
void db_dump(char *db_path,char *file_path){

    DB *dbp=NULL;
    DB_ENV *bayes_env;
    //u_int32_t db_flags;
    DBC *db_cursor;
    DBT key,data;
    FILE *file;

    if(create_env(&bayes_env,BAYES_LEARN_ENV_PATH)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not create environment\n");
        exit(EXIT_FAILURE);
    }

    /* If the database can't be open.*/
    if(create_db_conexion(&dbp, bayes_env, db_path,  DB_CREATE)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open database\n");
        exit(EXIT_FAILURE);
    }
    /*
    if(db_create(&dbp,NULL,0)!=0){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not create database\n");
        exit(1);
    }

    db_flags= DB_CREATE;

    if (dbp->open(dbp,NULL, db_path, NULL, DB_HASH, db_flags,0) !=0 ){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open database\n");
        exit(1);
    }
    */
    dbp->cursor(dbp,NULL,&db_cursor,0);

    memset(&key,0,sizeof(DBT));
    memset(&data,0,sizeof(DBT));

    file = fopen(file_path,"wb");

    if(file==NULL){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","File not open\n");
        exit(1);
    }
    int i=0;
    /* For each token is stored its number of spam and ham messages and its probability.*/
    while (db_cursor->get(db_cursor,&key,&data,DB_NEXT)==0){
        printf("  KEY %s\n",(char *)key.data);
        printf("  SPAM: %ld\n",((tokendata *)data.data)->spam_count);
        printf("  HAM: %ld\n",((tokendata *)data.data)->ham_count);
        printf("  Probability: %2.2f\n\n",((tokendata *)data.data)->probability);
        fwrite(((char *)key.data),sizeof(char)*16,1,file);
        fwrite(((tokendata *)data.data),sizeof(tokendata),1,file);
        i++;
    }

    if(fclose(file)!=0){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not close file\n");
        exit(1);
    }

    if(db_cursor!=NULL)
        db_cursor->c_close(db_cursor);

    if(dbp!=NULL)
        close_db_conexion(&dbp, db_path);
    
    bayes_env->close(bayes_env,0);

    wblprintf(LOG_INFO,"LEARN_BAYES","Total %d records saved\n\t\t\t\t\t       Export succesfully completed\n",i);
    
}
Example #30
0
/**
 * Imports a berkeley database from a file.
 */
void db_load(char *file_path, char *db_path){
    
    DB *dbp=NULL;
    DB_ENV *bayes_env;
    //u_int32_t db_flags;
    DBT key,data;
    FILE *file;

    file = fopen(file_path,"rb");

    if(file==NULL){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open. File %s does not exist\n",file_path);
        exit(1);
    }
    
    if(create_env(&bayes_env,BAYES_LEARN_ENV_PATH)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not create environment\n");
        exit(EXIT_FAILURE);
    }

    /* If the database can't be open.*/
    if(create_db_conexion(&dbp, bayes_env, db_path,  DB_CREATE)!=DB_OK){
        wblprintf(LOG_CRITICAL,"LEARN_BAYES","Could not open database\n");
        exit(EXIT_FAILURE);
    }
    
    memset(&key,0,sizeof(DBT));
    memset(&data,0,sizeof(DBT));

    /* Stores each token and its information in a berkeley database.*/
    tokendata *token=malloc(sizeof(tokendata));
    char *tokenkey=malloc(sizeof(char)*17);
    int i=0;
    
    while(fread(tokenkey,sizeof(char),16,file)){
        if(fread(token,sizeof(tokendata),1,file)!=sizeof(tokendata))
            wblprintf(LOG_CRITICAL,"LEARN_BAYES","Error reading token from file\n");
        
        tokenkey[16]='\0';
        
        key.data = tokenkey;
        key.size = (sizeof(char)*(strlen(tokenkey)+1));

        data.data= token;
        data.size= sizeof(tokendata);

        printf("Inserting token\n");
        printf("  KEY %s\n",(char *)key.data);
        printf("  SPAM: %ld\n",((tokendata *)data.data)->spam_count);
        printf("  HAM: %ld\n",((tokendata *)data.data)->ham_count);
        
        dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
        i++;
    }
    free(token);
    free(tokenkey);
    fclose(file);

    wblprintf(LOG_INFO,"LEARN_BAYES","Total %d records saved\n\t\t\t\t\t       Import succesfully completed\n",i);

    if(dbp!=NULL){
        wblprintf(LOG_DEBUG,"Closing Database in %s\n",db_path);
            close_db_conexion(&dbp, db_path);
    }
    bayes_env->close(bayes_env,0);
    

}