add listener for submit events
[blerg.git] / tools / blergtool.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "database.h"
7 #include "tags.h"
8
9 void help() {
10         printf("Usage: blergtool <store|fetch> <storename> [record]\n");
11 }
12
13 int main(int argc, char *argv[]) {
14         if (argc < 2) {
15                 help();
16                 exit(1);
17         }
18
19         if (strncmp(argv[1], "store", 5) == 0) {
20                 char *store = argv[2];
21                 struct blerg *f = blerg_open(store);
22                 if (!f) {
23                         printf("Blerg open failed\n");
24                         exit(1);
25                 }
26
27                 size_t bytes_read = 0;
28                 char *data = malloc(65536);
29                 do {
30                         bytes_read += fread(data + bytes_read, 1, 65536 - bytes_read, stdin);
31                 } while (bytes_read < 65536 && !feof(stdin));
32                 int record = blerg_store(f, data, bytes_read);
33                 blerg_close(f);
34                 free(data);
35
36                 if (record < 0) {
37                         fprintf(stderr, "Could not store\n");
38                         exit(1);
39                 } else {
40                         fprintf(stderr, "Stored record %d\n", record);
41                 }
42         } else if (strncmp(argv[1], "fetch", 5) == 0) {
43                 char *store = argv[2];
44                 int record = atoi(argv[3]);
45                 struct blerg *f = blerg_open(store);
46                 if (!f) {
47                         printf("Blerg open failed\n");
48                         exit(1);
49                 }
50
51                 char *data;
52                 int size;
53                 blerg_fetch(f, record, &data, &size);
54                 blerg_close(f);
55                 fwrite(data, 1, size, stdout);
56                 free(data);
57         } else if (strncmp(argv[1], "list", 4) == 0) {
58                 char *tag = argv[2];
59                 int count = 50;
60                 struct blergref *list = tag_list(tag, 0, &count, -1);
61                 if (list == NULL) {
62                         printf("No entries");
63                 } else {
64                         int i;
65                         for (i = 0; i < count; i++) {
66                                 printf("%s %d\n", list[i].author, list[i].record);
67                         }
68                         free(list);
69                 }
70         } else {
71                 help();
72         }
73
74         return 0;
75 }