irc-bot/extras/factoids.c

319 lines
6.6 KiB
C
Raw Normal View History

2012-11-14 15:09:06 -07:00
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
2012-11-20 18:13:16 -07:00
#include <unistd.h>
2012-11-21 13:10:03 -07:00
#include <fnmatch.h>
#include <ctype.h>
2012-11-19 14:53:00 -07:00
#include <sys/time.h>
#include <sysexits.h>
#include "cdb.h"
2012-11-20 18:13:16 -07:00
#include "cdbmake.h"
2012-11-14 15:09:06 -07:00
int
2012-11-21 14:25:40 -07:00
usage(char *self)
{
2012-11-21 14:25:40 -07:00
fprintf(stderr, "Usage: %s [OPTIONS] CDB \"KEY\"\n", self);
fprintf(stderr, "\n");
fprintf(stderr, "Default: Display one randomly-picked entry for KEY\n");
fprintf(stderr, "-n Create database from scratch, ignoring KEY\n");
fprintf(stderr, "-l Display all entries for KEY\n");
fprintf(stderr, "-a VAL Append VAL to entries for KEY\n");
fprintf(stderr, "-r GLOB Remove entries matching GLOB from KEY\n");
fprintf(stderr, "\n");
fprintf(stderr, "KEY is always converted to lowercase (Latin-1 only)\n");
2012-11-20 18:13:16 -07:00
return EX_USAGE;
}
size_t
lowercase(char *text)
{
size_t ret;
for (ret = 0; text[ret]; ret += 1) {
text[ret] = tolower(text[ret]);
}
return ret;
}
int
2012-11-20 18:13:16 -07:00
choose(char *filename, char *key)
{
2012-11-19 14:53:00 -07:00
struct cdb_ctx c;
FILE *f = fopen(filename, "r");
2012-11-20 18:13:16 -07:00
size_t keylen = lowercase(key);
uint32_t nresults;
if (! f) {
perror("Opening database");
return EX_NOINPUT;
}
cdb_init(&c, f);
/* Count how many results there are */
2012-11-20 18:13:16 -07:00
cdb_find(&c, key, keylen);
2012-11-19 14:53:00 -07:00
for (nresults = 0; cdb_next(&c, NULL, 0); nresults += 1);
if (nresults > 0) {
/* This is horrible: say rand() returned between 0 and 2, and results
* was 2. Possible values would be (0, 1, 0): not a uniform
* distribution. But this is random enough for our purposes. */
2012-11-19 14:53:00 -07:00
uint32_t which = rand() % nresults;
uint32_t vallen;
char val[8192];
2012-11-19 14:53:00 -07:00
uint32_t i;
2012-11-20 18:13:16 -07:00
cdb_find(&c, key, keylen);
2012-11-19 14:53:00 -07:00
for (i = 0; i < which; i += 1) {
cdb_next(&c, NULL, 0);
}
2012-11-20 18:13:16 -07:00
vallen = cdb_next(&c, val, sizeof(val));
printf("%.*s\n", vallen, val);
}
fclose(f);
return 0;
}
int
list(char *filename, char *key)
{
struct cdb_ctx c;
size_t keylen = lowercase(key);
FILE *f = fopen(filename, "rb");
if (! f) {
perror("Opening database");
return EX_NOINPUT;
}
cdb_init(&c, f);
cdb_find(&c, key, keylen);
for (;;) {
uint32_t vallen;
char val[8192];
vallen = cdb_next(&c, val, sizeof(val));
if (vallen == 0) {
break;
}
2012-11-19 14:53:00 -07:00
printf("%.*s\n", vallen, val);
}
2012-11-19 14:53:00 -07:00
fclose(f);
return 0;
}
2012-11-14 15:09:06 -07:00
2012-11-21 13:10:03 -07:00
static int
setup_copy(char *infn, struct cdb_ctx *inc, FILE **inf,
char **outfn, struct cdbmake_ctx *outc, FILE **outf)
2012-11-20 18:13:16 -07:00
{
2012-11-21 13:10:03 -07:00
static char tmpfn[8192];
2012-11-20 18:13:16 -07:00
2012-11-21 13:10:03 -07:00
*inf = fopen(infn, "rb");
if (! *inf) {
2012-11-20 18:13:16 -07:00
perror("Opening database");
return EX_NOINPUT;
}
2012-11-21 13:10:03 -07:00
snprintf(tmpfn, sizeof(tmpfn), "%s.%d", infn, getpid());
*outf = fopen(tmpfn, "wb");
if (! *outf) {
2012-11-20 18:13:16 -07:00
perror("Creating temporary database");
return EX_CANTCREAT;
}
2012-11-21 13:10:03 -07:00
cdb_init(inc, *inf);
cdbmake_init(outc, *outf);
*outfn = strdup(tmpfn);
return 0;
}
static void
finish_copy(char *infn, struct cdb_ctx *inc, FILE **inf,
char *outfn, struct cdbmake_ctx *outc, FILE **outf)
{
cdbmake_finalize(outc);
fclose(*outf);
fclose(*inf);
rename(outfn, infn);
free(outfn);
}
int
add(char *filename, char *key, char *val)
{
struct cdb_ctx inc;
struct cdbmake_ctx outc;
FILE *inf;
FILE *outf;
char *outfn;
int ret;
ret = setup_copy(filename, &inc, &inf, &outfn, &outc, &outf);
if (ret) {
return ret;
}
2012-11-20 18:13:16 -07:00
for (;;) {
char key[8192];
char val[8192];
size_t keylen = sizeof(key);
size_t vallen = sizeof(val);
if (EOF == cdb_dump(&inc, key, &keylen, val, &vallen)) {
break;
}
cdbmake_add(&outc, key, keylen, val, vallen);
}
cdbmake_add(&outc, key, strlen(key), val, strlen(val));
2012-11-21 13:10:03 -07:00
finish_copy(filename, &inc, &inf, outfn, &outc, &outf);
2012-11-20 20:02:04 -07:00
2012-11-20 18:13:16 -07:00
return 0;
}
2012-11-21 13:10:03 -07:00
int
del(char *filename, char *key, char *glob)
{
size_t keylen = strlen(key);
struct cdb_ctx inc;
struct cdbmake_ctx outc;
FILE *inf;
FILE *outf;
char *outfn;
int ret;
ret = setup_copy(filename, &inc, &inf, &outfn, &outc, &outf);
if (ret) {
return ret;
}
for (;;) {
char k[8192];
char v[8192];
size_t klen = sizeof(k);
size_t vlen = sizeof(v) - 1;
if (EOF == cdb_dump(&inc, k, &klen, v, &vlen)) {
break;
}
v[vlen] = '\0';
if ((klen == keylen) &&
(0 == memcmp(k, key, klen)) &&
(0 == fnmatch(glob, v, 0))) {
// Skip if it matches
2012-11-23 13:02:31 -07:00
printf("-%.*s\n", vlen, v);
2012-11-21 13:10:03 -07:00
} else {
cdbmake_add(&outc, k, klen, v, vlen);
}
}
finish_copy(filename, &inc, &inf, outfn, &outc, &outf);
return 0;
}
2012-11-21 14:25:40 -07:00
int
create(char *filename)
{
FILE *f = fopen(filename, "wb");
struct cdbmake_ctx outc;
if (! f) {
perror("Creating database");
return EX_CANTCREAT;
}
cdbmake_init(&outc, f);
cdbmake_finalize(&outc);
return 0;
}
2012-11-20 18:13:16 -07:00
enum action {
ACT_ONE,
ACT_ALL,
ACT_ADD,
2012-11-21 14:25:40 -07:00
ACT_DEL,
ACT_NEW
2012-11-20 18:13:16 -07:00
};
2012-11-14 15:09:06 -07:00
int
main(int argc, char *argv[])
{
char *filename;
2012-11-20 18:13:16 -07:00
char *key;
char *val;
enum action act = ACT_ONE;
for (;;) {
2012-11-21 14:25:40 -07:00
int opt = getopt(argc, argv, "hlna:r:");
2012-11-20 18:13:16 -07:00
if (-1 == opt) {
break;
}
switch (opt) {
case 'l':
act = ACT_ALL;
break;
2012-11-21 14:25:40 -07:00
case 'n':
act = ACT_NEW;
break;
2012-11-20 18:13:16 -07:00
case 'a':
act = ACT_ADD;
val = optarg;
break;
case 'r':
act = ACT_DEL;
val = optarg;
break;
default:
return usage(argv[0]);
}
}
2012-11-21 14:25:40 -07:00
if (! (filename = argv[optind++])) {
2012-11-21 21:39:25 -07:00
return usage(argv[0]);
2012-11-21 14:25:40 -07:00
}
if ((act != ACT_NEW) &&
(! (key = argv[optind++]))) {
2012-11-21 21:39:25 -07:00
return usage(argv[0]);
2012-11-21 14:25:40 -07:00
}
if (argv[optind]) {
2012-11-21 21:39:25 -07:00
return usage(argv[0]);
2012-11-14 15:09:06 -07:00
}
2012-11-20 18:13:16 -07:00
// Seed PRNG with some crap
2012-11-19 14:53:00 -07:00
{
struct timeval tv;
gettimeofday(&tv, NULL);
srand((unsigned int)(tv.tv_sec * tv.tv_usec));
}
2012-11-20 18:13:16 -07:00
switch (act) {
case ACT_ONE:
return choose(filename, key);
case ACT_ALL:
return list(filename, key);
case ACT_ADD:
return add(filename, key, val);
2012-11-21 13:10:03 -07:00
case ACT_DEL:
return del(filename, key, val);
2012-11-21 14:25:40 -07:00
case ACT_NEW:
return create(filename);
2012-11-20 18:13:16 -07:00
}
2012-11-20 18:13:16 -07:00
return 0;
2012-11-14 15:09:06 -07:00
}