forked from brasko/gdbwire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdbwire_mi_example.c
73 lines (66 loc) · 1.85 KB
/
gdbwire_mi_example.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
#include <assert.h>
#include <stdio.h>
#include "gdbwire_mi_parser.h"
/**
* The gdbwire_mi parser callback function.
*
* This function silently accepts gdbwire_mi output commands. If the parser
* fails to parse a gdbwire_mi output command this function will print the
* failed gdbwire_mi output line and then abort the program.
*
* @param context
* The context passed to the parser in gdbwire_mi_parser_create.
*
* @param output
* The gdbwire_mi output command discovered by the parser.
*/
void
parser_callback(void *context, struct gdbwire_mi_output *output)
{
assert(!context);
assert(output);
if (output->kind == GDBWIRE_MI_OUTPUT_PARSE_ERROR) {
printf("\n Parse Error: %s\n", output->line);
}
assert(output->kind != GDBWIRE_MI_OUTPUT_PARSE_ERROR);
assert(!output->next);
gdbwire_mi_output_free(output);
}
/**
* The main loop in the gdbwire_mi example.
*
* The main loop is responsible for reading data from stdin and sending it
* to the gdbwire_mi parser. This happens until stdin is closed and EOF is read.
*
* @param parser
* The gdbwire_mi parser.
*/
void
main_loop(struct gdbwire_mi_parser *parser)
{
int c;
enum gdbwire_result result;
while ((c = getchar()) != EOF) {
char ch = c;
printf("%c", ch);
result = gdbwire_mi_parser_push_data(parser, &ch, 1);
assert(result == GDBWIRE_OK);
}
}
/**
* The gdbwire_mi main function.
*
* This function is responsible for allocating the gdbwire_mi parser,
* calling the main loop which uses the parser and then deleting the
* gdbwire_mi parser.
*/
int
main(void) {
struct gdbwire_mi_parser_callbacks callbacks = { 0, parser_callback };
struct gdbwire_mi_parser *parser;
parser = gdbwire_mi_parser_create(callbacks);
assert(parser);
main_loop(parser);
gdbwire_mi_parser_destroy(parser);
return 0;
}