Skip to content

Commit

Permalink
Add cmd line options for dictpopup_create
Browse files Browse the repository at this point in the history
  • Loading branch information
btrkeks committed May 20, 2024
1 parent d1366a3 commit 9fc9cd6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
20 changes: 13 additions & 7 deletions man1/dictpopup-create.1
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
.TH DICTPOPUP\-CREATE 1

.SH SYNOPSIS
.B dictpopup\-create
[\fILOCATION\fR]...
\fBdictpopup\-create\fR [\fIOPTION\fR]...

.SH DESCRIPTION
.B dictpopup\-create
creates a database used by
.BR dictpopup (1)
from the Yomichan dictionary files in the current directory.
By default, the database is stored in $XDG_DATA_HOME/dictpopup
which can be overriden by providing \fILOCATION\fR.
parses Yomichan dictionary files to create a database used by
.BR dictpopup (1).

.SH OPTIONS
.TP
\fB\-i\fR \fIPATH\fR
Specify the directory containing the dictionaries to parse. The default is the current directory.
.TP
\fB\-d\fR \fIPATH\fR
Specify the directory where the database will be stored. The default is ~/.local/share/dictpopup.
20 changes: 11 additions & 9 deletions man1/dictpopup.1
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
.TH DICTPOPUP 1
.SH SYNOPSIS
.B dictpopup
[\fB\-cdh\fR]
[\fIsearch\fR]...
[\fB\-ch\fR]
[\fILOOKUP\fR]...
.SH DESCRIPTION
.B dictpopup
looks up selected Japanese text in Yomichan dictionaries
and displays the search result in a gtk3 window.
It does this by means of an index made by
.BR dictpopup\-create (1).
looks up selected Japanese text in a database created by
.BR dictpopup\-create (1)
and displays the search result in a gtk3 window.
.sp
Debug messages can be enabled by defining the environment variable DP_DEBUG, e.g.:
.sp
.nf
\&$ DP_DEBUG=1 dictpopup "面白い"
.fi
.SH OPTIONS
.TP
.BR \-d
Enable debug messages.
.TP
.BR \-c
Print parsed config.
.TP
Expand Down
50 changes: 33 additions & 17 deletions src/dictpopup_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#include "platformdep.h"
#include "util.h"

typedef struct {
s8 dbpath;
s8 dictspath;
} config;

#define error_return(retval, ...) \
do { \
err(__VA_ARGS__); \
Expand Down Expand Up @@ -475,10 +480,6 @@ static bool askyn(const char *msg) {
return true;
}

static s8 get_default_dbpath(void) {
return buildpath(fromcstr_((char *)get_user_data_dir()), S("dictpopup"));
}

static void sigint_handler(int s) {
sigint_received = 1;
}
Expand All @@ -489,14 +490,17 @@ static void setup_sighandler(void) {
sigaction(SIGINT, &act, NULL);
}

static void parse_cmd_line(int argc, char **argv, s8 dbpath[static 1]) {
static void parse_cmd_line(int argc, char **argv, config *cfg) {
int c;
opterr = 0;

while ((c = getopt(argc, argv, "hd:")) != -1)
while ((c = getopt(argc, argv, "hd:i:")) != -1)
switch (c) {
case 'd':
*dbpath = s8dup(fromcstr_(optarg));
cfg->dbpath = s8dup(fromcstr_(optarg));
break;
case 'i':
cfg->dictspath = s8dup(fromcstr_(optarg));
break;
case 'h':
puts("See 'man dictpopup-create' for help.");
Expand All @@ -509,33 +513,45 @@ static void parse_cmd_line(int argc, char **argv, s8 dbpath[static 1]) {
}
}

static void set_default_values(config *cfg) {
if (!cfg->dbpath.len) {
cfg->dbpath = buildpath(fromcstr_((char *)get_user_data_dir()), S("dictpopup"));
msg("Storing database in: %.*s", (int)cfg->dbpath.len, (char *)cfg->dbpath.s);
}

if (!cfg->dictspath.len) {
msg("Using current directory as dictionary path.");
cfg->dbpath = S(".");
}
}

#ifndef UNIT_TEST
int main(int argc, char *argv[]) {
setup_sighandler();

_drop_(frees8) s8 dbdir = {0};
parse_cmd_line(argc, argv, &dbdir);
if (!dbdir.len)
dbdir = get_default_dbpath();
dbg("Using database path: %s", (char *)dbdir.s);
config cfg = {0};
parse_cmd_line(argc, argv, &cfg);
set_default_values(&cfg);

if (db_check_exists(dbdir)) {
if (db_check_exists(cfg.dbpath)) {
if (askyn("A database file already exists. Would you like to delete the old one?"))
db_remove(dbdir);
db_remove(cfg.dbpath);
else
exit(EXIT_FAILURE);
} else
createdir((char *)dbdir.s);
createdir((char *)cfg.dbpath.s);

_drop_(db_close) database_t *db = db_open((char *)dbdir.s, false);
_drop_(closedir) DIR *dir = opendir(".");
_drop_(db_close) database_t *db = db_open((char *)cfg.dbpath.s, false);
_drop_(closedir) DIR *dir = opendir((char *)cfg.dictspath.s);
die_on(!dir, "Error opening current directory: %s", strerror(errno));

struct dirent *entry;
while ((entry = readdir(dir)) && !sigint_received) {
s8 fn = fromcstr_(entry->d_name);
_drop_(frees8) s8 fn = buildpath(cfg.dictspath, fromcstr_(entry->d_name));
if (endswith(fn, S(".zip")))
add_from_zip(db, (char *)fn.s);
}
}
#endif
#endif

0 comments on commit 9fc9cd6

Please sign in to comment.