1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1992-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                                                                      *
20 ***********************************************************************/
21 #pragma prototyped
22 /*
23  * uniq
24  *
25  * Written by David Korn
26  */
27 
28 static const char usage[] =
29 "[-n?\n@(#)$Id: uniq (AT&T Research) 2009-08-10 $\n]"
30 USAGE_LICENSE
31 "[+NAME?uniq - Report or filter out repeated lines in a file]"
32 "[+DESCRIPTION?\buniq\b reads the input, compares adjacent lines, and "
33 	"writes one copy of each input line on the output.  The second "
34 	"and succeeding copies of the repeated adjacent lines are not "
35 	"written.]"
36 "[+?If the output file, \aoutfile\a, is not specified, \buniq\b writes "
37 	"to standard output.  If no \ainfile\a is given, or if the \ainfile\a "
38 	"is \b-\b, \buniq\b reads from standard input with the start of "
39 	"the file defined as the current offset.]"
40 "[c:count?Output the number of times each line occurred  along with "
41 	"the line.]"
42 "[d:repeated|duplicates?Output the first of each duplicate line.]"
43 "[D:all-repeated?Output all duplicate lines as a group with an empty "
44     "line delimiter specified by \adelimit\a:]:?[delimit:=none]"
45     "{"
46         "[n:none?Do not delimit duplicate groups.]"
47         "[p:prepend?Prepend an empty line before each group.]"
48         "[s:separate?Separate each group with an empty line.]"
49     "}"
50 "[f:skip-fields]#[fields?\afields\a is the number of fields to skip over "
51     "before checking for uniqueness. A field is the minimal string matching "
52     "the BRE \b[[:blank:]]]]*[^[:blank:]]]]*\b. -\anumber\a is equivalent to "
53     "\b--skip-fields\b=\anumber\a.]"
54 "[i:ignore-case?Ignore case in comparisons.]"
55 "[s:skip-chars]#[chars?\achars\a is the number of characters to skip over "
56 	"before checking for uniqueness.  If specified along with \b-f\b, "
57 	"the first \achars\a after the first \afields\a are ignored.  If "
58 	"the \achars\a specifies more characters than are on the line, "
59 	"an empty string will be used for comparison. +\anumber\a is "
60 	"equivalent to \b--skip-chars\b=\anumber\a.]"
61 "[u:unique?Output unique lines.]"
62 "[w:check-chars]#[chars?\achars\a is the number of characters to compare "
63 	"after skipping any specified fields and characters.]"
64 "\n"
65 "\n[infile [outfile]]\n"
66 "\n"
67 "[+EXIT STATUS?]{"
68 	"[+0?The input file was successfully processed.]"
69 	"[+>0?An error occurred.]"
70 "}"
71 "[+SEE ALSO?\bsort\b(1), \bgrep\b(1)]"
72 ;
73 
74 #include <cmd.h>
75 
76 #define C_FLAG	1
77 #define D_FLAG	2
78 #define U_FLAG	4
79 
80 #define CWIDTH	4
81 #define MAXCNT	9999
82 
83 typedef int (*Compare_f)(const char*, const char*, size_t);
84 
85 static int uniq(Sfio_t *fdin, Sfio_t *fdout, int fields, int chars, int width, int mode, int* all, Compare_f compare)
86 {
87 	register int n, f, outsize=0;
88 	register char *cp, *ep, *bufp, *outp;
89 	char *orecp, *sbufp=0, *outbuff;
90 	int reclen,oreclen= -1,count=0,cwidth=0,sep,next;
91 	if(mode&C_FLAG)
92 		cwidth = CWIDTH+1;
93 	while(1)
94 	{
95 		if(bufp = sfgetr(fdin,'\n',0))
96 			n = sfvalue(fdin);
97 		else if(bufp = sfgetr(fdin,'\n',SF_LASTR))
98 		{
99 			n = sfvalue(fdin);
100 			bufp = memcpy(fmtbuf(n + 1), bufp, n);
101 			bufp[n++] = '\n';
102 		}
103 		else
104 			n = 0;
105 		if(n)
106 		{
107 			cp = bufp;
108 			ep = cp + n;
109 			if(f=fields)
110 				while(f-->0 && cp<ep) /* skip over fields */
111 				{
112 					while(cp<ep && *cp==' ' || *cp=='\t')
113 						cp++;
114 					while(cp<ep && *cp!=' ' && *cp!='\t')
115 						cp++;
116 				}
117 			if(chars)
118 				cp += chars;
119 			if((reclen = n - (cp-bufp)) <=0)
120 			{
121 				reclen = 1;
122 				cp = bufp + sfvalue(fdin)-1;
123 			}
124 			else if(width >= 0 && width < reclen)
125 				reclen = width;
126 		}
127 		else
128 			reclen=-2;
129 		if(reclen==oreclen && (!reclen || !(*compare)(cp,orecp,reclen)))
130 		{
131 			count++;
132 			if (!all)
133 				continue;
134 			next = count;
135 		}
136 		else
137 		{
138 			next = 0;
139 			if(outsize>0)
140 			{
141 				if(((mode&D_FLAG)&&count==0) || ((mode&U_FLAG)&&count))
142 				{
143 					if(outp!=sbufp)
144 						sfwrite(fdout,outp,0);
145 				}
146 				else
147 				{
148 					if(cwidth)
149 					{
150 						if(count<9)
151 						{
152 							f = 0;
153 							while(f < CWIDTH-1)
154 								outp[f++] = ' ';
155 							outp[f++] = '0' + count + 1;
156 							outp[f] = ' ';
157 						}
158 						else if(count<MAXCNT)
159 						{
160 							count++;
161 							f = CWIDTH;
162 							outp[f--] = ' ';
163 							do
164 							{
165 								outp[f--] = '0' + (count % 10);
166 							} while (count /= 10);
167 							while (f >= 0)
168 								outp[f--] = ' ';
169 						}
170 						else
171 						{
172 							outsize -= (CWIDTH+1);
173 							if(outp!=sbufp)
174 							{
175 								if(!(sbufp=fmtbuf(outsize)))
176 									return(1);
177 								memcpy(sbufp,outp+CWIDTH+1,outsize);
178 								sfwrite(fdout,outp,0);
179 								outp = sbufp;
180 							}
181 							else
182 								outp += CWIDTH+1;
183 							sfprintf(fdout,"%4d ",count+1);
184 						}
185 					}
186 					if(sfwrite(fdout,outp,outsize) != outsize)
187 						return(1);
188 				}
189 			}
190 		}
191 		if(n==0)
192 			break;
193 		if(count = next)
194 		{
195 			if(sfwrite(fdout,outp,outsize) != outsize)
196 				return(1);
197 			if(*all >= 0)
198 				*all = 1;
199 			sep = 0;
200 		}
201 		else
202 			sep = all && *all > 0;
203 		/* save current record */
204 		if (!(outbuff = sfreserve(fdout, 0, 0)) || (outsize = sfvalue(fdout)) < 0)
205 			return(1);
206 		outp = outbuff;
207 		if(outsize < n+cwidth+sep)
208 		{
209 			/* no room in outp, clear lock and use side buffer */
210 			sfwrite(fdout,outp,0);
211 			if(!(sbufp = outp=fmtbuf(outsize=n+cwidth+sep)))
212 				return(1);
213 		}
214 		else
215 			outsize = n+cwidth+sep;
216 		memcpy(outp+cwidth+sep,bufp,n);
217 		if(sep)
218 			outp[cwidth] = '\n';
219 		oreclen = reclen;
220 		orecp = outp+cwidth+sep + (cp-bufp);
221 	}
222 	return(0);
223 }
224 
225 int
226 b_uniq(int argc, char** argv, void* context)
227 {
228 	register int n, mode=0;
229 	register char *cp;
230 	int fields=0, chars=0, width=-1;
231 	Sfio_t *fpin, *fpout;
232 	int* all = 0;
233 	int sep;
234 	Compare_f compare = (Compare_f)memcmp;
235 
236 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
237 	while (n = optget(argv, usage)) switch (n)
238 	{
239 	    case 'c':
240 		mode |= C_FLAG;
241 		break;
242 	    case 'd':
243 		mode |= D_FLAG;
244 		break;
245 	    case 'D':
246 		mode |= D_FLAG;
247 		switch ((int)opt_info.num)
248 		{
249 		case 'p':
250 			sep = 1;
251 			break;
252 		case 's':
253 			sep = 0;
254 			break;
255 		default:
256 			sep = -1;
257 			break;
258 		}
259 		all = &sep;
260 		break;
261 	    case 'i':
262 		compare = (Compare_f)strncasecmp;
263 		break;
264 	    case 'u':
265 		mode |= U_FLAG;
266 		break;
267 	    case 'f':
268 		if(*opt_info.option=='-')
269 			fields = opt_info.num;
270 		else
271 			chars = opt_info.num;
272 		break;
273 	    case 's':
274 		chars = opt_info.num;
275 		break;
276 	    case 'w':
277 		width = opt_info.num;
278 		break;
279 	    case ':':
280 		error(2, "%s", opt_info.arg);
281 		break;
282 	    case '?':
283 		error(ERROR_usage(2), "%s", opt_info.arg);
284 		break;
285 	}
286 	argv += opt_info.index;
287 	if(all && (mode&C_FLAG))
288 		error(2, "-c and -D are mutually exclusive");
289 	if(error_info.errors)
290 		error(ERROR_usage(2), "%s", optusage(NiL));
291 	if((cp = *argv) && (argv++,!streq(cp,"-")))
292 	{
293 		if(!(fpin = sfopen(NiL,cp,"r")))
294 			error(ERROR_system(1),"%s: cannot open",cp);
295 	}
296 	else
297 		fpin = sfstdin;
298 	if(cp = *argv)
299 	{
300 		argv++;
301 		if(!(fpout = sfopen(NiL,cp,"w")))
302 			error(ERROR_system(1),"%s: cannot create",cp);
303 	}
304 	else
305 		fpout = sfstdout;
306 	if(*argv)
307 	{
308 		error(2, "too many arguments");
309 		error(ERROR_usage(2), "%s", optusage(NiL));
310 	}
311 	error_info.errors = uniq(fpin,fpout,fields,chars,width,mode,all,compare);
312 	if(fpin!=sfstdin)
313 		sfclose(fpin);
314 	if(fpout!=sfstdout)
315 		sfclose(fpout);
316 	return(error_info.errors);
317 }
318 
319