Fix primary buffer memory leak.
[wine/multimedia.git] / programs / winedbg / debug.l
blobafb8bb111e1577832860201ac4b631ae83002455
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 "debugger.h"
28 #include "y.tab.h"
30 #undef YY_INPUT
32 static int read_input(const char* pfx, char* buf, int size)
34     size_t      len;
35 static char*  last_line = NULL;
36 static size_t last_line_size = 0;
37 static size_t last_line_idx = 0;
39     /* first alloc of our current buffer */
40     if (!last_line)
41     {
42         last_line = HeapAlloc(GetProcessHeap(), 0, last_line_size = 2);
43         assert(last_line);
44         last_line[0] = '\n';
45         last_line[1] = '\0';
46     }
48     /* try first to fetch the remaining of an existing line */
49     if (last_line_idx == 0)
50     {
51         /* no remaining chars to be read from last line, grab a brand new line up to '\n' */
52         lexeme_flush();
53         input_fetch_entire_line(pfx, &last_line, &last_line_size, TRUE);
54     }
56     len = min(strlen(last_line + last_line_idx), size - 1);
57     memcpy(buf, last_line + last_line_idx, len);
58     buf[len] = '\0';
59     if ((last_line_idx += len) >= strlen(last_line))
60         last_line_idx = 0;
61     return len;
64 #define YY_INPUT(buf,result,max_size) \
65         if ((result = read_input("Wine-dbg>", buf, max_size)) < 0) \
66             YY_FATAL_ERROR("read_input in flex scanner failed");
68 #define YY_NO_UNPUT
70 static int syntax_error;
73 DIGIT      [0-9]
74 HEXDIGIT   [0-9a-fA-F]
75 FORMAT     [ubcdgiswx]
76 IDENTIFIER [_a-zA-Z~][_a-zA-Z0-9~@]*
77 PATHNAME   [/_a-zA-Z\.~][/_a-zA-Z0-9\.~@]*
78 STRING     \"[^\n"]+\"
80 %s FORMAT_EXPECTED
81 %s PATH_EXPECTED
82 %s INFO_CMD
83 %s HELP_CMD
84 %s BD_CMD
85 %s LOCAL_CMD
86 %s SHOW_CMD
87 %s MODE_CMD
88 %s MAINT_CMD
89 %s NOCMD
91 %x ASTRING_EXPECTED
92 %x NOPROCESS
94                                         /* set to special state when no process is loaded. */
95                                         if (!dbg_curr_process && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
97 <<EOF>>                                 { return tEOF; }
98 <*>\n                                   { BEGIN(INITIAL); syntax_error = 0; return tEOL; }
99                                         /* Indicates end of command. Reset state. */
101 "||"                                    { return OP_LOR; }
102 "&&"                                    { return OP_LAND; }
103 "=="                                    { return OP_EQ; }
104 "!="                                    { return OP_NE; }
105 "<="                                    { return OP_LE; }
106 ">="                                    { return OP_GE; }
107 "<<"                                    { return OP_SHL; }
108 ">>"                                    { return OP_SHR; }
109 "->"                                    { return OP_DRF; }
110 [-+<=>|&^()*/%:!~,\.]                   { return *yytext; }
111 "["                                     { return *yytext; }
112 "]"                                     { return *yytext; }
114 "0x"{HEXDIGIT}+                         { sscanf(yytext, "%x", &yylval.integer); return tNUM; }
115 {DIGIT}+                                { sscanf(yytext, "%d", &yylval.integer); return tNUM; }
117 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT}    { char* last;
118                                           yylval.integer = strtol(yytext+1, &last, 0) << 8;
119                                           yylval.integer |= *last;
120                                           return tFORMAT; }
122 <FORMAT_EXPECTED>"/"{FORMAT}            { yylval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
124 {STRING}                                { yylval.string = lexeme_alloc(yytext); return tSTRING; }
125 <ASTRING_EXPECTED>[^\n]+                { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
126                                           yylval.string = lexeme_alloc(p); return tSTRING; }
128 <INITIAL,NOPROCESS>info|inf|in          { BEGIN(INFO_CMD); return tINFO; }
129 <INITIAL>up                             { BEGIN(NOCMD); return tUP; }
130 <INITIAL>down|dow|do                    { BEGIN(NOCMD); return tDOWN; }
131 <INITIAL>frame|fram|fra|fr              { BEGIN(NOCMD); return tFRAME; }
132 <INITIAL>list|lis|li|l                  { BEGIN(PATH_EXPECTED); return tLIST; }
133 <INITIAL>enable|enabl|enab|ena          { BEGIN(BD_CMD); return tENABLE;}
134 <INITIAL>disable|disabl|disab|disa|dis  { BEGIN(BD_CMD); return tDISABLE; }
135 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
136 <INITIAL>locally|local                  { BEGIN(LOCAL_CMD); return tLOCAL; }
137 <INITIAL,LOCAL_CMD>display|displa|displ|disp    { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
138 <INFO_CMD,BD_CMD>display|displa|displ|disp|dis|di|d     { BEGIN(NOCMD); return tDISPLAY; }
139 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und       { BEGIN(NOCMD); return tUNDISPLAY; }
140 <INITIAL>delete|delet|dele|del          { BEGIN(BD_CMD); return tDELETE; }
141 <INITIAL,NOPROCESS>quit|qui|qu|q        { BEGIN(NOCMD); return tQUIT; }
142 <INITIAL>set|se                         { BEGIN(NOCMD); return tSET; }
143 <INITIAL>x                              { BEGIN(FORMAT_EXPECTED); return tEXAM; }
144 <INITIAL,NOPROCESS>help|hel|he|"?"      { BEGIN(HELP_CMD); return tHELP; }
146 <INITIAL,NOPROCESS>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
147 <INITIAL,NOPROCESS>where|wher|whe       { BEGIN(NOCMD); return tBACKTRACE; }
149 <INITIAL>cont|con|co|c                  { BEGIN(NOCMD); return tCONT; }
150 <INITIAL>pass|pas|pa                    { BEGIN(NOCMD); return tPASS; }
151 <INITIAL>condition|conditio|conditi|condit|condi|cond   { BEGIN(NOCMD); return tCOND; }
152 <INITIAL>step|ste|st|s                  { BEGIN(NOCMD); return tSTEP; }
153 <INITIAL>next|nex|ne|n                  { BEGIN(NOCMD); return tNEXT; }
154 <INITIAL>stepi|si                       { BEGIN(NOCMD); return tSTEPI; }
155 <INITIAL>nexti|ni                       { BEGIN(NOCMD); return tNEXTI; }
156 <INITIAL>finish|finis|fini|fin|fi       { BEGIN(NOCMD); return tFINISH; }
158 <INITIAL>abort|abor|abo                 { BEGIN(NOCMD); return tABORT; }
159 <INITIAL>print|prin|pri|pr|p            { BEGIN(FORMAT_EXPECTED); return tPRINT; }
161 <INITIAL>mode                           { BEGIN(MODE_CMD); return tMODE; }
162 <INITIAL>show|sho|sh                    { BEGIN(SHOW_CMD); return tSHOW; }
163 <INITIAL,NOPROCESS>source|sourc|sour|src { BEGIN(PATH_EXPECTED); return tSOURCE; }
164 <INITIAL>symbolfile|symbols|symbol|sf   { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
166 <INITIAL,INFO_CMD,BD_CMD>break|brea|bre|br|b    { BEGIN(NOCMD); return tBREAK; }
167 <INITIAL>watch|watc|wat                 { BEGIN(NOCMD); return tWATCH; }
168 <INITIAL>whatis|whati|what              { BEGIN(NOCMD); return tWHATIS; }
169 <INITIAL,NOPROCESS>run|ru|r             { BEGIN(ASTRING_EXPECTED); return tRUN;}
170 <INITIAL>detach|detac|deta|det          { BEGIN(NOCMD); return tDETACH; }
171 <INITIAL>maintenance|maint              { BEGIN(MAINT_CMD); return tMAINTENANCE; }
172 <NOPROCESS>attach|attac|atta|att        { BEGIN(NOCMD); return tATTACH; }
173 <INFO_CMD>share|shar|sha                { return tSHARE; }
174 <INFO_CMD>locals|local|loca|loc         { return tLOCAL; }
175 <INFO_CMD>class|clas|cla                { return tCLASS; }
176 <INFO_CMD>process|proces|proce|proc     { return tPROCESS; }
177 <INFO_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
178 <INFO_CMD>exception|except|exc|ex       { return tEXCEPTION; }
179 <INFO_CMD>registers|regs|reg|re         { return tREGS; }
180 <INFO_CMD>segments|segment|segm|seg|se  { return tSEGMENTS; }
181 <INFO_CMD>stack|stac|sta|st             { return tSTACK; }
182 <INFO_CMD>symbol|symbo|symb|sym         { BEGIN(ASTRING_EXPECTED); return tSYMBOL; }
183 <INFO_CMD>maps|map                      { return tMAPS; }
184 <INFO_CMD>window|windo|wind|win|wnd     { return tWND; }
185 <HELP_CMD>info|inf|in                   { return tINFO; }
186 <MODE_CMD>vm86                          { return tVM86; }
187 <MAINT_CMD>type                         { return tTYPE; }
189 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir {
190                                           BEGIN(PATH_EXPECTED); return tDIR; }
192 char                                    { return tCHAR; }
193 short                                   { return tSHORT; }
194 int                                     { return tINT; }
195 long                                    { return tLONG; }
196 float                                   { return tFLOAT; }
197 double                                  { return tDOUBLE; }
198 unsigned                                { return tUNSIGNED; }
199 signed                                  { return tSIGNED; }
200 struct                                  { return tSTRUCT; }
201 union                                   { return tUNION; }
202 enum                                    { return tENUM; }
203 all                                     { return tALL; }
205 {IDENTIFIER}                            { yylval.string = lexeme_alloc(yytext); return tIDENTIFIER; }
206 "$"{IDENTIFIER}                         { yylval.string = lexeme_alloc(yytext+1); return tINTVAR; }
208 <PATH_EXPECTED>{PATHNAME}               { yylval.string = lexeme_alloc(yytext); return tPATH; }
210 <*>[ \t\r]+                             /* Eat up whitespace and DOS LF */
212 <NOPROCESS>.                            { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
213 <*>.                                    { if (syntax_error == 0) { syntax_error++; dbg_printf("Syntax Error (%s)\n", yytext); } }
216 #ifndef yywrap
217 int yywrap(void) { return 1; }
218 #endif
220 static char** local_lexemes /* = NULL */;
221 static int next_lexeme /* = 0 */;
222 static int alloc_lexeme /* = 0 */;
224 char* lexeme_alloc(const char* lexeme)
226     assert(0 <= next_lexeme && next_lexeme < alloc_lexeme + 1);
227     if (next_lexeme >= alloc_lexeme)
228     {
229         alloc_lexeme += 32;
230         local_lexemes = dbg_heap_realloc(local_lexemes, alloc_lexeme * sizeof(local_lexemes[0]));
231         assert(local_lexemes);
232     }
233     return local_lexemes[next_lexeme++] = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(lexeme) + 1), lexeme);
236 void lexeme_flush(void)
238     while (--next_lexeme >= 0) HeapFree(GetProcessHeap(), 0, local_lexemes[next_lexeme]);
239     next_lexeme = 0;