Browse Source

Enable parsing of several commands in single buffer.

It is not guaranteed that each command will come in it's own buffer. In
fact when several changes are made at once the commands are not separated.
Georgi Chorbadzhiyski 9 years ago
parent
commit
84c20a4ce4
3 changed files with 28 additions and 2 deletions
  1. 26
    0
      cmd.c
  2. 1
    0
      cmd.h
  3. 1
    2
      videohubctrl.c

+ 26
- 0
cmd.c View File

140
 
140
 
141
 bool parse_command(struct videohub_data *data, char *cmd) {
141
 bool parse_command(struct videohub_data *data, char *cmd) {
142
 	unsigned int i;
142
 	unsigned int i;
143
+	if (!strlen(cmd))
144
+		return false;
143
 	struct videohub_commands *v = NULL;
145
 	struct videohub_commands *v = NULL;
144
 	for (i = 0; i < ARRAY_SIZE(videohub_commands); i++) {
146
 	for (i = 0; i < ARRAY_SIZE(videohub_commands); i++) {
145
 		if (!videohub_commands[i].txt)
147
 		if (!videohub_commands[i].txt)
301
 
303
 
302
 	return true;
304
 	return true;
303
 }
305
 }
306
+
307
+int parse_text_buffer(struct videohub_data *data, char *cmd_buffer) {
308
+	// The buffer contains only one command, no splitting is needed
309
+	if (!strstr(cmd_buffer, "\n\n"))
310
+		return parse_command(data, cmd_buffer);
311
+	// Split commands and parse them one by one
312
+	int ok_commands = 0;
313
+	char *bcopy = xstrdup(cmd_buffer);
314
+	char *newcmd, *cmd = bcopy;
315
+	while(1) {
316
+		newcmd = strstr(cmd, "\n\n"); // Find next command
317
+		if (!newcmd) {
318
+			if (parse_command(data, cmd)) // Parse current command
319
+				ok_commands++;
320
+			break;
321
+		}
322
+		newcmd[0] = '\0'; // Terminate previous command
323
+		if (parse_command(data, cmd)) // Parse previous command
324
+			ok_commands++;
325
+		cmd = newcmd + 2; // Advance cmd to the next command
326
+	}
327
+	free(bcopy);
328
+	return ok_commands;
329
+}

+ 1
- 0
cmd.h View File

14
 #define CMD_H
14
 #define CMD_H
15
 
15
 
16
 bool parse_command(struct videohub_data *d, char *cmd);
16
 bool parse_command(struct videohub_data *d, char *cmd);
17
+bool parse_text_buffer(struct videohub_data *data, char *cmd_buffer);
17
 
18
 
18
 #endif
19
 #endif

+ 1
- 2
videohubctrl.c View File

156
 	char buf[8192 + 1];
156
 	char buf[8192 + 1];
157
 	memset(buf, 0, sizeof(buf));
157
 	memset(buf, 0, sizeof(buf));
158
 	while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
158
 	while ((ret = fdread_ex(d->dev_fd, buf, sizeof(buf) - 1, 5, 0, 0)) >= 0) {
159
-		if (parse_command(d, buf))
160
-			ncommands++;
159
+		ncommands += parse_text_buffer(d, buf);
161
 		memset(buf, 0, sizeof(buf));
160
 		memset(buf, 0, sizeof(buf));
162
 	}
161
 	}
163
 	return ncommands;
162
 	return ncommands;

Loading…
Cancel
Save