2 * j2t.lex : An example of the use (possibly abuse!)
7 #define MAX_STATES 1024
11 #define CHAPTER "@chapter"
12 #define SECTION "@section"
13 #define SSECTION "@subsection"
14 #define SSSECTION "@subsubsection"
16 int states[MAX_STATES];
19 int need_closing = FALSE;
21 char buffer[YY_BUF_SIZE];
26 * set up the head of the *.texinfo file the program
27 * will produce. This is a standard texinfo header.
30 void print_header(void)
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");
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");
54 printf("\n@c ==========================================================\n");
56 printf("@unnumbered Preface\n");
57 printf("@c *******\n");
61 * create the tail of the texinfo file produced.
64 void print_trailer(void)
66 printf("\n@c ==========================================================\n");
67 printf("@contents\n"); /* print the table of contents */
72 * write an underline under a section
73 * or chapter so we can find it later.
76 void write_underline(int len, int space, char ch)
82 for(loop=3; loop<space; loop++){
93 * check for texinfo special characters
97 char *check_and_convert(char *string)
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];
108 buffer[buffpos++] = string[loop];
111 buffer[buffpos] = '\0';
116 * write out a chapter,section, or subsection
120 void write_block_header(char *type)
125 (void)check_and_convert(yytext);
126 len = strlen(buffer);
127 for(loop=0; buffer[loop] != '\n';loop++)
130 printf("%s %s\n",type,buffer);
131 write_underline(strlen(buffer),strlen(type)+1,'*');
137 * the flex description starts here
140 %x HEADING EXAMPLE ENUM EXAMPLE2
146 ^#[^#]*"#" /* skip the header & trailer */
147 /* chapters have asterisks under them
148 * and are terminated by a colon
150 ^[^\n:]+\n[*]+\n write_block_header(CHAPTER);
152 ^"= "[A-Z]" ="\n"="* { /* we create a seciton for each category */
153 if(need_closing == TRUE){
154 printf("@end table\n\n\n");
157 write_block_header(SECTION);
158 printf("\n\n@table @b\n");
161 "Examples:"[^\.]+ ECHO;
163 "*"[^*\n]+"*" { /* @emph{}(emphasized) text */
164 yytext[yyleng-1] = '\0';
165 (void)check_and_convert(&yytext[1]);
166 printf("@i{%s}",buffer);
169 "{{"[^}]+"}}" { /* special emphasis */
170 yytext[yyleng-2] = '\0';
171 (void)check_and_convert(&yytext[2]);
172 printf("@b{%s}",buffer);
175 "{"[^}]+"}" { /* special emphasis */
176 yytext[yyleng-1] = '\0';
177 (void)check_and_convert(&yytext[1]);
178 printf("@b{%s}",buffer);
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("@}");
187 * reproduce @example code
190 ":"\n+[^\n0-9*]+\n" "[^ ] {
195 printf(":\n\n@example \n");
196 strcpy(buffer,yytext);
197 len = strlen(buffer);
199 for(loop=len; loop > 0;loop--){
200 if(buffer[loop] == '\n')
207 states[statep] = EXAMPLE2;
210 <EXAMPLE,EXAMPLE2>^\n {
211 printf("@end example\n\n");
213 BEGIN(states[statep]);
217 * repoduce @enumerate lists
220 ":"\n+[ \t]*[0-9]+"." {
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')
233 states[statep] = ENUM;
237 <ENUM>"@" printf("@@");
238 <ENUM>":"\n+" "[^0-9] {
239 printf(":\n\n@example\n");
241 states[statep] = EXAMPLE;
246 <ENUM>\n[ \t]+[0-9]+"." {
247 printf("\n\n@item ");
250 <ENUM>\n\n\n[ \t]+[^0-9] {
251 printf("\n\n@end enumerate\n\n");
253 BEGIN(states[statep]);
257 * reproduce one kind of @itemize list
264 printf(":\n\n@itemize @bullet \n");
267 states[statep] = LITEM2;
271 (void)check_and_convert(&yytext[1]);
272 buffer[strlen(buffer)-1]='\0';
273 printf("@item @b{%s:}\n",buffer);
276 <LITEM2>\n\n\n+[^:\n] {
277 printf("\n\n@end itemize\n\n");
280 BEGIN(states[statep]);
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.
289 :[\n]+"Version"[^:\n*]+":" {
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')
302 states[statep] = LITEM;
306 (void)check_and_convert(yytext);
307 buffer[strlen(buffer)-1]='\0';
308 printf("@item @b{%s}\n\n",buffer);
311 <LITEM>^[^:\n]+\n\n[^:\n]+\n {
314 strcpy(buffer,yytext);
315 for(loop=0; buffer[loop] != '\n'; loop++);
317 printf("%s\n",buffer);
318 printf("@end itemize\n\n");
319 printf("%s",&buffer[loop+1]);
321 BEGIN(states[statep]);
325 * reproduce @itemize @bullet lists
332 printf(":\n\n@itemize @bullet \n");
333 len = strlen(buffer);
334 for(loop=0; loop < len;loop++){
335 if(buffer[loop] == '\n')
338 yyless((len-loop)+2);
340 states[statep] = BITEM;
347 states[statep] = BITEM_ITEM;
350 <BITEM>"@" printf("@@");
352 printf("@end itemize\n\n");
354 BEGIN(states[statep]);
357 printf(" @b{%s}\n\n",check_and_convert(yytext));
361 BEGIN(states[statep]);
365 * recreate @chapter, @section etc.
369 (void)check_and_convert(&yytext[1]);
371 states[statep] = HEADING;
375 printf("@item @b{%s}\n",buffer);
376 write_underline(strlen(buffer),6,'~');
378 BEGIN(states[statep]);
381 if(need_closing == TRUE){
382 printf("@end table\n\n\n");
383 need_closing = FALSE;
385 printf("@chapter %s\n",buffer);
386 write_underline(strlen(buffer),9,'*');
388 BEGIN(states[statep]);
391 if(need_closing == TRUE){
392 printf("@end table\n\n\n");
393 need_closing = FALSE;
395 printf("@section %s\n",buffer);
396 write_underline(strlen(buffer),9,'=');
398 BEGIN(states[statep]);
400 <HEADING>"@" printf("@@");
402 if(need_closing == TRUE){
403 printf("@end table\n\n\n");
404 need_closing = FALSE;
406 printf("@subsection %s\n",buffer);
407 write_underline(strlen(buffer),12,'-');
409 BEGIN(states[statep]);
413 * recreate @example text
417 printf("@example\n");
419 states[statep] = EXAMPLE;
431 int main(int argc, char *argv[])