[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.8

1.8     ! ratchov     1: /*     $OpenBSD: opt.c,v 1.7 2010/04/06 20:07:01 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,
1.8     ! ratchov    31:     int maxweight, int mmc, int join, 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.8     ! ratchov    63:        o->join = join;
1.7       ratchov    64:        o->mode = mode;
1.4       ratchov    65: #ifdef DEBUG
                     66:        if (debug_level >= 2) {
                     67:                dbg_puts(o->name);
1.7       ratchov    68:                dbg_puts(":");
                     69:                if (mode & MODE_REC) {
                     70:                        dbg_puts(" rec=");
                     71:                        dbg_putu(o->wpar.cmin);
                     72:                        dbg_puts(":");
                     73:                        dbg_putu(o->wpar.cmax);
                     74:                }
                     75:                if (mode & MODE_PLAY) {
                     76:                        dbg_puts(" play=");
                     77:                        dbg_putu(o->rpar.cmin);
                     78:                        dbg_puts(":");
                     79:                        dbg_putu(o->rpar.cmax);
                     80:                        dbg_puts(" vol=");
                     81:                        dbg_putu(o->maxweight);
                     82:                }
                     83:                if (mode & MODE_MON) {
                     84:                        dbg_puts(" mon=");
                     85:                        dbg_putu(o->wpar.cmin);
                     86:                        dbg_puts(":");
                     87:                        dbg_putu(o->wpar.cmax);
                     88:                }
1.4       ratchov    89:                if (o->mmc)
1.7       ratchov    90:                        dbg_puts(" mmc");
1.4       ratchov    91:                dbg_puts("\n");
                     92:        }
                     93: #endif
1.1       ratchov    94:        SLIST_INSERT_HEAD(&opt_list, o, entry);
                     95: }
                     96:
                     97: struct opt *
                     98: opt_byname(char *name)
                     99: {
                    100:        struct opt *o;
                    101:
                    102:        SLIST_FOREACH(o, &opt_list, entry) {
                    103:                if (strcmp(name, o->name) == 0) {
1.4       ratchov   104: #ifdef DEBUG
                    105:                        if (debug_level >= 3) {
                    106:                                dbg_puts(o->name);
                    107:                                dbg_puts(": option found\n");
                    108:                        }
                    109: #endif
1.1       ratchov   110:                        return o;
                    111:                }
                    112:        }
1.4       ratchov   113: #ifdef DEBUG
                    114:        if (debug_level >= 3) {
                    115:                dbg_puts(name);
                    116:                dbg_puts(": option not found\n");
                    117:        }
                    118: #endif
1.1       ratchov   119:        return NULL;
                    120: }
                    121: