Reorganize the way command line options are parsed and stored.

No behaviour change.
This commit is contained in:
Alexandre Ratchov 2011-06-03 12:05:47 +02:00
parent 0101028663
commit fc036bc26e
2 changed files with 106 additions and 97 deletions

View File

@ -270,8 +270,8 @@ struct cfnet {
SLIST_HEAD(cfnetlist, cfnet); SLIST_HEAD(cfnetlist, cfnet);
void struct cfdev *
cfdev_add(struct cfdevlist *list, struct cfdev *templ, char *path) cfdev_new(struct cfdev *templ)
{ {
struct cfdev *cd; struct cfdev *cd;
@ -280,38 +280,61 @@ cfdev_add(struct cfdevlist *list, struct cfdev *templ, char *path)
perror("malloc"); perror("malloc");
abort(); abort();
} }
*cd = *templ; if (templ)
cd->path = path; memcpy(cd, templ, sizeof(struct cfdev));
SLIST_INSERT_HEAD(list, cd, entry); else {
SLIST_INIT(&templ->ins); aparams_init(&cd->ipar, 0, 1, DEFAULT_RATE);
SLIST_INIT(&templ->outs); aparams_init(&cd->opar, 0, 1, DEFAULT_RATE);
SLIST_INIT(&templ->opts); cd->bufsz = 0;
SLIST_INIT(&templ->mids); 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 void
cfstr_add(struct cfstrlist *list, struct cfstr *templ, char *path) cfdev_add(struct cfdevlist *list, struct cfdev *cd, char *path)
{ {
size_t len; cd->path = path;
struct cfstr *cs; SLIST_INSERT_HEAD(list, cd, entry);
unsigned hdr; }
struct cfstr *
cfstr_new(struct cfstr *templ)
{
struct cfstr *cs;
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)); cs = malloc(sizeof(struct cfstr));
if (cs == NULL) { if (cs == NULL) {
perror("malloc"); perror("malloc");
abort(); 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->path = path;
cs->hdr = hdr;
SLIST_INSERT_HEAD(list, cs, entry); SLIST_INSERT_HEAD(list, cs, entry);
} }
@ -464,7 +487,7 @@ aucat_main(int argc, char **argv)
int autostart; int autostart;
struct dev *d, *dnext; struct dev *d, *dnext;
unsigned active; unsigned active;
unsigned nsock, nfile; unsigned nsock;
/* /*
* global options defaults * global options defaults
@ -476,44 +499,13 @@ aucat_main(int argc, char **argv)
n_flag = 0; n_flag = 0;
SLIST_INIT(&cfdevs); SLIST_INIT(&cfdevs);
SLIST_INIT(&cfnets); SLIST_INIT(&cfnets);
nfile = nsock = 0; nsock = 0;
/* /*
* default stream params * default device and stream
*/ */
cs = malloc(sizeof(struct cfstr)); cd = cfdev_new(NULL);
if (cs == NULL) { cs = cfstr_new(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;
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) { 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) { switch (c) {
@ -540,7 +532,6 @@ aucat_main(int argc, char **argv)
break; break;
case 'm': case 'm':
cs->mode = opt_mode(); cs->mode = opt_mode();
cd->mode = cs->mode;
break; break;
case 'h': case 'h':
cs->hdr = opt_hdr(); cs->hdr = opt_hdr();
@ -582,14 +573,15 @@ aucat_main(int argc, char **argv)
break; break;
case 'i': case 'i':
cfstr_add(&cd->ins, cs, optarg); cfstr_add(&cd->ins, cs, optarg);
nfile++; cs = cfstr_new(cs);
break; break;
case 'o': case 'o':
cfstr_add(&cd->outs, cs, optarg); cfstr_add(&cd->outs, cs, optarg);
nfile++; cs = cfstr_new(cs);
break; break;
case 's': case 's':
cfstr_add(&cd->opts, cs, optarg); cfstr_add(&cd->opts, cs, optarg);
cs = cfstr_new(cs);
nsock++; nsock++;
break; break;
case 'a': case 'a':
@ -616,9 +608,11 @@ aucat_main(int argc, char **argv)
SLIST_EMPTY(&cd->ins) && SLIST_EMPTY(&cd->ins) &&
SLIST_EMPTY(&cd->outs)) { SLIST_EMPTY(&cd->outs)) {
cfstr_add(&cd->opts, cs, DEFAULT_OPT); cfstr_add(&cd->opts, cs, DEFAULT_OPT);
cs = cfstr_new(cs);
nsock++; nsock++;
} }
cfdev_add(&cfdevs, cd, optarg); cfdev_add(&cfdevs, cd, optarg);
cd = cfdev_new(cd);
break; break;
case 'l': case 'l':
l_flag = 1; l_flag = 1;
@ -663,15 +657,20 @@ aucat_main(int argc, char **argv)
SLIST_EMPTY(&cd->outs)) { SLIST_EMPTY(&cd->outs)) {
cfstr_add(&cd->opts, cs, DEFAULT_OPT); cfstr_add(&cd->opts, cs, DEFAULT_OPT);
nsock++; nsock++;
} } else
free(cs);
if (!cd->hold) if (!cd->hold)
errx(1, "-a off not compatible with default device"); errx(1, "-a off not compatible with default device");
cfdev_add(&cfdevs, cd, "default"); 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 * Check modes and calculate "best" device parameters. Iterate over all
@ -899,29 +898,10 @@ midicat_main(int argc, char **argv)
nsock = 0; nsock = 0;
/* /*
* default stream params * default device and stream
*/ */
cs = malloc(sizeof(struct cfstr)); cs = cfstr_new(NULL);
if (cs == NULL) { cd = cfdev_new(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;
while ((c = getopt(argc, argv, "di:o:ls:q:U:L:")) != -1) { while ((c = getopt(argc, argv, "di:o:ls:q:U:L:")) != -1) {
switch (c) { switch (c) {
@ -934,9 +914,11 @@ midicat_main(int argc, char **argv)
break; break;
case 'i': case 'i':
cfstr_add(&cd->ins, cs, optarg); cfstr_add(&cd->ins, cs, optarg);
cs = cfstr_new(cs);
break; break;
case 'o': case 'o':
cfstr_add(&cd->outs, cs, optarg); cfstr_add(&cd->outs, cs, optarg);
cs = cfstr_new(cs);
break; break;
case 'q': case 'q':
cfmid_add(&cd->mids, optarg); cfmid_add(&cd->mids, optarg);
@ -944,6 +926,8 @@ midicat_main(int argc, char **argv)
case 's': case 's':
cfstr_add(&cd->opts, cs, optarg); cfstr_add(&cd->opts, cs, optarg);
cfdev_add(&cfdevs, cd, optarg); cfdev_add(&cfdevs, cd, optarg);
cd = cfdev_new(cd);
cs = cfstr_new(NULL);
nsock++; nsock++;
break; break;
case 'l': case 'l':
@ -986,14 +970,19 @@ midicat_main(int argc, char **argv)
*/ */
if (SLIST_EMPTY(&cfdevs)) { if (SLIST_EMPTY(&cfdevs)) {
if (SLIST_EMPTY(&cd->mids)) { if (SLIST_EMPTY(&cd->mids)) {
if (!SLIST_EMPTY(&cd->ins) || !SLIST_EMPTY(&cd->outs)) if (SLIST_EMPTY(&cd->ins) && SLIST_EMPTY(&cd->outs)) {
cfmid_add(&cd->mids, "default");
else {
cfstr_add(&cd->opts, cs, DEFAULT_OPT); cfstr_add(&cd->opts, cs, DEFAULT_OPT);
nsock++; nsock++;
} else {
cfmid_add(&cd->mids, "default");
free(cs);
} }
} } else
free(cs);
cfdev_add(&cfdevs, cd, "default"); cfdev_add(&cfdevs, cd, "default");
} else {
free(cs);
free(cd);
} }
if (nsock > 0) { if (nsock > 0) {
getbasepath(base, sizeof(path)); getbasepath(base, sizeof(path));
@ -1010,8 +999,6 @@ midicat_main(int argc, char **argv)
d = dev_new_thru(); d = dev_new_thru();
if (d == NULL) if (d == NULL)
errx(1, "%s: can't open device", cd->path); 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)) if (SLIST_EMPTY(&cd->opts) && APROC_OK(d->midi))
d->midi->flags |= APROC_QUIT; d->midi->flags |= APROC_QUIT;

View File

@ -14,9 +14,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "abuf.h" #include "abuf.h"
@ -660,6 +661,25 @@ wav_quitreq(void *arg)
wav_exit(f); wav_exit(f);
} }
/*
* determine the header by the file name
*/
unsigned
wav_autohdr(char *name, unsigned hdr)
{
size_t len;
if (hdr != HDR_AUTO)
return hdr;
if (name == NULL)
return HDR_RAW;
len = strlen(name);
if (len >= 4 && strcasecmp(name + len - 4, ".wav") == 0)
return HDR_WAV;
else
return HDR_RAW;
}
/* /*
* create a file reader in the ``INIT'' state * create a file reader in the ``INIT'' state
*/ */
@ -671,6 +691,7 @@ wav_new_in(struct fileops *ops,
int fd; int fd;
struct wav *f; struct wav *f;
hdr = wav_autohdr(name, hdr);
if (name != NULL) { if (name != NULL) {
fd = open(name, O_RDONLY | O_NONBLOCK, 0666); fd = open(name, O_RDONLY | O_NONBLOCK, 0666);
if (fd < 0) { if (fd < 0) {
@ -726,7 +747,7 @@ wav_new_in(struct fileops *ops,
f->join = join; f->join = join;
f->mode = mode; f->mode = mode;
f->hpar = *par; f->hpar = *par;
f->hdr = 0; f->hdr = hdr;
f->xrun = xrun; f->xrun = xrun;
f->maxweight = MIDI_TO_ADATA(volctl); f->maxweight = MIDI_TO_ADATA(volctl);
f->slot = ctl_slotnew(f->dev->midi, "play", &ctl_wavops, f, 1); f->slot = ctl_slotnew(f->dev->midi, "play", &ctl_wavops, f, 1);
@ -760,6 +781,7 @@ wav_new_out(struct fileops *ops,
int fd; int fd;
struct wav *f; struct wav *f;
hdr = wav_autohdr(name, hdr);
if (name == NULL) { if (name == NULL) {
name = "stdout"; name = "stdout";
fd = STDOUT_FILENO; fd = STDOUT_FILENO;