-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIParser.c
585 lines (520 loc) · 13.5 KB
/
MIParser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/******************************************************************************
* Copyright (c) 2005 The Regents of the University of California.
* This material was produced under U.S. Government contract W-7405-ENG-36
* for Los Alamos National Laboratory, which is operated by the University
* of California for the U.S. Department of Energy. The U.S. Government has
* rights to use, reproduce, and distribute this software. NEITHER THE
* GOVERNMENT NOR THE UNIVERSITY MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
* ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified
* to produce derivative works, such modified software should be clearly
* marked, so as not to confuse it with the version available from LANL.
*
* Additionally, this program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* LA-CC 04-115
*
* Copyright 2012-2014 Cray Inc. All Rights Reserved.
*
******************************************************************************/
/*
* Based on the QNX Java implementation of the MI interface
*/
/**
<pre>
`OUTPUT :'
`( OUT-OF-BAND-RECORD )* [ RESULT-RECORD ] "(gdb)" NL'
`RESULT-RECORD :'
` [ TOKEN ] "^" RESULT-CLASS ( "," RESULT )* NL'
`OUT-OF-BAND-RECORD :'
`ASYNC-RECORD | STREAM-RECORD'
`ASYNC-RECORD :'
`EXEC-ASYNC-OUTPUT | STATUS-ASYNC-OUTPUT | NOTIFY-ASYNC-OUTPUT'
`EXEC-ASYNC-OUTPUT :'
`[ TOKEN ] "*" ASYNC-OUTPUT'
`STATUS-ASYNC-OUTPUT :'
`[ TOKEN ] "+" ASYNC-OUTPUT'
`NOTIFY-ASYNC-OUTPUT :'
`[ TOKEN ] "=" ASYNC-OUTPUT'
`ASYNC-OUTPUT :'
`ASYNC-CLASS ( "," RESULT )* NL'
`RESULT-CLASS :'
`"done" | "running" | "connected" | "error" | "exit"'
`ASYNC-CLASS :'
`"stopped" | OTHERS' (where OTHERS will be added depending on the
needs--this is still in development).
`RESULT :'
` VARIABLE "=" VALUE'
`VARIABLE :'
` STRING '
`VALUE :'
` CONST | TUPLE | LIST '
`CONST :'
`C-STRING'
`TUPLE :'
` "{}" | "{" RESULT ( "," RESULT )* "}" '
`LIST :'
` "[]" | "[" VALUE ( "," VALUE )* "]" | "[" RESULT ( "," RESULT )*
"]" '
`STREAM-RECORD :'
`CONSOLE-STREAM-OUTPUT | TARGET-STREAM-OUTPUT | LOG-STREAM-OUTPUT'
`CONSOLE-STREAM-OUTPUT :'
`"~" C-STRING'
`TARGET-STREAM-OUTPUT :'
`"@" C-STRING'
`LOG-STREAM-OUTPUT :'
`"&" C-STRING'
`NL :'
`CR | CR-LF'
`TOKEN :'
_any sequence of digits_.
`C-STRING :'
`""" SEVEN-BIT-ISO-C-STRING-CONTENT """'
</pre>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "MIList.h"
#include "MIOOBRecord.h"
#include "MIValue.h"
#include "MIResult.h"
#include "MIResultRecord.h"
#include "MIOutput.h"
static MIResultRecord *processMIResultRecord(char *buffer, int id);
static MIOOBRecord *processMIOOBRecord(char *buffer, int id);
static MIList *processMIResults(char **buffer);
static MIResult *processMIResult(char **buffer);
static MIValue *processMIValue(char **buffer);
static MIValue *processMITuple(char **buffer);
static MIValue *processMIList(char **buffer);
static char *translateCString(char **buffer);
char *primaryPrompt = "(gdb)"; //$NON-NLS-1$
char *secondaryPrompt = ">"; //$NON-NLS-1$
/**
* Point of entry to create an AST for MI.
*
* @param buffer Output from MI Channel.
* @param mi MIOutput
* @see MIOutput
*/
void
MIParse(char *buffer, MIOutput *mi)
{
int id = -1;
char *pos = buffer;
char *token = buffer;
while (pos != NULL && *pos != '\0')
{
pos = strchr(token, '\n');
if (pos != NULL)
{
*pos++ = '\0';
}
// Fetch the Token/Id
if (*token != '\0' && isdigit(*token)) {
id = strtol(token, &token, 10);
}
// ResultRecord || Out-Of-Band Records
if (*token != '\0') {
if (*token == '^') {
++token;
mi->rr = processMIResultRecord(token, id);
} else if (strncmp(token, primaryPrompt, strlen(primaryPrompt)) == 0) {
// Do nothing.
} else {
MIOOBRecord *oob = processMIOOBRecord(token, id);
if (oob != NULL) {
if (mi->oobs == NULL) {
mi->oobs = MIListNew();
}
MIListAdd(mi->oobs, (void *)oob);
}
}
}
// set token to the current position pointer
token = pos;
}
}
/**
* Assuming '^' was deleted from the Result Record.
*/
static MIResultRecord *
processMIResultRecord(char *buffer, int id)
{
MIResultRecord *rr = MIResultRecordNew();
rr->token = id;
if (strncmp(buffer, "done", 4) == 0) {
rr->resultClass = MIResultRecordDONE;
buffer += 4;
} else if (strncmp(buffer, "error", 5) == 0) {
rr->resultClass = MIResultRecordERROR;
buffer += 5;
} else if (strncmp(buffer, "exit", 4) == 0) {
rr->resultClass = MIResultRecordEXIT;
buffer += 4;
} else if (strncmp(buffer, "running", 7) == 0) {
rr->resultClass = MIResultRecordRUNNING;
buffer += 7;
} else if (strncmp(buffer, "connected", 9) == 0) {
rr->resultClass = MIResultRecordCONNECTED;
buffer += 9;
} else {
// FIXME:
// Error throw an exception?
}
// Results are separated by commas.
if (*buffer != '\0' && *buffer == ',') {
buffer++;
MIList *res = processMIResults(&buffer);
rr->results = res;
}
return rr;
}
/**
* Find OutOfBand Records depending on the starting token.
*/
static MIOOBRecord *
processMIOOBRecord(char *buffer, int id)
{
MIOOBRecord *oob = NULL;
char c = *buffer;
// async records
if (c == '*' || c == '+' || c == '=') {
// Consume the first char
buffer++;
switch (c) {
case '*' :
oob = NewMIExecAsyncOutput();
break;
case '+' :
oob = NewMIStatusAsyncOutput();
break;
case '=' :
oob = NewMINotifyAsyncOutput();
break;
}
oob->token = id;
// Extract the Async-Class
char *s = strchr(buffer, ',');
if (s != NULL) {
*s++ = '\0';
oob->class = strdup(buffer);
// Consume the async-class and the comma
buffer = s;
} else {
oob->class = strdup(buffer);
buffer += strlen(buffer);
}
MIList *res = processMIResults(&buffer);
oob->results = res;
// stream records
} else if (c == '~' || c == '@' || c == '&') {
// Consume the first char
buffer++;
switch (c) {
case '~' :
oob = NewMIConsoleStreamOutput();
break;
case '@' :
oob = NewMITargetStreamOutput();
break;
case '&' :
oob = NewMILogStreamOutput();
break;
}
if (*buffer != '\0' && *buffer == '"') {
buffer++;
}
oob->cstring = translateCString(&buffer);
} else {
// Badly format MI line, just pass it to the user as target stream
oob = NewMITargetStreamOutput();
oob->cstring = strdup(buffer); //$NON-NLS-1$
}
return oob;
}
/**
* Assuming that the usual leading comma was consumed.
* Extract the MI Result comma seperated responses.
*/
static MIList *
processMIResults(char **buffer)
{
MIList * aList = MIListNew();
MIResult * result = processMIResult(buffer);
if (result != NULL) {
MIListAdd(aList, (void *)result);
}
while (*(*buffer) != '\0' && *(*buffer) == ',')
{
(*buffer)++;
result = processMIResult(buffer);
if (result != NULL) {
MIListAdd(aList, (void *)result);
}
}
return aList;
}
/**
* Construct the MIResult. Characters will be consume/delete
* moving forward constructing the AST.
*/
static MIResult *
processMIResult(char **buffer)
{
int len;
MIResult * result = MIResultNew();
MIValue * value;
char * variable;
char * equal = strchr(*buffer, '=');
if (*(*buffer) != '\0' && isalpha(*(*buffer)) && equal != NULL)
{
variable = *buffer;
*equal++ = '\0';
// copy the variable to the result->variable
len = strlen(variable)*sizeof(char);
result->variable = malloc(len+1);
if (result->variable == (void *)0)
{
fprintf(stderr, "Malloc failed.\n");
return (MIResult *)NULL;
}
memset(result->variable, 0, len+1);
memcpy(result->variable, variable, len);
*buffer = equal;
value = processMIValue(buffer);
result->value = value;
} else if(*(*buffer) != '\0' && *(*buffer) == '"')
{
// This an error but we just swallow it and move on.
value = processMIValue(buffer);
result->value = value;
} else
{
result->variable = strdup(*buffer);
result->value = NewMIConst(); // Empty string:???
*(*buffer) = '\0';
}
return result;
}
/**
* Find a MIValue implementation or return null.
*/
static MIValue *
processMIValue(char **buffer)
{
MIValue *value = NULL;
if (*(*buffer) != '\0') {
if (*(*buffer) == '{') {
(*buffer)++;
value = processMITuple(buffer);
} else if (*(*buffer) == '[') {
(*buffer)++;
value = processMIList(buffer);
} else if (*(*buffer) == '"') {
(*buffer)++;
value = NewMIConst();
value->cstring = translateCString(buffer);
}
}
return value;
}
/**
* Assuming the starting '{' was deleted form the StringBuffer,
* go to the closing '}' consuming/deleting all the characters.
* This is usually call by processMIvalue();
*/
static MIValue *
processMITuple(char **buffer)
{
MIValue *tuple = NewMITuple();
#ifdef __APPLE__
MIList *values = MIListNew();
MIValue *value;
MIResult *result;
#endif /* __APPLE__ */
MIList *results = NULL;
// Catch closing '}'
while (*(*buffer) != '\0' && *(*buffer) != '}')
{
#ifdef __APPLE__
// Try for the MIValue first
value = processMIValue(buffer);
if (value != NULL)
{
MIListAdd(values, (void *)value);
} else
{
result = processMIResult(buffer);
if (result != NULL)
{
if (results == NULL)
{
results = MIListNew();
}
MIListAdd(results, (void *)result);
}
}
if (*(*buffer) != '\0' && *(*buffer) == ',')
{
(*buffer)++;
}
#else /* __APPLE__ */
results = processMIResults(buffer);
#endif /* __APPLE__ */
}
if (*(*buffer) != '\0' && *(*buffer) == '}')
{
(*buffer)++;
}
tuple->results = results;
#ifdef __APPLE__
tuple->values = values;
#endif /* __APPLE__ */
return tuple;
}
/**
* Assuming the leading '[' was deleted, find the closing
* ']' consuming/delete chars from the StringBuffer.
*/
static MIValue *
processMIList(char **buffer)
{
MIValue *list = NewMIList();
MIList *valueList = MIListNew();
MIList *resultList = MIListNew();
MIValue *value;
MIResult *result;
// catch closing ']'
while (*(*buffer) != '\0' && *(*buffer) != ']')
{
// Try for the MIValue first
value = processMIValue(buffer);
if (value != NULL)
{
MIListAdd(valueList, (void *)value);
} else
{
result = processMIResult(buffer);
if (result != NULL)
{
MIListAdd(resultList, (void *)result);
}
}
if (*(*buffer) != '\0' && *(*buffer) == ',')
{
(*buffer)++;
}
}
if (*(*buffer) != '\0' && *(*buffer) == ']')
{
(*buffer)++;
}
list->values = valueList;
list->results = resultList;
return list;
}
/*
* MI C-String rather MICOnst values are enclose in double quotes
* and any double quotes or backslash in the string are escaped.
* Assuming the starting double quote was removed.
* This method will stop at the closing double quote remove the extra
* backslach escaping and return the string __without__ the enclosing double quotes
* The orignal StringBuffer will move forward.
*/
static char *
translateCString(char **buffer)
{
int escape = 0;
int closingQuotes = 0;
size_t len = 0;
char *s = *buffer;
char *sb;
char *p, *q;
while (*s != '\0' && !closingQuotes)
{
switch (*s)
{
case '\\':
if (escape)
{
escape = 0;
} else
{
escape = 1;
}
++len;
++s;
break;
case '"':
if (escape)
{
escape = 0;
++s;
++len;
} else
{
++s;
++closingQuotes;
}
break;
default:
++len;
++s;
escape = 0;
}
}
if ((sb = malloc((len+1)*sizeof(char))) == (void *)0)
{
fprintf(stderr, "Malloc failed.\n");
return NULL;
}
memset(sb, 0, len+1);
memcpy(sb, *buffer, len);
// point buffer past the closing quote
*buffer = s;
p = sb;
q = sb;
sb[len] = '\0'; // get rid of the final quote
escape = 0;
// strip out any escape backslashes
for (; *p != '\0'; p++)
{
switch (*p)
{
case '\\':
if (escape)
{
*q++ = *p;
escape = 0;
} else
{
escape = 1;
}
break;
case '"':
if (escape)
{
*q++ = *p;
escape = 0;
}
break;
default:
if (escape)
{
*q++ = '\\';
}
*q++ = *p;
escape = 0;
}
}
*q = '\0';
return sb;
}