1 | /* |
---|
2 | * $XConsortium: util.c,v 2.40 91/07/05 18:30:00 converse Exp $ |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT 1987 |
---|
6 | * DIGITAL EQUIPMENT CORPORATION |
---|
7 | * MAYNARD, MASSACHUSETTS |
---|
8 | * ALL RIGHTS RESERVED. |
---|
9 | * |
---|
10 | * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND |
---|
11 | * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION. |
---|
12 | * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR |
---|
13 | * ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. |
---|
14 | * |
---|
15 | * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT |
---|
16 | * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN |
---|
17 | * ADDITION TO THAT SET FORTH ABOVE. |
---|
18 | * |
---|
19 | * Permission to use, copy, modify, and distribute this software and its |
---|
20 | * documentation for any purpose and without fee is hereby granted, provided |
---|
21 | * that the above copyright notice appear in all copies and that both that |
---|
22 | * copyright notice and this permission notice appear in supporting |
---|
23 | * documentation, and that the name of Digital Equipment Corporation not be |
---|
24 | * used in advertising or publicity pertaining to distribution of the software |
---|
25 | * without specific, written prior permission. |
---|
26 | */ |
---|
27 | |
---|
28 | /* util.c -- little miscellaneous utilities. */ |
---|
29 | |
---|
30 | #include "xmh.h" |
---|
31 | #include <sys/stat.h> |
---|
32 | #include <errno.h> |
---|
33 | #include <ctype.h> |
---|
34 | #include <X11/cursorfont.h> |
---|
35 | |
---|
36 | #ifndef abs |
---|
37 | #define abs(x) ((x) < 0 ? (-(x)) : (x)) |
---|
38 | #endif |
---|
39 | |
---|
40 | static char *SysErrorMsg (n) |
---|
41 | int n; |
---|
42 | { |
---|
43 | return strerror(n); |
---|
44 | } |
---|
45 | |
---|
46 | /* Something went wrong; panic and quit. */ |
---|
47 | |
---|
48 | void Punt(str) |
---|
49 | char *str; |
---|
50 | { |
---|
51 | (void) fprintf( stderr, "%s: %s\nerrno = %d; %s\007\n", |
---|
52 | progName, str, errno, SysErrorMsg(errno) ); |
---|
53 | if (app_resources.debug) { |
---|
54 | (void)fprintf(stderr, "forcing core dump.\n"); |
---|
55 | (void) fflush(stderr); |
---|
56 | abort(); |
---|
57 | } |
---|
58 | else { |
---|
59 | (void)fprintf(stderr, "exiting.\n"); |
---|
60 | (void)fflush(stderr); |
---|
61 | _exit(-1); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | int myopen(path, flags, mode) |
---|
67 | char *path; |
---|
68 | int flags, mode; |
---|
69 | { |
---|
70 | int fid; |
---|
71 | fid = open(path, flags, mode); |
---|
72 | if (fid >= 0) DEBUG2("# %d : %s\n", fid, path) |
---|
73 | return fid; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | FILE *myfopen(path, mode) |
---|
78 | char *path, *mode; |
---|
79 | { |
---|
80 | FILE *result; |
---|
81 | result = fopen(path, mode); |
---|
82 | if (result) DEBUG2("# %d : %s\n", fileno(result), path) |
---|
83 | return result; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | int myclose(fid) |
---|
89 | { |
---|
90 | if (close(fid) < 0) Punt("Error in myclose!"); |
---|
91 | DEBUG1( "# %d : <Closed>\n", fid) |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | int myfclose(file) |
---|
96 | FILE *file; |
---|
97 | { |
---|
98 | int fid = fileno(file); |
---|
99 | if (fclose(file) < 0) Punt("Error in myfclose!"); |
---|
100 | DEBUG1("# %d : <Closed>\n", fid) |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /* Return a unique file name. */ |
---|
106 | |
---|
107 | char *MakeNewTempFileName() |
---|
108 | { |
---|
109 | static char name[60]; |
---|
110 | static int uniqueid = 0; |
---|
111 | do { |
---|
112 | (void) sprintf(name, "%s/xmh_%ld_%d", app_resources.temp_dir, |
---|
113 | getpid(), uniqueid++); |
---|
114 | } while (FileExists(name)); |
---|
115 | return name; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /* Make an array of string pointers big enough to hold n+1 entries. */ |
---|
120 | |
---|
121 | char **MakeArgv(n) |
---|
122 | int n; |
---|
123 | { |
---|
124 | char **result; |
---|
125 | result = ((char **) XtMalloc((unsigned) (n+1) * sizeof(char *))); |
---|
126 | result[n] = 0; |
---|
127 | return result; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | char **ResizeArgv(argv, n) |
---|
132 | char **argv; |
---|
133 | int n; |
---|
134 | { |
---|
135 | argv = ((char **) XtRealloc((char *) argv, (unsigned) (n+1) * sizeof(char *))); |
---|
136 | argv[n] = 0; |
---|
137 | return argv; |
---|
138 | } |
---|
139 | |
---|
140 | /* Open a file, and punt if we can't. */ |
---|
141 | |
---|
142 | FILEPTR FOpenAndCheck(name, mode) |
---|
143 | char *name, *mode; |
---|
144 | { |
---|
145 | FILEPTR result; |
---|
146 | result = myfopen(name, mode); |
---|
147 | if (result == NULL) { |
---|
148 | char str[500]; |
---|
149 | perror(progName); |
---|
150 | (void)sprintf(str, "Error in FOpenAndCheck(%s, %s)", name, mode); |
---|
151 | Punt(str); |
---|
152 | } |
---|
153 | return result; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | /* Read one line from a file. */ |
---|
158 | |
---|
159 | static char *DoReadLine(fid, lastchar) |
---|
160 | FILEPTR fid; |
---|
161 | char lastchar; |
---|
162 | { |
---|
163 | static char *buf; |
---|
164 | static int maxlength = 0; |
---|
165 | char *ptr, c; |
---|
166 | int length = 0; |
---|
167 | ptr = buf; |
---|
168 | c = ' '; |
---|
169 | while (c != '\n' && !feof(fid)) { |
---|
170 | c = getc(fid); |
---|
171 | if (length++ > maxlength - 5) { |
---|
172 | if (maxlength) |
---|
173 | buf = XtRealloc(buf, (unsigned) (maxlength *= 2)); |
---|
174 | else |
---|
175 | buf = XtMalloc((unsigned) (maxlength = 512)); |
---|
176 | ptr = buf + length - 1; |
---|
177 | } |
---|
178 | *ptr++ = c; |
---|
179 | } |
---|
180 | if (!feof(fid) || length > 1) { |
---|
181 | *ptr = 0; |
---|
182 | *--ptr = lastchar; |
---|
183 | return buf; |
---|
184 | } |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | char *ReadLine(fid) |
---|
190 | FILEPTR fid; |
---|
191 | { |
---|
192 | return DoReadLine(fid, 0); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | /* Read a line, and keep the CR at the end. */ |
---|
197 | |
---|
198 | char *ReadLineWithCR(fid) |
---|
199 | FILEPTR fid; |
---|
200 | { |
---|
201 | return DoReadLine(fid, '\n'); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | /* Delete a file, and Punt if it fails. */ |
---|
207 | |
---|
208 | void DeleteFileAndCheck(name) |
---|
209 | char *name; |
---|
210 | { |
---|
211 | if (strcmp(name, "/dev/null") != 0 && unlink(name) == -1) { |
---|
212 | char str[500]; |
---|
213 | perror(progName); |
---|
214 | (void)sprintf(str, "DeleteFileAndCheck(%s) failed!", name); |
---|
215 | Punt(str); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void CopyFileAndCheck(from, to) |
---|
220 | char *from, *to; |
---|
221 | { |
---|
222 | int fromfid, tofid, n; |
---|
223 | char buf[512]; |
---|
224 | fromfid = myopen(from, O_RDONLY, 0666); |
---|
225 | tofid = myopen(to, O_WRONLY | O_TRUNC | O_CREAT, 0666); |
---|
226 | if (fromfid < 0 || tofid < 0) { |
---|
227 | perror(progName); |
---|
228 | (void)sprintf(buf, "CopyFileAndCheck(%s->%s) failed!", from, to); |
---|
229 | Punt(buf); |
---|
230 | } |
---|
231 | do { |
---|
232 | n = read(fromfid, buf, 512); |
---|
233 | if (n) (void) write(tofid, buf, n); |
---|
234 | } while (n); |
---|
235 | (void) myclose(fromfid); |
---|
236 | (void) myclose(tofid); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | void RenameAndCheck(from, to) |
---|
241 | char *from, *to; |
---|
242 | { |
---|
243 | if (rename(from, to) == -1) { |
---|
244 | if (errno != EXDEV) { |
---|
245 | char str[500]; |
---|
246 | perror(progName); |
---|
247 | (void)sprintf(str, "RenameAndCheck(%s->%s) failed!", from, to); |
---|
248 | Punt(str); |
---|
249 | } |
---|
250 | CopyFileAndCheck(from, to); |
---|
251 | DeleteFileAndCheck(from); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | char *CreateGeometry(gbits, x, y, width, height) |
---|
257 | int gbits; |
---|
258 | int x, y, width, height; |
---|
259 | { |
---|
260 | char *result, str1[10], str2[10], str3[10], str4[10]; |
---|
261 | if (gbits & WidthValue) |
---|
262 | (void) sprintf(str1, "=%d", width); |
---|
263 | else |
---|
264 | (void) strcpy(str1, "="); |
---|
265 | if (gbits & HeightValue) |
---|
266 | (void) sprintf(str2, "x%d", height); |
---|
267 | else |
---|
268 | (void) strcpy(str2, "x"); |
---|
269 | if (gbits & XValue) |
---|
270 | (void) sprintf(str3, "%c%d", (gbits & XNegative) ? '-' : '+', abs(x)); |
---|
271 | else |
---|
272 | (void) strcpy(str3, ""); |
---|
273 | if (gbits & YValue) |
---|
274 | (void) sprintf(str4, "%c%d", (gbits & YNegative) ? '-' : '+', abs(y)); |
---|
275 | else |
---|
276 | (void) strcpy(str4, ""); |
---|
277 | result = XtMalloc((unsigned) 22); |
---|
278 | (void) sprintf(result, "%s%s%s%s", str1, str2, str3, str4); |
---|
279 | return result; |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | FileExists(file) |
---|
284 | char *file; |
---|
285 | { |
---|
286 | return (access(file, F_OK) == 0); |
---|
287 | } |
---|
288 | |
---|
289 | LastModifyDate(file) |
---|
290 | char *file; |
---|
291 | { |
---|
292 | struct stat buf; |
---|
293 | if (stat(file, &buf)) return -1; |
---|
294 | return buf.st_mtime; |
---|
295 | } |
---|
296 | |
---|
297 | CurrentDate() |
---|
298 | { |
---|
299 | struct timeval time; |
---|
300 | struct timezone zone; |
---|
301 | (void) gettimeofday(&time, &zone); |
---|
302 | return time.tv_sec; |
---|
303 | } |
---|
304 | |
---|
305 | GetFileLength(file) |
---|
306 | char *file; |
---|
307 | { |
---|
308 | struct stat buf; |
---|
309 | if (stat(file, &buf)) return -1; |
---|
310 | return buf.st_size; |
---|
311 | } |
---|
312 | |
---|
313 | Boolean IsSubfolder(foldername) |
---|
314 | char *foldername; |
---|
315 | { |
---|
316 | return (index(foldername, '/')) ? True : False; |
---|
317 | } |
---|
318 | |
---|
319 | void SetCurrentFolderName(scrn, foldername) |
---|
320 | Scrn scrn; |
---|
321 | char *foldername; |
---|
322 | { |
---|
323 | scrn->curfolder = foldername; |
---|
324 | ChangeLabel((Widget) scrn->folderlabel, foldername); |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | void ChangeLabel(widget, str) |
---|
329 | Widget widget; |
---|
330 | char *str; |
---|
331 | { |
---|
332 | static Arg arglist[] = {XtNlabel, (XtArgVal)NULL}; |
---|
333 | arglist[0].value = (XtArgVal) str; |
---|
334 | XtSetValues(widget, arglist, XtNumber(arglist)); |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | Widget CreateTextSW(scrn, name, args, num_args) |
---|
339 | Scrn scrn; |
---|
340 | char *name; |
---|
341 | ArgList args; |
---|
342 | Cardinal num_args; |
---|
343 | { |
---|
344 | /* most text widget options are set in the application defaults file */ |
---|
345 | |
---|
346 | return XtCreateManagedWidget( name, asciiTextWidgetClass, scrn->widget, |
---|
347 | args, num_args); |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | Widget CreateTitleBar(scrn, name) |
---|
352 | Scrn scrn; |
---|
353 | char *name; |
---|
354 | { |
---|
355 | Widget result; |
---|
356 | int height; |
---|
357 | static Arg arglist[] = { |
---|
358 | {XtNlabel, (XtArgVal)NULL}, |
---|
359 | }; |
---|
360 | arglist[0].value = (XtArgVal) app_resources.banner; /* xmh version */ |
---|
361 | result = XtCreateManagedWidget( name, labelWidgetClass, scrn->widget, |
---|
362 | arglist, XtNumber(arglist) ); |
---|
363 | height = GetHeight(result); |
---|
364 | XawPanedSetMinMax(result, height, height); |
---|
365 | return result; |
---|
366 | } |
---|
367 | |
---|
368 | |
---|
369 | void Feep() |
---|
370 | { |
---|
371 | XBell(theDisplay, 0); |
---|
372 | } |
---|
373 | |
---|
374 | |
---|
375 | MsgList CurMsgListOrCurMsg(toc) |
---|
376 | Toc toc; |
---|
377 | { |
---|
378 | MsgList result; |
---|
379 | Msg curmsg; |
---|
380 | result = TocCurMsgList(toc); |
---|
381 | if (result->nummsgs == 0 && (curmsg = TocGetCurMsg(toc))) { |
---|
382 | FreeMsgList(result); |
---|
383 | result = MakeSingleMsgList(curmsg); |
---|
384 | } |
---|
385 | return result; |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | int GetHeight(w) |
---|
390 | Widget w; |
---|
391 | { |
---|
392 | Dimension height; |
---|
393 | Arg args[1]; |
---|
394 | |
---|
395 | XtSetArg(args[0], XtNheight, &height); |
---|
396 | XtGetValues( w, args, (Cardinal)1 ); |
---|
397 | return (int)height; |
---|
398 | } |
---|
399 | |
---|
400 | |
---|
401 | int GetWidth(w) |
---|
402 | Widget w; |
---|
403 | { |
---|
404 | Dimension width; |
---|
405 | Arg args[1]; |
---|
406 | |
---|
407 | XtSetArg(args[0], XtNwidth, &width); |
---|
408 | XtGetValues( w, args, (Cardinal)1 ); |
---|
409 | return (int)width; |
---|
410 | } |
---|
411 | |
---|
412 | |
---|
413 | Toc SelectedToc(scrn) |
---|
414 | Scrn scrn; |
---|
415 | { |
---|
416 | Toc toc; |
---|
417 | |
---|
418 | /* tocs of subfolders are created upon the first reference */ |
---|
419 | |
---|
420 | if ((toc = TocGetNamed(scrn->curfolder)) == NULL) |
---|
421 | toc = TocCreate(scrn->curfolder); |
---|
422 | return toc; |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | Toc CurrentToc(scrn) |
---|
427 | Scrn scrn; |
---|
428 | { |
---|
429 | /* return the toc currently being viewed */ |
---|
430 | |
---|
431 | return scrn->toc; |
---|
432 | } |
---|
433 | |
---|
434 | |
---|
435 | int strncmpIgnoringCase(str1, str2, length) |
---|
436 | char *str1, *str2; |
---|
437 | int length; |
---|
438 | { |
---|
439 | int i, diff; |
---|
440 | for (i=0 ; i<length ; i++, str1++, str2++) { |
---|
441 | diff = ((*str1 >= 'A' && *str1 <= 'Z') ? (*str1 + 'a' - 'A') : *str1) - |
---|
442 | ((*str2 >= 'A' && *str2 <= 'Z') ? (*str2 + 'a' - 'A') : *str2); |
---|
443 | if (diff) return diff; |
---|
444 | } |
---|
445 | return 0; |
---|
446 | } |
---|
447 | |
---|
448 | |
---|
449 | void StoreWindowName(scrn, str) |
---|
450 | Scrn scrn; |
---|
451 | char *str; |
---|
452 | { |
---|
453 | static Arg arglist[] = { |
---|
454 | {XtNiconName, (XtArgVal) NULL}, |
---|
455 | {XtNtitle, (XtArgVal) NULL}, |
---|
456 | }; |
---|
457 | arglist[0].value = arglist[1].value = (XtArgVal) str; |
---|
458 | XtSetValues(scrn->parent, arglist, XtNumber(arglist)); |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | /* Create an input-only window with a busy-wait cursor. */ |
---|
463 | |
---|
464 | void InitBusyCursor(scrn) |
---|
465 | Scrn scrn; |
---|
466 | { |
---|
467 | unsigned long valuemask; |
---|
468 | XSetWindowAttributes attributes; |
---|
469 | |
---|
470 | /* the second condition is for the pick scrn */ |
---|
471 | if (! app_resources.block_events_on_busy || scrn->wait_window) |
---|
472 | return; |
---|
473 | |
---|
474 | /* Ignore device events while the busy cursor is displayed. */ |
---|
475 | |
---|
476 | valuemask = CWDontPropagate | CWCursor; |
---|
477 | attributes.do_not_propagate_mask = (KeyPressMask | KeyReleaseMask | |
---|
478 | ButtonPressMask | ButtonReleaseMask | |
---|
479 | PointerMotionMask); |
---|
480 | attributes.cursor = app_resources.busy_cursor; |
---|
481 | |
---|
482 | /* The window will be as big as the display screen, and clipped by |
---|
483 | * it's own parent window, so we never have to worry about resizing. |
---|
484 | */ |
---|
485 | |
---|
486 | scrn->wait_window = |
---|
487 | XCreateWindow(XtDisplay(scrn->parent), XtWindow(scrn->parent), 0, 0, |
---|
488 | rootwidth, rootheight, (unsigned int) 0, CopyFromParent, |
---|
489 | InputOnly, CopyFromParent, valuemask, &attributes); |
---|
490 | } |
---|
491 | |
---|
492 | void ShowBusyCursor() |
---|
493 | { |
---|
494 | register int i; |
---|
495 | |
---|
496 | for (i=0; i < numScrns; i++) |
---|
497 | if (scrnList[i]->mapped) |
---|
498 | XMapWindow(theDisplay, scrnList[i]->wait_window); |
---|
499 | } |
---|
500 | |
---|
501 | void UnshowBusyCursor() |
---|
502 | { |
---|
503 | register int i; |
---|
504 | |
---|
505 | for (i=0; i < numScrns; i++) |
---|
506 | if (scrnList[i]->mapped) |
---|
507 | XUnmapWindow(theDisplay, scrnList[i]->wait_window); |
---|
508 | } |
---|
509 | |
---|
510 | |
---|
511 | void SetCursorColor(widget, cursor, foreground) |
---|
512 | Widget widget; |
---|
513 | Cursor cursor; |
---|
514 | unsigned long foreground; |
---|
515 | { |
---|
516 | XColor colors[2]; |
---|
517 | Arg args[2]; |
---|
518 | Colormap cmap; |
---|
519 | |
---|
520 | colors[0].pixel = foreground; |
---|
521 | XtSetArg(args[0], XtNbackground, &(colors[1].pixel)); |
---|
522 | XtSetArg(args[1], XtNcolormap, &cmap); |
---|
523 | XtGetValues(widget, args, (Cardinal) 2); |
---|
524 | XQueryColors(XtDisplay(widget), cmap, colors, 2); |
---|
525 | XRecolorCursor(XtDisplay(widget), cursor, &colors[0], &colors[1]); |
---|
526 | } |
---|
527 | |
---|