Moved a number of 16-bit functions to file16.c.
[wine.git] / programs / winedbg / debug.l
blobb582a7f8cd430ad9975c1e91f32f561e6a92c778
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  */
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wincon.h"
30 #include "debugger.h"
31 #include "y.tab.h"
33 #undef YY_INPUT
35 HANDLE DEBUG_hParserInput;
36 HANDLE DEBUG_hParserOutput;
38 static int DEBUG_FetchFromLine(const char* pfx, char* buf, int size);
40 #define YY_INPUT(buf,result,max_size) \
41         if ( (result = DEBUG_FetchFromLine("Wine-dbg>", buf, max_size)) < 0 ) \
42             YY_FATAL_ERROR( "FetchFromLine() in flex scanner failed" );
44 #define YY_NO_UNPUT
46 static int syntax_error;
49 DIGIT      [0-9]
50 HEXDIGIT   [0-9a-fA-F]
51 FORMAT     [ubcdgiswx]
52 IDENTIFIER [_a-zA-Z~][_a-zA-Z0-9~@]*
53 PATHNAME   [/_a-zA-Z\.~][/_a-zA-Z0-9\.~@]*
54 STRING     \"[^\n"]+\"
56 %s FORMAT_EXPECTED
57 %s PATH_EXPECTED
58 %s INFO_CMD
59 %s HELP_CMD
60 %s BD_CMD
61 %s LOCAL_CMD
62 %s WALK_CMD
63 %s SHOW_CMD
64 %s MODE_CMD
65 %s MAINT_CMD
66 %s NOCMD
68 %x ASTRING_EXPECTED
69 %x NOPROCESS
71                                         /* set to special state when no process is loaded. */
72                                         if (!DEBUG_CurrProcess && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
74 <<EOF>>                                 { return tEOF; }
75 <*>\n                                   { BEGIN(INITIAL); syntax_error = 0; return tEOL; }
76                                         /* Indicates end of command. Reset state. */
78 "||"                                    { return OP_LOR; }
79 "&&"                                    { return OP_LAND; }
80 "=="                                    { return OP_EQ; }
81 "!="                                    { return OP_NE; }
82 "<="                                    { return OP_LE; }
83 ">="                                    { return OP_GE; }
84 "<<"                                    { return OP_SHL; }
85 ">>"                                    { return OP_SHR; }
86 "->"                                    { return OP_DRF; }
87 [-+<=>|&^()*/%:!~,\.]                   { return *yytext; }
88 "["                                     { return *yytext; }
89 "]"                                     { return *yytext; }
91 "0x"{HEXDIGIT}+                         { sscanf(yytext, "%x", &yylval.integer); return tNUM; }
92 {DIGIT}+                                { sscanf(yytext, "%d", &yylval.integer); return tNUM; }
94 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT}    { char* last;
95                                           yylval.integer = strtol( yytext+1, &last, 0 ) << 8;
96                                           yylval.integer |= *last;
97                                           return tFORMAT; }
99 <FORMAT_EXPECTED>"/"{FORMAT}            { yylval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
101 {STRING}                                { yylval.string = DEBUG_MakeSymbol(yytext); return tSTRING; }
102 <ASTRING_EXPECTED>[^\n]+                { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
103                                           yylval.string = DEBUG_MakeSymbol(p); return tSTRING; }
105 <INITIAL>info|inf|in                    { BEGIN(INFO_CMD); return tINFO; }
106 <INITIAL>up                             { BEGIN(NOCMD); return tUP; }
107 <INITIAL>down|dow|do                    { BEGIN(NOCMD); return tDOWN; }
108 <INITIAL>frame|fram|fra|fr              { BEGIN(NOCMD); return tFRAME; }
109 <INITIAL>list|lis|li|l                  { BEGIN(PATH_EXPECTED); return tLIST; }
110 <INITIAL>enable|enabl|enab|ena          { BEGIN(BD_CMD); return tENABLE;}
111 <INITIAL>disable|disabl|disab|disa|dis  { BEGIN(BD_CMD); return tDISABLE; }
112 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
113 <INITIAL>locally|local                  { BEGIN(LOCAL_CMD); return tLOCAL; }
114 <INITIAL,LOCAL_CMD>display|displa|displ|disp    { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
115 <INFO_CMD,BD_CMD>display|displa|displ|disp|dis|di|d     { BEGIN(NOCMD); return tDISPLAY; }
116 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und       { BEGIN(NOCMD); return tUNDISPLAY; }
117 <INITIAL>delete|delet|dele|del          { BEGIN(BD_CMD); return tDELETE; }
118 <INITIAL,NOPROCESS>quit|qui|qu|q        { BEGIN(NOCMD); return tQUIT; }
119 <INITIAL>set|se                         { BEGIN(NOCMD); return tSET; }
120 <INITIAL,NOPROCESS>walk|w               { BEGIN(WALK_CMD); return tWALK; }
121 <INITIAL>x                              { BEGIN(FORMAT_EXPECTED); return tEXAM; }
122 <INITIAL,NOPROCESS>help|hel|he|"?"              { BEGIN(HELP_CMD); return tHELP; }
124 <INITIAL>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
125 <INITIAL>where|wher|whe                 { BEGIN(NOCMD); return tBACKTRACE; }
127 <INITIAL>cont|con|co|c                  { BEGIN(NOCMD); return tCONT; }
128 <INITIAL>pass|pas|pa                    { BEGIN(NOCMD); return tPASS; }
129 <INITIAL>condition|conditio|conditi|condit|condi|cond   { BEGIN(NOCMD); return tCOND; }
130 <INITIAL>step|ste|st|s                  { BEGIN(NOCMD); return tSTEP; }
131 <INITIAL>next|nex|ne|n                  { BEGIN(NOCMD); return tNEXT; }
132 <INITIAL>stepi|si                       { BEGIN(NOCMD); return tSTEPI; }
133 <INITIAL>nexti|ni                       { BEGIN(NOCMD); return tNEXTI; }
134 <INITIAL>finish|finis|fini|fin|fi       { BEGIN(NOCMD); return tFINISH; }
136 <INITIAL>abort|abor|abo                 { BEGIN(NOCMD); return tABORT; }
137 <INITIAL>print|prin|pri|pr|p            { BEGIN(FORMAT_EXPECTED); return tPRINT; }
139 <INITIAL>mode                           { BEGIN(MODE_CMD); return tMODE; }
140 <INITIAL>show|sho|sh                    { BEGIN(SHOW_CMD); return tSHOW; }
141 <INITIAL,NOPROCESS>source|sourc|sour|src { BEGIN(PATH_EXPECTED); return tSOURCE; }
142 <INITIAL>symbolfile|symbols|symbol|sf   { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
144 <INITIAL,INFO_CMD,BD_CMD>break|brea|bre|br|b    { BEGIN(NOCMD); return tBREAK; }
145 <INITIAL>watch|watc|wat                 { BEGIN(NOCMD); return tWATCH; }
146 <INITIAL>whatis|whati|what              { BEGIN(NOCMD); return tWHATIS; }
147 <INITIAL,NOPROCESS>run|ru|r             { BEGIN(ASTRING_EXPECTED); return tRUN;}
148 <INITIAL>detach|detac|deta|det          { BEGIN(NOCMD); return tDETACH; }
149 <INITIAL>maintenance|maint              { BEGIN(MAINT_CMD); return tMAINTENANCE; }
150 <NOPROCESS>attach|attac|atta|att        { BEGIN(NOCMD); return tATTACH; }
151 <INFO_CMD>share|shar|sha                { return tSHARE; }
152 <INFO_CMD>locals|local|loca|loc         { return tLOCAL; }
153 <INFO_CMD,WALK_CMD>class|clas|cla       { return tCLASS; }
154 <INFO_CMD,WALK_CMD>module|modul|modu|mod { return tMODULE; }
155 <INFO_CMD,WALK_CMD>process|proces|proce|proc            { return tPROCESS; }
156 <INFO_CMD,WALK_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
157 <WALK_CMD>exception|except|exc|ex       { return tEXCEPTION; }
158 <INFO_CMD>registers|regs|reg|re         { return tREGS; }
159 <INFO_CMD>segments|segment|segm|seg|se  { return tSEGMENTS; }
160 <INFO_CMD>stack|stac|sta|st             { return tSTACK; }
161 <INFO_CMD>symbol|symbo|symb|sym         { BEGIN(ASTRING_EXPECTED); return tSYMBOL; }
162 <WALK_CMD>maps|map                      { return tMAPS; }
163 <INFO_CMD,WALK_CMD>window|windo|wind|win|wnd    { return tWND; }
164 <HELP_CMD>info|inf|in                   { return tINFO; }
165 <MODE_CMD>vm86                          { return tVM86; }
166 <MAINT_CMD>type                         { return tTYPE; }
168 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir {
169                                           BEGIN(PATH_EXPECTED); return tDIR; }
171 char                                    { return tCHAR; }
172 short                                   { return tSHORT; }
173 int                                     { return tINT; }
174 long                                    { return tLONG; }
175 float                                   { return tFLOAT; }
176 double                                  { return tDOUBLE; }
177 unsigned                                { return tUNSIGNED; }
178 signed                                  { return tSIGNED; }
179 struct                                  { return tSTRUCT; }
180 union                                   { return tUNION; }
181 enum                                    { return tENUM; }
183 {IDENTIFIER}                            { yylval.string = DEBUG_MakeSymbol(yytext); return tIDENTIFIER; }
184 "$"{IDENTIFIER}                         { yylval.string = DEBUG_MakeSymbol(yytext+1); return tINTVAR; }
186 <PATH_EXPECTED>{PATHNAME}               { yylval.string = DEBUG_MakeSymbol(yytext); return tPATH; }
188 <*>[ \t]+                               /* Eat up whitespace */
190 <NOPROCESS>.                            { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
191 <*>.                                    { if (syntax_error == 0) {
192                                              syntax_error++;
193                                              DEBUG_Printf("Syntax Error (%s)\n", yytext); }
194                                         }
197 #ifndef yywrap
198 int yywrap(void) { return 1; }
199 #endif
201 #ifndef whitespace
202 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
203 #endif
206 /* Strip whitespace from the start and end of STRING. */
207 static void stripwhite(char *string)
209     int         i, last;
211     for (i = 0; whitespace(string[i]); i++);
212     if (i) strcpy(string, string + i);
214     last = i = strlen(string) - 1;
215     if (string[last] == '\n') i--;
217     while (i > 0 && whitespace(string[i])) i--;
218     if (string[last] == '\n')
219         string[++i] = '\n';
220     string[++i] = '\0';
223 static int      DEBUG_FetchEntireLine(const char* pfx, char** line, size_t* alloc, BOOL check_nl)
225     char        buf_line[256];
226     DWORD       nread;
227     size_t      len;
229     /* as of today, console handles can be file handles... so better use file APIs rather than
230      * consoles
231      */
232     WriteFile(DEBUG_hParserOutput, pfx, strlen(pfx), NULL, NULL);
234     len = 0;
235     do
236     {
237         if (!ReadFile(DEBUG_hParserInput, buf_line, sizeof(buf_line) - 1, &nread, NULL) || nread == 0)
238             break;
239         buf_line[nread] = '\0';
241         if (check_nl && len == 0 && nread == 1 && buf_line[0] == '\n')
242             return 0;
244         /* store stuff at the end of last_line */
245         if (len + nread + 1 > *alloc)
246         {
247             if (*line)
248                 *line = HeapReAlloc(GetProcessHeap(), 0, *line, *alloc += nread + 1);
249             else
250                 *line = HeapAlloc(GetProcessHeap(), 0, *alloc += nread + 1);
251         }
252         strcpy(*line + len, buf_line);
253         len += nread;
254     } while (nread == 0 || buf_line[nread - 1] != '\n');
256     if (!len)
257     {
258         *line = HeapReAlloc(GetProcessHeap(), 0, *line, *alloc = 1);
259         **line = '\0';
260     }
262     /* Remove leading and trailing whitespace from the line */
263     stripwhite(*line);
264     return 1;
267 static int DEBUG_FetchFromLine(const char* pfx, char* buf, int size)
269     size_t      len;
270 static char*  last_line = NULL;
271 static size_t last_line_size = 0;
272 static size_t last_line_idx = 0;
274     /* first alloc of our current buffer */
275     if (!last_line)
276     {
277         last_line = HeapAlloc(GetProcessHeap(), 0, last_line_size = 2);
278         assert(last_line);
279         last_line[0] = '\n';
280         last_line[1] = '\0';
281     }
283     /* try first to fetch the remaining of an existing line */
284     if (last_line_idx == 0)
285     {
286         /* no remaining chars to be read from last line, grab a brand new line up to '\n' */
287         DEBUG_FlushSymbols();
289         DEBUG_FetchEntireLine(pfx, &last_line, &last_line_size, TRUE);
290     }
292     len = min(strlen(last_line + last_line_idx), size - 1);
293     memcpy(buf, last_line + last_line_idx, len);
294     buf[len] = '\0';
295     if ((last_line_idx += len) >= strlen(last_line))
296         last_line_idx = 0;
297     return len;
300 int DEBUG_ReadLine(const char* pfx, char* buf, int size)
302     char*       line = NULL;
303     size_t      len = 0;
305     /* first alloc of our current buffer */
306     line = HeapAlloc(GetProcessHeap(), 0, len = 2);
307     assert(line);
308     line[0] = '\n';
309     line[1] = '\0';                   
310         
311     DEBUG_FetchEntireLine(pfx, &line, &len, FALSE);
312     len = strlen(line);
313     /* remove trailing \n */
314     if (len > 0 && line[len - 1] == '\n') len--;
315     len = min(size - 1, len);
316     memcpy(buf, line, len);
317     buf[len] = '\0';
318     HeapFree(GetProcessHeap(), 0, line);
319     return 1;
322 static char** local_symbols /* = NULL */;
323 static int next_symbol /* = 0 */;
324 static int alloc_symbol /* = 0 */;
326 char* DEBUG_MakeSymbol(const char* symbol)
328     assert(0 <= next_symbol && next_symbol < alloc_symbol + 1);
329     if (next_symbol >= alloc_symbol)
330     {
331         if (!local_symbols)
332             local_symbols = HeapAlloc(GetProcessHeap(), 0,
333                                       (alloc_symbol += 32) * sizeof(local_symbols[0]));
334         else
335             local_symbols = HeapReAlloc(GetProcessHeap(), 0, local_symbols,
336                                         (alloc_symbol += 32) * sizeof(local_symbols[0]));
337         assert(local_symbols);
338     }
339     return local_symbols[next_symbol++] = DBG_strdup(symbol);
342 void DEBUG_FlushSymbols(void)
344     while(--next_symbol >= 0) DBG_free(local_symbols[next_symbol]);
345     next_symbol = 0;