#include #include #include #include #include #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(); } }