Changeset 6329cd5 for zwrite.c


Ignore:
Timestamp:
Sep 30, 2011, 11:09:17 PM (10 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.9
Children:
f89cc6f
Parents:
7b89e8c
git-author:
David Benjamin <davidben@mit.edu> (09/30/11 20:48:57)
git-committer:
David Benjamin <davidben@mit.edu> (09/30/11 23:09:17)
Message:
Allow building a zwrite from an argv

Add new versions of owl_zwrite_create and owl_zwrite_new. Note that this
means we rebuild z->zwriteline from scratch unconditionally. The only
behavior change is that the user's spacing and quoting is not preserved
in zwrite locktext.  For instance

  :zwrite     davidben       -i   "spaces yay?"

now gets a locktext of

  ----> zwrite davidben -i 'spaces yay?'

I think this isn't a big deal. It already happens for every other *write
locktext, and if we kill the buff argument, it'll happen there too.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zwrite.c

    rd953ede r6329cd5  
    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    owl_zwrite_delete(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  }
    120 
    121   g_strfreev(argv);
    122135
    123136  if (badargs) {
Note: See TracChangeset for help on using the changeset viewer.