test/sam: add tests for empty matches at the end of a range
[vis-test.git] / fuzz / buffer-fuzzer.c
blobfea3ec8322728dbcc438fbfedc0de75cc5abbc14
1 #include <stddef.h>
2 #include <stdbool.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include "fuzzer.h"
8 #include "buffer.h"
9 #include "util.h"
11 #ifndef BUFSIZ
12 #define BUFSIZ 1024
13 #endif
15 typedef enum CmdStatus (*Cmd)(Buffer *buf, const char *cmd);
17 static enum CmdStatus cmd_insert(Buffer *buf, const char *cmd) {
18 char data[BUFSIZ];
19 size_t pos;
20 if (sscanf(cmd, "%zu %s\n", &pos, data) != 2)
21 return CMD_ERR;
22 return buffer_insert0(buf, pos, data);
25 static enum CmdStatus cmd_set(Buffer *buf, const char *cmd) {
26 char data[BUFSIZ];
27 if (sscanf(cmd, "%s\n", data) != 1)
28 return CMD_ERR;
29 return buffer_put0(buf, data);
32 static enum CmdStatus cmd_delete(Buffer *buf, const char *cmd) {
33 size_t pos, len;
34 if (sscanf(cmd, "%zu %zu", &pos, &len) != 2)
35 return CMD_ERR;
36 return buffer_remove(buf, pos, len);
39 static enum CmdStatus cmd_clear(Buffer *buf, const char *cmd) {
40 buffer_clear(buf);
41 return CMD_OK;
44 static enum CmdStatus cmd_size(Buffer *buf, const char *cmd) {
45 printf("%zu bytes\n", buffer_length(buf));
46 return CMD_OK;
49 static enum CmdStatus cmd_capacity(Buffer *buf, const char *cmd) {
50 printf("%zu bytes\n", buffer_capacity(buf));
51 return CMD_OK;
54 static enum CmdStatus cmd_print(Buffer *buf, const char *cmd) {
55 size_t len = buffer_length(buf);
56 const char *data = buffer_content(buf);
57 if (data && fwrite(data, len, 1, stdout) != 1)
58 return CMD_ERR;
59 if (data)
60 puts("");
61 return CMD_OK;
64 static enum CmdStatus cmd_quit(Buffer *buf, const char *cmd) {
65 return CMD_QUIT;
68 static Cmd commands[] = {
69 ['?'] = cmd_capacity,
70 ['='] = cmd_set,
71 ['#'] = cmd_size,
72 ['c'] = cmd_clear,
73 ['d'] = cmd_delete,
74 ['i'] = cmd_insert,
75 ['p'] = cmd_print,
76 ['q'] = cmd_quit,
79 int main(int argc, char *argv[]) {
80 char line[BUFSIZ];
81 Buffer buf;
82 buffer_init(&buf);
84 for (;;) {
85 printf("> ");
86 if (!fgets(line, sizeof(line), stdin))
87 break;
88 if (!isatty(0))
89 printf("%s", line);
90 if (line[0] == '\n')
91 continue;
92 size_t idx = line[0];
93 if (idx < LENGTH(commands) && commands[idx]) {
94 enum CmdStatus ret = commands[idx](&buf, line+1);
95 printf("%s", cmd_status_msg[ret]);
96 if (ret == CMD_QUIT)
97 break;
98 } else {
99 puts("Invalid command");
103 buffer_release(&buf);
105 return 0;