15 typedef enum CmdStatus (*Cmd
)(Buffer
*buf
, const char *cmd
);
17 static enum CmdStatus
cmd_insert(Buffer
*buf
, const char *cmd
) {
20 if (sscanf(cmd
, "%zu %s\n", &pos
, data
) != 2)
22 return buffer_insert0(buf
, pos
, data
);
25 static enum CmdStatus
cmd_set(Buffer
*buf
, const char *cmd
) {
27 if (sscanf(cmd
, "%s\n", data
) != 1)
29 return buffer_put0(buf
, data
);
32 static enum CmdStatus
cmd_delete(Buffer
*buf
, const char *cmd
) {
34 if (sscanf(cmd
, "%zu %zu", &pos
, &len
) != 2)
36 return buffer_remove(buf
, pos
, len
);
39 static enum CmdStatus
cmd_clear(Buffer
*buf
, const char *cmd
) {
44 static enum CmdStatus
cmd_size(Buffer
*buf
, const char *cmd
) {
45 printf("%zu bytes\n", buffer_length(buf
));
49 static enum CmdStatus
cmd_capacity(Buffer
*buf
, const char *cmd
) {
50 printf("%zu bytes\n", buffer_capacity(buf
));
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)
64 static enum CmdStatus
cmd_quit(Buffer
*buf
, const char *cmd
) {
68 static Cmd commands
[] = {
79 int main(int argc
, char *argv
[]) {
86 if (!fgets(line
, sizeof(line
), stdin
))
93 if (idx
< LENGTH(commands
) && commands
[idx
]) {
94 enum CmdStatus ret
= commands
[idx
](&buf
, line
+1);
95 printf("%s", cmd_status_msg
[ret
]);
99 puts("Invalid command");
103 buffer_release(&buf
);