Changeset 6503194


Ignore:
Timestamp:
Oct 10, 2011, 8:07:20 PM (10 years ago)
Author:
GitHub Merge Button <merge-button@github.com>
Parents:
ef4074b (diff), e21b921 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:
Merge e21b9212952a5f6ff9f30193013153a0470944bb into ef4074b758108eb46e2b1884e950d6f092a01260
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • owl.h

    rb9517cf rc0e728a  
    237237  void *pval_default;  /* for types other and string */
    238238  int   ival_default;  /* for types int and bool     */
    239   const char *validsettings;    /* documentation of valid settings */
     239  char *validsettings;          /* documentation of valid settings */
    240240  char *summary;                /* summary of usage */
    241241  char *description;            /* detailed description */
  • perl/lib/BarnOwl.pm

    r7803326 rcce9369  
    1717                    add_io_dispatch remove_io_dispatch
    1818                    new_command
    19                     new_variable_int new_variable_bool new_variable_string
     19                    new_variable_int new_variable_bool new_variable_string new_variable_enum
    2020                    quote redisplay);
    2121our %EXPORT_TAGS = (all => \@EXPORT_OK);
     
    389389=head2 new_variable_string NAME [{ARGS}]
    390390
    391 Add a new owl variable, either an int, a bool, or a string, with the
    392 specified name.
     391=head2 new_variable_enum NAME [{ARGS}]
     392
     393Add a new owl variable, either an int, a bool, a string, or an enum,
     394with the specified name.
    393395
    394396ARGS can optionally contain the following keys:
     
    398400=item default
    399401
    400 The default and initial value for the variable
     402The default and initial value for the variable.
     403Note that this should be a string value for an enum.
    401404
    402405=item summary
     
    408411A longer description of the function of the variable
    409412
     413=item valid_settings
     414
     415A listref of valid setttings for the enum variable, or a string describing
     416the valid settings for any other type of variable.  The settings for an enum
     417variable may not contain any commas.  You should not specify valid settings
     418for boolean variables.
     419
    410420=back
    411421
     
    413423
    414424sub new_variable_int {
    415     unshift @_, \&BarnOwl::Internal::new_variable_int, 0;
    416     goto \&_new_variable;
     425    _new_variable(\&BarnOwl::Internal::new_variable_int, 0, "<int>", @_);
    417426}
    418427
    419428sub new_variable_bool {
    420     unshift @_, \&BarnOwl::Internal::new_variable_bool, 0;
    421     goto \&_new_variable;
     429    _new_variable(\&BarnOwl::Internal::new_variable_bool, 0, "on,off", @_);
    422430}
    423431
    424432sub new_variable_string {
    425     unshift @_, \&BarnOwl::Internal::new_variable_string, "";
    426     goto \&_new_variable;
    427 }
    428 
    429 sub _new_variable {
    430     my $func = shift;
    431     my $default_default = shift;
     433    _new_variable(\&BarnOwl::Internal::new_variable_string, "", "<string>", @_);
     434}
     435
     436sub new_variable_enum {
    432437    my $name = shift;
    433438    my $args = shift || {};
     
    435440        summary     => "",
    436441        description => "",
    437         default     => $default_default,
    438442        %{$args});
    439     $func->($name, $args{default}, $args{summary}, $args{description});
     443
     444    my @valid_settings =  @{$args{valid_settings}};
     445    if (defined $args{default}) {
     446        ($args{default}) = grep { $args{default} eq $valid_settings[$_] } 0..$#valid_settings; # turn the string default into a numerical default
     447    } else {
     448        $args{default} = 0;
     449    }
     450    $args{valid_settings} = join ",", @valid_settings;
     451
     452    BarnOwl::Internal::new_variable_enum($name, $args{default}, $args{summary}, $args{description}, $args{valid_settings});
     453}
     454
     455sub _new_variable {
     456    my $func = shift;
     457    my $default_default = shift;
     458    my $default_valid_settings = shift;
     459    my $name = shift;
     460    my $args = shift || {};
     461    my %args = (
     462        summary        => "",
     463        description    => "",
     464        default        => $default_default,
     465        valid_settings => $default_valid_settings,
     466        %{$args});
     467    $func->($name, $args{default}, $args{summary}, $args{description}, $args{valid_settings});
    440468}
    441469
  • perlglue.xs

    re89ec48 rcce9369  
    405405
    406406void
    407 new_variable_string(name, ival, summ, desc)
     407new_variable_string(name, ival, summ, desc, validset)
    408408        const char * name
    409409        const char * ival
    410410        const char * summ
    411411        const char * desc
     412        const char * validset
    412413        CODE:
    413414        owl_variable_dict_newvar_string(owl_global_get_vardict(&g),
     
    415416                                        summ,
    416417                                        desc,
    417                                         ival);
    418 
    419 void
    420 new_variable_int(name, ival, summ, desc)
     418                                        ival,
     419                                        validset);
     420
     421void
     422new_variable_int(name, ival, summ, desc, validset)
    421423        const char * name
    422424        int ival
    423425        const char * summ
    424426        const char * desc
     427        const char * validset
    425428        CODE:
    426429        owl_variable_dict_newvar_int(owl_global_get_vardict(&g),
     
    428431                                     summ,
    429432                                     desc,
    430                                      ival);
    431 
    432 void
    433 new_variable_bool(name, ival, summ, desc)
     433                                     ival,
     434                                     validset);
     435
     436void
     437new_variable_bool(name, ival, summ, desc, validset)
    434438        const char * name
    435439        int ival
    436440        const char * summ
    437441        const char * desc
    438         CODE:
     442        const char * validset
     443        CODE:
     444        if (strcmp(validset, "on,off")) /* gcc complains if we don't do anything with it, but the perl code isn't nice enough to let us get rid of it */
     445                croak("Invalid bool variable valid settings: %s", validset);
    439446        owl_variable_dict_newvar_bool(owl_global_get_vardict(&g),
    440447                                      name,
     
    442449                                      desc,
    443450                                      ival);
     451
     452void
     453new_variable_enum(name, ival, summ, desc, validset)
     454        const char * name
     455        int ival
     456        const char * summ
     457        const char * desc
     458        const char * validset
     459        CODE:
     460        owl_variable_dict_newvar_enum(owl_global_get_vardict(&g),
     461                                      name,
     462                                      summ,
     463                                      desc,
     464                                      ival,
     465                                      validset);
    444466
    445467void
  • tester.c

    ra74a044 re21b921  
    390390  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
    391391
    392   owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
     392  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval", "<string>");
    393393  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
    394394  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
     
    397397  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
    398398
    399   owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
     399  owl_variable_dict_newvar_enum(&vd, "enumvar", "", "", 0, "foo,bar,baz");
     400  FAIL_UNLESS("get new enum var", NULL != (var = owl_variable_get_var(&vd, "enumvar")));
     401  FAIL_UNLESS("get new enum var", NULL != (v = owl_variable_get(var)));
     402  FAIL_UNLESS("get new enum val tostring", !strcmp("foo", value = owl_variable_get_tostring(var)));
     403  g_free(value);
     404  FAIL_UNLESS("valid update enum var", 0 == owl_variable_set_int(var, 1));
     405  FAIL_UNLESS("update int enum val tostring", !strcmp("bar", value = owl_variable_get_tostring(var)));
     406  g_free(value);
     407  FAIL_UNLESS("update int enum val", 1 == owl_variable_get_int(var));
     408  FAIL_UNLESS("invalid update enum var", -1 == owl_variable_set_int(var, 5));
     409  FAIL_UNLESS("no update int enum val", 1 == owl_variable_get_int(var));
     410  FAIL_UNLESS("valid update enum var fromstring", 0 == owl_variable_set_fromstring(var, "baz", 0));
     411  FAIL_UNLESS("update int enum val fromstring", 2 == owl_variable_get_int(var));
     412  FAIL_UNLESS("invalid update enum var fromstring", -1 == owl_variable_set_fromstring(var, "xxx", 0));
     413  FAIL_UNLESS("no update int enum val fromstring", 2 == owl_variable_get_int(var));
     414
     415  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47, 0);
    400416  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
    401417  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
     
    411427  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
    412428
    413   owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);
     429  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL, "<string>");
    414430  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
    415431  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
  • variable.c

    rf271129 rcce9369  
    33
    44#define OWLVAR_BOOL(name,default,summary,description) \
    5         { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \
     5        { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, g_strdup("on,off"), g_strdup(summary), g_strdup(description), NULL, \
    66        NULL, NULL, NULL, NULL, NULL, NULL }
    77
    88#define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \
    9         { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \
     9        { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, g_strdup("on,off"), g_strdup(summary), g_strdup(description), NULL, \
    1010        validate, set, NULL, get, NULL, NULL }
    1111
    1212#define OWLVAR_INT(name,default,summary,description) \
    13         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, "<int>", g_strdup(summary), g_strdup(description), NULL, \
     13        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, g_strdup("<int>"), g_strdup(summary), g_strdup(description), NULL, \
    1414        NULL, NULL, NULL, NULL, NULL, NULL }
    1515
    1616#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
    17         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
     17        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, g_strdup(validset), g_strdup(summary), g_strdup(description), NULL, \
    1818        validate, set, NULL, get, NULL, NULL }
    1919
    2020#define OWLVAR_PATH(name,default,summary,description) \
    21         { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<path>", g_strdup(summary), g_strdup(description),  NULL, \
     21        { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, g_strdup("<path>"), g_strdup(summary), g_strdup(description),  NULL, \
    2222        NULL, NULL, NULL, NULL, NULL, NULL }
    2323
    2424#define OWLVAR_STRING(name,default,summary,description) \
    25         { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<string>", g_strdup(summary), g_strdup(description), NULL, \
     25        { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, g_strdup("<string>"), g_strdup(summary), g_strdup(description), NULL, \
    2626        NULL, NULL, NULL, NULL, NULL, NULL }
    2727
    2828#define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \
    29         { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, validset, g_strdup(summary), g_strdup(description), NULL, \
     29        { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, g_strdup(validset), g_strdup(summary), g_strdup(description), NULL, \
    3030        validate, set, NULL, get, NULL, NULL }
    3131
     
    3434 * correspond to the values that may be specified. */
    3535#define OWLVAR_ENUM(name,default,summary,description,validset) \
    36         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
     36        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, g_strdup(validset), g_strdup(summary), g_strdup(description), NULL, \
    3737        owl_variable_enum_validate, \
    3838        NULL, owl_variable_enum_set_fromstring, \
     
    4141
    4242#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
    43         { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \
     43        { g_strdup(name), OWL_VARIABLE_INT, NULL, default, g_strdup(validset), g_strdup(summary), g_strdup(description), NULL, \
    4444        validate, \
    4545        set, owl_variable_enum_set_fromstring, \
     
    568568int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable *variables_to_init)
    569569{
     570  int ret;
    570571  owl_variable *var, *cur;
    571572  for (var = variables_to_init; var->name != NULL; var++) {
     
    576577    cur->summary = g_strdup(var->summary);
    577578    cur->description = g_strdup(var->description);
    578     switch (cur->type) {
     579    cur->validsettings = g_strdup(var->validsettings);
     580    if (cur->type == OWL_VARIABLE_STRING)
     581      cur->pval_default = g_strdup(var->pval_default);
     582    ret = owl_variable_init_defaults(cur);
     583    if (ret != 0) {
     584      fprintf(stderr, "owl_variable_setup: invalid variable type\n");
     585      return ret;
     586    }
     587    owl_dict_insert_element(vd, cur->name, cur, NULL);
     588  }
     589  return 0;
     590}
     591
     592int owl_variable_init_defaults(owl_variable *cur)
     593{
     594  switch (cur->type) {
    579595    case OWL_VARIABLE_OTHER:
    580596      cur->set_fn(cur, cur->pval_default);
     
    593609      if (!cur->delete_fn)
    594610        cur->delete_fn = owl_variable_delete_default;
    595       cur->pval_default = g_strdup(var->pval_default);
    596611      cur->set_fn(cur, cur->pval_default);
    597612      break;
     
    629644      break;
    630645    default:
    631       fprintf(stderr, "owl_variable_setup: invalid variable type\n");
    632       return(-2);
    633     }
    634     owl_dict_insert_element(vd, cur->name, cur, NULL);
     646      return -2;
    635647  }
    636648  return 0;
     
    642654}
    643655
    644 CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description)
     656CALLER_OWN owl_variable *owl_variable_newvar(const char *name, const char *summary, const char *description, const char *validsettings, int type)
    645657{
    646658  owl_variable * var = g_new0(owl_variable, 1);
     
    648660  var->summary = g_strdup(summary);
    649661  var->description = g_strdup(description);
     662  var->validsettings = g_strdup(validsettings);
     663  var->type = type;
    650664  return var;
    651665}
    652666
    653 void owl_variable_update(owl_variable *var, const char *summary, const char *desc) {
    654   g_free(var->summary);
    655   var->summary = g_strdup(summary);
    656   g_free(var->description);
    657   var->description = g_strdup(desc);
    658 }
    659 
    660 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval)
     667void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval, const char *validsettings)
    661668{
    662669  owl_variable *old = owl_variable_get_var(vd, name);
    663   if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) {
    664     owl_variable_update(old, summ, desc);
    665     g_free(old->pval_default);
    666     old->pval_default = g_strdup(initval);
    667   } else {
    668     owl_variable * var = owl_variable_newvar(name, summ, desc);
    669     var->type = OWL_VARIABLE_STRING;
    670     var->validsettings = "<string>";
    671     var->pval_default = g_strdup(initval);
    672     var->set_fn = owl_variable_string_set_default;
    673     var->set_fromstring_fn = owl_variable_string_set_fromstring_default;
    674     var->get_fn = owl_variable_get_default;
    675     var->get_tostring_fn = owl_variable_string_get_tostring_default;
    676     var->delete_fn = owl_variable_delete_default;
    677     var->set_fn(var, initval);
    678     owl_variable_dict_add_variable(vd, var);
    679   }
    680 }
    681 
    682 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval)
     670  char *oldval = NULL;
     671  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING)
     672    oldval = owl_variable_get_tostring(old);
     673  owl_variable *var = owl_variable_newvar(name, summ, desc, validsettings, OWL_VARIABLE_STRING);
     674  var->pval_default = g_strdup(initval);
     675  owl_variable_init_defaults(var);
     676  if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING)
     677    var->set_fn(var, oldval);
     678  g_free(oldval);
     679  owl_variable_dict_add_variable(vd, var);
     680}
     681
     682void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval, const char *validsettings)
    683683{
    684684  owl_variable *old = owl_variable_get_var(vd, name);
    685   if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) {
    686     owl_variable_update(old, summ, desc);
    687     old->ival_default = initval;
    688   } else {
    689     owl_variable * var = owl_variable_newvar(name, summ, desc);
    690     var->type = OWL_VARIABLE_INT;
    691     var->validsettings = "<int>";
    692     var->ival_default = initval;
    693     var->validate_fn = owl_variable_int_validate_default;
    694     var->set_fn = owl_variable_int_set_default;
    695     var->set_fromstring_fn = owl_variable_int_set_fromstring_default;
    696     var->get_fn = owl_variable_get_default;
    697     var->get_tostring_fn = owl_variable_int_get_tostring_default;
    698     var->delete_fn = owl_variable_delete_default;
    699     var->val = g_new(int, 1);
    700     var->set_fn(var, &initval);
    701     owl_variable_dict_add_variable(vd, var);
    702   }
     685  int oldval;
     686  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT)
     687    oldval = owl_variable_get_int(old);
     688  owl_variable *var = owl_variable_newvar(name, summ, desc, validsettings, OWL_VARIABLE_INT);
     689  var->ival_default = initval;
     690  owl_variable_init_defaults(var);
     691  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT)
     692    var->set_fn(var, &oldval);
     693  owl_variable_dict_add_variable(vd, var);
    703694}
    704695
     
    706697{
    707698  owl_variable *old = owl_variable_get_var(vd, name);
    708   if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) {
    709     owl_variable_update(old, summ, desc);
    710     old->ival_default = initval;
    711   } else {
    712     owl_variable * var = owl_variable_newvar(name, summ, desc);
    713     var->type = OWL_VARIABLE_BOOL;
    714     var->validsettings = "on,off";
    715     var->ival_default = initval;
    716     var->validate_fn = owl_variable_bool_validate_default;
    717     var->set_fn = owl_variable_bool_set_default;
    718     var->set_fromstring_fn = owl_variable_bool_set_fromstring_default;
    719     var->get_fn = owl_variable_get_default;
    720     var->get_tostring_fn = owl_variable_bool_get_tostring_default;
    721     var->delete_fn = owl_variable_delete_default;
    722     var->val = g_new(int, 1);
    723     var->set_fn(var, &initval);
    724     owl_variable_dict_add_variable(vd, var);
    725   }
     699  int oldval;
     700  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL)
     701    oldval = owl_variable_get_bool(old);
     702  owl_variable *var = owl_variable_newvar(name, summ, desc, "on,off", OWL_VARIABLE_BOOL);
     703  var->ival_default = initval;
     704  owl_variable_init_defaults(var);
     705  if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL)
     706    var->set_fn(var, &oldval);
     707  owl_variable_dict_add_variable(vd, var);
     708}
     709
     710void owl_variable_dict_newvar_enum(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval, const char *validsettings)
     711{
     712  owl_variable *old = owl_variable_get_var(vd, name);
     713  int oldval;
     714  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT)
     715    oldval = owl_variable_get_int(old);
     716  owl_variable *var = owl_variable_newvar(name, summ, desc, validsettings, OWL_VARIABLE_INT);
     717  var->ival_default = initval;
     718  var->validate_fn = owl_variable_enum_validate;
     719  var->set_fromstring_fn = owl_variable_enum_set_fromstring;
     720  var->get_tostring_fn = owl_variable_enum_get_tostring;
     721  owl_variable_init_defaults(var);
     722  if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT)
     723    var->set_fn(var, &oldval);
     724  owl_variable_dict_add_variable(vd, var);
    726725}
    727726
     
    741740  g_free(v->summary);
    742741  g_free(v->description);
     742  g_free(v->validsettings);
    743743  if (v->type == OWL_VARIABLE_STRING)
    744744    g_free(v->pval_default);
  • commands.c

    r7803326 rf89cc6f  
    19851985  }
    19861986  /* check for a zwrite -m */
    1987   z = owl_zwrite_new(buff);
     1987  z = owl_zwrite_new(argc, argv);
    19881988  if (!z) {
    19891989    owl_function_error("Error in zwrite arguments");
  • perl/lib/BarnOwl/ModuleLoader.pm

    rf544216 rf34728b  
    127127}
    128128
     129sub complete_module_name {
     130    return sort(keys %modules);
     131}
     132
    129133sub register_keybindings {
    130134    BarnOwl::new_command('reload-modules', sub {BarnOwl::ModuleLoader->reload}, {
     
    138142                           description => q{Reloads a single module located in ~/.owl/modules or the system modules directory}
    139143                          });
     144
     145    BarnOwl::Completion::register_completer('reload-module', \&complete_module_name);
    140146}
    141147
  • util.c

    r6646fdb r7b89e8c  
    262262CALLER_OWN char *owl_arg_quote(const char *arg)
    263263{
    264   GString *buf = g_string_new("");;
     264  GString *buf = g_string_new("");
    265265  owl_string_append_quoted_arg(buf, arg);
     266  return g_string_free(buf, false);
     267}
     268
     269/* Returns a quoted version of argv. owl_parseline on the result should give
     270 * back the input. */
     271CALLER_OWN char *owl_argv_quote(int argc, const char *const *argv)
     272{
     273  int i;
     274  GString *buf = g_string_new("");
     275  for (i = 0; i < argc; i++) {
     276    if (i > 0)
     277      g_string_append_c(buf, ' ');
     278    owl_string_append_quoted_arg(buf, argv[i]);
     279  }
    266280  return g_string_free(buf, false);
    267281}
  • zephyr.c

    rf271129 rd953ede  
    909909  g_free(to);
    910910
    911   z = owl_zwrite_new(tmpbuff);
     911  z = owl_zwrite_new_from_line(tmpbuff);
    912912  g_free(tmpbuff);
    913913  if (z == NULL) {
  • zwrite.c

    rf271129 ref4074b  
    11#include "owl.h"
    22
    3 CALLER_OWN owl_zwrite *owl_zwrite_new(const char *line)
     3CALLER_OWN owl_zwrite *owl_zwrite_new_from_line(const char *line)
    44{
    55  owl_zwrite *z = g_new(owl_zwrite, 1);
    6   if (owl_zwrite_create_from_line(z, line) < 0) {
    7     owl_zwrite_delete(z);
     6  if (owl_zwrite_create_from_line(z, line) != 0) {
     7    g_free(z);
    88    return NULL;
    99  }
     
    1111}
    1212
     13CALLER_OWN owl_zwrite *owl_zwrite_new(int argc, const char *const *argv)
     14{
     15  owl_zwrite *z = g_new(owl_zwrite, 1);
     16  if (owl_zwrite_create(z, argc, argv) != 0) {
     17    g_free(z);
     18    return NULL;
     19  }
     20  return z;
     21}
     22
    1323G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create_from_line(owl_zwrite *z, const char *line)
    1424{
    15   int argc, badargs, myargc;
     25  int argc;
    1626  char **argv;
    17   const char *const *myargv;
     27  int ret;
     28
     29  /* parse the command line for options */
     30  argv = owl_parseline(line, &argc);
     31  if (argc < 0) {
     32    owl_function_error("Unbalanced quotes in zwrite");
     33    return -1;
     34  }
     35  ret = owl_zwrite_create(z, argc, strs(argv));
     36  g_strfreev(argv);
     37  return ret;
     38}
     39
     40G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create(owl_zwrite *z, int argc, const char *const *argv)
     41{
     42  int badargs = 0;
    1843  char *msg = NULL;
    19 
    20   badargs=0;
    2144 
    2245  /* start with null entries */
     
    3154  z->noping=0;
    3255  z->recips = g_ptr_array_new();
    33   z->zwriteline = g_strdup(line);
    34 
    35   /* parse the command line for options */
    36   argv=owl_parseline(line, &argc);
    37   myargv=strs(argv);
    38   if (argc<0) {
    39     owl_function_error("Unbalanced quotes in zwrite");
    40     return(-1);
    41   }
    42   myargc=argc;
    43   if (myargc && *(myargv[0])!='-') {
    44     z->cmd=g_strdup(myargv[0]);
    45     myargc--;
    46     myargv++;
    47   }
    48   while (myargc) {
    49     if (!strcmp(myargv[0], "-c")) {
    50       if (myargc<2) {
    51         badargs=1;
    52         break;
    53       }
    54       z->class=owl_validate_utf8(myargv[1]);
    55       myargv+=2;
    56       myargc-=2;
    57     } else if (!strcmp(myargv[0], "-i")) {
    58       if (myargc<2) {
    59         badargs=1;
    60         break;
    61       }
    62       z->inst=owl_validate_utf8(myargv[1]);
    63       myargv+=2;
    64       myargc-=2;
    65     } else if (!strcmp(myargv[0], "-r")) {
    66       if (myargc<2) {
    67         badargs=1;
    68         break;
    69       }
    70       z->realm=owl_validate_utf8(myargv[1]);
    71       myargv+=2;
    72       myargc-=2;
    73     } else if (!strcmp(myargv[0], "-s")) {
    74       if (myargc<2) {
    75         badargs=1;
    76         break;
    77       }
    78       z->zsig=owl_validate_utf8(myargv[1]);
    79       myargv+=2;
    80       myargc-=2;
    81     } else if (!strcmp(myargv[0], "-O")) {
    82       if (myargc<2) {
    83         badargs=1;
    84         break;
    85       }
    86       z->opcode=owl_validate_utf8(myargv[1]);
    87       myargv+=2;
    88       myargc-=2;
    89     } else if (!strcmp(myargv[0], "-m")) {
    90       if (myargc<2) {
     56  z->zwriteline = owl_argv_quote(argc, argv);
     57
     58  if (argc && *(argv[0])!='-') {
     59    z->cmd=g_strdup(argv[0]);
     60    argc--;
     61    argv++;
     62  }
     63  while (argc) {
     64    if (!strcmp(argv[0], "-c")) {
     65      if (argc<2) {
     66        badargs=1;
     67        break;
     68      }
     69      z->class=owl_validate_utf8(argv[1]);
     70      argv+=2;
     71      argc-=2;
     72    } else if (!strcmp(argv[0], "-i")) {
     73      if (argc<2) {
     74        badargs=1;
     75        break;
     76      }
     77      z->inst=owl_validate_utf8(argv[1]);
     78      argv+=2;
     79      argc-=2;
     80    } else if (!strcmp(argv[0], "-r")) {
     81      if (argc<2) {
     82        badargs=1;
     83        break;
     84      }
     85      z->realm=owl_validate_utf8(argv[1]);
     86      argv+=2;
     87      argc-=2;
     88    } else if (!strcmp(argv[0], "-s")) {
     89      if (argc<2) {
     90        badargs=1;
     91        break;
     92      }
     93      z->zsig=owl_validate_utf8(argv[1]);
     94      argv+=2;
     95      argc-=2;
     96    } else if (!strcmp(argv[0], "-O")) {
     97      if (argc<2) {
     98        badargs=1;
     99        break;
     100      }
     101      z->opcode=owl_validate_utf8(argv[1]);
     102      argv+=2;
     103      argc-=2;
     104    } else if (!strcmp(argv[0], "-m")) {
     105      if (argc<2) {
    91106        badargs=1;
    92107        break;
     
    99114
    100115      /* Once we have -m, gobble up everything else on the line */
    101       myargv++;
    102       myargc--;
    103       msg = g_strjoinv(" ", (char**)myargv);
     116      argv++;
     117      argc--;
     118      msg = g_strjoinv(" ", (char**)argv);
    104119      break;
    105     } else if (!strcmp(myargv[0], "-C")) {
     120    } else if (!strcmp(argv[0], "-C")) {
    106121      z->cc=1;
    107       myargv++;
    108       myargc--;
    109     } else if (!strcmp(myargv[0], "-n")) {
     122      argv++;
     123      argc--;
     124    } else if (!strcmp(argv[0], "-n")) {
    110125      z->noping=1;
    111       myargv++;
    112       myargc--;
     126      argv++;
     127      argc--;
    113128    } else {
    114129      /* anything unattached is a recipient */
    115       g_ptr_array_add(z->recips, owl_validate_utf8(myargv[0]));
    116       myargv++;
    117       myargc--;
     130      g_ptr_array_add(z->recips, owl_validate_utf8(argv[0]));
     131      argv++;
     132      argc--;
    118133    }
    119134  }
    120135
    121   g_strfreev(argv);
    122 
    123136  if (badargs) {
     137    owl_zwrite_cleanup(z);
    124138    return(-1);
    125139  }
     
    129143      z->recips->len == 0) {
    130144    owl_function_error("You must specify a recipient for zwrite");
     145    owl_zwrite_cleanup(z);
    131146    return(-1);
    132147  }
     
    254269  owl_zwrite z;
    255270  int rv;
    256   rv=owl_zwrite_create_from_line(&z, cmd);
    257   if (rv) return(rv);
     271  rv = owl_zwrite_create_from_line(&z, cmd);
     272  if (rv != 0) return rv;
    258273  if (!owl_zwrite_is_message_set(&z)) {
    259274    owl_zwrite_set_message(&z, msg);
Note: See TracChangeset for help on using the changeset viewer.