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