forked from brasko/gdbwire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdbwire_example.c
102 lines (95 loc) · 2.1 KB
/
gdbwire_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
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
#include <assert.h>
#include <stdio.h>
#include "gdbwire.h"
/**
* The gdbwire stream record event.
*
* @param context
* The context passed to gdbwire in gdbwire_mi_parser_create.
*
* @param stream_record
* The stream record to display to the user.
*/
void
gdbwire_stream_record(void *context,
struct gdbwire_mi_stream_record *stream_record)
{
assert(!context && stream_record);
printf("%s", stream_record->cstring);
fflush(stdout);
}
/**
* The gdbwire prompt event.
*
* @param context
* The context passed to gdbwire in gdbwire_mi_parser_create.
*
* @param prompt
* The prompt to display to the user.
*/
void
gdbwire_prompt(void *context, const char *prompt)
{
assert(!context && prompt);
printf("%s", prompt);
fflush(stdout);
}
/**
* The gdbwire parse error event.
*
* @param context
* The context passed to gdbwire in gdbwire_mi_parser_create.
*
*/
void
gdbwire_parse_error(void *context, const char *mi, const char *token,
struct gdbwire_mi_position position)
{
assert(!context && mi && token);
printf("Parse error:\n");
printf(" at token:[%s]\n", mi);
printf(" token start column:%d\n", position.start_column);
printf(" token end column:%d\n", position.end_column);
printf(" line:[%s]\n", mi);
fflush(stdout);
}
/**
* The main loop in the gdbwire_mi example.
*
* The main loop is responsible for reading data from stdin and sending it
* to gdbwire. This happens until stdin is closed and EOF is read.
*
* @param parser
* The gdbwire_mi parser.
*/
void
main_loop(struct gdbwire *wire)
{
int c;
enum gdbwire_result result;
while ((c = getchar()) != EOF) {
char ch = c;
result = gdbwire_push_data(wire, &ch, 1);
assert(result == GDBWIRE_OK);
}
}
/**
* The gdbwire example.
*/
int
main(void) {
struct gdbwire_callbacks callbacks = {
0,
gdbwire_stream_record,
0,
0,
gdbwire_prompt,
gdbwire_parse_error
};
struct gdbwire *wire;
wire = gdbwire_create(callbacks);
assert(wire);
main_loop(wire);
gdbwire_destroy(wire);
return 0;
}