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

1.1     ! ratchov     1: /*     $OpenBSD: listen.c,v 1.8 2009/02/04 20:35:14 ratchov Exp $      */
        !             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"
        !            23:
        !            24: struct optlist opt_list = SLIST_HEAD_INITIALIZER(&opt_list);
        !            25:
        !            26: void
        !            27: opt_new(char *name, struct aparams *wpar, struct aparams *rpar, int maxweight)
        !            28: {
        !            29:        struct opt *o;
        !            30:        unsigned len;
        !            31:        char c;
        !            32:
        !            33:        for (len = 0; name[len] != '\0'; len++) {
        !            34:                if (len == OPT_NAMEMAX) {
        !            35:                        fprintf(stderr, "%s: name too long\n", name);
        !            36:                        exit(1);
        !            37:                }
        !            38:                c = name[len];
        !            39:                if (c < 'a' && c > 'z' &&
        !            40:                    c < 'A' && c > 'Z' &&
        !            41:                    c < '0' && c > '9' &&
        !            42:                    c != '_') {
        !            43:                        fprintf(stderr, "%s: '%c' not allowed\n", name, c);
        !            44:                        exit(1);
        !            45:                }
        !            46:        }
        !            47:        o = malloc(sizeof(struct opt));
        !            48:        if (o == NULL) {
        !            49:                perror("opt_new: malloc");
        !            50:                exit(1);
        !            51:        }
        !            52:        memcpy(o->name, name, len + 1);
        !            53:        o->wpar = *wpar;
        !            54:        o->rpar = *rpar;
        !            55:        o->maxweight = maxweight;
        !            56: #ifdef DEBUG
        !            57:        if (debug_level > 0) {
        !            58:                fprintf(stderr, "opt_new: %s: wpar=", o->name);
        !            59:                aparams_print(&o->wpar);
        !            60:                fprintf(stderr, ", rpar=");
        !            61:                aparams_print(&o->rpar);
        !            62:                fprintf(stderr, ", vol=%u\n", o->maxweight);
        !            63:        }
        !            64: #endif
        !            65:        SLIST_INSERT_HEAD(&opt_list, o, entry);
        !            66: }
        !            67:
        !            68: struct opt *
        !            69: opt_byname(char *name)
        !            70: {
        !            71:        struct opt *o;
        !            72:
        !            73:        SLIST_FOREACH(o, &opt_list, entry) {
        !            74:                if (strcmp(name, o->name) == 0) {
        !            75:                        DPRINTF("opt_byname: %s found\n", o->name);
        !            76:                        return o;
        !            77:                }
        !            78:        }
        !            79:        DPRINTF("opt_byname: %s not found\n", name);
        !            80:        return NULL;
        !            81: }
        !            82: