1 /*
2  * Author:      William Chia-Wei Cheng (bill.cheng@acm.org)
3  *
4  * Copyright (C) 2001-2009, William Chia-Wei Cheng.
5  *
6  * This file may be distributed under the terms of the Q Public License
7  * as defined by Trolltech AS of Norway and appearing in the file
8  * LICENSE.QPL included in the packaging of this file.
9  *
10  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
11  * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
13  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * @(#)$Header: /mm2/home/cvs/bc-src/tgif/msg.c,v 1.49 2011/05/16 16:21:58 william Exp $
19  */
20 
21 #define _INCLUDE_FROM_MSG_C_
22 
23 #include "tgifdefs.h"
24 #include "patchlvl.h"
25 
26 #include "auxtext.e"
27 #include "button.e"
28 #include "chat.e"
29 #include "cutpaste.e"
30 #include "cursor.e"
31 #include "dialog.e"
32 #include "drawing.e"
33 #include "file.e"
34 #include "font.e"
35 #include "grid.e"
36 #include "inmethod.e"
37 #include "mainloop.e"
38 #include "markup.e"
39 #include "menu.e"
40 #include "msg.e"
41 #include "navigate.e"
42 #include "pattern.e"
43 #include "ps.e"
44 #include "raster.e"
45 #include "rect.e"
46 #include "remote.e"
47 #include "rm_intrf.e"
48 #include "setup.e"
49 #include "strtbl.e"
50 #include "tangram2.e"
51 #include "text.e"
52 #include "util.e"
53 #include "version.e"
54 #include "wb.e"
55 #include "xbitmap.e"
56 
57 #include "xbm/redraw.xbm"
58 
59 char	scanFileFullPath[MAXPATHLENGTH+1];
60 char	scanFileName[MAXPATHLENGTH+1];
61 int	scanLineNum=0;
62 
63 char	progName[MAXPATHLENGTH+1];
64 char	fatalMsgBuf1[MAXSTRING<<1];
65 char	fatalMsgBuf2[MAXSTRING<<1];
66 
67 int	cmdLineParentWindowID=0;
68 int	cmdLineTgrm2=FALSE;
69 int	cmdLineWb2=FALSE;
70 
71 struct MsgRec {
72    char			* s;
73    struct MsgRec	* next, * prev;
74 };
75 
76 static struct MsgRec	* topMsg = NULL, * botMsg = NULL;
77 static struct MsgRec	* mostRecentTopMsgPtr = NULL;
78 static int		msgCount = 0;
79 static int		topMsgNumber = 0, mostRecentTopMsgNumber = INVALID;
80 static int		firstCharPos = 0;
81 
82 static int		reverseMouseStatusButtons=FALSE;
83 static int		gnMsgRows=2;
84 
ShowOrPrintMsg(buf,use_msgbox)85 int ShowOrPrintMsg(buf, use_msgbox)
86    char *buf;
87    int use_msgbox;
88 {
89    if (PRTGIF && !cmdLineOpenDisplay) {
90       fprintf(stderr, "%s\n", buf);
91    } else if (use_msgbox) {
92       MsgBox(buf, TOOL_NAME, INFO_MB);
93    } else {
94       Msg(buf);
95    }
96    return FALSE;
97 }
98 
FailAllocMessage()99 int FailAllocMessage()
100 {
101    fprintf(stderr, "%s\n", TgLoadString(STID_OUT_OF_VMEM_CANNOT_MALLOC));
102    fflush(stderr);
103    return FALSE;
104 }
105 
FailAllocBitmapMessage(w,h)106 int FailAllocBitmapMessage(w, h)
107    int w, h;
108 {
109    fprintf(stderr, TgLoadString(STID_CANT_ALLOC_BITMAP_OF_SIZE), w, h);
110    fprintf(stderr, "\n");
111    fflush(stderr);
112    return FALSE;
113 }
114 
FailAllocPixmapMessage(w,h)115 int FailAllocPixmapMessage(w, h)
116    int w, h;
117 {
118    fprintf(stderr, TgLoadString(STID_CANNOT_ALLOC_PIXMAP_OF_SIZE), w, h);
119    fprintf(stderr, "\n");
120    fflush(stderr);
121    return FALSE;
122 }
123 
FailToWriteFileMessage(file_name)124 int FailToWriteFileMessage(file_name)
125    char *file_name;
126 {
127    if (PRTGIF) {
128       fprintf(stderr, TgLoadString(STID_FAIL_TO_WRITE_TO_FILE), file_name);
129       fprintf(stderr, "\n");
130    } else {
131       char msg[MAXPATHLENGTH<<1];
132 
133       sprintf(msg, TgLoadString(STID_FAIL_TO_WRITE_TO_FILE), file_name);
134       MsgBox(msg, TOOL_NAME, INFO_MB);
135    }
136    return FALSE;
137 }
138 
FailToOpenMessage(file_name,rwa,extra_msg)139 int FailToOpenMessage(file_name, rwa, extra_msg)
140    char *file_name, *rwa, *extra_msg;
141    /* rwa is either "r", "w", or "a" */
142 {
143    char msg[MAXPATHLENGTH<<1];
144    int msgid=0;
145 
146    switch (*rwa) {
147    case 'r': msgid = STID_CANNOT_OPEN_FILE_FOR_READING; break;
148    case 'w': msgid = STID_CANNOT_OPEN_FILE_FOR_WRITING; break;
149    case 'a': msgid = STID_CANNOT_OPEN_FILE_FOR_APPEND; break;
150    default: msgid = INVALID; break;
151    }
152    if (extra_msg == NULL) {
153       sprintf(msg, TgLoadString(msgid), file_name);
154       if (PRTGIF) {
155          fprintf(stderr, "%s\n", msg);
156       } else {
157          MsgBox(msg, TOOL_NAME, INFO_MB);
158       }
159    } else {
160       sprintf(msg, TgLoadString(msgid), file_name);
161       strcat(msg, "  ");
162       strcat(msg, extra_msg);
163       if (PRTGIF) {
164          fprintf(stderr, "%s\n", msg);
165       } else {
166          MsgBox(msg, TOOL_NAME, INFO_MB);
167       }
168    }
169    return FALSE;
170 }
171 
FailToCreateWindowMessage(where,win_name,do_exit)172 int FailToCreateWindowMessage(where, win_name, do_exit)
173    char *where, *win_name;
174    int do_exit;
175 {
176    char msg[MAXSTRING<<1];
177 
178    if (win_name == NULL) {
179       strcpy(msg, TgLoadString(STID_FAIL_TO_CREATE_WINDOW));
180    } else {
181       sprintf(msg, TgLoadString(STID_FAIL_TO_CREATE_NAMED_WINDOW), win_name);
182    }
183    if (do_exit) {
184       Error(where, msg);
185    }
186    fprintf(stderr, "%s\n", msg);
187 
188    return FALSE;
189 }
190 
FatalUnexpectedError(msg1,msg2)191 int FatalUnexpectedError(msg1, msg2)
192    char *msg1, *msg2;
193 {
194    char buf[2048];
195 
196    if (msg2 == NULL) {
197       fprintf(stderr, "%s\n", msg1);
198    } else {
199       fprintf(stderr, "%s\n%s\n", msg1, msg2);
200    }
201    fprintf(stderr, TgLoadString(STID_SAFEST_SAVE_N_EXIT_REPRODUCE),
202          authorEmailString);
203    fprintf(stderr, "\n");
204    fflush(stderr);
205 
206    sprintf(buf, TgLoadString(STID_FATAL_UNEXPECTED_ERROR),
207          msg1, (msg2==NULL ? "" : "\n"), (msg2==NULL ? "" : msg2),
208          authorEmailString);
209    MsgBox(buf, TOOL_NAME, STOP_MB);
210    XFlush(mainDisplay);
211    XSync(mainDisplay, False);
212    return FALSE;
213 }
214 
TgAssert(must_be_true,msg1,msg2)215 int TgAssert(must_be_true, msg1, msg2)
216    int must_be_true;
217    char *msg1, *msg2;
218 {
219    if (must_be_true) return TRUE;
220 
221    FatalUnexpectedError(msg1, msg2);
222    return FALSE;
223 }
224 
BoldMsgCharBytes(pszStr)225 int BoldMsgCharBytes(pszStr)
226    char *pszStr;
227 {
228 #ifdef ENABLE_NLS
229    if (boldMsgFontDoubleByte) {
230       if ((*pszStr) & 0x80) {
231          return 2;
232       }
233    }
234 #endif /* ENABLE_NLS */
235    return 1;
236 }
237 
BoldMsgNextChar(pszStr)238 char *BoldMsgNextChar(pszStr)
239    char *pszStr;
240 {
241 #ifdef ENABLE_NLS
242    if (boldMsgFontDoubleByte) {
243       if ((*pszStr) & 0x80) {
244          return (&pszStr[2]);
245       }
246    }
247 #endif /* ENABLE_NLS */
248    return (&pszStr[1]);
249 }
250 
BoldMsgLastChar(pszStr)251 char *BoldMsgLastChar(pszStr)
252    char *pszStr;
253 {
254    if (*pszStr == '\0') return NULL;
255 
256 #ifdef ENABLE_NLS
257    if (boldMsgFontDoubleByte) {
258       int nLen=strlen(pszStr), nIndex=0;
259       char *pszLast=NULL;
260 
261       while (nIndex < nLen) {
262          int nSubStrIndex=0, nDoubleByte=FALSE;
263          char *psz=NULL, *pszStart=NULL;
264 
265          pszStart = &pszStr[nIndex];
266          nDoubleByte = ((*pszStart) & 0x80);
267          if (nDoubleByte) {
268             for (psz=pszStart, nSubStrIndex=0;
269                   nIndex+nSubStrIndex < nLen && ((*psz)&0x80) == nDoubleByte;
270                   psz=(&psz[2]), nSubStrIndex+=2) {
271                pszLast = psz;
272             }
273          } else {
274             for (psz=pszStart, nSubStrIndex=0;
275                   nIndex+nSubStrIndex < nLen && ((*psz)&0x80) == 0;
276                   psz++, nSubStrIndex++) {
277                pszLast = psz;
278             }
279          }
280          nIndex += nSubStrIndex;
281       }
282       return pszLast;
283    }
284 #endif /* ENABLE_NLS */
285    return (&pszStr[strlen(pszStr)-1]);
286 }
287 
BoldMsgStrChr(pszStr,c)288 char *BoldMsgStrChr(pszStr, c)
289    char *pszStr;
290    int c;
291    /* c can only be a single-byte char */
292 {
293 #ifdef ENABLE_NLS
294    if (boldMsgFontDoubleByte) {
295       int nLen=strlen(pszStr), nIndex=0;
296 
297       while (nIndex < nLen) {
298          int nSubStrIndex=0, nDoubleByte=FALSE;
299          char *psz=NULL, *pszStart=NULL;
300 
301          pszStart = &pszStr[nIndex];
302          nDoubleByte = ((*pszStart) & 0x80);
303          if (nDoubleByte) {
304             for (psz=pszStart, nSubStrIndex=0;
305                   nIndex+nSubStrIndex < nLen && ((*psz)&0x80) == nDoubleByte;
306                   psz=(&psz[2]), nSubStrIndex+=2) {
307             }
308          } else {
309             for (psz=pszStart, nSubStrIndex=0;
310                   nIndex+nSubStrIndex < nLen && ((*psz)&0x80) == 0;
311                   psz++, nSubStrIndex++) {
312                if ((int)(*psz) == c) {
313                   return psz;
314                }
315             }
316          }
317          nIndex += nSubStrIndex;
318       }
319       return NULL;
320    }
321 #endif /* ENABLE_NLS */
322    return strchr(pszStr, c);
323 }
324 
MenuTextWidth(font_ptr,psz_str,len)325 int MenuTextWidth(font_ptr, psz_str, len)
326    XFontStruct *font_ptr;
327    char *psz_str;
328    int len;
329 {
330 #ifdef ENABLE_NLS
331    if (menuFontSet != NULL) {
332       return XmbTextEscapement(menuFontSet, psz_str, len);
333    }
334 #endif /* ENABLE_NLS */
335    return XTextWidth(font_ptr, psz_str, len);
336 }
337 
MsgTextWidth(font_ptr,psz_str,len)338 int MsgTextWidth(font_ptr, psz_str, len)
339    XFontStruct *font_ptr;
340    char *psz_str;
341    int len;
342 {
343 #ifdef ENABLE_NLS
344    if (msgFontSet != NULL) {
345       return XmbTextEscapement(msgFontSet, psz_str, len);
346    }
347 #endif /* ENABLE_NLS */
348    return XTextWidth(font_ptr, psz_str, len);
349 }
350 
BoldMsgTextWidth(font_ptr,psz_str,len)351 int BoldMsgTextWidth(font_ptr, psz_str, len)
352    XFontStruct *font_ptr;
353    char *psz_str;
354    int len;
355 {
356 #ifdef ENABLE_NLS
357    if (boldMsgFontSet != NULL) {
358       return XmbTextEscapement(boldMsgFontSet, psz_str, len);
359    }
360 #endif /* ENABLE_NLS */
361    return XTextWidth(font_ptr, psz_str, len);
362 }
363 
ItalicMsgTextWidth(font_ptr,psz_str,len)364 int ItalicMsgTextWidth(font_ptr, psz_str, len)
365    XFontStruct *font_ptr;
366    char *psz_str;
367    int len;
368 {
369 #ifdef ENABLE_NLS
370    if (italicMsgFontSet != NULL) {
371       return XmbTextEscapement(italicMsgFontSet, psz_str, len);
372    }
373 #endif /* ENABLE_NLS */
374    return XTextWidth(font_ptr, psz_str, len);
375 }
376 
BoldItalicMsgTextWidth(font_ptr,psz_str,len)377 int BoldItalicMsgTextWidth(font_ptr, psz_str, len)
378    XFontStruct *font_ptr;
379    char *psz_str;
380    int len;
381 {
382 #ifdef ENABLE_NLS
383    if (boldItalicMsgFontSet != NULL) {
384       return XmbTextEscapement(boldItalicMsgFontSet, psz_str, len);
385    }
386 #endif /* ENABLE_NLS */
387    return XTextWidth(font_ptr, psz_str, len);
388 }
389 
DrawMenuString(dpy,win,gc,x,baseline_y,menu_str,len)390 void DrawMenuString(dpy, win, gc, x, baseline_y, menu_str, len)
391    Display *dpy;
392    Window win;
393    GC gc;
394    int x, baseline_y, len;
395    char *menu_str;
396 {
397 #ifdef ENABLE_NLS
398    if (menuFontSet != NULL) {
399       XmbDrawString(dpy, win, menuFontSet, gc, x, baseline_y, menu_str, len);
400       return;
401    }
402 #endif /* ENABLE_NLS */
403    XDrawString(dpy, win, gc, x, baseline_y, menu_str, len);
404 }
405 
DrawMsgString(dpy,win,gc,x,baseline_y,msg_str,len)406 void DrawMsgString(dpy, win, gc, x, baseline_y, msg_str, len)
407    Display *dpy;
408    Window win;
409    GC gc;
410    int x, baseline_y, len;
411    char *msg_str;
412 {
413 #ifdef ENABLE_NLS
414    if (msgFontSet != NULL) {
415       XmbDrawString(dpy, win, msgFontSet, gc, x, baseline_y, msg_str, len);
416       return;
417    }
418 #endif /* ENABLE_NLS */
419    XDrawString(dpy, win, gc, x, baseline_y, msg_str, len);
420 }
421 
DrawBoldMsgString(dpy,win,gc,x,baseline_y,msg_str,len)422 void DrawBoldMsgString(dpy, win, gc, x, baseline_y, msg_str, len)
423    Display *dpy;
424    Window win;
425    GC gc;
426    int x, baseline_y, len;
427    char *msg_str;
428 {
429 #ifdef ENABLE_NLS
430    if (boldMsgFontSet != NULL) {
431       XmbDrawString(dpy, win, boldMsgFontSet, gc, x, baseline_y, msg_str, len);
432       return;
433    }
434 #endif /* ENABLE_NLS */
435    XDrawString(dpy, win, gc, x, baseline_y, msg_str, len);
436 }
437 
DrawItalicMsgString(dpy,win,gc,x,baseline_y,msg_str,len)438 void DrawItalicMsgString(dpy, win, gc, x, baseline_y, msg_str, len)
439    Display *dpy;
440    Window win;
441    GC gc;
442    int x, baseline_y, len;
443    char *msg_str;
444 {
445 #ifdef ENABLE_NLS
446    if (italicMsgFontSet != NULL) {
447       XmbDrawString(dpy, win, italicMsgFontSet, gc, x, baseline_y, msg_str,
448             len);
449       return;
450    }
451 #endif /* ENABLE_NLS */
452    XDrawString(dpy, win, gc, x, baseline_y, msg_str, len);
453 }
454 
DrawBoldItalicMsgString(dpy,win,gc,x,baseline_y,msg_str,len)455 void DrawBoldItalicMsgString(dpy, win, gc, x, baseline_y, msg_str, len)
456    Display *dpy;
457    Window win;
458    GC gc;
459    int x, baseline_y, len;
460    char *msg_str;
461 {
462 #ifdef ENABLE_NLS
463    if (boldItalicMsgFontSet != NULL) {
464       XmbDrawString(dpy, win, boldItalicMsgFontSet, gc, x, baseline_y, msg_str,
465             len);
466       return;
467    }
468 #endif /* ENABLE_NLS */
469    XDrawString(dpy, win, gc, x, baseline_y, msg_str, len);
470 }
471 
472 static
AddMsg(Msg)473 void AddMsg(Msg)
474    char *Msg;
475 {
476    char *s;
477    struct MsgRec *msg_ptr;
478 
479    firstCharPos = 0;
480    if (*Msg == '\0') { topMsgNumber = msgCount; return; }
481 
482    s = (char*)malloc((strlen(Msg)+1)*sizeof(char));
483    if (s == NULL) FailAllocMessage();
484    msg_ptr = (struct MsgRec *)malloc(sizeof(struct MsgRec));
485    if (msg_ptr == NULL) FailAllocMessage();
486 
487    strcpy(s, Msg);
488    msg_ptr->s = s;
489 
490    ++msgCount;
491    if (msgCount > topMsgNumber+gnMsgRows) topMsgNumber = msgCount-gnMsgRows;
492 
493    msg_ptr->prev = botMsg;
494    msg_ptr->next = NULL;
495 
496    if (botMsg == NULL) {
497       topMsg = msg_ptr;
498    } else {
499       botMsg->next = msg_ptr;
500    }
501    botMsg = msg_ptr;
502 }
503 
CleanUpMsg()504 void CleanUpMsg()
505 {
506    register struct MsgRec *msg_ptr, *prev_msg;
507 
508    for (msg_ptr=botMsg; msg_ptr != NULL; msg_ptr=prev_msg) {
509       prev_msg = msg_ptr->prev;
510       free(msg_ptr->s);
511       free(msg_ptr);
512    }
513    topMsg = botMsg = mostRecentTopMsgPtr = NULL;
514    msgCount = topMsgNumber = firstCharPos = 0;
515    mostRecentTopMsgNumber = INVALID;
516 }
517 
518 static
FindMsg(Number)519 struct MsgRec *FindMsg(Number)
520    int Number;
521 {
522    register int i;
523    register struct MsgRec *ptr;
524 
525    if (Number >= msgCount) {
526       return botMsg;
527    } else if (Number < 0) {
528       return topMsg;
529    } else if (Number > (int)(msgCount/2)) {
530       for (i=msgCount-1, ptr=botMsg; i != Number; i--, ptr=ptr->prev) {
531       }
532    } else {
533       for (i=0, ptr=topMsg; i != Number; i++, ptr=ptr->next) {
534       }
535    }
536    return ptr;
537 }
538 
RedrawMsg(erase_background)539 void RedrawMsg(erase_background)
540    int erase_background;
541 {
542    int i, x, y, len;
543    XEvent ev;
544    struct MsgRec *msg_ptr;
545 
546    if (msgWindow == None) return;
547 
548    if (erase_background) {
549       XClearWindow(mainDisplay, msgWindow);
550       XSync(mainDisplay, False);
551    }
552    while (XCheckWindowEvent(mainDisplay, msgWindow, ExposureMask, &ev)) ;
553 
554    if (topMsgNumber == msgCount) {
555       if (threeDLook) {
556          struct BBRec bbox;
557 
558          SetBBRec(&bbox, 0, 0, msgWindowW, msgWindowH);
559          TgDrawThreeDButton(mainDisplay, msgWindow, textMenuGC, &bbox,
560                TGBS_LOWRED, 2, TRUE);
561       }
562       return;
563    }
564    x = 2 + windowPadding;
565    y = 2 + ((msgFontSet==NULL && msgFontPtr==NULL) ? defaultFontAsc :
566          msgFontAsc) + windowPadding;
567 
568    mostRecentTopMsgPtr = msg_ptr = (topMsgNumber == mostRecentTopMsgNumber) ?
569          mostRecentTopMsgPtr : FindMsg(topMsgNumber);
570    mostRecentTopMsgNumber = topMsgNumber;
571 
572    if (msgFontPtr != NULL) {
573       XSetFont(mainDisplay, defaultGC, msgFontPtr->fid);
574    }
575    for (i=topMsgNumber; i < min(msgCount,topMsgNumber+gnMsgRows); i++) {
576       int w=0;
577 
578       len = strlen(msg_ptr->s);
579       if (msgFontSet == NULL && msgFontPtr == NULL) {
580          w = defaultFontWidth*strlen(msg_ptr->s);
581       } else {
582          w = MsgTextWidth(msgFontPtr, msg_ptr->s, len);
583       }
584       if (w > firstCharPos) {
585          DrawMsgString(mainDisplay, msgWindow, defaultGC, x-firstCharPos, y,
586                msg_ptr->s, len);
587       }
588       msg_ptr = msg_ptr->next;
589       y += ((msgFontSet==NULL && msgFontPtr==NULL) ? defaultFontHeight :
590             msgFontHeight);
591    }
592    if (msgFontSet != NULL || msgFontPtr != NULL) {
593       XSetFont(mainDisplay, defaultGC, defaultFontPtr->fid);
594    }
595    if (erase_background && threeDLook) {
596       struct BBRec bbox;
597 
598       SetBBRec(&bbox, 0, 0, msgWindowW, msgWindowH);
599       TgDrawThreeDButton(mainDisplay, msgWindow, textMenuGC, &bbox,
600             TGBS_LOWRED, 2, TRUE);
601    }
602    XSync(mainDisplay, False);
603 }
604 
Msg(Message)605 void Msg(Message)
606    char *Message;
607 {
608    AddMsg(Message);
609    RedrawMsg(TRUE);
610 }
611 
TwoLineMsg(Msg1,Msg2)612 void TwoLineMsg(Msg1, Msg2)
613    char *Msg1, *Msg2;
614 {
615    AddMsg(Msg1);
616    AddMsg(Msg2);
617    RedrawMsg(TRUE);
618 }
619 
PrintMsgBuffer()620 void PrintMsgBuffer()
621 {
622    char file_name[MAXPATHLENGTH], *rest, msg[MAXSTRING];
623    FILE *fp;
624    struct MsgRec *msg_ptr;
625 
626    *file_name = '\0';
627    Dialog(TgLoadString(STID_ENTER_FNAME_TO_WRITE_MSG_BUF),
628          TgLoadString(STID_STDOUT_STDERR_ESC_CANCEL), file_name);
629    UtilTrimBlanks(file_name);
630    if (*file_name == '\0') return;
631 
632    /* do not translate -- program constants */
633    if (strcmp(file_name, "stdout") == 0) {
634       for (msg_ptr=topMsg; msg_ptr != NULL; msg_ptr=msg_ptr->next) {
635          printf("%s\n", msg_ptr->s);
636       }
637    } else if (strcmp(file_name, "stderr") == 0) {
638       for (msg_ptr=topMsg; msg_ptr != NULL; msg_ptr=msg_ptr->next) {
639          fprintf(stderr, "%s\n", msg_ptr->s);
640       }
641    } else {
642       int short_name;
643 
644       if (!OkayToCreateFile(file_name)) return;
645 
646       if ((short_name=IsPrefix(bootDir, file_name, &rest))) ++rest;
647       if ((fp=fopen(file_name, "w")) == NULL) {
648          sprintf(msg, TgLoadString(STID_CANNOT_OPEN_FILE_FOR_WRITING),
649                (short_name ? rest : file_name));
650          MsgBox(msg, TOOL_NAME, INFO_MB);
651          return;
652       }
653       writeFileFailed = FALSE;
654       for (msg_ptr=topMsg; msg_ptr != NULL; msg_ptr=msg_ptr->next) {
655          if (fprintf(fp, "%s\n", msg_ptr->s) == EOF) {
656             writeFileFailed = TRUE;
657          }
658       }
659       fclose(fp);
660 
661       if (writeFileFailed) {
662          writeFileFailed = FALSE;
663          FailToWriteFileMessage(file_name);
664       } else {
665          sprintf(msg, TgLoadString(STID_MSG_BUF_SAVED_INTO_GIVEN),
666                (short_name ? rest : file_name));
667          MsgBox(msg, TOOL_NAME, INFO_MB);
668       }
669    }
670 }
671 
672 #include "xbm/btn1.xbm"
673 
674 static int	oneLineStatus=FALSE;
675 static char	btnStatusStr[MAX_STATUS_BTNS][MAXSTRING+1];
676 static char	oneLineStatusStr[MAXSTRING+1];
677 
678 static
RedrawStatusStrings()679 void RedrawStatusStrings()
680 {
681    register int i;
682    struct BBRec bbox;
683 
684    if (PRTGIF || noStatusWindow || statusWindow==None) return;
685 
686    if (oneLineStatus) {
687       int y;
688 
689       XClearWindow(mainDisplay, statusWindow);
690       if (threeDLook) {
691          SetBBRec(&bbox, 0, 0, statusWindowW, statusWindowH);
692          TgDrawThreeDButton(mainDisplay, statusWindow, textMenuGC, &bbox,
693                TGBS_RAISED, 1, FALSE);
694       }
695       if (msgFontSet == NULL && msgFontPtr == NULL) {
696          if (defaultFontHeight+windowPadding+(brdrW<<1)+2 > btn1_height) {
697             y = 2 + brdrW + defaultFontAsc + (windowPadding>>1);
698          } else {
699             y = ((statusWindowH-defaultFontHeight-windowPadding)>>1) +
700                   defaultFontAsc;
701          }
702          DrawMsgString(mainDisplay, statusWindow, defaultGC,
703                (windowPadding>>1)+2, y, oneLineStatusStr,
704                strlen(oneLineStatusStr));
705       } else {
706          if (msgFontHeight+windowPadding+(brdrW<<1)+2 > btn1_height) {
707             y = 2 + brdrW + msgFontAsc + (windowPadding>>1);
708          } else {
709             y = ((statusWindowH-msgFontHeight-windowPadding)>>1) + msgFontAsc;
710          }
711          if (msgFontPtr != NULL) {
712             XSetFont(mainDisplay, defaultGC, msgFontPtr->fid);
713          }
714          DrawMsgString(mainDisplay, statusWindow, defaultGC,
715                (windowPadding>>1)+2, y, oneLineStatusStr,
716                strlen(oneLineStatusStr));
717          XSetFont(mainDisplay, defaultGC, defaultFontPtr->fid);
718       }
719    } else {
720       int left=0, w=(int)(statusWindowW/3), right=0;
721 
722       if (msgFontPtr != NULL) {
723          XSetFont(mainDisplay, defaultGC, msgFontPtr->fid);
724       }
725       for (i=0; i < MAX_STATUS_BTNS; i++) {
726          right += w;
727          if (right >= statusWindowW) right = statusWindowW-1;
728          XClearWindow(mainDisplay, statusSubWindow[i]);
729          if (msgFontSet == NULL && msgFontPtr == NULL) {
730             DrawMsgString(mainDisplay, statusSubWindow[i], defaultGC, 2,
731                   1+defaultFontAsc, btnStatusStr[i], strlen(btnStatusStr[i]));
732          } else {
733             DrawMsgString(mainDisplay, statusSubWindow[i], defaultGC, 2,
734                   1+msgFontAsc, btnStatusStr[i], strlen(btnStatusStr[i]));
735          }
736          if (threeDLook) {
737             SetBBRec(&bbox, 0, 0, statusSubWindowW[i], statusSubWindowH[i]);
738             TgDrawThreeDButton(mainDisplay, statusSubWindow[i], textMenuGC,
739                   &bbox, TGBS_LOWRED, 1, FALSE);
740          }
741          left += w;
742       }
743       if (msgFontSet != NULL || msgFontPtr != NULL) {
744          XSetFont(mainDisplay, defaultGC, defaultFontPtr->fid);
745       }
746    }
747 }
748 
RedrawStatusWindow()749 void RedrawStatusWindow()
750 {
751    register int i;
752    XEvent ev;
753 
754    if (PRTGIF || noStatusWindow || statusWindow==None) return;
755 
756    if (!oneLineStatus) {
757       int left=windowPadding, w=(int)(statusWindowW/3), y=0, right=0;
758 
759       if (msgFontSet == NULL && msgFontPtr == NULL) {
760          if (defaultFontHeight+(brdrW<<1)+2 > btn1_height) {
761             y = (statusWindowH-btn1_height)>>1;
762          } else {
763             y = 1;
764          }
765       } else {
766          if (msgFontHeight+(brdrW<<1)+2 > btn1_height) {
767             y = (statusWindowH-btn1_height)>>1;
768          } else {
769             y = 1;
770          }
771       }
772       for (i=0; i < MAX_STATUS_BTNS; i++) {
773          int x=left+(brdrW<<2);
774 
775          right += w;
776          if (right >= statusWindowW) right = statusWindowW-1;
777          XSetTSOrigin(mainDisplay, rasterGC, x, y);
778          XSetStipple(mainDisplay, rasterGC, statusBtnPixmap[i]);
779          XFillRectangle(mainDisplay, statusWindow, rasterGC,
780                x, y, btn1_width, btn1_height);
781          XSetTSOrigin(mainDisplay, rasterGC, 0, 0);
782          left += w;
783       }
784    }
785    RedrawStatusStrings();
786    if (threeDLook) {
787       struct BBRec bbox;
788 
789       SetBBRec(&bbox, 0, 0, statusWindowW, statusWindowH);
790       TgDrawThreeDButton(mainDisplay, statusWindow, textMenuGC, &bbox,
791             TGBS_RAISED, 1, FALSE);
792    }
793    XSync(mainDisplay, False);
794    while (XCheckWindowEvent(mainDisplay, statusWindow, ExposureMask, &ev)) ;
795 }
796 
SetMouseStatus(Left,Middle,Right)797 void SetMouseStatus(Left, Middle, Right)
798    char *Left, *Middle, *Right;
799    /* If Left, Middle, and Right are all NULL, just refresh the status */
800 {
801    register int i;
802    int force_redraw=FALSE, left_index, right_index;
803 
804    if (reverseMouseStatusButtons) {
805       left_index = 2;
806       right_index = 0;
807    } else {
808       left_index = 0;
809       right_index = 2;
810    }
811 
812    if (PRTGIF || noStatusWindow) return;
813 
814    if (oneLineStatus) {
815       oneLineStatus = FALSE;
816       if (statusWindow != None) {
817          XClearWindow(mainDisplay, statusWindow);
818          for (i=0; i < MAX_STATUS_BTNS; i++) {
819             XMapWindow(mainDisplay, statusSubWindow[i]);
820          }
821          XSync(mainDisplay, False);
822       }
823       force_redraw = TRUE;
824    } else if (Left != NULL || Middle != NULL || Right != NULL) {
825       if (((Left == NULL && *btnStatusStr[left_index] == '\0') ||
826             (Left != NULL && strcmp(Left, btnStatusStr[left_index]) == 0)) &&
827             ((Middle == NULL && *btnStatusStr[1] == '\0') ||
828             (Middle != NULL && strcmp(Middle, btnStatusStr[1]) == 0)) &&
829             ((Right == NULL && *btnStatusStr[right_index] == '\0') ||
830             (Right != NULL && strcmp(Right, btnStatusStr[right_index]) == 0))) {
831          return;
832       }
833    }
834    if (Left != NULL || Middle != NULL || Right != NULL) {
835       if (Left != NULL) {
836          if (Left != btnStatusStr[left_index] &&
837                strcmp(Left, btnStatusStr[left_index]) != 0) {
838             UtilStrCpyN(btnStatusStr[left_index], MAXSTRING+1, Left);
839          }
840       } else {
841          *btnStatusStr[left_index] = '\0';
842       }
843       if (Middle != NULL) {
844          if (Middle != btnStatusStr[1] &&
845                strcmp(Middle, btnStatusStr[1]) != 0) {
846             UtilStrCpyN(btnStatusStr[1], MAXSTRING+1, Middle);
847          }
848       } else {
849          *btnStatusStr[1] = '\0';
850       }
851       if (Right != NULL) {
852          if (Right != btnStatusStr[right_index] &&
853                strcmp(Right, btnStatusStr[right_index]) != 0) {
854             UtilStrCpyN(btnStatusStr[right_index], MAXSTRING+1, Right);
855          }
856       } else {
857          *btnStatusStr[right_index] = '\0';
858       }
859    }
860    if (force_redraw) {
861       RedrawStatusWindow();
862    } else {
863       RedrawStatusStrings();
864    }
865 }
866 
SetStringStatus(StatusStr)867 void SetStringStatus(StatusStr)
868    char *StatusStr;
869    /* If StatusStr is NULL, just refresh the status */
870 {
871    register int i;
872    int force_redraw=FALSE;
873 
874    if (PRTGIF || noStatusWindow) return;
875 
876    if (!oneLineStatus) {
877       oneLineStatus = TRUE;
878       if (statusWindow != None) {
879          for (i=0; i < MAX_STATUS_BTNS; i++) {
880             XUnmapWindow(mainDisplay, statusSubWindow[i]);
881          }
882          XSync(mainDisplay, False);
883       }
884       force_redraw = TRUE;
885    } else if (StatusStr != NULL && strcmp(StatusStr, oneLineStatusStr) == 0) {
886       return;
887    }
888    /* if StatusStr and oneLineStatusStr are the same string, don't copy */
889    if (StatusStr != NULL && StatusStr != oneLineStatusStr) {
890       UtilStrCpyN(oneLineStatusStr, MAXSTRING+1, StatusStr);
891    }
892    if (force_redraw) {
893       RedrawStatusWindow();
894    } else {
895       RedrawStatusStrings();
896    }
897 }
898 
899 static MouseOverStatusInfo *topStatusInfo=NULL;
900 
SaveStatusStrings()901 void SaveStatusStrings()
902 {
903    MouseOverStatusInfo *pmosi=NULL;
904    int i=0;
905 
906    if (PRTGIF || noStatusWindow) return;
907 
908    pmosi = (MouseOverStatusInfo *)malloc(sizeof(MouseOverStatusInfo));
909    if (pmosi == NULL) FailAllocMessage();
910    pmosi->next = topStatusInfo;
911    for (i=0; i < MAX_STATUS_BTNS; i++) {
912       UtilStrCpyN(pmosi->btn_str[i], MAXSTRING+1, btnStatusStr[i]);
913    }
914    UtilStrCpyN(pmosi->one_line_str, MAXSTRING+1, oneLineStatusStr);
915    pmosi->one_line_status = oneLineStatus;
916    topStatusInfo = pmosi;
917 }
918 
RestoreStatusStrings()919 void RestoreStatusStrings()
920 {
921    MouseOverStatusInfo *pmosi=NULL;
922    int i=0;
923 
924    if (PRTGIF || noStatusWindow || topStatusInfo == NULL) return;
925 
926    for (i=0; i < MAX_STATUS_BTNS; i++) {
927       UtilStrCpyN(btnStatusStr[i], MAXSTRING+1, topStatusInfo->btn_str[i]);
928    }
929    UtilStrCpyN(oneLineStatusStr, MAXSTRING+1, topStatusInfo->one_line_str);
930    if (topStatusInfo->one_line_status != oneLineStatus) {
931       if (topStatusInfo->one_line_status) {
932          SetStringStatus(oneLineStatusStr);
933       } else {
934          SetMouseStatus(btnStatusStr[0], btnStatusStr[1], btnStatusStr[2]);
935       }
936    } else {
937       RedrawStatusWindow();
938    }
939    pmosi = topStatusInfo->next;
940    free(topStatusInfo);
941    topStatusInfo = pmosi;
942 }
943 
SaveStatusStringsIntoBuf(ppsz_buf,pn_one_line)944 void SaveStatusStringsIntoBuf(ppsz_buf, pn_one_line)
945    char ppsz_buf[MAX_STATUS_BTNS+1][MAXSTRING+1];
946    int *pn_one_line;
947    /* dimension of ppsz_buf must be [MAX_STATUS_BTNS+1][MAXSTRING+1] */
948 {
949    register int i;
950 
951    if (PRTGIF || noStatusWindow) return;
952    for (i=0; i < MAX_STATUS_BTNS; i++) {
953       UtilStrCpyN(ppsz_buf[i], MAXSTRING+1, btnStatusStr[i]);
954    }
955    UtilStrCpyN(ppsz_buf[i], MAXSTRING+1, oneLineStatusStr);
956    *pn_one_line = oneLineStatus;
957 }
958 
RestoreStatusStringsFromBuf(ppsz_buf,one_line)959 void RestoreStatusStringsFromBuf(ppsz_buf, one_line)
960    char ppsz_buf[MAX_STATUS_BTNS+1][MAXSTRING+1];
961    int one_line;
962    /* dimension of ppsz_buf must be [MAX_STATUS_BTNS+1][MAXSTRING+1] */
963 {
964    register int i;
965 
966    if (PRTGIF || noStatusWindow) return;
967    for (i=0; i < MAX_STATUS_BTNS; i++) {
968       UtilStrCpyN(btnStatusStr[i], MAXSTRING+1, ppsz_buf[i]);
969    }
970    UtilStrCpyN(oneLineStatusStr, MAXSTRING+1, ppsz_buf[i]);
971    if (one_line != oneLineStatus) {
972       if (one_line) {
973          SetStringStatus(oneLineStatusStr);
974       } else {
975          SetMouseStatus(btnStatusStr[0], btnStatusStr[1], btnStatusStr[2]);
976       }
977    } else {
978       RedrawStatusWindow();
979    }
980 }
981 
BeginProgress(ppi,total_size)982 void BeginProgress(ppi, total_size)
983    ProgressInfo *ppi;
984    int total_size;
985 {
986    memset(ppi, 0, sizeof(ProgressInfo));
987    ppi->total_size = total_size;
988    SaveStatusStringsIntoBuf(ppi->status_buf, &ppi->one_line_status);
989 }
990 
UpdateProgress(ppi,cur_size)991 void UpdateProgress(ppi, cur_size)
992    ProgressInfo *ppi;
993    int cur_size;
994 {
995    int percent=(int)(((double)cur_size) / ((double)ppi->total_size) *
996             ((double)100));
997 
998    if (percent >= ppi->target_percent) {
999       sprintf(gszMsgBox, TgLoadCachedString(CSTID_PROGRESS_PERCENT),
1000             percent);
1001       SetStringStatus(gszMsgBox);
1002       XSync(mainDisplay, False);
1003       while (ppi->target_percent <= percent) ppi->target_percent += 5;
1004    }
1005 }
1006 
EndProgress(ppi)1007 void EndProgress(ppi)
1008    ProgressInfo *ppi;
1009 {
1010    RestoreStatusStringsFromBuf(ppi->status_buf, ppi->one_line_status);
1011 }
1012 
MsgEventHandler(input)1013 void MsgEventHandler(input)
1014    XEvent *input;
1015 {
1016    XButtonEvent *button_ev;
1017 
1018    if (input->type == Expose) {
1019       RedrawMsg(TRUE);
1020    } else if (input->type == EnterNotify) {
1021       SetMouseStatus(TgLoadCachedString(CSTID_SCROLL_DOWN_1_LINE),
1022             TgLoadCachedString(CSTID_SCROLL_AROUND),
1023             TgLoadCachedString(CSTID_SCROLL_UP_1_LINE));
1024    } else if (input->type == ButtonPress) {
1025       button_ev = &(input->xbutton);
1026       if (button_ev->button == Button1) {
1027          if (button_ev->state & (ShiftMask | ControlMask)) {
1028             firstCharPos += (((msgFontSet==NULL && msgFontPtr==NULL) ?
1029                   defaultFontWidth : msgFontWidth)<<2);
1030             RedrawMsg(TRUE);
1031          } else {
1032             if (topMsgNumber+1 >= msgCount) return;
1033 
1034             topMsgNumber++;
1035             RedrawMsg(TRUE);
1036          }
1037       } else if (button_ev->button == Button2) {
1038          int done=FALSE, saved_x=button_ev->x, saved_y=button_ev->y, x, y;
1039          int saved_pos=firstCharPos, cur_top_msg_number=topMsgNumber, cur_dx=0;
1040          XEvent ev;
1041 
1042          RedrawMsg(TRUE);
1043 
1044          XGrabPointer(mainDisplay, msgWindow, False,
1045             PointerMotionMask | ButtonReleaseMask, GrabModeAsync,
1046             GrabModeAsync, None, handCursor, CurrentTime);
1047 
1048          while (!done) {
1049             XNextEvent(mainDisplay, &ev);
1050 
1051             if (ev.type == Expose || ev.type == VisibilityNotify) {
1052                ExposeEventHandler(&ev, TRUE);
1053             } else if (ev.type == ButtonRelease) {
1054                XUngrabPointer(mainDisplay, CurrentTime);
1055                done = TRUE;
1056             } else if (ev.type == MotionNotify) {
1057                double dx=(double)0, dy=(double)0;
1058 
1059                x = ev.xmotion.x;
1060                y = ev.xmotion.y;
1061                dx = ((double)(x-saved_x)) /
1062                      ((double)((msgFontSet==NULL && msgFontPtr==NULL) ?
1063                      defaultFontWidth : msgFontWidth));
1064                dy = ((double)(y-saved_y)) /
1065                      ((double)((msgFontSet==NULL && msgFontPtr==NULL) ?
1066                      defaultFontHeight : msgFontHeight));
1067                topMsgNumber += round(dy);
1068                if (topMsgNumber > msgCount) topMsgNumber = msgCount;
1069                if (topMsgNumber < 0) topMsgNumber = 0;
1070                if (topMsgNumber != cur_top_msg_number || round(dx) != cur_dx) {
1071                   cur_top_msg_number = topMsgNumber;
1072                   cur_dx = round(dx);
1073                   firstCharPos = cur_dx *
1074                         ((msgFontSet==NULL && msgFontPtr==NULL) ?
1075                         defaultFontWidth : msgFontWidth) + saved_pos;
1076                   if (firstCharPos < 0) firstCharPos = 0;
1077                   RedrawMsg(TRUE);
1078                }
1079                while (XCheckMaskEvent(mainDisplay, PointerMotionMask, &ev)) ;
1080             }
1081          }
1082       } else if (button_ev->button == Button3) {
1083          if (button_ev->state & (ShiftMask | ControlMask)) {
1084             if (firstCharPos <= 0) return;
1085 
1086             firstCharPos -= (((msgFontSet==NULL && msgFontPtr==NULL) ?
1087                   defaultFontWidth : msgFontWidth)<<2);
1088             if (firstCharPos < 0) firstCharPos = 0;
1089             RedrawMsg(TRUE);
1090          } else {
1091             if (topMsgNumber == 0) return;
1092 
1093             topMsgNumber--;
1094             RedrawMsg(TRUE);
1095          }
1096       }
1097    }
1098 }
1099 
StatusEventHandler(input)1100 void StatusEventHandler(input)
1101    XEvent *input;
1102 {
1103    if (PRTGIF || noStatusWindow) return;
1104 
1105    if (input->type == Expose) {
1106       RedrawStatusWindow();
1107    }
1108 }
1109 
RedrawUserRedrawWindow()1110 void RedrawUserRedrawWindow()
1111 {
1112    XEvent ev;
1113    int x, y;
1114    XGCValues values;
1115 
1116    while (XCheckWindowEvent(mainDisplay,userRedrawWindow,ExposureMask,&ev)) ;
1117    while (XCheckWindowEvent(mainDisplay,userRedrawWindow,ButtonPressMask,&ev)) ;
1118 
1119    x = ((userRedrawWindowW-redraw_width)>>1);
1120    y = ((userRedrawWindowH-redraw_height)>>1);
1121    XClearWindow(mainDisplay, userRedrawWindow);
1122    values.stipple = userRedrawBitmap;
1123    values.ts_x_origin = x;
1124    values.ts_y_origin = y;
1125    XChangeGC(mainDisplay, rasterGC,
1126          GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, &values);
1127    XFillRectangle(mainDisplay, userRedrawWindow, rasterGC, x, y,
1128          redraw_width, redraw_height);
1129    XSetTSOrigin(mainDisplay, rasterGC, 0, 0);
1130    if (userDisableRedraw) {
1131       GC gc;
1132       int shift=windowPadding;
1133 
1134       values.foreground = myFgPixel;
1135       values.background = (threeDLook ? myLtGryPixel : myBgPixel);
1136       gc = XCreateGC(mainDisplay, userRedrawWindow, GCForeground | GCBackground,
1137             &values);
1138       XDrawLine(mainDisplay, userRedrawWindow, gc, shift,
1139             userRedrawWindowH-1-shift, userRedrawWindowW-1-shift, shift);
1140       XFreeGC(mainDisplay, gc);
1141    }
1142    if (threeDLook) {
1143       struct BBRec bbox;
1144 
1145       SetBBRec(&bbox, 0, 0, userRedrawWindowW, userRedrawWindowH);
1146       TgDrawThreeDButton(mainDisplay, userRedrawWindow, textMenuGC, &bbox,
1147             (userDisableRedraw ? TGBS_LOWRED : TGBS_RAISED), 2, TRUE);
1148    }
1149 }
1150 
1151 static
ToggleUserRedraw()1152 void ToggleUserRedraw()
1153 {
1154    userDisableRedraw = !userDisableRedraw;
1155    RedrawUserRedrawWindow();
1156    if (!userDisableRedraw) {
1157       ClearAndRedrawDrawWindow();
1158    }
1159 }
1160 
UserRedrawEventHandler(input)1161 void UserRedrawEventHandler(input)
1162    XEvent *input;
1163 {
1164    if (PRTGIF || noStatusWindow) return;
1165 
1166    if (input->type == Expose) {
1167       RedrawUserRedrawWindow();
1168    } else if (input->type == EnterNotify) {
1169       SetMouseStatus(TgLoadCachedString(userDisableRedraw ?
1170             CSTID_ENABLE_REDRAW : CSTID_DISABLE_REDRAW),
1171             TgLoadCachedString(CSTID_PARANED_NONE),
1172             TgLoadCachedString(CSTID_PARANED_NONE));
1173    } else if (input->type == ButtonPress) {
1174       ToggleUserRedraw();
1175       SetMouseStatus(TgLoadCachedString(userDisableRedraw ?
1176             CSTID_ENABLE_REDRAW : CSTID_DISABLE_REDRAW),
1177             TgLoadCachedString(CSTID_PARANED_NONE),
1178             TgLoadCachedString(CSTID_PARANED_NONE));
1179    }
1180 }
1181 
InitStatus()1182 void InitStatus()
1183 {
1184    register int i;
1185    char *c_ptr;
1186 
1187    oneLineStatus = FALSE;
1188    *oneLineStatusStr = '\0';
1189    for (i=0; i < MAX_STATUS_BTNS; i++) *btnStatusStr[i] = '\0';
1190 
1191    reverseMouseStatusButtons = FALSE;
1192    if ((c_ptr=XGetDefault(mainDisplay, TOOL_NAME,
1193          "ReverseMouseStatusButtons")) != NULL &&
1194          UtilStrICmp(c_ptr, "true") == 0) {
1195       reverseMouseStatusButtons = TRUE;
1196    }
1197    gnMsgRows = (int)((msgWindowH-2-(windowPadding<<1)) /
1198          ((msgFontSet==NULL && msgFontPtr==NULL) ? defaultFontHeight :
1199          msgFontHeight));
1200 }
1201 
CleanUpStatus()1202 void CleanUpStatus()
1203 {
1204    while (topStatusInfo != NULL) {
1205       MouseOverStatusInfo *next_mosi=topStatusInfo->next;
1206 
1207       free(topStatusInfo);
1208       topStatusInfo = next_mosi;
1209    }
1210    if (redraw_bits == NULL) { }
1211    if (btn1_bits == NULL) { }
1212 }
1213 
1214 static char	*scanVal=NULL;
1215 static char	*scanSep=NULL;
1216 
InitScan(s,pat)1217 void InitScan(s, pat)
1218    char *s, *pat;
1219 {
1220    scanVal = s;
1221    scanSep = pat;
1222 }
1223 
1224 static
GetString()1225 char *GetString()
1226 {
1227    char *c_ptr;
1228 
1229    if (scanVal == NULL || scanSep == NULL) return NULL;
1230    while (*scanVal!='\0' && strchr(scanSep,*scanVal)!=NULL) *scanVal++ = '\0';
1231    if (*scanVal == '\0') return NULL;
1232 
1233    c_ptr=scanVal;
1234    for ( ; *scanVal!='\0' && strchr(scanSep,*scanVal)==NULL; scanVal++) ;
1235    if (*scanVal != '\0') *scanVal++ = '\0';
1236 
1237    return c_ptr;
1238 }
1239 
ScanValue(fmt,v,item,stype)1240 int ScanValue(fmt, v, item, stype)
1241    char *fmt, *item, *stype;
1242    void *v;
1243 {
1244    char *c_ptr, msg[MAXPATHLENGTH];
1245 
1246    if (scanVal == NULL) return INVALID;
1247 #ifdef DEBUG /* debug, do not translate */
1248    printf("get %s for %s from %s:  ", item, stype, scanVal);
1249 #endif
1250 
1251    if ((c_ptr=GetString()) == NULL) {
1252 #ifdef DEBUG /* debug, do not translate */
1253       printf("nothing\n");
1254 #endif
1255       (void)sprintf(msg, TgLoadString(STID_FILE_LINE_MISS_FIELD_IN_OBJ),
1256             scanFileName, scanLineNum, item, stype);
1257       if (PRTGIF) {
1258          fprintf(stderr, "%s\n", msg);
1259       } else {
1260          Msg(msg);
1261       }
1262       return INVALID;
1263    }
1264    if (sscanf(c_ptr, fmt, v) != 1) {
1265 #ifdef DEBUG /* debug, do not translate */
1266       printf("error in %s\n", c_ptr);
1267 #endif
1268       (void)sprintf(msg, TgLoadString(STID_FILE_LINE_BAD_FIELD_IN_OBJ),
1269             scanFileName, scanLineNum, item, stype, c_ptr);
1270       if (PRTGIF) {
1271          fprintf(stderr, "%s\n", msg);
1272       } else {
1273          Msg(msg);
1274       }
1275       return INVALID;
1276    }
1277 
1278 #ifdef DEBUG /* debug, do not translate */
1279    printf("got %d\n", (*(int *)v));
1280 #endif
1281 
1282    return 0;
1283 }
1284 
ScanDynStrValue(v,item,stype)1285 int ScanDynStrValue(v, item, stype)
1286    struct DynStrRec *v;
1287    char *item, *stype;
1288    /*
1289     *
1290     */
1291 {
1292    char *c_ptr=NULL, *psz=NULL, msg[MAXPATHLENGTH];
1293 
1294    if (scanVal == NULL) return INVALID;
1295 #ifdef DEBUG /* debug, do not translate */
1296    printf("get %s for %s from %s:  ", item, stype, scanVal);
1297 #endif
1298 
1299    if ((c_ptr=GetString()) == NULL) {
1300 #ifdef DEBUG /* debug, do not translate */
1301       printf("nothing\n");
1302 #endif
1303       (void)sprintf(msg, TgLoadString(STID_FILE_LINE_MISS_FIELD_IN_OBJ),
1304             scanFileName, scanLineNum, item, stype);
1305       if (PRTGIF) {
1306          fprintf(stderr, "%s\n", msg);
1307       } else {
1308          Msg(msg);
1309       }
1310       return INVALID;
1311    }
1312    while (*c_ptr != '"' && *c_ptr != '\0') c_ptr++;
1313    if (c_ptr != NULL) {
1314       psz = ReadString(++c_ptr);
1315    }
1316    if (psz == NULL) {
1317 #ifdef DEBUG /* debug, do not translate */
1318       printf("error in %s\n", c_ptr);
1319 #endif
1320       (void)sprintf(msg, TgLoadString(STID_FILE_LINE_BAD_FIELD_IN_OBJ),
1321             scanFileName, scanLineNum, item, stype, c_ptr);
1322       if (PRTGIF) {
1323          fprintf(stderr, "%s\n", msg);
1324       } else {
1325          Msg(msg);
1326       }
1327       return INVALID;
1328    }
1329    *(--psz) = '\0';
1330    DynStrSet(v, c_ptr);
1331 
1332 #ifdef DEBUG /* debug, do not translate */
1333    printf("got \"%s\"\n", ((struct DynStrRec *)v)->s);
1334 #endif
1335 
1336    return 0;
1337 }
1338 
1339 #define MAXEMERGENCYCOUNT 5
1340 
1341 static int	emergencyCount = 0;
1342 
1343 static
SaveEmergencyTmpFile()1344 void SaveEmergencyTmpFile()
1345 {
1346    switch (SaveTmpFile("EmergencySave")) {
1347    case OBJ_FILE_TYPE:
1348       fprintf(stderr, TgLoadString(STID_WORKING_FILE_SAVED_TO),
1349             "EmergencySave", OBJ_FILE_EXT);
1350       fprintf(stderr, "\n");
1351       break;
1352    case SYM_FILE_TYPE:
1353       fprintf(stderr, TgLoadString(STID_WORKING_FILE_SAVED_TO),
1354             "EmergencySave", SYM_FILE_EXT);
1355       fprintf(stderr, "\n");
1356       break;
1357    case PIN_FILE_TYPE:
1358       fprintf(stderr, TgLoadString(STID_WORKING_FILE_SAVED_TO),
1359             "EmergencySave", PIN_FILE_EXT);
1360       fprintf(stderr, "\n");
1361       break;
1362    case INVALID:
1363       fprintf(stderr, "%s\n",
1364             TgLoadString(STID_UNABLE_TO_SAVE_WORKING_FILE));
1365       break;
1366    }
1367 }
1368 
EmergencySave(sig)1369 void EmergencySave(sig)
1370    int sig;
1371 {
1372    switch (sig) {
1373    case SIGHUP:
1374       fprintf(stderr, TgLoadString(STID_NAMED_SIG_RECV), "SIGHUP");
1375       fprintf(stderr, "\n");
1376       break;
1377    case SIGFPE:
1378       fprintf(stderr, TgLoadString(STID_NAMED_SIG_RECV), "SIGFPE");
1379       fprintf(stderr, "\n");
1380       break;
1381 #ifndef linux
1382    case SIGBUS:
1383       fprintf(stderr, TgLoadString(STID_NAMED_SIG_RECV), "SIGBUS");
1384       fprintf(stderr, "\n");
1385       break;
1386 #endif
1387    case SIGSEGV:
1388       fprintf(stderr, TgLoadString(STID_NAMED_SIG_RECV), "SIGSEGV");
1389       fprintf(stderr, "\n");
1390       break;
1391    }
1392    if (++emergencyCount > MAXEMERGENCYCOUNT) {
1393       fprintf(stderr, TgLoadString(STID_ERR_COUNT_EXCEED_TOOL_ABORTED),
1394             MAXEMERGENCYCOUNT, "EmergencySave()", TOOL_NAME);
1395       fprintf(stderr, "\n");
1396       exit(-1);
1397    }
1398    if (exitNormally) return;
1399 
1400    signal(SIGHUP, SIG_DFL);
1401    signal(SIGFPE, SIG_DFL);
1402 #ifndef linux
1403    signal(SIGBUS, SIG_DFL);
1404 #endif
1405    signal(SIGSEGV, SIG_DFL);
1406 
1407    if (fileModified) {
1408       SaveEmergencyTmpFile();
1409    }
1410    exitNormally = TRUE;
1411    exit(0);
1412 }
1413 
1414 static
EmergencySaveForXCont(s)1415 int EmergencySaveForXCont(s)
1416    char *s;
1417 {
1418    if (++emergencyCount > MAXEMERGENCYCOUNT) {
1419       fprintf(stderr, TgLoadString(STID_ERR_COUNT_EXCEED_TOOL_ABORTED),
1420             MAXEMERGENCYCOUNT, s, TOOL_NAME);
1421       fprintf(stderr, "\n");
1422       exit(-1);
1423    }
1424    if (exitNormally) return 0;
1425 
1426    if (copyingToCutBuffer) {
1427       emergencyCount--;
1428       copyingToCutBuffer = INVALID;
1429       return 0;
1430    }
1431    signal(SIGHUP, SIG_DFL);
1432    signal(SIGFPE, SIG_DFL);
1433 #ifndef linux
1434    signal(SIGBUS, SIG_DFL);
1435 #endif
1436    signal(SIGSEGV, SIG_DFL);
1437 
1438    if (fileModified) {
1439       SaveEmergencyTmpFile();
1440    }
1441    exitNormally = TRUE;
1442    return (-1);
1443 }
1444 
EmergencySaveForX(dsp,ev)1445 int EmergencySaveForX(dsp, ev)
1446    Display *dsp;
1447    XErrorEvent *ev;
1448 {
1449    char msg[MAXSTRING+1];
1450 
1451    XGetErrorText(mainDisplay, (int)(ev->error_code), msg, MAXSTRING);
1452    fprintf(stderr, TgLoadString(STID_X_ERROR_MSG), msg);
1453    fprintf(stderr, "\n");
1454 
1455    return EmergencySaveForXCont("EmergencySaveForX()");
1456 }
1457 
IOEmergencySaveForX(dsp)1458 int IOEmergencySaveForX(dsp)
1459    Display *dsp;
1460 {
1461    return EmergencySaveForXCont("IOEmergencySaveForX()");
1462 }
1463 
Error(where,msg)1464 void Error(where, msg)
1465    char *where, *msg;
1466 {
1467    fprintf(stderr, TgLoadString(STID_FATAL_ERROR_IN_FUNC_PLUS_DESC),
1468          where, msg);
1469    fprintf(stderr, "\n");
1470    fprintf(stderr, TgLoadString(STID_TOOL_ABORTED), TOOL_NAME);
1471    fprintf(stderr, "\n");
1472    if (fileModified) EmergencySave(0);
1473    exit(-1);
1474 }
1475 
GetTgifVersionAndPatchLevel(buf,buf_sz)1476 void GetTgifVersionAndPatchLevel(buf, buf_sz)
1477    char *buf;
1478    int buf_sz;
1479 {
1480    char buf1[MAXSTRING], buf2[MAXSTRING];
1481 
1482    *buf1 = *buf2 = '\0';
1483    if (TGIF_PATCHLEVEL == 0) {
1484       if (*specialBuild == '\0') {
1485          sprintf(buf1, TgLoadString(STID_TOOL_VERSION),
1486                TOOL_NAME, versionString);
1487       } else {
1488          sprintf(buf1, TgLoadString(STID_TOOL_VERSION_SPC_BUILD),
1489                TOOL_NAME, versionString, specialBuild);
1490       }
1491       strcpy(buf2, versionString);
1492    } else {
1493       if (*specialBuild == '\0') {
1494          sprintf(buf1, TgLoadString(STID_TOOL_VERSION_PATCH),
1495                TOOL_NAME, versionString, TGIF_PATCHLEVEL);
1496       } else {
1497          sprintf(buf1, TgLoadString(STID_TOOL_VERSION_PATCH_SPC_BUILD),
1498                TOOL_NAME, versionString, TGIF_PATCHLEVEL, specialBuild);
1499       }
1500       sprintf(buf2, "%s.%1d", versionString, TGIF_PATCHLEVEL);
1501    }
1502 #ifdef TGIF_DISTR_VER
1503    if (strcmp(TGIF_DISTR_VER, buf2) != 0) {
1504       sprintf(&buf1[strlen(buf1)], TgLoadString(STID_TGIF_DISTRUBITION),
1505             TGIF_DISTR_VER);
1506    }
1507 #endif /* TGIF_DISTR_VER */
1508 #ifdef NOT_DEFINED
1509    sprintf(&buf1[strlen(buf1)], TgLoadString(STID_TGIF_FLAGS), "_TGIF_WB2");
1510 #endif /* NOT_DEFINED */
1511    UtilStrCpyN(buf, buf_sz, buf1);
1512 }
1513 
Usage(tool_name)1514 void Usage(tool_name)
1515    char *tool_name;
1516 {
1517    char buf[MAXSTRING];
1518 
1519    GetTgifVersionAndPatchLevel(buf, sizeof(buf));
1520    fprintf(stdout, "%s\n", buf);
1521 
1522    fprintf(stdout, "%s\n\n", copyrightString);
1523    fprintf(stdout, "Usage:\t%s \\\n", tool_name);
1524    fprintf(stdout, "\t[-display displayname] \\\n");
1525    fprintf(stdout, "\t[-fg <color>] \\\n");
1526    fprintf(stdout, "\t[-bg <color>] \\\n");
1527    fprintf(stdout, "\t[-bd <color>] \\\n");
1528    fprintf(stdout, "\t[-rv] [-nv] \\\n");
1529    fprintf(stdout, "\t[-bw] \\\n");
1530    fprintf(stdout, "\t[-tgwb2 [-rmcastlibdir <directory> | -rmcastlibpath <path>] [-parent <parent_window_id>]] \\\n");
1531    fprintf(stdout, "\t[-reqcolor] \\\n");
1532    fprintf(stdout, "\t[-cwo[+sbwarp]] \\\n");
1533    fprintf(stdout, "\t[-hyper] \\\n");
1534    fprintf(stdout, "\t[-a4 | -letter] \\\n");
1535    fprintf(stdout, "\t[-geometry <geom>] [=<geom>] \\\n");
1536    fprintf(stdout, "\t[-exec <file>] \\\n");
1537    fprintf(stdout, "\t[-dbim {xcin|chinput|xim|kinput2}] \\\n");
1538    fprintf(stdout, "\t[-sbim xim] \\\n");
1539    fprintf(stdout, "\t[-usexlib] \\\n");
1540    fprintf(stdout, "\t[-listdontreencode] \\\n");
1541    fprintf(stdout, "\t[-version | --version] \\\n");
1542    fprintf(stdout, "\t[-pdfspd | -pdfspd=true | -pdfspd=false] \\\n");
1543    fprintf(stdout, "\t[-pssetup \"<string>\"] \\\n");
1544    fprintf(stdout, "\t[-nomode] \\\n");
1545    fprintf(stdout, "\t[-tg2genparse=\"<string>\"] \\\n");
1546    fprintf(stdout, "\t[{<file>[.%s] | -merge <file1>[.%s] <file2>[.%s] ...}]\n",
1547          OBJ_FILE_EXT, OBJ_FILE_EXT, OBJ_FILE_EXT);
1548    fprintf(stdout, "or:\n");
1549    fprintf(stdout, "\t%s -print \\\n", tool_name);
1550    fprintf(stdout, "\t[-display displayname] \\\n");
1551    fprintf(stdout, "\t[-eps | -p | -epsi | -tiffepsi] \\\n");
1552    fprintf(stdout, "\t[-ps | -f] \\\n");
1553    fprintf(stdout, "\t[-text] \\\n");
1554    fprintf(stdout, "\t[-gif | -png | -jpeg | -ppm | -pbm | -xpm | -xbm | -html] \\\n");
1555    fprintf(stdout, "\t[-pdf] \\\n");
1556    fprintf(stdout, "\t[-netlist] \\\n");
1557    fprintf(stdout, "\t[-stdout] \\\n");
1558    fprintf(stdout, "\t[-raw[+h[eaderonly]]] \\\n");
1559    fprintf(stdout, "\t[-dosepsfilter [-previewonly]] \\\n");
1560    fprintf(stdout, "\t[-status] \\\n");
1561    fprintf(stdout, "\t[-gray] \\\n");
1562    fprintf(stdout, "\t[-color | -reqcolor] \\\n");
1563    fprintf(stdout, "\t[-adobe | -adobe=<number>/<number>] \\\n");
1564    fprintf(stdout,
1565          "\t[-dontreencode=\"<%s>:<%s>:...\" | -listdontreencode] \\\n",
1566          "fontfamily", "fontfamily");
1567    fprintf(stdout, "\t[-version | --version | -justversion] \\\n");
1568    fprintf(stdout, "\t[-producedby=\"<string>\"] \\\n");
1569    fprintf(stdout, "\t[-page <number>] \\\n");
1570    fprintf(stdout, "\t[-print_cmd \"<command>\"] \\\n");
1571    fprintf(stdout, "\t[-one_file_per_page] \\\n");
1572    fprintf(stdout, "\t[-pepsc] \\\n");
1573    fprintf(stdout, "\t[-pdfspd | -pdfspd=true | -pdfspd=false] \\\n");
1574    fprintf(stdout, "\t[-pssetup \"<string>\"] \\\n");
1575    fprintf(stdout, "\t[-j2p6_cmd \"<command>\"] \\\n");
1576    fprintf(stdout, "\t[-dontcondense | -condensed] \\\n");
1577    fprintf(stdout, "\t[-a4 | -letter] \\\n");
1578    fprintf(stdout, "\t[-noshowpageineps] \\\n");
1579    fprintf(stdout, "\t[-quiet] \\\n");
1580    fprintf(stdout, "\t[-bop_hook \"<string>\"] \\\n");
1581    fprintf(stdout, "\t[-eop_hook \"<string>\"] \\\n");
1582    fprintf(stdout, "\t[-tmp_file_mode \"<octal number>\"] \\\n");
1583    fprintf(stdout, "\t[-odir] \\\n");
1584    fprintf(stdout, "\t[-exec <file>] \\\n");
1585    fprintf(stdout, "\t[<file1>[.%s] <file2>[.%s] ...]\n",
1586          OBJ_FILE_EXT, OBJ_FILE_EXT);
1587    exit(-1);
1588 }
1589 
ProcessPrTgifOptions(argc,argv,from_prtgif)1590 int ProcessPrTgifOptions(argc, argv, from_prtgif)
1591    int argc, from_prtgif;
1592    char *argv[];
1593 {
1594    char *c_ptr=NULL;
1595    int saved_argc=argc, show_copyright=FALSE;
1596    char **saved_argv=argv;
1597 
1598    *cmdLineTmpFileModeStr = '\0';
1599    *cmdLineGenParser = '\0';
1600    *cmdLineFileToExec = '\0';
1601    *cmdLineJpegToPpm6Cmd = '\0';
1602    *cmdLineCustomPatternDir = '\0';
1603    cmdLineOneFilePerPage = FALSE;
1604    *outputDir = '\0';
1605    for (argc--, argv++; argc > 0; argc--, argv++) {
1606       if (**argv == '-') {
1607          if (strcmp(*argv, "-version") == 0 ||
1608                strcmp(*argv, "--version") == 0) {
1609             show_copyright = TRUE;
1610          } else if (strcmp(*argv, "-tgrm2") == 0) {
1611             cmdLineTgrm2 = TRUE;
1612          } else if (strcmp(*argv, "-justversion") == 0) {
1613             char buf[MAXSTRING];
1614 
1615             GetTgifVersionAndPatchLevel(buf, sizeof(buf));
1616             fprintf(stdout, "%s\n", buf);
1617             fprintf(stdout, "%s (%s)\n\n", copyrightString, authorEmailString);
1618             fflush(stdout);
1619 
1620             return INVALID;
1621          }
1622       }
1623    }
1624    argc = saved_argc;
1625    argv = saved_argv;
1626    if (show_copyright) {
1627       char buf[MAXSTRING];
1628 
1629       GetTgifVersionAndPatchLevel(buf, sizeof(buf));
1630       fprintf(stderr, "%s\n", buf);
1631       fprintf(stderr, "%s (%s)\n\n", copyrightString, authorEmailString);
1632       fflush(stderr);
1633    }
1634    if (!from_prtgif) {
1635       if ((c_ptr=getenv("TGIFPATH")) == NULL) {
1636          strcpy(drawPath, TGIF_PATH);
1637       } else {
1638          if (((int)strlen(c_ptr)) >= MAXSTRING) {
1639             /* must be an error */
1640             strcpy(drawPath, TGIF_PATH);
1641          } else {
1642             strcpy(drawPath, c_ptr);
1643          }
1644       }
1645 #ifdef PRINT_CMD
1646       strcpy(printCommand, PRINT_CMD);
1647 #else
1648 #ifdef VMS
1649       strcpy(printCommand, "print");
1650 #else
1651 #ifdef SYSV
1652       strcpy(printCommand, "lp -dpostscript");
1653 #else
1654       strcpy(printCommand, "lpr");
1655 #endif /* SYSV */
1656 #endif /* VMS */
1657 #endif /* PRINT_CMD */
1658    }
1659    cmdLineWhereToPrint = INVALID;
1660    whereToPrint = PRINTER;
1661 
1662    usePsAdobeString = FALSE;
1663    *adobeString = *epsfString = '\0';
1664    *cmdLineDontReencode = *cmdLineProducedBy = '\0';
1665    *cmdLineOpenFile = '\0';
1666 #ifdef _USE_PS_ADOBE_STRING
1667    {
1668       char buf[MAXSTRING];
1669 
1670       UtilStrCpyN(buf, sizeof(buf), _USE_PS_ADOBE_STRING);
1671       if (!ParsePsAdobeString(buf,&usePsAdobeString,adobeString,epsfString)) {
1672          fprintf(stderr, TgLoadString(STID_BAD_COMPILE_OPT_USE_ALT_STR),
1673                "_USE_PS_ADOBE_STRING", _USE_PS_ADOBE_STRING, "false");
1674          fprintf(stderr, "\n");
1675          usePsAdobeString = FALSE;
1676          *adobeString = *epsfString = '\0';
1677       }
1678    }
1679 #endif /* _USE_PS_ADOBE_STRING */
1680    c_ptr = printCommand;
1681    c_ptr += strlen(printCommand);
1682    for (argc--, argv++; argc > 0; argc--, argv++) {
1683       if (**argv == '-') {
1684          int len=0;
1685 
1686          if (strcmp(*argv, "-display") == 0) {
1687             argc--; argv++;
1688             if (argc <= 0) {
1689                fprintf(stderr, "%s\n\n",
1690                      TgLoadString(STID_MISSING_DISPLAY_NAME));
1691                Usage(progName);
1692             }
1693             if ((displayName=UtilStrDup(*argv)) == NULL) FailAllocMessage();
1694             cmdLineOpenDisplay = TRUE;
1695          } else if (strcmp(*argv, "-p") == 0 || strcmp(*argv, "-eps") == 0) {
1696             whereToPrint = cmdLineWhereToPrint = LATEX_FIG;
1697          } else if (strcmp(*argv,"-f") == 0 || strcmp(*argv,"-ps") == 0) {
1698             whereToPrint = cmdLineWhereToPrint = PS_FILE;
1699          } else if (strcmp(*argv,"-stdout") == 0) {
1700             cmdLineStdOut = TRUE;
1701          } else if (strcmp(*argv,"-epsi") == 0) {
1702             cmdLineWhereToPrint = EPSI_FILE;
1703             cmdLineTiffEPSI = FALSE;
1704             cmdLineOpenDisplay = TRUE;
1705          } else if (strcmp(*argv,"-tiffepsi") == 0) {
1706             cmdLineWhereToPrint = EPSI_FILE;
1707             cmdLineTiffEPSI = TRUE;
1708             cmdLineOpenDisplay = TRUE;
1709          } else if (strcmp(*argv,"-gif") == 0) {
1710             cmdLineWhereToPrint = GIF_FILE;
1711             cmdLineColor = TRUE;
1712             cmdLineOpenDisplay = TRUE;
1713          } else if (strcmp(*argv,"-png") == 0) {
1714             cmdLineWhereToPrint = PNG_FILE;
1715             cmdLineColor = TRUE;
1716             cmdLineOpenDisplay = TRUE;
1717          } else if (strcmp(*argv,"-jpeg") == 0) {
1718             cmdLineWhereToPrint = JPEG_FILE;
1719             cmdLineColor = TRUE;
1720             cmdLineOpenDisplay = TRUE;
1721          } else if (strcmp(*argv,"-ppm") == 0) {
1722             cmdLineWhereToPrint = PPM_FILE;
1723             cmdLineColor = TRUE;
1724             cmdLineOpenDisplay = TRUE;
1725          } else if (strcmp(*argv,"-pbm") == 0) {
1726             cmdLineWhereToPrint = PPM_FILE;
1727             cmdLineColor = FALSE;
1728             cmdLineOpenDisplay = TRUE;
1729          } else if (strcmp(*argv,"-xpm") == 0) {
1730             cmdLineWhereToPrint = XBM_FILE;
1731             cmdLineColor = TRUE;
1732             cmdLineOpenDisplay = TRUE;
1733          } else if (strcmp(*argv,"-xbm") == 0) {
1734             cmdLineWhereToPrint = XBM_FILE;
1735             cmdLineColor = FALSE;
1736             cmdLineOpenDisplay = TRUE;
1737          } else if (strcmp(*argv,"-html") == 0) {
1738             cmdLineWhereToPrint = HTML_FILE;
1739             cmdLineColor = TRUE;
1740             cmdLineOpenDisplay = TRUE;
1741          } else if (strcmp(*argv,"-pdf") == 0) {
1742             whereToPrint = cmdLineWhereToPrint = PDF_FILE;
1743          } else if (strcmp(*argv, "-text") == 0) {
1744             whereToPrint = cmdLineWhereToPrint = TEXT_FILE;
1745          } else if (strcmp(*argv, "-netlist") == 0) {
1746             whereToPrint = cmdLineWhereToPrint = NETLIST_FILE;
1747          } else if (strcmp(*argv,"-svg") == 0) {
1748             whereToPrint = cmdLineWhereToPrint = SVG_FILE;
1749          } else if (strcmp(*argv, "-raw") == 0) {
1750             cmdLineDumpURL = TRUE;
1751             cmdLineDumpURLWithHeader = FALSE;
1752             cmdLineDumpURLHeaderOnly = FALSE;
1753          } else if (strcmp(*argv, "-raw+h") == 0) {
1754             cmdLineDumpURL = TRUE;
1755             cmdLineDumpURLWithHeader = TRUE;
1756             cmdLineDumpURLHeaderOnly = FALSE;
1757          } else if (strcmp(*argv, "-raw+headeronly") == 0) {
1758             cmdLineDumpURL = TRUE;
1759             cmdLineDumpURLWithHeader = TRUE;
1760             cmdLineDumpURLHeaderOnly = TRUE;
1761          } else if (strcmp(*argv, "-dosepsfilter") == 0) {
1762             cmdLineDosEpsFilter = TRUE;
1763          } else if (strcmp(*argv, "-previewonly") == 0) {
1764             cmdLinePreviewOnly = TRUE;
1765          } else if (strcmp(*argv, "-status") == 0) {
1766             cmdLineDumpURLShowStatus = TRUE;
1767          } else if (strcmp(*argv, "-page") == 0) {
1768             argc--; argv++;
1769             if (argc <= 0) {
1770                fprintf(stderr, "%s\n\n",
1771                      TgLoadString(STID_MISSING_PAGE_NUMBER));
1772                Usage(progName);
1773             }
1774             cmdLineHasPageNum = TRUE;
1775             UtilStrCpyN(cmdLinePageNumStr, sizeof(cmdLinePageNumStr), *argv);
1776             if (sscanf(cmdLinePageNumStr, "%d", &cmdLinePageNum) != 1 ||
1777                   cmdLinePageNum <= 0) {
1778                cmdLineHasPageNum = FALSE;
1779                fprintf(stderr, TgLoadString(STID_INVALID_GIVEN_PAGE_NUMBER_STR),
1780                      cmdLinePageNumStr);
1781                fprintf(stderr, "\n\n");
1782                Usage(progName);
1783             }
1784          } else if (strcmp(*argv, "-one_file_per_page") == 0) {
1785             cmdLineOneFilePerPage = TRUE;
1786          } else if (strcmp(*argv, "-pepsc") == 0) {
1787             stripEPSComments = FALSE;
1788          } else if (strcmp(*argv, "-nolandpdfspd") == 0) {
1789             cmdLinePdfSetPageDevice = FALSE;
1790             fprintf(stderr, TgLoadString(STID_OBSOLETE_CMDLINE_ASSUME),
1791                   "-nolandpdfspd", "-pdfspd=false");
1792             fprintf(stderr, "\n");
1793          } else if (strcmp(*argv, "-pdfspd") == 0) {
1794             cmdLinePdfSetPageDevice = TRUE;
1795          } else if (strncmp(*argv, "-pdfspd=", 8) == 0) {
1796             char tmp_str[80];
1797 
1798             UtilStrCpyN(tmp_str, sizeof(tmp_str), &(*argv)[8]);
1799             UtilTrimBlanks(tmp_str);
1800             if (UtilStrICmp(tmp_str, "true") == 0) {
1801                cmdLinePdfSetPageDevice = TRUE;
1802             } else if (UtilStrICmp(tmp_str, "false") == 0) {
1803                cmdLinePdfSetPageDevice = FALSE;
1804             } else {
1805                fprintf(stderr, TgLoadString(STID_BAD_CMDLINE_OPT_IGNORED),
1806                      "-pdfspd", tmp_str);
1807                fprintf(stderr, "\n");
1808             }
1809          } else if (strcmp(*argv, "-pssetup") == 0) {
1810             argc--; argv++;
1811             if (argc <= 0) {
1812                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1813                      "-pssetup");
1814                fprintf(stderr, "\n\n");
1815                Usage(progName);
1816             }
1817             cmdLinePsSetup = TRUE;
1818             SetPsSetup(*argv);
1819          } else if (strcmp(*argv, "-dontcondense") == 0) {
1820             SetCmdLineDontCondense();
1821          } else if (strcmp(*argv, "-condensed") == 0) {
1822             SetCmdLineCondensedPS();
1823          } else if (strcmp(*argv, "-a4") == 0) {
1824             cmdLineA4 = TRUE;
1825          } else if (strcmp(*argv, "-letter") == 0) {
1826             cmdLineLetter = TRUE;
1827          } else if (strcmp(*argv, "-noshowpageineps") == 0) {
1828             cmdLineNoShowPageInEPS = TRUE;
1829          } else if (strcmp(*argv, "-quiet") == 0) {
1830             cmdLineQuiet = TRUE;
1831          } else if (strcmp(*argv, "-bop_hook") == 0) {
1832             argc--; argv++;
1833             if (argc <= 0) {
1834                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1835                      "-bop_hook");
1836                fprintf(stderr, "\n\n");
1837                Usage(progName);
1838             }
1839             SetBopHook(*argv);
1840          } else if (strcmp(*argv, "-eop_hook") == 0) {
1841             argc--; argv++;
1842             if (argc <= 0) {
1843                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1844                      "-eop_hook");
1845                fprintf(stderr, "\n\n");
1846                Usage(progName);
1847             }
1848             SetEopHook(*argv);
1849          } else if (strcmp(*argv, "-tmp_file_mode") == 0) {
1850             int nFileMode=0;
1851 
1852             argc--; argv++;
1853             if (argc <= 0) {
1854                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1855                      "-tmp_file_mode");
1856                fprintf(stderr, "\n\n");
1857                Usage(progName);
1858             }
1859             UtilStrCpyN(cmdLineTmpFileModeStr, sizeof(cmdLineTmpFileModeStr),
1860                   *argv);
1861             UtilTrimBlanks(cmdLineTmpFileModeStr);
1862             if (*cmdLineTmpFileModeStr != '\0' && sscanf(cmdLineTmpFileModeStr,
1863                   "%o", &nFileMode) == 1) {
1864                if (nFileMode >= 0) {
1865                   tmpFileMode = nFileMode;
1866                } else {
1867                   fprintf(stderr,
1868                         TgLoadString(STID_INVALID_ARG_FOR_CMDLINE_OPT),
1869                         "-tmp_file_mode", cmdLineTmpFileModeStr);
1870                   fprintf(stderr, "\n\n");
1871                   Usage(progName);
1872                }
1873             }
1874          } else if (strcmp(*argv, "-gray") == 0) {
1875             useGray = TRUE;
1876          } else if (strcmp(*argv, "-bw") == 0) {
1877          } else if (strcmp(*argv, "-color") == 0) {
1878             cmdLineRequestedColor = FALSE;
1879             cmdLineColor = TRUE;
1880          } else if (strcmp(*argv, "-reqcolor") == 0) {
1881             cmdLineRequestedColor = TRUE;
1882             cmdLineColor = TRUE;
1883          } else if (strcmp(*argv, "-adobe") == 0) {
1884             usePsAdobeString = TRUE;
1885          } else if (strncmp(*argv, "-adobe=", 7) == 0) {
1886             char tmp_str[80];
1887 
1888             UtilStrCpyN(tmp_str, sizeof(tmp_str), &(*argv)[7]);
1889             UtilTrimBlanks(tmp_str);
1890             if (!ParsePsAdobeString(tmp_str, &usePsAdobeString, adobeString,
1891                   epsfString)) {
1892                fprintf(stderr, TgLoadString(STID_BAD_CMDLINE_OPT_USE_ALT_STR),
1893                      "-adobe", tmp_str, "false");
1894                fprintf(stderr, "\n");
1895                usePsAdobeString = FALSE;
1896                *adobeString = *epsfString = '\0';
1897             }
1898          } else if (strcmp(*argv, "-listdontreencode") == 0) {
1899 #ifdef _DONT_REENCODE
1900             fprintf(stderr, "-D_DONT_REENCODE=\"%s\"\n", _DONT_REENCODE);
1901 #endif /* _DONT_REENCODE */
1902          } else if (strcmp(*argv, "-version") == 0 ||
1903                strcmp(*argv, "--version") == 0) {
1904             cmdLineVersion = TRUE;
1905          } else if (strncmp(*argv, "-dontreencode=", 14) == 0) {
1906             strcpy(cmdLineDontReencode, &(*argv)[14]);
1907             UtilTrimBlanks(cmdLineDontReencode);
1908             if (*cmdLineDontReencode != '\0') {
1909                UtilRemoveQuotes(cmdLineDontReencode);
1910                UtilTrimBlanks(cmdLineDontReencode);
1911             }
1912          } else if (strncmp(*argv, "-producedby=", 12) == 0) {
1913             UtilStrCpyN(cmdLineProducedBy, sizeof(cmdLineProducedBy),
1914                   &(*argv)[12]);
1915             UtilTrimBlanks(cmdLineProducedBy);
1916             if (*cmdLineProducedBy != '\0') {
1917                UtilRemoveQuotes(cmdLineProducedBy);
1918             }
1919          } else if (strncmp(*argv, "-tg2genparser=", 14) == 0) {
1920             UtilStrCpyN(cmdLineGenParser, sizeof(cmdLineGenParser),
1921                   &(*argv)[14]);
1922             UtilTrimBlanks(cmdLineGenParser);
1923             if (*cmdLineGenParser != '\0') {
1924                UtilRemoveQuotes(cmdLineGenParser);
1925             }
1926          } else if (strcmp(*argv, "-parse_html") == 0) {
1927             cmdLineParseHtml = TRUE;
1928             /* cmdLineDumpURL will be reset in UrlDump() */
1929             cmdLineDumpURL = TRUE;
1930             cmdLineDumpURLWithHeader = FALSE;
1931             cmdLineDumpURLHeaderOnly = FALSE;
1932          } else if (strcmp(*argv, "-exec") == 0) {
1933             argc--; argv++;
1934             if (argc <= 0) {
1935                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1936                      "-exec");
1937                fprintf(stderr, "\n\n");
1938                Usage(progName);
1939             }
1940             UtilStrCpyN(cmdLineFileToExec, sizeof(cmdLineFileToExec), *argv);
1941             UtilTrimBlanks(cmdLineFileToExec);
1942             if (*cmdLineFileToExec == '-' && cmdLineFileToExec[1] != '\0') {
1943                fprintf(stderr, "%s\n\n",
1944                      TgLoadString(STID_BAD_EXEC_FNAME_DASH));
1945                Usage(progName);
1946             }
1947             cmdLineOpenDisplay = TRUE;
1948          } else if (strcmp(*argv, "-tgif_path") == 0) {
1949             argc--; argv++;
1950             if (argc <= 0) {
1951                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1952                      "-tgif_path");
1953                fprintf(stderr, "\n\n");
1954                Usage(progName);
1955             }
1956             len = strlen(*argv);
1957             if (**argv == '"' && (*argv)[len-1] == '"') {
1958                strcpy(drawPath, &(*argv)[1]);
1959                drawPath[strlen(drawPath)-1] = '\0';
1960             } else {
1961                strcpy(drawPath, *argv);
1962             }
1963             if (!from_prtgif) {
1964                fprintf(stderr, "%s\n",
1965                      TgLoadString(STID_TGIF_PATH_SHOULD_NOT_BE_USED));
1966             }
1967          } else if (strcmp(*argv, "-print_cmd") == 0) {
1968             argc--; argv++;
1969             if (argc <= 0) {
1970                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1971                      "-print_cmd");
1972                fprintf(stderr, "\n\n");
1973                Usage(progName);
1974             }
1975             len = strlen(*argv);
1976             if (**argv == '"' && (*argv)[len-1] == '"') {
1977                strcpy(printCommand, &(*argv)[1]);
1978                printCommand[strlen(printCommand)-1] = '\0';
1979             } else {
1980                strcpy(printCommand, *argv);
1981             }
1982          } else if (strcmp(*argv, "-j2p6_cmd") == 0) {
1983             argc--; argv++;
1984             if (argc <= 0) {
1985                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
1986                      "-j2p6_cmd");
1987                fprintf(stderr, "\n\n");
1988                Usage(progName);
1989             }
1990             len = strlen(*argv);
1991             if (**argv == '"' && (*argv)[len-1] == '"') {
1992                if (len-3 >= MAXSTRING) {
1993                   fprintf(stderr, TgLoadString(STID_CMDLINE_ARG_GIVEN_TOO_LONG),
1994                         "-j2p6_cmd");
1995                   fprintf(stderr, "\n");
1996                } else {
1997                   strcpy(cmdLineJpegToPpm6Cmd, &(*argv)[1]);
1998                   cmdLineJpegToPpm6Cmd[strlen(cmdLineJpegToPpm6Cmd)-1] = '\0';
1999                }
2000             } else {
2001                if (len-1 >= MAXSTRING) {
2002                   fprintf(stderr, TgLoadString(STID_CMDLINE_ARG_GIVEN_TOO_LONG),
2003                         "-j2p6_cmd");
2004                   fprintf(stderr, "\n");
2005                } else {
2006                   strcpy(cmdLineJpegToPpm6Cmd, *argv);
2007                   cmdLineJpegToPpm6Cmd[strlen(cmdLineJpegToPpm6Cmd)-1] = '\0';
2008                }
2009             }
2010          } else if (strcmp(*argv, "-patterndir") == 0) {
2011             argc--; argv++;
2012             if (argc <= 0) {
2013                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2014                      "-patterndir");
2015                fprintf(stderr, "\n\n");
2016                Usage(progName);
2017             }
2018             UtilStrCpyN(cmdLineCustomPatternDir,
2019                   sizeof(cmdLineCustomPatternDir), *argv);
2020             UtilTrimBlanks(cmdLineCustomPatternDir);
2021             len = strlen(cmdLineCustomPatternDir);
2022             if (len > 0 && cmdLineCustomPatternDir[len-1] == DIR_SEP) {
2023                cmdLineCustomPatternDir[len-1] = '\0';
2024             }
2025             if (*cmdLineCustomPatternDir == '\0' ||
2026                   !UtilIsDirectory(cmdLineCustomPatternDir)) {
2027                fprintf(stderr, TgLoadString(STID_PATTERNDIR_NOT_EXIST),
2028                      cmdLineCustomPatternDir);
2029                fprintf(stderr, "\n");
2030                *cmdLineCustomPatternDir = '\0';
2031             }
2032          } else if (strncmp(*argv, "-o", 2) == 0) {
2033             strcpy(outputDir, &(*argv)[2]);
2034          } else if (strcmp(*argv, "-prtgif") != 0 &&
2035                strcmp(*argv, "-print") != 0 &&
2036                strcmp(*argv, "-PRINT") != 0) {
2037             strcat(c_ptr++, " ");
2038             strcat(c_ptr, *argv);
2039             c_ptr += strlen(*argv);
2040          }
2041       } else {
2042          break;
2043       }
2044    }
2045    if (cmdLineOneFilePerPage) {
2046       if ((!cmdLineOpenDisplay && whereToPrint == PRINTER) ||
2047             (cmdLineOpenDisplay && cmdLineWhereToPrint == INVALID)) {
2048          fprintf(stderr, TgLoadString(STID_PRINT_TO_PRINTER_INCOMPAT),
2049                "-one_file_page_page");
2050          fprintf(stderr, "\n\n");
2051          Usage(progName);
2052       }
2053    }
2054    return argc;
2055 }
2056 
ProcessTgifOptions(argc,argv,file_name,name_sz)2057 int ProcessTgifOptions(argc, argv, file_name, name_sz)
2058    int argc, name_sz;
2059    char *argv[], *file_name;
2060    /* All these strangeness with strings are related to */
2061    /*	Prolog's foreign function interface. */
2062 {
2063    int i=0, len=0;
2064 
2065    *file_name = '\0';
2066    *cmdLineTmpFileModeStr = '\0';
2067    *cmdLineGenParser = '\0';
2068    *cmdLineFileToExec = '\0';
2069    *cmdLineRMCastLibDir = *cmdLineRMCastLibPath = '\0';
2070    *cmdLineCustomPatternDir = '\0';
2071    cmdLineMergeArgv = NULL;
2072    for (i=1; i < argc; i++) {
2073       if (strcmp(argv[i], "-display") == 0) {
2074          if (++i == argc) {
2075             fprintf(stderr, "%s\n\n", TgLoadString(STID_MISSING_DISPLAY_NAME));
2076             Usage(progName);
2077          }
2078          if ((displayName=UtilStrDup(argv[i])) == NULL) FailAllocMessage();
2079       } else if (strcmp(argv[i], "-tgrm2") == 0) {
2080          cmdLineTgrm2 = TRUE;
2081       } else if (strcmp(argv[i], "-rmcastlibdir") == 0) {
2082          if (++i == argc) {
2083             fprintf(stderr, "%s\n\n", TgLoadString(STID_MISSING_RMCASTDIR));
2084             Usage(progName);
2085          }
2086          UtilStrCpyN(cmdLineRMCastLibDir, sizeof(cmdLineRMCastLibDir), argv[i]);
2087       } else if (strcmp(argv[i], "-rmcastlibpath") == 0) {
2088          if (++i == argc) {
2089             fprintf(stderr, "%s\n\n", TgLoadString(STID_MISSING_RMCASTLIB));
2090             Usage(progName);
2091          }
2092          UtilStrCpyN(cmdLineRMCastLibPath, sizeof(cmdLineRMCastLibPath),
2093                argv[i]);
2094       } else if (strcmp(argv[i], "-tgwb2") == 0) {
2095          cmdLineWb2 = TRUE;
2096       } else if (strcmp(argv[i], "-bw") == 0) {
2097          cmdLineBW = TRUE;
2098       } else if (strcmp(argv[i], "-parent") == 0) {
2099          if (++i == argc) {
2100             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2101                   "-parent");
2102             fprintf(stderr, "\n\n");
2103             Usage(progName);
2104          }
2105          if (strncmp(argv[i], "0x", 2) == 0) {
2106             if (sscanf(&(argv[i])[2], "%x", &cmdLineParentWindowID) != 1) {
2107                fprintf(stderr, TgLoadString(STID_FAIL_TO_PARSE_FOR_A_VAL),
2108                      argv[i]);
2109                fprintf(stderr, "\n\n");
2110                Usage(progName);
2111             }
2112          } else {
2113             if (sscanf(argv[i], "%d", &cmdLineParentWindowID) != 1) {
2114                fprintf(stderr, TgLoadString(STID_FAIL_TO_PARSE_FOR_A_VAL),
2115                      argv[i]);
2116                fprintf(stderr, "\n\n");
2117                Usage(progName);
2118             }
2119          }
2120       } else if (strcmp(argv[i], "-reqcolor") == 0) {
2121          cmdLineRequestedColor = TRUE;
2122       } else if (strcmp(argv[i], "-rv") == 0) {
2123          cmdLineRV = TRUE;
2124       } else if (strcmp(argv[i], "-nv") == 0) {
2125          cmdLineRV = FALSE;
2126       } else if (strcmp(argv[i], "-cwo") == 0) {
2127          cmdLineCWO = TRUE;
2128          cmdLineBtn1Warp = FALSE;
2129       } else if (strcmp(argv[i], "-cwo+sbwarp") == 0) {
2130          cmdLineCWO = TRUE;
2131          cmdLineBtn1Warp = TRUE;
2132       } else if (strcmp(argv[i], "-hyper") == 0) {
2133          inHyperSpace = TRUE;
2134       } else if (strcmp(argv[i], "-exec") == 0) {
2135          if (++i == argc) {
2136             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2137                   "-exec");
2138             fprintf(stderr, "\n\n");
2139             Usage(progName);
2140          }
2141          UtilStrCpyN(cmdLineFileToExec, sizeof(cmdLineFileToExec), argv[i]);
2142          UtilTrimBlanks(cmdLineFileToExec);
2143          if (*cmdLineFileToExec == '-' && cmdLineFileToExec[1] != '\0') {
2144             fprintf(stderr, "%s\n\n", TgLoadString(STID_BAD_EXEC_FNAME_DASH));
2145             Usage(progName);
2146          }
2147       } else if (strcmp(argv[i], "-dbim") == 0) {
2148          if (++i == argc) {
2149             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2150                   "-dbim");
2151             fprintf(stderr, "\n\n");
2152             Usage(progName);
2153          }
2154          if (cmdLineHasInputMethod) {
2155             fprintf(stderr, "%s\n\n", TgLoadString(STID_ONLYONE_DBIM_OR_SBIM));
2156             Usage(progName);
2157          }
2158          strcpy(cmdLineInputMethod, argv[i]);
2159          cmdLineHasInputMethod = TRUE;
2160          cmdLineDoubleByteInputMethod = TRUE;
2161       } else if (strcmp(argv[i], "-sbim") == 0) {
2162          if (++i == argc) {
2163             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2164                   "-sbim");
2165             fprintf(stderr, "\n\n");
2166             Usage(progName);
2167          }
2168          if (cmdLineHasInputMethod) {
2169             fprintf(stderr, "%s\n\n", TgLoadString(STID_ONLYONE_DBIM_OR_SBIM));
2170             Usage(progName);
2171          }
2172          strcpy(cmdLineInputMethod, argv[i]);
2173          cmdLineHasInputMethod = TRUE;
2174          cmdLineDoubleByteInputMethod = FALSE;
2175       } else if (strcmp(argv[i], "-usexlib") == 0) {
2176          cmdLineUseXlib = TRUE;
2177       } else if (strcmp(argv[i], "-merge") == 0) {
2178          cmdLineMerge = TRUE;
2179       } else if (strcmp(argv[i], "-a4") == 0) {
2180          cmdLineA4 = TRUE;
2181       } else if (strcmp(argv[i], "-letter") == 0) {
2182          cmdLineLetter = TRUE;
2183 #ifdef NOT_DEFINED
2184       } else if (strcmp(argv[i], "-listen") == 0) {
2185          cmdLineWhiteBoardListenOnly = TRUE;
2186 #endif /* NOT_DEFINED */
2187       } else if (strcmp(argv[i], "-no-wb") == 0) {
2188          cmdLineNoWhiteBoard = TRUE;
2189       } else if (strcmp(argv[i], "-no-chat") == 0) {
2190          cmdLineNoChatWindow = TRUE;
2191       } else if (strcmp(argv[i], "-chatnick") == 0) {
2192          if (++i == argc) {
2193             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2194                   "-chatnick");
2195             fprintf(stderr, "\n\n");
2196             Usage(progName);
2197          }
2198          cmdLineChatNickName = UtilStrDup(argv[i]);
2199          if (cmdLineChatNickName == NULL) FailAllocMessage();
2200       } else if (strcmp(argv[i], "-fg") == 0) {
2201          if (++i == argc) {
2202             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2203                   "-fg");
2204             fprintf(stderr, "\n\n");
2205             Usage(progName);
2206          }
2207          cmdLineForeground = (char*)malloc(80*sizeof(char));
2208          if (cmdLineForeground == NULL) FailAllocMessage();
2209          strcpy(cmdLineForeground, argv[i]);
2210       } else if (strcmp(argv[i], "-bg") == 0) {
2211          if (++i == argc) {
2212             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2213                   "-bg");
2214             fprintf(stderr, "\n\n");
2215             Usage(progName);
2216          }
2217          cmdLineBackground = (char*)malloc(80*sizeof(char));
2218          if (cmdLineBackground == NULL) FailAllocMessage();
2219          strcpy(cmdLineBackground, argv[i]);
2220       } else if (strcmp(argv[i], "-bd") == 0) {
2221          if (++i == argc) {
2222             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2223                   "-bd");
2224             fprintf(stderr, "\n\n");
2225             Usage(progName);
2226          }
2227          cmdLineBorder = (char*)malloc(80*sizeof(char));
2228          if (cmdLineBorder == NULL) FailAllocMessage();
2229          strcpy(cmdLineBorder, argv[i]);
2230       } else if (strcmp(argv[i], "-geometry") == 0) {
2231          if (++i == argc) {
2232             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2233                   "-geometry");
2234             fprintf(stderr, "\n\n");
2235             Usage(progName);
2236          }
2237          strcat(geometrySpec, argv[i]);
2238          geometrySpecified = TRUE;
2239       } else if (strcmp(argv[i], "-listdontreencode") == 0) {
2240 #ifdef _DONT_REENCODE
2241          fprintf(stderr, "-D_DONT_REENCODE=\"%s\"\n", _DONT_REENCODE);
2242 #endif /* _DONT_REENCODE */
2243       } else if (strcmp(argv[i], "-version") == 0 ||
2244             strcmp(argv[i], "--version") == 0) {
2245          cmdLineVersion = TRUE;
2246       } else if (strcmp(argv[i], "-nomode") == 0) {
2247          cmdLineNoModeWindow = TRUE;
2248       } else if (strcmp(argv[i], "-pdfspd") == 0) {
2249          cmdLinePdfSetPageDevice = TRUE;
2250       } else if (strncmp(argv[i], "-pdfspd=", 8) == 0) {
2251          char tmp_str[80];
2252 
2253          UtilStrCpyN(tmp_str, sizeof(tmp_str), &(argv[i])[8]);
2254          UtilTrimBlanks(tmp_str);
2255          if (UtilStrICmp(tmp_str, "true") == 0) {
2256             cmdLinePdfSetPageDevice = TRUE;
2257          } else if (UtilStrICmp(tmp_str, "false") == 0) {
2258             cmdLinePdfSetPageDevice = FALSE;
2259          } else {
2260             fprintf(stderr, TgLoadString(STID_BAD_CMDLINE_OPT_IGNORED),
2261                   "-pdfspd", tmp_str);
2262             fprintf(stderr, "\n");
2263          }
2264       } else if (strcmp(argv[i], "-pssetup") == 0) {
2265          i++;
2266          if (i >= argc) {
2267             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2268                   "-pssetup");
2269             fprintf(stderr, "\n\n");
2270             Usage(progName);
2271          }
2272          cmdLinePsSetup = TRUE;
2273          SetPsSetup(argv[i]);
2274       } else if (strcmp(*argv, "-patterndir") == 0) {
2275          argc--; argv++;
2276          if (argc <= 0) {
2277             fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2278                   "-patterndir");
2279             fprintf(stderr, "\n\n");
2280             Usage(progName);
2281          }
2282          UtilStrCpyN(cmdLineCustomPatternDir,
2283                sizeof(cmdLineCustomPatternDir), *argv);
2284          UtilTrimBlanks(cmdLineCustomPatternDir);
2285          len = strlen(cmdLineCustomPatternDir);
2286          if (len > 0 && cmdLineCustomPatternDir[len-1] == DIR_SEP) {
2287             cmdLineCustomPatternDir[len-1] = '\0';
2288          }
2289          if (*cmdLineCustomPatternDir == '\0' ||
2290                !UtilIsDirectory(cmdLineCustomPatternDir)) {
2291             fprintf(stderr, TgLoadString(STID_PATTERNDIR_NOT_EXIST),
2292                   cmdLineCustomPatternDir);
2293             fprintf(stderr, "\n");
2294             *cmdLineCustomPatternDir = '\0';
2295          }
2296       } else if (argv[i][0] == '=') {
2297          strcpy(geometrySpec, argv[i]);
2298          geometrySpecified = TRUE;
2299       } else if (argv[i][0] != '-') {
2300          if (cmdLineMerge) {
2301             int j=0;
2302 
2303             cmdLineMergeArgc = argc-i;
2304             cmdLineMergeArgv = (char**)malloc(cmdLineMergeArgc*sizeof(char*));
2305             if (cmdLineMergeArgv == NULL) FailAllocMessage();
2306             for (j=i; j < argc; j++) {
2307                if (*argv[j] == '-') {
2308                   Usage(progName);
2309                }
2310                cmdLineMergeArgv[j-i] = UtilStrDup(argv[j]);
2311                if (cmdLineMergeArgv[j-i] == NULL) FailAllocMessage();
2312             }
2313             break;
2314          } else {
2315             UtilStrCpyN(file_name, name_sz, argv[i]);
2316          }
2317       } else {
2318 #ifndef USE_XT_INITIALIZE
2319          Usage(progName);
2320 #else /* USE_XT_INITIALIZE */
2321          if (strcmp(argv[i], "-xrm") == 0) {
2322             if (++i == argc) {
2323                fprintf(stderr, TgLoadString(STID_MISSING_ARG_FOR_CMDLINE_OPT),
2324                      "-xrm");
2325                fprintf(stderr, "\n");
2326             }
2327          }
2328 #endif /* ~USE_XT_INITIALIZE */
2329       }
2330    }
2331    return TRUE;
2332 }
2333