1 /*===========================================================================
2  Copyright (c) 1998-2000, The Santa Cruz Operation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  *Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11  *Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  *Neither name of The Santa Cruz Operation nor the names of its contributors
16  may be used to endorse or promote products derived from this software
17  without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  INTERRUPTION)
27  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  DAMAGE.
31  =========================================================================*/
32 
33 /*
34  *	command history
35  *
36  *	cscope - interactive C symbol or text cross-reference
37  */
38 
39 #include "global-cscope.h"
40 
41 #include "alloc.h"
42 
43 static	struct cmd *tail, *current;
44 
45 /**
46  *	add a cmd to the history list
47  *
48  *	@param[in]	f	field number
49  *	@param[in]	s	command text
50  */
51 void
addcmd(int f,char * s)52 addcmd(int f, char *s)
53 {
54 	struct cmd *h;
55 
56 	h = mymalloc(sizeof(struct cmd));
57 	if( tail) {
58 		tail->next = h;
59 		h->next = 0;
60 		h->prev = tail;
61 		tail = h;
62 	} else {
63 		tail = h;
64 		h->next = h->prev = 0;
65 	}
66 	h->field = f;
67 	h->text = my_strdup( s);
68 	current = 0;
69 }
70 
71 /** return previous history item */
72 struct cmd *
prevcmd(void)73 prevcmd(void)
74 {
75 	if( current) {
76 		if( current->prev)	/* stay on first item */
77 			return current = current->prev;
78 		else
79 			return current;
80 	} else if( tail)
81 		return current = tail;
82 	else
83 		return NULL;
84 }
85 
86 /** return next history item */
87 struct cmd *
nextcmd(void)88 nextcmd(void)
89 {
90 	if( current) {
91 		if( current->next)	/* stay on first item */
92 			return current = current->next;
93 		else
94 			return current;
95 	} else
96 		return NULL;
97 }
98 /** reset current to tail */
99 void
resetcmd(void)100 resetcmd(void)
101 {
102 	current = 0;
103 }
104 
105 struct cmd *
currentcmd(void)106 currentcmd(void)
107 {
108 	return current;
109 }
110