[BACK]Return to opt.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / sndiod

Annotation of src/usr.bin/sndiod/opt.c, Revision 1.6

1.6     ! ratchov     1: /*     $OpenBSD: opt.c,v 1.5 2021/01/29 11:21:00 ratchov Exp $ */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008-2011 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <string.h>
                     18:
                     19: #include "dev.h"
                     20: #include "opt.h"
                     21: #include "utils.h"
                     22:
1.5       ratchov    23: struct opt *opt_list;
                     24:
1.1       ratchov    25: /*
                     26:  * create a new audio sub-device "configuration"
                     27:  */
                     28: struct opt *
1.4       ratchov    29: opt_new(struct dev *d, char *name,
1.1       ratchov    30:     int pmin, int pmax, int rmin, int rmax,
                     31:     int maxweight, int mmc, int dup, unsigned int mode)
                     32: {
1.6     ! ratchov    33:        struct opt *o, **po;
        !            34:        unsigned int len, num;
1.1       ratchov    35:        char c;
                     36:
                     37:        for (len = 0; name[len] != '\0'; len++) {
                     38:                if (len == OPT_NAMEMAX) {
1.3       ratchov    39:                        log_puts(name);
                     40:                        log_puts(": too long\n");
                     41:                        return NULL;
1.1       ratchov    42:                }
                     43:                c = name[len];
                     44:                if ((c < 'a' || c > 'z') &&
                     45:                    (c < 'A' || c > 'Z')) {
1.3       ratchov    46:                        log_puts(name);
                     47:                        log_puts(": only alphabetic chars allowed\n");
                     48:                        return NULL;
1.1       ratchov    49:                }
                     50:        }
1.6     ! ratchov    51:        num = 0;
        !            52:        for (po = &opt_list; *po != NULL; po = &(*po)->next)
        !            53:                num++;
        !            54:        if (num >= OPT_NMAX) {
        !            55:                log_puts(name);
        !            56:                log_puts(": too many opts\n");
        !            57:                return NULL;
        !            58:        }
        !            59:        if (opt_byname(d, name)) {
        !            60:                dev_log(d);
        !            61:                log_puts(".");
        !            62:                log_puts(name);
        !            63:                log_puts(": already defined\n");
        !            64:                return NULL;
        !            65:        }
1.1       ratchov    66:        o = xmalloc(sizeof(struct opt));
1.6     ! ratchov    67:        o->num = num;
1.5       ratchov    68:        o->dev = d;
1.1       ratchov    69:        if (mode & MODE_PLAY) {
                     70:                o->pmin = pmin;
                     71:                o->pmax = pmax;
                     72:        }
                     73:        if (mode & MODE_RECMASK) {
                     74:                o->rmin = rmin;
                     75:                o->rmax = rmax;
                     76:        }
                     77:        o->maxweight = maxweight;
                     78:        o->mmc = mmc;
                     79:        o->dup = dup;
                     80:        o->mode = mode;
                     81:        memcpy(o->name, name, len + 1);
1.6     ! ratchov    82:        o->next = *po;
        !            83:        *po = o;
1.1       ratchov    84:        if (log_level >= 2) {
1.4       ratchov    85:                dev_log(d);
1.1       ratchov    86:                log_puts(".");
                     87:                log_puts(o->name);
                     88:                log_puts(":");
                     89:                if (o->mode & MODE_REC) {
                     90:                        log_puts(" rec=");
                     91:                        log_putu(o->rmin);
                     92:                        log_puts(":");
                     93:                        log_putu(o->rmax);
                     94:                }
                     95:                if (o->mode & MODE_PLAY) {
                     96:                        log_puts(" play=");
                     97:                        log_putu(o->pmin);
                     98:                        log_puts(":");
                     99:                        log_putu(o->pmax);
                    100:                        log_puts(" vol=");
                    101:                        log_putu(o->maxweight);
                    102:                }
                    103:                if (o->mode & MODE_MON) {
                    104:                        log_puts(" mon=");
                    105:                        log_putu(o->rmin);
                    106:                        log_puts(":");
                    107:                        log_putu(o->rmax);
                    108:                }
                    109:                if (o->mode & (MODE_RECMASK | MODE_PLAY)) {
                    110:                        if (o->mmc)
                    111:                                log_puts(" mmc");
                    112:                        if (o->dup)
                    113:                                log_puts(" dup");
                    114:                }
                    115:                log_puts("\n");
                    116:        }
                    117:        return o;
                    118: }
                    119:
                    120: struct opt *
1.4       ratchov   121: opt_byname(struct dev *d, char *name)
1.1       ratchov   122: {
                    123:        struct opt *o;
                    124:
1.5       ratchov   125:        for (o = opt_list; o != NULL; o = o->next) {
                    126:                if (d != NULL && o->dev != d)
                    127:                        continue;
1.1       ratchov   128:                if (strcmp(name, o->name) == 0)
                    129:                        return o;
                    130:        }
                    131:        return NULL;
                    132: }
                    133:
                    134: void
1.5       ratchov   135: opt_del(struct opt *o)
1.1       ratchov   136: {
                    137:        struct opt **po;
                    138:
1.5       ratchov   139:        for (po = &opt_list; *po != o; po = &(*po)->next) {
1.1       ratchov   140: #ifdef DEBUG
                    141:                if (*po == NULL) {
                    142:                        log_puts("opt_del: not on list\n");
                    143:                        panic();
                    144:                }
                    145: #endif
                    146:        }
                    147:        *po = o->next;
                    148:        xfree(o);
                    149: }