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 | ** cache.c: manage the files in /tmp |
---|
19 | ** |
---|
20 | ** Ensure we have the ones we need, and get rid of the ones no |
---|
21 | /* |
---|
22 | ** cache.c: manage the files in /tmp |
---|
23 | ** |
---|
24 | ** Ensure we have the ones we need, and get rid of the ones no |
---|
25 | ** longer in use. |
---|
26 | ** |
---|
27 | */ |
---|
28 | |
---|
29 | #include <stdio.h> |
---|
30 | #include <X11/Intrinsic.h> |
---|
31 | #include <X11/StringDefs.h> |
---|
32 | #include <X11/IntrinsicP.h> |
---|
33 | #include <X11/CoreP.h> |
---|
34 | #include <X11/Xaw/Command.h> |
---|
35 | #include <X11/Xaw/AsciiText.h> |
---|
36 | #include <X11/Xaw/TextP.h> |
---|
37 | #include "xdsc.h" |
---|
38 | |
---|
39 | #define NUM_CACHED_FILES 5 |
---|
40 | #define CACHE_DIR_NEXT 1 |
---|
41 | #define CACHE_DIR_PREV 2 |
---|
42 | #define CACHE_DIR_NREF 3 |
---|
43 | #define CACHE_DIR_PREF 4 |
---|
44 | |
---|
45 | |
---|
46 | static char rcsid[] = ""; |
---|
47 | |
---|
48 | extern char *RunCommand(); |
---|
49 | extern EntryRec toplevelbuttons[2][MAX_BUTTONS]; |
---|
50 | extern TextWidget bottextW, toptextW; |
---|
51 | extern Boolean debug; |
---|
52 | extern char filebase[]; |
---|
53 | extern int topscreen; |
---|
54 | extern int edscversion; |
---|
55 | extern Widget topW; |
---|
56 | extern void TopSelect(); |
---|
57 | extern Boolean nocache; |
---|
58 | extern char axis[]; |
---|
59 | |
---|
60 | int next=0, prev=0; |
---|
61 | static int nref = 0, pref=0, fref=0, lref=0; |
---|
62 | static int first=0, last=0; |
---|
63 | static int highestseen; |
---|
64 | static int current; |
---|
65 | |
---|
66 | static int cache[NUM_CACHED_FILES] = {-1,-1,-1,-1,-1}; |
---|
67 | static char currentmtglong[LONGNAMELEN] = ""; |
---|
68 | static char currentmtgshort[SHORTNAMELEN] = ""; |
---|
69 | |
---|
70 | char *GetTransactionFile(); |
---|
71 | |
---|
72 | char * |
---|
73 | CurrentMtg(which) |
---|
74 | int which; |
---|
75 | { |
---|
76 | if (which == 0) |
---|
77 | return (currentmtglong); |
---|
78 | else |
---|
79 | return (currentmtgshort); |
---|
80 | } |
---|
81 | |
---|
82 | int |
---|
83 | SaveMeetingNames(newlong, newshort) |
---|
84 | char *newlong; |
---|
85 | char *newshort; |
---|
86 | { |
---|
87 | char oldlong[LONGNAMELEN]; |
---|
88 | char oldshort[SHORTNAMELEN]; |
---|
89 | if (*currentmtglong) { |
---|
90 | MarkLastRead(); |
---|
91 | DeleteOldTransactions(); |
---|
92 | } |
---|
93 | |
---|
94 | if (*newlong) { |
---|
95 | strcpy (oldlong, currentmtglong); |
---|
96 | strcpy (currentmtglong, newlong); |
---|
97 | strcpy (oldshort, currentmtgshort); |
---|
98 | strcpy (currentmtgshort, newshort); |
---|
99 | if (SetUpTransactionNumbers() == -1) { |
---|
100 | strcpy (currentmtglong, oldlong); |
---|
101 | strcpy (currentmtgshort, oldshort); |
---|
102 | return (-1); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | else { |
---|
107 | *currentmtglong = '\0'; |
---|
108 | *currentmtgshort = '\0'; |
---|
109 | } |
---|
110 | return (0); |
---|
111 | } |
---|
112 | |
---|
113 | /* |
---|
114 | ** GoToTransaction updates the data structures and cache so that |
---|
115 | ** we are in a particular transaction. |
---|
116 | ** |
---|
117 | ** if 'update' is True (it usually is) we put the contents of the |
---|
118 | ** transaction into the lower text widget. |
---|
119 | */ |
---|
120 | |
---|
121 | GoToTransaction (num, update) |
---|
122 | int num; |
---|
123 | Boolean update; |
---|
124 | { |
---|
125 | static char command[LONGNAMELEN + 25]; |
---|
126 | char *filename; |
---|
127 | char *returndata; |
---|
128 | int transactionnum; |
---|
129 | |
---|
130 | if (!num) return; |
---|
131 | |
---|
132 | if (num < 0) |
---|
133 | transactionnum = TransactionNum(num); |
---|
134 | else |
---|
135 | transactionnum = num; |
---|
136 | |
---|
137 | if (update && topscreen == LISTTRNS); |
---|
138 | UpdateHighlightedTransaction(transactionnum); |
---|
139 | |
---|
140 | sprintf (command, "Reading %s [%d-%d], #%d...", |
---|
141 | currentmtglong, first, last, transactionnum); |
---|
142 | PutUpStatusMessage(command); |
---|
143 | |
---|
144 | if ((filename = GetTransactionFile(num)) == (char *) -1) |
---|
145 | return (-1); |
---|
146 | |
---|
147 | if (update) |
---|
148 | FileIntoWidget(filename, bottextW); |
---|
149 | |
---|
150 | sprintf (command, "(gti %d %s)\n", transactionnum, currentmtglong); |
---|
151 | returndata = RunCommand (command, NULL, NULL, True); |
---|
152 | |
---|
153 | if ((int) returndata <= 0) goto DONE; |
---|
154 | |
---|
155 | sscanf ( returndata, |
---|
156 | "%*c%d%d%d%d%d%d%d%*s", |
---|
157 | ¤t, &prev, &next, &pref, |
---|
158 | &nref, &fref, &lref); |
---|
159 | |
---|
160 | myfree(returndata); |
---|
161 | CheckButtonSensitivity(BUTTONS_UPDATE); |
---|
162 | |
---|
163 | if (!nocache) |
---|
164 | CacheSurroundingTransactions(); |
---|
165 | |
---|
166 | if ( current > highestseen) |
---|
167 | highestseen = current; |
---|
168 | |
---|
169 | if ( next > last || current > last) { |
---|
170 | MarkLastRead(); |
---|
171 | (void) SetUpTransactionNumbers(); |
---|
172 | } |
---|
173 | |
---|
174 | if (highestseen == last && topscreen == MAIN) |
---|
175 | RemoveLetterC(); |
---|
176 | DONE: |
---|
177 | sprintf (command, "Reading %s [%d-%d], #%d", |
---|
178 | currentmtglong, first, last, transactionnum); |
---|
179 | PutUpStatusMessage(command); |
---|
180 | } |
---|
181 | |
---|
182 | DeleteOldTransactions() |
---|
183 | { |
---|
184 | char filename[80]; |
---|
185 | unsigned int fd; |
---|
186 | |
---|
187 | DeleteTransactionFile(current); |
---|
188 | DeleteTransactionFile(prev); |
---|
189 | DeleteTransactionFile(next); |
---|
190 | DeleteTransactionFile(pref); |
---|
191 | DeleteTransactionFile(nref); |
---|
192 | |
---|
193 | sprintf (filename, "%s-list", filebase); |
---|
194 | if ((fd = open(filename, O_RDONLY)) != -1) { |
---|
195 | close (fd); |
---|
196 | unlink (filename); |
---|
197 | } |
---|
198 | |
---|
199 | next=0, prev=0, nref=0; |
---|
200 | pref=0, fref=0, lref=0; |
---|
201 | first=0, last=0, current=0; |
---|
202 | } |
---|
203 | |
---|
204 | /* |
---|
205 | ** Read in the transactions immedidately before and after the |
---|
206 | ** current one. |
---|
207 | */ |
---|
208 | |
---|
209 | CacheSurroundingTransactions() |
---|
210 | { |
---|
211 | |
---|
212 | int i; |
---|
213 | |
---|
214 | /* |
---|
215 | ** Remove unnecessary files |
---|
216 | */ |
---|
217 | |
---|
218 | for (i = 0; i < NUM_CACHED_FILES; i++) { |
---|
219 | if ( cache[i] != next && cache[i] != prev && |
---|
220 | cache[i] != nref && cache[i] != pref && |
---|
221 | cache[i] != current && cache[i] != -1) { |
---|
222 | |
---|
223 | DeleteTransactionFile(cache[i]); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | /* |
---|
228 | ** Get the files we need |
---|
229 | */ |
---|
230 | (void) GetTransactionFile (next); |
---|
231 | (void) GetTransactionFile (prev); |
---|
232 | (void) GetTransactionFile (nref); |
---|
233 | (void) GetTransactionFile (pref); |
---|
234 | (void) GetTransactionFile (current); |
---|
235 | |
---|
236 | } |
---|
237 | |
---|
238 | /* |
---|
239 | ** Remove the file from /tmp and the cache. |
---|
240 | */ |
---|
241 | |
---|
242 | DeleteTransactionFile(num) |
---|
243 | int num; |
---|
244 | { |
---|
245 | char filename[50]; |
---|
246 | int i; |
---|
247 | |
---|
248 | sprintf (filename, "%s-%d", filebase, num); |
---|
249 | unlink (filename); |
---|
250 | |
---|
251 | for (i = 0; i < NUM_CACHED_FILES; i++) |
---|
252 | if (cache[i] == num) |
---|
253 | cache[i] = -1; |
---|
254 | } |
---|
255 | |
---|
256 | /* |
---|
257 | ** This function behaves differently depending on the version of |
---|
258 | ** edsc we're talking to. |
---|
259 | ** |
---|
260 | ** If edsc is doing the caching, just ask for the transaction and |
---|
261 | ** return the filename. |
---|
262 | ** |
---|
263 | ** If we're doing the caching, ask if the file is already in the cache, |
---|
264 | ** and return the filename if it is. Otherwise, run the |
---|
265 | ** edsc command to fetch it, note it as cached, and return the filename. |
---|
266 | ** |
---|
267 | */ |
---|
268 | |
---|
269 | char * |
---|
270 | GetTransactionFile(num) |
---|
271 | int num; |
---|
272 | { |
---|
273 | static char filename[50]; |
---|
274 | char command[LONGNAMELEN + 25]; |
---|
275 | int i; |
---|
276 | char *retval; |
---|
277 | int transactionnum; |
---|
278 | int direction; |
---|
279 | |
---|
280 | if (num == 0) |
---|
281 | return((char *)-1); |
---|
282 | |
---|
283 | /* |
---|
284 | ** have we been passed a symbol instead of a number? |
---|
285 | */ |
---|
286 | |
---|
287 | if (num < 0) |
---|
288 | transactionnum = TransactionNum(num); |
---|
289 | else |
---|
290 | transactionnum = num; |
---|
291 | |
---|
292 | if (!nocache) |
---|
293 | sprintf (filename, "%s-%d", filebase, transactionnum); |
---|
294 | /* |
---|
295 | ** See if the file is in our cache. If so, return its filename;. |
---|
296 | */ |
---|
297 | if (!nocache) { |
---|
298 | for (i = 0; i < NUM_CACHED_FILES; i++) { |
---|
299 | if (cache[i] == num) { |
---|
300 | return (filename); |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | /* |
---|
306 | ** If file doesn't exist, go get it. |
---|
307 | */ |
---|
308 | if (nocache) { |
---|
309 | switch (num) { |
---|
310 | case NEXT: |
---|
311 | direction = CACHE_DIR_NEXT; |
---|
312 | break; |
---|
313 | case PREV: |
---|
314 | direction = CACHE_DIR_PREV; |
---|
315 | break; |
---|
316 | case NREF: |
---|
317 | direction = CACHE_DIR_NREF; |
---|
318 | break; |
---|
319 | case PREF: |
---|
320 | direction = CACHE_DIR_PREF; |
---|
321 | break; |
---|
322 | default: |
---|
323 | direction = 0; |
---|
324 | break; |
---|
325 | } |
---|
326 | |
---|
327 | sprintf ( command, "(gtfc %d %d %s)\n", |
---|
328 | direction, transactionnum, currentmtglong); |
---|
329 | |
---|
330 | retval = RunCommand (command, NULL, NULL, True); |
---|
331 | if ((int) retval <= 0) |
---|
332 | return((char *)-1); |
---|
333 | sscanf (retval, "(\"%[^\"]", filename); |
---|
334 | } |
---|
335 | |
---|
336 | else { |
---|
337 | sprintf ( command, "(gtf %s %d %s)\n", |
---|
338 | filename, transactionnum, currentmtglong); |
---|
339 | retval = RunCommand (command, NULL, NULL, True); |
---|
340 | if ((int) retval <= 0) |
---|
341 | return((char *)-1); |
---|
342 | |
---|
343 | for (i = 0; i < NUM_CACHED_FILES; i++) { |
---|
344 | if (cache[i] == -1) { |
---|
345 | cache[i] = num; |
---|
346 | break; |
---|
347 | } |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | myfree (retval); |
---|
352 | return (filename); |
---|
353 | } |
---|
354 | |
---|
355 | /* |
---|
356 | ** Return the highestseen from mtg. Also set first, last, and highestseen. |
---|
357 | ** Set current to highestseen if it's not already set (ie, we've just |
---|
358 | ** entered the meeting) |
---|
359 | ** |
---|
360 | ** Return -1 if something went wrong. |
---|
361 | */ |
---|
362 | |
---|
363 | int |
---|
364 | SetUpTransactionNumbers() |
---|
365 | { |
---|
366 | |
---|
367 | char command[LONGNAMELEN + 25]; |
---|
368 | char *retval; |
---|
369 | static int oldhighestseen = 0; |
---|
370 | |
---|
371 | sprintf (command, "(gmi %s)\n", currentmtglong); |
---|
372 | |
---|
373 | retval = RunCommand (command, NULL, NULL, True); |
---|
374 | if ((int) retval <= 0) return (-1); |
---|
375 | |
---|
376 | sscanf (retval, "(\"%*[^\"]\" \"%*[^\"]\" \"%*[^\"]\" %d %d %*d %*d \"%*[^\"]\" \"%*[^\"]\" %*d \"%[^\"]\" %d", |
---|
377 | &first, &last, axis, &highestseen); |
---|
378 | |
---|
379 | oldhighestseen = highestseen; |
---|
380 | |
---|
381 | if (last == 0 && first == 0) { |
---|
382 | fprintf (stderr,"xdsc: Reply out of sync with request!\n"); |
---|
383 | fprintf (stderr,"xdsc: requested '%s', got '%s'\n",command, retval); |
---|
384 | PutUpWarning( "INTERNAL ERROR DETECTED", |
---|
385 | "I suggest you quit immediately to avoid\npossible corruption of your .meetings file.", True); |
---|
386 | myfree(retval); |
---|
387 | return (-1); |
---|
388 | } |
---|
389 | myfree(retval); |
---|
390 | |
---|
391 | if (highestseen > last) { |
---|
392 | PutUpWarning( "WARNING", |
---|
393 | "The highest-read transaction in this\nmeeting no longer exists.\nGoing to end of the meeting.", True); |
---|
394 | highestseen = last; |
---|
395 | } |
---|
396 | |
---|
397 | if (debug) { |
---|
398 | fprintf (stderr, "highestseen message of %s is %d\n", |
---|
399 | currentmtglong, highestseen); |
---|
400 | |
---|
401 | fprintf (stderr, "first is %d, last is %d\n",first, last); |
---|
402 | } |
---|
403 | |
---|
404 | GoToTransaction (highestseen, False); |
---|
405 | |
---|
406 | return (highestseen); |
---|
407 | } |
---|
408 | |
---|
409 | MarkLastRead() |
---|
410 | { |
---|
411 | static char command[LONGNAMELEN + 25]; |
---|
412 | |
---|
413 | if (*currentmtglong) { |
---|
414 | sprintf ( command, |
---|
415 | "(ss %d %s)\n", |
---|
416 | highestseen, |
---|
417 | currentmtglong); |
---|
418 | if (edscversion >= 24) |
---|
419 | (void) RunCommand (command, NULL, NULL, True); |
---|
420 | else |
---|
421 | (void) RunCommand (command, NULL, NULL, False); |
---|
422 | } |
---|
423 | |
---|
424 | } |
---|
425 | |
---|
426 | TransactionNum(arg) |
---|
427 | int arg; |
---|
428 | { |
---|
429 | switch (arg) { |
---|
430 | case NEXT: |
---|
431 | return(next); |
---|
432 | case PREV: |
---|
433 | return(prev); |
---|
434 | case NREF: |
---|
435 | return(nref); |
---|
436 | case PREF: |
---|
437 | return(pref); |
---|
438 | case LREF: |
---|
439 | return(lref); |
---|
440 | case FREF: |
---|
441 | return(fref); |
---|
442 | case FIRST: |
---|
443 | return(first); |
---|
444 | case LAST: |
---|
445 | return(last); |
---|
446 | case CURRENT: |
---|
447 | return(current); |
---|
448 | case HIGHESTSEEN: |
---|
449 | return(highestseen); |
---|
450 | default: |
---|
451 | fprintf (stderr, "Unknown arg to TransactionNum: %d\n", arg); |
---|
452 | return(0); |
---|
453 | } |
---|
454 | } |
---|
455 | |
---|
456 | MoveToMeeting(which) |
---|
457 | int which; |
---|
458 | { |
---|
459 | Arg args[2]; |
---|
460 | Boolean transactionflag = False; |
---|
461 | /* |
---|
462 | ** If we're currently showing transactions, we have to restore the top-level |
---|
463 | ** list of meetings. |
---|
464 | */ |
---|
465 | if (topscreen == LISTTRNS) { |
---|
466 | XawTextDisableRedisplay(toptextW); |
---|
467 | TopSelect (NULL, 4 + (1 << 4)); |
---|
468 | transactionflag = True; |
---|
469 | } |
---|
470 | |
---|
471 | /* |
---|
472 | ** HighlightNewItem will eventually call SaveMeetingNames and |
---|
473 | ** SetUpTransactionNumbers to get the next, prev, etc. vars set. |
---|
474 | */ |
---|
475 | if (HighlightNewItem(toptextW, which, True) == 0) { |
---|
476 | |
---|
477 | /* |
---|
478 | ** Need to restore transaction list? |
---|
479 | */ |
---|
480 | if (transactionflag == True) { |
---|
481 | TopSelect (NULL, 4 + (0 << 4)); |
---|
482 | XawTextEnableRedisplay(toptextW); |
---|
483 | } |
---|
484 | |
---|
485 | /* |
---|
486 | ** Clear out bottom text window. |
---|
487 | */ |
---|
488 | XtSetArg (args[0], XtNstring, ""); |
---|
489 | XtSetArg (args[1], XtNlength, 0); |
---|
490 | XtSetValues (bottextW, args, 1); |
---|
491 | |
---|
492 | /* |
---|
493 | ** If we're at the end of the meeting, make it so the "next" button |
---|
494 | ** will show us the last transaction. |
---|
495 | */ |
---|
496 | if (next == 0) |
---|
497 | next = last; |
---|
498 | |
---|
499 | CheckButtonSensitivity(BUTTONS_ON); |
---|
500 | return (0); |
---|
501 | } |
---|
502 | |
---|
503 | else { |
---|
504 | return (-1); |
---|
505 | } |
---|
506 | } |
---|
507 | |
---|
508 | /* |
---|
509 | ** Make sure bottom buttons are set appropriately, according to context |
---|
510 | ** and mode. |
---|
511 | ** |
---|
512 | ** Mode = BUTTONS_UPDATE: turn on appropriate buttons, if sensitive = True. |
---|
513 | ** Mode = BUTTONS_OFF: Turn off all buttons. Set sensitive = False. |
---|
514 | ** Mode = BUTTONS_ON: Turn on appropriate buttons. Set sensitive = True. |
---|
515 | ** |
---|
516 | */ |
---|
517 | CheckButtonSensitivity(mode) |
---|
518 | int mode; |
---|
519 | { |
---|
520 | static Boolean sensitive = True; |
---|
521 | Arg args[2]; |
---|
522 | |
---|
523 | if (mode == BUTTONS_ON) sensitive = True; |
---|
524 | if (mode == BUTTONS_OFF) sensitive = False; |
---|
525 | |
---|
526 | if (next && sensitive) { |
---|
527 | XtSetArg(args[0], XtNsensitive, True); |
---|
528 | XtSetArg(args[1], XtNborderWidth, 1); |
---|
529 | } |
---|
530 | else { |
---|
531 | XtSetArg(args[0], XtNsensitive, False); |
---|
532 | XtSetArg(args[1], XtNborderWidth, 4); |
---|
533 | } |
---|
534 | XtSetValues ((toplevelbuttons[1][0]).button, args, 2); |
---|
535 | |
---|
536 | if (prev && sensitive) |
---|
537 | XtSetArg(args[0], XtNsensitive, True); |
---|
538 | else |
---|
539 | XtSetArg(args[0], XtNsensitive, False); |
---|
540 | XtSetValues ((toplevelbuttons[1][1]).button, args, 1); |
---|
541 | |
---|
542 | if (nref && sensitive) |
---|
543 | XtSetArg(args[0], XtNsensitive, True); |
---|
544 | else |
---|
545 | XtSetArg(args[0], XtNsensitive, False); |
---|
546 | XtSetValues ((toplevelbuttons[1][2]).button, args, 1); |
---|
547 | |
---|
548 | if (pref && sensitive) |
---|
549 | XtSetArg(args[0], XtNsensitive, True); |
---|
550 | else |
---|
551 | XtSetArg(args[0], XtNsensitive, False); |
---|
552 | XtSetValues ((toplevelbuttons[1][3]).button, args, 1); |
---|
553 | |
---|
554 | if (topscreen == LISTTRNS && sensitive) |
---|
555 | XtSetArg(args[0], XtNsensitive, True); |
---|
556 | else |
---|
557 | XtSetArg(args[0], XtNsensitive, False); |
---|
558 | XtSetValues ((toplevelbuttons[0][5]).button, args, 1); |
---|
559 | |
---|
560 | if (axis[0] == ' ' && axis[6] == ' ') |
---|
561 | XtSetArg(args[0], XtNsensitive, False); |
---|
562 | else |
---|
563 | XtSetArg(args[0], XtNsensitive, True); |
---|
564 | XtSetValues ((toplevelbuttons[1][5]).button, args, 1); |
---|
565 | |
---|
566 | if (axis[0] == ' ') |
---|
567 | XtSetArg(args[0], XtNsensitive, False); |
---|
568 | else |
---|
569 | XtSetArg(args[0], XtNsensitive, True); |
---|
570 | XtSetValues ((toplevelbuttons[1][5]).nextrec->button, args, 1); |
---|
571 | |
---|
572 | if (axis[6] == ' ') |
---|
573 | XtSetArg(args[0], XtNsensitive, False); |
---|
574 | else |
---|
575 | XtSetArg(args[0], XtNsensitive, True); |
---|
576 | XtSetValues ((toplevelbuttons[1][5]).nextrec->nextrec->button, args, 1); |
---|
577 | |
---|
578 | XtSetArg(args[0], XtNsensitive, False); |
---|
579 | XtSetValues ((toplevelbuttons[1][6]).nextrec->nextrec->button, args, 1); |
---|
580 | } |
---|