Skip to content

Commit ed06ff5

Browse files
author
Joshua Haberman
committed
Store number of intermediate chars explicitly, instead of relying on null-terminated string.
Also, report errors by signaling an error action to the callback instead of doing a cheesy fprintf() to stderr.
1 parent 084d683 commit ed06ff5

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

vtparse.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,15 @@
66
* This code is in the public domain.
77
*/
88

9-
#include <string.h>
10-
#include <stdlib.h>
11-
#include <stdio.h>
12-
139
#include "vtparse.h"
1410

1511
void vtparse_init(vtparse_t *parser, vtparse_callback_t cb)
1612
{
17-
parser->state = VTPARSE_STATE_GROUND;
18-
parser->intermediate_chars[0] = '\0';
19-
parser->num_params = 0;
20-
parser->ignore_flagged = 0;
21-
parser->cb = cb;
13+
parser->state = VTPARSE_STATE_GROUND;
14+
parser->num_intermediate_chars = 0;
15+
parser->num_params = 0;
16+
parser->ignore_flagged = 0;
17+
parser->cb = cb;
2218
}
2319

2420
static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)
@@ -47,12 +43,10 @@ static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)
4743
case VTPARSE_ACTION_COLLECT:
4844
{
4945
/* Append the character to the intermediate params */
50-
int num_intermediate_chars = strlen((char*)parser->intermediate_chars);
51-
52-
if(num_intermediate_chars + 1 > MAX_INTERMEDIATE_CHARS)
46+
if(parser->num_intermediate_chars + 1 > MAX_INTERMEDIATE_CHARS)
5347
parser->ignore_flagged = 1;
5448
else
55-
parser->intermediate_chars[num_intermediate_chars++] = ch;
49+
parser->intermediate_chars[parser->num_intermediate_chars++] = ch;
5650

5751
break;
5852
}
@@ -85,13 +79,13 @@ static void do_action(vtparse_t *parser, vtparse_action_t action, char ch)
8579
}
8680

8781
case VTPARSE_ACTION_CLEAR:
88-
parser->intermediate_chars[0] = '\0';
82+
parser->num_intermediate_chars = 0;
8983
parser->num_params = 0;
9084
parser->ignore_flagged = 0;
9185
break;
9286

9387
default:
94-
fprintf(stderr, "Internal error, unknown action %d", action);
88+
parser->cb(parser, VTPARSE_ACTION_ERROR, 0);
9589
}
9690
}
9791

vtparse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct vtparse {
2020
vtparse_state_t state;
2121
vtparse_callback_t cb;
2222
unsigned char intermediate_chars[MAX_INTERMEDIATE_CHARS+1];
23+
int num_intermediate_chars;
2324
char ignore_flagged;
2425
int params[16];
2526
int num_params;

vtparse_tables.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def transition_to(state)
213213

214214
# establish an ordering to the states and actions
215215

216-
$actions_in_order = action_names.keys.sort { |a1, a2| a1.to_s <=> a2.to_s }
216+
$actions_in_order = action_names.keys.sort { |a1, a2| a1.to_s <=> a2.to_s } + [:error]
217217
$states_in_order = $states.keys.sort { |s1, s2| s1.to_s <=> s2.to_s }
218218

219219
#

vtparse_test.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,29 @@
77
*/
88

99
#include <stdio.h>
10+
#include <unistd.h>
1011
#include "vtparse.h"
1112

1213
void parser_callback(vtparse_t *parser, vtparse_action_t action, unsigned char ch)
1314
{
1415
int i;
1516

16-
printf("Received action %s, char=0x%02x\n", ACTION_NAMES[action]);
17-
printf("Intermediate chars: '%s'\n", parser->intermediate_chars);
18-
printf("%d Parameters:\n", parser->num_params);
19-
for(i = 0; i < parser->num_params; i++)
20-
printf("\t%d\n", parser->params[i]);
17+
printf("Received action %s\n", ACTION_NAMES[action]);
18+
if(ch != 0) printf("Char: 0x%02x ('%c')\n", ch, ch);
19+
if(parser->num_intermediate_chars > 0)
20+
{
21+
printf("%d Intermediate chars:\n", parser->num_intermediate_chars);
22+
for(i = 0; i < parser->num_intermediate_chars; i++)
23+
printf(" 0x%02x ('%c')\n", parser->intermediate_chars[i],
24+
parser->intermediate_chars[i]);
25+
}
26+
if(parser->num_params > 0)
27+
{
28+
printf("%d Parameters:\n", parser->num_params);
29+
for(i = 0; i < parser->num_params; i++)
30+
printf("\t%d\n", parser->params[i]);
31+
}
32+
2133
printf("\n");
2234
}
2335

@@ -29,9 +41,11 @@ int main()
2941

3042
vtparse_init(&parser, parser_callback);
3143

32-
while(1) {
33-
bytes = read(0, buf, 1024);
44+
do {
45+
bytes = read(STDIN_FILENO, buf, 1024);
3446
vtparse(&parser, buf, bytes);
35-
}
47+
} while(bytes > 0);
48+
49+
return 0;
3650
}
3751

0 commit comments

Comments
 (0)