xref: /freebsd/usr.bin/grep/queue.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*	$NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
7  * All rights reserved.
8  * Copyright (c) 2020 Kyle Evans <kevans@FreeBSD.org>
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * A really poor man's queue.  It does only what it has to and gets out of
34  * Dodge.  It is used in place of <sys/queue.h> to get a better performance.
35  */
36 
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "grep.h"
45 
46 typedef struct str		qentry_t;
47 
48 static long long		filled;
49 static qentry_t			*qend, *qpool;
50 
51 /*
52  * qnext is the next entry to populate.  qlist is where the list actually
53  * starts, for the purposes of printing.
54  */
55 static qentry_t		*qlist, *qnext;
56 
57 void
58 initqueue(void)
59 {
60 
61 	qlist = qnext = qpool = grep_calloc(Bflag, sizeof(*qpool));
62 	qend = qpool + (Bflag - 1);
63 }
64 
65 static qentry_t *
66 advqueue(qentry_t *itemp)
67 {
68 
69 	if (itemp == qend)
70 		return (qpool);
71 	return (itemp + 1);
72 }
73 
74 /*
75  * Enqueue another line; return true if we've dequeued a line as a result
76  */
77 bool
78 enqueue(struct str *x)
79 {
80 	qentry_t *item;
81 	bool rotated;
82 
83 	item = qnext;
84 	qnext = advqueue(qnext);
85 	rotated = false;
86 
87 	if (filled < Bflag) {
88 		filled++;
89 	} else if (filled == Bflag) {
90 		/* We had already filled up coming in; just rotate. */
91 		qlist = advqueue(qlist);
92 		rotated = true;
93 		free(item->dat);
94 	}
95 	/* len + 1 for NUL-terminator */
96 	item->dat = grep_malloc(sizeof(char) * x->len + 1);
97 	item->len = x->len;
98 	item->line_no = x->line_no;
99 	item->boff = x->boff;
100 	item->off = x->off;
101 	memcpy(item->dat, x->dat, x->len);
102 	item->dat[x->len] = '\0';
103 	item->file = x->file;
104 
105 	return (rotated);
106 }
107 
108 void
109 printqueue(void)
110 {
111 	qentry_t *item;
112 
113 	item = qlist;
114 	do {
115 		/* Buffer must have ended early. */
116 		if (item->dat == NULL)
117 			break;
118 
119 		grep_printline(item, '-');
120 		free(item->dat);
121 		item->dat = NULL;
122 		item = advqueue(item);
123 	} while (item != qlist);
124 
125 	qlist = qnext = qpool;
126 	filled = 0;
127 }
128 
129 void
130 clearqueue(void)
131 {
132 	qentry_t *item;
133 
134 	item = qlist;
135 	do {
136 		free(item->dat);
137 		item->dat = NULL;
138 		item = advqueue(item);
139 	} while (item != qlist);
140 
141 	qlist = qnext = qpool;
142 	filled = 0;
143 }
144