Changeset - 2b4b4fb95800
[Not reviewed]
default
0 3 0
Nathan Brink (binki) - 16 years ago 2009-04-18 23:08:05
ohnobinki@ohnopublishing.net
fixed leak, implemented options_free function
3 files changed with 56 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/client/distren.c
Show inline comments
 
@@ -90,30 +90,29 @@ if( i == 1 ){
 
  fprintf(stderr, "execio_open returns %d\n", execio_open(&testrem, "ssh", execargv));
 
  buf[9] = '\0';
 
  while(!execio_read(testrem, buf, 9, &readlen))
 
    {
 
      if(readlen > 9)
 
	{
 
	  fprintf(stderr, "execio_read doesn't set readlen correctly or read() is messed up\n");
 
	  return 1;
 
	}
 
      buf[readlen] = '\0';
 
      fprintf(stderr, "read \"%s\"\n", buf);
 
    }
 
  return 0;
 
}
 

	
 
else
 
{
 
	printf("Well now, you must have registered!\n");
 
/* put code here to ssh to zserver2 w/ execio as the user from the conf file */
 
return 0;
 
}
 

	
 

	
 
//cfg_free(cfg);
 
 options_free(options);
 
 return 0;
 
};
 

	
 

	
 

	
 

	
 

	
src/common/options.c
Show inline comments
 
@@ -13,36 +13,65 @@
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "options.h"
 

	
 
#include <confuse.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 

	
 
struct options_common_data 
 
{
 
  cfg_t *cfg;
 
};
 

	
 

	
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts)
 
{
 
  char *configfile;
 
  
 

	
 
  /* For those of you who don't know, the following is an example of concatenation of constant strings by the C compiler. Now, that doesn't mean you can do run-time string concatenation ;-) 
 
   strdup is because someday configfile will be customizable via argv[]
 
   */
 
  configfile = strdup(SYSCONFDIR "/distren.conf");
 
  if(!configfile)
 
    {
 
      perror("strdup");
 
      return 1;
 
    }
 
      
 
  
 
  *allopts = malloc(sizeof(struct options_common));
 
  if(!*allopts)
 
    {
 
      perror("malloc");
 
      free(configfile);
 
      return 1;
 
    }
 
  memset(*allopts, '\0', sizeof(struct options_common));
 
  
 
  (*allopts)->data = malloc(sizeof(struct options_common_data));
 
  if(!(*allopts)->data)
 
    {
 
      perror("malloc");
 
      free(configfile);
 
      free(*allopts);
 
      return 1;
 
    }
 
  memset((*allopts)->data, '\0', sizeof(struct options_common_data));
 
  
 
  /* Conf File Parser */
 
  cfg_opt_t common_opts[] =
 
    {
 
      CFG_END()
 
    };
 
  
 
  cfg_opt_t server_opts[] = 
 
    {
 
      CFG_STR("username", NULL, CFGF_NONE),
 
      CFG_STR("hostname", NULL, CFGF_NONE),
 
      CFG_STR("method", "ssh", CFGF_NONE),
 
      CFG_END()
 
@@ -52,37 +81,50 @@ int options_init(int argc, char *argv[],
 
    {
 
      CFG_SEC("common",
 
	      common_opts,
 
	      CFGF_NONE),
 
      CFG_SEC("server",
 
	      server_opts,
 
	      CFGF_MULTI | CFGF_TITLE),
 
      CFG_SEC(myname,
 
	      myopts,
 
	      0),
 
      CFG_END()
 
    };
 
  cfg_t *cfg;
 

	
 

	
 

	
 
  cfg = cfg_init(opts, 0);
 
  switch(cfg_parse(cfg, configfile))
 
  (*allopts)->data->cfg = cfg_init(opts, 0);
 
  switch(cfg_parse((*allopts)->data->cfg, configfile))
 
    {
 
    default:
 
      fprintf(stderr, "cfg_parse returned an unknown error code\n");
 
    case CFG_FILE_ERROR:
 
      cfg_error(cfg, "Couldn't open file ``%s''\n", configfile);
 
      cfg_error((*allopts)->data->cfg, "Couldn't open file ``%s''\n", configfile);
 
      /* no break; on purpose */
 

	
 
    case CFG_PARSE_ERROR:
 
      
 
      free((*allopts)->data);
 
      free(*allopts);
 
      free(configfile);
 
      
 
      return 1;
 
      
 
    case CFG_SUCCESS:
 
      break;
 
    }
 
  
 
  /* End Conf File Parser */
 

	
 
  printf("Client Username: %s\n", cfg_getstr(cfg_getsec(cfg, "client"), "username"));
 
  cfg_free(cfg);
 
  free(configfile);
 
  
 
  return 0;
 
}
 

	
 
int options_free(struct options_common *opts)
 
{
 
  cfg_free(opts->data->cfg);
 

	
 
  free(opts->data);
 
  free(opts);
 
  
 
  return 0;
 
}
src/common/options.h
Show inline comments
 
@@ -21,27 +21,29 @@
 
  The point of this file is to handle options parsing that is common to both the server and client of distren. Options parsing that is specific to either should be managed by the server or the client. This means that the user of this interface will be given a handle to a part of a confuse config tree and have to eat that part of the tree to fulfill its own options.
 
  Unfortunately, this extra layer of abstraction shall possibly require a common_opts struct to be made that must be passed to all functions that use the common config. This would include any interface in /src/common/
 
 */
 

	
 
#ifndef _DISTREN_OPTIONS_H
 
#define _DISTREN_OPTIONS_H
 

	
 
#include <confuse.h>
 

	
 
/**
 
  incomplete:
 
  maybe remoteio.h should define its own struct which is injected into this struct and have its own handlers that parse its section of the confuse config file... (it'd be more modular)?
 
  Maybe we should just stick with compile-time modularity :-D (did I just suggest we make tons of macros?)
 
 */
 
struct options_common
 
{
 
  struct options_common_data *data;
 
  struct remoteio_opts *remoteio;
 
  struct client_opts *client;
 
  struct daemon_opts *daemon;
 
};
 

	
 
/**
 
  This function needs to eat some of your arguments to it, just like gtk+ and other libs like to. It particularly needs to be able to find the location of the config file, which should be taken from arguments if it's there. The argument list will have members that I recognize removed.
 
  
 
  Options should be kept alive during the program's life.
 
  
 
  \param mycfg will be set to the section of the configuration file specific to the caller (client or server specific section)
 
  \param myopts holds the caller's confuse option declarations
0 comments (0 inline, 0 general)