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);
void
cfdev_add(struct cfdevlist *list, struct cfdev *templ, char *path)
struct cfdev *
cfdev_new(struct cfdev *templ)
{
struct cfdev *cd;
@ -280,38 +280,61 @@ cfdev_add(struct cfdevlist *list, struct cfdev *templ, char *path)
perror("malloc");
abort();
}
*cd = *templ;
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);
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
cfstr_add(struct cfstrlist *list, struct cfstr *templ, char *path)
cfdev_add(struct cfdevlist *list, struct cfdev *cd, char *path)
{
size_t len;
struct cfstr *cs;
unsigned hdr;
cd->path = path;
SLIST_INSERT_HEAD(list, cd, entry);
}
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));
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);
}
@ -464,7 +487,7 @@ aucat_main(int argc, char **argv)
int autostart;
struct dev *d, *dnext;
unsigned active;
unsigned nsock, nfile;
unsigned nsock;
/*
* global options defaults
@ -476,44 +499,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) {
@ -540,7 +532,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();
@ -582,14 +573,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':
@ -616,9 +608,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;
@ -663,15 +657,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
@ -899,29 +898,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) {
@ -934,9 +914,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);
@ -944,6 +926,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':
@ -986,14 +970,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));
@ -1010,8 +999,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;

View File

@ -14,9 +14,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "abuf.h"
@ -660,6 +661,25 @@ wav_quitreq(void *arg)
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
*/
@ -671,6 +691,7 @@ wav_new_in(struct fileops *ops,
int fd;
struct wav *f;
hdr = wav_autohdr(name, hdr);
if (name != NULL) {
fd = open(name, O_RDONLY | O_NONBLOCK, 0666);
if (fd < 0) {
@ -726,7 +747,7 @@ wav_new_in(struct fileops *ops,
f->join = join;
f->mode = mode;
f->hpar = *par;
f->hdr = 0;
f->hdr = hdr;
f->xrun = xrun;
f->maxweight = MIDI_TO_ADATA(volctl);
f->slot = ctl_slotnew(f->dev->midi, "play", &ctl_wavops, f, 1);
@ -760,6 +781,7 @@ wav_new_out(struct fileops *ops,
int fd;
struct wav *f;
hdr = wav_autohdr(name, hdr);
if (name == NULL) {
name = "stdout";
fd = STDOUT_FILENO;