xref: /freebsd/usr.bin/grep/queue.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1f3f50de6SPedro F. Giffuni /*	$NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $	*/
2b66a823bSGabor Kovesdan /*	$FreeBSD$	*/
3b66a823bSGabor Kovesdan 
44dc88ebeSGabor Kovesdan /*-
5*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6*1de7b4b8SPedro F. Giffuni  *
7a0ef9ad6SDag-Erling Smørgrav  * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
84dc88ebeSGabor Kovesdan  * All rights reserved.
94dc88ebeSGabor Kovesdan  *
104dc88ebeSGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
114dc88ebeSGabor Kovesdan  * modification, are permitted provided that the following conditions
124dc88ebeSGabor Kovesdan  * are met:
134dc88ebeSGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
144dc88ebeSGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
154dc88ebeSGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
164dc88ebeSGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
174dc88ebeSGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
184dc88ebeSGabor Kovesdan  *
194dc88ebeSGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
204dc88ebeSGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214dc88ebeSGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224dc88ebeSGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
234dc88ebeSGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244dc88ebeSGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254dc88ebeSGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264dc88ebeSGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274dc88ebeSGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284dc88ebeSGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294dc88ebeSGabor Kovesdan  * SUCH DAMAGE.
304dc88ebeSGabor Kovesdan  */
314dc88ebeSGabor Kovesdan 
324dc88ebeSGabor Kovesdan /*
334dc88ebeSGabor Kovesdan  * A really poor man's queue.  It does only what it has to and gets out of
344dc88ebeSGabor Kovesdan  * Dodge.  It is used in place of <sys/queue.h> to get a better performance.
354dc88ebeSGabor Kovesdan  */
364dc88ebeSGabor Kovesdan 
374dc88ebeSGabor Kovesdan #include <sys/cdefs.h>
384dc88ebeSGabor Kovesdan __FBSDID("$FreeBSD$");
394dc88ebeSGabor Kovesdan 
404dc88ebeSGabor Kovesdan #include <sys/param.h>
414dc88ebeSGabor Kovesdan #include <sys/queue.h>
424dc88ebeSGabor Kovesdan 
434dc88ebeSGabor Kovesdan #include <stdlib.h>
444dc88ebeSGabor Kovesdan #include <string.h>
454dc88ebeSGabor Kovesdan 
464dc88ebeSGabor Kovesdan #include "grep.h"
474dc88ebeSGabor Kovesdan 
484dc88ebeSGabor Kovesdan struct qentry {
494dc88ebeSGabor Kovesdan 	STAILQ_ENTRY(qentry)	list;
504dc88ebeSGabor Kovesdan 	struct str	 	data;
514dc88ebeSGabor Kovesdan };
524dc88ebeSGabor Kovesdan 
534dc88ebeSGabor Kovesdan static STAILQ_HEAD(, qentry)	queue = STAILQ_HEAD_INITIALIZER(queue);
54b5fc583cSEd Maste static long long		count;
554dc88ebeSGabor Kovesdan 
564dc88ebeSGabor Kovesdan static struct qentry	*dequeue(void);
574dc88ebeSGabor Kovesdan 
58a4f3f02bSEd Maste /*
59a4f3f02bSEd Maste  * Enqueue another line; return true if we've dequeued a line as a result
60a4f3f02bSEd Maste  */
61a4f3f02bSEd Maste bool
624dc88ebeSGabor Kovesdan enqueue(struct str *x)
634dc88ebeSGabor Kovesdan {
644dc88ebeSGabor Kovesdan 	struct qentry *item;
654dc88ebeSGabor Kovesdan 
664dc88ebeSGabor Kovesdan 	item = grep_malloc(sizeof(struct qentry));
674dc88ebeSGabor Kovesdan 	item->data.dat = grep_malloc(sizeof(char) * x->len);
684dc88ebeSGabor Kovesdan 	item->data.len = x->len;
694dc88ebeSGabor Kovesdan 	item->data.line_no = x->line_no;
706d635d3bSEd Maste 	item->data.boff = x->boff;
714dc88ebeSGabor Kovesdan 	item->data.off = x->off;
7259218eb7SGabor Kovesdan 	memcpy(item->data.dat, x->dat, x->len);
734dc88ebeSGabor Kovesdan 	item->data.file = x->file;
744dc88ebeSGabor Kovesdan 
754dc88ebeSGabor Kovesdan 	STAILQ_INSERT_TAIL(&queue, item, list);
764dc88ebeSGabor Kovesdan 
77f3f50de6SPedro F. Giffuni 	if (++count > Bflag) {
78f3f50de6SPedro F. Giffuni 		item = dequeue();
79f3f50de6SPedro F. Giffuni 		free(item->data.dat);
80f3f50de6SPedro F. Giffuni 		free(item);
81a4f3f02bSEd Maste 		return (true);
82f3f50de6SPedro F. Giffuni 	}
83a4f3f02bSEd Maste 	return (false);
844dc88ebeSGabor Kovesdan }
854dc88ebeSGabor Kovesdan 
864dc88ebeSGabor Kovesdan static struct qentry *
874dc88ebeSGabor Kovesdan dequeue(void)
884dc88ebeSGabor Kovesdan {
894dc88ebeSGabor Kovesdan 	struct qentry *item;
904dc88ebeSGabor Kovesdan 
914dc88ebeSGabor Kovesdan 	item = STAILQ_FIRST(&queue);
924dc88ebeSGabor Kovesdan 	if (item == NULL)
934dc88ebeSGabor Kovesdan 		return (NULL);
944dc88ebeSGabor Kovesdan 
954dc88ebeSGabor Kovesdan 	STAILQ_REMOVE_HEAD(&queue, list);
964dc88ebeSGabor Kovesdan 	--count;
974dc88ebeSGabor Kovesdan 	return (item);
984dc88ebeSGabor Kovesdan }
994dc88ebeSGabor Kovesdan 
1004dc88ebeSGabor Kovesdan void
1014dc88ebeSGabor Kovesdan printqueue(void)
1024dc88ebeSGabor Kovesdan {
1034dc88ebeSGabor Kovesdan 	struct qentry *item;
1044dc88ebeSGabor Kovesdan 
1054dc88ebeSGabor Kovesdan 	while ((item = dequeue()) != NULL) {
106a4f3f02bSEd Maste 		grep_printline(&item->data, '-');
1078e5fc70fSPedro F. Giffuni 		free(item->data.dat);
1084dc88ebeSGabor Kovesdan 		free(item);
1094dc88ebeSGabor Kovesdan 	}
1104dc88ebeSGabor Kovesdan }
1114dc88ebeSGabor Kovesdan 
1124dc88ebeSGabor Kovesdan void
1134dc88ebeSGabor Kovesdan clearqueue(void)
1144dc88ebeSGabor Kovesdan {
1154dc88ebeSGabor Kovesdan 	struct qentry *item;
1164dc88ebeSGabor Kovesdan 
117f3f50de6SPedro F. Giffuni 	while ((item = dequeue()) != NULL) {
118f3f50de6SPedro F. Giffuni 		free(item->data.dat);
1194dc88ebeSGabor Kovesdan 		free(item);
1204dc88ebeSGabor Kovesdan 	}
121f3f50de6SPedro F. Giffuni }
122