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 (?).
20 #include "functions.h"
24 #include <sys/ioctl.h>
26 extern FILE * d_stream_dbg
;
30 #define IF(arg) if (t_map[i] == arg)
31 #define IF2(arga, argb) if (t_map[i] == arga && t_map[i+1] == argb)
32 #define is_space(arg) (arg == ' ' || arg == '\t' || arg == '\n')
33 #define IF_breaker if (t_map[i]==';' || t_map[i] == '}' || t_map[i] == '{')
36 #define Toff(arg) t_map[i+arg]
37 #define ATTACH t_attach ();
39 //#define TN(arg) do { t_new[ii] = arg; ii++; } while (0)
48 int brace_depth
= 0; // for '()', not '{}' !
49 int block_depth
= 0; // for '{}'
50 bool b_make_attachment
;
52 pair_t
* d_attachment
= NULL
;
54 // äÏÂÁ×ÉÔØ Ó×ÑÚËÕ ÎÏÍÅÒ ÏÐÅÒÁÔÏÒÁ + ÎÏÍÅÒ ÓÔÒÏËÉ.
55 inline void t_attach ()
58 d_attachment
[t_op_no
].pair_op
= t_op_no
;
59 d_attachment
[t_op_no
].pair_line
= t_input_line
;
60 d_attachment
[t_op_no
].offset
= i
;
61 brace_depth
= 0; // Paranoid
64 // äÏÂÁ×ÉÔØ ÓÉÍ×ÏÌ "ÎÁ ×ÙÈÏÄ". åÓÌÉ ';','{','}' ÔÏ ÄÏÂÁ×ÉÔØ.
65 // ÎÏÍÅÒ ÓÔÒÏËÉ Ë ÓÐÉÓËÕ ÏÐÅÒÁÔÏÒÏ×.
66 inline void TN (char arg
)
71 arg
== '{' || arg
== '}' ||
72 (arg
== ';' && !brace_depth
)) {
83 #define TNs(arg) do { t_new[ii] = arg; ++ii; } while (0)
85 // ÐÒÏÐÕÓÔÉÔØ ËÏÍÍÅÎÔÁÒÉÊ "//"
86 inline void tt_skip ()
88 while (T
!= '\n' && i
< t_size
)
93 /* ðÒÏÐÕÓÔÉÔØ ËÏÍÍÅÎÔÁÒÉÊ / * * / */
94 inline void tt_comment ()
107 // NOTE: you _must_ allocate d_input and d_output before call this.
108 // ÷ÚÑÔØ ÔÅËÓÔ D_INPUT, ÕÂÒÁÔØ ×ÓÅ ÌÉÛÎÉÅ ÐÒÏÂÅÌØÎÙÅ ÓÉÍ×ÏÌÙ,
109 // ÄÏÂÁ×ÉÔÔØ ÎÕÖÎÙÅ, ÕÂÉÔØ ËÏÍÍÅÎÔÁÒÉÉ etc É ×ÙÌÏÖÉÔØ × d_output.
110 int the_tt_for_buffers (char * d_input
,
114 unsigned int t_spaces
= 1;
115 bool b_mustlined
= false;
116 bool b_instring
= false;
117 bool b_newline
= true;
118 int d_slash_count
= 0;
135 if (t_map
[0] == '\n') {
136 ++i
; // "mmap BUG" fixed :))
140 if (Toff (t_size
-1) == '\n')
141 t_size
--; // mmap'ed TT not work without this !
143 for (; i
< t_size
; ++i
) {
145 /* But count all lines anyway... */
149 if (T
== '\'' || T
== '\"') {
150 if (b_instring
&& ch_last
!= T
) {
156 if (Toff(-1) != '\\') // Normal skip \" and... \\" :)
160 while (Toff (-(d_slash_count
)) == '\\') // Yes, I'm don't like this.
163 if (d_slash_count
& 1)
176 if (Toff(-1) == '\\')
182 if (if_abc (&t_map
[i
]) || if_digit (&t_map
[i
])) {
189 if (Toff(-1) == '\\') {
210 // Next code for not-comment and not-string C code. May silent counting operators...
223 if (Toff(1) == '#' && !b_lined
) {
234 if (!(Toff (-1) == '\\')) {
240 if (t_spaces
== 0 && !b_lined
) {
254 if (T
== '(' && t_spaces
== 0 && b_mustlined
== 0) {
255 t_spaces
++; // No space after '('.
261 if (T
== ')' && t_spaces
== 0) {
276 IF('#' && b_newline
) {
294 char * do_tt_file (tt_state_t
* d_tt_state
)
296 char * t_output
= NULL
;
297 char * t_input
= NULL
;
309 f_name
= d_tt_state
->d_file_name
;
311 if (EQ(f_name
, "-")) {
312 t_input
= Dread_to_eof (fileno (stdin
), &t_size
);
318 t_input
= DFMAP (f_name
, &t_fd
, &t_size
);
319 if (t_input
== NULL
) {
320 t_size
= fsize (f_name
);
322 t_input
= CNEW (char, t_size
);
323 Dfnread (f_name
, t_input
, t_size
);
327 if (!(t_input
> NULL
)) {
328 fprintf (stderr
, "can't open/mmap file %s\n", f_name
);
329 perror ("open/mmap");
333 t_output
= CNEW (char, t_size
<<1); // Paranoid.
334 if (t_output
== NULL
) {
335 perror ("no such memory");
340 d_attachment
= CNEW (pair_t
, t_size
>>1);
342 d_tt_state
->d_attachment
= d_attachment
;
343 d_tt_state
->d_fd
= t_fd
;
344 d_tt_state
->d_file_in
= t_input
;
345 d_tt_state
->d_filein_size
= t_size
;
346 d_tt_state
->b_mmap
= b_mmap
;
348 t_Ret
= the_tt_for_buffers (t_input
, t_size
, t_output
);
349 t_output
[t_Ret
] = 0; // Required.
350 d_tt_state
->d_output_size
= t_Ret
;
351 d_tt_state
->d_output
= t_output
;
356 // $ silent_bob --the-tt
357 int the_tt_main (char * f_name
)
360 tt_state_t
* d_tt_state
;
362 d_tt_state
= CNEW (tt_state_t
, 1);
363 bzero (d_tt_state
, sizeof (tt_state_t
));
364 d_tt_state
->d_file_name
= f_name
;
365 t_output
= do_tt_file (d_tt_state
);
367 if (! SB_FLGET (SB_FLSIMULATE
))
368 write (fileno (stdout
), t_output
, d_tt_state
->d_output_size
);
370 free_tt_state (d_tt_state
);
374 void free_tt_state (struct tt_state_t
* S
)
377 munmap (S
->d_file_in
, S
->d_filein_size
);
385 DROP (S
->d_attachment
);
389 } // namespace THE_TT