summaryrefslogtreecommitdiff
path: root/zwgc/xrevstack.c
blob: 12e15aba3c82a1d66f4c730cc73d217dfe81e430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_xrevstack_c[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

#ifndef X_DISPLAY_MISSING

#ifndef TRUEREVSTACK
#include <zephyr/zephyr.h>
#include "X_gram.h"

x_gram *bottom_gram = NULL;
x_gram *unlinked = NULL;
int reverse_stack = 0;

void
add_to_bottom(x_gram *gram)
{
   if (bottom_gram) {
      bottom_gram->below = gram;
      gram->below = NULL;
      gram->above = bottom_gram;
      bottom_gram = gram;
   } else {
      gram->above = NULL;
      gram->below = NULL;
      bottom_gram = gram;
   }
}

/*ARGSUSED*/
void
pull_to_top(x_gram *gram)
{
}

/*ARGSUSED*/
void
push_to_bottom(x_gram *gram)
{
}

void
delete_gram(x_gram *gram)
{
   if (gram == bottom_gram) {
      if (gram->above) {
	 bottom_gram = gram->above;
	 bottom_gram->below = NULL;
      } else {
	 bottom_gram = NULL;
      }
   } else if (gram == unlinked) {
      if (gram->above) {
	 unlinked = gram->above;
	 unlinked->below = NULL;
      } else {
	 unlinked = NULL;
      }
   } else {
      if (gram->above)
	gram->above->below = gram->below;
      gram->below->above = gram->above;     
   }

   /* fix up above & below pointers so that calling delete_gram
      again is safe */
   gram->below = gram;
   gram->above = gram;
}

void
unlink_gram(x_gram *gram)
{
   delete_gram(gram);

   if (unlinked) {
      unlinked->below = gram;
      gram->below = NULL;
      gram->above = unlinked;
      unlinked = gram;
   } else {
      gram->above = NULL;
      gram->below = NULL;
      unlinked = gram;
   }
}

#endif

#ifdef TRUEREVSTACK

#include "X_gram.h"
#include "zwgc.h"
#include <stdio.h>

x_gram *bottom_gram=NULL;
static x_gram *top_gram=NULL;

#ifdef DEBUG
void
print_gram_list(char *str)
{
   x_gram *gram;
   char buf[80];

   if (zwgc_debug) {
      printf("----- From function %s: Top of tree\n",str);
      if (top_gram) 
	if (top_gram->above) 
	  printf("Tree munged: something above top_gram\n");
      for (gram=top_gram;gram;gram=gram->below) {
	 strncpy(buf,gram->text,63);
	 buf[63]='\0';
	 printf("wid %lx txt: %s\n",(long) gram->w,buf);
      }
      if (bottom_gram) 
	if (bottom_gram->below) 
	  printf("Tree munged: something below bottom_gram\n");
      printf("----- Bottom of tree\n");
   }
}
#endif

void
pull_to_top(x_gram *gram)
{
   if (gram==top_gram) {
      /* already here */
      return;
   } else if (top_gram==NULL) {
      /* no grams at all.  Make gram both top and bottom */
      top_gram=gram;
      bottom_gram=gram;
   } else if (gram==bottom_gram) {
      /* bottom gram is special case */
      bottom_gram=bottom_gram->above;
      bottom_gram->below=NULL;
      top_gram->above=gram;
      gram->below=top_gram;
      top_gram=gram;
   } else {
      /* normal case of a gram in the middle */
      gram->above->below=gram->below;
      gram->below->above=gram->above;
      top_gram->above=gram;
      gram->below=top_gram;
      gram->above=NULL;
      top_gram=gram;
   }
#ifdef DEBUG
   print_gram_list("pull_to_top");
#endif
}

void
push_to_bottom(x_gram *gram)
{
   if (gram==bottom_gram) {
      /* already here */
      return;
   } else if (bottom_gram==NULL) {
      /* no grams at all.  Make gram both top and bottom */
      gram->above=NULL;
      gram->below=NULL;
      top_gram=gram;
      bottom_gram=gram;
   } else if (gram==top_gram) {
      /* top gram is special case */
      top_gram=top_gram->below;
      top_gram->above=NULL;
      bottom_gram->below=gram;
      gram->above=bottom_gram;
      bottom_gram=gram;
   } else {
      /* normal case of a gram in the middle */
      gram->above->below=gram->below;
      gram->below->above=gram->above;
      bottom_gram->below=gram;
      gram->above=bottom_gram;
      gram->below=NULL;
      bottom_gram=gram;
   }
#ifdef DEBUG
   print_gram_list("push_to_bottom");
#endif
}

void
unlink_gram(x_gram *gram)
{
   if (top_gram==bottom_gram) {
      /* the only gram in the stack */
      top_gram=NULL;
      bottom_gram=NULL;
   } else if (gram==top_gram) {
      top_gram=gram->below;
      top_gram->above=NULL;
   } else if (gram==bottom_gram) {
      bottom_gram=gram->above;
      bottom_gram->below=NULL;
   } else {
      gram->above->below=gram->below;
      gram->below->above=gram->above;
   }
#ifdef DEBUG
   print_gram_list("unlink_gram");
#endif
}

#ifdef notdef
void
add_to_top(x_gram *gram)
{
   if (top_gram==NULL) {
      gram->above=NULL;
      gram->below=NULL;
      top_gram=gram;
      bottom_gram=gram;
   } else {
      top_gram->above=gram;
      gram->above=NULL;
      gram->below=top_gram;
      top_gram=gram;
   }
#ifdef DEBUG
   print_gram_list("add_to_top");
#endif
}
#endif

void
add_to_bottom(x_gram *gram)
{
   if (bottom_gram==NULL) {
      gram->above=NULL;
      gram->below=NULL;
      top_gram=gram;
      bottom_gram=gram;
   } else {
      bottom_gram->below=gram;
      gram->above=bottom_gram;
      gram->below=NULL;
      bottom_gram=gram;
   }
#ifdef DEBUG
   print_gram_list("add_to_bottom");
#endif
}

#endif /* TRUEREVSTACK */

#endif /* X_DISPLAY_MISSING */