1 
2 #include <string.h>
3 #include "common.hpp"
4 
5 #ifdef __QNXNTO__
6    #include <strings.h>
7 #endif // __QNXNTO__
8 
9 HeapStrStore *hs;
10 
11 
12 struct _tags {
13     char *tag;
14     int ret;
15 };
16             // 1 ipf only till '.';  2 ipf full line
17 
18 _tags taglist[] = { { "acviewport", 2 },
19                     { "artlink", 2 },
20                     { "artwork", 2 },
21                     { "caution", 1 },
22                     { "cgraphic", 2 },
23                     { "color", 1 },
24                     { "ctrl", 2 },
25                     { "ctrldef", 2 },
26                     { "ddf", 2 },
27                     { "dl", 2 },
28                     { "docprof", 2 },
29                     { "eartlink", 1 },
30                     { "ecaution", 1 },
31                     { "ecgraphic", 1 },
32                     { "ectrldef", 1 },
33                     { "edl", 1 },
34                     { "efig", 1 },
35                     { "efn", 1 },
36                     { "ehide", 1 },
37                     { "ehp1", 1 },
38                     { "ehp2", 1 },
39                     { "ehp3", 1 },
40                     { "ehp4", 1 },
41                     { "ehp5", 1 },
42                     { "ehp6", 1 },
43                     { "ehp7", 1 },
44                     { "ehp8", 1 },
45                     { "ehp9", 1 },
46                     { "elines", 1 },
47                     { "elink", 1 },
48                     { "ent", 1 },
49                     { "eol", 1 },
50                     { "eparml", 1 },
51                     { "esl", 1 },
52                     { "etable", 1 },
53                     { "eul", 1 },
54                     { "euserdoc", 1 },
55                     { "ewarning", 1 },
56                     { "exmp", 1 },
57                     { "fig", 2 },
58                     { "figcap", 2 },
59                     { "font", 1 },
60                     { "fn", 1 },
61                     { "h1", 2 },
62                     { "h2", 2 },
63                     { "h3", 2 },
64                     { "h4", 2 },
65                     { "h5", 2 },
66                     { "h6", 2 },
67                     { "hide", 1 },
68                     { "hp1", 1 },
69                     { "hp2", 1 },
70                     { "hp3", 1 },
71                     { "hp4", 1 },
72                     { "hp5", 1 },
73                     { "hp6", 1 },
74                     { "hp7", 1 },
75                     { "hp8", 1 },
76                     { "hp9", 1 },
77                     { "i1", 2 },
78                     { "i2", 2 },
79                     { "icmd", 2 },
80                     { "isyn", 2 },
81                     { "li", 2 },
82                     { "lines", 1 },
83                     { "link", 1 },
84                     { "lm", 1 },
85                     { "lp", 1 },
86                     { "note", 1 },
87                     { "nt", 1 },
88                     { "ol", 1 },
89                     { "p", 1 },
90                     { "parml", 1 },
91                     { "pbutton", 2 },
92                     { "pd", 1 },
93                     { "pt", 1 },
94                     { "rm", 1 },
95                     { "sl", 1 },
96                     { "table", 1 },
97                     { "title", 1 },
98                     { "ul", 1 },
99                     { "userdoc", 1 },
100                     { "warning", 1 },
101                     { "xmp", 1 },
102                     { "" , 0 }
103                   };
104 
IsIpfTag(const char * Tag)105 int IsIpfTag (const char *Tag)
106 {
107     int i = 0;
108     int TagLen = strcspn (Tag+1, " .");
109     while (taglist[i].tag[0]) {
110         int taglistlen = strlen (taglist[i].tag);
111         if (TagLen == taglistlen)
112             if (strncasecmp (taglist[i].tag, Tag+1, TagLen) == 0)
113                 return taglist[i].ret;
114         i ++;
115     }
116     return 0;
117 }
118 
119 
120 struct _cwords {
121     char *cword;
122     int ret;
123 };
124 
125 
126 _cwords cwordlist[] = { { "im",     _FileImport_ },
127                         { "br",     _Remove_ },
128                         { "ce",     _Center_ },
129                         { "*",      _Remove_ },
130                         { "nameit", _Define_ },
131                         { "ifdef",  _IfDef_  },
132                         { "" , 0 }
133                       };
134 
IsCWord(const char * CWord)135 int IsCWord (const char *CWord)
136 {
137     int i = 0;
138     int CWordLen = strcspn (CWord+1, " ");
139     while (cwordlist[i].cword[0]) {
140         int cwordlistlen = strlen (cwordlist[i].cword);
141         if (CWordLen == cwordlistlen)
142             if (strncasecmp (cwordlist[i].cword, CWord+1, CWordLen) == 0)
143                 return cwordlist[i].ret;
144         i ++;
145     }
146     return _None_;
147 }
148 
149 
150 struct SymLst {
151     char symbol[10];
152     char text[10];
153 };
154 
155 
GetStdSymbol(const char * str,int len)156 char *GetStdSymbol (const char *str, int len)
157 {
158     static SymLst symtbl[] = { { "osq", "'"  },
159                                { ""   , "" }
160                              };
161 
162     SymLst *s = symtbl;
163     while (*s->symbol) {
164         if (strncmp (str, s->symbol, len) == 0)
165             if (s->symbol[len] == '\0')
166                 return s->text;
167         s ++;
168     }
169     return NULL;
170 }
171 
172 // ================ HeapStrStore ================
173 
174 #define bufsize 256
175 
176 
ocmpf(const heapobj ** obj1,const heapobj ** obj2)177 int ocmpf (const heapobj **obj1, const heapobj **obj2)
178 {
179     return strcasecmp ((char *)(*obj1)->buffer, (char *)(*obj2)->buffer);
180 }
181 
182 
HeapStrStore()183 HeapStrStore::HeapStrStore ()
184 {
185     objbuf = (heapobj *) new byte[sizeof (heapobj) + bufsize];
186     sorted = FALSE;
187 }
188 
~HeapStrStore()189 HeapStrStore::~HeapStrStore ()
190 {
191     delete objbuf;
192 }
193 
194 
StrStore(const char * sym,int symlen,const char * text,int textlen)195 void HeapStrStore::StrStore (const char *sym, int symlen, const char *text, int textlen)
196 {
197     int totstrlen = symlen + 1 + textlen;
198     if (totstrlen > bufsize)
199         return;
200     objbuf->size = (word) (totstrlen + sizeof (heapobj));
201     objbuf->symsize = word (symlen + 1);
202 
203     strncpy ((char *)objbuf->buffer, sym, symlen);  // symbol name
204     objbuf->buffer[symlen] = '\0';
205 
206     strncpy ((char *)objbuf->buffer+objbuf->symsize, text, textlen); // text
207     objbuf->buffer[totstrlen] = '\0';
208 
209     Store (objbuf);
210     sorted = FALSE;
211 }
212 
213 
GetSymbol(const char * str,int len)214 char *HeapStrStore::GetSymbol (const char *str, int len)
215 {
216     if (!sorted) {
217         Sort (CMPF (ocmpf));
218         sorted = TRUE;
219     }
220 
221     len = __min (len, bufsize);
222     strncpy ((char *)objbuf->buffer, str, len);
223     objbuf->buffer[len] = '\0';
224 
225     heapobj *obj = (heapobj *) Retrieve (&objbuf);
226 
227     if (!obj)
228         return NULL;
229 
230     return (char *)obj->buffer+obj->symsize;
231 }
232 
233 
234 // ================ Expline ================
235 
236 
ExpLine()237 ExpLine::ExpLine ()
238 {
239     bufi = 0;           // use first buffer for destination
240 }
241 
242 
Expand(const char * s)243 char *ExpLine::Expand (const char *s)
244 {
245     BOOL ExpDone = FALSE;
246     char *d = buf[bufi];
247 
248     while (*s) {
249         if (*s == '&') {
250             int len = strcspn (s, ".");  // length of possible symbol name
251             if (s[len] == '.') {
252                 len ++;
253                 if (len > 2) {
254                     char *text = hs->GetSymbol (s+1, len-2);
255                     if (!text)
256                         text = GetStdSymbol (s+1, len-2);
257                     if (text) {
258                         d = fl_stpcpy (d, text);
259                         s += len;
260                         ExpDone = TRUE;
261                         continue;
262                     }
263                 }
264             }
265         }
266         *(d++) = *(s++);
267     }
268     *d = '\0';
269 
270     d = buf[bufi];      // reset string pointer
271 
272     if (ExpDone) {
273         bufi = (bufi + 1) % 2;      // new destination buffer
274         return Expand (d);
275     }
276 
277     return d;
278 }
279 
280 // ============== Function to process .nameit =================
281 
GetNameIt(const char * line,const char * & s,int & slen,const char * & t,int & tlen)282 int GetNameIt (const char *line, const char *&s, int &slen, const char *&t, int &tlen)
283 {                                                   // 0 on success
284     s = line;
285     s += strcspn (s, " \t\n"); // skip nameit
286     s += strspn (s, " \t\n"); // skip space
287     slen = strcspn (s, " \t\n"); // length of symbol name
288     if (slen == 0)
289         return -1;
290     t = s + slen;          // skip symbol name
291     t += strspn (t, " \t\n");   // skip space
292     if (*t != '\'')
293         return -1;
294     t ++;           // skip heading '
295     #ifndef __QNXNTO__
296        char *endofstr = strrchr (t, '\'');
297     #else
298        char *endofstr = strrchr ((char *)t, '\'');
299     #endif // __QNXNTO__
300     if (!endofstr)
301         return -1;
302     tlen = int (endofstr - t); // length of text
303     return 0;
304 }
305 
306 
307 
308