diff options
author | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2011-06-03 10:05:28 +0000 |
---|---|---|
committer | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2011-06-03 10:05:28 +0000 |
commit | 46f85188c75fff49f7295ee4761f7b573ab029cc (patch) | |
tree | 4b478cbba95aa825d34922ec7a9c28f539aea081 /usr.bin/aucat/aucat.c | |
parent | a03e1c8277627b56bbf035a78c69e465f7de914d (diff) |
Reorganize the way command line options are parsed and stored.
No behaviour change.
Diffstat (limited to 'usr.bin/aucat/aucat.c')
-rw-r--r-- | usr.bin/aucat/aucat.c | 177 |
1 files changed, 82 insertions, 95 deletions
diff --git a/usr.bin/aucat/aucat.c b/usr.bin/aucat/aucat.c index ae3d916237f..b3a5fc1e96e 100644 --- a/usr.bin/aucat/aucat.c +++ b/usr.bin/aucat/aucat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aucat.c,v 1.115 2011/05/26 07:18:40 ratchov Exp $ */ +/* $OpenBSD: aucat.c,v 1.116 2011/06/03 10:05:27 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -269,8 +269,8 @@ struct cfnet { SLIST_HEAD(cfnetlist, cfnet); -void -cfdev_add(struct cfdevlist *list, struct cfdev *templ, char *path) +struct cfdev * +cfdev_new(struct cfdev *templ) { struct cfdev *cd; @@ -279,38 +279,61 @@ cfdev_add(struct cfdevlist *list, struct cfdev *templ, char *path) perror("malloc"); abort(); } - *cd = *templ; + if (templ) + memcpy(cd, templ, sizeof(struct cfdev)); + else { + aparams_init(&cd->ipar, 0, 1, DEFAULT_RATE); + aparams_init(&cd->opar, 0, 1, DEFAULT_RATE); + cd->bufsz = 0; + cd->round = 0; + cd->hold = 1; + cd->autovol = 1; + } + SLIST_INIT(&cd->ins); + SLIST_INIT(&cd->outs); + SLIST_INIT(&cd->opts); + SLIST_INIT(&cd->mids); + cd->path = NULL; + return cd; +} + +void +cfdev_add(struct cfdevlist *list, struct cfdev *cd, char *path) +{ cd->path = path; SLIST_INSERT_HEAD(list, cd, entry); - SLIST_INIT(&templ->ins); - SLIST_INIT(&templ->outs); - SLIST_INIT(&templ->opts); - SLIST_INIT(&templ->mids); } -void -cfstr_add(struct cfstrlist *list, struct cfstr *templ, char *path) +struct cfstr * +cfstr_new(struct cfstr *templ) { - size_t len; struct cfstr *cs; - unsigned hdr; - - if (templ->hdr == HDR_AUTO) { - len = strlen(path); - if (len >= 4 && strcasecmp(path + len - 4, ".wav") == 0) - hdr = HDR_WAV; - else - hdr = HDR_RAW; - } else - hdr = templ->hdr; + cs = malloc(sizeof(struct cfstr)); if (cs == NULL) { perror("malloc"); abort(); } - *cs = *templ; + if (templ) + memcpy(cs, templ, sizeof(struct cfstr)); + else { + aparams_init(&cs->ipar, 0, 1, DEFAULT_RATE); + aparams_init(&cs->opar, 0, 1, DEFAULT_RATE); + cs->mmc = 0; + cs->hdr = HDR_AUTO; + cs->xrun = XRUN_IGNORE; + cs->vol = MIDI_MAXCTL; + cs->mode = MODE_PLAY | MODE_REC; + cs->join = 1; + } + cs->path = NULL; + return cs; +} + +void +cfstr_add(struct cfstrlist *list, struct cfstr *cs, char *path) +{ cs->path = path; - cs->hdr = hdr; SLIST_INSERT_HEAD(list, cs, entry); } @@ -463,7 +486,7 @@ aucat_main(int argc, char **argv) int autostart; struct dev *d, *dnext; unsigned active; - unsigned nsock, nfile; + unsigned nsock; /* * global options defaults @@ -475,44 +498,13 @@ aucat_main(int argc, char **argv) n_flag = 0; SLIST_INIT(&cfdevs); SLIST_INIT(&cfnets); - nfile = nsock = 0; + nsock = 0; /* - * default stream params + * default device and stream */ - cs = malloc(sizeof(struct cfstr)); - if (cs == NULL) { - perror("malloc"); - exit(1); - } - aparams_init(&cs->ipar, 0, 1, DEFAULT_RATE); - aparams_init(&cs->opar, 0, 1, DEFAULT_RATE); - cs->mmc = 0; - cs->hdr = HDR_AUTO; - cs->xrun = XRUN_IGNORE; - cs->vol = MIDI_MAXCTL; - cs->mode = MODE_PLAY | MODE_REC; - cs->join = 1; - - /* - * default device - */ - cd = malloc(sizeof(struct cfdev)); - if (cd == NULL) { - perror("malloc"); - exit(1); - } - aparams_init(&cd->ipar, 0, 1, DEFAULT_RATE); - aparams_init(&cd->opar, 0, 1, DEFAULT_RATE); - SLIST_INIT(&cd->ins); - SLIST_INIT(&cd->outs); - SLIST_INIT(&cd->opts); - SLIST_INIT(&cd->mids); - cd->path = NULL; - cd->bufsz = 0; - cd->round = 0; - cd->hold = 1; - cd->autovol = 1; + cd = cfdev_new(NULL); + cs = cfstr_new(NULL); while ((c = getopt(argc, argv, "a:w:dnb:c:C:e:r:h:x:v:i:o:f:m:luq:s:U:L:t:j:z:")) != -1) { switch (c) { @@ -539,7 +531,6 @@ aucat_main(int argc, char **argv) break; case 'm': cs->mode = opt_mode(); - cd->mode = cs->mode; break; case 'h': cs->hdr = opt_hdr(); @@ -581,14 +572,15 @@ aucat_main(int argc, char **argv) break; case 'i': cfstr_add(&cd->ins, cs, optarg); - nfile++; + cs = cfstr_new(cs); break; case 'o': cfstr_add(&cd->outs, cs, optarg); - nfile++; + cs = cfstr_new(cs); break; case 's': cfstr_add(&cd->opts, cs, optarg); + cs = cfstr_new(cs); nsock++; break; case 'a': @@ -615,9 +607,11 @@ aucat_main(int argc, char **argv) SLIST_EMPTY(&cd->ins) && SLIST_EMPTY(&cd->outs)) { cfstr_add(&cd->opts, cs, DEFAULT_OPT); + cs = cfstr_new(cs); nsock++; } cfdev_add(&cfdevs, cd, optarg); + cd = cfdev_new(cd); break; case 'l': l_flag = 1; @@ -662,15 +656,20 @@ aucat_main(int argc, char **argv) SLIST_EMPTY(&cd->outs)) { cfstr_add(&cd->opts, cs, DEFAULT_OPT); nsock++; - } + } else + free(cs); if (!cd->hold) errx(1, "-a off not compatible with default device"); cfdev_add(&cfdevs, cd, "default"); + } else { + if (!SLIST_EMPTY(&cd->opts) || + !SLIST_EMPTY(&cd->ins) || + !SLIST_EMPTY(&cd->outs) || + !SLIST_EMPTY(&cd->outs)) + errx(1, "no device to attach last stream to"); + free(cs); + free(cd); } - if ((cs = SLIST_FIRST(&cd->opts)) || - (cs = SLIST_FIRST(&cd->ins)) || - (cs = SLIST_FIRST(&cd->outs))) - errx(1, "%s: no device to attach the stream to", cs->path); /* * Check modes and calculate "best" device parameters. Iterate over all @@ -898,29 +897,10 @@ midicat_main(int argc, char **argv) nsock = 0; /* - * default stream params + * default device and stream */ - cs = malloc(sizeof(struct cfstr)); - if (cs == NULL) { - perror("malloc"); - exit(1); - } - cs->hdr = HDR_RAW; - - /* - * default device - */ - cd = malloc(sizeof(struct cfdev)); - if (cd == NULL) { - perror("malloc"); - exit(1); - } - SLIST_INIT(&cd->ins); - SLIST_INIT(&cd->outs); - SLIST_INIT(&cd->opts); - SLIST_INIT(&cd->mids); - cd->path = NULL; - + cs = cfstr_new(NULL); + cd = cfdev_new(NULL); while ((c = getopt(argc, argv, "di:o:ls:q:U:L:")) != -1) { switch (c) { @@ -933,9 +913,11 @@ midicat_main(int argc, char **argv) break; case 'i': cfstr_add(&cd->ins, cs, optarg); + cs = cfstr_new(cs); break; case 'o': cfstr_add(&cd->outs, cs, optarg); + cs = cfstr_new(cs); break; case 'q': cfmid_add(&cd->mids, optarg); @@ -943,6 +925,8 @@ midicat_main(int argc, char **argv) case 's': cfstr_add(&cd->opts, cs, optarg); cfdev_add(&cfdevs, cd, optarg); + cd = cfdev_new(cd); + cs = cfstr_new(NULL); nsock++; break; case 'l': @@ -985,14 +969,19 @@ midicat_main(int argc, char **argv) */ if (SLIST_EMPTY(&cfdevs)) { if (SLIST_EMPTY(&cd->mids)) { - if (!SLIST_EMPTY(&cd->ins) || !SLIST_EMPTY(&cd->outs)) - cfmid_add(&cd->mids, "default"); - else { + if (SLIST_EMPTY(&cd->ins) && SLIST_EMPTY(&cd->outs)) { cfstr_add(&cd->opts, cs, DEFAULT_OPT); nsock++; + } else { + cfmid_add(&cd->mids, "default"); + free(cs); } - } + } else + free(cs); cfdev_add(&cfdevs, cd, "default"); + } else { + free(cs); + free(cd); } if (nsock > 0) { getbasepath(base, sizeof(path)); @@ -1009,8 +998,6 @@ midicat_main(int argc, char **argv) d = dev_new_thru(); if (d == NULL) errx(1, "%s: can't open device", cd->path); - if (!dev_ref(d)) - errx(1, "couldn't open midi thru box"); if (SLIST_EMPTY(&cd->opts) && APROC_OK(d->midi)) d->midi->flags |= APROC_QUIT; |