diff options
| author | George Abbott <george@gabbott.dev> | 2023-12-20 13:43:37 +0000 | 
|---|---|---|
| committer | George Abbott <george@gabbott.dev> | 2023-12-20 13:43:37 +0000 | 
| commit | 46d1a2f8d1455e1b4e400acccfee3a583770ff34 (patch) | |
| tree | 94f017c4fb04929727e3ae43fcdf74255d20bee5 | |
| parent | 33e185f075ad87d43571b053e0eab546dec2b74a (diff) | |
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | main.c | 146 | 
2 files changed, 148 insertions, 0 deletions
| diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b82f86f --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +wasureta: +	gcc -Wpedantic -Werror -Warith-conversion -Wconversion -std=c99 main.c -o wasureta @@ -0,0 +1,146 @@ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> + +#define COUNT_DECK_PARAMS 32 +#define PROGRAM_NAME "wasureta" + +/* wasureta: Anki on the terminal. + * Anki decks (.apkg) are found in: + * - $XDG_DATA_HOME/wasureta, or $HOME/.local/share/wasureta + * - passed in via the -d argument. + * + * When you run `wasureta`, the program will check for any decks, if any + * revision needs to be done. If so, it will give a choice of deck to select. + * On selecting the deck, the practice will begin. + * This only displays the text, not audio or images. + */ + +/* ******************************** Types ********************************** */ +enum error_t { +	err_copyfailed = 0, +}; + +/* **************************** Function Decls ***************************** */ +void die(const char*, ...); +void usage(void); +void version(void); +int run(void); +enum error_t copy_decks(const char* decks[], size_t decks_len); + +/* **************************** Function Impls ***************************** */ +void  +die(const char *fmt, ...) +{ +	va_list ap; +	va_start(ap, fmt); +	vfprintf(stderr, fmt, ap); +	va_end(ap); + +	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { +		fputc(' ', stderr); +		perror(NULL); +	} else { +		fputc('\n', stderr); +	} + +	exit(1); +} + +void  +usage(void) +{ +	printf( +		PROGRAM_NAME ": Anki on the terminal.\n" +		"\tSource:  https://gabbott.dev/git/" PROGRAM_NAME "\n" +		"\tLicense: BSD 3-Clause\n" +		"\n" +		"Usage:\n" +		"\t" PROGRAM_NAME " FLAGS\n" +		"\t-d DECK   Copy a deck to $XDG_DATA_HOME/" PROGRAM_NAME " for use.\n" +		"\t-h        Display this help message.\n" +		"\t-v        Display version information.\n" +	); +} + +void  +version(void)  +{ +	printf(PROGRAM_NAME ": ver 0.1.0\n"); +} + +enum error_t  +copy_decks(const char* decks[], size_t decks_len)  +{ +	const char xdgpath[] = "/.local/share"; +	const char dir[] = "/" PROGRAM_NAME; +	const char *xdg = getenv("XDG_DATA_HOME"); +	const char *home = getenv("HOME"); +	if (!xdg && !home) { +		die("Both HOME AND XDG_DATA_HOME envvars unset - this is bad!"); +	} +	 +	char* target; +	if (xdg) { +		target = malloc(strlen(xdg) + strlen(dir)); +		strcpy(target, xdg); +		strcat(target, dir); +	} else { +		target = malloc(strlen(home) + strlen(xdgpath)); +		strcpy(target, home); +		strcat(target, xdgpath); +	} + +	printf("target: %s\n", target); + +	for (size_t i = 0; i < decks_len; ++i) { +		printf("decks: %s", decks[i]); +	} + +/* cleanup:*/	free(target); + +	return err_copyfailed; +} + +int  +run(void) +{ +	return 0; +	 +} + +int  +main(int argc, char* argv[]) +{ +	const char* decks[COUNT_DECK_PARAMS] = {0}; +	size_t decks_index = 0; // used for setting decks values +	 +	for (int i = 0; i < argc; ++i) { +		if (argv[i][0] == '-') { +			switch (argv[i][1]) { +				case 'h': +					usage(); +					exit(0); +				case 'v': +					version(); +					exit(0); +				case 'd': +					decks[decks_index++] = argv[++i]; +					break; +				default: +					die("Unsupported flag -%c; run " PROGRAM_NAME " -h for usage information.", argv[i][1]); +			} +		} +	} + +	// If we have any decks, copy them into the appropriate location. +	if (decks_index) { +		enum error_t err = copy_decks(decks, decks_index); +		die("Failed to copy for -d flag. Error: %d", (int)err); +	} else { +		return run(); +	} +} + | 
