Properly handle loadorder specifications containing a .dll extension.
[wine] / debugger / debug.l
1 /* -*-C-*-
2  * Lexical scanner for command line parsing
3  *
4  * Copyright 1993 Eric Youngdale
5  *           2000 Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 %{
23 #include <stdlib.h>
24 #include <string.h>
25 #include "winbase.h"
26 #include "wincon.h"
27 #include "debugger.h"
28 #include "y.tab.h"
29
30 #undef YY_INPUT
31 #define YY_INPUT(buf,result,max_size) \
32         if ( (result = DEBUG_ReadLine("Wine-dbg>", (char *) buf, max_size, TRUE, TRUE )) < 0 ) \
33             YY_FATAL_ERROR( "read() in flex scanner failed" );
34
35
36 #define YY_NO_UNPUT
37
38 static int syntax_error;
39 %}
40
41 DIGIT      [0-9]
42 HEXDIGIT   [0-9a-fA-F]
43 FORMAT     [ubcdiswx]
44 IDENTIFIER [_a-zA-Z~][_a-zA-Z0-9~@]*
45 PATHNAME   [/_a-zA-Z\.~][/_a-zA-Z0-9\.~@]*
46 STRING     \"[^\n"]+\"
47
48 %s FORMAT_EXPECTED
49 %s PATH_EXPECTED
50 %s INFO_CMD
51 %s HELP_CMD
52 %s DEL_CMD
53 %s WALK_CMD
54 %s SHOW_CMD
55 %s MODE_CMD
56 %s NOCMD
57
58 %x ASTRING_EXPECTED
59 %x NOPROCESS
60 %%
61                                         /* set to special state when no process is loaded. */
62                                         if (!DEBUG_CurrProcess && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
63
64 <*>\n                                   { BEGIN(INITIAL); syntax_error = 0; return tEOL; } 
65                                         /* Indicates end of command. Reset state. */
66
67 "||"                                    { return OP_LOR; }
68 "&&"                                    { return OP_LAND; }
69 "=="                                    { return OP_EQ; }
70 "!="                                    { return OP_NE; }
71 "<="                                    { return OP_LE; }
72 ">="                                    { return OP_GE; }
73 "<<"                                    { return OP_SHL; }
74 ">>"                                    { return OP_SHR; }
75 "->"                                    { return OP_DRF; }
76 [-+<=>|&^()*/%:!~,\.]                   { return *yytext; }
77 "["                                     { return *yytext; }
78 "]"                                     { return *yytext; }
79
80 "0x"{HEXDIGIT}+                         { sscanf(yytext, "%x", &yylval.integer); return tNUM; }
81 {DIGIT}+                                { sscanf(yytext, "%d", &yylval.integer); return tNUM; }
82
83 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT}    { char* last;
84                                           yylval.integer = strtol( yytext+1, &last, 0 ) << 8;
85                                           yylval.integer |= *last;
86                                           return tFORMAT; }
87
88 <FORMAT_EXPECTED>"/"{FORMAT}            { yylval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
89
90 {STRING}                                { yylval.string = DEBUG_MakeSymbol(yytext); return tSTRING; }
91 <ASTRING_EXPECTED>[^\n]+                { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
92                                           yylval.string = DEBUG_MakeSymbol(p); return tSTRING; }
93
94 <INITIAL>info|inf|in                    { BEGIN(INFO_CMD); return tINFO; }
95 <INITIAL>up                             { BEGIN(NOCMD); return tUP; }
96 <INITIAL>down|dow|do                    { BEGIN(NOCMD); return tDOWN; }
97 <INITIAL>frame|fram|fra|fr              { BEGIN(NOCMD); return tFRAME; }
98 <INITIAL>list|lis|li|l                  { BEGIN(PATH_EXPECTED); return tLIST; }
99 <INITIAL>enable|enabl|enab|ena          { BEGIN(NOCMD); return tENABLE;}
100 <INITIAL>disable|disabl|disab|disa|dis  { BEGIN(NOCMD); return tDISABLE; }
101 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
102 <INITIAL,INFO_CMD,DEL_CMD>display|displa|displ|disp     { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
103 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und       { BEGIN(NOCMD); return tUNDISPLAY; }
104 <INITIAL>delete|delet|dele|del          { BEGIN(DEL_CMD); return tDELETE; }
105 <INITIAL,NOPROCESS>quit|qui|qu|q        { BEGIN(NOCMD); return tQUIT; }
106 <INITIAL>set|se                         { BEGIN(NOCMD); return tSET; }
107 <INITIAL,NOPROCESS>walk|w               { BEGIN(WALK_CMD); return tWALK; }
108 <INITIAL>x                              { BEGIN(FORMAT_EXPECTED); return tEXAM; }
109 <INITIAL,NOPROCESS>help|hel|he|"?"              { BEGIN(HELP_CMD); return tHELP; }
110
111 <INITIAL>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
112 <INITIAL>where|wher|whe                 { BEGIN(NOCMD); return tBACKTRACE; }
113
114 <INITIAL>cont|con|co|c                  { BEGIN(NOCMD); return tCONT; }
115 <INITIAL>pass|pas|pa                    { BEGIN(NOCMD); return tPASS; }
116 <INITIAL>condition|conditio|conditi|condit|condi|cond   { BEGIN(NOCMD); return tCOND; }
117 <INITIAL>step|ste|st|s                  { BEGIN(NOCMD); return tSTEP; }
118 <INITIAL>next|nex|ne|n                  { BEGIN(NOCMD); return tNEXT; }
119 <INITIAL>stepi|si                       { BEGIN(NOCMD); return tSTEPI; }
120 <INITIAL>nexti|ni                       { BEGIN(NOCMD); return tNEXTI; }
121 <INITIAL>finish|finis|fini|fin|fi       { BEGIN(NOCMD); return tFINISH; }
122
123 <INITIAL>abort|abor|abo                 { BEGIN(NOCMD); return tABORT; }
124 <INITIAL>print|prin|pri|pr|p            { BEGIN(FORMAT_EXPECTED); return tPRINT; }
125
126 <INITIAL>mode                           { BEGIN(MODE_CMD); return tMODE; }
127 <INITIAL>show|sho|sh                    { BEGIN(SHOW_CMD); return tSHOW; }
128 <INITIAL>symbolfile|symbols|symbol|sf   { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
129
130 <INITIAL,INFO_CMD,DEL_CMD>break|brea|bre|br|b   { BEGIN(NOCMD); return tBREAK; }
131 <INITIAL>watch|watc|wat                 { BEGIN(NOCMD); return tWATCH; }
132 <INITIAL>whatis|whati|what              { BEGIN(NOCMD); return tWHATIS; }
133 <INITIAL,NOPROCESS>run|ru|r             { BEGIN(ASTRING_EXPECTED); return tRUN;}
134 <INITIAL>detach|detac|deta|det          { BEGIN(NOCMD); return tDETACH; }
135 <NOPROCESS>attach|attac|atta|att        { BEGIN(NOCMD); return tATTACH; }
136 <INFO_CMD>share|shar|sha                { return tSHARE; }
137 <INFO_CMD>locals|local|loca|loc         { return tLOCAL; }
138 <INFO_CMD,WALK_CMD>class|clas|cla       { return tCLASS; }
139 <INFO_CMD,WALK_CMD>module|modul|modu|mod { return tMODULE; }
140 <INFO_CMD,WALK_CMD>queue|queu|que       { return tQUEUE; }
141 <INFO_CMD,WALK_CMD>process|proces|proce|proc            { return tPROCESS; }
142 <INFO_CMD,WALK_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
143 <INFO_CMD,WALK_CMD>modref|modre|modr    { return tMODREF; }
144 <INFO_CMD>registers|regs|reg|re         { return tREGS; }
145 <INFO_CMD>segments|segment|segm|seg|se  { return tSEGMENTS; }
146 <INFO_CMD>stack|stac|sta|st             { return tSTACK; }
147 <INFO_CMD>maps|map                      { return tMAPS; }
148 <INFO_CMD,WALK_CMD>window|windo|wind|win|wnd    { return tWND; }
149 <HELP_CMD>info|inf|in                   { return tINFO; }
150 <MODE_CMD>vm86                          { return tVM86; }
151
152 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir { 
153                                           BEGIN(PATH_EXPECTED); return tDIR; }
154
155 char                                    { return tCHAR; }
156 short                                   { return tSHORT; }
157 int                                     { return tINT; }
158 long                                    { return tLONG; }
159 float                                   { return tFLOAT; }
160 double                                  { return tDOUBLE; }
161 unsigned                                { return tUNSIGNED; }
162 signed                                  { return tSIGNED; }
163 struct                                  { return tSTRUCT; }
164 union                                   { return tUNION; }
165 enum                                    { return tENUM; }
166
167 {IDENTIFIER}                            { yylval.string = DEBUG_MakeSymbol(yytext); return tIDENTIFIER; }
168 "$"{IDENTIFIER}                         { yylval.string = DEBUG_MakeSymbol(yytext+1); return tINTVAR; }
169
170 <PATH_EXPECTED>{PATHNAME}               { yylval.string = DEBUG_MakeSymbol(yytext); return tPATH; }
171
172 <*>[ \t]+                               /* Eat up whitespace */ 
173
174 <NOPROCESS>.                            { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
175 <*>.                                    { if (syntax_error == 0) {
176                                              syntax_error++; 
177                                              DEBUG_Printf(DBG_CHN_MESG, "Syntax Error (%s)\n", yytext); }
178                                         }
179 %%
180
181 #ifndef yywrap
182 int yywrap(void) { return 1; }
183 #endif
184
185 #ifndef whitespace
186 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
187 #endif
188
189
190 /* Strip whitespace from the start and end of STRING. */
191 static void stripwhite (char *string)
192 {
193   register int i = 0;
194
195   while (whitespace (string[i]))
196     i++;
197
198   if (i)
199     strcpy (string, string + i);
200
201   i = strlen (string) - 1;
202
203   while (i > 0 && whitespace (string[i]))
204     i--;
205
206   string[++i] = '\0';
207 }
208
209 int DEBUG_ReadLine(const char* pfx, char * buf, int size, int flush_sym, int keep_hist)
210 {
211     char        buf_line[256];
212     char*       ptr;
213     int         len;
214     DWORD       nread;
215
216     for (;;)
217     {
218         if (flush_sym) DEBUG_FlushSymbols();
219         /* as of today, console handles can be file handles... so better use file APIs rather than
220          * consoles
221          */
222         WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), pfx, strlen(pfx), &nread, NULL);
223
224         if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf_line, sizeof(buf_line), &nread, NULL))
225             break;
226         /* FIXME: should be rewritten not to remove and then add the trailing '\n' */
227         if (nread > 0 && buf_line[nread - 1] == '\n') nread--;
228         buf_line[nread] = 0;
229
230         /* Remove leading and trailing whitespace from the line */
231         stripwhite (buf_line);
232
233         if (keep_hist)
234         {
235             static char last_line[256] = "";
236             /* If there is anything left, add it to the history list
237                and execute it. Otherwise, re-execute last command. */
238         
239             if (*buf_line)
240             {
241                 strncpy( last_line, buf_line, sizeof(last_line) - 1 );
242                 last_line[sizeof(last_line) - 1] = '\0'; 
243             }
244             ptr = last_line;
245         }
246         else
247         {
248             /* I should also tweak with the undoc functions to remove this line from the console
249              * history... */
250             ptr = buf_line;
251         }
252
253         if ((len = strlen(ptr)) > 0)
254         {
255             if (size < len + 1)
256             {
257                 DEBUG_Printf(DBG_CHN_MESG, "Fatal readline goof.\n");
258                 DEBUG_Exit(0);
259             }
260             strcpy(buf, ptr);
261             buf[len] = '\n';
262             buf[len+1] = 0;
263             return len + 1;
264         }
265     }
266     return 0;
267 }
268
269 static char *local_symbols[30];
270 static int next_symbol;
271
272 char * DEBUG_MakeSymbol(const char * symbol)
273 {
274         assert(0 <= next_symbol && next_symbol < (sizeof(local_symbols) / sizeof(local_symbols[0])));
275         return local_symbols[next_symbol++] = DBG_strdup(symbol);
276 }
277
278 void DEBUG_FlushSymbols(void)
279 {
280         while(--next_symbol >= 0) DBG_free(local_symbols[next_symbol]);
281         next_symbol = 0;
282 }