aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/config.h.in9
-rw-r--r--src/logerr.c67
-rw-r--r--src/logerr.h34
-rw-r--r--src/remembering.c410
-rw-r--r--src/tests-lib.c29
-rw-r--r--src/tests-lib.h13
-rw-r--r--src/unit-test.h15
7 files changed, 0 insertions, 577 deletions
diff --git a/src/config.h.in b/src/config.h.in
deleted file mode 100644
index 5f7b398..0000000
--- a/src/config.h.in
+++ /dev/null
@@ -1,9 +0,0 @@
-#ifndef CONFIG_H
-#define CONFIG_H
-
-#define _POSIX_C_SOURCE 200809L
-#define VERSION "@VERSION@"
-#define DATE "@DATE@"
-#define PROGNAME "@NAME@"
-
-#endif
diff --git a/src/logerr.c b/src/logerr.c
deleted file mode 100644
index 029ed7f..0000000
--- a/src/logerr.c
+++ /dev/null
@@ -1,67 +0,0 @@
-#include "config.h"
-#include "logerr.h"
-
-#include <stdio.h>
-
-
-void
-logerr_file(
- const char *const s,
- const char *const msg,
- const char *const file,
- const int lineno
-) {
- fprintf(
- stderr,
- "%s:%s:%d: %s: %s\n",
- PROGNAME,
- file,
- lineno,
- s,
- msg
- );
-}
-
-void
-logerrs_file(
- const char *const pre,
- const char *const mid,
- const char *const post,
- const char *const msg,
- const char *const file,
- const int lineno
-) {
- fprintf(
- stderr,
- "%s:%s:%d: %s%s%s: %s\n",
- PROGNAME,
- file,
- lineno,
- pre,
- mid,
- post,
- msg
- );
-}
-
-void
-logerrl_file(
- const char *const pre,
- const size_t mid,
- const char *const post,
- const char *const msg,
- const char *const file,
- int lineno
-) {
- fprintf(
- stderr,
- "%s:%s:%d: %s%ld%s: %s\n",
- PROGNAME,
- file,
- lineno,
- pre,
- mid,
- post,
- msg
- );
-}
diff --git a/src/logerr.h b/src/logerr.h
deleted file mode 100644
index 47228ab..0000000
--- a/src/logerr.h
+++ /dev/null
@@ -1,34 +0,0 @@
-#ifndef GISTATIC_LOGERR_H
-#define GISTATIC_LOGERR_H
-
-#include <stddef.h>
-
-void
-logerr_file(
- const char *const s,
- const char *const msg,
- const char *const file,
- const int lineno
-);
-
-void
-logerrs_file(
- const char *const pre,
- const char *const mid,
- const char *const post,
- const char *const msg,
- const char *const file,
- const int lineno
-);
-
-void
-logerrl_file(
- const char *const pre,
- const size_t mid,
- const char *const post,
- const char *const msg,
- const char *const file,
- const int lineno
-);
-
-#endif
diff --git a/src/remembering.c b/src/remembering.c
deleted file mode 100644
index bca6ec0..0000000
--- a/src/remembering.c
+++ /dev/null
@@ -1,410 +0,0 @@
-#include "config.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <libgen.h>
-#include <sys/stat.h>
-
-
-#ifdef TEST
-#include "unit-test.h"
-#include "tests-lib.h"
-#include <stdbool.h>
-#include <errno.h>
-#endif
-
-
-const int EXIT_ERROR = 1;
-const int EXIT_USAGE = 2;
-
-static int
-usage(FILE *stream) {
- const char *const msg = "Usage: remembering -p PROFILE -c COMMAND\n";
- if (fprintf(stream, msg) < 0) {
- perror("usage() - fprintf()");
- return -1;
- }
- return 0;
-}
-
-static int
-help(FILE *stream) {
- static const char *const msg =
- "Options:\n"
- " -p, PROFILE profile to be used for gathering and storing data\n"
- " -c, COMMAND commant to be run, reading from STDIN, writing to STDOUT\n"
- " -h, --help show this help\n"
- " -V, --version print the version number\n"
- "\nSee \"man remembering\" for more information\n";
- if (fprintf(stream, msg) < 0) {
- perror("help() - fprint()");
- return -1;
- }
- return 0;
-}
-
-static int
-version(FILE *stream) {
- const char *const msg = "remembering-" VERSION " " DATE "\n";
- if (fprintf(stream, msg) < 0) {
- perror("version() - fprintf()");
- return -1;
- }
- return 0;
-}
-
-static int
-missing(FILE *stream, const char *const argument) {
- const char *const msg = "Missing option: %s\n";
- if (fprintf(stream, msg, argument) < 0) {
- perror("missing() - fprintf()");
- return -1;
- }
- return 0;
-}
-
-static char *
-get_profile_file(const char *const profile, FILE *stream) {
- char *home = getenv("XDG_DATA_HOME");
- if (!home || (strcmp(home, "") == 0)) {
- if (!(home = getenv("HOME"))) {
- fprintf(stream, "Unable to get $XDG_DATA_HOME or $HOME environment variables\n");
- return NULL;
- }
- const char *const suffix = "/.local/share/remembering";
- const size_t home_suffix_size =
- strlen(home) +
- strlen(suffix) +
- sizeof(char);
- char *const home_with_suffix = malloc(home_suffix_size);
- if (!home_with_suffix) {
- perror("get_profile_file() - malloc() - home_with_suffix");
- return NULL;
- }
- strcpy(home_with_suffix, home);
- strcat(home_with_suffix, suffix);
- home = home_with_suffix;
- } else {
- if (!(home = strdup(home))) {
- perror("get_profile_file() - strdup()");
- return NULL;
- }
- }
-
- size_t size =
- strlen(home) +
- strlen("/") +
- strlen(profile) +
- sizeof(char);
- char *const profile_file = malloc(size);
- if (!profile_file) {
- perror("get_profile_file() - malloc() - profile_file");
- free(home);
- return NULL;
- }
- strcpy(profile_file, home);
- strcat(profile_file, "/");
- strcat(profile_file, profile);
-
- free(home);
- return profile_file;
-}
-
-#ifdef TEST
-static void
-test_get_profile_file(void) {
- #define EXPECTED_FMT "\nexpected: %s\ngot: %s\n"
- FILE *f = fopen("/dev/null", "w");
- assert(f);
-
- {
- testing("get_profile_file() when $XDG_DATA_HOME is *unset*");
- const char *const previous_xdg_data_home =
- getenv("XDG_DATA_HOME");
- unsetenv("XDG_DATA_HOME");
-
- const char *const HOME = getenv("HOME");
- const char *const SUFFIX = "/.local/share/remembering/";
- const char *const A_PROFILE = "A-PROFILE-NAME";
- assert(HOME);
-
- const size_t expected_size =
- strlen(HOME) +
- strlen(SUFFIX) +
- strlen(A_PROFILE) +
- sizeof(char);
- char *const expected = malloc(expected_size);
- assert(expected);
- strcpy(expected, HOME);
- strcat(expected, SUFFIX);
- strcat(expected, A_PROFILE);
-
- char *const profile_file = get_profile_file(A_PROFILE, f);
- assert(profile_file);
- assertf(
- strcmp(profile_file, expected) == 0,
- EXPECTED_FMT,
- expected,
- profile_file
- );
-
- free(profile_file);
- free(expected);
- if (previous_xdg_data_home) {
- setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
- }
- test_ok();
- }
- {
- testing("get_profile_file() when $XDG_DATA_HOME is *empty*");
- const char *const previous_xdg_data_home =
- getenv("XDG_DATA_HOME");
- setenv("XDG_DATA_HOME", "", true);
-
- const char *const HOME = getenv("HOME");
- const char *const SUFFIX = "/.local/share/remembering/";
- const char *const A_PROFILE = "ANOTHER-PROFILE-NAME";
- assert(HOME);
-
- const size_t expected_size =
- strlen(HOME) +
- strlen(SUFFIX) +
- strlen(A_PROFILE) +
- sizeof(char);
- char *const expected = malloc(expected_size);
- assert(expected);
- strcpy(expected, HOME);
- strcat(expected, SUFFIX);
- strcat(expected, A_PROFILE);
-
- char *const profile_file = get_profile_file(A_PROFILE, f);
- assert(profile_file);
- assertf(
- strcmp(profile_file, expected) == 0,
- EXPECTED_FMT,
- expected,
- profile_file
- );
-
- free(profile_file);
- free(expected);
- if (previous_xdg_data_home) {
- setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
- } else {
- unsetenv("XDG_DATA_HOME");
- }
- test_ok();
- }
- {
- testing("get_profile_file() when $XDG_DATA_HOME is *set*");
- const char *const previous_xdg_data_home =
- getenv("XDG_DATA_HOME");
-
- const char *const XDG_DATA_HOME = "/a/custom/path";
- const char *const A_PROFILE = "YET-ANOTHER-PROFILE-NAME";
- const char *const EXPECTED =
- "/a/custom/path/YET-ANOTHER-PROFILE-NAME";
-
- setenv("XDG_DATA_HOME", XDG_DATA_HOME, true);
-
- char *const profile_file = get_profile_file(A_PROFILE, f);
- assert(profile_file);
- assertf(
- strcmp(profile_file, EXPECTED) == 0,
- EXPECTED_FMT,
- EXPECTED,
- profile_file
- );
-
- free(profile_file);
- if (previous_xdg_data_home) {
- setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
- } else {
- unsetenv("XDG_DATA_HOME");
- }
- test_ok();
- }
- {
- testing("get_profile_file() when $HOME is *unset*");
- const char *const previous_home = getenv("HOME");
- const char *const previous_xdg_data_home =
- getenv("XDG_DATA_HOME");
- unsetenv("XDG_DATA_HOME");
- unsetenv("HOME");
-
- assert(get_profile_file("ANY-PROFILE", f) == NULL);
-
- setenv("HOME", previous_home, true);
- if (previous_xdg_data_home) {
- setenv("XDG_DATA_HOME", previous_xdg_data_home, true);
- }
- test_ok();
- }
-
- fclose(f);
-}
-#endif
-
-static int
-mkdir_p(const char *const path, mode_t mode) {
- struct stat s;
- int ret;
-
- if (stat(path, &s) == 0 && S_ISDIR(s.st_mode)) {
- return 0;
- }
-
- char *const path_dup = strdup(path);
- if (!path_dup) {
- perror("mkdir_p() - strdup()");
- return -1;
- }
-
- const char *const parent = dirname(path_dup);
- if ((ret = mkdir_p(parent, mode))) {
- free(path_dup);
- return ret;
- }
-
- free(path_dup);
- return mkdir(path, mode);
-}
-
-#ifdef TEST
-static void
-test_mkdir_p(void) {
- {
- testing("mkdir_p() with an existing directory is a noop");
- struct stat s;
- assert(stat("/tmp", &s) == 0 && S_ISDIR(s.st_mode));
- assert(mkdir_p("/tmp", S_IRWXU | S_IRWXG | S_IRWXO) == 0);
- assert(stat("/tmp", &s) == 0 && S_ISDIR(s.st_mode));
- test_ok();
- }
- {
- testing("mkdir_p() with a new directory creates it");
- struct stat s;
- const char *const SUBDIRECTORY_SUFFIX = "/a/b/c/d/e";
- char template[] = "/tmp/remembering.XXXXXX";
- const char *const prefix = mkdtemp(template);
- assert(prefix);
-
- assert(stat(prefix, &s) == 0 && S_ISDIR(s.st_mode));
-
- const size_t subdirectory_size =
- strlen(prefix) +
- strlen(SUBDIRECTORY_SUFFIX) +
- sizeof(char);
- char *const subdirectory = malloc(subdirectory_size);
- assert(subdirectory);
- strcpy(subdirectory, prefix);
- strcat(subdirectory, SUBDIRECTORY_SUFFIX);
-
- assert(stat(subdirectory, &s) == -1 && errno == ENOENT);
- assert(mkdir_p(subdirectory, S_IRWXU | S_IRWXG | S_IRWXO) == 0);
- assert(stat(subdirectory, &s) == 0 && S_ISDIR(s.st_mode));
-
- free(subdirectory);
- test_ok();
- }
-}
-#endif
-
-#ifdef TEST
-static void
-unit_tests(void) {
- test_get_profile_file();
- test_mkdir_p();
-}
-#endif
-
-int
-main(int argc, char *argv[]) {
-#ifdef TEST
- unit_tests();
- return EXIT_SUCCESS;
-#endif
-
- for (int i = 0; i < argc; i++) {
- if (strcmp("--", argv[i]) == 0) {
- break;
- } else if (strcmp("--help", argv[i]) == 0) {
- if (usage(stdout)) return EXIT_ERROR;
- if (help(stdout)) return EXIT_ERROR;
- return EXIT_SUCCESS;
- } else if (strcmp("--version", argv[i]) == 0) {
- if (version(stdout)) return EXIT_ERROR;
- return EXIT_SUCCESS;
- }
- }
-
- int option;
- const char *profile = NULL;
- const char *command = NULL;
- while ((option = getopt(argc, argv, "p:c:hV")) != -1) {
- switch (option) {
- case 'p':
- profile = optarg;
- break;
- case 'c':
- command = optarg;
- break;
- case 'h':
- if (usage(stdout)) return EXIT_ERROR;
- if (help(stdout)) return EXIT_ERROR;
- return EXIT_SUCCESS;
- case 'V':
- if (version(stdout)) return EXIT_ERROR;
- return EXIT_SUCCESS;
- default:
- if (usage(stderr)) return EXIT_ERROR;
- return EXIT_USAGE;
- }
- }
-
- if (!profile) {
- if (missing(stderr, "-p PROFILE")) return EXIT_ERROR;
- if (usage(stderr)) return EXIT_ERROR;
- return EXIT_USAGE;
- }
-
- if (!command) {
- if (missing(stderr, "-c COMMAND")) return EXIT_ERROR;
- if (usage(stderr)) return EXIT_ERROR;
- return EXIT_USAGE;
- }
-
- /* End getopt() */
-
-
- int ret = EXIT_SUCCESS;
-
- char *profile_file = NULL;
- char *profile_file_dup = NULL;
- if (!(profile_file = get_profile_file(profile, stderr))) {
- ret = EXIT_ERROR;
- goto cleanup;
- }
-
- if (!(profile_file_dup = strdup(profile_file))) {
- perror("main() - strdup()");
- ret = EXIT_ERROR;
- goto cleanup;
- }
-
- if ((ret = mkdir_p(dirname(profile_file_dup),
- S_IRWXU | S_IRWXG | S_IRWXO))) {
- goto cleanup;
- }
-
- printf("profile: %s\ncommand: %s\n", profile, command);
- printf("profile_file: %s\n", profile_file);
-
-
-cleanup:
- free(profile_file_dup);
- free(profile_file);
- return ret;
-}
diff --git a/src/tests-lib.c b/src/tests-lib.c
deleted file mode 100644
index a9d229a..0000000
--- a/src/tests-lib.c
+++ /dev/null
@@ -1,29 +0,0 @@
-#include "config.h"
-#include "tests-lib.h"
-#include <stdio.h>
-#include <assert.h>
-
-#define COLOUR_RESET "\033[0m"
-#define COLOUR_GREEN "\033[0;32m"
-#define COLOUR_YELLOW "\033[0;33m"
-
-void
-test_start(const char *const name) {
- assert(fprintf(stderr, "%s():\n", name) > 0);
-}
-
-void
-testing(const char *const message) {
- assert(
- fprintf(
- stderr,
- COLOUR_YELLOW "testing" COLOUR_RESET ": %s...",
- message
- ) > 0
- );
-}
-
-void
-test_ok(void) {
- assert(fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n") > 0);
-}
diff --git a/src/tests-lib.h b/src/tests-lib.h
deleted file mode 100644
index 1ceab27..0000000
--- a/src/tests-lib.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#ifndef GISTATIC_TESTS_LIB_H
-#define GISTATIC_TESTS_LIB_H
-
-void
-test_start(const char *const name);
-
-void
-testing(const char *const message);
-
-void
-test_ok(void);
-
-#endif
diff --git a/src/unit-test.h b/src/unit-test.h
deleted file mode 100644
index 2188f9f..0000000
--- a/src/unit-test.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef UNIT_TEST_H
-#define UNIT_TEST_H
-
-#include <assert.h>
-
-#define COLOUR_RESET "\033[0m"
-#define COLOUR_RED "\033[0;31m"
-#define COLOUR_GREEN "\033[0;32m"
-#define COLOUR_YELLOW "\033[0;33m"
-
-#define ASSERT_MSG "\n" COLOUR_RED "ERROR" COLOUR_RESET ": "
-#define assertf(A, M, ...) if (!(A)) { fprintf(stderr, (ASSERT_MSG M "\n"), __VA_ARGS__); assert(A); }
-#define asserte(A) if (!(A)) { fprintf(stderr, ASSERT_MSG "\n"); assert(A); }
-
-#endif