winedbg: Rewrite auto mode.
[wine] / programs / winedbg / dbg.y
1 %{
2 /*
3  * Parser for command lines in the Wine debugger
4  *
5  * Copyright 1993 Eric Youngdale
6  * Copyright 1995 Morten Welinder
7  * Copyright 2000 Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32
33 #include "debugger.h"
34 #include "wine/exception.h"
35 #include "expr.h"
36
37 int yylex(void);
38 int yyerror(const char*);
39
40 %}
41
42 %union
43 {
44     struct dbg_lvalue   lvalue;
45     char*               string;
46     int                 integer;
47     IMAGEHLP_LINE       listing;
48     struct expr*        expression;
49     struct type_expr_t  type;
50 }
51
52 %token tCONT tPASS tSTEP tLIST tNEXT tQUIT tHELP tBACKTRACE tALL tINFO tUP tDOWN
53 %token tENABLE tDISABLE tBREAK tHBREAK tWATCH tDELETE tSET tMODE tPRINT tEXAM
54 %token tABORT tVM86 tECHO
55 %token tCLASS tMAPS tSTACK tSEGMENTS tSYMBOL tREGS tWND tQUEUE tLOCAL tEXCEPTION
56 %token tPROCESS tTHREAD tMODREF tEOL tEOF
57 %token tFRAME tSHARE tCOND tDISPLAY tUNDISPLAY tDISASSEMBLE
58 %token tSTEPI tNEXTI tFINISH tSHOW tDIR tWHATIS tSOURCE
59 %token <string> tPATH tIDENTIFIER tSTRING tDEBUGSTR tINTVAR
60 %token <integer> tNUM tFORMAT
61 %token tSYMBOLFILE tRUN tATTACH tDETACH tMAINTENANCE tTYPE tMINIDUMP
62 %token tNOPROCESS
63
64 %token tCHAR tSHORT tINT tLONG tFLOAT tDOUBLE tUNSIGNED tSIGNED
65 %token tSTRUCT tUNION tENUM
66
67 /* %left ',' */
68 /* %left '=' OP_OR_EQUAL OP_XOR_EQUAL OP_AND_EQUAL OP_SHL_EQUAL \
69          OP_SHR_EQUAL OP_PLUS_EQUAL OP_MINUS_EQUAL \
70          OP_TIMES_EQUAL OP_DIVIDE_EQUAL OP_MODULO_EQUAL */
71 /* %left OP_COND */ /* ... ? ... : ... */
72 %left OP_LOR
73 %left OP_LAND
74 %left '|'
75 %left '^'
76 %left '&'
77 %left OP_EQ OP_NE
78 %left '<' '>' OP_LE OP_GE
79 %left OP_SHL OP_SHR
80 %left '+' '-'
81 %left '*' '/' '%'
82 %left OP_SIGN '!' '~' OP_DEREF /* OP_INC OP_DEC OP_ADDR */
83 %left '.' '[' OP_DRF OP_SCOPE
84 %nonassoc ':'
85
86 %type <expression> expr lvalue
87 %type <lvalue> expr_lvalue lvalue_addr
88 %type <integer> expr_rvalue
89 %type <string> pathname identifier
90 %type <listing> list_arg
91 %type <type> type_expr
92
93 %%
94
95 input:
96       line
97     | input line
98     ;
99
100 line:
101       command tEOL              { expr_free_all(); }
102     | tEOL
103     | tEOF                      { return 1; }
104     | error tEOL                { yyerrok; expr_free_all(); }
105     ;
106
107 command:
108       tQUIT                     { return 1; }
109     | tHELP                     { print_help(); }
110     | tHELP tINFO               { info_help(); }
111     | tPASS                     { dbg_wait_next_exception(DBG_EXCEPTION_NOT_HANDLED, 0, 0); }
112     | tCONT                     { dbg_wait_next_exception(DBG_CONTINUE, 1,  dbg_exec_cont); }
113     | tCONT tNUM                { dbg_wait_next_exception(DBG_CONTINUE, $2, dbg_exec_cont); }
114     | tSTEP                     { dbg_wait_next_exception(DBG_CONTINUE, 1,  dbg_exec_step_into_line); }
115     | tSTEP tNUM                { dbg_wait_next_exception(DBG_CONTINUE, $2, dbg_exec_step_into_line); }
116     | tNEXT                     { dbg_wait_next_exception(DBG_CONTINUE, 1,  dbg_exec_step_over_line); }
117     | tNEXT tNUM                { dbg_wait_next_exception(DBG_CONTINUE, $2, dbg_exec_step_over_line); }
118     | tSTEPI                    { dbg_wait_next_exception(DBG_CONTINUE, 1,  dbg_exec_step_into_insn); }
119     | tSTEPI tNUM               { dbg_wait_next_exception(DBG_CONTINUE, $2, dbg_exec_step_into_insn); }
120     | tNEXTI                    { dbg_wait_next_exception(DBG_CONTINUE, 1,  dbg_exec_step_over_insn); }
121     | tNEXTI tNUM               { dbg_wait_next_exception(DBG_CONTINUE, $2, dbg_exec_step_over_insn); }
122     | tFINISH                   { dbg_wait_next_exception(DBG_CONTINUE, 0,  dbg_exec_finish); }
123     | tABORT                    { abort(); }
124     | tBACKTRACE                { stack_backtrace(dbg_curr_tid); }
125     | tBACKTRACE tNUM           { stack_backtrace($2); }
126     | tBACKTRACE tALL           { stack_backtrace(-1); }
127     | tUP                       { stack_set_frame(dbg_curr_thread->curr_frame + 1);  }
128     | tUP tNUM                  { stack_set_frame(dbg_curr_thread->curr_frame + $2); }
129     | tDOWN                     { stack_set_frame(dbg_curr_thread->curr_frame - 1);  }
130     | tDOWN tNUM                { stack_set_frame(dbg_curr_thread->curr_frame - $2); }
131     | tFRAME tNUM               { stack_set_frame($2); }
132     | tSHOW tDIR                { source_show_path(); }
133     | tDIR pathname             { source_add_path($2); }
134     | tDIR                      { source_nuke_path(); }
135     | tCOND tNUM                { break_add_condition($2, NULL); }
136     | tCOND tNUM expr           { break_add_condition($2, $3); }
137     | tSOURCE pathname          { parser($2); }
138     | tSYMBOLFILE pathname      { symbol_read_symtable($2, 0); }
139     | tSYMBOLFILE pathname expr_rvalue { symbol_read_symtable($2, $3); }
140     | tWHATIS expr_lvalue       { dbg_printf("type = "); types_print_type(&$2.type, FALSE); dbg_printf("\n"); }
141     | tATTACH tNUM              { dbg_attach_debuggee($2, FALSE, TRUE); }
142     | tDETACH                   { dbg_detach_debuggee(); }
143     | tMINIDUMP pathname        { minidump_write($2, (dbg_curr_thread && dbg_curr_thread->in_exception) ? &dbg_curr_thread->excpt_record : NULL);}
144     | tECHO tSTRING             { dbg_printf("%s\n", $2); }
145     | run_command
146     | list_command
147     | disassemble_command
148     | set_command
149     | x_command
150     | print_command     
151     | break_command
152     | watch_command
153     | display_command
154     | info_command
155     | maintenance_command
156     | noprocess_state
157     ;
158
159 pathname:
160       identifier                { $$ = $1; }
161     | tSTRING                   { $$ = $1; }
162     | tPATH                     { $$ = $1; }
163     ;
164
165 identifier:
166       tIDENTIFIER               { $$ = $1; }
167     | tPATH '!' tIDENTIFIER     { $$ = lexeme_alloc_size(strlen($1) + 1 + strlen($3) + 1);
168                                   sprintf($$, "%s!%s", $1, $3); }
169     | identifier OP_SCOPE tIDENTIFIER { $$ = lexeme_alloc_size(strlen($1) + 2 + strlen($3) + 1);
170                                        sprintf($$, "%s::%s", $1, $3); }
171     ;
172
173 list_arg:
174       tNUM                      { $$.FileName = NULL; $$.LineNumber = $1; }
175     | pathname ':' tNUM         { $$.FileName = $1; $$.LineNumber = $3; }
176     | identifier                { symbol_get_line(NULL, $1, &$$); }
177     | pathname ':' identifier   { symbol_get_line($3, $1, &$$); }
178     | '*' expr_lvalue           { DWORD disp; $$.SizeOfStruct = sizeof($$);
179                                   SymGetLineFromAddr(dbg_curr_process->handle, (unsigned long)memory_to_linear_addr(& $2.addr), &disp, & $$); }
180     ;
181
182 run_command:
183       tRUN                      { dbg_run_debuggee(NULL); }
184     | tRUN tSTRING              { dbg_run_debuggee($2); }
185     ;
186
187 list_command:
188       tLIST                     { source_list(NULL, NULL, 10); }
189     | tLIST '-'                 { source_list(NULL,  NULL, -10); }
190     | tLIST list_arg            { source_list(& $2, NULL, 10); }
191     | tLIST ',' list_arg        { source_list(NULL, & $3, -10); }
192     | tLIST list_arg ',' list_arg      { source_list(& $2, & $4, 0); }
193     ;
194
195 disassemble_command:
196       tDISASSEMBLE              { memory_disassemble(NULL, NULL, 10); }
197     | tDISASSEMBLE expr_lvalue  { memory_disassemble(&$2, NULL, 10); }
198     | tDISASSEMBLE expr_lvalue ',' expr_lvalue { memory_disassemble(&$2, &$4, 0); }
199     ;
200
201 set_command:
202       tSET lvalue_addr '=' expr_rvalue { memory_write_value(&$2, sizeof(int), &$4); }
203     | tSET '+' tIDENTIFIER      { info_wine_dbg_channel(TRUE, NULL, $3); }
204     | tSET '-' tIDENTIFIER      { info_wine_dbg_channel(FALSE, NULL, $3); }
205     | tSET tIDENTIFIER '+' tIDENTIFIER { info_wine_dbg_channel(TRUE, $2, $4); }
206     | tSET tIDENTIFIER '-' tIDENTIFIER { info_wine_dbg_channel(FALSE, $2, $4); }
207     ;
208
209 x_command:
210       tEXAM expr_lvalue         { memory_examine(&$2, 1, 'x'); }
211     | tEXAM tFORMAT expr_lvalue { memory_examine(&$3, $2 >> 8, $2 & 0xff); }
212     ;
213
214 print_command:
215       tPRINT expr_lvalue         { print_value(&$2, 0, 0); }
216     | tPRINT tFORMAT expr_lvalue { if (($2 >> 8) == 1) print_value(&$3, $2 & 0xff, 0); else dbg_printf("Count is meaningless in print command\n"); }
217     ;
218
219 break_command:
220       tBREAK '*' expr_lvalue    { break_add_break_from_lvalue(&$3, TRUE); }
221     | tBREAK identifier         { break_add_break_from_id($2, -1, TRUE); }
222     | tBREAK identifier ':' tNUM { break_add_break_from_id($2, $4, TRUE); }
223     | tBREAK tNUM               { break_add_break_from_lineno($2, TRUE); }
224     | tBREAK                    { break_add_break_from_lineno(-1, TRUE); }
225     | tHBREAK '*' expr_lvalue   { break_add_break_from_lvalue(&$3, FALSE); }
226     | tHBREAK identifier        { break_add_break_from_id($2, -1, FALSE); }
227     | tHBREAK identifier ':' tNUM { break_add_break_from_id($2, $4, FALSE); }
228     | tHBREAK tNUM              { break_add_break_from_lineno($2, FALSE); }
229     | tHBREAK                   { break_add_break_from_lineno(-1, FALSE); }
230     | tENABLE tNUM              { break_enable_xpoint($2, TRUE); }
231     | tENABLE tBREAK tNUM       { break_enable_xpoint($3, TRUE); }
232     | tDISABLE tNUM             { break_enable_xpoint($2, FALSE); }
233     | tDISABLE tBREAK tNUM      { break_enable_xpoint($3, FALSE); }
234     | tDELETE tNUM              { break_delete_xpoint($2); }
235     | tDELETE tBREAK tNUM       { break_delete_xpoint($3); }
236     ;
237
238 watch_command:
239       tWATCH '*' expr_lvalue    { break_add_watch_from_lvalue(&$3); }
240     | tWATCH identifier         { break_add_watch_from_id($2); }
241     ;
242
243 display_command:
244       tDISPLAY                  { display_print(); }
245     | tDISPLAY expr             { display_add($2, 1, 0); }
246     | tDISPLAY tFORMAT expr     { display_add($3, $2 >> 8, $2 & 0xff); }
247     | tENABLE tDISPLAY tNUM     { display_enable($3, TRUE); }
248     | tDISABLE tDISPLAY tNUM    { display_enable($3, FALSE); }
249     | tDELETE tDISPLAY tNUM     { display_delete($3); }
250     | tDELETE tDISPLAY          { display_delete(-1); }
251     | tUNDISPLAY tNUM           { display_delete($2); }
252     | tUNDISPLAY                { display_delete(-1); }
253     ;
254
255 info_command:
256       tINFO tBREAK              { break_info(); }
257     | tINFO tSHARE              { info_win32_module(0); }
258     | tINFO tSHARE expr_rvalue  { info_win32_module($3); }
259     | tINFO tREGS               { be_cpu->print_context(dbg_curr_thread->handle, &dbg_context); }
260     | tINFO tSEGMENTS expr_rvalue { info_win32_segments($3 >> 3, 1); }
261     | tINFO tSEGMENTS           { info_win32_segments(0, -1); }
262     | tINFO tSTACK              { stack_info(); }
263     | tINFO tSYMBOL tSTRING     { symbol_info($3); }
264     | tINFO tLOCAL              { symbol_info_locals(); }
265     | tINFO tDISPLAY            { display_info(); }
266     | tINFO tCLASS              { info_win32_class(NULL, NULL); }
267     | tINFO tCLASS tSTRING      { info_win32_class(NULL, $3); }
268     | tINFO tWND                { info_win32_window(NULL, FALSE); }
269     | tINFO tWND expr_rvalue    { info_win32_window((HWND)$3, FALSE); }
270     | tINFO '*' tWND            { info_win32_window(NULL, TRUE); }
271     | tINFO '*' tWND expr_rvalue { info_win32_window((HWND)$4, TRUE); }
272     | tINFO tPROCESS            { info_win32_processes(); }
273     | tINFO tTHREAD             { info_win32_threads(); }
274     | tINFO tEXCEPTION          { info_win32_exceptions(dbg_curr_tid); }
275     | tINFO tEXCEPTION expr_rvalue { info_win32_exceptions($3); }
276     | tINFO tMAPS               { info_win32_virtual(dbg_curr_pid); }
277     | tINFO tMAPS expr_rvalue   { info_win32_virtual($3); }
278     ;
279
280 maintenance_command:
281       tMAINTENANCE tTYPE        { print_types(); }
282     ;
283
284 noprocess_state:
285       tNOPROCESS                 {} /* <CR> shall not barf anything */
286     | tNOPROCESS tBACKTRACE tALL { stack_backtrace(-1); } /* can backtrace all threads with no attached process */
287     | tNOPROCESS tSTRING         { dbg_printf("No process loaded, cannot execute '%s'\n", $2); }
288     ;
289
290 type_expr:
291       tCHAR                     { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_char; }
292     | tINT                      { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_int; }
293     | tLONG tINT                { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_long_int; }
294     | tLONG                     { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_long_int; }
295     | tUNSIGNED tINT            { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_int; }
296     | tUNSIGNED                 { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_int; }
297     | tLONG tUNSIGNED tINT      { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_long_int; }
298     | tLONG tUNSIGNED           { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_long_int; }
299     | tSHORT tINT               { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_short_int; }
300     | tSHORT                    { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_short_int; }
301     | tSHORT tUNSIGNED tINT     { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_short_int; }
302     | tSHORT tUNSIGNED          { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_short_int; }
303     | tSIGNED tCHAR             { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_char_int; }
304     | tUNSIGNED tCHAR           { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_char_int; }
305     | tLONG tLONG tUNSIGNED tINT{ $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_longlong_int; }
306     | tLONG tLONG tUNSIGNED     { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_unsigned_longlong_int; }
307     | tLONG tLONG tINT          { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_longlong_int; }
308     | tLONG tLONG               { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_signed_longlong_int; }
309     | tFLOAT                    { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_short_real; }
310     | tDOUBLE                   { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_real; }
311     | tLONG tDOUBLE             { $$.type = type_expr_type_id; $$.deref_count = 0; $$.u.type.module = 0; $$.u.type.id = dbg_itype_long_real; }
312     | type_expr '*'             { $$ = $1; $$.deref_count++; }
313     | tCLASS identifier         { $$.type = type_expr_udt_class; $$.deref_count = 0; $$.u.name = $2; }
314     | tSTRUCT identifier        { $$.type = type_expr_udt_struct; $$.deref_count = 0; $$.u.name = $2; }
315     | tUNION identifier         { $$.type = type_expr_udt_union; $$.deref_count = 0; $$.u.name = $2; }
316     | tENUM identifier          { $$.type = type_expr_enumeration; $$.deref_count = 0; $$.u.name = $2; }
317     ;
318
319 expr_lvalue:
320       expr                      { $$ = expr_eval($1); }
321     ;
322
323 expr_rvalue:
324       expr_lvalue               { $$ = types_extract_as_integer(&$1); }
325     ;
326
327 /*
328  * The expr rule builds an expression tree.  When we are done, we call
329  * EvalExpr to evaluate the value of the expression.  The advantage of
330  * the two-step approach is that it is possible to save expressions for
331  * use in 'display' commands, and in conditional watchpoints.
332  */
333 expr:
334       tNUM                      { $$ = expr_alloc_sconstant($1); }
335     | tSTRING                   { $$ = expr_alloc_string($1); }
336     | tINTVAR                   { $$ = expr_alloc_internal_var($1); }
337     | identifier                { $$ = expr_alloc_symbol($1); }
338     | expr OP_DRF tIDENTIFIER   { $$ = expr_alloc_pstruct($1, $3); }
339     | expr '.' tIDENTIFIER      { $$ = expr_alloc_struct($1, $3); }
340     | identifier '(' ')'        { $$ = expr_alloc_func_call($1, 0); }
341     | identifier '(' expr ')'   { $$ = expr_alloc_func_call($1, 1, $3); }
342     | identifier '(' expr ',' expr ')' { $$ = expr_alloc_func_call($1, 2, $3, $5); }
343     | identifier '(' expr ',' expr ',' expr ')' { $$ = expr_alloc_func_call($1, 3, $3, $5, $7); }
344     | identifier '(' expr ',' expr ',' expr ',' expr ')' { $$ = expr_alloc_func_call($1, 4, $3, $5, $7, $9); }
345     | identifier '(' expr ',' expr ',' expr ',' expr ',' expr ')' { $$ = expr_alloc_func_call($1, 5, $3, $5, $7, $9, $11); }
346     | expr '[' expr ']'          { $$ = expr_alloc_binary_op(EXP_OP_ARR, $1, $3); }
347     | expr ':' expr              { $$ = expr_alloc_binary_op(EXP_OP_SEG, $1, $3); }
348     | expr OP_LOR expr           { $$ = expr_alloc_binary_op(EXP_OP_LOR, $1, $3); }
349     | expr OP_LAND expr          { $$ = expr_alloc_binary_op(EXP_OP_LAND, $1, $3); }
350     | expr '|' expr              { $$ = expr_alloc_binary_op(EXP_OP_OR, $1, $3); }
351     | expr '&' expr              { $$ = expr_alloc_binary_op(EXP_OP_AND, $1, $3); }
352     | expr '^' expr              { $$ = expr_alloc_binary_op(EXP_OP_XOR, $1, $3); }
353     | expr OP_EQ expr            { $$ = expr_alloc_binary_op(EXP_OP_EQ, $1, $3); }
354     | expr '>' expr              { $$ = expr_alloc_binary_op(EXP_OP_GT, $1, $3); }
355     | expr '<' expr              { $$ = expr_alloc_binary_op(EXP_OP_LT, $1, $3); }
356     | expr OP_GE expr            { $$ = expr_alloc_binary_op(EXP_OP_GE, $1, $3); }
357     | expr OP_LE expr            { $$ = expr_alloc_binary_op(EXP_OP_LE, $1, $3); }
358     | expr OP_NE expr            { $$ = expr_alloc_binary_op(EXP_OP_NE, $1, $3); }
359     | expr OP_SHL expr           { $$ = expr_alloc_binary_op(EXP_OP_SHL, $1, $3); }
360     | expr OP_SHR expr           { $$ = expr_alloc_binary_op(EXP_OP_SHR, $1, $3); }
361     | expr '+' expr              { $$ = expr_alloc_binary_op(EXP_OP_ADD, $1, $3); }
362     | expr '-' expr              { $$ = expr_alloc_binary_op(EXP_OP_SUB, $1, $3); }
363     | expr '*' expr              { $$ = expr_alloc_binary_op(EXP_OP_MUL, $1, $3); }
364     | expr '/' expr              { $$ = expr_alloc_binary_op(EXP_OP_DIV, $1, $3); }
365     | expr '%' expr              { $$ = expr_alloc_binary_op(EXP_OP_REM, $1, $3); }
366     | '-' expr %prec OP_SIGN     { $$ = expr_alloc_unary_op(EXP_OP_NEG, $2); }
367     | '+' expr %prec OP_SIGN     { $$ = $2; }
368     | '!' expr                   { $$ = expr_alloc_unary_op(EXP_OP_NOT, $2); }
369     | '~' expr                   { $$ = expr_alloc_unary_op(EXP_OP_LNOT, $2); }
370     | '(' expr ')'               { $$ = $2; }
371     | '*' expr %prec OP_DEREF    { $$ = expr_alloc_unary_op(EXP_OP_DEREF, $2); }
372     | '&' expr %prec OP_DEREF    { $$ = expr_alloc_unary_op(EXP_OP_ADDR, $2); }
373     | '(' type_expr ')' expr %prec OP_DEREF { $$ = expr_alloc_typecast(&$2, $4); }
374     ;
375
376 /*
377  * The lvalue rule builds an expression tree.  This is a limited form
378  * of expression that is suitable to be used as an lvalue.
379  */
380 lvalue_addr: 
381       lvalue                     { $$ = expr_eval($1); }
382     ;
383
384 lvalue: 
385       tNUM                       { $$ = expr_alloc_sconstant($1); }
386     | tINTVAR                    { $$ = expr_alloc_internal_var($1); }
387     | identifier                 { $$ = expr_alloc_symbol($1); }
388     | lvalue OP_DRF tIDENTIFIER  { $$ = expr_alloc_pstruct($1, $3); }
389     | lvalue '.' tIDENTIFIER     { $$ = expr_alloc_struct($1, $3); }
390     | lvalue '[' expr ']'        { $$ = expr_alloc_binary_op(EXP_OP_ARR, $1, $3); }
391     | '*' expr                   { $$ = expr_alloc_unary_op(EXP_OP_FORCE_DEREF, $2); }
392     ;
393
394 %%
395
396 static WINE_EXCEPTION_FILTER(wine_dbg_cmd)
397 {
398     switch (GetExceptionCode())
399     {
400     case DEBUG_STATUS_INTERNAL_ERROR:
401         dbg_printf("\nWineDbg internal error\n");
402         break;
403     case DEBUG_STATUS_NO_SYMBOL:
404         dbg_printf("\nUndefined symbol\n");
405         break;
406     case DEBUG_STATUS_DIV_BY_ZERO:
407         dbg_printf("\nDivision by zero\n");
408         break;
409     case DEBUG_STATUS_BAD_TYPE:
410         dbg_printf("\nNo type or type mismatch\n");
411         break;
412     case DEBUG_STATUS_NO_FIELD:
413         dbg_printf("\nNo such field in structure or union\n");
414         break;
415     case DEBUG_STATUS_CANT_DEREF:
416         dbg_printf("\nDereference failed (not a pointer, or out of array bounds)\n");
417         break;
418     case DEBUG_STATUS_ABORT:
419         break;
420     case DEBUG_STATUS_NOT_AN_INTEGER:
421         dbg_printf("\nNeeding an integral value\n");
422         break;
423     case CONTROL_C_EXIT:
424         /* this is generally sent by a ctrl-c when we run winedbg outside of wineconsole */
425         /* stop the debuggee, and continue debugger execution, we will be reentered by the
426          * debug events generated by stopping
427          */
428         dbg_interrupt_debuggee();
429         return EXCEPTION_CONTINUE_EXECUTION;
430     default:
431         dbg_printf("\nException %lx\n", GetExceptionCode());
432         break;
433     }
434
435     return EXCEPTION_EXECUTE_HANDLER;
436 }
437
438 static HANDLE dbg_parser_input;
439 static HANDLE dbg_parser_output;
440
441 int      input_fetch_entire_line(const char* pfx, char** line)
442 {
443     char        ch;
444     DWORD       nread;
445     size_t      len, alloc;
446     
447     /* as of today, console handles can be file handles... so better use file APIs rather than
448      * console's
449      */
450     WriteFile(dbg_parser_output, pfx, strlen(pfx), &nread, NULL);
451
452     if (*line)
453     {
454         alloc = HeapSize(GetProcessHeap(), 0, *line);
455         assert(alloc);
456     }
457     else
458     {
459         *line = HeapAlloc(GetProcessHeap(), 0, alloc = 16);
460         assert(*line);
461     }
462
463     len = 0;
464     do
465     {
466         if (!ReadFile(dbg_parser_input, &ch, 1, &nread, NULL) || nread == 0)
467             break;
468         if (len + 2 > alloc)
469         {
470             while (len + 2 > alloc) alloc *= 2;
471             *line = dbg_heap_realloc(*line, alloc);
472         }
473         (*line)[len++] = ch;
474     }
475     while (ch != '\n');
476     (*line)[len] = '\0';
477
478     return len;
479 }
480
481 int input_read_line(const char* pfx, char* buf, int size)
482 {
483     char*       line = NULL;
484     size_t      len = 0;
485
486     len = input_fetch_entire_line(pfx, &line);
487     /* remove trailing \n */
488     if (len > 0 && line[len - 1] == '\n') len--;
489     len = min(size - 1, len);
490     memcpy(buf, line, len);
491     buf[len] = '\0';
492     HeapFree(GetProcessHeap(), 0, line);
493     return 1;
494 }
495
496 /***********************************************************************
497  *           parser_handle
498  *
499  * Debugger command line parser
500  */
501 void    parser_handle(HANDLE input)
502 {
503     BOOL                ret_ok;
504     HANDLE              in_copy  = dbg_parser_input;
505     HANDLE              out_copy = dbg_parser_output;
506
507 #ifdef YYDEBUG
508     yydebug = 0;
509 #endif
510
511     ret_ok = FALSE;
512
513     if (input != INVALID_HANDLE_VALUE)
514     {
515         dbg_parser_output = INVALID_HANDLE_VALUE;
516         dbg_parser_input  = input;
517     }
518     else
519     {
520         dbg_parser_output = GetStdHandle(STD_OUTPUT_HANDLE);
521         dbg_parser_input  = GetStdHandle(STD_INPUT_HANDLE);
522     }
523
524     do
525     {
526        __TRY
527        {
528           ret_ok = TRUE;
529           yyparse();
530        }
531        __EXCEPT(wine_dbg_cmd)
532        {
533           ret_ok = FALSE;
534        }
535        __ENDTRY;
536        lexeme_flush();
537     } while (!ret_ok);
538
539     dbg_parser_input  = in_copy;
540     dbg_parser_output = out_copy;
541 }
542
543 void parser(const char* filename)
544 {
545     HANDLE h = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0L, 0);
546     if (h != INVALID_HANDLE_VALUE)
547     {
548         parser_handle(h);
549         CloseHandle(h);
550     }
551 }
552
553 int yyerror(const char* s)
554 {
555     dbg_printf("%s\n", s);
556     return 0;
557 }
558
559 HANDLE parser_generate_command_file(const char* pmt, ...)
560 {
561     HANDLE      hFile;
562     char        path[MAX_PATH], file[MAX_PATH];
563     DWORD       w;
564     const char* p;
565
566     GetTempPath(sizeof(path), path);
567     GetTempFileName(path, "WD", 0, file);
568     hFile = CreateFileA(file, GENERIC_READ|GENERIC_WRITE|DELETE, FILE_SHARE_DELETE, 
569                         NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0);
570     if (hFile != INVALID_HANDLE_VALUE)
571     {
572         va_list ap;
573
574         WriteFile(hFile, pmt, strlen(pmt), &w, 0);
575         va_start(ap, pmt);
576         while ((p = va_arg(ap, const char*)) != NULL)
577         {
578             WriteFile(hFile, "\n", 1, &w, 0);
579             WriteFile(hFile, p, strlen(p), &w, 0);
580         }
581         va_end(ap);
582         WriteFile(hFile, "\nquit\n", 6, &w, 0);
583         SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
584     }
585     return hFile;
586 }