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