1 /*
2 ** Copyright (C) 2000 Breyten J. Ernsting <bje@dds.nl>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 **
18 */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "xtermset.h"
25 
26 argument *first,*current,*save;
27 
argument_new(int style,int code,char * param)28 argument *argument_new(int style, int code, char *param) {
29    argument *r;
30 
31    r = (argument *)malloc(sizeof(argument));
32    if(r==NULL) return r;
33 
34    memset(r,0,sizeof(argument));
35 
36    r->style = style;
37    r->command = code;
38    if(param!=NULL)
39       strcpy(r->param,param);
40 
41    r->next = NULL;
42 
43    if(first==NULL) {
44       first = r;
45       first->previous = NULL;
46       first->next = NULL;
47    }
48 
49    if(first->previous != NULL) {
50       first->previous->next = r;
51       r->previous=first->previous;
52    }
53 
54    first->previous = r;
55 
56    current = r;
57 
58    return r;
59 }
60 
argument_freeall()61 void argument_freeall() {
62    current = first;
63 
64    while(current!=NULL) {
65       argument *n = current->next;
66       free(current);
67       current=n;
68    }
69 
70    current = NULL;
71 }
72 
argument_first()73 argument *argument_first() {
74    if(first!=NULL) {
75       current = first;
76       return current;
77    } else {
78       return NULL;
79    }
80 }
81 
argument_last()82 argument *argument_last() {
83    if(first!=NULL) {
84       current=first->previous;
85       return current;
86    } else {
87       return NULL;
88    }
89 }
90 
argument_next()91 argument *argument_next() {
92    if(current!=NULL) {
93       if (current->next) {
94          current = current->next;
95          return current;
96       } else {
97          return NULL;
98       }
99    } else {
100       return NULL;
101    }
102 }
103 
argument_prev()104 argument *argument_prev() {
105    if(current!=first) {
106       if (current->previous) {
107          current = current->previous;
108          return current;
109       } else {
110          return NULL;
111       }
112    } else {
113       return NULL;
114    }
115 }
116 
argument_save()117 void argument_save() {
118    save = current;
119 }
argument_pop()120 argument *argument_pop() {
121    current = save;
122    return save;
123 }
124 
argument_delete(int afree)125 argument *argument_delete(int afree) {
126    argument *prev,*next,*tmp;
127    if(current!=NULL) {
128       prev = current->previous;
129       next = current->next;
130       tmp = current;
131       if(prev!=NULL) {
132 	 prev->next = (tmp!=first)?next:NULL;
133       }
134       if(next!=NULL)
135 	next->previous = prev;
136       if(tmp==first)
137 	first=next;
138       if(afree) {
139 	 free(tmp);
140 	 return NULL;
141       } else {
142 	 return tmp;
143       }
144    } else {
145       return NULL;
146    }
147 }
148 
argument_find(int style,int code)149 argument *argument_find(int style, int code) {
150    argument *tmp = first;
151    int fnd = 0;
152 
153    while((tmp!=NULL)&&(!fnd)) {
154       fnd=((tmp->style==style)&&(tmp->command==code));
155       if(!fnd)
156 	tmp=tmp->next;
157    }
158 
159    if(tmp) {
160       return tmp;
161    } else {
162       return NULL;
163    }
164 }
165