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