1/*
2 * j2t.lex : An example of the use (possibly abuse!)
3 *           of start states.
4 */
5
6%{
7#define MAX_STATES 1024
8#define TRUE  1
9#define FALSE 0
10
11#define CHAPTER   "@chapter"
12#define SECTION   "@section"
13#define SSECTION  "@subsection"
14#define SSSECTION "@subsubsection"
15
16int  states[MAX_STATES];
17int  statep = 0;
18
19int  need_closing = FALSE;
20
21char buffer[YY_BUF_SIZE];
22
23extern char *yytext;
24
25/*
26 * set up the head of the *.texinfo file the program
27 * will produce. This is a standard texinfo header.
28 */
29
30void print_header(void)
31{
32   printf("\\input texinfo @c -*-texinfo-*-\n");
33   printf("@c           %c**start of header\n",'%');
34   printf("@setfilename       jargon.info\n");
35   printf("@settitle          The New Hackers Dictionary\n");
36   printf("@synindex          fn cp\n");
37   printf("@synindex          vr cp\n");
38   printf("@c           %c**end of header\n",'%');
39   printf("@setchapternewpage odd\n");
40   printf("@finalout\n");
41   printf("@c @smallbook\n");
42   printf("\n");
43   printf("@c ==========================================================\n\n");
44   printf("@c  This file was produced by j2t. Any mistakes are *not* the\n");
45   printf("@c  fault of the jargon file editors.                        \n");
46   printf("@c ==========================================================\n\n");
47   printf("@titlepage\n");
48   printf("@title     The New Hackers Dictionary\n");
49   printf("@subtitle  Version 2.9.10\n");
50   printf("@subtitle  Generated by j2t\n");
51   printf("@author    Eric S. Raymond, Guy L. Steel, Mark Crispin et al.\n");
52   printf("@end titlepage\n");
53   printf("@page\n");
54   printf("\n@c ==========================================================\n");
55   printf("\n\n");
56   printf("@unnumbered Preface\n");
57   printf("@c          *******\n");
58}
59
60/*
61 * create the tail of the texinfo file produced.
62 */
63
64void print_trailer(void)
65{
66   printf("\n@c ==========================================================\n");
67   printf("@contents\n");   /* print the table of contents */
68   printf("@bye\n\n");
69}
70
71/*
72 * write an underline under a section
73 * or chapter so we can find it later.
74 */
75
76void write_underline(int len, int space, char ch)
77{
78  int loop;
79
80  printf("@c ");
81
82  for(loop=3; loop<space; loop++){
83    printf(" ");
84  }
85
86  while(len--){
87    printf("%c",ch);
88  }
89  printf("\n\n");
90}
91
92/*
93 * check for texinfo special characters
94 * and escape them
95 */
96
97char *check_and_convert(char *string)
98{
99  int  buffpos = 0;
100  int  len,loop;
101
102  len = strlen(string);
103  for(loop=0; loop<len; loop++){
104    if(string[loop] == '@' || string[loop] == '{' || string[loop] == '}'){
105      buffer[buffpos++] = '@';
106      buffer[buffpos++] = string[loop];
107    } else {
108      buffer[buffpos++] = string[loop];
109    }
110  }
111  buffer[buffpos] = '\0';
112  return(buffer);
113}
114
115/*
116 * write out a chapter,section, or subsection
117 * header
118 */
119
120void write_block_header(char *type)
121{
122  int loop;
123  int len;
124
125  (void)check_and_convert(yytext);
126  len = strlen(buffer);
127  for(loop=0; buffer[loop] != '\n';loop++)
128         ;
129  buffer[loop] = '\0';
130  printf("%s %s\n",type,buffer);
131  write_underline(strlen(buffer),strlen(type)+1,'*');
132}
133
134%}
135
136/*
137 * the flex description starts here
138 */
139
140%x HEADING EXAMPLE ENUM EXAMPLE2
141%x BITEM BITEM_ITEM
142%s LITEM LITEM2
143
144%%
145
146^#[^#]*"#"  /* skip the header & trailer */
147                     /* chapters have asterisks under them
148                      * and are terminated by a colon
149                      */
150^[^\n:]+\n[*]+\n      write_block_header(CHAPTER);
151
152^"= "[A-Z]" ="\n"="*  { /* we create a seciton for each category */
153                        if(need_closing == TRUE){
154                          printf("@end table\n\n\n");
155                        }
156                        need_closing = TRUE;
157                        write_block_header(SECTION);
158                        printf("\n\n@table @b\n");
159                      }
160
161"Examples:"[^\.]+     ECHO;
162
163"*"[^*\n]+"*"         { /* @emph{}(emphasized) text */
164                        yytext[yyleng-1] = '\0';
165                        (void)check_and_convert(&yytext[1]);
166                        printf("@i{%s}",buffer);
167                      }
168
169"{{"[^}]+"}}"         { /* special emphasis */
170                        yytext[yyleng-2] = '\0';
171                        (void)check_and_convert(&yytext[2]);
172                        printf("@b{%s}",buffer);
173                      }
174
175"{"[^}]+"}"           { /* special emphasis */
176                        yytext[yyleng-1] = '\0';
177                        (void)check_and_convert(&yytext[1]);
178                        printf("@b{%s}",buffer);
179                      }
180
181 /* escape some special texinfo characters */
182<INITIAL,LITEM,LITEM2,BITEM,ENUM,EXAMPLE,EXAMPLE2>"@"  printf("@@");
183<INITIAL,LITEM,LITEM2,BITEM,ENUM,EXAMPLE,EXAMPLE2>"{"  printf("@{");
184<INITIAL,LITEM,LITEM2,BITEM,ENUM,EXAMPLE,EXAMPLE2>"}"  printf("@}");
185
186 /*
187  * reproduce @example code
188  */
189
190":"\n+[^\n0-9*]+\n"     "[^ ]   {
191                        int loop;
192                        int len;
193                        int cnt;
194
195                        printf(":\n\n@example \n");
196                        strcpy(buffer,yytext);
197                        len = strlen(buffer);
198                        cnt = 0;
199                        for(loop=len; loop > 0;loop--){
200                          if(buffer[loop] == '\n')
201                             cnt++;
202                          if(cnt == 2)
203                              break;
204                        }
205                        yyless(loop+1);
206                        statep++;
207                        states[statep] = EXAMPLE2;
208                        BEGIN(EXAMPLE2);
209                      }
210<EXAMPLE,EXAMPLE2>^\n  {
211                      printf("@end example\n\n");
212                      statep--;
213                      BEGIN(states[statep]);
214                    }
215
216 /*
217  * repoduce @enumerate lists
218  */
219
220":"\n+[ \t]*[0-9]+"."   {
221                      int loop;
222                      int len;
223
224                      printf(":\n\n@enumerate \n");
225                      strcpy(buffer,yytext);
226                      len = strlen(buffer);
227                      for(loop=len; loop > 0;loop--){
228                        if(buffer[loop] == '\n')
229                           break;
230                      }
231                      yyless(loop);
232                      statep++;
233                      states[statep] = ENUM;
234                      BEGIN(ENUM);
235                    }
236
237<ENUM>"@"           printf("@@");
238<ENUM>":"\n+"     "[^0-9]    {
239                    printf(":\n\n@example\n");
240                    statep++;
241                    states[statep] = EXAMPLE;
242                    BEGIN(EXAMPLE);
243                  }
244
245
246<ENUM>\n[ \t]+[0-9]+"." {
247                    printf("\n\n@item ");
248                   }
249<ENUM>^[^ ] |
250<ENUM>\n\n\n[ \t]+[^0-9] {
251                    printf("\n\n@end enumerate\n\n");
252                    statep--;
253                    BEGIN(states[statep]);
254                  }
255
256 /*
257  * reproduce one kind of @itemize list
258  */
259
260":"\n+":"         {
261                    int loop;
262                    int len;
263
264                    printf(":\n\n@itemize @bullet \n");
265                    yyless(2);
266                    statep++;
267                    states[statep] = LITEM2;
268                    BEGIN(LITEM2);
269                  }
270<LITEM2>^":".+":" {
271                    (void)check_and_convert(&yytext[1]);
272                    buffer[strlen(buffer)-1]='\0';
273                    printf("@item @b{%s:}\n",buffer);
274                  }
275
276<LITEM2>\n\n\n+[^:\n] {
277                    printf("\n\n@end itemize\n\n");
278                    ECHO;
279                    statep--;
280                    BEGIN(states[statep]);
281                  }
282
283 /*
284  * create a list out of the revision history part.
285  * We need the "Version" for this because it
286  * clashes with other rules otherwise.
287  */
288
289:[\n]+"Version"[^:\n*]+":" {
290                    int loop;
291                    int len;
292
293                    printf(":\n\n@itemize @bullet \n");
294                    strcpy(buffer,yytext);
295                    len = strlen(buffer);
296                    for(loop=len; loop > 0;loop--){
297                      if(buffer[loop] == '\n')
298                         break;
299                    }
300                    yyless(loop);
301                    statep++;
302                    states[statep] = LITEM;
303                    BEGIN(LITEM);
304                  }
305<LITEM>^.+":"     {
306                    (void)check_and_convert(yytext);
307                    buffer[strlen(buffer)-1]='\0';
308                    printf("@item @b{%s}\n\n",buffer);
309                  }
310
311<LITEM>^[^:\n]+\n\n[^:\n]+\n  {
312                    int loop;
313
314                    strcpy(buffer,yytext);
315                    for(loop=0; buffer[loop] != '\n'; loop++);
316                    buffer[loop] = '\0';
317                    printf("%s\n",buffer);
318                    printf("@end itemize\n\n");
319                    printf("%s",&buffer[loop+1]);
320                    statep--;
321                    BEGIN(states[statep]);
322                  }
323
324 /*
325  * reproduce @itemize @bullet lists
326  */
327
328":"\n[ ]*"*"      {
329                    int loop;
330                    int len;
331
332                    printf(":\n\n@itemize @bullet \n");
333                    len = strlen(buffer);
334                    for(loop=0; loop < len;loop++){
335                      if(buffer[loop] == '\n')
336                         break;
337                    }
338                    yyless((len-loop)+2);
339                    statep++;
340                    states[statep] = BITEM;
341                    BEGIN(BITEM);
342                  }
343
344<BITEM>^" "*"*"   {
345                    printf("@item");
346                    statep++;
347                    states[statep] = BITEM_ITEM;
348                    BEGIN(BITEM_ITEM);
349                  }
350<BITEM>"@"          printf("@@");
351<BITEM>^\n        {
352                    printf("@end itemize\n\n");
353                    statep--;
354                    BEGIN(states[statep]);
355                  }
356<BITEM_ITEM>[^\:]* {
357                     printf(" @b{%s}\n\n",check_and_convert(yytext));
358                   }
359<BITEM_ITEM>":"   {
360                    statep--;
361                    BEGIN(states[statep]);
362                  }
363
364 /*
365  * recreate @chapter, @section etc.
366  */
367
368^:[^:]*           {
369                    (void)check_and_convert(&yytext[1]);
370                    statep++;
371                    states[statep] = HEADING;
372                    BEGIN(HEADING);
373                  }
374<HEADING>:[^\n]   {
375                    printf("@item @b{%s}\n",buffer);
376                    write_underline(strlen(buffer),6,'~');
377                    statep--;
378                    BEGIN(states[statep]);
379                  }
380<HEADING>:\n"*"*  {
381                    if(need_closing == TRUE){
382                      printf("@end table\n\n\n");
383                      need_closing = FALSE;
384                    }
385                    printf("@chapter %s\n",buffer);
386                    write_underline(strlen(buffer),9,'*');
387                    statep--;
388                    BEGIN(states[statep]);
389                  }
390<HEADING>:\n"="*  {
391                    if(need_closing == TRUE){
392                     printf("@end table\n\n\n");
393                      need_closing = FALSE;
394                    }
395                    printf("@section %s\n",buffer);
396                    write_underline(strlen(buffer),9,'=');
397                    statep--;
398                    BEGIN(states[statep]);
399                  }
400<HEADING>"@"        printf("@@");
401<HEADING>:\n"-"*  {
402                    if(need_closing == TRUE){
403                      printf("@end table\n\n\n");
404                      need_closing = FALSE;
405                    }
406                    printf("@subsection %s\n",buffer);
407                    write_underline(strlen(buffer),12,'-');
408                    statep--;
409                    BEGIN(states[statep]);
410                  }
411
412 /*
413  * recreate @example text
414  */
415
416^"     "          {
417                    printf("@example\n");
418                    statep++;
419                    states[statep] = EXAMPLE;
420                    BEGIN(EXAMPLE);
421                  }
422<EXAMPLE>^"     "
423.                 ECHO;
424
425%%
426
427/*
428 * initialise and go.
429 */
430
431int main(int argc, char *argv[])
432{
433  states[0] = INITIAL;
434  statep    = 0;
435  print_header();
436  yylex();
437  print_trailer();
438  return(0);
439}
440
441
442
443