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

Annotation of src/usr.bin/aucat/opt.c, Revision 1.7

1.7     ! ratchov     1: /*     $OpenBSD: opt.c,v 1.6 2010/04/03 17:59:17 ratchov Exp $ */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 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 <stdio.h>
                     18: #include <stdlib.h>
                     19: #include <string.h>
                     20:
                     21: #include "conf.h"
                     22: #include "opt.h"
1.4       ratchov    23: #ifdef DEBUG
                     24: #include "dbg.h"
                     25: #endif
1.1       ratchov    26:
                     27: struct optlist opt_list = SLIST_HEAD_INITIALIZER(&opt_list);
                     28:
                     29: void
1.7     ! ratchov    30: opt_new(char *name, struct aparams *wpar, struct aparams *rpar,
        !            31:     int maxweight, int mmc, unsigned mode)
1.1       ratchov    32: {
                     33:        struct opt *o;
                     34:        unsigned len;
                     35:        char c;
                     36:
                     37:        for (len = 0; name[len] != '\0'; len++) {
                     38:                if (len == OPT_NAMEMAX) {
                     39:                        fprintf(stderr, "%s: name too long\n", name);
                     40:                        exit(1);
                     41:                }
                     42:                c = name[len];
                     43:                if (c < 'a' && c > 'z' &&
                     44:                    c < 'A' && c > 'Z' &&
                     45:                    c < '0' && c > '9' &&
                     46:                    c != '_') {
                     47:                        fprintf(stderr, "%s: '%c' not allowed\n", name, c);
                     48:                        exit(1);
                     49:                }
                     50:        }
                     51:        o = malloc(sizeof(struct opt));
                     52:        if (o == NULL) {
                     53:                perror("opt_new: malloc");
                     54:                exit(1);
                     55:        }
                     56:        memcpy(o->name, name, len + 1);
1.7     ! ratchov    57:        if (mode & MODE_RECMASK)
        !            58:                o->wpar = (mode & MODE_MON) ? *rpar : *wpar;
        !            59:        if (mode & MODE_PLAY)
        !            60:                o->rpar = *rpar;
1.1       ratchov    61:        o->maxweight = maxweight;
1.3       ratchov    62:        o->mmc = mmc;
1.7     ! ratchov    63:        o->mode = mode;
1.4       ratchov    64: #ifdef DEBUG
                     65:        if (debug_level >= 2) {
                     66:                dbg_puts(o->name);
1.7     ! ratchov    67:                dbg_puts(":");
        !            68:                if (mode & MODE_REC) {
        !            69:                        dbg_puts(" rec=");
        !            70:                        dbg_putu(o->wpar.cmin);
        !            71:                        dbg_puts(":");
        !            72:                        dbg_putu(o->wpar.cmax);
        !            73:                }
        !            74:                if (mode & MODE_PLAY) {
        !            75:                        dbg_puts(" play=");
        !            76:                        dbg_putu(o->rpar.cmin);
        !            77:                        dbg_puts(":");
        !            78:                        dbg_putu(o->rpar.cmax);
        !            79:                        dbg_puts(" vol=");
        !            80:                        dbg_putu(o->maxweight);
        !            81:                }
        !            82:                if (mode & MODE_MON) {
        !            83:                        dbg_puts(" mon=");
        !            84:                        dbg_putu(o->wpar.cmin);
        !            85:                        dbg_puts(":");
        !            86:                        dbg_putu(o->wpar.cmax);
        !            87:                }
1.4       ratchov    88:                if (o->mmc)
1.7     ! ratchov    89:                        dbg_puts(" mmc");
1.4       ratchov    90:                dbg_puts("\n");
                     91:        }
                     92: #endif
1.1       ratchov    93:        SLIST_INSERT_HEAD(&opt_list, o, entry);
                     94: }
                     95:
                     96: struct opt *
                     97: opt_byname(char *name)
                     98: {
                     99:        struct opt *o;
                    100:
                    101:        SLIST_FOREACH(o, &opt_list, entry) {
                    102:                if (strcmp(name, o->name) == 0) {
1.4       ratchov   103: #ifdef DEBUG
                    104:                        if (debug_level >= 3) {
                    105:                                dbg_puts(o->name);
                    106:                                dbg_puts(": option found\n");
                    107:                        }
                    108: #endif
1.1       ratchov   109:                        return o;
                    110:                }
                    111:        }
1.4       ratchov   112: #ifdef DEBUG
                    113:        if (debug_level >= 3) {
                    114:                dbg_puts(name);
                    115:                dbg_puts(": option not found\n");
                    116:        }
                    117: #endif
1.1       ratchov   118:        return NULL;
                    119: }
                    120: