source: git/Singular/libparse.ll @ f22f22a

spielwiese
Last change on this file since f22f22a was f22f22a, checked in by Volker Braun <vbraun.name@…>, 7 years ago
fix: Increase library filename buffer length Increase the buffer size from 128 to 1024 chars. E.g. Conda and Sage intentionally build binaries with long (>100 chars) parent directories to truncate them when installing the binaries. This caused random segfaults, see https://trac.sagemath.org/ticket/22175
  • Property mode set to 100644
File size: 30.0 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8#include <ctype.h>
9#include <kernel/mod2.h>
10#ifdef STANDALONE_PARSER
11  #include <Singular/utils.h>
12
13  #define HAVE_LIBPARSER
14  #define YYLPDEBUG 1
15  #define myfread fread
16#else
17  #include <Singular/subexpr.h>
18  #include <Singular/grammar.h>
19  #include <Singular/ipshell.h>
20  #include <Singular/ipid.h>
21  #include <Singular/tok.h>
22  #include <misc/options.h>
23  #include <omalloc/omalloc.h>
24#endif
25#include <Singular/libparse.h>
26
27#ifdef HAVE_LIBPARSER
28#define YY_SKIP_YYWRAP
29
30typedef enum { LP_NONE, LP_INFO, LP_CATEGORY, LP_URL, LP_VERSION} lib_cmds;
31
32int libread(FILE* f, char* buf, int max_size);
33int current_pos(int i);
34void print_version(lp_modes mode, char *p);
35void copy_string(lp_modes mode);
36void make_version(char *p, int what);
37
38int brace1 = 0;  /* { } */
39int brace2 = 0;  /* ( ) */
40int brace3 = 0;  /* [ ] */
41int quote  = 0;  /* " */
42int offset = 0;
43BOOLEAN p_static = FALSE;
44int old_state = 0;
45lib_cmds last_cmd = LP_NONE;
46
47char libnamebuf[1024];
48char *text_buffer=NULL;
49long string_start;
50
51char *yylp_buffer_start;
52#ifndef NEW_FLEX
53int yylplineno = 1;
54#endif /* NEW_FLEX */
55int lpverbose = 0, check = 0;
56int texinfo_out = 0;
57int found_info=0,
58    found_cat=0,
59    found_version=0,
60    found_oldhelp = 0,
61    found_proc_in_proc = 0;
62
63const char *yylp_errlist[]= {
64   "",
65   "missing close bracket ')' for proc definition in line %d.",  /*  1 */
66   "missing close bracket ')' for procbody in line %d.",         /*  2 */
67   "missing close bracket ']' for procbody in line %d.",         /*  3 */
68   "too many ')' closed brackets in line %d.",                   /*  4 */
69   "too many ']' closed brackets in line %d.",                   /*  5 */
70   "missing close bracket ')' for example in line %d.",          /*  6 */
71   "missing close bracket ']' for example in line %d.",          /*  7 */
72   "cannot assign character '%c' in line %d to any group.",      /*  8 */
73   "there must be a quote missing somewhere before line %d.",    /*  9 */
74   "missing close bracket '}' at end of library in line %d.",    /* 10 */
75   "missing close bracket ')' at end of library in line %d.",    /* 11 */
76   "missing close bracket ']' at end of library in line %d.",    /* 12 */
77   NULL
78};
79int yylp_errno = 0;
80
81#ifdef STANDALONE_PARSER
82procinfov pi;
83int category_out = 0;
84void printpi(procinfov pi);
85void pi_clear(procinfov pi);
86extern "C" {
87  int yylpwrap();
88}
89void main_init(int argc, char *argv[]);
90void main_result(char *libname);
91#else /* STANDALONE_PARSER */
92idhdl h0;
93idhdl h_top;
94#define pi IDPROC(h0)
95extern "C"
96{
97  int yylpwrap();
98}
99extern libstackv library_stack;
100#endif /* STANDALONE_PARSER */
101
102static unsigned long help_chksum;
103
104#define SET_DEF_END(mode, pi, p) \
105  if ( mode == LOAD_LIB) pi->data.s.def_end = p;
106#define SET_HELP_START(mode, pi, p) \
107  if ( mode == LOAD_LIB) {pi->data.s.help_start = p; help_chksum = 0;}
108#define SET_HELP_END(mode, pi, p) \
109  if ( mode == LOAD_LIB) {pi->data.s.help_end = p;  \
110              pi->data.s.help_chksum = help_chksum;}
111
112#define SET_BODY_START(mode, pi, l, p) \
113     if ( mode == LOAD_LIB)            \
114     {                                 \
115       pi->data.s.body_lineno = l;     \
116       pi->data.s.body_start = p;      \
117     }
118#define SET_BODY_END(mode, pi, p) \
119     if ( mode == LOAD_LIB)       \
120     {                            \
121       pi->data.s.body_end = p-1; \
122       pi->data.s.proc_end = p-1; \
123     }
124
125#define SET_EXAMPLE_START(mode, pi, l, p) \
126   if ( mode == LOAD_LIB)                 \
127   {                                      \
128     pi->data.s.example_lineno = l;       \
129     pi->data.s.example_start = p;        \
130   }
131#define SET_PROC_END(mode, pi, p)   \
132     if ( mode == LOAD_LIB)         \
133     {                              \
134       pi->data.s.proc_end = p-1;   \
135       if(pi->data.s.body_end==0)   \
136         pi->data.s.body_end = p-1; \
137     }
138
139#define ROTATE_RIGHT(c) if ((c) & 01) (c) = ((c) >>1) + 0x8000; else (c) >>= 1;
140#define IncrCheckSum(c)                          \
141do                                            \
142{                                             \
143  ROTATE_RIGHT(help_chksum);                  \
144  help_chksum += c;                           \
145  help_chksum &= 0xffff;                      \
146}                                             \
147while(0)
148
149#undef YY_DECL
150#define YY_DECL int yylex(const char *newlib, const char *libfile, \
151                           lib_style_types *lib_style, \
152                           idhdl pl, BOOLEAN autoexport, lp_modes mode)
153#undef YY_INPUT
154#define YY_INPUT(buf,result,max_size) \
155          if ( ((result = libread( (yyin), (char *) buf, max_size )) < 0 ) \
156                  && ferror( yyin ) ) \
157                YY_FATAL_ERROR( "read in flex scanner failed" );
158
159#define YY_USER_INIT { \
160       BEGIN(header); \
161       yylplineno = 1; \
162       yylp_errno = 0; \
163       *lib_style = OLD_LIBSTYLE; \
164       strcpy(libnamebuf,"(**unknown version**)"); \
165     }
166
167#if 0
168<pbody>proc[ \t]+{name}  {
169                           printf("MISSING: PROC-cmd found. ERROR!\n"); }
170<pbody>example[ \t]*\n   {
171                           yylplineno++;
172                           printf("MISSING: EXAMPLE-cmd found. ERROR!\n"); }
173info=+"\"" {
174#endif
175
176%}
177
178digit          [0-9]
179letter         [@a-zA-Z\']
180name           ({letter}({letter}*{digit}*_*)*|_)
181varname        ({letter}({letter}*{digit}*_*\(\))*|_|#)
182letters        ({letter}|{digit}|[_./#%^*:,])
183string         ({letters}*)
184comment        [\/][\/]
185dolar          [$]
186nls            [\n\r]
187symbols        [~!@#$%^&*()_+-=\\\|\[\];:,<.>/\?\' \t\~\`]
188asymbols       ({symbols}|[{}])
189bsymbols       ({symbols}|{escbrack}|[}])
190aletters       ({letter}|{digit}|{asymbols}|{dolar}|{escquote}|{nls})
191bletters       ({letter}|{digit}|{bsymbols}|{dolar}|{quote}|{nls})
192cletters       ({letter}|{digit}|{asymbols}|{dolar}|{quote})
193strings        ({aletters}*)
194escstrings     ({bletters}*)
195fstring        ({cletters}*)
196param          ({name}+{tos}+{varname})
197parameters     ({param}(,{param})*)
198paramlist      ("("{parameters}")")
199quote          [\"]
200escquote       (\\\")
201escbrack       (\\\{)
202tnl            ([ \t\n\r]*)
203eos            ({quote}+{tnl}+"{")
204tos            ([ \t]*)
205eq             ([ \t]*+=[ \t]*)
206eqnl           ([ \t\n\r]*+=[ \t\n\r]*)
207
208/* %start START */
209
210%x header
211%x help
212%x libcmd
213%x libcmd2
214%x pdef
215%x phead
216%x poldhelp
217%x phelp
218%x pbody
219%x pstr
220%x pexample
221%x pestr
222%x string
223%x comment
224%x info
225%x category
226%x url
227%x version
228
229%%
230(\/\/[^\n]*)|(^#![^\n]*)|([ \t]) { }
231\/\/*      { old_state = YYSTATE; BEGIN(comment); }
232
233(info+{eqnl}+{quote}+{strings}+{quote}) {
234         yyless(4); old_state = YYSTATE; BEGIN(info);
235       }
236(category+{eqnl}+{quote}+{strings}+{quote}) {
237         yyless(8); old_state = YYSTATE; BEGIN(category);
238       }
239(url+{eqnl}+{quote}+{strings}+{quote}) {
240             if ( mode != GET_INFO )
241             {
242               #ifdef STANDALONE_PARSER
243               if (texinfo_out)
244               {
245                 char *c = yytext;
246                 printf("$url = \"");
247                 while ((*c != '\0') && (*c != '"')) c++;
248                 c++;
249                 while ((*c != '\0') && (*c != '"'))
250                 {
251                    if (*c != '\r') putchar(*c);
252                    c++;
253                 }
254                 printf("\";\n");
255               }
256               #endif
257             }
258       }
259
260(version+{eqnl}+{quote}+{strings}+{quote}) {
261             found_version++;
262             if ( mode != GET_INFO )
263             {
264               make_version(yytext,1);
265               #ifdef STANDALONE_PARSER
266               if (texinfo_out)
267               {
268                 char *c = libnamebuf;
269                 printf("$version = \"");
270                 while (*c != '\0')
271                 {
272                    if (*c  == '$' || *c  == '@') putchar('\\');
273                    if (*c != '\r') putchar(*c);
274                    if (*c  == '\\')
275                    {
276                      c++;
277                      if (*c != '"') putchar('\\');
278                    }
279                    else
280                      c++;
281                 }
282                 printf("\";\n");
283               }
284               else if (!category_out)
285                 printf("Version:%s;\n", libnamebuf);
286               #else
287               if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
288               text_buffer = omStrDup(libnamebuf);
289               omMarkAsStaticAddr(text_buffer);
290               #endif
291             }
292           }
293
294static     { p_static=TRUE; }
295
296(proc[ \t]+{name})|([ \t]proc[ \t]+{name}) {
297             char proc[256];
298             BEGIN(pdef);
299             found_proc_in_proc = 0;
300             proc[0]='\0';
301             sscanf( yytext, "%*[^p]proc %s", proc);
302             if(strlen(proc)<1) sscanf( yytext, "proc %s", proc);
303             #if YYLPDEBUG > 1
304             printf("Newlib:%s\n", newlib);
305             #endif
306             #ifdef STANDALONE_PARSER
307             if ( pi != NULL )
308             {
309               printpi(pi);
310               pi_clear(pi);
311             }
312             pi = (procinfo *)malloc(sizeof(procinfo));
313             iiInitSingularProcinfo(pi, newlib, proc, yylplineno,
314                                    current_pos(0), p_static);
315             #else /*STANDALONE_PARSER*/
316             if( mode == LOAD_LIB)
317             {
318               h0 = enterid( proc, 0 /*myynest*/, PROC_CMD,
319                                 &(IDPACKAGE(pl)->idroot), TRUE, !p_static);
320               if (h0==NULL) return(1);
321               if((!p_static) && autoexport)
322               {
323                  package save=currPack;
324                  currPack=basePack;
325                  h_top = enterid( proc, 0 /*myynest*/, PROC_CMD,
326                                 &(basePack->idroot), FALSE );
327                  currPack=save;
328                  if (h_top==NULL) return(1);
329               }
330               /* omCheckAddr(IDID(h0)); */
331               if (h0!=NULL)
332               {
333                 iiInitSingularProcinfo(IDPROC(h0), newlib, proc,
334                                yylplineno, current_pos(0),p_static);
335                 if ((!p_static) && (h_top != NULL) && autoexport)
336                 {
337                   if(IDPROC(h_top)!=NULL) piKill((procinfo *)IDPROC(h_top));
338                   IDPROC(h_top)=IDPROC(h0);
339                   IDPROC(h_top)->ref++;
340                 }
341                 IDPROC(h0)->pack=IDPACKAGE(pl);
342                 if (BVERBOSE(V_LOAD_PROC))
343                   Warn( "     proc '%s' registered", proc );
344               }
345               #endif /*STANDALONE_PARSER*/
346               SET_DEF_END(mode, pi, current_pos(yyleng+1));
347               #if YYLPDEBUG
348               if(lpverbose)
349               {
350                  printf("// PROCEDURE '%s' status: %s, ", proc,
351                      p_static ? "local" : "global");
352                  printf("starting at line %d,%d: definition end: %d (%d).\n",
353                      yylplineno, current_pos(0), (int)pi->data.s.def_end, brace1);
354               }
355               #endif
356               p_static=FALSE;
357             #ifndef STANDALONE_PARSER
358             }
359             #endif /*STANDALONE_PARSER*/
360           }
361example    {
362             BEGIN(pexample);
363             SET_EXAMPLE_START(mode, pi, yylplineno, current_pos(0));
364             #if YYLPDEBUG
365             if(lpverbose)
366             {
367                printf("//     EXAMPLE at line %d,%d (%d)\n", yylplineno,
368                    current_pos(0), brace1);
369             }
370             #endif
371           }
372
373LIB[ \t]+"\"" { quote++;
374             BEGIN(libcmd);
375           }
376
377LIB[ \t]*"(\"" { quote++; brace2++;
378             BEGIN(libcmd2);
379           }
380
381<header>({comment}+{tos}+{dolar}+Id:+{string}+[^\n]*)|({comment}+{tos}+{dolar}+Header:+{string}+[^\n]*) {
382             make_version(yytext, 0);
383             #if YYLPDEBUG > 1
384             printf("+(id)HEAD:%s\n", yytext);
385             #endif
386           }
387<header>(^{comment}+[^\n]*) {
388             #if YYLPDEBUG
389             printf("+(cmt)HEAD:%s\n", yytext);
390             #endif
391           }
392<header>(^#![^\n]*) {
393             #if YYLPDEBUG > 1
394             printf("-HEAD:%s\n", yytext);
395             #endif
396           }
397<header>^proc\  { yyless(0);
398             BEGIN(INITIAL);
399             yymore();
400           }
401<header>(info+{eqnl}+{quote})|(version+{eqnl}+{quote})|(category+{eqnl}+{quote})|(url+{eqnl}+{quote}) {
402             yyless(0);
403             *lib_style = NEW_LIBSTYLE;
404             BEGIN(INITIAL);
405             yymore();
406           }
407
408<header>^LIB[ \t]+"\""   { quote++;
409             BEGIN(libcmd);
410           }
411<header>^LIB[ \t]+"(\""   { quote++; brace2++;
412             BEGIN(libcmd2);
413           }
414<header>\n { yylplineno++; }
415<header>.  {
416             #if YYLPDEBUG > 1
417             printf(" HEAD:%s\n", yytext);
418             #endif
419             yyless(0);
420             BEGIN(help);
421           }
422<help>(^{comment}+[^\n]*)  {
423             #if YYLPDEBUG > 1
424             printf(" HELP:%s\n", yytext);
425             #endif
426             BEGIN(INITIAL); }
427<help>(^#![^\n]*) {
428             #if YYLPDEBUG > 1
429             printf(" HELP:%s\n", yytext);
430             #endif
431             BEGIN(INITIAL);
432           }
433<help>(info+{eqnl}+{quote})|(version+{eqnl}+{quote})|(category+{eqnl}+{quote})|(url+{eqnl}+{quote}) {
434             yyless(0);
435             *lib_style = NEW_LIBSTYLE;
436             BEGIN(INITIAL);
437             yymore();
438           }
439<help>^proc\  {
440             yyless(0);
441             //printf("2) proc found.\n");
442             BEGIN(INITIAL);
443             yymore();
444           }
445<help>^LIB[ \t]+"\""     { quote++;
446             BEGIN(libcmd);
447           }
448<help>^LIB[ \t]+"(\""     { quote++; brace2++;
449             BEGIN(libcmd2);
450           }
451
452<help>\n { yylplineno++; }
453<help>({tos}|{comment}+{fstring}) {
454             #if YYLPDEBUG
455             if(lpverbose>2) printf("--->%s<---\n", yytext);
456             #endif
457           }
458<help>.    {
459             found_oldhelp=1;
460             #if YYLPDEBUG > 1
461             printf("-HELP:%s\n", yytext);
462             #endif
463           }
464
465
466<libcmd>{string}"\""     { quote--;
467             yytext[yyleng-1] = '\0';
468             #ifndef STANDALONE_PARSER
469             if ( mode == LOAD_LIB )
470             {
471               library_stack->push(newlib, yytext);
472             }
473             #endif /* STANDALONE_PARSER */
474             #if YYLPDEBUG
475             if(lpverbose>1) printf("LIB:'%s'\n", yytext);
476             #endif
477             BEGIN(INITIAL);
478           }
479<libcmd2>{string}"\")"     { quote--; brace2--;
480             yytext[yyleng-1] = '\0';
481             #ifndef STANDALONE_PARSER
482             if ( mode == LOAD_LIB )
483             {
484               library_stack->push(newlib, yytext);
485             }
486             #endif /* STANDALONE_PARSER */
487             #if YYLPDEBUG
488             if(lpverbose>1) printf("LIB:'%s'\n", yytext);
489             #endif
490             BEGIN(INITIAL);
491           }
492
493<pdef>[ \t] { }
494<pdef>\(   {
495             brace2++;
496             #if YYLPDEBUG > 1
497             printf("%s", yytext);
498             #endif
499           }
500<pdef>\)   {
501             brace2--;
502             #if YYLPDEBUG > 1
503             printf(">%s<\n", yytext);
504             printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
505             #endif
506             if(brace2<=0)
507             {
508               #if YYLPDEBUG > 1
509               printf("BEGIN(phead){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
510               #endif
511               SET_DEF_END(mode, pi, current_pos(yyleng));
512               BEGIN(phead);
513             }
514           }
515<pdef>"{"  {
516             if(brace2>0)
517             {
518               #if YYLPDEBUG > 1
519               printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
520               #endif
521               yylp_errno = YYLP_DEF_BR2;
522               return(1);
523             }
524             else
525             {
526               brace1++; BEGIN(pbody);
527               if(lpverbose)
528                  printf("//     BODY at line %d,%d (%d)\n", yylplineno,
529                      current_pos(0), brace1);
530               SET_BODY_START(mode, pi, yylplineno, current_pos(0));
531             }
532           }
533<pdef>\n { yylplineno++;
534              if(brace2<=0)
535              {
536#if YYLPDEBUG > 1
537                printf("BEGIN(phead-2){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
538#endif
539                BEGIN(phead);
540              }
541            }
542<pdef>({comment}[^\n]*)  { }
543<pdef>\/\/*      { old_state = YYSTATE; BEGIN(comment); }
544<pdef>.    {
545             if(brace2<=0)
546             {
547               BEGIN(phead);
548               yyless(0);
549             }
550           }
551
552<phead>({tnl}+{quote}+{strings}+{escquote}+{tnl}+"{") {
553              #if YYLPDEBUG
554              if(lpverbose>2)printf("0-Len=%d;\n", yyleng);
555              #endif
556              if(check)
557              {
558                printf("Procedure %s has OLD-STYLE-HELP!\n",
559                       pi->procname);
560              }
561              SET_HELP_START(mode, pi, current_pos(0));
562              BEGIN(poldhelp);
563              yyless(0);
564           }
565<phead>({tnl}+{quote}+{strings}+{eos}) {
566              #if YYLPDEBUG
567              if(lpverbose>2)printf("1-Len=%d;\n", yyleng);
568              #endif
569              BEGIN(phelp);
570              yyless(0);
571           }
572<phead>{escstrings}+"{" {
573              if(check && yyleng>2)
574              {
575                printf("Procedure %s has OLD-STYLE-HELP!\n",
576                       pi->procname);
577              }
578              #if YYLPDEBUG
579              if(lpverbose>2 && yyleng>2)
580                 printf("2-Len=%d, %s;\n", yyleng, pi->procname);
581              #endif
582              SET_HELP_START(mode, pi, current_pos(0));
583              BEGIN(poldhelp);
584              yyless(0);
585           }
586<phead>.   { printf("[%s]", yytext); }
587
588<poldhelp>{escbrack} { }
589<poldhelp>"{" {
590                SET_HELP_END(mode, pi, current_pos(0));
591                brace1++; BEGIN(pbody);
592                if(lpverbose)
593                {
594                   printf("//     HELP from %d to %d\n",
595                       (int)pi->data.s.help_start, (int)pi->data.s.help_end);
596                   printf("//     BODY at line %d,%d (%d)\n", yylplineno,
597                       current_pos(0), brace1);
598                }
599#if YYLPDEBUG > 1
600                printf("BEGIN(pbody){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
601#endif
602                SET_BODY_START(mode, pi, yylplineno, current_pos(0));
603#if YYLPDEBUG > 1
604                printf("BODY at %d/%d", yylplineno, current_pos(0));
605#endif
606              }
607<poldhelp>\n  { yylplineno++; }
608<poldhelp>.   { }
609
610<phelp>{quote} {
611             old_state = YYSTATE;
612             BEGIN(string);
613             SET_HELP_START(mode, pi, current_pos(1));
614           }
615<phelp>{tos} {}
616<phelp>"{" {
617             brace1++; BEGIN(pbody);
618             if(lpverbose)
619             {
620                printf("//     HELP from %d to %d\n",
621                    (int)pi->data.s.help_start, (int)pi->data.s.help_end);
622                printf("//     BODY at line %d,%d (%d)\n", yylplineno,
623                    current_pos(0), brace1);
624             }
625             #if YYLPDEBUG > 1
626             printf("BEGIN(pbody){=%d, (=%d, [=%d\n", brace1, brace2, brace3);
627             #endif
628             SET_BODY_START(mode, pi, yylplineno, current_pos(0));
629             #if YYLPDEBUG > 1
630             printf("BODY at %d/%d", yylplineno, current_pos(0));
631             #endif
632           }
633<phelp>\n  { yylplineno++;}
634
635<pbody>({comment}[^\n]*) { }
636<pbody>{quote} { quote++; old_state = YYSTATE;
637                 BEGIN(string); /* printf("%s", yytext); */
638               }
639
640<pbody>proc+{tos}+{name}+{tnl}+{paramlist}+{tnl}+"{" {
641             if(check) printf("*** found 2 proc whithin procedure '%s'.\n",
642                          pi->procname);
643             yyless(yyleng-1);
644           }
645<pbody>proc+{tos}+{name}+{tnl}+"{" {
646             if(check) printf("*** found 1 proc whithin procedure '%s'.\n",
647                          pi->procname);
648             yyless(yyleng-1);
649           }
650<pbody>"{"     {
651                 brace1++;
652                 #if YYLPDEBUG > 1
653                 printf("line: %d, (%d)%s\n", yylplineno, brace1, yytext);
654                 #endif
655                }
656<pbody>"}"               {
657                           #if YYLPDEBUG > 1
658                           printf("line: %d, (%d)%s\n",
659                             yylplineno, brace1, yytext);
660                           #endif
661                           brace1--;
662                           if(brace2>0)
663                           {
664                             yylp_errno = YYLP_BODY_BR2;
665                             return(1);
666                           }
667                           if(brace3>0)
668                           {
669                             yylp_errno = YYLP_BODY_BR3;
670                             return(1);
671                           }
672                           if(brace1<=0)
673                           {
674                             SET_BODY_END(mode, pi, current_pos(yyleng));
675                             SET_PROC_END(mode, pi, current_pos(yyleng));
676                             #if YYLPDEBUG > 1
677                               printf("-%d\n", current_pos(0));
678                             #endif
679                             BEGIN(INITIAL);
680                           }
681                         }
682<pbody>"("               {
683                           brace2++; /* printf("%s", yytext); */
684                         }
685<pbody>")"               {
686                           brace2--; /* printf("%s", yytext); */
687                           if(brace2<0) {
688                             yylp_errno = YYLP_BODY_TMBR2;
689                             return(1);
690                           }
691                         }
692<pbody>"["               {
693                           brace3++; /* printf("%s", yytext); */
694                         }
695<pbody>"]"               {
696                           brace3--; /* printf("%s", yytext); */
697                           if(brace3<0) {
698                             yylp_errno = YYLP_BODY_TMBR3;
699                             return(1);
700                           }
701                         }
702<pbody>\n                { yylplineno++; }
703<pbody>.                 { }
704
705<info>{quote} {
706             quote++; BEGIN(string);
707             found_info++;
708             string_start = current_pos(yyleng);
709             *lib_style = NEW_LIBSTYLE;
710             last_cmd = LP_INFO;
711       }
712<info>\n { yylplineno++; }
713<info>.  { }
714
715<category>{quote} {
716             quote++; BEGIN(string);
717             found_cat++;
718             string_start = current_pos(yyleng);
719             *lib_style = NEW_LIBSTYLE;
720             last_cmd = LP_CATEGORY;
721       }
722<category>\n { yylplineno++; }
723<category>.  { }
724
725
726<string>"\""             { quote--;
727                           copy_string(mode);
728                           last_cmd = LP_NONE;
729                           if(old_state==phelp)
730                           {
731                              SET_HELP_END(mode, pi, current_pos(0));
732                           }
733                           BEGIN(old_state); /* printf("%s", yytext); */
734                         }
735<string>(\\\\)|(\\\")    { if (old_state == phelp) IncrCheckSum(*yytext);}
736<string>\n               { yylplineno++; if (old_state == phelp) IncrCheckSum('\n');}
737<string>.                { if (old_state == phelp) IncrCheckSum(*yytext);}
738
739<pexample>(\/\/[^\n]*)  { }
740<pexample>"\""           { quote++; old_state = YYSTATE;
741                           BEGIN(string); /* printf("%s", yytext); */
742                         }
743<pexample>"{"            {
744                           brace1++; /* printf("(%d)%s", brace1, yytext); */
745                         }
746<pexample>"}"            {
747                           brace1--; /* printf("(%d)%s", brace1, yytext); */
748                           if(brace1<=0) {
749                             if(brace2>0) { yylp_errno=YYLP_EX_BR2; return(1); }
750                             if(brace3>0) { yylp_errno=YYLP_EX_BR3; return(1); }
751                             BEGIN(INITIAL);
752                             SET_PROC_END(mode, pi, current_pos(yyleng));
753                           }
754                         }
755<pexample>"("            {
756                           brace2++; /* printf("%s", yytext); */
757                         }
758<pexample>")"            {
759                           brace2--; /* printf("%s", yytext); */
760                         }
761<pexample>"["            {
762                           brace3++; /* printf("%s", yytext); */
763                         }
764<pexample>"]"            {
765                           brace3--; /* printf("%s", yytext); */
766                         }
767<pexample>\n             { yylplineno++; }
768<pexample>.              { }
769
770<pestr>"\""              { quote--;
771                           BEGIN(pexample); /* printf("%s", yytext); */
772                         }
773<pestr>\\\\              { }
774<pestr>\\\"              { }
775<pestr>\n                { yylplineno++; }
776<pestr>.                 { }
777
778<comment>\*\/            { BEGIN(old_state); }
779<comment>\n              { yylplineno++; }
780<comment>.               { }
781
782\n                       { yylplineno++; }
783\r                       { }
784;                        { p_static = FALSE;
785                            #if YYLPDEBUG > 1
786                            printf("%s", yytext);
787                            #endif
788                         }
789.                        { p_static = FALSE;
790                           yylp_errno = YYLP_BAD_CHAR;
791                           #ifdef STANDALONE_PARSER
792                           printf("[%d]", *yytext);
793                           #else
794                           if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
795                           text_buffer = omStrDup(yytext);
796                           omMarkAsStaticAddr(text_buffer);
797                           #endif
798                           #if YYLPDEBUG > 1
799                             printf("[%s]", yytext);
800                           #endif
801                           return(1);
802                         }
803
804%%
805
806int current_pos(int i)
807{
808  return(i+offset+(int)(yytext-yylp_buffer_start));
809}
810
811int libread(FILE* f, char* buf, int max_size)
812{ int rc;
813
814  offset = ftell(f);
815  rc  = myfread( buf, 1, max_size, f );
816  #if YYLPDEBUG >2
817    printf("fread: %d of %d\n", rc, max_size);
818  #endif
819  yylp_buffer_start = buf;
820  return rc;
821}
822
823extern "C" {
824  int yylpwrap()
825  {
826    //printf("======================= YYWRAP ====================\n");
827    if(brace1>0) { yylp_errno=YYLP_MISS_BR1; }
828    if(brace2>0) { yylp_errno=YYLP_MISS_BR2; }
829    if(brace3>0) { yylp_errno=YYLP_MISS_BR3; }
830    if(quote>0) { yylp_errno=YYLP_MISSQUOT; }
831    //printf("{=%d, (=%d, [=%d\n", brace1, brace2, brace3);
832    if(feof(yyin)) return 1; else return 0;
833  }
834}
835
836void reinit_yylp()
837{
838   brace1 = 0;
839   brace2 = 0;
840   brace3 = 0;
841   quote  = 0;
842   yy_init=1;
843   yy_delete_buffer(YY_CURRENT_BUFFER);
844}
845
846void make_version(char *p,int what)
847{
848  char ver[10];
849  char date[16];
850  ver[0]='?'; ver[1]='.'; ver[2]='?'; ver[3]='\0';
851  date[0]='?'; date[1]='\0';
852  if(what) sscanf(p,"%*[^=]= %*s %*s %10s %16s",ver,date);
853  else sscanf(p,"// %*s %*s %10s %16s",ver,date);
854  strcpy(libnamebuf,"(");
855  strcat(libnamebuf,ver);
856  strcat(libnamebuf,",");
857  strcat(libnamebuf,date);
858  strcat(libnamebuf,")");
859  if(what && strcmp(libnamebuf, "(?.?,?)")==0)
860  {
861    sscanf(p,"%*[^\"]\"%[^\"]\"",libnamebuf);
862  }
863  //printf("ID=(%d)%s; \n", what, p);
864}
865
866void copy_string(lp_modes mode)
867{
868#ifdef STANDALONE_PARSER
869  if ((texinfo_out
870     && (last_cmd == LP_INFO || last_cmd == LP_CATEGORY || last_cmd == LP_URL))
871  || (category_out && last_cmd == LP_CATEGORY)
872)
873  {
874    long current_location = ftell(yylpin), i = string_start, quote = 0;
875    char c;
876    if (texinfo_out)
877    {
878     if (last_cmd == LP_INFO)
879     {
880       printf("$info = <<EOT;\n");
881     }
882     else if (last_cmd == LP_URL)
883     {
884       printf("$url = <<EOT;\n");
885     }
886     else
887     {
888       printf("$category = <<EOT;\n");
889     }
890    }
891    fseek (yylpin, i, SEEK_SET);
892    while (i< current_location)
893    {
894      c = fgetc(yylpin);
895      if (c == '\\')
896      {
897        quote = (! quote);
898      }
899      else if (c == '"')
900      {
901        if (! quote) break;
902      }
903      else
904        quote = 0;
905      if (c == '@' || c == '$') putchar('\\');
906      if (c != '\r') putchar(c);
907      i++;
908    }
909    if (category_out) exit(0);
910    fseek (yylpin, current_location, SEEK_SET);
911    printf("\nEOT\n");
912  }
913#else
914  if((last_cmd == LP_INFO)&&(mode == GET_INFO))
915  {
916    int i, offset=0;
917    long current_location = ftell(yylpin);
918    int len = (int)(current_pos(0) - string_start);
919    fseek(yylpin, string_start, SEEK_SET);
920    if (text_buffer!=NULL) omFree((ADDRESS)text_buffer);
921    text_buffer = (char *)omAlloc(len+2);
922    omMarkAsStaticAddr(text_buffer);
923    myfread(text_buffer, len, 1, yylpin);
924    fseek(yylpin, current_location, SEEK_SET);
925    text_buffer[len]='\0';
926    offset=0;
927    for(i=0;i<=len; i++)
928    {
929      if(text_buffer[i]=='\\' &&
930         (text_buffer[i+1]=='\"' || text_buffer[i+1]=='{' ||
931          text_buffer[i+1]=='}' || text_buffer[i+1]=='\\'))
932      {
933        i++;
934        offset++;
935      }
936      if(offset>0) text_buffer[i-offset] = text_buffer[i];
937    }
938  }
939#endif /* STANDALONE_PARSER */
940}
941
942void print_init()
943{
944   printf("Init=%d\n", yy_init);
945}
946
947void print_version(lp_modes mode, char *p)
948{
949#ifdef STANDALONE_PARSER
950  //printf("loading %s%s", p, libnamebuf);
951#else
952  if ( mode == LOAD_LIB )
953  {
954    if (BVERBOSE(V_LOAD_LIB) && p!=NULL ) Print(" %s...", p);
955       //Warn( "loading %s%s", p, libnamebuf);
956  }
957#endif
958}
959
960#ifdef STANDALONE_PARSER
961int main( int argc, char *argv[] )
962{
963  lib_style_types lib_style;
964  main_init(argc, argv);
965  if(yyin == NULL)
966  {
967    fprintf(stderr, "No library found to parse.\n");
968    return 1;
969  }
970  if (! (texinfo_out || category_out))
971  {
972    if(lpverbose)printf("Verbose level=%d\n", lpverbose);
973    if(check)printf("Reporting most possible annomalies.\n");
974    if(lpverbose||check)printf("\n");
975
976    printf( "  %-15s  %20s      %s,%s    %s,%s     %s,%s\n", "Library",
977            "function", "line", "start-eod", "line", "body-eob",
978            "line", "example-eoe");
979  }
980  yylplex(argv[0], argv[0], &lib_style,NULL);
981  if(yylp_errno)
982  {
983    printf("ERROR occurred: [%d] ", yylp_errno);
984    printf(yylp_errlist[yylp_errno], yylplineno);
985    printf("\n");
986  }
987  else if(pi!=NULL) printpi(pi);
988  if (texinfo_out)
989    printf("1;");
990  return 0;
991}
992
993#endif /* STANDALONE_PARSER */
994#endif /* HAVE_LIBPARSE */
Note: See TracBrowser for help on using the repository browser.