Respect line breaks in the original code, making it easier to compare.
[reactos.git] / reactos / tools / ms2ps / ms2ps.cpp
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2004 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: ms2ps.cpp,v 1.3 2004/08/21 19:49:22 arty Exp $
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS seh conversion tool
22 * FILE: tools/ms2ps/ms2ps.cpp
23 * PURPOSE: Conversion tool from msvc to pseh style seh
24 * PROGRAMMER: Art Yerkes
25 */
26
27 #include <iostream>
28 #include <vector>
29 #include <list>
30 #include <set>
31 #include <map>
32 #include <deque>
33 #include <ctype.h>
34
35 #define MAYBE(x)
36
37 using std::string;
38 using std::cin;
39 using std::cout;
40 using std::cerr;
41 using std::endl;
42 using std::istream;
43
44 typedef std::list<std::string> sl_t;
45 typedef sl_t::iterator sl_it;
46
47 string TRY_TOKEN = "__try";
48 string EXCEPT_TOKEN = "__except";
49 string FINALLY_TOKEN = "__finally";
50 char *c_operators[] = {
51 "{",
52 "}",
53 "(",
54 ")",
55 NULL
56 };
57
58 int isident( int c ) {
59 return (c != '{') && (c != '}') && (c != '(') && (c != ')') &&
60 (c != '\'') && (c != '\"') && !isspace(c);
61 }
62
63 bool areinclass( string str, int (*isclass)(int) ) {
64 int i;
65
66 for( i = 0; i < (int)str.size(); i++ )
67 if( !isclass( str[i] ) ) return false;
68
69 return true;
70 }
71
72 bool areident( string s ) { return areinclass( s, isident ); }
73
74 bool isop( string tok ) {
75 int i;
76 for( i = 0; c_operators[i] && tok != c_operators[i]; i++ );
77 if( c_operators[i] ) return true; else return false;
78 }
79
80 enum fstate { EMPTY, INT, FRAC, EXP };
81
82 string generic_match( string tok, string la,
83 bool (*mf)( string ) ) {
84 if( tok.size() < 1 ) return "";
85 if( mf(tok) && !mf(la) ) return tok; else return "";
86 }
87
88 string match_operator( string tok, string la ) {
89 return generic_match( tok, la, isop );
90 }
91
92 string match_ident( string tok, string la ) {
93 return generic_match( tok, la, areident );
94 }
95
96 string match_quoted_const( string tok, string la, char q ) {
97 if( ((tok.size() && tok[0] == q) ||
98 ((tok.size() > 2) && tok[0] == 'L' && tok[1] == q)) &&
99 (tok.rfind(q) == (tok.size() - 1)) ) {
100 if( (tok.rfind("\\") != (tok.size() - 2)) ||
101 (tok.size() > 3 && tok.rfind("\\\\") == (tok.size() - 3)) )
102 return tok;
103 else return "";
104 }
105 else return "";
106 }
107
108 sl_t snarf_tokens( string line ) {
109 int i;
110 sl_t out;
111 string curtok, la, op, ident, qconst;
112
113 line += " ";
114
115 for( i = 0; i < (int)line.size() - 1; i++ ) {
116 /*cout << "Char [" << line[i] << "] and [" << line[i+1] << "]"
117 << endl; */
118
119 if( (!curtok.size() ||
120 (curtok[0] != '\'' && curtok[0] != '\"')) &&
121 (curtok.size() <= 2 ||
122 curtok[0] != 'L' ||
123 (curtok[1] != '\'' && curtok[1] != '\"')) &&
124 isspace(line[i]) ) {
125 if( curtok.size() ) out.push_back( curtok );
126 curtok = "";
127 continue;
128 }
129
130 curtok.push_back( line[i] );
131
132 la = curtok + line[i+1];
133
134 op = match_operator( curtok, la );
135
136 if( op != "" ) {
137 out.push_back( op MAYBE(+ "O") );
138 curtok = "";
139 continue;
140 }
141
142 if( la != "L\"" && la != "L\'" ) {
143 ident = match_ident( curtok, la );
144
145 if( ident != "" ) {
146 out.push_back( ident MAYBE(+ "I") );
147 curtok = "";
148 continue;
149 }
150 }
151
152 qconst = match_quoted_const( curtok, la, '\'' );
153
154 if( qconst != "" ) {
155 out.push_back( qconst MAYBE(+ "q") );
156 curtok = "";
157 continue;
158 }
159
160 qconst = match_quoted_const( curtok, la, '\"' );
161
162 if( qconst != "" ) {
163 out.push_back( qconst MAYBE(+ "Q") );
164 curtok = "";
165 continue;
166 }
167 }
168
169 return out;
170 }
171
172 istream &getline_no_comments( istream &is, string &line ) {
173 string buf;
174 int ch;
175 int seen_slash = false;
176
177 while( (ch = is.get()) != -1 ) {
178 if( seen_slash ) {
179 if( ch == '/' ) {
180 do {
181 ch = is.get();
182 } while( ch != -1 && ch != '\n' && ch != '\r' );
183 break;
184 } else if( ch == '*' ) {
185 ch = is.get(); /* Skip one char */
186 do {
187 while( ch != '*' )
188 ch = is.get();
189 ch = is.get();
190 } while( ch != '/' );
191 buf += ' ';
192 } else {
193 buf += '/'; buf += (char)ch;
194 }
195 seen_slash = false;
196 } else {
197 if( ch == '/' ) seen_slash = true;
198 else if( ch == '\r' || ch == '\n' ) break;
199 else buf += (char)ch;
200 }
201 }
202
203 line = buf;
204
205 return is;
206 }
207
208 bool expand_input( sl_t &tok ) {
209 string line;
210 sl_t new_tokens;
211 bool out = false;
212
213 out = getline_no_comments( cin, line );
214 while( line.size() && isspace( line[0] ) )
215 line = line.substr( 1 );
216 if( line[0] == '#' ) {
217 tok.push_back( line );
218 while( line[line.size()-1] == '\\' ) {
219 getline_no_comments( cin, line );
220 tok.push_back( line );
221 tok.push_back( "\n" );
222 }
223 } else {
224 new_tokens = snarf_tokens( line );
225 tok.splice( tok.end(), new_tokens );
226 tok.push_back( "\n" );
227 }
228
229 return out;
230 }
231
232 sl_it
233 complete_block( sl_it i,
234 sl_it end,
235 string start_ch, string end_ch) {
236 int bc = 1;
237
238 for( i++; i != end && bc; i++ ) {
239 if( *i == start_ch ) bc++;
240 if( *i == end_ch ) bc--;
241 }
242
243 return i;
244 }
245
246 string makename( string intro ) {
247 static int i = 0;
248 char buf[100];
249
250 sprintf( buf, "%s%d", intro.c_str(), i++ );
251
252 return buf;
253 }
254
255 void append_block( sl_t &t, sl_it b, sl_it e ) {
256 while( b != e ) {
257 t.push_back( *b );
258 b++;
259 }
260 }
261
262 void error( sl_t &container, sl_it it, string l ) {
263 int line = 0;
264 for( sl_it i = container.begin(); i != it; i++ )
265 if( (*i)[0] == '#' ) {
266 sscanf( i->substr(1).c_str(), "%d", &line );
267 cerr << "*standard-input*:" << line << ": " << l;
268 }
269 }
270
271 /* Goal: match and transform one __try { a } __except [ (foo) ] { b }
272 * [ __finally { c } ]
273 *
274 * into
275 *
276 * _SEH_FINALLY(name1) { c }
277 * _SEH_FILTER(name2) { return (foo | EXCEPTION_EXECUTE_HANDLER); }
278 * _SEH_TRY_FILTER_FINALLY(name1,name2) {
279 * a
280 * } _SEH_HANDLE {
281 * b
282 * } _SEH_END;
283 */
284
285 void handle_try( sl_t &container, sl_it try_kw, sl_it end ) {
286 string temp;
287 sl_t pseh_clause, temp_tok;
288 string finally_name, filter_name;
289 sl_it try_block, try_block_end, except_kw, paren, end_paren,
290 except_block, except_block_end, todelete,
291 finally_kw, finally_block, finally_block_end, clause_end;
292
293 try_block = try_kw;
294 try_block++;
295 try_block_end = complete_block( try_block, end, "{", "}" );
296
297 if( try_block_end == end )
298 error( container, try_block, "unclosed try block");
299
300 except_kw = try_block_end;
301
302 if( *except_kw == FINALLY_TOKEN ) {
303 finally_kw = except_kw;
304 except_kw = end;
305 paren = end;
306 end_paren = end;
307 except_block = end;
308 except_block_end = end;
309 } else if( *except_kw == EXCEPT_TOKEN ) {
310 paren = except_kw;
311 paren++;
312 if( *paren == "(" ) {
313 end_paren = complete_block( paren, end, "(", ")" );
314 except_block = end_paren;
315 } else {
316 except_block = paren;
317 paren = end;
318 end_paren = end;
319 }
320 except_block_end = complete_block( except_block, end, "{", "}" );
321 finally_kw = except_block_end;
322 } else {
323 except_kw = paren = end_paren = except_block = except_block_end =
324 finally_kw = finally_block = finally_block_end = end;
325 }
326
327 if( finally_kw != end && *finally_kw != FINALLY_TOKEN ) {
328 finally_kw = end;
329 finally_block = end;
330 finally_block_end = end;
331 } else {
332 finally_block = finally_kw;
333 finally_block++;
334 finally_block_end = complete_block( finally_block, end, "{", "}" );
335 }
336
337 if( finally_block_end != end ) clause_end = finally_block_end;
338 else if( except_block_end != end ) clause_end = except_block_end;
339 else clause_end = try_block_end;
340
341 /* Skip one so that we can do != on clause_end */
342
343 /* Now for the output phase -- we've collected the whole seh clause
344 * and it lies between try_kw and clause_end */
345
346 finally_name = makename("_Finally");
347 filter_name = makename("_Filter");
348
349 pseh_clause.push_back( "_SEH_FINALLY" );
350 pseh_clause.push_back( "(" );
351 pseh_clause.push_back( finally_name );
352 pseh_clause.push_back( ")" );
353 if( finally_kw != end )
354 append_block( pseh_clause, finally_block, finally_block_end );
355 else {
356 pseh_clause.push_back( "{" );
357 pseh_clause.push_back( "}" );
358 }
359
360 pseh_clause.push_back( "_SEH_FILTER" );
361 pseh_clause.push_back( "(" );
362 pseh_clause.push_back( filter_name );
363 pseh_clause.push_back( ")" );
364 pseh_clause.push_back( "{" );
365 pseh_clause.push_back( "return" );
366 if( paren != end )
367 append_block( pseh_clause, paren, end_paren );
368 else
369 pseh_clause.push_back( "EXCEPTION_EXECUTE_HANDLER" );
370 pseh_clause.push_back( ";" );
371 pseh_clause.push_back( "}" );
372
373 pseh_clause.push_back( "_SEH_TRY_FILTER_FINALLY" );
374 pseh_clause.push_back( "(" );
375 pseh_clause.push_back( filter_name );
376 pseh_clause.push_back( "," );
377 pseh_clause.push_back( finally_name );
378 pseh_clause.push_back( ")" );
379 append_block( pseh_clause, try_block, try_block_end );
380 pseh_clause.push_back( "_SEH_HANDLE" );
381 pseh_clause.push_back( "{" );
382 if( except_block != end )
383 append_block( pseh_clause, except_block, except_block_end );
384 pseh_clause.push_back( "}" );
385 pseh_clause.push_back( "_SEH_END" );
386 pseh_clause.push_back( ";" );
387
388 container.splice( try_kw, pseh_clause );
389 while( try_kw != clause_end ) {
390 todelete = try_kw;
391 try_kw++;
392 container.erase( todelete );
393 }
394 }
395
396 void print_tokens( sl_it begin, sl_it end ) {
397 for( sl_it i = begin; i != end; i++ )
398 cout << /*"[" <<*/ *i << /*"]" <<*/ " ";
399 }
400
401 int main( int argc, char **argv ) {
402 sl_t tok;
403 sl_it try_found;
404 int i;
405
406 for( i = 1; i < argc; i++ ) {
407 if( string(argv[i]) == "-try" && i < argc - 1 ) {
408 i++;
409 TRY_TOKEN = argv[i];
410 } else if( string(argv[i]) == "-except" && i < argc - 1 ) {
411 i++;
412 EXCEPT_TOKEN = argv[i];
413 } else if( string(argv[i]) == "-finally" && i < argc - 1 ) {
414 i++;
415 FINALLY_TOKEN = argv[i];
416 }
417 }
418
419 /* XXX Uses much memory for large files */
420 while( expand_input(tok) );
421
422 while( (try_found = find( tok.begin(), tok.end(), TRY_TOKEN )) !=
423 tok.end() ) {
424 handle_try( tok, try_found, tok.end() );
425 }
426
427 print_tokens( tok.begin(), tok.end() );
428 }