2 * Lexical scanner for command line parsing
4 * Copyright 1993 Eric Youngdale
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.
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.
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
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" );
46 static int syntax_error;
52 IDENTIFIER [_a-zA-Z~][_a-zA-Z0-9~@]*
53 PATHNAME [/_a-zA-Z\.~][/_a-zA-Z0-9\.~@]*
69 /* set to special state when no process is loaded. */
70 if (!DEBUG_CurrProcess && YYSTATE == INITIAL) {BEGIN(NOPROCESS);}
72 <<EOF>> { return tEOF; }
73 <*>\n { BEGIN(INITIAL); syntax_error = 0; return tEOL; }
74 /* Indicates end of command. Reset state. */
76 "||" { return OP_LOR; }
77 "&&" { return OP_LAND; }
78 "==" { return OP_EQ; }
79 "!=" { return OP_NE; }
80 "<=" { return OP_LE; }
81 ">=" { return OP_GE; }
82 "<<" { return OP_SHL; }
83 ">>" { return OP_SHR; }
84 "->" { return OP_DRF; }
85 [-+<=>|&^()*/%:!~,\.] { return *yytext; }
86 "[" { return *yytext; }
87 "]" { return *yytext; }
89 "0x"{HEXDIGIT}+ { sscanf(yytext, "%x", &yylval.integer); return tNUM; }
90 {DIGIT}+ { sscanf(yytext, "%d", &yylval.integer); return tNUM; }
92 <FORMAT_EXPECTED>"/"{DIGIT}+{FORMAT} { char* last;
93 yylval.integer = strtol( yytext+1, &last, 0 ) << 8;
94 yylval.integer |= *last;
97 <FORMAT_EXPECTED>"/"{FORMAT} { yylval.integer = (1 << 8) | yytext[1]; return tFORMAT; }
99 {STRING} { yylval.string = DEBUG_MakeSymbol(yytext); return tSTRING; }
100 <ASTRING_EXPECTED>[^\n]+ { char* p = yytext; while (*p == ' ' || *p == '\t') p++;
101 yylval.string = DEBUG_MakeSymbol(p); return tSTRING; }
103 <INITIAL>info|inf|in { BEGIN(INFO_CMD); return tINFO; }
104 <INITIAL>up { BEGIN(NOCMD); return tUP; }
105 <INITIAL>down|dow|do { BEGIN(NOCMD); return tDOWN; }
106 <INITIAL>frame|fram|fra|fr { BEGIN(NOCMD); return tFRAME; }
107 <INITIAL>list|lis|li|l { BEGIN(PATH_EXPECTED); return tLIST; }
108 <INITIAL>enable|enabl|enab|ena { BEGIN(NOCMD); return tENABLE;}
109 <INITIAL>disable|disabl|disab|disa|dis { BEGIN(NOCMD); return tDISABLE; }
110 <INITIAL>disassemble|disassembl|disassemb|disassem|disasse|disass|disas { BEGIN(NOCMD); return tDISASSEMBLE; }
111 <INITIAL,INFO_CMD,DEL_CMD>display|displa|displ|disp { BEGIN(FORMAT_EXPECTED); return tDISPLAY; }
112 <INITIAL>undisplay|undispla|undispl|undisp|undis|undi|und { BEGIN(NOCMD); return tUNDISPLAY; }
113 <INITIAL>delete|delet|dele|del { BEGIN(DEL_CMD); return tDELETE; }
114 <INITIAL,NOPROCESS>quit|qui|qu|q { BEGIN(NOCMD); return tQUIT; }
115 <INITIAL>set|se { BEGIN(NOCMD); return tSET; }
116 <INITIAL,NOPROCESS>walk|w { BEGIN(WALK_CMD); return tWALK; }
117 <INITIAL>x { BEGIN(FORMAT_EXPECTED); return tEXAM; }
118 <INITIAL,NOPROCESS>help|hel|he|"?" { BEGIN(HELP_CMD); return tHELP; }
120 <INITIAL>backtrace|backtrac|backtra|backt|back|bac|ba|bt { BEGIN(NOCMD); return tBACKTRACE; }
121 <INITIAL>where|wher|whe { BEGIN(NOCMD); return tBACKTRACE; }
123 <INITIAL>cont|con|co|c { BEGIN(NOCMD); return tCONT; }
124 <INITIAL>pass|pas|pa { BEGIN(NOCMD); return tPASS; }
125 <INITIAL>condition|conditio|conditi|condit|condi|cond { BEGIN(NOCMD); return tCOND; }
126 <INITIAL>step|ste|st|s { BEGIN(NOCMD); return tSTEP; }
127 <INITIAL>next|nex|ne|n { BEGIN(NOCMD); return tNEXT; }
128 <INITIAL>stepi|si { BEGIN(NOCMD); return tSTEPI; }
129 <INITIAL>nexti|ni { BEGIN(NOCMD); return tNEXTI; }
130 <INITIAL>finish|finis|fini|fin|fi { BEGIN(NOCMD); return tFINISH; }
132 <INITIAL>abort|abor|abo { BEGIN(NOCMD); return tABORT; }
133 <INITIAL>print|prin|pri|pr|p { BEGIN(FORMAT_EXPECTED); return tPRINT; }
135 <INITIAL>mode { BEGIN(MODE_CMD); return tMODE; }
136 <INITIAL>show|sho|sh { BEGIN(SHOW_CMD); return tSHOW; }
137 <INITIAL,NOPROCESS>source|sourc|sour|src { BEGIN(PATH_EXPECTED); return tSOURCE; }
138 <INITIAL>symbolfile|symbols|symbol|sf { BEGIN(PATH_EXPECTED); return tSYMBOLFILE; }
140 <INITIAL,INFO_CMD,DEL_CMD>break|brea|bre|br|b { BEGIN(NOCMD); return tBREAK; }
141 <INITIAL>watch|watc|wat { BEGIN(NOCMD); return tWATCH; }
142 <INITIAL>whatis|whati|what { BEGIN(NOCMD); return tWHATIS; }
143 <INITIAL,NOPROCESS>run|ru|r { BEGIN(ASTRING_EXPECTED); return tRUN;}
144 <INITIAL>detach|detac|deta|det { BEGIN(NOCMD); return tDETACH; }
145 <NOPROCESS>attach|attac|atta|att { BEGIN(NOCMD); return tATTACH; }
146 <INFO_CMD>share|shar|sha { return tSHARE; }
147 <INFO_CMD>locals|local|loca|loc { return tLOCAL; }
148 <INFO_CMD,WALK_CMD>class|clas|cla { return tCLASS; }
149 <INFO_CMD,WALK_CMD>module|modul|modu|mod { return tMODULE; }
150 <INFO_CMD,WALK_CMD>process|proces|proce|proc { return tPROCESS; }
151 <INFO_CMD,WALK_CMD>threads|thread|threa|thre|thr|th { return tTHREAD; }
152 <WALK_CMD>exception|except|exc|ex { return tEXCEPTION; }
153 <INFO_CMD>registers|regs|reg|re { return tREGS; }
154 <INFO_CMD>segments|segment|segm|seg|se { return tSEGMENTS; }
155 <INFO_CMD>stack|stac|sta|st { return tSTACK; }
156 <INFO_CMD>symbol|sym { BEGIN(ASTRING_EXPECTED); return tSYMBOL; }
157 <WALK_CMD>maps|map { return tMAPS; }
158 <INFO_CMD,WALK_CMD>window|windo|wind|win|wnd { return tWND; }
159 <HELP_CMD>info|inf|in { return tINFO; }
160 <MODE_CMD>vm86 { return tVM86; }
162 <INITIAL,SHOW_CMD>directories|directorie|directori|director|directo|direct|direc|direc|dir {
163 BEGIN(PATH_EXPECTED); return tDIR; }
165 char { return tCHAR; }
166 short { return tSHORT; }
168 long { return tLONG; }
169 float { return tFLOAT; }
170 double { return tDOUBLE; }
171 unsigned { return tUNSIGNED; }
172 signed { return tSIGNED; }
173 struct { return tSTRUCT; }
174 union { return tUNION; }
175 enum { return tENUM; }
177 {IDENTIFIER} { yylval.string = DEBUG_MakeSymbol(yytext); return tIDENTIFIER; }
178 "$"{IDENTIFIER} { yylval.string = DEBUG_MakeSymbol(yytext+1); return tINTVAR; }
180 <PATH_EXPECTED>{PATHNAME} { yylval.string = DEBUG_MakeSymbol(yytext); return tPATH; }
182 <*>[ \t]+ /* Eat up whitespace */
184 <NOPROCESS>. { BEGIN(ASTRING_EXPECTED); yyless(0); return tNOPROCESS;}
185 <*>. { if (syntax_error == 0) {
187 DEBUG_Printf(DBG_CHN_MESG, "Syntax Error (%s)\n", yytext); }
192 int yywrap(void) { return 1; }
196 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
200 /* Strip whitespace from the start and end of STRING. */
201 static void stripwhite(char *string)
205 for (i = 0; whitespace(string[i]); i++);
206 if (i) strcpy(string, string + i);
208 last = i = strlen(string) - 1;
209 if (string[last] == '\n') i--;
211 while (i > 0 && whitespace(string[i])) i--;
212 if (string[last] == '\n')
217 static int DEBUG_FetchEntireLine(const char* pfx, char** line, size_t* alloc, BOOL check_nl)
223 /* as of today, console handles can be file handles... so better use file APIs rather than
226 WriteFile(DEBUG_hParserOutput, pfx, strlen(pfx), NULL, NULL);
231 if (!ReadFile(DEBUG_hParserInput, buf_line, sizeof(buf_line) - 1, &nread, NULL) || nread == 0)
233 buf_line[nread] = '\0';
235 if (check_nl && len == 0 && nread == 1 && buf_line[0] == '\n')
238 /* store stuff at the end of last_line */
239 if (len + nread + 1 > *alloc)
241 *line = HeapReAlloc(GetProcessHeap(), 0, *line, *alloc += nread + 1);
243 strcpy(*line + len, buf_line);
245 } while (nread == 0 || buf_line[nread - 1] != '\n');
249 *line = HeapReAlloc(GetProcessHeap(), 0, *line, *alloc = 1);
253 /* Remove leading and trailing whitespace from the line */
258 static int DEBUG_FetchFromLine(const char* pfx, char* buf, int size)
261 static char* last_line = NULL;
262 static size_t last_line_size = 0;
263 static size_t last_line_idx = 0;
265 /* first alloc of our current buffer */
268 last_line = HeapAlloc(GetProcessHeap(), 0, last_line_size = 2);
274 /* try first to fetch the remaining of an existing line */
275 if (last_line_idx == 0)
277 /* no remaining chars to be read from last line, grab a brand new line up to '\n' */
278 DEBUG_FlushSymbols();
280 DEBUG_FetchEntireLine(pfx, &last_line, &last_line_size, TRUE);
283 len = min(strlen(last_line + last_line_idx), size - 1);
284 memcpy(buf, last_line + last_line_idx, len);
286 if ((last_line_idx += len) >= strlen(last_line))
291 int DEBUG_ReadLine(const char* pfx, char* buf, int size)
296 DEBUG_FetchEntireLine(pfx, &line, &len, FALSE);
298 /* remove trailing \n */
299 if (len > 0 && line[len - 1] == '\n') len--;
300 len = min(size - 1, len);
301 memcpy(buf, line, len);
303 HeapFree(GetProcessHeap(), 0, line);
307 static char** local_symbols /* = NULL */;
308 static int next_symbol /* = 0 */;
309 static int alloc_symbol /* = 0 */;
311 char* DEBUG_MakeSymbol(const char* symbol)
313 assert(0 <= next_symbol && next_symbol < alloc_symbol + 1);
314 if (next_symbol >= alloc_symbol)
316 local_symbols = HeapReAlloc(GetProcessHeap(), 0, local_symbols,
317 (alloc_symbol += 32) * sizeof(local_symbols[0]));
318 assert(local_symbols);
320 return local_symbols[next_symbol++] = DBG_strdup(symbol);
323 void DEBUG_FlushSymbols(void)
325 while(--next_symbol >= 0) DBG_free(local_symbols[next_symbol]);