1268799Spfg/*	$NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $	*/
2220422Sgabor/*	$FreeBSD: stable/11/usr.bin/grep/queue.c 330449 2018-03-05 07:26:05Z eadler $	*/
3220422Sgabor
4210389Sgabor/*-
5330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6330449Seadler *
7211496Sdes * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav
8210389Sgabor * All rights reserved.
9210389Sgabor *
10210389Sgabor * Redistribution and use in source and binary forms, with or without
11210389Sgabor * modification, are permitted provided that the following conditions
12210389Sgabor * are met:
13210389Sgabor * 1. Redistributions of source code must retain the above copyright
14210389Sgabor *    notice, this list of conditions and the following disclaimer.
15210389Sgabor * 2. Redistributions in binary form must reproduce the above copyright
16210389Sgabor *    notice, this list of conditions and the following disclaimer in the
17210389Sgabor *    documentation and/or other materials provided with the distribution.
18210389Sgabor *
19210389Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20210389Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21210389Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22210389Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23210389Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24210389Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25210389Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26210389Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27210389Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28210389Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29210389Sgabor * SUCH DAMAGE.
30210389Sgabor */
31210389Sgabor
32210389Sgabor/*
33210389Sgabor * A really poor man's queue.  It does only what it has to and gets out of
34210389Sgabor * Dodge.  It is used in place of <sys/queue.h> to get a better performance.
35210389Sgabor */
36210389Sgabor
37210389Sgabor#include <sys/cdefs.h>
38210389Sgabor__FBSDID("$FreeBSD: stable/11/usr.bin/grep/queue.c 330449 2018-03-05 07:26:05Z eadler $");
39210389Sgabor
40210389Sgabor#include <sys/param.h>
41210389Sgabor#include <sys/queue.h>
42210389Sgabor
43210389Sgabor#include <stdlib.h>
44210389Sgabor#include <string.h>
45210389Sgabor
46210389Sgabor#include "grep.h"
47210389Sgabor
48210389Sgaborstruct qentry {
49210389Sgabor	STAILQ_ENTRY(qentry)	list;
50210389Sgabor	struct str	 	data;
51210389Sgabor};
52210389Sgabor
53210389Sgaborstatic STAILQ_HEAD(, qentry)	queue = STAILQ_HEAD_INITIALIZER(queue);
54322607Skevansstatic long long		count;
55210389Sgabor
56210389Sgaborstatic struct qentry	*dequeue(void);
57210389Sgabor
58322587Skevans/*
59322587Skevans * Enqueue another line; return true if we've dequeued a line as a result
60322587Skevans */
61322587Skevansbool
62210389Sgaborenqueue(struct str *x)
63210389Sgabor{
64210389Sgabor	struct qentry *item;
65210389Sgabor
66210389Sgabor	item = grep_malloc(sizeof(struct qentry));
67210389Sgabor	item->data.dat = grep_malloc(sizeof(char) * x->len);
68210389Sgabor	item->data.len = x->len;
69210389Sgabor	item->data.line_no = x->line_no;
70322610Skevans	item->data.boff = x->boff;
71210389Sgabor	item->data.off = x->off;
72211364Sgabor	memcpy(item->data.dat, x->dat, x->len);
73210389Sgabor	item->data.file = x->file;
74210389Sgabor
75210389Sgabor	STAILQ_INSERT_TAIL(&queue, item, list);
76210389Sgabor
77268799Spfg	if (++count > Bflag) {
78268799Spfg		item = dequeue();
79268799Spfg		free(item->data.dat);
80268799Spfg		free(item);
81322587Skevans		return (true);
82268799Spfg	}
83322587Skevans	return (false);
84210389Sgabor}
85210389Sgabor
86210389Sgaborstatic struct qentry *
87210389Sgabordequeue(void)
88210389Sgabor{
89210389Sgabor	struct qentry *item;
90210389Sgabor
91210389Sgabor	item = STAILQ_FIRST(&queue);
92210389Sgabor	if (item == NULL)
93210389Sgabor		return (NULL);
94210389Sgabor
95210389Sgabor	STAILQ_REMOVE_HEAD(&queue, list);
96210389Sgabor	--count;
97210389Sgabor	return (item);
98210389Sgabor}
99210389Sgabor
100210389Sgaborvoid
101210389Sgaborprintqueue(void)
102210389Sgabor{
103210389Sgabor	struct qentry *item;
104210389Sgabor
105210389Sgabor	while ((item = dequeue()) != NULL) {
106322587Skevans		grep_printline(&item->data, '-');
107268801Spfg		free(item->data.dat);
108210389Sgabor		free(item);
109210389Sgabor	}
110210389Sgabor}
111210389Sgabor
112210389Sgaborvoid
113210389Sgaborclearqueue(void)
114210389Sgabor{
115210389Sgabor	struct qentry *item;
116210389Sgabor
117268799Spfg	while ((item = dequeue()) != NULL) {
118268799Spfg		free(item->data.dat);
119210389Sgabor		free(item);
120268799Spfg	}
121210389Sgabor}
122