/tools/blergtool.c
/* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
* BSD-style license. Please see the COPYING file for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "database.h"
#include "subscription.h"
#include "tags.h"
void help() {
printf(
"Usage: blergtool <command> [args]\n"
"\n"
"Where command is one of:\n"
"\n"
" blergtool store <storename>\n"
" blergtool fetch <storename> [record]\n"
" blergtool mute <storename>\n"
" blergtool unmute <storename>\n"
" blergtool subscribe <storename> <target>\n"
" blergtool unsubscribe <storename> <target>\n"
);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
help();
exit(1);
}
if (!blerg_init()) {
exit(2);
}
if (strncmp(argv[1], "store", 5) == 0) {
char *store = argv[2];
struct blerg *f = blerg_open(store);
if (!f) {
printf("Blerg open failed\n");
exit(1);
}
size_t bytes_read = 0;
char *data = malloc(65536);
do {
bytes_read += fread(data + bytes_read, 1, 65536 - bytes_read, stdin);
} while (bytes_read < 65536 && !feof(stdin));
int record = blerg_store(f, data, bytes_read);
blerg_close(f);
free(data);
if (record < 0) {
fprintf(stderr, "Could not store\n");
exit(1);
} else {
fprintf(stderr, "Stored record %d\n", record);
}
} else if (strncmp(argv[1], "fetch", 5) == 0) {
char *store = argv[2];
int record = atoi(argv[3]);
struct blerg *f = blerg_open(store);
if (!f) {
printf("Blerg open failed\n");
exit(1);
}
char *data;
int size;
if (blerg_fetch(f, record, &data, &size)) {
fwrite(data, 1, size, stdout);
free(data);
}
blerg_close(f);
} else if (strncmp(argv[1], "list", 4) == 0) {
char *tag = argv[2];
int count = 50;
struct blergref *list = tag_list(tag, 0, &count, -1);
if (list == NULL) {
printf("No entries");
} else {
int i;
for (i = 0; i < count; i++) {
printf("%s %llu\n", list[i].author, list[i].record);
}
free(list);
}
} else if (strncmp(argv[1], "mute", 4) == 0) {
char *store = argv[2];
struct blerg *f = blerg_open(store);
if (!f) {
printf("Blerg open failed\n");
exit(1);
}
blerg_set_status(f, BLERGSTATUS_MUTED, 1);
blerg_close(f);
} else if (strncmp(argv[1], "unmute", 6) == 0) {
char *store = argv[2];
struct blerg *f = blerg_open(store);
if (!f) {
printf("Blerg open failed\n");
exit(1);
}
blerg_set_status(f, BLERGSTATUS_MUTED, 0);
blerg_close(f);
} else if (strncmp(argv[1], "subscribe", 9) == 0) {
if (argc < 4) {
printf("Not enough arguments for subscribe\n");
help();
exit(1);
}
if (!subscription_add(argv[2], argv[3])) {
printf("Could not subscribe %s to %s\n", argv[2], argv[3]);
exit(1);
}
} else if (strncmp(argv[1], "unsubscribe", 11) == 0) {
if (argc < 4) {
printf("Not enough arguments for unsubscribe\n");
help();
exit(1);
}
if (!subscription_remove(argv[2], argv[3])) {
printf("Could not subscribe %s to %s\n", argv[2], argv[3]);
exit(1);
}
} else {
help();
}
return 0;
}