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