xref: /freebsd/usr.bin/grep/queue.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
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 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "grep.h"
47 
48 typedef struct str		qentry_t;
49 
50 static long long		filled;
51 static qentry_t			*qend, *qpool;
52 
53 /*
54  * qnext is the next entry to populate.  qlist is where the list actually
55  * starts, for the purposes of printing.
56  */
57 static qentry_t		*qlist, *qnext;
58 
59 void
60 initqueue(void)
61 {
62 
63 	qlist = qnext = qpool = grep_calloc(Bflag, sizeof(*qpool));
64 	qend = qpool + (Bflag - 1);
65 }
66 
67 static qentry_t *
68 advqueue(qentry_t *itemp)
69 {
70 
71 	if (itemp == qend)
72 		return (qpool);
73 	return (itemp + 1);
74 }
75 
76 /*
77  * Enqueue another line; return true if we've dequeued a line as a result
78  */
79 bool
80 enqueue(struct str *x)
81 {
82 	qentry_t *item;
83 	bool rotated;
84 
85 	item = qnext;
86 	qnext = advqueue(qnext);
87 	rotated = false;
88 
89 	if (filled < Bflag) {
90 		filled++;
91 	} else if (filled == Bflag) {
92 		/* We had already filled up coming in; just rotate. */
93 		qlist = advqueue(qlist);
94 		rotated = true;
95 		free(item->dat);
96 	}
97 	/* len + 1 for NUL-terminator */
98 	item->dat = grep_malloc(sizeof(char) * x->len + 1);
99 	item->len = x->len;
100 	item->line_no = x->line_no;
101 	item->boff = x->boff;
102 	item->off = x->off;
103 	memcpy(item->dat, x->dat, x->len);
104 	item->dat[x->len] = '\0';
105 	item->file = x->file;
106 
107 	return (rotated);
108 }
109 
110 void
111 printqueue(void)
112 {
113 	qentry_t *item;
114 
115 	item = qlist;
116 	do {
117 		/* Buffer must have ended early. */
118 		if (item->dat == NULL)
119 			break;
120 
121 		grep_printline(item, '-');
122 		free(item->dat);
123 		item->dat = NULL;
124 		item = advqueue(item);
125 	} while (item != qlist);
126 
127 	qlist = qnext = qpool;
128 	filled = 0;
129 }
130 
131 void
132 clearqueue(void)
133 {
134 	qentry_t *item;
135 
136 	item = qlist;
137 	do {
138 		free(item->dat);
139 		item->dat = NULL;
140 		item = advqueue(item);
141 	} while (item != qlist);
142 
143 	qlist = qnext = qpool;
144 	filled = 0;
145 }
146