1/*	$NetBSD: uniq.c,v 1.4 2008/04/28 20:24:17 martin Exp $	*/
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Christos Zoulas.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33#include <sys/cdefs.h>
34#include <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37#include <db.h>
38#include <err.h>
39#include <libutil.h>
40#include <ctype.h>
41#include <fcntl.h>
42
43#include "extern.h"
44
45static int comp(const char *, char **, size_t *);
46
47/*
48 * Preserve only unique content lines in a file. Input lines that have
49 * content [alphanumeric characters before a comment] are white-space
50 * normalized and have their comments removed. Then they are placed
51 * in a hash table, and only the first instance of them is printed.
52 * Comment lines without any alphanumeric content are always printed
53 * since they are there to make the file "pretty". Comment lines with
54 * alphanumeric content are also placed into the hash table and only
55 * printed once.
56 */
57void
58uniq(const char *fname)
59{
60	DB *db;
61	DBT key;
62	static const DBT data = { NULL, 0 };
63	FILE *fp;
64	char *line;
65	size_t len;
66
67	if ((db = dbopen(NULL, O_RDWR, 0, DB_HASH, &hinfo)) == NULL)
68		err(1, "Cannot create in memory database");
69
70	if ((fp = fopen(fname, "r")) == NULL)
71		err(1, "Cannot open `%s'", fname);
72	while ((line = fgetln(fp, &len)) != NULL) {
73		size_t complen = len;
74		char *compline;
75		if (!comp(line, &compline, &complen)) {
76			(void)fprintf(stdout, "%*.*s", (int)len, (int)len,
77			    line);
78			continue;
79		}
80		key.data = compline;
81		key.size = complen;
82		switch ((db->put)(db, &key, &data, R_NOOVERWRITE)) {
83		case 0:
84			(void)fprintf(stdout, "%*.*s", (int)len, (int)len,
85			    line);
86			break;
87		case 1:
88			break;
89		case -1:
90			err(1, "put");
91			/* NOTREACHED */
92		default:
93			abort();
94			break;
95		}
96	}
97	(void)fflush(stdout);
98	exit(0);
99}
100
101/*
102 * normalize whitespace in the original line and place a new string
103 * with whitespace converted to a single space in compline. If the line
104 * contains just comments, we preserve them. If it contains data and
105 * comments, we kill the comments. Return 1 if the line had actual
106 * contents, or 0 if it was just a comment without alphanumeric characters.
107 */
108static int
109comp(const char *origline, char **compline, size_t *len)
110{
111	const unsigned char *p;
112	unsigned char *q;
113	char *cline;
114	size_t l = *len, complen;
115	int hasalnum, iscomment;
116
117	/* Eat leading space */
118	for (p = (const unsigned char *)origline; l && *p && isspace(*p);
119	    p++, l--)
120		continue;
121	if (*p == '\0' || l == 0)
122		return 0;
123
124	if ((cline = malloc(l + 1)) == NULL)
125		err(1, "Cannot allocate %zu bytes", l + 1);
126	(void)memcpy(cline, p, l);
127	cline[l] = '\0';
128
129	complen = 0;
130	hasalnum = 0;
131	iscomment = 0;
132
133	for (q = (unsigned char *)cline; l && *p; p++, l--) {
134		if (isspace(*p)) {
135			if (complen && isspace(q[-1]))
136				continue;
137			*q++ = ' ';
138			complen++;
139		} else {
140			if (!iscomment && *p == '#') {
141				if (hasalnum)
142					break;
143				iscomment = 1;
144			} else
145				hasalnum |= isalnum(*p);
146			*q++ = *p;
147			complen++;
148		}
149	}
150
151	/* Eat trailing space */
152	while (complen && isspace(q[-1])) {
153		--q;
154		--complen;
155	}
156	*q = '\0';
157	if (!hasalnum) {
158		free(cline);
159		cline = NULL;
160		complen = 0;
161	}
162	*compline = cline;
163	*len = complen;
164	return hasalnum;
165}
166