| 1 |
/* $FreeWRT: src/share/misc/licence.template,v 1.20 2006/12/11 21:04:56 tg Rel $ */ |
| 2 |
|
| 3 |
/*- |
| 4 |
* Copyright (c) 2007 |
| 5 |
* Thorsten Glaser <tg@mirbsd.de> |
| 6 |
* |
| 7 |
* Provided that these terms and disclaimer and all copyright notices |
| 8 |
* are retained or reproduced in an accompanying document, permission |
| 9 |
* is granted to deal in this work without restriction, including un- |
| 10 |
* limited rights to use, publicly perform, distribute, sell, modify, |
| 11 |
* merge, give away, or sublicence. |
| 12 |
* |
| 13 |
* Advertising materials mentioning features or use of this work must |
| 14 |
* display the following acknowledgement: |
| 15 |
* This product includes material provided by Thorsten Glaser. |
| 16 |
* This acknowledgement does not need to be reprinted if this work is |
| 17 |
* linked into a bigger work whose licence does not allow such clause |
| 18 |
* and the author of this work is given due credit in the bigger work |
| 19 |
* or its accompanying documents, where such information is generally |
| 20 |
* kept, provided that said credits are retained. |
| 21 |
* |
| 22 |
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to |
| 23 |
* the utmost extent permitted by applicable law, neither express nor |
| 24 |
* implied; without malicious intent or gross negligence. In no event |
| 25 |
* may a licensor, author or contributor be held liable for indirect, |
| 26 |
* direct, other damage, loss, or other issues arising in any way out |
| 27 |
* of dealing in the work, even if advised of the possibility of such |
| 28 |
* damage or existence of a defect, except proven that it results out |
| 29 |
* of said person's immediate fault when using the work as intended. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <sys/param.h> |
| 33 |
|
| 34 |
#include <err.h> |
| 35 |
#include <errno.h> |
| 36 |
#include <locale.h> |
| 37 |
#include <stdint.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <unistd.h> |
| 42 |
|
| 43 |
#define EXTERN |
| 44 |
#include "extern.h" |
| 45 |
|
| 46 |
extern const char *__progname; |
| 47 |
|
| 48 |
static void usage(void) __attribute__((noreturn)); |
| 49 |
|
| 50 |
int |
| 51 |
main(int argc, char *argv[]) |
| 52 |
{ |
| 53 |
int ch; |
| 54 |
const char *ofile; |
| 55 |
|
| 56 |
/* set a UTF-8 locale (which is required for SQLite3) */ |
| 57 |
ofile = "en_US.UTF-8"; |
| 58 |
errno = 0; |
| 59 |
if (strcmp(ofile, setlocale(LC_CTYPE, "en_US.UTF-8"))) |
| 60 |
warn("cannot set the UTF-8 locale"); |
| 61 |
|
| 62 |
/* make stdin unbuffered */ |
| 63 |
if (setvbuf(stdin, NULL, _IONBF, 0)) |
| 64 |
warn("cannot set stdin to unbuffered I/O"); |
| 65 |
|
| 66 |
ofile = "nfo_.db"; /* default output file */ |
| 67 |
/* parse arguments */ |
| 68 |
while ((ch = getopt(argc, argv, "do:")) != -1) |
| 69 |
switch (ch) { |
| 70 |
case 'd': |
| 71 |
++debug; |
| 72 |
break; |
| 73 |
case 'o': |
| 74 |
ofile = optarg; |
| 75 |
break; |
| 76 |
case '?': |
| 77 |
default: |
| 78 |
usage(); |
| 79 |
break; |
| 80 |
} |
| 81 |
argc -= optind; |
| 82 |
argv += optind; |
| 83 |
|
| 84 |
D(1, "debugging: on, level %d\n", debug); |
| 85 |
D(1, "writing to output database %s from %d files\n", ofile, argc); |
| 86 |
if (debug >= 2) { |
| 87 |
int i; |
| 88 |
for (i = 0; i < argc; ++i) |
| 89 |
fprintf(stderr, "- %s\n", argv[i]); |
| 90 |
} |
| 91 |
|
| 92 |
/* need at least one .nfo file to parse */ |
| 93 |
if (!argc) |
| 94 |
usage(); |
| 95 |
|
| 96 |
/* if the output file exists, remove it */ |
| 97 |
if (unlink(ofile) && (errno != ENOENT)) |
| 98 |
err(1, "cannot unlink output file \"%s\"", ofile); |
| 99 |
|
| 100 |
/* create the output database */ |
| 101 |
D(2, "creating database\n"); |
| 102 |
if (sqlite3_open(ofile, &db) != SQLITE_OK) |
| 103 |
dberr(1, "cannot create output file \"%s\"", ofile); |
| 104 |
sqlite3_extended_result_codes(db, 1); |
| 105 |
|
| 106 |
if (dbexecv(NULL, NULL, "%s", dbscheme)) |
| 107 |
dberr(1, "error processing the database scheme"); |
| 108 |
D(1, "database created\n"); |
| 109 |
|
| 110 |
for (const struct categories *ctp = categories; ctp->catname != NULL; |
| 111 |
++ctp) { |
| 112 |
xdbexecv(NULL, NULL, NULL, |
| 113 |
"INSERT INTO categories (name, desc) VALUES ('%s', '%s');", |
| 114 |
ctp->catname, ctp->catdesc); |
| 115 |
} |
| 116 |
|
| 117 |
/* parse input */ |
| 118 |
|
| 119 |
while (*argv) |
| 120 |
pfile(*argv++); |
| 121 |
|
| 122 |
/* … */ |
| 123 |
|
| 124 |
D(1, "finishing...\n"); |
| 125 |
if (sqlite3_close(db) != SQLITE_OK) |
| 126 |
dberr(255, "cannot close database"); |
| 127 |
return (0); |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
usage(void) |
| 132 |
{ |
| 133 |
fprintf(stderr, |
| 134 |
"Usage: %s [-d] [-o ofile] package/FOO/info.nfo ...\n", |
| 135 |
__progname); |
| 136 |
if (db) { |
| 137 |
fprintf(stderr, |
| 138 |
"Warning, usage() called too late.\n" |
| 139 |
"Please report this to the author!\n"); |
| 140 |
sqlite3_close(db); |
| 141 |
} |
| 142 |
exit(1); |
| 143 |
} |