queue.c revision 1.1 1 1.1 cjep /*-
2 1.1 cjep * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
3 1.1 cjep * All rights reserved.
4 1.1 cjep *
5 1.1 cjep * Redistribution and use in source and binary forms, with or without
6 1.1 cjep * modification, are permitted provided that the following conditions
7 1.1 cjep * are met:
8 1.1 cjep * 1. Redistributions of source code must retain the above copyright
9 1.1 cjep * notice, this list of conditions and the following disclaimer.
10 1.1 cjep * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cjep * notice, this list of conditions and the following disclaimer in the
12 1.1 cjep * documentation and/or other materials provided with the distribution.
13 1.1 cjep *
14 1.1 cjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 1.1 cjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 1.1 cjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 1.1 cjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 1.1 cjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 1.1 cjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 1.1 cjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 1.1 cjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 1.1 cjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 1.1 cjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 1.1 cjep * SUCH DAMAGE.
25 1.1 cjep *
26 1.1 cjep * $Id: queue.c,v 1.1 2004/01/02 14:58:43 cjep Exp $
27 1.1 cjep */
28 1.1 cjep
29 1.1 cjep /*
30 1.1 cjep * A really poor man's queue. It does only what it has to and gets out of
31 1.1 cjep * Dodge.
32 1.1 cjep */
33 1.1 cjep
34 1.1 cjep #include <sys/param.h>
35 1.1 cjep
36 1.1 cjep #include <stdlib.h>
37 1.1 cjep #include <string.h>
38 1.1 cjep
39 1.1 cjep #include "grep.h"
40 1.1 cjep
41 1.1 cjep typedef struct queue {
42 1.1 cjep struct queue *next;
43 1.1 cjep str_t data;
44 1.1 cjep } queue_t;
45 1.1 cjep
46 1.1 cjep static queue_t *q_head, *q_tail;
47 1.1 cjep static int count;
48 1.1 cjep
49 1.1 cjep static queue_t *dequeue(void);
50 1.1 cjep
51 1.1 cjep void
52 1.1 cjep initqueue(void)
53 1.1 cjep {
54 1.1 cjep q_head = q_tail = NULL;
55 1.1 cjep }
56 1.1 cjep
57 1.1 cjep static void
58 1.1 cjep free_item(queue_t *item)
59 1.1 cjep {
60 1.1 cjep free(item);
61 1.1 cjep }
62 1.1 cjep
63 1.1 cjep void
64 1.1 cjep enqueue(str_t *x)
65 1.1 cjep {
66 1.1 cjep queue_t *item;
67 1.1 cjep
68 1.1 cjep item = grep_malloc(sizeof *item + x->len);
69 1.1 cjep item->data.len = x->len;
70 1.1 cjep item->data.line_no = x->line_no;
71 1.1 cjep item->data.off = x->off;
72 1.1 cjep item->data.dat = (char *)item + sizeof *item;
73 1.1 cjep memcpy(item->data.dat, x->dat, x->len);
74 1.1 cjep item->data.file = x->file;
75 1.1 cjep item->next = NULL;
76 1.1 cjep
77 1.1 cjep if (!q_head) {
78 1.1 cjep q_head = q_tail = item;
79 1.1 cjep } else {
80 1.1 cjep q_tail->next = item;
81 1.1 cjep q_tail = item;
82 1.1 cjep }
83 1.1 cjep
84 1.1 cjep if (++count > Bflag)
85 1.1 cjep free_item(dequeue());
86 1.1 cjep }
87 1.1 cjep
88 1.1 cjep static queue_t *
89 1.1 cjep dequeue(void)
90 1.1 cjep {
91 1.1 cjep queue_t *item;
92 1.1 cjep
93 1.1 cjep if (q_head == NULL)
94 1.1 cjep return NULL;
95 1.1 cjep
96 1.1 cjep --count;
97 1.1 cjep item = q_head;
98 1.1 cjep q_head = item->next;
99 1.1 cjep if (q_head == NULL)
100 1.1 cjep q_tail = NULL;
101 1.1 cjep return item;
102 1.1 cjep }
103 1.1 cjep
104 1.1 cjep void
105 1.1 cjep printqueue(void)
106 1.1 cjep {
107 1.1 cjep queue_t *item;
108 1.1 cjep
109 1.1 cjep while ((item = dequeue()) != NULL) {
110 1.1 cjep printline(&item->data, '-');
111 1.1 cjep free_item(item);
112 1.1 cjep }
113 1.1 cjep }
114 1.1 cjep
115 1.1 cjep void
116 1.1 cjep clearqueue(void)
117 1.1 cjep {
118 1.1 cjep queue_t *item;
119 1.1 cjep
120 1.1 cjep while ((item = dequeue()) != NULL)
121 1.1 cjep free_item(item);
122 1.1 cjep }
123 1.1 cjep
124 1.1 cjep int
125 1.1 cjep countqueue(void)
126 1.1 cjep {
127 1.1 cjep return count;
128 1.1 cjep }
129