Changeset - 2b98f7dd04fc
[Not reviewed]
default
0 1 0
Nathan Brink (binki) - 16 years ago 2009-05-21 23:16:55
ohnobinki@ohnopublishing.net
added getopt to distren client
1 file changed with 58 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/client/distren.c
Show inline comments
 
@@ -31,6 +31,7 @@
 

	
 
#include <stdio.h> /* sprintf, printf */
 
#include <stdlib.h> /* malloc, free */
 
#include <unistd.h> /* getopt */
 
#include <libxml/tree.h> /* Happy fun XML time */
 
#include <libxml/xmlwriter.h>
 
#include <confuse.h>
 
@@ -39,6 +40,11 @@
 

	
 
int main(int argc, char *argv[])
 
{
 
  char *input;
 
  char *output;
 
  
 
  char curopt;
 

	
 
  struct options_common *options;
 

	
 
  cfg_t *cfg;
 
@@ -52,16 +58,65 @@ int main(int argc, char *argv[])
 
      CFG_END()
 
    };
 

	
 
  int i;
 
  input = NULL;
 
  output = NULL;
 

	
 
  i = 1;
 

	
 
  while(-1 != (curopt = getopt(argc, argv, "i:o:h")))
 
    {
 
      if(curopt == ':')
 
	{
 
	  fprintf(stderr, "-%c: is missing an argument\n", optopt);
 
	  return 1;
 
	}
 
      else if(curopt == '?')
 
	{
 
	  fprintf(stderr, "-%c: invalid option specified\n", optopt);
 
	  return 1;
 
	}
 
      else if(curopt == 'h')
 
	{
 
	  fprintf(stderr, "Usage: %s -i <inputfile> -o <outputfile>\n", argv[0]);
 
	  fprintf(stderr, "\t-i\tSpecifies the input file\n\
 
\t-o\tSpecifies the output file\n\
 
\t-h\tShows this help message\n");
 
	  /*
 
	    don't return here because options_init will have an overall
 
	    options help page
 
	  */
 
	}
 
      else if(curopt == 'i')
 
	/* 
 
	   TODO: is strdup good or bad? what about 
 
	   the idea that all libs should allow plugging different
 
	   malloc/free implementations in?
 
	 */
 
	input = strdup(optarg);
 
      else if(curopt == 'o')
 
	output = strdup(optarg);
 
    } 
 
  /*
 
    parse the config file after the arguments so we can intercept -h for help
 
  */  
 
  if(options_init(argc, argv, &cfg, cfg_opts, "client", &options))
 
    {
 
      fprintf(stderr, "error getting configuration\n");
 
      //return 1;
 
    }
 

	
 
  /* 
 
     give this error after the general arguments parsing so that
 
     the general help from options_init can have effect 
 
  */
 
  if(!input
 
     || !output)
 
    {
 
      fprintf(stderr, "Input and output files must be specified\n");
 
      return 1;
 
    }
 

	
 
  fprintf(stderr, "reading from %s\nwriting to %s\n", input, output);
 
  
 
  
 

	
 
  // Please find a better way of doing this :( you can't strcat multiple strings.. meh
 
  // use something like sprintf
0 comments (0 inline, 0 general)