Add handler to close post widget on ESC
[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                 if (blerg_fetch(f, record, &data, &size)) {
54                         fwrite(data, 1, size, stdout);
55                         free(data);
56                 }
57                 blerg_close(f);
58         } else if (strncmp(argv[1], "list", 4) == 0) {
59                 char *tag = argv[2];
60                 int count = 50;
61                 struct blergref *list = tag_list(tag, 0, &count, -1);
62                 if (list == NULL) {
63                         printf("No entries");
64                 } else {
65                         int i;
66                         for (i = 0; i < count; i++) {
67                                 printf("%s %d\n", list[i].author, list[i].record);
68                         }
69                         free(list);
70                 }
71         } else if (strncmp(argv[1], "mute", 4) == 0) {
72                 char *store = argv[2];
73                 struct blerg *f = blerg_open(store);
74                 if (!f) {
75                         printf("Blerg open failed\n");
76                         exit(1);
77                 }
78
79                 blerg_set_mute(f, 1);
80                 blerg_close(f);
81         } else if (strncmp(argv[1], "unmute", 6) == 0) {
82                 char *store = argv[2];
83                 struct blerg *f = blerg_open(store);
84                 if (!f) {
85                         printf("Blerg open failed\n");
86                         exit(1);
87                 }
88
89                 blerg_set_mute(f, 0);
90                 blerg_close(f);
91         } else {
92                 help();
93         }
94
95         return 0;
96 }