2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
8 * "Oleg, THE_TT and BUGs"
10 * 01/01/06 00:00 - Started...
11 * January 2006 - string BUG fixed.
12 * "//" BUG fixed. It's work currently ?!
14 * January 2006, last day. added operator to line conversion.
15 * February 2006 - operator to line conversion work properly.
16 * February 2006 - "macro BUG" fixed.
17 * March 2006 - "Style BUG" fixed (?).
22 #include "../the_tt.h"
24 extern FILE * d_stream_dbg
;
28 #define IF(arg) if (t_map[i] == arg)
29 #define IF2(arga, argb) if (t_map[i] == arga && t_map[i+1] == argb)
30 #define is_space(arg) (arg == ' ' || arg == '\t' || arg == '\n')
31 #define IF_breaker if (t_map[i]==';' || t_map[i] == '}' || t_map[i] == '{')
34 #define Toff(arg) t_map[i+arg]
35 #define ATTACH t_attach ();
44 int brace_depth
= 0; // for '()'
45 int block_depth
= 0; // for '{}'
46 bool b_make_attachment
;
48 pair_t
* d_attachment
= NULL
;
50 char * tt_out_buf
= NULL
;
51 int tt_outbuf_size
= 0;
53 inline void t_attach ()
56 d_attachment
[t_op_no
].pair_op
= t_op_no
;
57 d_attachment
[t_op_no
].pair_line
= t_input_line
;
58 d_attachment
[t_op_no
].offset
= i
;
59 brace_depth
= 0; // Paranoid
62 inline void TN (char arg
)
67 arg
== '{' || arg
== '}' ||
68 (arg
== ';' && !brace_depth
)) {
79 #define TNs(arg) do { t_new[ii] = arg; ++ii; } while (0)
81 inline void tt_comment ()
94 inline void tt_skip ()
96 while (T
!= '\n' && i
< t_size
)
101 // NOTE: you _must_ allocate d_input and d_output before call this.
102 int the_tt_for_buffers (char * d_input
,
106 unsigned int t_spaces
= 1;
107 bool b_mustlined
= false;
108 bool b_instring
= false;
109 bool b_newline
= true;
110 int d_slash_count
= 0;
127 if (t_map
[0] == '\n') {
128 ++i
; // "mmap BUG" fixed :))
132 if (Toff (t_size
-1) == '\n')
133 t_size
--; // mmap'ed TT not work without this !
135 for (; i
< t_size
; ++i
) {
136 if (if_abc (&t_map
[i
]) || if_digit (&t_map
[i
])) {
145 if (T
== '\'' || T
== '\"') {
146 if (b_instring
&& ch_last
!= T
) {
152 if (Toff(-1) != '\\') // Normal skip \" and... \\" :)
156 while (Toff (-(d_slash_count
)) == '\\') // Yes, I'm don't like this.
159 if (d_slash_count
& 1)
172 if (Toff(-1) == '\\')
179 if (Toff(-1) == '\\') {
211 if (Toff(1) == '#' && !b_lined
) {
222 if (!(Toff (-1) == '\\')) {
228 if (t_spaces
== 0 && !b_lined
) {
242 if (T
== '(' && t_spaces
== 0 && b_mustlined
== 0) {
243 ++t_spaces
; // No space after '('.
249 if (T
== ')' && t_spaces
== 0) {
264 IF('#' && b_newline
) {
283 char * do_tt_file (tt_state_t
* d_tt_state
)
285 char * t_output
= NULL
;
294 f_name
= d_tt_state
->d_file_name
;
296 if (EQ(f_name
, "-")) {
297 d_tt_state
->d_file_in
= Dread_to_eof (fileno (stdin
), &d_tt_state
->d_filein_size
);
298 if (d_tt_state
->d_filein_size
<= 0)
303 t_size
= d_tt_state
->d_filein_size
;
305 t_output
= CNEW (char, t_size
<<1); // Paranoid.
306 if (t_output
== NULL
) {
307 perror ("no such memory");
313 d_attachment
= CNEW (pair_t
, 4096);
315 d_attachment
= CNEW (pair_t
, t_size
>>1);
317 d_tt_state
->d_attachment
= d_attachment
;
319 t_Ret
= the_tt_for_buffers (d_tt_state
->d_file_in
, t_size
, t_output
);
320 t_output
[t_Ret
] = 0; // Required.
321 d_tt_state
->d_output_size
= t_Ret
;
322 d_tt_state
->d_output
= t_output
;
327 // $ silent_bob --the-tt
328 int the_tt_main (char * f_name
)
331 tt_state_t
* d_tt_state
;
333 d_tt_state
= CNEW (tt_state_t
, 1);
334 memset (d_tt_state
, 0, sizeof (tt_state_t
));
335 d_tt_state
->d_file_name
= f_name
;
336 t_output
= do_tt_file (d_tt_state
);
338 if (! SB_FLGET (SB_FLSIMULATE
))
339 write (fileno (stdout
), t_output
, d_tt_state
->d_output_size
);
341 free_tt_state (d_tt_state
);
345 } // namespace THE_TT
347 void free_tt_state (struct tt_state_t
* S
)
350 munmap (S
->d_file_in
, S
->d_filein_size
);
358 DROP (S
->d_attachment
);
362 int tt_map (tt_state_t
*tt
)
365 tt
->d_file_in
= DFMAP (tt
->d_file_name
, &tt
->d_fd
, &tt
->d_filein_size
);
367 if (tt
->d_file_in
== NULL
) {
368 tt
->d_filein_size
= fsize (tt
->d_file_name
);
370 tt
->d_file_in
= CNEW (char, tt
->d_filein_size
);
371 if (Dfnread (tt
->d_file_name
, tt
->d_file_in
, tt
->d_filein_size
) < 0)
372 DROP (tt
->d_file_in
);
375 if (! tt
->d_file_in
) {
376 fprintf (stderr
, "can't open/mmap file %s\n", tt
->d_file_name
);
377 perror ("open/mmap");