Annotation of src/usr.bin/aucat/opt.c, Revision 1.9
1.9 ! ratchov 1: /* $OpenBSD: opt.c,v 1.8 2010/04/21 06:13:07 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.9 ! ratchov 30: opt_new(char *name, struct dev *d, 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: }
1.9 ! ratchov 51: SLIST_FOREACH(o, &opt_list, entry) {
! 52: if (strcmp(name, o->name) == 0) {
! 53: fprintf(stderr, "%s: already defined\n", name);
! 54: exit(1);
! 55: }
! 56: }
1.1 ratchov 57: o = malloc(sizeof(struct opt));
58: if (o == NULL) {
59: perror("opt_new: malloc");
60: exit(1);
61: }
62: memcpy(o->name, name, len + 1);
1.7 ratchov 63: if (mode & MODE_RECMASK)
64: o->wpar = (mode & MODE_MON) ? *rpar : *wpar;
65: if (mode & MODE_PLAY)
66: o->rpar = *rpar;
1.1 ratchov 67: o->maxweight = maxweight;
1.3 ratchov 68: o->mmc = mmc;
1.8 ratchov 69: o->join = join;
1.7 ratchov 70: o->mode = mode;
1.9 ! ratchov 71: o->dev = d;
1.4 ratchov 72: #ifdef DEBUG
73: if (debug_level >= 2) {
74: dbg_puts(o->name);
1.7 ratchov 75: dbg_puts(":");
76: if (mode & MODE_REC) {
77: dbg_puts(" rec=");
78: dbg_putu(o->wpar.cmin);
79: dbg_puts(":");
80: dbg_putu(o->wpar.cmax);
81: }
82: if (mode & MODE_PLAY) {
83: dbg_puts(" play=");
84: dbg_putu(o->rpar.cmin);
85: dbg_puts(":");
86: dbg_putu(o->rpar.cmax);
87: dbg_puts(" vol=");
88: dbg_putu(o->maxweight);
89: }
90: if (mode & MODE_MON) {
91: dbg_puts(" mon=");
92: dbg_putu(o->wpar.cmin);
93: dbg_puts(":");
94: dbg_putu(o->wpar.cmax);
95: }
1.4 ratchov 96: if (o->mmc)
1.7 ratchov 97: dbg_puts(" mmc");
1.4 ratchov 98: dbg_puts("\n");
99: }
100: #endif
1.1 ratchov 101: SLIST_INSERT_HEAD(&opt_list, o, entry);
102: }
103:
104: struct opt *
105: opt_byname(char *name)
106: {
107: struct opt *o;
108:
109: SLIST_FOREACH(o, &opt_list, entry) {
110: if (strcmp(name, o->name) == 0) {
1.4 ratchov 111: #ifdef DEBUG
112: if (debug_level >= 3) {
113: dbg_puts(o->name);
114: dbg_puts(": option found\n");
115: }
116: #endif
1.1 ratchov 117: return o;
118: }
119: }
1.4 ratchov 120: #ifdef DEBUG
121: if (debug_level >= 3) {
122: dbg_puts(name);
123: dbg_puts(": option not found\n");
124: }
125: #endif
1.1 ratchov 126: return NULL;
127: }
128: