Changeset 799f36c


Ignore:
Timestamp:
Sep 13, 2009, 8:06:06 PM (12 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
f350fc3
Parents:
7967433
git-author:
Nelson Elhage <nelhage@mit.edu> (09/13/09 19:51:10)
git-committer:
Nelson Elhage <nelhage@mit.edu> (09/13/09 20:06:06)
Message:
Use getopt(3) for option parsing.

While we're at it, refactor option parsing into its own function.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    r7967433 r799f36c  
    3535
    3636owl_global g;
     37
     38typedef struct _owl_options {           /* noproto */
     39  bool load_initial_subs;
     40  char *configfile;
     41  char *tty;
     42  char *confdir;
     43  bool debug;
     44  bool rm_debug;
     45} owl_options;
     46
     47/* TODO: free owl_options after init is done? */
     48void owl_parse_options(int argc, char *argv[], owl_options *opts) /* noproto */ {
     49  char c;
     50
     51  while((c = getopt(argc, argv, "nc:t:s:dDvh")) != -1) {
     52    switch(c) {
     53    case 'n':
     54      opts->load_initial_subs = 0;
     55      break;
     56    case 'c':
     57      opts->configfile = owl_strdup(optarg);
     58      break;
     59    case 's':
     60      opts->confdir = owl_strdup(optarg);
     61      break;
     62    case 't':
     63      opts->tty = owl_strdup(optarg);
     64      break;
     65    case 'D':
     66      opts->rm_debug = 1;
     67      /* fallthrough */
     68    case 'd':
     69      opts->debug = 1;
     70      break;
     71    case 'v':
     72      printf("This is barnowl version %s\n", OWL_VERSION_STRING);
     73      exit(0);
     74    case 'h':
     75    default:
     76      usage();
     77      exit(1);
     78    }
     79  }
     80}
    3781
    3882void owl_register_signal_handlers(void) /* noproto */ {
     
    156200  owl_editwin *tw;
    157201  owl_popwin *pw;
    158   int debug, argcsave, followlast;
     202  int argcsave, followlast;
    159203  int newmsgs, nexttimediff;
    160   const char *configfile, *tty, *const *argvsave;
     204  const char *const *argvsave;
    161205  char *perlout, *perlerr;
    162   const char *confdir;
    163206  const owl_style *s;
    164207  time_t nexttime, now;
     
    166209  const char *dir;
    167210  owl_message *m;
     211  owl_options opts;
    168212
    169213  if (!GLIB_CHECK_VERSION (2, 12, 0))
     
    172216  argcsave=argc;
    173217  argvsave=strs(argv);
    174   configfile=NULL;
    175   confdir = NULL;
    176   tty=NULL;
    177   debug=0;
    178   g.load_initial_subs = 1;
    179218
    180219  setlocale(LC_ALL, "");
    181  
    182   if (argc>0) {
    183     argv++;
    184     argc--;
    185   }
    186   while (argc>0) {
    187     if (!strcmp(argv[0], "-n")) {
    188       g.load_initial_subs = 0;
    189       argv++;
    190       argc--;
    191     } else if (!strcmp(argv[0], "-c")) {
    192       if (argc<2) {
    193         fprintf(stderr, "Too few arguments to -c\n");
    194         usage();
    195         exit(1);
    196       }
    197       configfile=argv[1];
    198       argv+=2;
    199       argc-=2;
    200     } else if (!strcmp(argv[0], "-t")) {
    201       if (argc<2) {
    202         fprintf(stderr, "Too few arguments to -t\n");
    203         usage();
    204         exit(1);
    205       }
    206       tty=argv[1];
    207       argv+=2;
    208       argc-=2;
    209     } else if (!strcmp(argv[0], "-s")){
    210       if (argc<2) {
    211         fprintf(stderr, "Too few arguments to -s\n");
    212         usage();
    213         exit(1);
    214       }
    215       confdir = argv[1];
    216       argv+=2;
    217       argc-=2;
    218     } else if (!strcmp(argv[0], "-d")) {
    219       debug=1;
    220       argv++;
    221       argc--;
    222     } else if (!strcmp(argv[0], "-D")) {
    223       debug=1;
    224       unlink(OWL_DEBUG_FILE);
    225       argv++;
    226       argc--;
    227     } else if (!strcmp(argv[0], "-v")) {
    228       printf("This is barnowl version %s\n", OWL_VERSION_STRING);
    229       exit(0);
    230     } else {
    231       fprintf(stderr, "Unknown argument\n");
    232       usage();       
    233       exit(1);
    234     }
    235   }
     220
     221  memset(&opts, 0, sizeof opts);
     222  opts.load_initial_subs = 1;
     223  owl_parse_options(argc, argv, &opts);
     224  g.load_initial_subs = opts.load_initial_subs;
    236225
    237226  owl_function_debugmsg("startup: Finished parsing arguments");
     
    242231  /* owl global init */
    243232  owl_global_init(&g);
    244   if (debug) owl_global_set_debug_on(&g);
    245   if (confdir) owl_global_set_confdir(&g, confdir);
     233  if (opts.rm_debug) unlink(OWL_DEBUG_FILE);
     234  if (opts.debug) owl_global_set_debug_on(&g);
     235  if (opts.confdir) owl_global_set_confdir(&g, opts.confdir);
    246236  owl_function_debugmsg("startup: first available debugging message");
    247237  owl_global_set_startupargs(&g, argcsave, argvsave);
     
    278268  /* set the tty, either from the command line, or by figuring it out */
    279269  owl_function_debugmsg("startup: setting tty name");
    280   if (tty) {
    281     owl_global_set_tty(&g, tty);
     270  if (opts.tty) {
     271    owl_global_set_tty(&g, opts.tty);
    282272  } else {
    283273    owl_global_set_tty(&g, owl_util_get_default_tty());
     
    287277  owl_function_debugmsg("startup: processing config file");
    288278  owl_context_set_readconfig(owl_global_get_context(&g));
    289   perlerr=owl_perlconfig_initperl(configfile, &argc, &argv, &env);
     279  perlerr=owl_perlconfig_initperl(opts.configfile, &argc, &argv, &env);
    290280  if (perlerr) {
    291281    endwin();
     
    369359  /* If we ever deprecate the mainloop hook, remove this. */
    370360  owl_select_add_timer(0, 1, owl_perlconfig_mainloop, NULL, NULL);
    371 
    372361
    373362  owl_function_debugmsg("startup: entering main loop");
Note: See TracChangeset for help on using the changeset viewer.