1*9f61b804Splunky /* $NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $ */
20a117266Sjoerg /* $FreeBSD: head/usr.bin/grep/queue.c 211496 2010-08-19 09:28:59Z des $ */
3232a750cScjep /*-
40a117266Sjoerg * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
5232a750cScjep * All rights reserved.
6232a750cScjep *
7232a750cScjep * Redistribution and use in source and binary forms, with or without
8232a750cScjep * modification, are permitted provided that the following conditions
9232a750cScjep * are met:
10232a750cScjep * 1. Redistributions of source code must retain the above copyright
11232a750cScjep * notice, this list of conditions and the following disclaimer.
12232a750cScjep * 2. Redistributions in binary form must reproduce the above copyright
13232a750cScjep * notice, this list of conditions and the following disclaimer in the
14232a750cScjep * documentation and/or other materials provided with the distribution.
15232a750cScjep *
16232a750cScjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17232a750cScjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18232a750cScjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19232a750cScjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20232a750cScjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21232a750cScjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22232a750cScjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23232a750cScjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24232a750cScjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25232a750cScjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26232a750cScjep * SUCH DAMAGE.
27232a750cScjep */
28232a750cScjep
29232a750cScjep /*
30232a750cScjep * A really poor man's queue. It does only what it has to and gets out of
310a117266Sjoerg * Dodge. It is used in place of <sys/queue.h> to get a better performance.
32232a750cScjep */
33232a750cScjep
34f7d22212Sjoerg #if HAVE_NBTOOL_CONFIG_H
35f7d22212Sjoerg #include "nbtool_config.h"
36f7d22212Sjoerg #endif
37f7d22212Sjoerg
380a117266Sjoerg #include <sys/cdefs.h>
39*9f61b804Splunky __RCSID("$NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $");
400a117266Sjoerg
41232a750cScjep #include <sys/param.h>
420a117266Sjoerg #include <sys/queue.h>
43232a750cScjep
44232a750cScjep #include <stdlib.h>
45232a750cScjep #include <string.h>
46232a750cScjep
47232a750cScjep #include "grep.h"
48232a750cScjep
490a117266Sjoerg struct qentry {
500a117266Sjoerg STAILQ_ENTRY(qentry) list;
510a117266Sjoerg struct str data;
520a117266Sjoerg };
53232a750cScjep
540a117266Sjoerg static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue);
550a117266Sjoerg static unsigned long long count;
56232a750cScjep
570a117266Sjoerg static struct qentry *dequeue(void);
58232a750cScjep
59232a750cScjep void
enqueue(struct str * x)600a117266Sjoerg enqueue(struct str *x)
61232a750cScjep {
620a117266Sjoerg struct qentry *item;
63232a750cScjep
640a117266Sjoerg item = grep_malloc(sizeof(struct qentry));
650a117266Sjoerg item->data.dat = grep_malloc(sizeof(char) * x->len);
66232a750cScjep item->data.len = x->len;
67232a750cScjep item->data.line_no = x->line_no;
68232a750cScjep item->data.off = x->off;
69232a750cScjep memcpy(item->data.dat, x->dat, x->len);
70232a750cScjep item->data.file = x->file;
71232a750cScjep
720a117266Sjoerg STAILQ_INSERT_TAIL(&queue, item, list);
73232a750cScjep
744c78e1eaSjoerg if (++count > Bflag) {
754c78e1eaSjoerg item = dequeue();
764c78e1eaSjoerg free(item->data.dat);
774c78e1eaSjoerg free(item);
784c78e1eaSjoerg }
79232a750cScjep }
80232a750cScjep
810a117266Sjoerg static struct qentry *
dequeue(void)82232a750cScjep dequeue(void)
83232a750cScjep {
840a117266Sjoerg struct qentry *item;
85232a750cScjep
860a117266Sjoerg item = STAILQ_FIRST(&queue);
870a117266Sjoerg if (item == NULL)
880a117266Sjoerg return (NULL);
89232a750cScjep
900a117266Sjoerg STAILQ_REMOVE_HEAD(&queue, list);
91232a750cScjep --count;
920a117266Sjoerg return (item);
93232a750cScjep }
94232a750cScjep
95232a750cScjep void
printqueue(void)96232a750cScjep printqueue(void)
97232a750cScjep {
980a117266Sjoerg struct qentry *item;
99232a750cScjep
100232a750cScjep while ((item = dequeue()) != NULL) {
101*9f61b804Splunky printline(&item->data, '-', NULL, 0);
1024c78e1eaSjoerg free(item->data.dat);
1030a117266Sjoerg free(item);
104232a750cScjep }
105232a750cScjep }
106232a750cScjep
107232a750cScjep void
clearqueue(void)108232a750cScjep clearqueue(void)
109232a750cScjep {
1100a117266Sjoerg struct qentry *item;
111232a750cScjep
1124c78e1eaSjoerg while ((item = dequeue()) != NULL) {
1134c78e1eaSjoerg free(item->data.dat);
1140a117266Sjoerg free(item);
115232a750cScjep }
1164c78e1eaSjoerg }
117