2 * Copyright (c) 1995 Andrew McRae. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/pccard/pccardd/file.c,v 1.24.2.5 2001/06/05 07:01:40 imp Exp $
27 * $DragonFly: src/usr.sbin/pccard/pccardd/Attic/file.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
33 #include <sys/types.h>
37 static int includes = 0;
42 } configfiles[MAXINCLUDES] = {{NULL, NULL, 0}, };
44 static int pushc, pusht;
46 static char *filename;
48 static char *keys[] = {
61 "debuglevel", /* 13 */
80 #define KWD_DEBUGLEVEL 13
81 #define KWD_INCLUDE 14
82 #define KWD_FUNCTION 15
85 /* for keyword compatibility with PAO/plain FreeBSD */
90 {"generic", KWD_FUNCTION},
101 static void parsefile(void);
102 static char *getline(void);
103 static char *next_tok(void);
104 static int num_tok(void);
105 static void error(char *);
106 static int keyword(char *);
107 static int irq_tok(int);
108 static int config_tok(unsigned char *);
109 static int func_tok(void);
110 static int debuglevel_tok(int);
111 static struct allocblk *ioblk_tok(int);
112 static struct allocblk *memblk_tok(int);
113 static struct driver *new_driver(char *);
114 static int iosize_tok(void);
115 static void file_include(char *);
117 static void addcmd(struct cmd **);
118 static void parse_card(int);
121 delete_card(struct card *cp)
123 struct ether *etherp, *ether_next;
124 struct card_config *configp, *config_next;
125 struct cmd *cmdp, *cmd_next;
134 /* free structures */
135 for (etherp = cp->ether; etherp; etherp = ether_next) {
136 ether_next = etherp->next;
139 for (configp = cp->config; configp; configp = config_next) {
140 config_next = configp->next;
143 for (cmdp = cp->insert; cmdp; cmdp = cmd_next) {
144 cmd_next = cmdp->next;
148 for (cmdp = cp->remove; cmdp; cmdp = cmd_next) {
149 cmd_next = cmdp->next;
157 * Read a file and parse the pcmcia configuration data.
158 * After parsing, verify the links.
164 struct card *cp, *card_next;
165 struct card *genericp, *tail_gp;
166 struct card_config *configp;
168 /* delete all card configuration data before we proceed */
171 cards = last_card = 0;
173 card_next = cp->next;
175 /* check whether this card is in use */
177 for (configp = cp->config; configp; configp = configp->next) {
178 if (configp->inuse) {
185 * don't delete entry in use for consistency.
186 * leave normal entry in the cards list,
187 * insert generic entry into the list after re-loading config files.
190 cp->next = 0; /* unchain from the cards list */
191 switch (cp->deftype) {
193 /* don't delete this entry for consistency */
194 if (debug_level >= 1) {
195 logmsg("Card \"%s\"(\"%s\") is in use, "
196 "can't change configuration\n",
197 cp->manuf, cp->version);
199 /* add this to the card list */
201 cards = last_card = cp;
203 last_card->next = cp;
209 /* generic entry must be inserted to the list later */
210 if (debug_level >= 1) {
211 logmsg("Generic entry is in use, "
212 "can't change configuration\n");
225 for (i = 0; i < MAXINCLUDES; i++) {
226 if (configfiles[i].filep) {
227 fclose(configfiles[i].filep);
228 configfiles[i].filep = NULL;
230 free(configfiles[i].filename);
234 in = fopen(name, "r");
240 configfiles[includes].filep = in;
241 filename = configfiles[includes].filename = name;
244 for (cp = cards; cp; cp = cp->next) {
246 logmsg("warning: card %s(%s) has no valid configuration\n",
247 cp->manuf, cp->version);
250 /* insert generic entries in use into the top of generic entries */
252 /* search tail of generic entries in use */
253 for (tail_gp = genericp; tail_gp->next; tail_gp = tail_gp->next)
257 * if the top of cards list is generic entry,
258 * insert generic entries in use before it.
260 if (cards && cards->deftype == DT_FUNC) {
261 tail_gp->next = cards;
266 /* search top of generic entries */
267 for (cp = cards; cp; cp = cp->next) {
268 if (cp->next && cp->next->deftype == DT_FUNC) {
274 * if we have generic entry in the cards list,
275 * insert generic entries in use into there.
278 tail_gp->next = cp->next;
284 * otherwise we don't have generic entries in
285 * cards list, just add them to the list.
290 last_card->next = genericp;
296 /* save the initial state of resource pool */
297 bcopy(io_avail, io_init, bitstr_size(IOPORTS));
298 bcopy(mem_avail, mem_init, bitstr_size(MEMBLKS));
299 bcopy(pool_irq, irq_init, sizeof(pool_irq));
307 struct allocblk *bp, *next;
313 switch (keyword(next_tok())) {
318 /* override reserved I/O blocks */
319 bit_nclear(io_avail, 0, IOPORTS-1);
320 for (bp = pool_ioblks; bp; bp = next) {
326 while ((bp = ioblk_tok(0)) != 0) {
327 if (bp->size == 0 || bp->addr == 0) {
331 bit_nset(io_avail, bp->addr,
332 bp->addr + bp->size - 1);
333 bp->next = pool_ioblks;
339 /* override reserved irqs */
340 bzero(pool_irq, sizeof(pool_irq));
341 while ((i = irq_tok(0)) > 0)
346 /* override reserved memory blocks. */
347 bit_nclear(mem_avail, 0, MEMBLKS-1);
348 for (bp = pool_mem; bp; bp = next) {
354 while ((bp = memblk_tok(0)) != 0) {
355 if (bp->size == 0 || bp->addr == 0) {
359 bit_nset(mem_avail, MEM2BIT(bp->addr),
360 MEM2BIT(bp->addr + bp->size) - 1);
367 /* Card definition. */
371 /* Function definition. */
375 i = debuglevel_tok(0);
380 incl = newstr(next_tok());
384 error("syntax error");
386 if (errors++ >= MAXERRORS) {
387 error("too many errors, giving up");
395 * Parse a card definition.
398 parse_card(int deftype)
400 char *man, *vers, *tmp;
402 unsigned char index_type;
405 struct card_config *confp, *lastp;
409 cp = xmalloc(sizeof(*cp));
410 cp->deftype = deftype;
413 man = newstr(next_tok());
414 vers = newstr(next_tok());
415 add_info = newstr(next_tok());
416 if (keyword(add_info)) {
419 cp->add_info1 = NULL;
420 cp->add_info2 = NULL;
422 cp->add_info1 = add_info;
423 add_info = newstr(next_tok());
424 if (keyword(add_info)) {
427 cp->add_info2 = NULL;
429 cp->add_info2 = add_info;
435 asprintf(&cp->logstr, "%s (%s)", man, vers);
442 cp->func_id = (u_char) func_tok();
445 fprintf(stderr, "parse_card: unknown deftype %d\n", deftype);
451 cards = last_card = cp;
453 last_card->next = cp;
457 switch (keyword(next_tok())) {
460 i = config_tok(&index_type);
462 error("illegal card config index");
465 confp = xmalloc(sizeof(*confp));
467 confp->driver = new_driver(man);
468 confp->irq = irq_tok(1);
469 confp->flags = num_tok();
470 if (confp->flags == -1) {
474 if (confp->irq < 0 || confp->irq > 15) {
475 error("illegal card IRQ value");
478 confp->index = i & 0x3F;
479 confp->index_type = index_type;
482 * If no valid driver for this config, then do not save
483 * this configuration entry.
489 for (lastp = cp->config; lastp->next;
490 lastp = lastp->next);
500 error("illegal card reset time");
507 ether = xmalloc(sizeof(*ether));
508 ether->type = ETHTYPE_GENERIC;
510 if (strcmp("attr2", tmp) == 0)
511 ether->type = ETHTYPE_ATTR2;
514 ether->value = num_tok();
515 if (ether->value == -1) {
516 error("illegal ether address offset");
521 ether->next = cp->ether;
534 iosize = iosize_tok();
536 error("Illegal cardio arguments");
540 error("iosize should be placed after config");
547 cp->logstr = newstr(next_tok());
557 * Generate a new driver structure. If one exists, use
558 * that one after confirming the correct class.
560 static struct driver *
561 new_driver(char *name)
566 for (drvp = drivers; drvp; drvp = drvp->next)
567 if (strcmp(drvp->name, name) == 0)
569 drvp = xmalloc(sizeof(*drvp));
570 drvp->next = drivers;
572 drvp->name = newstr(name);
573 drvp->kernel = newstr(name);
576 if (*p >= '0' && *p <= '9') {
577 drvp->unit = atoi(p);
582 printf("Drv %s%d created\n", drvp->kernel, drvp->unit);
589 * Parse one I/O block.
591 static struct allocblk *
597 /* ignore the keyword to allow separete blocks in multiple lines */
598 if (keyword(next_tok()) != KWD_IO) {
602 if ((i = num_tok()) >= 0) {
603 if (strcmp("-", next_tok()) || (j = num_tok()) < 0 || j < i) {
604 error("I/O block format error");
607 io = xmalloc(sizeof(*io));
609 io->size = j - i + 1;
611 error("I/O port out of range");
616 io->addr = io->size = 0;
621 error("illegal or missing I/O block spec");
626 * Parse a memory block.
628 static struct allocblk *
629 memblk_tok(int force)
631 struct allocblk *mem;
634 /* ignore the keyword to allow separete blocks in multiple lines */
635 if (keyword(next_tok()) != KWD_MEMORY) {
639 if ((i = num_tok()) >= 0) {
640 if ((j = num_tok()) < 0)
641 error("illegal memory block");
643 mem = xmalloc(sizeof(*mem));
644 mem->addr = i & ~(MEMUNIT - 1);
645 mem->size = (j + MEMUNIT - 1) & ~(MEMUNIT - 1);
646 if (i < MEMSTART || (i + j) > MEMEND) {
647 error("memory address out of range");
652 mem->addr = mem->size = 0;
658 error("illegal or missing memory block spec");
663 * IRQ token. Must be number > 0 && < 16.
664 * If force is set, IRQ must exist, and can also be '?'.
671 /* ignore the keyword to allow separete blocks in multiple lines */
672 if (keyword(next_tok()) != KWD_IRQ) {
676 if (strcmp("?", next_tok()) == 0 && force)
683 error("illegal IRQ value");
691 config_tok(unsigned char *index_type)
693 if (strcmp("default", next_tok()) == 0) {
694 *index_type = DEFAULT_INDEX;
698 if (strcmp("auto", next_tok()) == 0) {
699 *index_type = AUTO_INDEX;
703 *index_type = NORMAL_INDEX;
712 if (strcmp("serial", next_tok()) == 0)
715 if (strcmp("fixed_disk", next_tok()) == 0)
723 * debuglevel token. Must be between 0 and 9.
726 debuglevel_tok(int force)
731 if (i >= 0 && i <= 9)
738 * iosize {<size>|auto}
744 if (strcmp("auto", next_tok()) == 0)
745 iosize = -1; /* wildcard */
754 printf("iosize: size=%x\n", iosize);
761 * search the table for a match.
769 for (s = keys; *s; s++, i++)
770 if (strcmp(*s, str) == 0)
773 /* search keyword aliases too */
774 for (i = 0; key_aliases[i].key ; i++)
775 if (strcmp(key_aliases[i].alias, str) == 0)
776 return (key_aliases[i].key);
782 * addcmd - Append the command line to the list of
786 addcmd(struct cmd **cp)
792 ncp = xmalloc(sizeof(*ncp));
805 logmsg("%s: %s at line %d, near %s\n",
806 filename, msg, lineno, next_tok());
810 static int last_char;
826 return (last_char = c);
838 while (((c = get()) != '\n') && (c != EOF));
839 return (last_char = c);
843 * num_tok - expecting a number token. If not a number,
845 * Handles octal (who uses octal anymore?)
848 * Looks for a 'k' at the end of decimal numbers
849 * and multiplies by 1024.
854 char *s = next_tok(), c;
862 if (c == '\0') return 0;
863 else if (c == 'x' || c == 'X') {
872 if (val && base == 10 && *s == 0)
885 val = val * base + c - '0';
893 val = val * base + c - '0';
902 val = val * base + c - 'a' + 10;
913 val = val * base + c - 'A' + 10;
918 } while ((c = *s++) != 0);
922 static char *_next_tok(void);
927 char *s = _next_tok();
929 printf("Tok = %s\n", s);
935 * get one token. Handles string quoting etc.
940 static char buf[1024];
941 char *p = buf, instr = 0;
963 error("unterminated string");
968 /* Eat whitespace unless in a string. */
980 /* Special characters that are tokens on their own. */
995 /* go back to previous config file */
997 in = configfiles[includes].filep;
998 filename = configfiles[includes].filename;
999 lineno = configfiles[includes].lineno;
1000 return _next_tok(); /* recursive */
1006 strcpy(buf, "__EOF__");
1013 * get the rest of the line. If the
1014 * last character scanned was a newline, then
1015 * return an empty line. If this isn't checked, then
1016 * a getline may incorrectly return the next line.
1021 char buf[1024], *p = buf;
1024 if (last_char == '\n')
1025 return (newstr(""));
1028 } while (c == ' ' || c == '\t');
1029 for (; c != '\n' && c != EOF; c = get())
1030 if (i++ < sizeof(buf) - 10)
1033 return (newstr(buf));
1037 * Include configuration file
1040 file_include(char *incl)
1045 /* check nesting overflow */
1046 if (includes >= MAXINCLUDES) {
1047 if (debug_level >= 1) {
1048 logmsg("%s: include nesting overflow "
1049 "at line %d, near %s\n", filename, lineno, incl);
1055 /* check recursive inclusion */
1056 for (i = 0, included = 0; i <= includes; i++) {
1057 if (strcmp(incl, configfiles[i].filename) == 0) {
1062 if (included == 1) {
1063 if (debug_level >= 1) {
1064 logmsg("%s: can't include the same file twice "
1065 "at line %d, near %s\n", filename, lineno, incl);
1071 if (!(fp = fopen(incl, "r"))) {
1072 if (debug_level >= 1) {
1073 logmsg("%s: can't open include file "
1074 "at line %d, near %s\n", filename, lineno, incl);
1080 /* save line number of the current config file */
1081 configfiles[includes].lineno = lineno;
1084 /* now we start parsing new config file */
1086 in = configfiles[includes].filep = fp;
1087 filename = configfiles[includes].filename = incl;