diff options
author | EuAndreh <eu@euandre.org> | 2022-01-28 07:33:30 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-01-01 12:35:01 -0300 |
commit | 9f554a72b01705ebf6be66143c5e69b09b3c1372 (patch) | |
tree | fadf04038b2d5df32fd4b5bc93ba291269c322a0 /src | |
download | pindaiba-9f554a72b01705ebf6be66143c5e69b09b3c1372.tar.gz pindaiba-9f554a72b01705ebf6be66143c5e69b09b3c1372.tar.xz |
Init project: copy files and skeletons from others
Diffstat (limited to 'src')
-rw-r--r-- | src/catalog.c | 507 | ||||
-rw-r--r-- | src/catalog.h | 22 | ||||
-rw-r--r-- | src/config.h.in | 6 | ||||
-rw-r--r-- | src/hash.c | 644 | ||||
-rw-r--r-- | src/hash.h | 8 | ||||
-rw-r--r-- | src/i18n.c | 74 | ||||
-rw-r--r-- | src/i18n.h | 45 | ||||
-rw-r--r-- | src/lib.c | 20 | ||||
l--------- | src/lib.h | 1 | ||||
-rw-r--r-- | src/logerr.c | 314 | ||||
-rw-r--r-- | src/logerr.h | 12 | ||||
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/pindaiba.en.msg | 74 | ||||
-rw-r--r-- | src/pindaiba.h | 14 | ||||
-rw-r--r-- | src/random.c | 122 | ||||
-rw-r--r-- | src/random.h | 2 | ||||
-rw-r--r-- | src/testing.c | 114 | ||||
-rw-r--r-- | src/testing.h | 8 |
18 files changed, 1995 insertions, 0 deletions
diff --git a/src/catalog.c b/src/catalog.c new file mode 100644 index 0000000..31aef9a --- /dev/null +++ b/src/catalog.c @@ -0,0 +1,507 @@ +#include "config.h" + +#include <assert.h> +#include <errno.h> +#include <nl_types.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "logerr.h" +#include "catalog.h" + +#ifdef TEST +#include "testing.h" +#include "../tests/slurp.h" +#endif + + +#ifdef TEST +static const char *const +FNAME = __FILE__ ".txt"; + +enum TEST_MSGCATALOG_ID { + MSG_X_FIRST = 1, + MSG_X_1, + MSG_X_2, + MSG_X_LAST, + MSG_STANDALONE, +}; + +static const char *const +TEST_MSGS[] = { + "", + [MSG_X_FIRST]="First line\n", + [MSG_X_1]="a second\n", + [MSG_X_2]="a third\n", + [MSG_X_LAST]="and the last one\n", + [MSG_STANDALONE]="single line message\n", + NULL +}; +#endif + + + +static const char *const +CATALOG_NAME = NAME; + +static nl_catd +catalog_descriptor = NULL; + +static const char *const +NLSPATH = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" + LOCALEDIR "/%l/LC_MESSAGES/%N.cat"; + +static const char *const +NLSPATH_KEY = "NLSPATH"; + + +int +i18n_init(void) { + int rc = 0; + + static const int should_overwrite = 0; + if (setenv(NLSPATH_KEY, NLSPATH, should_overwrite)) { + logerr("setenv(\"%s\", \"%s\", 0): %s\n", NLSPATH_KEY, + NLSPATH, strerror(errno)); + rc = -1; + goto out; + } + + catalog_descriptor = catopen(CATALOG_NAME, 0); + if (catalog_descriptor && catalog_descriptor == (nl_catd)-1) { + logerr("catopen(\"%s\", 0): %s\n", CATALOG_NAME, strerror(errno)); + catalog_descriptor = NULL; + rc = -1; + goto out; + } + +out: + return rc; +} + +#ifdef TEST +static int +test_i18n_init(void) { + int rc = 0; + + test_start("i18n_init()"); + + { + testing("simple call without touching the environment"); + + const int should_overwrite = 1; + if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) { + logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n", + NLSPATH_KEY, strerror(errno)); + rc = -1; + goto out; + } + + if (i18n_init()) { + logerr("i18n_init()\n"); + rc = -1; + goto out; + } + + test_ok(); + } + +out: + if (i18n_destroy()) { + logerr("i18n_destroy()\n"); + rc = -1; + } + return rc; +} +#endif + + +int +i18n_destroy(void) { + int rc = 0; + + if (catalog_descriptor) { + if (catclose(catalog_descriptor)) { + logerr("catclose(...): %s\n", strerror(errno)); + rc = -1; + goto out; + } + } + +out: + if (catalog_descriptor) { + catalog_descriptor = NULL; + } + return rc; +} + +#ifdef TEST +static int +test_i18n_destroy(void) { + int rc = 0; + + test_start("i18n_destroy()"); + + { + testing("simple call without init first"); + + if (i18n_destroy()) { + logerr("i18n_destroy()\n"); + rc = -1; + goto out; + } + + test_ok(); + } + +out: + return rc; +} +#endif + + +/** + * Infallible: always returns a valid string, no matter what. + */ +const char * +s(const char* const MSGS[], const int msg_id) { + assert(msg_id > 0); + // FIXME: assert within bounds! + // printf("sizeof(MSGS): %ld\n", sizeof(MSGS)); + if (!catalog_descriptor) { + return MSGS[msg_id]; + } + + errno = 0; + const char *const ret = + catgets(catalog_descriptor, NL_SETD, msg_id, MSGS[msg_id]); + if (errno) { + logerr("catgets(%d): %s\n", msg_id, strerror(errno)); + } + + return ret; +} + +#ifdef TEST +static int +test_s(void) { + int rc = 0; + + test_start("_()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("empty string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + // FIXME: implement correct test + + + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} +#endif + + +int +s_print_msgs( + const char *const MSGS[], + FILE *restrict stream, + const int msg_begin, + const int msg_end +) { + int rc = 0; + + for (int i = msg_begin; i <= msg_end; i++) { + if (fprintf(stream, "%s", s(MSGS, i)) < 0) { + logerr("fprintf(stream, \"%%s\", _(%d)): %s\n", i, + strerror(errno)); + rc = -1; + goto out; + } + } + +out: + return rc; +} + +#ifdef TEST +static int +test_s_print_msgs(void) { + int rc = 0; + + test_start("s_print_msgs()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("message in range"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { + logerr("print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "First line\n" + "a second\n" + "a third\n" + "and the last one\n" + ; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("range begin and end is the same"); + + file = fopen(FNAME, "w"); + if (!file) { + logerr("fopen(FNAME, \"w\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { + logerr("s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "First line\n"; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} +#endif + + +int +s_print_msg(const char *const MSGS[], FILE *const fd, const int msg_id) { + return s_print_msgs(MSGS, fd, msg_id, msg_id); +} + +#ifdef TEST +static int +test_s_print_msg(void) { + int rc = 0; + + test_start("s_print_msg()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("simple individual message"); + + file = fopen(FNAME, "w"); + if (!file) { + logerr("fopen(FNAME, \"w\"): %s\n"); + rc = -1; + goto out; + } + + if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) { + logerr("s_print_msg(TEST_MSGS, file, MSG_STANDALONE)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fopen(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "single line message\n"; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} +#endif + + +int +dump_translatable_strings(const char *const MSGS[]) { + int rc = 0; + + for (size_t i = 1; MSGS[i]; i++) { + if (printf("%ld ", i) < 0) { + logerr("printf(\"%%ld\", %d): %s\n", i); + rc = -1; + goto out; + } + + for (size_t j = 0; MSGS[i][j]; j++) { + if (MSGS[i][j] == '\n') { + if (printf("\\n") < 0) { + logerr("printf(\"\\\\n\"): %s\n", + strerror(errno)); + rc = -1; + goto out; + } + } else { + if (printf("%c", MSGS[i][j]) < 0) { + logerr("printf(\"%%c\", " + "MSGS[%ld][%ld]): %s\n", + i, j, strerror(errno)); + rc = -1; + goto out; + } + } + } + + if (printf("\n\n") < 0) { + logerr("printf(\"\\n\\n\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + } + +out: + return rc; +} + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (test_i18n_init()) { + logerr("test_i18n_init()\n"); + rc = -1; + goto out; + } + + if (test_i18n_destroy()) { + logerr("test_i18n_destroy()\n"); + rc = -1; + goto out; + } + + if (test_s()) { + logerr("test_s()\n"); + rc = -1; + goto out; + } + + if (test_s_print_msgs()) { + logerr("test_s_print_msgs()\n"); + rc = -1; + goto out; + } + + if (test_s_print_msg()) { + logerr("test_s_print_msg()\n"); + rc = -1; + goto out; + } + +out: + return !!rc; +} +#endif diff --git a/src/catalog.h b/src/catalog.h new file mode 100644 index 0000000..25adbeb --- /dev/null +++ b/src/catalog.h @@ -0,0 +1,22 @@ +int +i18n_init(void); + +int +i18n_destroy(void); + +const char * +s(const char *const MSGS[], const int msg_id); + +int +s_print_msgs( + const char *const MSGS[], + FILE *restrict stream, + const int msg_begin, + const int msg_end +); + +int +s_print_msg(const char *const MSGS[], FILE *restrict stream, const int msg_id); + +int +dump_translatable_strings(const char *const MSGS[]); diff --git a/src/config.h.in b/src/config.h.in new file mode 100644 index 0000000..ab2256e --- /dev/null +++ b/src/config.h.in @@ -0,0 +1,6 @@ +#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200809L +#define VERSION "@VERSION@" +#define DATE "@DATE@" +#define NAME "@NAME@" +#define LOCALEDIR "@LOCALEDIR@" diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..c8fd5d6 --- /dev/null +++ b/src/hash.c @@ -0,0 +1,644 @@ +#include "config.h" + +#include <assert.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <pthread.h> + +#include "logerr.h" +#include "random.h" +#include "hash.h" + +#ifdef TEST +#include "testing.h" +#endif + + +// FIXME: verify that this is the correct type of mutex for the use case +static pthread_mutex_t +SEED_INIT_MUTEX = PTHREAD_MUTEX_INITIALIZER; + +#define PRIV_SEED_LENGTH 16 + +struct Seed { + bool initialized; + uint8_t bytes[PRIV_SEED_LENGTH]; +}; + +static struct Seed +SEED = { + .initialized = false, + .bytes = { 0 }, +}; + + +int +hash_init(void) { + int rc = 0; + + int ret = 0; + struct Seed seed = { 0 }; + + if (SEED.initialized) { + goto out; + } + + if (urandom_bytes(sizeof(seed.bytes), &seed.bytes)) { + logerr("urandom_bytes(sizeof(seed.bytes), &seed.bytes);\n"); + rc = -1; + goto out; + } + + ret = pthread_mutex_lock(&SEED_INIT_MUTEX); + if (ret) { + logerr( + "pthread_mutex_lock(&SEED_INIT_MUTEX): %s;\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + /* + In case where the seed was initialized by another thread while the + current was enterinig hash_init() and locking the mutex. The + previous check is only a best-effort one to avoid all threads that + start at the same time to serially acquire the mutex and do nothing + with. In order to sometimes avoid the extra overhead, the previous + check exists, but only the following check that has any correctness + guarantee, and instead of being best-effort it blocks others so it + can make the correct decision. + */ + if (SEED.initialized) { + goto out; + } + + // FIXME: memcpy? + const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]); + for (uint8_t i = 0; i < length; i++) { + SEED.bytes[i] = seed.bytes[i]; + } + + SEED.initialized = true; + +out: + // FIXME: verify that unlocking an unlocked mutex is fine + ret = pthread_mutex_unlock(&SEED_INIT_MUTEX); + if (ret) { + logerr( + "pthread_mutex_unlock(&SEED_INIT_MUTEX): %s\n", + strerror(ret) + ); + rc = -1; + } + return rc; +} + +#ifdef TEST +static void * +hash_init_thread(void *data) { + int *ret = (int *)data; + *ret = hash_init(); + pthread_exit(NULL); + return NULL; +} + +static int +test_hash_init(void) { + int rc = 0; + + test_start("hash_init()"); + + const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]); + + assert(length == 16); + for (uint8_t i = 0; i < length; i++) { + assert(SEED.bytes[i] == 0); + } + + pthread_mutex_t test_mutex = PTHREAD_MUTEX_INITIALIZER; + int ret; + + { + testing("when is initialized we do nothing"); + + SEED.initialized = true; + assert(hash_init() == 0); + + for (uint8_t i = 0; i < length; i++) { + assert(SEED.bytes[i] == 0); + } + + test_ok(); + } + + { + testing("do nothing when initialized on the second check"); + + /* + 0. start with an unitialized seed + 1. grab the lock + 2. start the other thread + 3. set SEED_INITIALIZED + 4. release the lock + 5. join the other thread + 6. assert HASH_SEED stilll empty + */ + + SEED.initialized = false; // 0 + ret = pthread_mutex_lock(&test_mutex); // 1 + if (ret) { + logerr( + "pthread_mutex_lock(&test_mutex): %s;\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + pthread_t t; + int t_rc = -1; + ret = pthread_create(&t, NULL, hash_init_thread, &t_rc); // 2 + if (ret) { + logerr("pthread_create(%s, %s, %s, %s): %s\n", + "&t", + "NULL", + "hash_init", + "&t_rc", + strerror(ret)); + rc = -1; + goto out; + } + + SEED.initialized = true; // 3 + ret = pthread_mutex_unlock(&test_mutex); // 4 + if (ret) { + logerr( + "pthread_mutex_unlock(&test_mutex): %s\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + ret = pthread_join(t, NULL); // 5 + if (ret) { + logerr("pthread_join(t, &rc): %s\n", strerror(ret)); + rc = -1; + goto out; + } + + if (t_rc) { + logerr("hash_init() in pthread_t\n"); + rc = -1; + goto out; + } + + for (size_t i = 0; i < length; i++) { + assert(SEED.bytes[i] == 0); // 6 + } + + test_ok(); + } + + { + testing("competing threads can init the seed besides us"); + + /* + 0. start with an unitialized seed + 1. grab the lock + 2. start the other 2 threads + 3. release the lock + 4. join the other threads + 5. assert(SEED_INITIALIZED == true); + */ + + SEED.initialized = false; // 0 + ret = pthread_mutex_lock(&test_mutex); // 1 + if (ret) { + logerr( + "pthread_mutex_lock(&SEED_INIT_MUTEX): %s;\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + pthread_t t1; + int t1_rc = -1; + ret = pthread_create(&t1, NULL, hash_init_thread, &t1_rc); // 2 + if (ret) { + logerr("pthread_create(%s, %s, %s, %s): %s\n", + "&t1", + "NULL", + "hash_init", + "&t1_rc", + strerror(ret)); + rc = -1; + goto out; + } + + pthread_t t2; + int t2_rc = -1; + ret = pthread_create(&t2, NULL, hash_init_thread, &t2_rc); // 2 + if (ret) { + logerr("pthread_create(%s, %s, %s, %s): %s\n", + "&t2", + "NULL", + "hash_init", + "&t2_rc", + strerror(ret)); + rc = -1; + goto out; + } + + ret = pthread_mutex_unlock(&test_mutex); // 3 + if (ret) { + logerr( + "pthread_mutex_unlock(&test_mutex): %s\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + ret = pthread_join(t1, NULL); // 4 + if (ret) { + logerr("pthread_join(t1, &rc): %s\n", strerror(ret)); + rc = -1; + goto out; + } + + if (t1_rc) { + logerr("hash_init() in pthread_t t1\n"); + rc = -1; + goto out; + } + + ret = pthread_join(t2, NULL); // 4 + if (ret) { + logerr("pthread_join(t2, &rc): %s\n", strerror(ret)); + rc = -1; + goto out; + } + + if (t2_rc) { + logerr("hash_init() in pthread_t t2\n"); + rc = -1; + goto out; + } + + assert(SEED.initialized == true); // 5 + + test_ok(); + } + +out: + ret = pthread_mutex_unlock(&test_mutex); + if (ret) { + logerr( + "pthread_mutex_unlock(&test_mutex): %s\n", + strerror(ret) + ); + rc = -1; + } + return rc; +} +#endif + + +#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) + +#define U32TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); + +#define U64TO8_LE(p, v) \ + U32TO8_LE((p), (uint32_t)((v))); \ + U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); + +#define U8TO64_LE(p) \ + (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ + ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ + ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ + ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) + +#define SIPROUND \ + do { \ + v0 += v1; \ + v1 = ROTL(v1, 13); \ + v1 ^= v0; \ + v0 = ROTL(v0, 32); \ + v2 += v3; \ + v3 = ROTL(v3, 16); \ + v3 ^= v2; \ + v0 += v3; \ + v3 = ROTL(v3, 21); \ + v3 ^= v0; \ + v2 += v1; \ + v1 = ROTL(v1, 17); \ + v1 ^= v2; \ + v2 = ROTL(v2, 32); \ + } while (0) + +/** + * Computes a SipHash value + * [in] key: pointer to the key data (read-only), must be 16 bytes + * [in] input: pointer to input data (read-only) + * [in] inlen: input data length in bytes (any size_t value) + * [out] out: pointer to output data (write-only), 16 bytes must be allocated + * + * @tags infallible +*/ +static void +sip_hash( + const uint8_t *const key, + const size_t inlen, + const void *const input, + uint8_t out[OUTPUT_LENGTH] +) { + static const int cROUNDS = 2; + static const int dROUNDS = 4; + + const unsigned char *ni = (const unsigned char *)input; + const unsigned char *kk = (const unsigned char *)key; + + uint64_t v0 = UINT64_C(0x736f6d6570736575); + uint64_t v1 = UINT64_C(0x646f72616e646f6d); + uint64_t v2 = UINT64_C(0x6c7967656e657261); + uint64_t v3 = UINT64_C(0x7465646279746573); + uint64_t k0 = U8TO64_LE(kk); /// Here we are relying on `key` to + uint64_t k1 = U8TO64_LE(kk + 8); /// be 16 bytes long. + uint64_t m; + int i; + const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t)); + const int left = inlen & 7; + uint64_t b = ((uint64_t)inlen) << 56; + v3 ^= k1; + v2 ^= k0; + v1 ^= k1; + v0 ^= k0; + + v1 ^= 0xee; + + for (; ni != end; ni += 8) { + m = U8TO64_LE(ni); + v3 ^= m; + + for (i = 0; i < cROUNDS; ++i) { + SIPROUND; + } + + v0 ^= m; + } + + switch (left) { + case 7: + b |= ((uint64_t)ni[6]) << 48; + /* fall-through */ + case 6: + b |= ((uint64_t)ni[5]) << 40; + /* fall-through */ + case 5: + b |= ((uint64_t)ni[4]) << 32; + /* fall-through */ + case 4: + b |= ((uint64_t)ni[3]) << 24; + /* fall-through */ + case 3: + b |= ((uint64_t)ni[2]) << 16; + /* fall-through */ + case 2: + b |= ((uint64_t)ni[1]) << 8; + /* fall-through */ + case 1: + b |= ((uint64_t)ni[0]); + break; + case 0: + break; + } + + v3 ^= b; + + for (i = 0; i < cROUNDS; ++i) { + SIPROUND; + } + + v0 ^= b; + + v2 ^= 0xee; + + for (i = 0; i < dROUNDS; ++i) { + SIPROUND; + } + + b = v0 ^ v1 ^ v2 ^ v3; + U64TO8_LE(out, b); + + v1 ^= 0xdd; + + for (i = 0; i < dROUNDS; ++i) { + SIPROUND; + } + + b = v0 ^ v1 ^ v2 ^ v3; + U64TO8_LE(out + 8, b); + + return; +} + +#ifdef TEST +static const uint8_t +TEST_VECTORS[64][OUTPUT_LENGTH] = { + { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6, 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93 }, + { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44, 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45 }, + { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7, 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4 }, + { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e, 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51 }, + { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf, 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79 }, + { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88, 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27 }, + { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13, 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e }, + { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53, 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39 }, + { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61, 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4 }, + { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab, 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed }, + { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56, 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba }, + { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77, 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18 }, + { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58, 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25 }, + { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10, 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7 }, + { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c, 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02 }, + { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11, 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9 }, + { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb, 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77 }, + { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98, 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40 }, + { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85, 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23 }, + { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0, 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1 }, + { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73, 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb }, + { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21, 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12 }, + { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a, 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae }, + { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0, 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c }, + { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8, 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad }, + { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b, 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f }, + { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8, 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66 }, + { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4, 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94 }, + { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88, 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4 }, + { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9, 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7 }, + { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde, 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87 }, + { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc, 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35 }, + { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e, 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68 }, + { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32, 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf }, + { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf, 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde }, + { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9, 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8 }, + { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46, 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11 }, + { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a, 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b }, + { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e, 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5 }, + { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c, 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9 }, + { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad, 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8 }, + { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39, 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb }, + { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb, 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b }, + { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa, 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89 }, + { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78, 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42 }, + { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65, 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c }, + { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89, 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02 }, + { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1, 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b }, + { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf, 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16 }, + { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f, 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03 }, + { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3, 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f }, + { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4, 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38 }, + { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e, 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c }, + { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94, 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e }, + { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c, 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87 }, + { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12, 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda }, + { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9, 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36 }, + { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe, 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e }, + { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8, 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d }, + { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe, 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59 }, + { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53, 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40 }, + { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe, 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a }, + { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0, 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd }, + { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a, 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c }, +}; + +/** + * @tags infallible +*/ +static void +test_sip_hash(void) { + test_start("sip_hash()"); + + { + testing("upstream test vectors"); + + const size_t LEN = 64; + uint8_t in[64 /* LEN */]; + + uint8_t out[OUTPUT_LENGTH]; + struct Seed key; + + const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]); + for (uint8_t i = 0; i < length; ++i) { + key.bytes[i] = i; + } + + for (uint8_t i = 0; i < LEN; ++i) { + in[i] = i; + } + + for (uint8_t i = 0; i < LEN; ++i) { + sip_hash(key.bytes, i, in, out); + for (uint8_t j = 0; j < OUTPUT_LENGTH; j++) { + assert(out[j] == TEST_VECTORS[i][j]); + } + } + + test_ok(); + } + + return; +} +#endif + + +/** + * @tags infallible +*/ +void +hash(const size_t len, const void *const in, uint8_t out[OUTPUT_LENGTH]) { + assert(SEED.initialized && "Missing call to hash_init() before using hash()."); + sip_hash(SEED.bytes, len, in, out); + return; +} + +#ifdef TEST +/** + * @tags infallible +*/ +static void +test_hash(void) { + test_start("hash()"); + + { + testing("we get the same hash given the same input"); + + uint8_t out1[OUTPUT_LENGTH] = { 0 }; + uint8_t out2[OUTPUT_LENGTH] = { 0 }; + + const char *const input_str = "the input"; + const uint8_t *const input = (uint8_t *)input_str; + const size_t input_length = strlen(input_str); + + hash(input_length, input, out1); + hash(input_length, input, out2); + + for (int i = 0; i < OUTPUT_LENGTH; i++) { + assert(out1[i] == out2[i]); + } + + test_ok(); + } + + { + testing("hashing twice overwrites whatever was in `out`"); + + uint8_t out1[OUTPUT_LENGTH]; + uint8_t out2[OUTPUT_LENGTH]; + + const char *const input_str = "another input"; + const uint8_t *const input = (uint8_t *)input_str; + const size_t input_length = strlen(input_str); + + hash(6, (uint8_t *)"before", out1); + hash(input_length, input, out1); + hash(input_length, input, out2); + + for (int i = 0; i < OUTPUT_LENGTH; i++) { + assert(out1[i] == out2[i]); + } + + test_ok(); + } + + return; +} +#endif + + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (test_hash_init()) { + logerr("test_hash_init();\n"); + rc = -1; + goto out; + } + + test_sip_hash(); + test_hash(); + +out: + return !!rc; +} +#endif diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..bdc6cbd --- /dev/null +++ b/src/hash.h @@ -0,0 +1,8 @@ +#define OUTPUT_LENGTH 16 + + +int +hash_init(void); + +void +hash(const size_t in_len, const void *const in, uint8_t out[OUTPUT_LENGTH]); diff --git a/src/i18n.c b/src/i18n.c new file mode 100644 index 0000000..60d900a --- /dev/null +++ b/src/i18n.c @@ -0,0 +1,74 @@ +#include "config.h" + +#include <stdio.h> + +#include "i18n.h" + +#ifdef TEST +#include <stdlib.h> + +#include "logerr.h" +#include "catalog.h" +#endif + +const char *const +MSGS[] = { + "", + [MSG_USAGE_FIRST]="Usage:\n", + [MSG_USAGE_1]=" " NAME " -p FILE [-o DIRECTORY]\n", + [MSG_USAGE_2]=" " NAME " -l FILE [-o DIRECTORY]\n", + [MSG_USAGE_LAST]=" " NAME " [-hV]\n", + [MSG_HELP_FIRST]="\n", + [MSG_HELP_1]="\n", + [MSG_HELP_2]="Options:\n", + [MSG_HELP_3]=" -p FILE parser file to be processed\n", + [MSG_HELP_4]=" -l FILE lexer file to be processed\n", + [MSG_HELP_5]=" -o DIRECTORY output where to place the\n", + [MSG_HELP_6]=" generated files (default .)\n", + [MSG_HELP_7]=" -h, --help show this help message\n", + [MSG_HELP_8]=" -V, --version print the version number\n", + [MSG_HELP_9]="\n", + [MSG_HELP_10]="\n", + [MSG_HELP_11]="Run the " NAME "(1) parser program.\n", + [MSG_HELP_12]="\n", + [MSG_HELP_13]="Here is the explanation for what it does, and the synopsis\n", + [MSG_HELP_14]="of its usage.\n", + [MSG_HELP_15]="\n", + [MSG_HELP_16]="See \"man " NAME "\" for usage information and\n", + [MSG_HELP_17]="\"man " NAME ".tutorial\" for a beginner introduction.\n", + [MSG_HELP_18]="\n", + [MSG_HELP_19]="\n", + [MSG_HELP_20]="Examples:\n", + [MSG_HELP_21]="\n", + [MSG_HELP_22]=" Do a one-line parser:\n", + [MSG_HELP_23]="\n", + [MSG_HELP_24]=" $ " NAME " run md.grammar < README.md\n", + [MSG_HELP_25]="\n", + [MSG_HELP_26]="\n", + [MSG_HELP_27]=" Compile the grammer:\n", + [MSG_HELP_28]="\n", + [MSG_HELP_LAST]=" $ " NAME " build csv.grammar > dunno.alsodunno\n", + [MSG_VERSION]= NAME " " VERSION " " DATE "\n", + [MSG_ERR_VECTOR_MAX_CAPACITY]= "Already at max capacity (%ld): %s\n", + [MSG_ERR_VECTOR_OUT_OF_BOUNDS]= "idx (%ld) is beyond bounds (%ld)\n", + NULL +}; + + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (getenv("DUMP_TRANSLATABLE_STRINGS")) { + if (dump_translatable_strings(MSGS)) { + logerr("dump_translatable_strings(MSGS)\n"); + rc = -1; + goto out; + } + } + +out: + return !!rc; +} +#endif diff --git a/src/i18n.h b/src/i18n.h new file mode 100644 index 0000000..2cf992e --- /dev/null +++ b/src/i18n.h @@ -0,0 +1,45 @@ +enum MSGCATALOG_ID { + MSG_USAGE_FIRST = 1, + MSG_USAGE_1, + MSG_USAGE_2, + MSG_USAGE_LAST, + MSG_HELP_FIRST, + MSG_HELP_1, + MSG_HELP_2, + MSG_HELP_3, + MSG_HELP_4, + MSG_HELP_5, + MSG_HELP_6, + MSG_HELP_7, + MSG_HELP_8, + MSG_HELP_9, + MSG_HELP_10, + MSG_HELP_11, + MSG_HELP_12, + MSG_HELP_13, + MSG_HELP_14, + MSG_HELP_15, + MSG_HELP_16, + MSG_HELP_17, + MSG_HELP_18, + MSG_HELP_19, + MSG_HELP_20, + MSG_HELP_21, + MSG_HELP_22, + MSG_HELP_23, + MSG_HELP_24, + MSG_HELP_25, + MSG_HELP_26, + MSG_HELP_27, + MSG_HELP_28, + MSG_HELP_LAST, + MSG_VERSION, + MSG_ERR_VECTOR_MAX_CAPACITY, + MSG_ERR_VECTOR_OUT_OF_BOUNDS, +}; + + +extern const char *const +MSGS[]; + +#define _(msg_id) s(MSGS, msg_id) diff --git a/src/lib.c b/src/lib.c new file mode 100644 index 0000000..8a8bff9 --- /dev/null +++ b/src/lib.c @@ -0,0 +1,20 @@ +#include <stdio.h> + + +int +re_main(int argc, char **argv) { + int rc = 0; + for (int i = 0; i < argc; i++) { + printf("argv[%d]: %s\n", i, argv[i]); + } + return rc; +} + + +#ifdef TEST +int +main(void) { + int rc = 0; + return rc; +} +#endif diff --git a/src/lib.h b/src/lib.h new file mode 120000 index 0000000..e823a5d --- /dev/null +++ b/src/lib.h @@ -0,0 +1 @@ +pindaiba.h
\ No newline at end of file diff --git a/src/logerr.c b/src/logerr.c new file mode 100644 index 0000000..eb160d2 --- /dev/null +++ b/src/logerr.c @@ -0,0 +1,314 @@ +#include "config.h" + +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> + +#include "logerr.h" + +#ifdef TEST +#include <assert.h> +#include <errno.h> +#include <string.h> + +#include "testing.h" +#include "../tests/slurp.h" +#endif + + +/** + * @tags infallible + */ +void +vlogerr( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const char *restrict format, + ... +) { + (void)fprintf(stream, "%s:%s:%d: ", file, function, lineno); + + va_list args; + va_start(args, format); + (void)vfprintf(stream, format, args); + va_end(args); + + return; +} + +#ifdef TEST +static const char *const +FNAME = __FILE__ ".txt"; + +static int +test_vlogerr(void) { + int rc = 0; + + test_start("vlogerr()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("empty varargs"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 100 + vlogerr(__FILE__, __func__, __LINE__, file, + ""); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:100: "; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("a newline only"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 200 + vlogerr(__FILE__, __func__, __LINE__, file, + "\n"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:200: \n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("static format string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 300 + vlogerr(__FILE__, __func__, __LINE__, file, + "some static string\n"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:300: some static string\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("single arg format string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 400 + vlogerr(__FILE__, __func__, __LINE__, file, + "fn(%s)\n", "an-arg"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:400: fn(an-arg)\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("multiple format strings"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 500 + vlogerr(__FILE__, __func__, __LINE__, file, + "int (%d), string (%s) and char (%c)\n", + 123, + "another-str", + 'z'); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:500: " + "int (123), string (another-str) and char (z)\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + perror("fclose(file)"); + rc = -1; + } + } + return rc; +} + +static int +test_logerr(void) { + int rc = 0; + + test_start("logerr()"); + + { + testing("can be called with an empty string"); + + logerr(""); + + test_ok(); + } + { + testing("can be called with a static string"); + + logerr("some err\n"); + + test_ok(); + } + { + testing("can be called with a formatted string"); + + logerr("some err: %s\n", strerror(errno)); + + test_ok(); + } + { + testing("can be called with formatting arguments"); + + logerr("int: %d\nstr: %s\n", 123, "an example string"); + + test_ok(); + } + + return rc; +} + + +int +main(void) { + int rc = 0; + + if (test_vlogerr()) { + perror("test_vlogerr()"); + rc = -1; + goto out; + } + + if (test_logerr()) { + perror("test_logerr()"); + rc = -1; + goto out; + } + +out: + return !!rc; +} +#endif diff --git a/src/logerr.h b/src/logerr.h new file mode 100644 index 0000000..56b2432 --- /dev/null +++ b/src/logerr.h @@ -0,0 +1,12 @@ +void +vlogerr( + + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const char *restrict format, + ... +); + +#define logerr(...) vlogerr(__FILE__, __func__, __LINE__, stderr, __VA_ARGS__) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ac9a1bd --- /dev/null +++ b/src/main.c @@ -0,0 +1,8 @@ +#include "config.h" + +#include "pindaiba.h" + +int +main(int argc, char **argv) { + return re_main(argc, argv); +} diff --git a/src/pindaiba.en.msg b/src/pindaiba.en.msg new file mode 100644 index 0000000..1fbb6f2 --- /dev/null +++ b/src/pindaiba.en.msg @@ -0,0 +1,74 @@ +1 Usage:\n + +2 pindaiba -p FILE [-o DIRECTORY]\n + +3 pindaiba -l FILE [-o DIRECTORY]\n + +4 pindaiba [-hV]\n + +5 \n + +6 \n + +7 Options:\n + +8 -p FILE parser file to be processed\n + +9 -l FILE lexer file to be processed\n + +10 -o DIRECTORY output where to place the\n + +11 generated files (default .)\n + +12 -h, --help show this help message\n + +13 -V, --version print the version number\n + +14 \n + +15 \n + +16 Run the pindaiba(1) parser program.\n + +17 \n + +18 Here is the explanation for what it does, and the synopsis\n + +19 of its usage.\n + +20 \n + +21 See "man pindaiba" for usage information and\n + +22 "man pindaiba.tutorial" for a beginner introduction.\n + +23 \n + +24 \n + +25 Examples:\n + +26 \n + +27 Do a one-line parser:\n + +28 \n + +29 $ pindaiba run md.grammar < README.md\n + +30 \n + +31 \n + +32 Compile the grammer:\n + +33 \n + +34 $ pindaiba build csv.grammar > dunno.alsodunno\n + +35 pindaiba 0.1.0 1970-01-01\n + +36 Already at max capacity (%ld): %s\n + +37 idx (%ld) is beyond bounds (%ld)\n + diff --git a/src/pindaiba.h b/src/pindaiba.h new file mode 100644 index 0000000..39c9f2d --- /dev/null +++ b/src/pindaiba.h @@ -0,0 +1,14 @@ +/** + * # Xa blau + * + * Xupli xablau xubliu, xubliu: + * + * ``` + * int + * main(int argc, char **argv) { + * return re_main(argc, argv); + * } + * ``` + */ +int +re_main(int argc, char **argv); diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000..d92d533 --- /dev/null +++ b/src/random.c @@ -0,0 +1,122 @@ +#include "config.h" + +#include <assert.h> +#include <errno.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "logerr.h" +#include "random.h" + +#ifdef TEST +#include "testing.h" +#endif + + +int +urandom_bytes(const size_t n, uint8_t (*const addr)[]) { + int rc = 0; + + FILE *f = NULL; + + f = fopen("/dev/urandom", "r"); + if (!f) { + logerr("fopen(\"/dev/urandom\", \"r\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + const size_t read_count = fread(addr, 1, n, f); + if (ferror(f)) { + logerr("fread(addr, 1, n, f): %s\n", strerror(errno)); + rc = -1; + goto out; + } + assert(read_count == n); + +out: + if (f) { + if (fclose(f)) { + logerr("fclose(f): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} + +#ifdef TEST +static int +test_urandom_bytes(void) { + int rc = 0; + + test_start("urandom_bytes()"); + + { + testing("we get to pick the size that comes out"); + + const size_t LEN = 256; + uint8_t arr[256 /* LEN */] = { 0 }; + + for (size_t n = 0; n < LEN; n++) { + if (urandom_bytes(n, &arr)) { + logerr("urandom_bytes(n, &arr);\n"); + rc = -1; + goto out; + } + for (size_t i = n; i < LEN; i++) { + assert(arr[i] == 0); + } + } + + test_ok(); + } + + { + testing("we always get a new value as a result"); + + const size_t LEN = 64; + uint8_t arr1[64 /* LEN */] = { 0 }; + uint8_t arr2[64 /* LEN */] = { 0 }; + + if (urandom_bytes(LEN, &arr1)) { + logerr("urandom_bytes(LEN, &arr1);\n"); + rc = -1; + goto out; + } + + const size_t attempts = 10; + for (size_t n = 0; n < attempts; n++) { + if (urandom_bytes(LEN, &arr2)) { + logerr("urandom_bytes(LEN, &arr2);\n"); + rc = -1; + goto out; + } + assert(memcmp(arr1, arr2, LEN) != 0); + } + + test_ok(); + } + +out: + return rc; +} +#endif + + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (test_urandom_bytes()) { + logerr("test_urandom_bytes();\n"); + rc = -1; + goto out; + } + +out: + return !!rc; +} +#endif diff --git a/src/random.h b/src/random.h new file mode 100644 index 0000000..ffaa69e --- /dev/null +++ b/src/random.h @@ -0,0 +1,2 @@ +int +urandom_bytes(const size_t n, uint8_t (*const addr)[]); diff --git a/src/testing.c b/src/testing.c new file mode 100644 index 0000000..028e48f --- /dev/null +++ b/src/testing.c @@ -0,0 +1,114 @@ +#include "config.h" + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#include "testing.h" + + +#define COLOUR_RESET "\033[0m" +#define COLOUR_GREEN "\033[0;32m" +#define COLOUR_YELLOW "\033[0;33m" + +static const char *const +ENVVAR_NAME = "NO_COLOR"; + + +static bool +show_colour(void) { + const char *const no_colour = getenv(ENVVAR_NAME); + return !no_colour || no_colour[0] == '\0'; +} + + +/** + * @tags infallible + */ +void +test_start(const char *const name) { + (void)fprintf(stderr, "%s:\n", name); + + return; +} + + +/** + * @tags infallible + */ +void +testing(const char *const message) { + if (show_colour()) { + (void)fprintf( + stderr, + COLOUR_YELLOW "testing" COLOUR_RESET ": %s...", + message + ); + } else { + (void)fprintf( + stderr, + "testing: %s...", + message + ); + } + + return; +} + + +/** + * @tags infallible + */ +void +test_ok(void) { + if (show_colour()) { + (void)fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n"); + } else { + (void)fprintf(stderr, " OK.\n"); + } + + return; +} + +#ifdef TEST +int +main(void) { + int rc = 0; + + test_start("testing.c"); + const int should_overwrite = 1; + + if (unsetenv(ENVVAR_NAME)) { + perror("unsetenv(\"NO_COLOR\")"); + rc = -1; + goto out; + } + { + testing("unset NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"\", 1)"); + rc = -1; + goto out; + } + { + testing("empty NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "something", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"something\", 1)"); + rc = -1; + goto out; + } + { + testing("defined NO_COLOR"); + test_ok(); + } + +out: + return !!rc; +} +#endif diff --git a/src/testing.h b/src/testing.h new file mode 100644 index 0000000..9a1bb2e --- /dev/null +++ b/src/testing.h @@ -0,0 +1,8 @@ +void +test_start(const char *const name); + +void +testing(const char *const message); + +void +test_ok(void); |