Home | History | Annotate | Line # | Download | only in tic
tic.c revision 1.14
      1  1.14     joerg /* $NetBSD: tic.c,v 1.14 2012/05/31 19:56:32 joerg Exp $ */
      2   1.1       roy 
      3   1.1       roy /*
      4   1.5       roy  * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc.
      5   1.1       roy  *
      6   1.1       roy  * This code is derived from software contributed to The NetBSD Foundation
      7   1.1       roy  * by Roy Marples.
      8   1.1       roy  *
      9   1.1       roy  * Redistribution and use in source and binary forms, with or without
     10   1.1       roy  * modification, are permitted provided that the following conditions
     11   1.1       roy  * are met:
     12   1.1       roy  * 1. Redistributions of source code must retain the above copyright
     13   1.1       roy  *    notice, this list of conditions and the following disclaimer.
     14   1.1       roy  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1       roy  *    notice, this list of conditions and the following disclaimer in the
     16   1.1       roy  *    documentation and/or other materials provided with the distribution.
     17   1.1       roy  *
     18   1.1       roy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1       roy  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1       roy  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1       roy  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1       roy  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1       roy  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1       roy  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1       roy  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1       roy  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1       roy  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1       roy  */
     29   1.1       roy 
     30   1.1       roy #if HAVE_NBTOOL_CONFIG_H
     31   1.1       roy #include "nbtool_config.h"
     32   1.1       roy #endif
     33   1.1       roy 
     34   1.1       roy #include <sys/cdefs.h>
     35  1.14     joerg __RCSID("$NetBSD: tic.c,v 1.14 2012/05/31 19:56:32 joerg Exp $");
     36   1.1       roy 
     37   1.1       roy #include <sys/types.h>
     38  1.14     joerg #include <sys/queue.h>
     39   1.8  pgoyette 
     40   1.9  pgoyette #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
     41   1.7  pgoyette #include <sys/endian.h>
     42   1.8  pgoyette #endif
     43   1.1       roy 
     44   1.1       roy #include <ctype.h>
     45   1.1       roy #include <err.h>
     46   1.1       roy #include <errno.h>
     47   1.1       roy #include <getopt.h>
     48   1.1       roy #include <limits.h>
     49   1.1       roy #include <fcntl.h>
     50   1.1       roy #include <ndbm.h>
     51   1.1       roy #include <stdarg.h>
     52   1.1       roy #include <stdlib.h>
     53   1.1       roy #include <stdio.h>
     54   1.1       roy #include <string.h>
     55   1.1       roy #include <term_private.h>
     56   1.1       roy #include <term.h>
     57   1.1       roy 
     58   1.1       roy /* We store the full list of terminals we have instead of iterating
     59   1.1       roy    through the database as the sequential iterator doesn't work
     60   1.1       roy    the the data size stored changes N amount which ours will. */
     61   1.1       roy typedef struct term {
     62  1.14     joerg 	SLIST_ENTRY(term) next;
     63   1.1       roy 	char *name;
     64   1.1       roy 	char type;
     65  1.10       roy 	TIC *tic;
     66   1.1       roy } TERM;
     67  1.14     joerg static SLIST_HEAD(, term) terms = SLIST_HEAD_INITIALIZER(terms);
     68   1.1       roy 
     69   1.1       roy static int error_exit;
     70  1.10       roy static int Sflag;
     71   1.1       roy static char *dbname;
     72   1.1       roy 
     73   1.1       roy static void
     74   1.1       roy do_unlink(void)
     75   1.1       roy {
     76   1.1       roy 
     77   1.1       roy 	if (dbname != NULL)
     78   1.1       roy 		unlink(dbname);
     79   1.1       roy }
     80   1.1       roy 
     81  1.13     joerg static void __printflike(1, 2)
     82   1.1       roy dowarn(const char *fmt, ...)
     83   1.1       roy {
     84   1.1       roy 	va_list va;
     85   1.1       roy 
     86   1.1       roy 	error_exit = 1;
     87   1.1       roy 	va_start(va, fmt);
     88   1.1       roy 	vwarnx(fmt, va);
     89   1.1       roy 	va_end(va);
     90   1.1       roy }
     91   1.1       roy 
     92   1.1       roy static char *
     93   1.1       roy grow_tbuf(TBUF *tbuf, size_t len)
     94   1.1       roy {
     95   1.1       roy 	char *buf;
     96   1.1       roy 
     97  1.10       roy 	buf = _ti_grow_tbuf(tbuf, len);
     98  1.10       roy 	if (buf == NULL)
     99  1.10       roy 		err(1, "_ti_grow_tbuf");
    100  1.10       roy 	return buf;
    101   1.5       roy }
    102   1.5       roy 
    103   1.5       roy static int
    104   1.5       roy save_term(DBM *db, TERM *term)
    105   1.5       roy {
    106  1.10       roy 	uint8_t *buf;
    107  1.10       roy 	ssize_t len;
    108   1.5       roy 	datum key, value;
    109   1.5       roy 
    110  1.10       roy 	len = _ti_flatten(&buf, term->tic);
    111  1.10       roy 	if (len == -1)
    112   1.5       roy 		return -1;
    113   1.1       roy 
    114   1.1       roy 	key.dptr = term->name;
    115   1.1       roy 	key.dsize = strlen(term->name);
    116  1.10       roy 	value.dptr = buf;
    117  1.10       roy 	value.dsize = len;
    118   1.1       roy 	if (dbm_store(db, key, value, DBM_REPLACE) == -1)
    119   1.1       roy 		err(1, "dbm_store");
    120  1.10       roy 	free(buf);
    121   1.1       roy 	return 0;
    122   1.1       roy }
    123   1.1       roy 
    124   1.1       roy static TERM *
    125   1.1       roy find_term(const char *name)
    126   1.1       roy {
    127   1.1       roy 	TERM *term;
    128  1.14     joerg 
    129  1.14     joerg 	SLIST_FOREACH(term, &terms, next) {
    130   1.1       roy 		if (strcmp(term->name, name) == 0)
    131   1.1       roy 			return term;
    132  1.14     joerg 	}
    133   1.1       roy 	return NULL;
    134   1.1       roy }
    135   1.1       roy 
    136   1.1       roy static TERM *
    137   1.1       roy store_term(const char *name, char type)
    138   1.1       roy {
    139   1.1       roy 	TERM *term;
    140   1.1       roy 
    141   1.1       roy 	term = calloc(1, sizeof(*term));
    142   1.1       roy 	if (term == NULL)
    143   1.1       roy 		errx(1, "malloc");
    144   1.1       roy 	term->name = strdup(name);
    145   1.1       roy 	term->type = type;
    146   1.1       roy 	if (term->name == NULL)
    147   1.1       roy 		errx(1, "malloc");
    148  1.14     joerg 	SLIST_INSERT_HEAD(&terms, term, next);
    149   1.1       roy 	return term;
    150   1.1       roy }
    151   1.1       roy 
    152   1.1       roy static int
    153  1.10       roy process_entry(TBUF *buf, int flags)
    154   1.1       roy {
    155  1.10       roy 	char *p, *e, *alias;
    156   1.1       roy 	TERM *term;
    157   1.1       roy 	TIC *tic;
    158   1.1       roy 
    159   1.1       roy 	if (buf->bufpos == 0)
    160   1.1       roy 		return 0;
    161   1.1       roy 	/* Terminate the string */
    162   1.1       roy 	buf->buf[buf->bufpos - 1] = '\0';
    163   1.1       roy 	/* First rewind the buffer for new entries */
    164   1.1       roy 	buf->bufpos = 0;
    165   1.1       roy 
    166   1.1       roy 	if (isspace((unsigned char)*buf->buf))
    167   1.1       roy 		return 0;
    168   1.1       roy 
    169  1.10       roy 	tic = _ti_compile(buf->buf, flags);
    170  1.10       roy 	if (tic == NULL)
    171   1.1       roy 		return 0;
    172  1.10       roy 
    173  1.10       roy 	if (find_term(tic->name) != NULL) {
    174  1.10       roy 		dowarn("%s: duplicate entry", tic->name);
    175  1.10       roy 		_ti_freetic(tic);
    176   1.1       roy 		return 0;
    177   1.1       roy 	}
    178  1.10       roy 	term = store_term(tic->name, 't');
    179  1.10       roy 	term->tic = tic;
    180   1.1       roy 
    181   1.1       roy 	/* Create aliased terms */
    182  1.10       roy 	if (tic->alias != NULL) {
    183  1.10       roy 		alias = p = strdup(tic->alias);
    184  1.10       roy 		while (p != NULL && *p != '\0') {
    185  1.10       roy 			e = strchr(p, '|');
    186  1.10       roy 			if (e != NULL)
    187  1.10       roy 				*e++ = '\0';
    188  1.10       roy 			if (find_term(p) != NULL) {
    189   1.1       roy 				dowarn("%s: has alias for already assigned"
    190  1.10       roy 				    " term %s", tic->name, p);
    191   1.1       roy 			} else {
    192  1.10       roy 				term = store_term(p, 'a');
    193  1.10       roy 				term->tic = calloc(sizeof(*term->tic), 1);
    194  1.10       roy 				if (term->tic == NULL)
    195  1.10       roy 					err(1, "malloc");
    196  1.10       roy 				term->tic->name = strdup(tic->name);
    197  1.10       roy 				if (term->tic->name == NULL)
    198   1.1       roy 					err(1, "malloc");
    199   1.1       roy 			}
    200  1.10       roy 			p = e;
    201   1.1       roy 		}
    202   1.1       roy 	}
    203   1.1       roy 
    204   1.1       roy 	return 0;
    205   1.1       roy }
    206   1.1       roy 
    207   1.1       roy static void
    208  1.10       roy merge(TIC *rtic, TIC *utic, int flags)
    209   1.1       roy {
    210   1.1       roy 	char *cap, flag, *code, type, *str;
    211   1.1       roy 	short ind, num;
    212   1.1       roy 	size_t n;
    213   1.1       roy 
    214   1.1       roy 	cap = utic->flags.buf;
    215   1.1       roy 	for (n = utic->flags.entries; n > 0; n--) {
    216   1.1       roy 		ind = le16dec(cap);
    217   1.1       roy 		cap += sizeof(uint16_t);
    218   1.1       roy 		flag = *cap++;
    219   1.1       roy 		if (VALID_BOOLEAN(flag) &&
    220  1.10       roy 		    _ti_find_cap(&rtic->flags, 'f', ind) == NULL)
    221   1.1       roy 		{
    222  1.10       roy 			_ti_grow_tbuf(&rtic->flags, sizeof(uint16_t) + 1);
    223   1.1       roy 			le16enc(rtic->flags.buf + rtic->flags.bufpos, ind);
    224   1.1       roy 			rtic->flags.bufpos += sizeof(uint16_t);
    225   1.1       roy 			rtic->flags.buf[rtic->flags.bufpos++] = flag;
    226   1.1       roy 			rtic->flags.entries++;
    227   1.1       roy 		}
    228   1.1       roy 	}
    229   1.1       roy 
    230   1.1       roy 	cap = utic->nums.buf;
    231   1.1       roy 	for (n = utic->nums.entries; n > 0; n--) {
    232   1.1       roy 		ind = le16dec(cap);
    233   1.1       roy 		cap += sizeof(uint16_t);
    234   1.1       roy 		num = le16dec(cap);
    235   1.1       roy 		cap += sizeof(uint16_t);
    236   1.1       roy 		if (VALID_NUMERIC(num) &&
    237  1.10       roy 		    _ti_find_cap(&rtic->nums, 'n', ind) == NULL)
    238   1.1       roy 		{
    239   1.1       roy 			grow_tbuf(&rtic->nums, sizeof(uint16_t) * 2);
    240   1.1       roy 			le16enc(rtic->nums.buf + rtic->nums.bufpos, ind);
    241   1.1       roy 			rtic->nums.bufpos += sizeof(uint16_t);
    242   1.1       roy 			le16enc(rtic->nums.buf + rtic->nums.bufpos, num);
    243   1.1       roy 			rtic->nums.bufpos += sizeof(uint16_t);
    244   1.1       roy 			rtic->nums.entries++;
    245   1.1       roy 		}
    246   1.1       roy 	}
    247   1.1       roy 
    248   1.1       roy 	cap = utic->strs.buf;
    249   1.1       roy 	for (n = utic->strs.entries; n > 0; n--) {
    250   1.1       roy 		ind = le16dec(cap);
    251   1.1       roy 		cap += sizeof(uint16_t);
    252   1.1       roy 		num = le16dec(cap);
    253   1.1       roy 		cap += sizeof(uint16_t);
    254   1.1       roy 		if (num > 0 &&
    255  1.10       roy 		    _ti_find_cap(&rtic->strs, 's', ind) == NULL)
    256   1.1       roy 		{
    257   1.1       roy 			grow_tbuf(&rtic->strs, (sizeof(uint16_t) * 2) + num);
    258   1.1       roy 			le16enc(rtic->strs.buf + rtic->strs.bufpos, ind);
    259   1.1       roy 			rtic->strs.bufpos += sizeof(uint16_t);
    260   1.1       roy 			le16enc(rtic->strs.buf + rtic->strs.bufpos, num);
    261   1.1       roy 			rtic->strs.bufpos += sizeof(uint16_t);
    262   1.1       roy 			memcpy(rtic->strs.buf + rtic->strs.bufpos,
    263   1.1       roy 			    cap, num);
    264   1.1       roy 			rtic->strs.bufpos += num;
    265   1.1       roy 			rtic->strs.entries++;
    266   1.1       roy 		}
    267   1.1       roy 		cap += num;
    268   1.1       roy 	}
    269   1.1       roy 
    270   1.1       roy 	cap = utic->extras.buf;
    271   1.1       roy 	for (n = utic->extras.entries; n > 0; n--) {
    272   1.1       roy 		num = le16dec(cap);
    273   1.1       roy 		cap += sizeof(uint16_t);
    274   1.1       roy 		code = cap;
    275   1.1       roy 		cap += num;
    276   1.1       roy 		type = *cap++;
    277   1.1       roy 		flag = 0;
    278   1.1       roy 		str = NULL;
    279   1.1       roy 		switch (type) {
    280   1.1       roy 		case 'f':
    281   1.1       roy 			flag = *cap++;
    282   1.1       roy 			if (!VALID_BOOLEAN(flag))
    283   1.1       roy 				continue;
    284   1.1       roy 			break;
    285   1.1       roy 		case 'n':
    286   1.1       roy 			num = le16dec(cap);
    287   1.1       roy 			cap += sizeof(uint16_t);
    288   1.1       roy 			if (!VALID_NUMERIC(num))
    289   1.1       roy 				continue;
    290   1.1       roy 			break;
    291   1.1       roy 		case 's':
    292   1.1       roy 			num = le16dec(cap);
    293   1.1       roy 			cap += sizeof(uint16_t);
    294   1.1       roy 			str = cap;
    295   1.1       roy 			cap += num;
    296   1.1       roy 			if (num == 0)
    297   1.1       roy 				continue;
    298   1.1       roy 			break;
    299   1.1       roy 		}
    300  1.10       roy 		_ti_store_extra(rtic, 0, code, type, flag, num, str, num,
    301  1.10       roy 		    flags);
    302   1.1       roy 	}
    303   1.1       roy }
    304   1.1       roy 
    305   1.1       roy static size_t
    306  1.10       roy merge_use(int flags)
    307   1.1       roy {
    308   1.1       roy 	size_t skipped, merged, memn;
    309   1.1       roy 	char *cap, *scap;
    310   1.1       roy 	uint16_t num;
    311   1.1       roy 	TIC *rtic, *utic;
    312   1.1       roy 	TERM *term, *uterm;;
    313   1.1       roy 
    314   1.1       roy 	skipped = merged = 0;
    315  1.14     joerg 	SLIST_FOREACH(term, &terms, next) {
    316   1.1       roy 		if (term->type == 'a')
    317   1.1       roy 			continue;
    318  1.10       roy 		rtic = term->tic;
    319  1.10       roy 		while ((cap = _ti_find_extra(&rtic->extras, "use")) != NULL) {
    320   1.1       roy 			if (*cap++ != 's') {
    321   1.1       roy 				dowarn("%s: use is not string", rtic->name);
    322   1.1       roy 				break;
    323   1.1       roy 			}
    324   1.1       roy 			cap += sizeof(uint16_t);
    325   1.1       roy 			if (strcmp(rtic->name, cap) == 0) {
    326   1.1       roy 				dowarn("%s: uses itself", rtic->name);
    327   1.1       roy 				goto remove;
    328   1.1       roy 			}
    329   1.1       roy 			uterm = find_term(cap);
    330   1.1       roy 			if (uterm != NULL && uterm->type == 'a')
    331  1.10       roy 				uterm = find_term(uterm->tic->name);
    332   1.1       roy 			if (uterm == NULL) {
    333   1.1       roy 				dowarn("%s: no use record for %s",
    334   1.1       roy 				    rtic->name, cap);
    335   1.1       roy 				goto remove;
    336   1.1       roy 			}
    337  1.10       roy 			utic = uterm->tic;
    338   1.1       roy 			if (strcmp(utic->name, rtic->name) == 0) {
    339   1.1       roy 				dowarn("%s: uses itself", rtic->name);
    340   1.1       roy 				goto remove;
    341   1.1       roy 			}
    342  1.10       roy 			if (_ti_find_extra(&utic->extras, "use") != NULL) {
    343   1.1       roy 				skipped++;
    344   1.1       roy 				break;
    345   1.1       roy 			}
    346  1.10       roy 			cap = _ti_find_extra(&rtic->extras, "use");
    347  1.10       roy 			merge(rtic, utic, flags);
    348   1.1       roy 	remove:
    349   1.1       roy 			/* The pointers may have changed, find the use again */
    350  1.10       roy 			cap = _ti_find_extra(&rtic->extras, "use");
    351   1.1       roy 			if (cap == NULL)
    352   1.1       roy 				dowarn("%s: use no longer exists - impossible",
    353   1.1       roy 					rtic->name);
    354   1.1       roy 			else {
    355   1.1       roy 				scap = cap - (4 + sizeof(uint16_t));
    356   1.1       roy 				cap++;
    357   1.1       roy 				num = le16dec(cap);
    358   1.1       roy 				cap += sizeof(uint16_t) + num;
    359   1.1       roy 				memn = rtic->extras.bufpos -
    360   1.1       roy 				    (cap - rtic->extras.buf);
    361  1.11       roy 				memmove(scap, cap, memn);
    362   1.1       roy 				rtic->extras.bufpos -= cap - scap;
    363   1.1       roy 				cap = scap;
    364   1.1       roy 				rtic->extras.entries--;
    365   1.1       roy 				merged++;
    366   1.1       roy 			}
    367   1.1       roy 		}
    368   1.1       roy 	}
    369   1.1       roy 
    370   1.1       roy 	if (merged == 0 && skipped != 0)
    371   1.1       roy 		dowarn("circular use detected");
    372   1.1       roy 	return merged;
    373   1.1       roy }
    374   1.1       roy 
    375   1.5       roy static int
    376   1.5       roy print_dump(int argc, char **argv)
    377   1.5       roy {
    378   1.5       roy 	TERM *term;
    379  1.10       roy 	uint8_t *buf;
    380   1.5       roy 	int i, n;
    381   1.5       roy 	size_t j, col;
    382  1.10       roy 	ssize_t len;
    383   1.5       roy 
    384   1.6       roy 	printf("struct compiled_term {\n");
    385   1.6       roy 	printf("\tconst char *name;\n");
    386   1.6       roy 	printf("\tconst char *cap;\n");
    387   1.6       roy 	printf("\tsize_t caplen;\n");
    388   1.6       roy 	printf("};\n\n");
    389   1.6       roy 
    390   1.6       roy 	printf("const struct compiled_term compiled_terms[] = {\n");
    391   1.6       roy 
    392   1.5       roy 	n = 0;
    393   1.5       roy 	for (i = 0; i < argc; i++) {
    394   1.5       roy 		term = find_term(argv[i]);
    395   1.5       roy 		if (term == NULL) {
    396   1.5       roy 			warnx("%s: no description for terminal", argv[i]);
    397   1.5       roy 			continue;
    398   1.5       roy 		}
    399   1.5       roy 		if (term->type == 'a') {
    400   1.5       roy 			warnx("%s: cannot dump alias", argv[i]);
    401   1.5       roy 			continue;
    402   1.5       roy 		}
    403  1.10       roy 		/* Don't compile the aliases in, save space */
    404  1.10       roy 		free(term->tic->alias);
    405  1.10       roy 		term->tic->alias = NULL;
    406  1.10       roy 		len = _ti_flatten(&buf, term->tic);
    407  1.10       roy 		if (len == 0 || len == -1)
    408   1.5       roy 			continue;
    409   1.5       roy 
    410   1.6       roy 		printf("\t{\n");
    411   1.6       roy 		printf("\t\t\"%s\",\n", argv[i]);
    412   1.5       roy 		n++;
    413  1.10       roy 		for (j = 0, col = 0; j < (size_t)len; j++) {
    414   1.5       roy 			if (col == 0) {
    415   1.6       roy 				printf("\t\t\"");
    416   1.6       roy 				col = 16;
    417   1.5       roy 			}
    418   1.5       roy 
    419  1.10       roy 			col += printf("\\%03o", (uint8_t)buf[j]);
    420   1.5       roy 			if (col > 75) {
    421   1.5       roy 				printf("\"%s\n",
    422  1.10       roy 				    j + 1 == (size_t)len ? "," : "");
    423   1.5       roy 				col = 0;
    424   1.5       roy 			}
    425   1.5       roy 		}
    426   1.5       roy 		if (col != 0)
    427   1.6       roy 			printf("\",\n");
    428  1.10       roy 		printf("\t\t%zu\n", len);
    429   1.6       roy 		printf("\t}");
    430   1.6       roy 		if (i + 1 < argc)
    431   1.6       roy 			printf(",");
    432   1.6       roy 		printf("\n");
    433  1.10       roy 		free(buf);
    434   1.5       roy 	}
    435   1.6       roy 	printf("};\n");
    436   1.5       roy 
    437   1.5       roy 	return n;
    438   1.5       roy }
    439   1.5       roy 
    440   1.1       roy int
    441   1.1       roy main(int argc, char **argv)
    442   1.1       roy {
    443  1.10       roy 	int ch, cflag, sflag, flags;
    444   1.1       roy 	char *source, *p, *buf, *ofile;
    445   1.1       roy 	FILE *f;
    446   1.1       roy 	DBM *db;
    447  1.12       roy 	size_t buflen, nterm, nalias;
    448  1.12       roy 	ssize_t len;
    449   1.1       roy 	TBUF tbuf;
    450   1.1       roy 	TERM *term;
    451   1.1       roy 
    452   1.1       roy 	cflag = sflag = 0;
    453   1.1       roy 	ofile = NULL;
    454  1.10       roy 	flags = TIC_ALIAS | TIC_DESCRIPTION | TIC_WARNING;
    455   1.5       roy 	while ((ch = getopt(argc, argv, "Saco:sx")) != -1)
    456   1.1       roy 	    switch (ch) {
    457   1.5       roy 	    case 'S':
    458   1.5       roy 		    Sflag = 1;
    459  1.10       roy 		    /* We still compile aliases so that use= works.
    460  1.10       roy 		     * However, it's removed before we flatten to save space. */
    461  1.10       roy 		    flags &= ~TIC_DESCRIPTION;
    462   1.5       roy 		    break;
    463   1.1       roy 	    case 'a':
    464  1.10       roy 		    flags |= TIC_COMMENT;
    465   1.1       roy 		    break;
    466   1.1       roy 	    case 'c':
    467   1.4       roy 		    cflag = 1;
    468   1.1       roy 		    break;
    469   1.1       roy 	    case 'o':
    470   1.1       roy 		    ofile = optarg;
    471   1.1       roy 		    break;
    472   1.1       roy 	    case 's':
    473   1.4       roy 		    sflag = 1;
    474   1.1       roy 		    break;
    475   1.1       roy 	    case 'x':
    476  1.10       roy 		    flags |= TIC_EXTRA;
    477   1.1       roy 		    break;
    478   1.1       roy 	    case '?': /* FALLTHROUGH */
    479   1.1       roy 	    default:
    480   1.6       roy 		    fprintf(stderr, "usage: %s [-acSsx] [-o file] source\n",
    481   1.1       roy 			getprogname());
    482   1.1       roy 		    return EXIT_FAILURE;
    483   1.1       roy 	    }
    484   1.1       roy 
    485   1.1       roy 	if (optind == argc)
    486   1.1       roy 		errx(1, "No source file given");
    487   1.1       roy 	source = argv[optind++];
    488   1.1       roy 	f = fopen(source, "r");
    489   1.1       roy 	if (f == NULL)
    490   1.1       roy 		err(1, "fopen: %s", source);
    491   1.6       roy 	if (!cflag && !Sflag) {
    492   1.1       roy 		if (ofile == NULL)
    493   1.1       roy 			ofile = source;
    494   1.1       roy 		len = strlen(ofile) + 9;
    495   1.1       roy 		dbname = malloc(len + 4); /* For adding .db after open */
    496   1.1       roy 		if (dbname == NULL)
    497   1.1       roy 			err(1, "malloc");
    498   1.1       roy 		snprintf(dbname, len, "%s.tmp", ofile);
    499   1.1       roy 		db = dbm_open(dbname, O_CREAT | O_RDWR | O_TRUNC, DEFFILEMODE);
    500   1.1       roy 		if (db == NULL)
    501   1.1       roy 			err(1, "dbopen: %s", source);
    502   1.1       roy 		p = dbname + strlen(dbname);
    503   1.1       roy 		*p++ = '.';
    504   1.1       roy 		*p++ = 'd';
    505   1.1       roy 		*p++ = 'b';
    506   1.1       roy 		*p++ = '\0';
    507   1.1       roy 		atexit(do_unlink);
    508   1.1       roy 	} else
    509   1.1       roy 		db = NULL; /* satisfy gcc warning */
    510   1.1       roy 
    511  1.12       roy 	buf = NULL;
    512  1.12       roy 	buflen = tbuf.buflen = tbuf.bufpos = 0;
    513  1.12       roy 	while ((len = getline(&buf, &buflen, f)) != -1) {
    514   1.1       roy 		/* Skip comments */
    515   1.1       roy 		if (*buf == '#')
    516   1.1       roy 			continue;
    517  1.12       roy 		if (buf[len - 1] != '\n') {
    518  1.10       roy 			process_entry(&tbuf, flags);
    519   1.1       roy 			dowarn("last line is not a comment"
    520   1.1       roy 			    " and does not end with a newline");
    521   1.1       roy 			continue;
    522   1.1       roy 		}
    523   1.1       roy 		/*
    524   1.1       roy 		  If the first char is space not a space then we have a
    525   1.1       roy 		  new entry, so process it.
    526   1.1       roy 		*/
    527   1.1       roy 		if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0)
    528  1.10       roy 			process_entry(&tbuf, flags);
    529   1.1       roy 
    530   1.1       roy 		/* Grow the buffer if needed */
    531  1.12       roy 		grow_tbuf(&tbuf, len);
    532   1.1       roy 		/* Append the string */
    533  1.12       roy 		memcpy(tbuf.buf + tbuf.bufpos, buf, len);
    534  1.12       roy 		tbuf.bufpos += len;
    535   1.1       roy 	}
    536   1.1       roy 	/* Process the last entry if not done already */
    537  1.10       roy 	process_entry(&tbuf, flags);
    538   1.1       roy 
    539   1.1       roy 	/* Merge use entries until we have merged all we can */
    540  1.10       roy 	while (merge_use(flags) != 0)
    541   1.1       roy 		;
    542   1.1       roy 
    543   1.6       roy 	if (Sflag) {
    544   1.5       roy 		print_dump(argc - optind, argv + optind);
    545   1.5       roy 		return error_exit;
    546   1.5       roy 	}
    547   1.5       roy 
    548   1.6       roy 	if (cflag)
    549   1.1       roy 		return error_exit;
    550   1.1       roy 
    551   1.1       roy 	/* Save the terms */
    552   1.1       roy 	nterm = nalias = 0;
    553  1.14     joerg 	SLIST_FOREACH(term, &terms, next) {
    554   1.1       roy 		save_term(db, term);
    555   1.1       roy 		if (term->type == 'a')
    556   1.1       roy 			nalias++;
    557   1.1       roy 		else
    558   1.1       roy 			nterm++;
    559   1.1       roy 	}
    560   1.1       roy 
    561   1.1       roy 	/* done! */
    562   1.1       roy 	dbm_close(db);
    563   1.1       roy 
    564   1.1       roy 	/* Rename the tmp db to the real one now */
    565   1.1       roy 	len = strlen(ofile) + 4;
    566   1.1       roy 	p = malloc(len);
    567   1.1       roy 	if (p == NULL)
    568   1.1       roy 		err(1, "malloc");
    569   1.1       roy 	snprintf(p, len, "%s.db", ofile);
    570   1.1       roy 	if (rename(dbname, p) == -1)
    571   1.1       roy 		err(1, "rename");
    572   1.1       roy 	free(dbname);
    573   1.1       roy 	dbname = NULL;
    574   1.1       roy 
    575   1.1       roy 	if (sflag != 0)
    576   1.1       roy 		fprintf(stderr, "%zu entries and %zu aliases written to %s\n",
    577   1.1       roy 		    nterm, nalias, p);
    578   1.1       roy 
    579   1.1       roy 	return EXIT_SUCCESS;
    580   1.1       roy }
    581