2 * Partial printf implementation.
8 typedef void (*WriteFunc)( char *data, int len );
22 void do_conv( struct format *fsm, char c )
24 printf( "flags: %x\n", fsm->flags );
25 printf( "width: %i\n", fsm->width );
26 printf( "prec: %i\n", fsm->prec );
27 printf( "conv: %c\n", c );
37 #define FL_HAS_WIDTH 0x0100
38 #define FL_WIDTH_ARG 0x0200
39 #define FL_HAS_PREC 0x0400
40 #define FL_PREC_ARG 0x0800
42 #define FL_LEN_H 0x010000
43 #define FL_LEN_HH 0x020000
44 #define FL_LEN_L 0x040000
45 #define FL_LEN_LL 0x080000
61 action width_num { fsm->width = 10 * fsm->width + (fc-'0'); }
62 action width_arg { fsm->flags |= FL_WIDTH_ARG; }
63 action width { fsm->flags |= FL_HAS_WIDTH; }
64 width = ( ( nznum $width_num | '*' @width_arg ) %width )?;
67 action prec_num { fsm->prec = 10 * fsm->prec + (fc-'0'); }
68 action prec_arg { fsm->flags |= FL_PREC_ARG; }
69 action prec { fsm->flags |= FL_HAS_PREC; }
70 precision = ( '.' ( digit* $prec_num %prec | '*' @prec_arg ) )?;
73 action flags_hash { fsm->flags |= FL_HASH; }
74 action flags_zero { fsm->flags |= FL_ZERO; }
75 action flags_dash { fsm->flags |= FL_DASH; }
76 action flags_space { fsm->flags |= FL_SPACE; }
77 action flags_plus { fsm->flags |= FL_PLUS; }
86 action length_h { fsm->flags |= FL_LEN_H; }
87 action length_l { fsm->flags |= FL_LEN_L; }
88 action length_hh { fsm->flags |= FL_LEN_HH; }
89 action length_ll { fsm->flags |= FL_LEN_LL; }
91 # Must use leaving transitions on 'h' and 'l' because they are
92 # prefixes for 'hh' and 'll'.
103 conversion = [diouxXcsp] @conversion;
114 if ( fsm->buflen == BUFLEN ) {
115 fsm->write( fsm->buf, fsm->buflen );
118 fsm->buf[fsm->buflen++] = fc;
122 if ( fsm->buflen > 0 )
123 fsm->write( fsm->buf, fsm->buflen );
126 printf("EOF IN FORMAT\n");
129 printf("ERROR ON CHAR: 0x%x\n", fc );
136 )* @/finish_err %/finish_ok $!err_char;
141 void format_init( struct format *fsm )
147 void format_execute( struct format *fsm, const char *data, int len, int isEof )
149 const char *p = data;
150 const char *pe = data + len;
151 const char *eof = isEof ? pe : 0;
156 int format_finish( struct format *fsm )
158 if ( fsm->cs == format_error )
160 if ( fsm->cs >= format_first_final )
166 #define INPUT_BUFSIZE 2048
169 char buf[INPUT_BUFSIZE];
171 void write(char *data, int len )
173 fwrite( data, 1, len, stdout );
181 int len = fread( buf, 1, INPUT_BUFSIZE, stdin );
182 int eof = len != INPUT_BUFSIZE;
183 format_execute( &fsm, buf, len, eof );
187 if ( format_finish( &fsm ) <= 0 )