1 | /* |
---|
2 | Copyright 1991 by the Massachusetts Institute of Technology |
---|
3 | |
---|
4 | Permission to use, copy, modify, and distribute this |
---|
5 | software and its documentation for any purpose and without |
---|
6 | fee is hereby granted, provided that the above copyright |
---|
7 | notice appear in all copies and that both that copyright |
---|
8 | notice and this permission notice appear in supporting |
---|
9 | documentation, and that the name of M.I.T. not be used in |
---|
10 | advertising or publicity pertaining to distribution of the |
---|
11 | software without specific, written prior permission. |
---|
12 | M.I.T. makes no representations about the suitability of |
---|
13 | this software for any purpose. It is provided "as is" |
---|
14 | without express or implied warranty. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | ** headers.c: manage the list of transaction headers |
---|
19 | ** |
---|
20 | */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <X11/Intrinsic.h> |
---|
24 | #include <X11/StringDefs.h> |
---|
25 | #include <X11/IntrinsicP.h> |
---|
26 | #include <X11/CoreP.h> |
---|
27 | #include <X11/Xaw/AsciiText.h> |
---|
28 | #include <X11/Xaw/TextP.h> |
---|
29 | #include "xdsc.h" |
---|
30 | |
---|
31 | static char rcsid[] = ""; |
---|
32 | |
---|
33 | extern char *RunCommand(); |
---|
34 | extern EntryRec toplevelbuttons[2][MAX_BUTTONS]; |
---|
35 | extern TextWidget bottextW, toptextW; |
---|
36 | extern Boolean debug; |
---|
37 | extern char filebase[]; |
---|
38 | extern int topscreen; |
---|
39 | extern void TopSelect(); |
---|
40 | |
---|
41 | static void FetchHeaders(); |
---|
42 | static char oldmeeting[LONGNAMELEN]; |
---|
43 | |
---|
44 | /* |
---|
45 | ** Get the list of transaction headers from start to finish and |
---|
46 | ** put them into the upper text widget. |
---|
47 | */ |
---|
48 | |
---|
49 | PutUpTransactionList(start, finish) |
---|
50 | int start; |
---|
51 | int finish; |
---|
52 | { |
---|
53 | char command[LONGNAMELEN + 25]; |
---|
54 | char filename[70]; |
---|
55 | static int oldstart=0, oldfinish=0; |
---|
56 | Arg args[1]; |
---|
57 | |
---|
58 | XtSetArg(args[0], XtNsensitive, True); |
---|
59 | XtSetValues ((toplevelbuttons[0][5]).button, args, 1); |
---|
60 | |
---|
61 | if (start < TransactionNum(FIRST)) |
---|
62 | start = TransactionNum(FIRST); |
---|
63 | |
---|
64 | if (finish > TransactionNum(LAST)) |
---|
65 | finish = TransactionNum(LAST); |
---|
66 | |
---|
67 | /* |
---|
68 | ** Can we optimize by keeping some of the old data? |
---|
69 | */ |
---|
70 | if ( *oldmeeting && |
---|
71 | !strcmp (oldmeeting, CurrentMtg(0)) && |
---|
72 | finish == oldfinish && |
---|
73 | start <= oldstart) { |
---|
74 | |
---|
75 | /* |
---|
76 | ** Just put up old list |
---|
77 | */ |
---|
78 | if (oldstart == start) { |
---|
79 | sprintf (filename, "%s-list", filebase); |
---|
80 | FileIntoWidget(filename, toptextW); |
---|
81 | return; |
---|
82 | } |
---|
83 | |
---|
84 | /* |
---|
85 | ** Prepend to old list |
---|
86 | */ |
---|
87 | sprintf ( command, "mv %s-list %s-old", |
---|
88 | filebase, filebase); |
---|
89 | |
---|
90 | if (system (command) != 0) { |
---|
91 | sprintf ( command, |
---|
92 | "Cannot write to '%s-old'\n", |
---|
93 | filebase); |
---|
94 | PutUpWarning("WARNING", command, False); |
---|
95 | } |
---|
96 | |
---|
97 | FetchHeaders(start, oldstart-1); |
---|
98 | |
---|
99 | sprintf ( command, |
---|
100 | "cat %s-old >> %s-list", |
---|
101 | filebase, filebase); |
---|
102 | if (system (command) != 0) { |
---|
103 | sprintf ( command, |
---|
104 | "Cannot write to '%s-list'\n", |
---|
105 | filebase); |
---|
106 | PutUpWarning("WARNING", command, False); |
---|
107 | } |
---|
108 | |
---|
109 | sprintf (filename, "%s-old", filebase); |
---|
110 | unlink (filename); |
---|
111 | } |
---|
112 | /* |
---|
113 | ** Get an entirely new list |
---|
114 | */ |
---|
115 | else { |
---|
116 | FetchHeaders(start, finish); |
---|
117 | } |
---|
118 | |
---|
119 | sprintf (filename, "%s-list", filebase); |
---|
120 | FileIntoWidget(filename, toptextW); |
---|
121 | |
---|
122 | strcpy (oldmeeting, CurrentMtg(0)); |
---|
123 | oldstart = start; |
---|
124 | oldfinish = finish; |
---|
125 | |
---|
126 | sprintf (command, "Reading %s [%d-%d], #%d", |
---|
127 | CurrentMtg(0), |
---|
128 | TransactionNum(FIRST), |
---|
129 | TransactionNum(LAST), |
---|
130 | TransactionNum(CURRENT)); |
---|
131 | |
---|
132 | PutUpStatusMessage(command); |
---|
133 | } |
---|
134 | |
---|
135 | /* |
---|
136 | ** Get the headers for the specified range of transactions. Get them in |
---|
137 | ** packets of CHUNKSIZE if necessary, and update status line as doing so. |
---|
138 | */ |
---|
139 | |
---|
140 | static void |
---|
141 | FetchHeaders(start, finish) |
---|
142 | int start; |
---|
143 | int finish; |
---|
144 | { |
---|
145 | int localstart, localfinish; |
---|
146 | char command[LONGNAMELEN + 25]; |
---|
147 | char filename[70]; |
---|
148 | char *returndata; |
---|
149 | |
---|
150 | #define MIN(x,y) ((x) < (y) ? (x) : (y)) |
---|
151 | #define CHUNKSIZE 25 |
---|
152 | |
---|
153 | localstart = start; |
---|
154 | localfinish = finish; |
---|
155 | |
---|
156 | sprintf (filename, "%s-list", filebase); |
---|
157 | unlink (filename); |
---|
158 | sprintf (filename, "%s-temp", filebase); |
---|
159 | unlink (filename); |
---|
160 | |
---|
161 | while (localstart <= finish) { |
---|
162 | localfinish = MIN (localstart + CHUNKSIZE - 1, finish); |
---|
163 | |
---|
164 | if (localfinish != finish) |
---|
165 | |
---|
166 | sprintf ( command, |
---|
167 | "Reading headers for transactions %d to %d (%d remaining)...", |
---|
168 | start, finish, (finish - localstart)); |
---|
169 | else |
---|
170 | sprintf ( command, |
---|
171 | "Reading headers for transactions %d to %d...", |
---|
172 | start, finish); |
---|
173 | |
---|
174 | if (start == finish) |
---|
175 | sprintf ( command, |
---|
176 | "Reading header for transaction %d...", |
---|
177 | start); |
---|
178 | |
---|
179 | |
---|
180 | PutUpTempMessage(command); |
---|
181 | |
---|
182 | sprintf (filename, "%s-temp", filebase); |
---|
183 | sprintf (command, "(ls %s %d %d 0 %s)\n", filename, |
---|
184 | localstart, localfinish, CurrentMtg(0)); |
---|
185 | returndata = RunCommand (command, NULL, NULL, True); |
---|
186 | if ((int) returndata <= 0) { |
---|
187 | TakeDownTempMessage(); |
---|
188 | sprintf (filename, "%s-temp", filebase); |
---|
189 | unlink (filename); |
---|
190 | return; |
---|
191 | } |
---|
192 | myfree (returndata); |
---|
193 | |
---|
194 | sprintf ( command, |
---|
195 | "cat %s-temp >> %s-list", |
---|
196 | filebase, filebase); |
---|
197 | if (system (command) != 0) { |
---|
198 | sprintf ( command, |
---|
199 | "Cannot write to '%s-list'\n", |
---|
200 | filebase); |
---|
201 | PutUpWarning("WARNING", command, False); |
---|
202 | } |
---|
203 | |
---|
204 | localstart += CHUNKSIZE; |
---|
205 | } |
---|
206 | TakeDownTempMessage(); |
---|
207 | sprintf (filename, "%s-temp", filebase); |
---|
208 | unlink (filename); |
---|
209 | } |
---|
210 | |
---|
211 | /* |
---|
212 | ** This should only be called if topscreen == LISTTRNS. It moves |
---|
213 | ** the marker on the upper text widget to the line for the |
---|
214 | ** specified transaction. If moveinsert is true, it tells PutUpArrow |
---|
215 | ** to move the text insert carat there, too. |
---|
216 | */ |
---|
217 | |
---|
218 | UpdateHighlightedTransaction(num, moveinsert) |
---|
219 | int num; |
---|
220 | Boolean moveinsert; |
---|
221 | { |
---|
222 | static int lastend; |
---|
223 | Arg args[5]; |
---|
224 | unsigned int n; |
---|
225 | char *tempstring, *foo = NULL, *bar = NULL; |
---|
226 | char buf[50]; |
---|
227 | |
---|
228 | n = 0; |
---|
229 | XtSetArg(args[n], XtNstring, &tempstring); n++; |
---|
230 | XtGetValues (toptextW, args, n); |
---|
231 | |
---|
232 | /* |
---|
233 | ** Okay, the following is REAL STUPID code. I'm looking for a specific |
---|
234 | ** transaction number, so I sprintf the specified transaction number |
---|
235 | ** into a string and search for it in each line of the text widget's string. |
---|
236 | ** |
---|
237 | ** Should really use a better way to find the line than sequential search! |
---|
238 | */ |
---|
239 | |
---|
240 | sprintf (buf, " [%04d]", num); |
---|
241 | foo = tempstring; |
---|
242 | |
---|
243 | /* |
---|
244 | ** Specialization for common case of moving one forwards... |
---|
245 | */ |
---|
246 | if ( lastend && |
---|
247 | lastend < strlen (tempstring) && |
---|
248 | (!strncmp (tempstring + lastend + 1, buf, strlen(buf)))) |
---|
249 | foo = tempstring + lastend + 1; |
---|
250 | |
---|
251 | else { |
---|
252 | while (*foo) { |
---|
253 | if (!strncmp (foo, buf, strlen(buf))) |
---|
254 | break; |
---|
255 | for ( ; *foo && *foo != '\n'; foo++) |
---|
256 | ; |
---|
257 | if (*foo) foo++; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | if (*foo) { |
---|
262 | for (bar = foo; *bar && *bar != '\n'; bar++) |
---|
263 | ; |
---|
264 | PutUpArrow(toptextW, foo - tempstring, moveinsert); |
---|
265 | } |
---|
266 | if (bar) |
---|
267 | lastend = bar - tempstring; |
---|
268 | } |
---|
269 | |
---|
270 | /* |
---|
271 | ** An arrow key has been hit in the upper text window. If we're showing |
---|
272 | ** transactions, see if we need to fetch another header. |
---|
273 | */ |
---|
274 | |
---|
275 | void |
---|
276 | FetchIfNecessary(w, event, params, num_params) |
---|
277 | Widget w; |
---|
278 | XEvent *event; |
---|
279 | String *params; |
---|
280 | int *num_params; |
---|
281 | { |
---|
282 | TextWidget ctx = toptextW; |
---|
283 | int num; |
---|
284 | int step; |
---|
285 | XawTextPosition inspoint = ctx->text.insertPos; |
---|
286 | |
---|
287 | if (*num_params < 2) |
---|
288 | return; |
---|
289 | |
---|
290 | if (topscreen != LISTTRNS) |
---|
291 | return; |
---|
292 | |
---|
293 | step = atoi (params[1]); |
---|
294 | |
---|
295 | if (!strcmp(params[0], "Up")) { |
---|
296 | num = HighlightedTransaction(); |
---|
297 | if ( (ctx->text.insertPos<ctx->text.lt.info[1].position) && |
---|
298 | ctx->text.lt.top == 0 && |
---|
299 | num != TransactionNum(FIRST)) { |
---|
300 | XawTextDisableRedisplay(ctx); |
---|
301 | PutUpTransactionList(num - step, TransactionNum(LAST)); |
---|
302 | UpdateHighlightedTransaction(TransactionNum(CURRENT),False); |
---|
303 | XawTextSetInsertionPoint(ctx, inspoint + ctx->text.lt.info[step].position); |
---|
304 | XawTextEnableRedisplay(ctx); |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | /* |
---|
310 | ** Pull the transaction number out of the highlighted line and return it |
---|
311 | */ |
---|
312 | |
---|
313 | HighlightedTransaction() |
---|
314 | { |
---|
315 | Arg args[5]; |
---|
316 | unsigned int n, num; |
---|
317 | XawTextPosition start, inspoint; |
---|
318 | char *tempstring; |
---|
319 | |
---|
320 | n = 0; |
---|
321 | XtSetArg(args[n], XtNstring, &tempstring); n++; |
---|
322 | XtGetValues (toptextW, args, n); |
---|
323 | |
---|
324 | inspoint = XawTextGetInsertionPoint(toptextW); |
---|
325 | |
---|
326 | if (tempstring[inspoint] == '\0') |
---|
327 | return; |
---|
328 | |
---|
329 | for (start = inspoint; start && tempstring[start-1] != '\n'; start--) |
---|
330 | ; |
---|
331 | |
---|
332 | num = atoi (strchr (tempstring + start, '[') + 1); |
---|
333 | |
---|
334 | return (num); |
---|
335 | } |
---|
336 | |
---|
337 | InvalidateHeaders() |
---|
338 | { |
---|
339 | strcpy (oldmeeting, "\0"); |
---|
340 | } |
---|