Fixed format string.
[wine/multimedia.git] / debugger / debug.l
blobd1c4f78211ab9f9cc1bedcbc92f4808b16585f1a
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 "winbase.h"
26 #include "wincon.h"
27 #include "debugger.h"
28 #include "y.tab.h"
30 #undef YY_INPUT
32 static int DEBUG_FetchFromLine(const char* pfx, char* buf, int size);
34 #define YY_INPUT(buf,result,max_size) \
35         if ( (result = DEBUG_FetchFromLine("Wine-dbg>", buf, max_size)) <= 0 ) \
36             YY_FATAL_ERROR( "ReadLine() in flex scanner failed" );
39 #define YY_NO_UNPUT
41 static int syntax_error;
44 DIGIT      [0-9]
45 HEXDIGIT   [0-9a-fA-F]
46 FORMAT     [ubcdgiswx]
47 IDENTIFIER [_a-zA-Z~][_a-zA-Z0-9~@]*
48 PATHNAME   [/_a-zA-Z\.~][/_a-zA-Z0-9\.~@]*
49 STRING     \"[^\n"]+\"
51 %s FORMAT_EXPECTED
52 %s PATH_EXPECTED
53 %s INFO_CMD
54 %s HELP_CMD
55 %s DEL_CMD
56 %s WALK_CMD
57 %s SHOW_CMD
58 %s MODE_CMD
59 %s NOCMD
61 %x ASTRING_EXPECTED
62 %x NOPROCESS
64                                         /* set to special state when no process is loaded. */
65                                         if (!DEBUG_CurrProcess && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
67 <*>\n                                   { BEGIN(INITIAL); syntax_error = 0; return tEOL; }
68                                         /* Indicates end of command. Reset state. */
70 "||"                                    { return OP_LOR; }
71 "&&"                                    { return OP_LAND; }
72 "=="                                    { return OP_EQ; }
73 "!="                                    { return OP_NE; }
74 "<="                                    { return OP_LE; }
75 ">="                                    { return OP_GE; }
76 "<<"                                    { return OP_SHL; }
77 ">>"                                    { return OP_SHR; }
78 "->"                                    { return OP_DRF; }
79 [-+<=>|&^()*/%:!~,\.]                   { return *yytext; }
80 "["                                     { return *yytext; }
81 "]"                                     { return *yytext; }
83 "0x"{HEXDIGIT}+                         { sscanf(yytext, "%x", &yylval.integer); return tNUM; }
84 {DIGIT}+                                { sscanf(yytext, "%d", &yylval.integer); return tNUM; }
86 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT}    { char* last;
87                                           yylval.integer = strtol( yytext+1, &last, 0 ) << 8;
88                                           yylval.integer |= *last;
89                                           return tFORMAT; }
91 <FORMAT_EXPECTED>"/"{FORMAT}            { yylval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
93 {STRING}                                { yylval.string = DEBUG_MakeSymbol(yytext); return tSTRING; }
94 <ASTRING_EXPECTED>[^\n]+                { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
95                                           yylval.string = DEBUG_MakeSymbol(p); return tSTRING; }
97 <INITIAL>info|inf|in                    { BEGIN(INFO_CMD); return tINFO; }
98 <INITIAL>up                             { BEGIN(NOCMD); return tUP; }
99 <INITIAL>down|dow|do                    { BEGIN(NOCMD); return tDOWN; }
100 <INITIAL>frame|fram|fra|fr              { BEGIN(NOCMD); return tFRAME; }
101 <INITIAL>list|lis|li|l                  { BEGIN(PATH_EXPECTED); return tLIST; }
102 <INITIAL>enable|enabl|enab|ena          { BEGIN(NOCMD); return tENABLE;}
103 <INITIAL>disable|disabl|disab|disa|dis  { BEGIN(NOCMD); return tDISABLE; }
104 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
105 <INITIAL,INFO_CMD,DEL_CMD>display|displa|displ|disp     { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
106 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und       { BEGIN(NOCMD); return tUNDISPLAY; }
107 <INITIAL>delete|delet|dele|del          { BEGIN(DEL_CMD); return tDELETE; }
108 <INITIAL,NOPROCESS>quit|qui|qu|q        { BEGIN(NOCMD); return tQUIT; }
109 <INITIAL>set|se                         { BEGIN(NOCMD); return tSET; }
110 <INITIAL,NOPROCESS>walk|w               { BEGIN(WALK_CMD); return tWALK; }
111 <INITIAL>x                              { BEGIN(FORMAT_EXPECTED); return tEXAM; }
112 <INITIAL,NOPROCESS>help|hel|he|"?"              { BEGIN(HELP_CMD); return tHELP; }
114 <INITIAL>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
115 <INITIAL>where|wher|whe                 { BEGIN(NOCMD); return tBACKTRACE; }
117 <INITIAL>cont|con|co|c                  { BEGIN(NOCMD); return tCONT; }
118 <INITIAL>pass|pas|pa                    { BEGIN(NOCMD); return tPASS; }
119 <INITIAL>condition|conditio|conditi|condit|condi|cond   { BEGIN(NOCMD); return tCOND; }
120 <INITIAL>step|ste|st|s                  { BEGIN(NOCMD); return tSTEP; }
121 <INITIAL>next|nex|ne|n                  { BEGIN(NOCMD); return tNEXT; }
122 <INITIAL>stepi|si                       { BEGIN(NOCMD); return tSTEPI; }
123 <INITIAL>nexti|ni                       { BEGIN(NOCMD); return tNEXTI; }
124 <INITIAL>finish|finis|fini|fin|fi       { BEGIN(NOCMD); return tFINISH; }
126 <INITIAL>abort|abor|abo                 { BEGIN(NOCMD); return tABORT; }
127 <INITIAL>print|prin|pri|pr|p            { BEGIN(FORMAT_EXPECTED); return tPRINT; }
129 <INITIAL>mode                           { BEGIN(MODE_CMD); return tMODE; }
130 <INITIAL>show|sho|sh                    { BEGIN(SHOW_CMD); return tSHOW; }
131 <INITIAL>symbolfile|symbols|symbol|sf   { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
133 <INITIAL,INFO_CMD,DEL_CMD>break|brea|bre|br|b   { BEGIN(NOCMD); return tBREAK; }
134 <INITIAL>watch|watc|wat                 { BEGIN(NOCMD); return tWATCH; }
135 <INITIAL>whatis|whati|what              { BEGIN(NOCMD); return tWHATIS; }
136 <INITIAL,NOPROCESS>run|ru|r             { BEGIN(ASTRING_EXPECTED); return tRUN;}
137 <INITIAL>detach|detac|deta|det          { BEGIN(NOCMD); return tDETACH; }
138 <NOPROCESS>attach|attac|atta|att        { BEGIN(NOCMD); return tATTACH; }
139 <INFO_CMD>share|shar|sha                { return tSHARE; }
140 <INFO_CMD>locals|local|loca|loc         { return tLOCAL; }
141 <INFO_CMD,WALK_CMD>class|clas|cla       { return tCLASS; }
142 <INFO_CMD,WALK_CMD>module|modul|modu|mod { return tMODULE; }
143 <INFO_CMD,WALK_CMD>queue|queu|que       { return tQUEUE; }
144 <INFO_CMD,WALK_CMD>process|proces|proce|proc            { return tPROCESS; }
145 <INFO_CMD,WALK_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
146 <INFO_CMD,WALK_CMD>modref|modre|modr    { return tMODREF; }
147 <INFO_CMD>registers|regs|reg|re         { return tREGS; }
148 <INFO_CMD>segments|segment|segm|seg|se  { return tSEGMENTS; }
149 <INFO_CMD>stack|stac|sta|st             { return tSTACK; }
150 <INFO_CMD>maps|map                      { return tMAPS; }
151 <INFO_CMD,WALK_CMD>window|windo|wind|win|wnd    { return tWND; }
152 <HELP_CMD>info|inf|in                   { return tINFO; }
153 <MODE_CMD>vm86                          { return tVM86; }
155 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir {
156                                           BEGIN(PATH_EXPECTED); return tDIR; }
158 char                                    { return tCHAR; }
159 short                                   { return tSHORT; }
160 int                                     { return tINT; }
161 long                                    { return tLONG; }
162 float                                   { return tFLOAT; }
163 double                                  { return tDOUBLE; }
164 unsigned                                { return tUNSIGNED; }
165 signed                                  { return tSIGNED; }
166 struct                                  { return tSTRUCT; }
167 union                                   { return tUNION; }
168 enum                                    { return tENUM; }
170 {IDENTIFIER}                            { yylval.string = DEBUG_MakeSymbol(yytext); return tIDENTIFIER; }
171 "$"{IDENTIFIER}                         { yylval.string = DEBUG_MakeSymbol(yytext+1); return tINTVAR; }
173 <PATH_EXPECTED>{PATHNAME}               { yylval.string = DEBUG_MakeSymbol(yytext); return tPATH; }
175 <*>[ \t]+                               /* Eat up whitespace */
177 <NOPROCESS>.                            { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
178 <*>.                                    { if (syntax_error == 0) {
179                                              syntax_error++;
180                                              DEBUG_Printf(DBG_CHN_MESG, "Syntax Error (%s)\n", yytext); }
181                                         }
184 #ifndef yywrap
185 int yywrap(void) { return 1; }
186 #endif
188 #ifndef whitespace
189 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
190 #endif
193 /* Strip whitespace from the start and end of STRING. */
194 static void stripwhite(char *string)
196     int         i, last;
198     for (i = 0; whitespace(string[i]); i++);
199     if (i) strcpy(string, string + i);
201     last = i = strlen(string) - 1;
202     if (string[last] == '\n') i--;
204     while (i > 0 && whitespace(string[i])) i--;
205     if (string[last] == '\n')
206         string[++i] = '\n';
207     string[++i] = '\0';
210 static int      DEBUG_FetchEntireLine(const char* pfx, char** line, size_t* alloc, BOOL check_nl)
212     char        buf_line[256];
213     DWORD       nread;
214     size_t      len;
216     /* as of today, console handles can be file handles... so better use file APIs rather than
217      * consoles
218      */
219     WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), pfx, strlen(pfx), NULL, NULL);
221     len = 0;
222     do
223     {
224         if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf_line, sizeof(buf_line) - 1, &nread, NULL))
225             break;
226         buf_line[nread] = '\0';
228         if (check_nl && len == 0 && nread == 1 && buf_line[0] == '\n')
229             return 0;
231         /* store stuff at the end of last_line */
232         if (len + nread + 1 > *alloc)
233         {
234             *line = HeapReAlloc(GetProcessHeap(), 0, *line, *alloc += nread + 1);
235         }
236         strcpy(*line + len, buf_line);
237         len += nread;
238     } while (nread == 0 || buf_line[nread - 1] != '\n');
240     /* Remove leading and trailing whitespace from the line */
241     stripwhite(*line);
242     return 1;
245 static int DEBUG_FetchFromLine(const char* pfx, char* buf, int size)
247     size_t      len;
248 static char*  last_line = NULL;
249 static size_t last_line_size = 0;
250 static size_t last_line_idx = 0;
252     /* first alloc of our current buffer */
253     if (!last_line)
254     {
255         last_line = HeapAlloc(GetProcessHeap(), 0, last_line_size = 2);
256         assert(last_line);
257         last_line[0] = '\n';
258         last_line[1] = '\0';
259     }
261     /* try first to fetch the remaining of an existing line */
262     if (last_line_idx == 0)
263     {
264         /* no remaining chars to be read from last line, grab a brand new line up to '\n' */
265         DEBUG_FlushSymbols();
267         DEBUG_FetchEntireLine(pfx, &last_line, &last_line_size, TRUE);
268     }
270     len = min(strlen(last_line + last_line_idx), size - 1);
271     memcpy(buf, last_line + last_line_idx, len);
272     buf[len] = '\0';
273     if ((last_line_idx += len) >= strlen(last_line))
274         last_line_idx = 0;
275     return len;
278 int DEBUG_ReadLine(const char* pfx, char* buf, int size)
280     char*       line = NULL;
281     size_t      len = 0;
283     DEBUG_FetchEntireLine(pfx, &line, &len, FALSE);
284     len = min(size, len);
285     memcpy(buf, line, len - 1);
286     buf[len] = '\0';
287     return len - 1;
290 static char** local_symbols /* = NULL */;
291 static int next_symbol /* = 0 */;
292 static int alloc_symbol /* = 0 */;
294 char* DEBUG_MakeSymbol(const char* symbol)
296     assert(0 <= next_symbol && next_symbol < alloc_symbol + 1);
297     if (next_symbol >= alloc_symbol)
298     {
299         local_symbols = HeapReAlloc(GetProcessHeap(), 0, local_symbols,
300                                     (alloc_symbol += 32) * sizeof(local_symbols[0]));
301         assert(local_symbols);
302     }
303     return local_symbols[next_symbol++] = DBG_strdup(symbol);
306 void DEBUG_FlushSymbols(void)
308     while(--next_symbol >= 0) DBG_free(local_symbols[next_symbol]);
309     next_symbol = 0;