Changeset - f17d2abc8601
[Not reviewed]
default
0 4 0
Nathan Brink (binki) - 16 years ago 2009-04-08 23:32:22
ohnobinki@ohnopublishing.net
progress on restructuring options system
4 files changed with 34 insertions and 9 deletions:
0 comments (0 inline, 0 general)
etc/distren.conf
Show inline comments
 
/*
 
  Configuration file for distren.
 
  Currently, this file is being prepared as the goal for this project. For instance, the ability to support connecting and communicating with servers is suggested by the server sections.
 
*/
 

	
 
server protofusion
 
{
 
  hostname = "protofusion.org"
 
  username = "distrenc"
 
  /* method's use is not implemented, ssh is the only option atm */
 
  method = "ssh"
 
}
 

	
 
server ohnopublishing
 
{
 
  hostname = "ohnopublishing.net"
 
  username = "distrenc"
 
  method = "ssh"
 
}
 

	
 
client
 
{
 
  render_types = {"povray", "blender", "inkscape", "imagemagick", "dcraw"}
 
  username = "distrenc"
 
}
 

	
src/client/distren.c
Show inline comments
 
@@ -24,52 +24,52 @@
 
 * the user must enter on the site. Apache should be running mod_peruser for safety, and I'm
 
 * thinking zserver2 for web serving for convenience...
 
 */
 

	
 

	
 
#include "options.h"
 
#include "execio.h"
 

	
 
#include <stdio.h> /* sprintf, printf */
 
#include <stdlib.h> /* malloc, free */
 
#include <libxml/tree.h> /* Happy fun XML time */
 
#include <libxml/xmlwriter.h>
 
#include <confuse.h>
 
#include<string.h> // for strcat
 

	
 

	
 
int main(int argc, char *argv[])
 
{
 
  struct options_common *options;
 
  
 
  cfg_t *cfg;
 
  
 
  cfg_opt_t cfg_opts[] =
 
    {
 
      CFG_STR("username", "guest", CFGF_NONE),
 
      CFG_STR("hostname", "protofusion.org", CFGF_NONE),
 
      CFG_STR("name", "Guest", CFGF_NONE),
 
      CFG_STR("email", "guest@protofusion.org", CFGF_NONE),
 
      CFG_STR_LIST("render_types", NULL, 0),
 
      CFG_STR("username", NULL, 0),
 
      CFG_STR("name", NULL, 0),
 
      CFG_STR("email", NULL, 0),
 
      CFG_END()
 
    };
 

	
 
  int i;
 
  
 
  if(options_init(argc, argv, &cfg, cfg_opts, "client", &options))
 
    {
 
      fprintf(stderr, "error getting configuration\n");
 
      return 1;
 
    }
 

	
 

	
 
// Please find a better way of doing this :( you can't strcat multiple strings.. meh
 
// use something like sprintf
 
//char *username = cfg_getstr(cfg, "username");
 
//char *hostname = cfg_getstr(cfg, "hostname");
 
//strcat(username, "@");
 
//strcat(username, hostname);
 

	
 

	
 
if( i == 1 ){
 
// Create new account on the server if no username exists
 
// THIS no longer SEGFAULTS!! YAY
 
  char buf[10];
src/common/options.c
Show inline comments
 
@@ -2,64 +2,87 @@
 
  Copyright 2008 Nathan Phillip Brink, Ethan Zonca
 

	
 
  This file is a part of DistRen.
 

	
 
  DistRen is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU Affero General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  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>
 

	
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, const char *myname, struct options_common **allopts)
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts)
 
{
 
  char *configfile;
 
  
 
  configfile = strdup(SYSCONFDIR "/distren.conf");
 
  if(!configfile)
 
    {
 
      perror("strdup");
 
      return 1;
 
    }
 
      
 
  /* 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()
 
    };
 
  
 
  cfg_opt_t opts[] =
 
    {
 
      CFG_STR("username", "guest", CFGF_NONE),
 
      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, CFGF_NONE);
 

	
 

	
 
  cfg = cfg_init(opts, 0);
 
  switch(cfg_parse(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);
 
      /* no break; on purpose */
 

	
 
    case CFG_PARSE_ERROR:
 
      return 1;
 
      
 
    case CFG_SUCCESS:
 
      break;
 
    }
 
  
 
  /* End Conf File Parser */
 

	
 
  printf("Client Username: %s\n", cfg_getstr(cfg, "username"));
 
  printf("Client Username: %s\n", cfg_getstr(cfg_getsec(cfg, "client"), "username"));
 
  cfg_free(cfg);
 
  return 0;
 
}
src/common/options.h
Show inline comments
 
@@ -30,31 +30,31 @@
 
/**
 
  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)?
 
 */
 
struct options_common
 
{
 
  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
 
  \param myname holds the name of the section of the config file that refers to mycfg and myopts
 

	
 
  \param allopts will be set to a pointer that many functions need for operation
 
  \param argc the argc that was passed to main()
 
  \param argv the argv that was passed to main()
 
 */
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, const char *myname, struct options_common **allopts);
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts);
 

	
 
/**
 
   Frees the struct options_common. This should be run before the program exits.
 
 */
 
int options_free(struct options_common *allopts);
 
#endif
0 comments (0 inline, 0 general)