Remove UXPDS support
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / compression / sgml.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* A lexical scanner generated by flex */
24
25 /* scanner skeleton version:
26  * $XConsortium: sgml.cc /main/5 1996/08/05 17:05:53 drk $
27  */
28
29 #define FLEX_SCANNER
30
31 #include <stdio.h>
32
33
34 /* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */
35 #ifdef c_plusplus
36 #ifndef __cplusplus
37 #define __cplusplus
38 #endif
39 #endif
40
41
42 #ifdef __cplusplus
43
44 #include <stdlib.h>
45
46 #if defined(__osf__) || defined(linux) || defined(CSRG_BASED) || defined(sun)
47 #include <unistd.h>
48 #else
49 #if !defined(USL)
50 #include <osfcn.h>
51 #endif
52 #endif
53
54 /* use prototypes in function declarations */
55 #define sgml_USE_PROTOS
56
57 /* the "const" storage-class-modifier is valid */
58 #define sgml_USE_CONST
59
60 #else   /* ! __cplusplus */
61
62 #ifdef __STDC__
63
64 #ifdef __GNUC__
65 #include <stddef.h>
66 void *malloc( size_t );
67 void free( void* );
68 #else
69 #include <stdlib.h>
70 #endif  /* __GNUC__ */
71
72 #define sgml_USE_PROTOS
73 #define sgml_USE_CONST
74
75 #endif  /* __STDC__ */
76 #endif  /* ! __cplusplus */
77
78
79 #ifdef __TURBOC__
80 #define sgml_USE_CONST
81 #endif
82
83
84 #ifndef sgml_USE_CONST
85 #define const
86 #endif
87
88
89 #ifdef sgml_USE_PROTOS
90 #define sgml_PROTO(proto) proto
91 #else
92 #define sgml_PROTO(proto) ()
93 /* we can't get here if it's an ANSI C compiler, or a C++ compiler,
94  * so it's got to be a K&R compiler, and therefore there's no standard
95  * place from which to include these definitions
96  */
97 char *malloc();
98 int free();
99 int read();
100 #endif
101
102
103 /* amount of stuff to slurp up with each read */
104 #ifndef sgml_READ_BUF_SIZE
105 #define sgml_READ_BUF_SIZE 8192
106 #endif
107
108 /* returned upon end-of-file */
109 #define sgml_END_TOK 0
110
111 /* copy whatever the last rule matched to the standard output */
112
113 /* cast to (char *) is because for 8-bit chars, sgmltext is (unsigned char *) */
114 /* this used to be an fputs(), but since the string might contain NUL's,
115  * we now use fwrite()
116  */
117 #define ECHO (void) fwrite( (char *) sgmltext, sgmlleng, 1, sgmlout )
118
119 /* gets input and stuffs it into "buf".  number of characters read, or sgml_NULL,
120  * is returned in "result".
121  */
122 #define sgml_INPUT(buf,result,max_size) \
123         if ( (result = read( fileno(sgmlin), (char *) buf, max_size )) < 0 ) \
124             sgml_FATAL_ERROR( "read() in flex scanner failed" );
125 #define sgml_NULL 0
126
127 /* no semi-colon after return; correct usage is to write "sgmlterminate();" -
128  * we don't want an extra ';' after the "return" because that will cause
129  * some compilers to complain about unreachable statements.
130  */
131 #define sgmlterminate() return ( sgml_NULL )
132
133 /* report a fatal error */
134
135 /* The funky do-while is used to turn this macro definition into
136  * a single C statement (which needs a semi-colon terminator).
137  * This avoids problems with code like:
138  *
139  *      if ( something_happens )
140  *              sgml_FATAL_ERROR( "oops, the something happened" );
141  *      else
142  *              everything_okay();
143  *
144  * Prior to using the do-while the compiler would get upset at the
145  * "else" because it interpreted the "if" statement as being all
146  * done when it reached the ';' after the sgml_FATAL_ERROR() call.
147  */
148
149 #define sgml_FATAL_ERROR(msg) \
150         do \
151                 { \
152                 (void) fputs( msg, stderr ); \
153                 (void) putc( '\n', stderr ); \
154                 exit( 1 ); \
155                 } \
156         while ( 0 )
157
158 /* default sgmlwrap function - always treat EOF as an EOF */
159 #define sgmlwrap() 1
160
161 /* enter a start condition.  This macro really ought to take a parameter,
162  * but we do it the disgusting crufty way forced on us by the ()-less
163  * definition of BEGIN
164  */
165 #define BEGIN sgml_start = 1 + 2 *
166
167 /* action number for EOF rule of a given start state */
168 #define sgml_STATE_EOF(state) (sgml_END_OF_BUFFER + state + 1)
169
170 /* special action meaning "start processing a new file" */
171 #define sgml_NEW_FILE \
172         do \
173                 { \
174                 sgml_init_buffer( sgml_current_buffer, sgmlin ); \
175                 sgml_load_buffer_state(); \
176                 } \
177         while ( 0 )
178
179 /* default declaration of generated scanner - a define so the user can
180  * easily add parameters
181  */
182 #define sgml_DECL int sgmllex sgml_PROTO(( void )) 
183
184 /* code executed at the end of each rule */
185 #define sgml_BREAK break;
186
187 #define sgml_END_OF_BUFFER_CHAR 0
188
189 #ifndef sgml_BUF_SIZE
190 #define sgml_BUF_SIZE (sgml_READ_BUF_SIZE * 2) /* size of default input buffer */
191 #endif
192
193 typedef struct sgml_buffer_state *sgml_BUFFER_STATE;
194
195 #define sgml_CHAR unsigned char
196 # line 1 "sgml.l"
197 #define INITIAL 0
198 /*
199 * $XConsortium: sgml.cc /main/5 1996/08/05 17:05:53 drk $
200 *
201 * Copyright (c) 1993 HAL Computer Systems International, Ltd.
202 * All rights reserved.  Unpublished -- rights reserved under
203 * the Copyright Laws of the United States.  USE OF A COPYRIGHT
204 * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
205 * OR DISCLOSURE.
206
207 * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
208 * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
209 * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
210 * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
211 * INTERNATIONAL, LTD.
212
213 *                         RESTRICTED RIGHTS LEGEND
214 * Use, duplication, or disclosure by the Government is subject
215 * to the restrictions as set forth in subparagraph (c)(l)(ii)
216 * of the Rights in Technical Data and Computer Software clause
217 * at DFARS 252.227-7013.
218 *
219 *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
220 *                  1315 Dell Avenue
221 *                  Campbell, CA  95008
222
223 */
224 # line 37 "sgml.l"
225
226 #include "compression/trie.h"
227 #include "compression/abs_agent.h"
228
229
230 static unsigned char sgmlbuf[LBUFSIZ];
231 static int sgmlbuf_sz = LBUFSIZ;
232 static int sgmlbuffed = 0;
233
234               
235 #undef sgmlwrap
236 int sgmlwrap();
237
238 #undef sgml_INPUT
239 #define sgml_INPUT(buf,result,max_size) \
240    { \
241       result = (*fill_buf_func)((unsigned char*)buf,max_size); \
242    }
243
244
245 # line 59 "sgml.l"
246
247 /* done after the current pattern has been matched and before the
248  * corresponding action - sets up sgmltext
249  */
250 #define sgml_DO_BEFORE_ACTION \
251         sgmltext = sgml_bp; \
252         sgmlleng = sgml_cp - sgml_bp; \
253         sgml_hold_char = *sgml_cp; \
254         *sgml_cp = '\0'; \
255         sgml_c_buf_p = sgml_cp;
256
257 #define EOB_ACT_CONTINUE_SCAN 0
258 #define EOB_ACT_END_OF_FILE 1
259 #define EOB_ACT_LAST_MATCH 2
260
261 /* return all but the first 'n' matched characters back to the input stream */
262 #define sgmlless(n) \
263         do \
264                 { \
265                 /* undo effects of setting up sgmltext */ \
266                 *sgml_cp = sgml_hold_char; \
267                 sgml_c_buf_p = sgml_cp = sgml_bp + n; \
268                 sgml_DO_BEFORE_ACTION; /* set up sgmltext again */ \
269                 } \
270         while ( 0 )
271
272 #define unput(c) sgmlunput( c, sgmltext )
273
274
275 struct sgml_buffer_state
276     {
277     FILE *sgml_input_file;
278
279     sgml_CHAR *sgml_ch_buf;             /* input buffer */
280     sgml_CHAR *sgml_buf_pos;    /* current position in input buffer */
281
282     /* size of input buffer in bytes, not including room for EOB characters*/
283     int sgml_buf_size;  
284
285     /* number of characters read into sgml_ch_buf, not including EOB characters */
286     int sgml_n_chars;
287
288     int sgml_eof_status;                /* whether we've seen an EOF on this buffer */
289 #define EOF_NOT_SEEN 0
290     /* "pending" happens when the EOF has been seen but there's still
291      * some text process
292      */
293 #define EOF_PENDING 1
294 #define EOF_DONE 2
295     };
296
297 static sgml_BUFFER_STATE sgml_current_buffer;
298
299 /* we provide macros for accessing buffer states in case in the
300  * future we want to put the buffer states in a more general
301  * "scanner state"
302  */
303 #define sgml_CURRENT_BUFFER sgml_current_buffer
304
305
306 /* sgml_hold_char holds the character lost when sgmltext is formed */
307 static sgml_CHAR sgml_hold_char;
308
309 static int sgml_n_chars;                /* number of characters read into sgml_ch_buf */
310
311
312
313 #ifndef sgml_USER_ACTION
314 #define sgml_USER_ACTION
315 #endif
316
317 #ifndef sgml_USER_INIT
318 #define sgml_USER_INIT
319 #endif
320
321 extern sgml_CHAR *sgmltext;
322 extern int sgmlleng;
323 extern FILE *sgmlin, *sgmlout;
324
325 sgml_CHAR *sgmltext;
326 int sgmlleng;
327
328 FILE *sgmlin = (FILE *) 0, *sgmlout = (FILE *) 0;
329
330 #define sgml_END_OF_BUFFER 4
331 typedef int sgml_state_type;
332 static const short int sgml_accept[11] =
333     {   0,
334         0,    0,    4,    2,    2,    0,    0,    1,    0,    0
335     } ;
336
337 static const sgml_CHAR sgml_ec[256] =
338     {   0,
339         1,    1,    1,    1,    1,    1,    1,    1,    1,    2,
340         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
341         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
342         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
343         1,    1,    1,    1,    1,    3,    4,    3,    3,    3,
344         3,    3,    3,    3,    3,    3,    3,    1,    1,    5,
345         1,    6,    1,    1,    3,    3,    3,    3,    3,    3,
346         3,    3,    3,    3,    3,    3,    3,    3,    3,    3,
347         3,    3,    3,    3,    3,    3,    3,    3,    3,    3,
348         1,    1,    1,    1,    3,    1,    3,    3,    3,    3,
349
350         3,    3,    3,    3,    3,    3,    3,    3,    3,    3,
351         3,    3,    3,    3,    3,    3,    3,    3,    3,    3,
352         3,    3,    1,    1,    1,    1,    1,    1,    1,    1,
353         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
354         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
355         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
356         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
357         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
358         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
359         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
360
361         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
362         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
363         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
364         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
365         1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
366         1,    1,    1,    1,    1
367     } ;
368
369 static const sgml_CHAR sgml_meta[7] =
370     {   0,
371         1,    1,    2,    1,    1,    1
372     } ;
373
374 static const short int sgml_base[13] =
375     {   0,
376         7,    6,    7,   13,    0,    2,    0,   13,    0,   13,
377         8,    0
378     } ;
379
380 static const short int sgml_def[13] =
381     {   0,
382        11,   11,   10,   10,   10,   10,   12,   10,   12,    0,
383        10,   10
384     } ;
385
386 static const short int sgml_nxt[20] =
387     {   0,
388        10,    9,    6,    7,    6,    8,   10,    8,    4,    4,
389         5,    5,    3,   10,   10,   10,   10,   10,   10
390     } ;
391
392 static const short int sgml_chk[20] =
393     {   0,
394         0,   12,    5,    5,    6,    9,    3,    6,   11,   11,
395         2,    1,   10,   10,   10,   10,   10,   10,   10
396     } ;
397
398 static sgml_state_type sgml_last_accepting_state;
399 static sgml_CHAR *sgml_last_accepting_cpos;
400
401 /* the intent behind this definition is that it'll catch
402  * any uses of REJECT which flex missed
403  */
404 #define REJECT reject_used_but_not_detected
405 #define sgmlmore() sgmlmore_used_but_not_detected
406 #define sgml_MORE_ADJ 0
407
408 /* these variables are all declared out here so that section 3 code can
409  * manipulate them
410  */
411 /* points to current character in buffer */
412 static sgml_CHAR *sgml_c_buf_p = (sgml_CHAR *) 0;
413 static int sgml_init = 1;               /* whether we need to initialize */
414 static int sgml_start = 0;      /* start state number */
415
416 /* flag which is used to allow sgmlwrap()'s to do buffer switches
417  * instead of setting up a fresh sgmlin.  A bit of a hack ...
418  */
419 static int sgml_did_buffer_switch_on_eof;
420
421 static sgml_state_type sgml_get_previous_state sgml_PROTO(( void ));
422 static sgml_state_type sgml_try_NUL_trans sgml_PROTO(( sgml_state_type current_state ));
423 static int sgml_get_next_buffer sgml_PROTO(( void ));
424 #if 0
425 static void sgmlunput sgml_PROTO(( sgml_CHAR c, sgml_CHAR *buf_ptr ));
426 #endif
427 void sgmlrestart sgml_PROTO(( FILE *input_file ));
428 void sgml_switch_to_buffer sgml_PROTO(( sgml_BUFFER_STATE new_buffer ));
429 void sgml_load_buffer_state sgml_PROTO(( void ));
430 sgml_BUFFER_STATE sgml_create_buffer sgml_PROTO(( FILE *file, int size ));
431 void sgml_delete_buffer sgml_PROTO(( sgml_BUFFER_STATE b ));
432 void sgml_init_buffer sgml_PROTO(( sgml_BUFFER_STATE b, FILE *file ));
433
434 #define sgml_new_buffer sgml_create_buffer
435
436 #if 0
437 #ifdef __cplusplus
438 static int sgmlinput sgml_PROTO(( void ));
439 #else
440 static int input sgml_PROTO(( void ));
441 #endif
442 #endif
443
444 sgml_DECL
445     {
446     register sgml_state_type sgml_current_state;
447     register sgml_CHAR *sgml_cp, *sgml_bp;
448     register int sgml_act;
449
450
451
452
453     if ( sgml_init )
454         {
455         sgml_USER_INIT;
456
457         if ( ! sgml_start )
458             sgml_start = 1;     /* first start state */
459
460         if ( ! sgmlin )
461             sgmlin = stdin;
462
463         if ( ! sgmlout )
464             sgmlout = stdout;
465
466         if ( sgml_current_buffer )
467             sgml_init_buffer( sgml_current_buffer, sgmlin );
468         else
469             sgml_current_buffer = sgml_create_buffer( sgmlin, sgml_BUF_SIZE );
470
471         sgml_load_buffer_state();
472
473         sgml_init = 0;
474         }
475
476     while ( 1 )         /* loops until end-of-file is reached */
477         {
478         sgml_cp = sgml_c_buf_p;
479
480         /* support of sgmltext */
481         *sgml_cp = sgml_hold_char;
482
483         /* sgml_bp points to the position in sgml_ch_buf of the start of the
484          * current run.
485          */
486         sgml_bp = sgml_cp;
487
488         sgml_current_state = sgml_start;
489 sgml_match:
490         do
491             {
492             register sgml_CHAR sgml_c = sgml_ec[*sgml_cp];
493             if ( sgml_accept[sgml_current_state] )
494                 {
495                 sgml_last_accepting_state = sgml_current_state;
496                 sgml_last_accepting_cpos = sgml_cp;
497                 }
498             while ( sgml_chk[sgml_base[sgml_current_state] + sgml_c] != sgml_current_state )
499                 {
500                 sgml_current_state = sgml_def[sgml_current_state];
501                 if ( sgml_current_state >= 11 )
502                     sgml_c = sgml_meta[sgml_c];
503                 }
504             sgml_current_state = sgml_nxt[sgml_base[sgml_current_state] + sgml_c];
505             ++sgml_cp;
506             }
507         while ( sgml_current_state != 10 );
508         sgml_cp = sgml_last_accepting_cpos;
509         sgml_current_state = sgml_last_accepting_state;
510
511 sgml_find_action:
512         sgml_act = sgml_accept[sgml_current_state];
513
514         sgml_DO_BEFORE_ACTION;
515         sgml_USER_ACTION;
516
517 do_action:      /* this label is used only to access EOF actions */
518
519
520         switch ( sgml_act )
521             {
522             case 0: /* must backtrack */
523             /* undo the effects of sgml_DO_BEFORE_ACTION */
524             *sgml_cp = sgml_hold_char;
525             sgml_cp = sgml_last_accepting_cpos;
526             sgml_current_state = sgml_last_accepting_state;
527             goto sgml_find_action;
528
529 case 1:
530 # line 61 "sgml.l"
531 {
532                (*lex_action_func)((unsigned char*)(sgmltext), sgmlleng, 1);
533         }
534         sgml_BREAK
535 case 2:
536 # line 65 "sgml.l"
537 {
538            if ( sgmlbuffed >= sgmlbuf_sz ) {
539               (*lex_action_func)(sgmlbuf, sgmlbuf_sz, 2);
540               sgmlbuffed = 0;
541            }
542
543            sgmlbuf[sgmlbuffed++] = sgmltext[0];
544         }
545         sgml_BREAK
546 case 3:
547 # line 75 "sgml.l"
548 sgml_FATAL_ERROR( "flex scanner jammed" );
549         sgml_BREAK
550 case sgml_STATE_EOF(INITIAL):
551     sgmlterminate();
552
553             case sgml_END_OF_BUFFER:
554                 {
555                 /* amount of text matched not including the EOB char */
556                 int sgml_amount_of_matched_text = sgml_cp - sgmltext - 1;
557
558                 /* undo the effects of sgml_DO_BEFORE_ACTION */
559                 *sgml_cp = sgml_hold_char;
560
561                 /* note that here we test for sgml_c_buf_p "<=" to the position
562                  * of the first EOB in the buffer, since sgml_c_buf_p will
563                  * already have been incremented past the NUL character
564                  * (since all states make transitions on EOB to the end-
565                  * of-buffer state).  Contrast this with the test in sgmlinput().
566                  */
567                 if ( sgml_c_buf_p <= &sgml_current_buffer->sgml_ch_buf[sgml_n_chars] )
568                     /* this was really a NUL */
569                     {
570                     sgml_state_type sgml_next_state;
571
572                     sgml_c_buf_p = sgmltext + sgml_amount_of_matched_text;
573
574                     sgml_current_state = sgml_get_previous_state();
575
576                     /* okay, we're now positioned to make the
577                      * NUL transition.  We couldn't have
578                      * sgml_get_previous_state() go ahead and do it
579                      * for us because it doesn't know how to deal
580                      * with the possibility of jamming (and we
581                      * don't want to build jamming into it because
582                      * then it will run more slowly)
583                      */
584
585                     sgml_next_state = sgml_try_NUL_trans( sgml_current_state );
586
587                     sgml_bp = sgmltext + sgml_MORE_ADJ;
588
589                     if ( sgml_next_state )
590                         {
591                         /* consume the NUL */
592                         sgml_cp = ++sgml_c_buf_p;
593                         sgml_current_state = sgml_next_state;
594                         goto sgml_match;
595                         }
596
597                     else
598                         {
599                             sgml_cp = sgml_last_accepting_cpos;
600                             sgml_current_state = sgml_last_accepting_state;
601                         goto sgml_find_action;
602                         }
603                     }
604
605                 else switch ( sgml_get_next_buffer() )
606                     {
607                     case EOB_ACT_END_OF_FILE:
608                         {
609                         sgml_did_buffer_switch_on_eof = 0;
610
611                         if ( sgmlwrap() )
612                             {
613                             /* note: because we've taken care in
614                              * sgml_get_next_buffer() to have set up sgmltext,
615                              * we can now set up sgml_c_buf_p so that if some
616                              * total hoser (like flex itself) wants
617                              * to call the scanner after we return the
618                              * sgml_NULL, it'll still work - another sgml_NULL
619                              * will get returned.
620                              */
621                             sgml_c_buf_p = sgmltext + sgml_MORE_ADJ;
622
623                             sgml_act = sgml_STATE_EOF((sgml_start - 1) / 2);
624                             goto do_action;
625                             }
626
627                         else
628                             {
629                             if ( ! sgml_did_buffer_switch_on_eof )
630                                 sgml_NEW_FILE;
631                             }
632                         }
633                         break;
634
635                     case EOB_ACT_CONTINUE_SCAN:
636                         sgml_c_buf_p = sgmltext + sgml_amount_of_matched_text;
637
638                         sgml_current_state = sgml_get_previous_state();
639
640                         sgml_cp = sgml_c_buf_p;
641                         sgml_bp = sgmltext + sgml_MORE_ADJ;
642                         goto sgml_match;
643
644                     case EOB_ACT_LAST_MATCH:
645                         sgml_c_buf_p =
646                             &sgml_current_buffer->sgml_ch_buf[sgml_n_chars];
647
648                         sgml_current_state = sgml_get_previous_state();
649
650                         sgml_cp = sgml_c_buf_p;
651                         sgml_bp = sgmltext + sgml_MORE_ADJ;
652                         goto sgml_find_action;
653                     }
654                 break;
655                 }
656
657             default:
658 #ifdef FLEX_DEBUG
659                 printf( "action # %d\n", sgml_act );
660 #endif
661                 sgml_FATAL_ERROR(
662                         "fatal flex scanner internal error--no action found" );
663             }
664         }
665     }
666
667
668 /* sgml_get_next_buffer - try to read in a new buffer
669  *
670  * synopsis
671  *     int sgml_get_next_buffer();
672  *     
673  * returns a code representing an action
674  *     EOB_ACT_LAST_MATCH - 
675  *     EOB_ACT_CONTINUE_SCAN - continue scanning from current position
676  *     EOB_ACT_END_OF_FILE - end of file
677  */
678
679 static int sgml_get_next_buffer()
680
681     {
682     register sgml_CHAR *dest = sgml_current_buffer->sgml_ch_buf;
683     register sgml_CHAR *source = sgmltext - 1; /* copy prev. char, too */
684     register int number_to_move, i;
685     int ret_val;
686
687     if ( sgml_c_buf_p > &sgml_current_buffer->sgml_ch_buf[sgml_n_chars + 1] )
688         sgml_FATAL_ERROR(
689                 "fatal flex scanner internal error--end of buffer missed" );
690
691     /* try to read more data */
692
693     /* first move last chars to start of buffer */
694     number_to_move = sgml_c_buf_p - sgmltext;
695
696     for ( i = 0; i < number_to_move; ++i )
697         *(dest++) = *(source++);
698
699     if ( sgml_current_buffer->sgml_eof_status != EOF_NOT_SEEN )
700         /* don't do the read, it's not guaranteed to return an EOF,
701          * just force an EOF
702          */
703         sgml_n_chars = 0;
704
705     else
706         {
707         int num_to_read = sgml_current_buffer->sgml_buf_size - number_to_move - 1;
708
709         if ( num_to_read > sgml_READ_BUF_SIZE )
710             num_to_read = sgml_READ_BUF_SIZE;
711
712         else if ( num_to_read <= 0 )
713             sgml_FATAL_ERROR( "fatal error - scanner input buffer overflow" );
714
715         /* read in more data */
716         sgml_INPUT( (&sgml_current_buffer->sgml_ch_buf[number_to_move]),
717                   sgml_n_chars, num_to_read );
718         }
719
720     if ( sgml_n_chars == 0 )
721         {
722         if ( number_to_move == 1 )
723             {
724             ret_val = EOB_ACT_END_OF_FILE;
725             sgml_current_buffer->sgml_eof_status = EOF_DONE;
726             }
727
728         else
729             {
730             ret_val = EOB_ACT_LAST_MATCH;
731             sgml_current_buffer->sgml_eof_status = EOF_PENDING;
732             }
733         }
734
735     else
736         ret_val = EOB_ACT_CONTINUE_SCAN;
737
738     sgml_n_chars += number_to_move;
739     sgml_current_buffer->sgml_ch_buf[sgml_n_chars] = sgml_END_OF_BUFFER_CHAR;
740     sgml_current_buffer->sgml_ch_buf[sgml_n_chars + 1] = sgml_END_OF_BUFFER_CHAR;
741
742     /* sgmltext begins at the second character in sgml_ch_buf; the first
743      * character is the one which preceded it before reading in the latest
744      * buffer; it needs to be kept around in case it's a newline, so
745      * sgml_get_previous_state() will have with '^' rules active
746      */
747
748     sgmltext = &sgml_current_buffer->sgml_ch_buf[1];
749
750     return ( ret_val );
751     }
752
753
754 /* sgml_get_previous_state - get the state just before the EOB char was reached
755  *
756  * synopsis
757  *     sgml_state_type sgml_get_previous_state();
758  */
759
760 static sgml_state_type sgml_get_previous_state()
761
762     {
763     register sgml_state_type sgml_current_state;
764     register sgml_CHAR *sgml_cp;
765
766     sgml_current_state = sgml_start;
767
768     for ( sgml_cp = sgmltext + sgml_MORE_ADJ; sgml_cp < sgml_c_buf_p; ++sgml_cp )
769         {
770         register sgml_CHAR sgml_c = (*sgml_cp ? sgml_ec[*sgml_cp] : 1);
771         if ( sgml_accept[sgml_current_state] )
772             {
773             sgml_last_accepting_state = sgml_current_state;
774             sgml_last_accepting_cpos = sgml_cp;
775             }
776         while ( sgml_chk[sgml_base[sgml_current_state] + sgml_c] != sgml_current_state )
777             {
778             sgml_current_state = sgml_def[sgml_current_state];
779             if ( sgml_current_state >= 11 )
780                 sgml_c = sgml_meta[sgml_c];
781             }
782         sgml_current_state = sgml_nxt[sgml_base[sgml_current_state] + sgml_c];
783         }
784
785     return ( sgml_current_state );
786     }
787
788
789 /* sgml_try_NUL_trans - try to make a transition on the NUL character
790  *
791  * synopsis
792  *     next_state = sgml_try_NUL_trans( current_state );
793  */
794
795 #ifdef sgml_USE_PROTOS
796 static sgml_state_type sgml_try_NUL_trans( register sgml_state_type sgml_current_state )
797 #else
798 static sgml_state_type sgml_try_NUL_trans( sgml_current_state )
799 register sgml_state_type sgml_current_state;
800 #endif
801
802     {
803     register int sgml_is_jam;
804     register sgml_CHAR *sgml_cp = sgml_c_buf_p;
805
806     register sgml_CHAR sgml_c = 1;
807     if ( sgml_accept[sgml_current_state] )
808         {
809         sgml_last_accepting_state = sgml_current_state;
810         sgml_last_accepting_cpos = sgml_cp;
811         }
812     while ( sgml_chk[sgml_base[sgml_current_state] + sgml_c] != sgml_current_state )
813         {
814         sgml_current_state = sgml_def[sgml_current_state];
815         if ( sgml_current_state >= 11 )
816             sgml_c = sgml_meta[sgml_c];
817         }
818     sgml_current_state = sgml_nxt[sgml_base[sgml_current_state] + sgml_c];
819     sgml_is_jam = (sgml_current_state == 10);
820
821     return ( sgml_is_jam ? 0 : sgml_current_state );
822     }
823
824
825 #if 0
826 #ifdef sgml_USE_PROTOS
827 static void sgmlunput( sgml_CHAR c, register sgml_CHAR *sgml_bp )
828 #else
829 static void sgmlunput( c, sgml_bp )
830 sgml_CHAR c;
831 register sgml_CHAR *sgml_bp;
832 #endif
833
834     {
835     register sgml_CHAR *sgml_cp = sgml_c_buf_p;
836
837     /* undo effects of setting up sgmltext */
838     *sgml_cp = sgml_hold_char;
839
840     if ( sgml_cp < sgml_current_buffer->sgml_ch_buf + 2 )
841         { /* need to shift things up to make room */
842         register int number_to_move = sgml_n_chars + 2; /* +2 for EOB chars */
843         register sgml_CHAR *dest =
844             &sgml_current_buffer->sgml_ch_buf[sgml_current_buffer->sgml_buf_size + 2];
845         register sgml_CHAR *source =
846             &sgml_current_buffer->sgml_ch_buf[number_to_move];
847
848         while ( source > sgml_current_buffer->sgml_ch_buf )
849             *--dest = *--source;
850
851         sgml_cp += dest - source;
852         sgml_bp += dest - source;
853         sgml_n_chars = sgml_current_buffer->sgml_buf_size;
854
855         if ( sgml_cp < sgml_current_buffer->sgml_ch_buf + 2 )
856             sgml_FATAL_ERROR( "flex scanner push-back overflow" );
857         }
858
859     if ( sgml_cp > sgml_bp && sgml_cp[-1] == '\n' )
860         sgml_cp[-2] = '\n';
861
862     *--sgml_cp = c;
863
864     /* note: the formal parameter *must* be called "sgml_bp" for this
865      *       macro to now work correctly
866      */
867     sgml_DO_BEFORE_ACTION; /* set up sgmltext again */
868     }
869 #endif
870
871
872 #if 0
873 #ifdef __cplusplus
874 static int sgmlinput()
875 #else
876 static int input()
877 #endif
878
879     {
880     int c;
881     sgml_CHAR *sgml_cp = sgml_c_buf_p;
882
883     *sgml_cp = sgml_hold_char;
884
885     if ( *sgml_c_buf_p == sgml_END_OF_BUFFER_CHAR )
886         {
887         /* sgml_c_buf_p now points to the character we want to return.
888          * If this occurs *before* the EOB characters, then it's a
889          * valid NUL; if not, then we've hit the end of the buffer.
890          */
891         if ( sgml_c_buf_p < &sgml_current_buffer->sgml_ch_buf[sgml_n_chars] )
892             /* this was really a NUL */
893             *sgml_c_buf_p = '\0';
894
895         else
896             { /* need more input */
897             sgmltext = sgml_c_buf_p;
898             ++sgml_c_buf_p;
899
900             switch ( sgml_get_next_buffer() )
901                 {
902                 case EOB_ACT_END_OF_FILE:
903                     {
904                     if ( sgmlwrap() )
905                         {
906                         sgml_c_buf_p = sgmltext + sgml_MORE_ADJ;
907                         return ( EOF );
908                         }
909
910                     sgml_NEW_FILE;
911
912 #ifdef __cplusplus
913                     return ( sgmlinput() );
914 #else
915                     return ( input() );
916 #endif
917                     }
918                     break;
919
920                 case EOB_ACT_CONTINUE_SCAN:
921                     sgml_c_buf_p = sgmltext + sgml_MORE_ADJ;
922                     break;
923
924                 case EOB_ACT_LAST_MATCH:
925 #ifdef __cplusplus
926                     sgml_FATAL_ERROR( "unexpected last match in sgmlinput()" );
927 #else
928                     sgml_FATAL_ERROR( "unexpected last match in input()" );
929 #endif
930                 }
931             }
932         }
933
934     c = *sgml_c_buf_p;
935     sgml_hold_char = *++sgml_c_buf_p;
936
937     return ( c );
938     }
939 #endif
940
941
942 #ifdef sgml_USE_PROTOS
943 void sgmlrestart( FILE *input_file )
944 #else
945 void sgmlrestart( input_file )
946 FILE *input_file;
947 #endif
948
949     {
950     sgml_init_buffer( sgml_current_buffer, input_file );
951     sgml_load_buffer_state();
952     }
953
954
955 #ifdef sgml_USE_PROTOS
956 void sgml_switch_to_buffer( sgml_BUFFER_STATE new_buffer )
957 #else
958 void sgml_switch_to_buffer( new_buffer )
959 sgml_BUFFER_STATE new_buffer;
960 #endif
961
962     {
963     if ( sgml_current_buffer == new_buffer )
964         return;
965
966     if ( sgml_current_buffer )
967         {
968         /* flush out information for old buffer */
969         *sgml_c_buf_p = sgml_hold_char;
970         sgml_current_buffer->sgml_buf_pos = sgml_c_buf_p;
971         sgml_current_buffer->sgml_n_chars = sgml_n_chars;
972         }
973
974     sgml_current_buffer = new_buffer;
975     sgml_load_buffer_state();
976
977     /* we don't actually know whether we did this switch during
978      * EOF (sgmlwrap()) processing, but the only time this flag
979      * is looked at is after sgmlwrap() is called, so it's safe
980      * to go ahead and always set it.
981      */
982     sgml_did_buffer_switch_on_eof = 1;
983     }
984
985
986 #ifdef sgml_USE_PROTOS
987 void sgml_load_buffer_state( void )
988 #else
989 void sgml_load_buffer_state()
990 #endif
991
992     {
993     sgml_n_chars = sgml_current_buffer->sgml_n_chars;
994     sgmltext = sgml_c_buf_p = sgml_current_buffer->sgml_buf_pos;
995     sgmlin = sgml_current_buffer->sgml_input_file;
996     sgml_hold_char = *sgml_c_buf_p;
997     }
998
999
1000 #ifdef sgml_USE_PROTOS
1001 sgml_BUFFER_STATE sgml_create_buffer( FILE *file, int size )
1002 #else
1003 sgml_BUFFER_STATE sgml_create_buffer( file, size )
1004 FILE *file;
1005 int size;
1006 #endif
1007
1008     {
1009     sgml_BUFFER_STATE b;
1010
1011     b = (sgml_BUFFER_STATE) malloc( sizeof( struct sgml_buffer_state ) );
1012
1013     if ( ! b )
1014         sgml_FATAL_ERROR( "out of dynamic memory in sgml_create_buffer()" );
1015
1016     b->sgml_buf_size = size;
1017
1018     /* sgml_ch_buf has to be 2 characters longer than the size given because
1019      * we need to put in 2 end-of-buffer characters.
1020      */
1021     b->sgml_ch_buf = (sgml_CHAR *) malloc( (unsigned) (b->sgml_buf_size + 2) );
1022
1023     if ( ! b->sgml_ch_buf )
1024         sgml_FATAL_ERROR( "out of dynamic memory in sgml_create_buffer()" );
1025
1026     sgml_init_buffer( b, file );
1027
1028     return ( b );
1029     }
1030
1031
1032 #ifdef sgml_USE_PROTOS
1033 void sgml_delete_buffer( sgml_BUFFER_STATE b )
1034 #else
1035 void sgml_delete_buffer( b )
1036 sgml_BUFFER_STATE b;
1037 #endif
1038
1039     {
1040     if ( b == sgml_current_buffer )
1041         sgml_current_buffer = (sgml_BUFFER_STATE) 0;
1042
1043     free( (char *) b->sgml_ch_buf );
1044     free( (char *) b );
1045     }
1046
1047
1048 #ifdef sgml_USE_PROTOS
1049 void sgml_init_buffer( sgml_BUFFER_STATE b, FILE *file )
1050 #else
1051 void sgml_init_buffer( b, file )
1052 sgml_BUFFER_STATE b;
1053 FILE *file;
1054 #endif
1055
1056     {
1057     b->sgml_input_file = file;
1058
1059     /* we put in the '\n' and start reading from [1] so that an
1060      * initial match-at-newline will be true.
1061      */
1062
1063     b->sgml_ch_buf[0] = '\n';
1064     b->sgml_n_chars = 1;
1065
1066     /* we always need two end-of-buffer characters.  The first causes
1067      * a transition to the end-of-buffer state.  The second causes
1068      * a jam in that state.
1069      */
1070     b->sgml_ch_buf[1] = sgml_END_OF_BUFFER_CHAR;
1071     b->sgml_ch_buf[2] = sgml_END_OF_BUFFER_CHAR;
1072
1073     b->sgml_buf_pos = &b->sgml_ch_buf[1];
1074
1075     b->sgml_eof_status = EOF_NOT_SEEN;
1076     }
1077 # line 75 "sgml.l"
1078
1079
1080 int sgmlwrap()
1081 {
1082    (*lex_action_func)(sgmlbuf, sgmlbuffed, 2);
1083    return 1;
1084 }
1085