1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 #include "../base/defines.h"
11 
12 /*---------------------------------------------------------------------*
13  |                                Parse.c                              |
14  |                           -------------------                       |
15  |                                                                     |
16  *---------------------------------------------------------------------*/
17 
18 #ifdef OS2
19 #include <stdlib.h>
20 #include <types.h>
21 #endif
22 #include <stdio.h>
23 #include <string.h>
24 
25 #include <Xm/Xm.h>
26 #include "../widgets/MultiText.h"
27 #include "help.h"
28 
29 #define STRCMP(a,b)  ((a) ? ((b) ? strcmp(a,b) : strcmp(a,"")) : \
30 			    ((b) ? strcmp("",b) : 0))
31 
32 #define TAGLEN        3
33 #define DEFAULT_FONT  "-adobe-times-medium-r-normal--14*"
34 #define DEFAULT_COLOR "black"
35 
36 int SendWidgetText (Widget w, FILE *infile, Bool ManPage, char *ref);
37 char *GetMathToken(FILE **mathfile, char *stoppat, char*buffer);
38 
39 
40 /* external routines */
41 
42 extern void   Push();
43 extern char  *Pop();
44 extern void   DoRedrawText();
45 extern Stack *NewStack();
46 extern char  *Top();
47 extern void FreeSpotList(SpotList *); /* from helplist.c */
48 extern void InsertList(SpotList *, ListNodeType *); /* from helplist.c */
49 
50 
51 
52 /*--------------------------------------------------------------------------*
53  |                                GetMathToken                              |
54  *--------------------------------------------------------------------------*/
GetMathToken(FILE ** mathfile,char * stoppat,char * buffer)55 char *GetMathToken(FILE **mathfile, char *stoppat, char*buffer)
56 {
57  char rest[255],fmt[20];
58  int  ch;
59  sprintf(fmt,"%s%s%s%s%s%s","%[^",stoppat,"]","%[",stoppat,"]");
60  while(((ch = fgetc(*mathfile)) != EOF) && ((ch == ' ') || ch == '\n')) ;
61  if (feof(*mathfile)) return(NULL);
62  ungetc(ch,*mathfile);
63  if (fscanf(*mathfile,fmt,buffer,rest) == EOF)
64     return(NULL);
65  return(buffer);
66 }
67 
68 /*--------------------------------------------------------------------------*
69  |                                GetToken                                  |
70  *--------------------------------------------------------------------------*/
GetToken(FILE ** file,char * stoppat,char * buffer,int * newline,Bool ManPage)71 char *GetToken (FILE **file, char *stoppat, char *buffer, int *newline, Bool ManPage)
72 {
73   char  rest[255];
74   char  fmt[20];
75   int   n = 0;
76 
77  if (ManPage == FALSE) {
78     sprintf(fmt, "%s%s%s%s%s%s", "%[^" ,stoppat, "]" , "%[" ,stoppat, "]");
79     if (fscanf(*file, fmt, buffer, rest) == EOF) return (NULL);
80     while (buffer[n] == ' ') n++;  /* skip any leading white space */
81   } else {
82     if (fscanf(*file, "%[^\n]%[\n]",buffer,rest) == EOF) return(NULL);
83     if (strchr(buffer,'	') != NULL)
84         *strchr(buffer,'\t') = '-';
85     *newline = TRUE;
86     }
87   return(buffer + n);
88 }
89 
90 /*--------------------------------------------------------------------------*
91  |                              PushDefaults                                |
92  *--------------------------------------------------------------------------*/
PushDefaults(UserData * userdata)93 void PushDefaults(UserData *userdata)
94 {
95   userdata->fontstack = NewStack();
96   userdata->colorstack = NewStack();
97   Push(userdata->fontstack,DEFAULT_FONT);
98   Push(userdata->colorstack,DEFAULT_COLOR);
99 }
100 
101 
102 /*--------------------------------------------------------------------------*
103  |                              SendWidgetText                              |
104  *--------------------------------------------------------------------------*/
SendWidgetText(Widget w,FILE * infile,Bool ManPage,char * ref)105 int SendWidgetText (Widget w, FILE *infile, Bool ManPage,char *ref)
106 {
107   int              indentnum = 0;
108   int              tabs[MAX_TAB_COUNT];
109   int              tabCount;
110   int              i,len;
111   int              newline = FALSE;
112   UserData         *userdata;
113   ListNodeType	   *spot;
114   char             buffer[255];
115   char             psfile[127];
116   char font[127];
117   char color[127];
118   char tabBuf[127];
119   char link[127];
120   char indent[15];
121   Boolean          dpsCapable;
122   char             *psBtmp;
123   FILE             *ascfile;
124   char             ascname[15];
125   char             fascname[127];
126   const char       *envptr;
127   int              psBpos;
128 
129   strcpy(font, DEFAULT_FONT);
130   strcpy(color, DEFAULT_COLOR);
131   strcpy(tabBuf, "\0");
132   strcpy(link, "\0");
133   strcpy(indent, "0");
134   strcpy(ascname, "\0");
135   strcpy(fascname, "\0");
136 
137   XtVaGetValues(w,
138 		XmNuserData, &userdata,
139 		XmNdpsCapable, &dpsCapable,
140 		NULL);
141 
142   if (userdata->fontstack == NULL) {
143       PushDefaults(userdata);
144    }
145   else {
146      if (userdata->spotlist != NULL)
147        FreeSpotList(userdata->spotlist);
148      len = userdata->fontstack->length;
149      for (i = 0; i < len ; i++)
150        Pop(userdata->fontstack);
151 
152      len = userdata->colorstack->length;
153      for (i = 0; i < len ; i++)
154        Pop(userdata->colorstack);
155    }
156 
157   while (GetToken(&infile," \n",buffer,&newline,ManPage) != NULL)
158     {
159       if (buffer[0] == '#')
160         {
161           if (buffer[1] == '!')
162 	  {
163             switch (buffer[2])
164               {
165               case 'R':
166                userdata->getposition = TRUE;
167                spot = (ListNodeType *) XtMalloc(sizeof(ListNodeType));
168                spot->refname = XtMalloc(strlen(buffer) + 1);
169                strcpy(spot->refname,buffer + TAGLEN);
170                InsertList(userdata->spotlist,spot);
171                break;
172 
173               case 'F':                                 /* Font */
174                 font[strlen(buffer) - TAGLEN] = '\0';
175                 strncpy(font,buffer+TAGLEN,strlen(buffer) - TAGLEN);
176                 Push(userdata->fontstack,font);
177                 break;
178               case 'C':                                 /* Color */
179                 color[strlen(buffer) - TAGLEN] = '\0';
180                 strncpy(color,buffer+TAGLEN,strlen(buffer) - TAGLEN);
181                 Push(userdata->colorstack,color);
182                 break;
183               case 'L':                                 /* Link */
184                 link[strlen(buffer) - TAGLEN] = '\0';
185                 strncpy(link,buffer+TAGLEN,strlen(buffer) - TAGLEN);
186                 GetToken(&infile," \n",buffer,&newline,ManPage);
187 		switch (buffer[0])
188 		  {
189 		  case 'l':	userdata->linkType = LINK;		break;
190                   case 'h':
191                   case 'v':
192 		  case 's':	userdata->linkType = SPOTREF;		break;
193 		  case 't':	userdata->linkType = LINK;		break;
194 		  case 'f':	userdata->linkType = SPOTREF;	break;
195 		  case 'p':	userdata->linkType = LINK;		break;
196 		  default:	userdata->linkType = NOOP;		break;
197 		  }
198                 break;
199                case 'P':
200                 psfile[strlen(buffer) - TAGLEN] = '\0';
201                 strncpy(psfile,buffer+TAGLEN,strlen(buffer) - TAGLEN);
202 
203 		XtVaGetValues(w,
204 			      XmNdpsCapable, &dpsCapable,
205 			      NULL);
206 
207                 if (dpsCapable) {
208                   XmMultiTextAppendDPS(w, "BITMAP", Top(userdata->fontstack),
209                          Top(userdata->colorstack), indentnum, psfile, 257, 166,
210                          XmMultiTextMakeLinkRecord(w,
211 						 (LinkType)userdata->linkType,
212 						 (LinkPosition)0, (char*)link));
213 
214                   if (userdata->getposition == TRUE) {
215                      userdata->getposition = FALSE;
216                      userdata->spotlist->tail->offset = XmMultiTextGetPosition(w);
217                   }
218                 }
219                 else {
220                  /* get ascii version of postscript files */
221                    psBtmp  = strstr(psfile, ".ps.B");
222                    if (psBtmp != NULL) {
223                      psBpos = (abs(psBtmp - psfile));
224 		     /*
225                      envptr = getenv("TEXTDIR");
226 		     */
227 		     envptr = GetHelpDirectory();;
228                      ascname[0] = '\0';
229                      strncat(ascname,psfile,psBpos);
230                      fascname[0] = '\0';
231                      sprintf(fascname,"%s%s%s%s",envptr,"/",ascname,".asc");
232                      ascfile = fopen(fascname,"r");
233                      if (ascfile == NULL)
234                        printf("Error opening ascii version of DPS file %s \n",fascname);
235                      else {
236                         if (strstr(ascname, "SVSPS") != NULL  ||
237                             strstr(ascname, "svsps") != NULL) {
238                           while (GetMathToken(&ascfile," \n",buffer) != NULL)
239                           XmMultiTextAppendWord(w, buffer, Top(userdata->fontstack),
240                                     Top(userdata->colorstack), indentnum,
241                                     XmMultiTextMakeLinkRecord(w,
242                                        (LinkType) userdata->linkType, 0, link));
243                         }
244                         else {
245                           while (GetToken(&ascfile," \n",buffer,&newline,ManPage) != NULL) {
246                              if (buffer[0] == '#')
247                                {
248                                  if (buffer[1] == '!')
249                                    switch (buffer[2]) {
250                                      case 'T':                                 /* Tab Stops */
251                                         switch (buffer[3]) {
252                                           case 'B':
253                                             break;
254                                           default:
255                                             tabBuf[strlen(buffer) - TAGLEN] = '\0';
256                                             strncpy(tabBuf,buffer+TAGLEN,strlen(buffer) - TAGLEN);
257                                             tabCount = 0;
258                                             for (i=0; i<strlen(tabBuf); i++)
259                                               if (tabBuf[i] == ',') {
260                                                 sscanf(&tabBuf[i+1], "%d", &tabs[tabCount]);
261                                                 tabCount++;
262                                               }
263                                             XmMultiTextSetTabStops(w, tabs, tabCount);
264                                             break;
265                                         }
266                                         break;
267                                       case 'F':                                 /* Font */
268                                         font[strlen(buffer) - TAGLEN] = '\0';
269                                         strncpy(font,buffer+TAGLEN,strlen(buffer) - TAGLEN);
270                                         Push(userdata->fontstack,font);
271                                         break;
272                                       case 'N':                                 /* New Line */
273                                         indentnum = atoi(indent);
274                                         XmMultiTextAppendNewLine(w, buffer, Top(userdata->fontstack),
275                                                                     indentnum);
276                                         break;
277                                       case 'E':                                 /* End */
278                                         switch (buffer[3])
279 		                          {
280 		                            case 'F':     /* End Font */
281 		                            Pop(userdata->fontstack);
282 		                            strcpy(font, Top(userdata->fontstack));
283 		                            break;
284                                           }
285                                      default:
286                                         ;
287                                    }
288                                }
289                                else
290                                  {
291                                    if (STRCMP(buffer, "TAB") == 0)
292               				XmMultiTextAppendWord(w, "	", Top(userdata->fontstack),
293                                          Top(userdata->colorstack), indentnum,
294                                          XmMultiTextMakeLinkRecord(w,
295                                                (LinkType) userdata->linkType,
296 					       0, link));
297                                     else
298                                          XmMultiTextAppendWord(w, buffer, Top(userdata->fontstack),
299                                             Top(userdata->colorstack), indentnum,
300                                             XmMultiTextMakeLinkRecord(w,
301                                                 (LinkType) userdata->linkType,
302 						0, link));
303                                   }
304                             }
305                         }
306                         fclose(ascfile);
307                      }
308                    }
309                 }
310                 break;
311               case 'N':                                 /* New Line */
312                 indentnum = atoi(indent);
313                 XmMultiTextAppendNewLine(w, buffer, Top(userdata->fontstack),
314                                                                     indentnum);
315                 break;
316               case 'T':                                 /* Tab Stops */
317                switch (buffer[3]) {
318                  case 'B':
319                     break;
320                  default:
321                    tabBuf[strlen(buffer) - TAGLEN] = '\0';
322                    strncpy(tabBuf,buffer+TAGLEN,strlen(buffer) - TAGLEN);
323                    tabCount = 0;
324                    for (i=0; i<strlen(tabBuf); i++)
325                      if (tabBuf[i] == ',') {
326                        sscanf(&tabBuf[i+1], "%d", &tabs[tabCount]);
327                        tabCount++;
328                      }
329                    XmMultiTextSetTabStops(w, tabs, tabCount);
330                    break;
331                 }
332                 break;
333               case 'I':                                 /* Indent */
334                 indent[strlen(buffer) - TAGLEN] = '\0';
335                 strncpy(indent,buffer+TAGLEN,strlen(buffer) - TAGLEN);
336                 break;
337               case 'E':                                 /* End */
338                 switch (buffer[3])
339 		  {
340 		  case 'L':     /* End link */
341 		    userdata->linkType = NOOP;
342 		    link[0] = '\0';
343 		    break;
344 		  case 'F':     /* End Font */
345 		    Pop(userdata->fontstack);
346 		    strcpy(font, Top(userdata->fontstack));
347 		    break;
348 		  case 'C':     /* End Color */
349 		    Pop(userdata->colorstack);
350 		    strcpy(color, Top(userdata->colorstack));
351 		    break;
352 		  case 'T':     /* End Tab  */
353 		    break;
354 		  }
355               default:
356                 ;
357               }
358 	  }
359 	  else
360 	  {
361 	    XmMultiTextAppendWord(w, buffer, Top(userdata->fontstack),
362 				 Top(userdata->colorstack), indentnum,
363 				 XmMultiTextMakeLinkRecord
364 				    (w, (LinkType) userdata->linkType, 0, link)
365 				 );
366 	  }
367         }
368       else
369         {
370             indentnum = atoi(indent);
371             if (STRCMP(buffer, "TAB") == 0)
372               XmMultiTextAppendWord(w, "	", Top(userdata->fontstack),
373                                     Top(userdata->colorstack), indentnum,
374                                     XmMultiTextMakeLinkRecord(w,
375                                        (LinkType) userdata->linkType, 0, link));
376             else
377               XmMultiTextAppendWord(w, buffer, Top(userdata->fontstack),
378                                     Top(userdata->colorstack), indentnum,
379                                     XmMultiTextMakeLinkRecord(w,
380                                        (LinkType) userdata->linkType, 0, link));
381 
382             if (userdata->getposition == TRUE) {
383                  userdata->getposition = FALSE;
384                  userdata->spotlist->tail->offset = XmMultiTextGetPosition(w);
385               }
386 
387             if (ManPage == TRUE && newline == TRUE) {
388                 indentnum = atoi(indent);
389                 XmMultiTextAppendNewLine(w, "#!N", Top(userdata->fontstack),
390                                                                     indentnum);
391             }
392 
393 
394         }
395      newline = FALSE;
396     }
397 
398  return(0);
399 
400 }
401