88 lines
2.4 KiB
C
88 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include <limits.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <libgen.h>
|
|
#include "options.h"
|
|
|
|
Options options = { .progname = NULL, .datadir = DATADIR, .styleSheetName = "html5" };
|
|
|
|
char*
|
|
datadir(const char* progname) {
|
|
struct stat sb;
|
|
char* progname_copy = strdup(progname);
|
|
const char* progdir = dirname(progname_copy);
|
|
char* local_datadir = NULL;
|
|
char result[PATH_MAX];
|
|
int l = strlen(progdir) + 9;
|
|
|
|
if (stat(DATADIR, &sb) == 0) {
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
free(progname_copy);
|
|
return strdup(DATADIR);
|
|
}
|
|
}
|
|
|
|
local_datadir = malloc(l);
|
|
snprintf(local_datadir, l, "%s/../data", progdir);
|
|
realpath(local_datadir, result);
|
|
free(local_datadir);
|
|
free(progname_copy);
|
|
if (stat(result, &sb) == 0) {
|
|
if (S_ISDIR(sb.st_mode)) {
|
|
return strdup(result);
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void usage(const char* progname) {
|
|
fprintf(stderr, "Usage: %s [-h] FILE\n"
|
|
"\n"
|
|
" -h Display help message\n"
|
|
" -s STYLESHEET Apply stylesheet (default \"html5\")\n"
|
|
"\n"
|
|
" FILE sermon file to scan (\"-\" for stdin)\n", progname);
|
|
}
|
|
|
|
void InitOptions(int argc, const char* argv[]) {
|
|
int i = 0;
|
|
options.progname = argv[0];
|
|
options.datadir = datadir(options.progname);
|
|
while (++i < argc) {
|
|
if (strcmp(argv[i], "-h") == 0) { usage(options.progname); exit(0); }
|
|
else if (strcmp(argv[i], "-") == 0) {
|
|
options.inputFileName = argv[i];
|
|
} else if (strcmp(argv[i], "-s") == 0) {
|
|
options.styleSheetName = argv[++i];
|
|
} else if (strncmp(argv[i], "-s", 2) == 0) {
|
|
options.styleSheetName = argv[i] + 2;
|
|
} else if (argv[i][0] == '-') {
|
|
fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
|
} else {
|
|
options.inputFileName = argv[i];
|
|
}
|
|
}
|
|
|
|
/* input filename required */
|
|
if (!options.inputFileName) {
|
|
usage(options.progname);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
char*
|
|
OptionsDataFile(const char* fname) {
|
|
char result[PATH_MAX], *t = malloc(strlen(options.datadir) + strlen(fname) + 2);
|
|
snprintf(t, PATH_MAX, "%s/%s", options.datadir, fname);
|
|
realpath(t, result);
|
|
free(t);
|
|
return strdup(result);
|
|
}
|
|
|
|
void FreeOptions() {
|
|
free(options.datadir);
|
|
}
|