summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-04-07 13:59:47 -0500
committersanine-a <sanine.not@pm.me>2023-04-07 13:59:47 -0500
commitd377b707b67b477ee5b22b08c714be34f3e82aa3 (patch)
treec898711c17f3834b255731ffc1250de71fe4c8c2 /src
parent6e31ca0c2e31e33f824aef1b4d68a91b64721fa7 (diff)
switch to cargs and remove honeysuckle
Diffstat (limited to 'src')
-rw-r--r--src/argent.c62
-rw-r--r--src/bindings.c45
-rw-r--r--src/cargs/cargs.c437
-rw-r--r--src/cargs/cargs.h162
-rw-r--r--src/honeysuckle/honeysuckle.h523
-rw-r--r--src/honeysuckle/hs_create_table.c123
-rw-r--r--src/honeysuckle/hs_parse_args.c149
-rw-r--r--src/honeysuckle/hs_process_table.c86
-rw-r--r--src/honeysuckle/hs_pushstring.c31
-rw-r--r--src/honeysuckle/hs_throw_error.c12
-rw-r--r--src/honeysuckle/hs_traceback.c37
-rw-r--r--src/honeysuckle/hs_type_to_string.c31
-rw-r--r--src/options.c84
13 files changed, 730 insertions, 1052 deletions
diff --git a/src/argent.c b/src/argent.c
index fb0d37b..adf6230 100644
--- a/src/argent.c
+++ b/src/argent.c
@@ -2,8 +2,6 @@
#include <lualib.h>
#include <lauxlib.h>
-#include "honeysuckle.h"
-
#include "options.h"
#include "bindings.h"
#include "logging.h"
@@ -25,6 +23,9 @@ struct settings {
} while(0)
+int hs_call(lua_State *L, int nargs, int nret);
+
+
int main(int argc, char **argv)
{
argent_log(DEBUG, "parse command-line options");
@@ -45,16 +46,16 @@ int main(int argc, char **argv)
luaL_openlibs(L);
argent_log(DEBUG, "create argent table");
- hs_create_table
- (L,
- hs_str_cfunc("markdown", markdown),
- hs_str_cfunc("currentWorkingDirectory", current_working_directory),
- hs_str_cfunc("scanDirectory", scan_directory),
- hs_str_cfunc("createDirectory", create_directory),
- hs_str_cfunc("copyFile", copy_file),
- hs_str_cfunc("log", argent_log_lua)
- );
- lua_setglobal(L, "argent");
+ luaL_Reg tbl[] = {
+ {"markdown", markdown},
+ {"currentWorkingDirectory", current_working_directory},
+ {"scanDirectory", scan_directory},
+ {"createDirectory", create_directory},
+ {"copyFile", copy_file},
+ {"log", argent_log_lua},
+ {NULL, NULL},
+ };
+ luaL_register(L, "argent", tbl);
// load argent lua script
argent_log(DEBUG, "load main lua script");
@@ -90,3 +91,40 @@ int main(int argc, char **argv)
lua_close(L);
return 0;
}
+
+
+int hs_traceback(lua_State *L)
+{
+ if (!lua_isstring(L, 1))
+ /* 'message' is not a string, keep intact */
+ return 1;
+
+ lua_getglobal(L, "debug");
+ if (!lua_istable(L, -1)) {
+ lua_pop(L, 1);
+ return 1;
+ }
+
+ lua_getfield(L, -1, "traceback");
+ if (!lua_isfunction(L, -1)) {
+ lua_pop(L, 2);
+ return 1;
+ }
+
+ lua_pushvalue(L, 1);
+ lua_pushinteger(L, 2);
+ lua_call(L, 2, 1);
+ return 1;
+}
+
+
+int hs_call(lua_State *L, int nargs, int nret)
+{
+ int traceback_pos = lua_gettop(L) - nargs;
+ lua_pushcfunction(L, hs_traceback);
+ lua_insert(L, traceback_pos);
+
+ int result = lua_pcall(L, nargs, nret, traceback_pos);
+ lua_remove(L, traceback_pos);
+ return result;
+}
diff --git a/src/bindings.c b/src/bindings.c
index 1a73fb0..5813d68 100644
--- a/src/bindings.c
+++ b/src/bindings.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include <lua.h>
+#include <lauxlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -8,7 +9,6 @@
#include <errno.h>
#include <fcntl.h>
-#include "honeysuckle.h"
#include "md4c-html.h"
#include "bindings.h"
#include "logging.h"
@@ -50,8 +50,7 @@ static void md_callback(const MD_CHAR *html, MD_SIZE size, void *data)
int markdown(lua_State *L)
{
argent_log(TRACE, "begin markdown parsing");
- char *markdown_buffer;
- hs_parse_args(L, hs_str(markdown_buffer));
+ char *markdown_buffer = luaL_checkstring(L, 1);
size_t len = strlen(markdown_buffer);
argent_log(TRACE, "markdown input (%ld bytes): %s", len, markdown_buffer);
@@ -67,10 +66,10 @@ int markdown(lua_State *L)
// fill out the buffer
int error = md_html(markdown_buffer, len, md_callback, &data, md_flags, 0);
if (error)
- hs_throw_error(L, "markdown parsing failed!");
+ luaL_error(L, "markdown parsing failed!");
if (data.ok == false)
- hs_throw_error(L, "encountered error (re)allocating html buffer memory!");
+ luaL_error(L, "encountered error (re)allocating html buffer memory!");
data.buf[data.index] = 0;
@@ -103,8 +102,7 @@ static struct dirent *read_dir(lua_State *L, DIR *directory);
int scan_directory(lua_State *L)
{
argent_log(TRACE, "begin scan_directory()");
- char *dir_name;
- hs_parse_args(L, hs_str(dir_name));
+ char *dir_name = luaL_checkstring(L, 1);
DIR *directory = opendir(dir_name);
if (directory == NULL)
@@ -142,23 +140,23 @@ static void throw_directory_error(lua_State *L, char *dir_name)
argent_log(ERROR, "failed to open directory: %d\n", errno);
switch(errno) {
case EACCES:
- hs_throw_error(L, "read %s: permission denied", dir_name);
+ luaL_error(L, "read %s: permission denied", dir_name);
break;
case EMFILE:
- hs_throw_error(L, "read %s: this process has too many open files", dir_name);
+ luaL_error(L, "read %s: this process has too many open files", dir_name);
break;
case ENFILE:
- hs_throw_error(L, "read %s: this file system cannot support any more open files", dir_name);
+ luaL_error(L, "read %s: this file system cannot support any more open files", dir_name);
break;
case ENOMEM:
- hs_throw_error(L, "read %s: Not enough memory", dir_name);
+ luaL_error(L, "read %s: Not enough memory", dir_name);
break;
default:
- hs_throw_error(L, "read %s: unknown error", dir_name);
+ luaL_error(L, "read %s: unknown error", dir_name);
break;
}
}
@@ -173,7 +171,7 @@ static struct dirent *read_dir(lua_State *L, DIR *directory)
if (entry == NULL && errno != 0) {
argent_log(ERROR, "attempted to read invalid dirstream");
closedir(directory);
- hs_throw_error(L, "attempted to read invalid dirstream");
+ luaL_error(L, "attempted to read invalid dirstream");
}
return entry;
@@ -207,8 +205,7 @@ static const char* mkdir_error_string()
int create_directory(lua_State *L)
{
- char *dir_name;
- hs_parse_args(L, hs_str(dir_name));
+ char *dir_name = luaL_checkstring(L, 1);
mode_t mode = S_IRWXU | S_IRWXG | (S_IROTH | S_IXOTH); // u+rwx, g+rwx, a+rx
@@ -216,7 +213,7 @@ int create_directory(lua_State *L)
if (error) {
argent_log(ERROR, "failed to create directory '%s': %s",
dir_name, mkdir_error_string());
- hs_throw_error(L, "failed to create directory '%s': %s",
+ luaL_error(L, "failed to create directory '%s': %s",
dir_name, mkdir_error_string());
}
@@ -278,8 +275,8 @@ static const char* open_error_string()
int copy_file(lua_State *L)
{
- char *source_name, *dest_name;
- hs_parse_args(L, hs_str(source_name), hs_str(dest_name));
+ char *source_name = luaL_checkstring(L, 1);
+ char *dest_name = luaL_checkstring(L, 2);
int source_fd, dest_fd;
@@ -287,7 +284,7 @@ int copy_file(lua_State *L)
if (source_fd == -1) {
argent_log(ERROR, "failed to open file '%s': %s",
source_name, open_error_string());
- hs_throw_error(L, "failed to open file '%s': %s",
+ luaL_error(L, "failed to open file '%s': %s",
source_name, open_error_string());
}
@@ -297,7 +294,7 @@ int copy_file(lua_State *L)
close(source_fd);
argent_log(ERROR, "failed to stat '%s': %s",
source_name, open_error_string());
- hs_throw_error(L, "failed to stat '%s': %s",
+ luaL_error(L, "failed to stat '%s': %s",
source_name, open_error_string());
}
mode_t dest_mode = source_stat.st_mode;
@@ -306,7 +303,7 @@ int copy_file(lua_State *L)
close(source_fd);
argent_log(ERROR, "failed to open file '%s': %s",
dest_name, open_error_string());
- hs_throw_error(L, "failed to open file '%s': %s",
+ luaL_error(L, "failed to open file '%s': %s",
dest_name, open_error_string());
}
@@ -321,7 +318,7 @@ int copy_file(lua_State *L)
argent_log(ERROR, "failed to write to file '%s'", dest_name);
close(source_fd);
close(dest_fd);
- hs_throw_error(L, "failed to write to file '%s'", dest_name);
+ luaL_error(L, "failed to write to file '%s'", dest_name);
}
else {
write(dest_fd, buffer, bytes_read);
@@ -338,8 +335,8 @@ int copy_file(lua_State *L)
int argent_log_lua(lua_State *L)
{
- char *level_string, *message;
- hs_parse_args(L, hs_str(level_string), hs_str(message));
+ char *level_string = luaL_checkstring(L, 1);
+ char *message = luaL_checkstring(L, 2);
int level;
diff --git a/src/cargs/cargs.c b/src/cargs/cargs.c
new file mode 100644
index 0000000..9f0ff7f
--- /dev/null
+++ b/src/cargs/cargs.c
@@ -0,0 +1,437 @@
+#include <assert.h>
+#include "cargs.h"
+#include <memory.h>
+#include <stdio.h>
+#include <string.h>
+
+#define CAG_OPTION_PRINT_DISTANCE 4
+#define CAG_OPTION_PRINT_MIN_INDENTION 20
+
+static void cag_option_print_value(const cag_option *option,
+ size_t *accessor_length, FILE *destination)
+{
+ if (option->value_name != NULL) {
+ *accessor_length += fprintf(destination, "=%s", option->value_name);
+ }
+}
+
+static void cag_option_print_letters(const cag_option *option, bool *first,
+ size_t *accessor_length, FILE *destination)
+{
+ const char *access_letter;
+ access_letter = option->access_letters;
+ if (access_letter != NULL) {
+ while (*access_letter) {
+ if (*first) {
+ *accessor_length += fprintf(destination, "-%c", *access_letter);
+ *first = false;
+ } else {
+ *accessor_length += fprintf(destination, ", -%c", *access_letter);
+ }
+ ++access_letter;
+ }
+ }
+}
+
+static void cag_option_print_name(const cag_option *option, bool *first,
+ size_t *accessor_length, FILE *destination)
+{
+ if (option->access_name != NULL) {
+ if (*first) {
+ *accessor_length += fprintf(destination, "--%s", option->access_name);
+ } else {
+ *accessor_length += fprintf(destination, ", --%s", option->access_name);
+ }
+ }
+}
+
+static size_t cag_option_get_print_indention(const cag_option *options,
+ size_t option_count)
+{
+ size_t option_index, indention, result;
+ const cag_option *option;
+
+ result = CAG_OPTION_PRINT_MIN_INDENTION;
+
+ for (option_index = 0; option_index < option_count; ++option_index) {
+ indention = CAG_OPTION_PRINT_DISTANCE;
+ option = &options[option_index];
+ if (option->access_letters != NULL && *option->access_letters) {
+ indention += strlen(option->access_letters) * 4 - 2;
+ if (option->access_name != NULL) {
+ indention += strlen(option->access_name) + 4;
+ }
+ } else if (option->access_name != NULL) {
+ indention += strlen(option->access_name) + 2;
+ }
+
+ if (option->value_name != NULL) {
+ indention += strlen(option->value_name) + 1;
+ }
+
+ if (indention > result) {
+ result = indention;
+ }
+ }
+
+ return result;
+}
+
+void cag_option_print(const cag_option *options, size_t option_count,
+ FILE *destination)
+{
+ size_t option_index, indention, i, accessor_length;
+ const cag_option *option;
+ bool first;
+
+ indention = cag_option_get_print_indention(options, option_count);
+
+ for (option_index = 0; option_index < option_count; ++option_index) {
+ option = &options[option_index];
+ accessor_length = 0;
+ first = true;
+
+ fputs(" ", destination);
+
+ cag_option_print_letters(option, &first, &accessor_length, destination);
+ cag_option_print_name(option, &first, &accessor_length, destination);
+ cag_option_print_value(option, &accessor_length, destination);
+
+ for (i = accessor_length; i < indention; ++i) {
+ fputs(" ", destination);
+ }
+
+ fputs(" ", destination);
+ fputs(option->description, destination);
+
+ fprintf(destination, "\n");
+ }
+}
+
+void cag_option_prepare(cag_option_context *context, const cag_option *options,
+ size_t option_count, int argc, char **argv)
+{
+ // This just initialized the values to the beginning of all the arguments.
+ context->options = options;
+ context->option_count = option_count;
+ context->argc = argc;
+ context->argv = argv;
+ context->index = 1;
+ context->inner_index = 0;
+ context->forced_end = false;
+}
+
+static const cag_option *cag_option_find_by_name(cag_option_context *context,
+ char *name, size_t name_size)
+{
+ const cag_option *option;
+ size_t i;
+
+ // We loop over all the available options and stop as soon as we have found
+ // one. We don't use any hash map table, since there won't be that many
+ // arguments anyway.
+ for (i = 0; i < context->option_count; ++i) {
+ option = &context->options[i];
+
+ // The option might not have an item name, we can just skip those.
+ if (option->access_name == NULL) {
+ continue;
+ }
+
+ // Try to compare the name of the access name. We can use the name_size or
+ // this comparison, since we are guaranteed to have null-terminated access
+ // names.
+ if (strncmp(option->access_name, name, name_size) == 0) {
+ return option;
+ }
+ }
+
+ return NULL;
+}
+
+static const cag_option *cag_option_find_by_letter(cag_option_context *context,
+ char letter)
+{
+ const cag_option *option;
+ size_t i;
+
+ // We loop over all the available options and stop as soon as we have found
+ // one. We don't use any look up table, since there won't be that many
+ // arguments anyway.
+ for (i = 0; i < context->option_count; ++i) {
+ option = &context->options[i];
+
+ // If this option doesn't have any access letters we will skip them.
+ if (option->access_letters == NULL) {
+ continue;
+ }
+
+ // Verify whether this option has the access letter in it's access letter
+ // string. If it does, then this is our option.
+ if (strchr(option->access_letters, letter) != NULL) {
+ return option;
+ }
+ }
+
+ return NULL;
+}
+
+static void cag_option_parse_value(cag_option_context *context,
+ const cag_option *option, char **c)
+{
+ // And now let's check whether this option is supposed to have a value, which
+ // is the case if there is a value name set. The value can be either submitted
+ // with a '=' sign or a space, which means we would have to jump over to the
+ // next argv index. This is somewhat ugly, but we do it to behave the same as
+ // the other option parsers.
+ if (option->value_name != NULL) {
+ if (**c == '=') {
+ context->value = ++(*c);
+ } else {
+ // If the next index is larger or equal to the argument count, then the
+ // parameter for this option is missing. The user will know about this,
+ // since the value pointer of the context will be NULL because we don't
+ // set it here in that case.
+ if (context->argc > context->index + 1) {
+ // We consider this argv to be the value, no matter what the contents
+ // are.
+ ++context->index;
+ *c = context->argv[context->index];
+ context->value = *c;
+ }
+ }
+
+ // Move c to the end of the value, to not confuse the caller about our
+ // position.
+ while (**c) {
+ ++(*c);
+ }
+ }
+}
+
+static void cag_option_parse_access_name(cag_option_context *context, char **c)
+{
+ const cag_option *option;
+ char *n;
+
+ // Now we need to extract the access name, which is any symbol up to a '=' or
+ // a '\0'.
+ n = *c;
+ while (**c && **c != '=') {
+ ++*c;
+ }
+
+ // Now this will obviously always be true, but we are paranoid. Sometimes. It
+ // doesn't hurt to check.
+ assert(*c >= n);
+
+ // Figure out which option this name belongs to. This might return NULL if the
+ // name is not registered, which means the user supplied an unknown option. In
+ // that case we return true to indicate that we finished with this option. We
+ // have to skip the value parsing since we don't know whether the user thinks
+ // this option has one or not. Since we don't set any identifier specifically,
+ // it will remain '?' within the context.
+ option = cag_option_find_by_name(context, n, (size_t)(*c - n));
+ if (option == NULL) {
+ // Since this option is invalid, we will move on to the next index. There is
+ // nothing we can do about this.
+ ++context->index;
+ return;
+ }
+
+ // We found an option and now we can specify the identifier within the
+ // context.
+ context->identifier = option->identifier;
+
+ // And now we try to parse the value. This function will also check whether
+ // this option is actually supposed to have a value.
+ cag_option_parse_value(context, option, c);
+
+ // And finally we move on to the next index.
+ ++context->index;
+}
+
+static void cag_option_parse_access_letter(cag_option_context *context,
+ char **c)
+{
+ const cag_option *option;
+ char *n = *c;
+ char *v;
+
+ // Figure out which option this letter belongs to. This might return NULL if
+ // the letter is not registered, which means the user supplied an unknown
+ // option. In that case we return true to indicate that we finished with this
+ // option. We have to skip the value parsing since we don't know whether the
+ // user thinks this option has one or not. Since we don't set any identifier
+ // specifically, it will remain '?' within the context.
+ option = cag_option_find_by_letter(context, n[context->inner_index]);
+ if (option == NULL) {
+ ++context->index;
+ context->inner_index = 0;
+ return;
+ }
+
+ // We found an option and now we can specify the identifier within the
+ // context.
+ context->identifier = option->identifier;
+
+ // And now we try to parse the value. This function will also check whether
+ // this option is actually supposed to have a value.
+ v = &n[++context->inner_index];
+ cag_option_parse_value(context, option, &v);
+
+ // Check whether we reached the end of this option argument.
+ if (*v == '\0') {
+ ++context->index;
+ context->inner_index = 0;
+ }
+}
+
+static void cag_option_shift(cag_option_context *context, int start, int option,
+ int end)
+{
+ char *tmp;
+ int a_index, shift_index, shift_count, left_index, right_index;
+
+ shift_count = option - start;
+
+ // There is no shift is required if the start and the option have the same
+ // index.
+ if (shift_count == 0) {
+ return;
+ }
+
+ // Lets loop through the option strings first, which we will move towards the
+ // beginning.
+ for (a_index = option; a_index < end; ++a_index) {
+ // First remember the current option value, because we will have to save
+ // that later at the beginning.
+ tmp = context->argv[a_index];
+
+ // Let's loop over all option values and shift them one towards the end.
+ // This will override the option value we just stored temporarily.
+ for (shift_index = 0; shift_index < shift_count; ++shift_index) {
+ left_index = a_index - shift_index;
+ right_index = a_index - shift_index - 1;
+ context->argv[left_index] = context->argv[right_index];
+ }
+
+ // Now restore the saved option value at the beginning.
+ context->argv[a_index - shift_count] = tmp;
+ }
+
+ // The new index will be before all non-option values, in such a way that they
+ // all will be moved again in the next fetch call.
+ context->index = end - shift_count;
+}
+
+static bool cag_option_is_argument_string(const char *c)
+{
+ return *c == '-' && *(c + 1) != '\0';
+}
+
+static int cag_option_find_next(cag_option_context *context)
+{
+ int next_index, next_option_index;
+ char *c;
+
+ // Prepare to search the next option at the next index.
+ next_index = context->index;
+ next_option_index = next_index;
+
+ // Grab a pointer to the string and verify that it is not the end. If it is
+ // the end, we have to return false to indicate that we finished.
+ c = context->argv[next_option_index];
+ if (context->forced_end || c == NULL) {
+ return -1;
+ }
+
+ // Check whether it is a '-'. We need to find the next option - and an option
+ // always starts with a '-'. If there is a string "-\0", we don't consider it
+ // as an option neither.
+ while (!cag_option_is_argument_string(c)) {
+ c = context->argv[++next_option_index];
+ if (c == NULL) {
+ // We reached the end and did not find any argument anymore. Let's tell
+ // our caller that we reached the end.
+ return -1;
+ }
+ }
+
+ // Indicate that we found an option which can be processed. The index of the
+ // next option will be returned.
+ return next_option_index;
+}
+
+bool cag_option_fetch(cag_option_context *context)
+{
+ char *c;
+ int old_index, new_index;
+
+ // Reset our identifier to a question mark, which indicates an "unknown"
+ // option. The value is set to NULL, to make sure we are not carrying the
+ // parameter from the previous option to this one.
+ context->identifier = '?';
+ context->value = NULL;
+
+ // Check whether there are any options left to parse and remember the old
+ // index as well as the new index. In the end we will move the option junk to
+ // the beginning, so that non option arguments can be read.
+ old_index = context->index;
+ new_index = cag_option_find_next(context);
+ if (new_index >= 0) {
+ context->index = new_index;
+ } else {
+ return false;
+ }
+
+ // Grab a pointer to the beginning of the option. At this point, the next
+ // character must be a '-', since if it was not the prepare function would
+ // have returned false. We will skip that symbol and proceed.
+ c = context->argv[context->index];
+ assert(*c == '-');
+ ++c;
+
+ // Check whether this is a long option, starting with a double "--".
+ if (*c == '-') {
+ ++c;
+
+ // This might be a double "--" which indicates the end of options. If this
+ // is the case, we will not move to the next index. That ensures that
+ // another call to the fetch function will not skip the "--".
+ if (*c == '\0') {
+ context->forced_end = true;
+ } else {
+ // We parse now the access name. All information about it will be written
+ // to the context.
+ cag_option_parse_access_name(context, &c);
+ }
+ } else {
+ // This is no long option, so we can just parse an access letter.
+ cag_option_parse_access_letter(context, &c);
+ }
+
+ // Move the items so that the options come first followed by non-option
+ // arguments.
+ cag_option_shift(context, old_index, new_index, context->index);
+
+ return context->forced_end == false;
+}
+
+char cag_option_get(const cag_option_context *context)
+{
+ // We just return the identifier here.
+ return context->identifier;
+}
+
+const char *cag_option_get_value(const cag_option_context *context)
+{
+ // We just return the internal value pointer of the context.
+ return context->value;
+}
+
+int cag_option_get_index(const cag_option_context *context)
+{
+ // Either we point to a value item,
+ return context->index;
+}
diff --git a/src/cargs/cargs.h b/src/cargs/cargs.h
new file mode 100644
index 0000000..17cba0a
--- /dev/null
+++ b/src/cargs/cargs.h
@@ -0,0 +1,162 @@
+#pragma once
+
+/**
+ * This is a simple alternative cross-platform implementation of getopt, which
+ * is used to parse argument strings submitted to the executable (argc and argv
+ * which are received in the main function).
+ */
+
+#ifndef CAG_LIBRARY_H
+#define CAG_LIBRARY_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+#define CAG_EXPORT __declspec(dllexport)
+#define CAG_IMPORT __declspec(dllimport)
+#elif __GNUC__ >= 4
+#define CAG_EXPORT __attribute__((visibility("default")))
+#define CAG_IMPORT __attribute__((visibility("default")))
+#else
+#define CAG_EXPORT
+#define CAG_IMPORT
+#endif
+
+#if defined(CAG_SHARED)
+#if defined(CAG_EXPORTS)
+#define CAG_PUBLIC CAG_EXPORT
+#else
+#define CAG_PUBLIC CAG_IMPORT
+#endif
+#else
+#define CAG_PUBLIC
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * An option is used to describe a flag/argument option submitted when the
+ * program is run.
+ */
+typedef struct cag_option
+{
+ const char identifier;
+ const char *access_letters;
+ const char *access_name;
+ const char *value_name;
+ const char *description;
+} cag_option;
+
+/**
+ * A context is used to iterate over all options provided. It stores the parsing
+ * state.
+ */
+typedef struct cag_option_context
+{
+ const struct cag_option *options;
+ size_t option_count;
+ int argc;
+ char **argv;
+ int index;
+ int inner_index;
+ bool forced_end;
+ char identifier;
+ char *value;
+} cag_option_context;
+
+/**
+ * This is just a small macro which calculates the size of an array.
+ */
+#define CAG_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+
+/**
+ * @brief Prints all options to the terminal.
+ *
+ * This function prints all options to the terminal. This can be used to
+ * generate the output for a "--help" option.
+ *
+ * @param options The options which will be printed.
+ * @param option_count The option count which will be printed.
+ * @param destination The destination where the output will be printed.
+ */
+CAG_PUBLIC void cag_option_print(const cag_option *options, size_t option_count,
+ FILE *destination);
+
+/**
+ * @brief Prepare argument options context for parsing.
+ *
+ * This function prepares the context for iteration and initializes the context
+ * with the supplied options and arguments. After the context has been prepared,
+ * it can be used to fetch arguments from it.
+ *
+ * @param context The context which will be initialized.
+ * @param options The registered options which are available for the program.
+ * @param option_count The amount of options which are available for the
+ * program.
+ * @param argc The amount of arguments the user supplied in the main function.
+ * @param argv A pointer to the arguments of the main function.
+ */
+CAG_PUBLIC void cag_option_prepare(cag_option_context *context,
+ const cag_option *options, size_t option_count, int argc, char **argv);
+
+/**
+ * @brief Fetches an option from the argument list.
+ *
+ * This function fetches a single option from the argument list. The context
+ * will be moved to that item. Information can be extracted from the context
+ * after the item has been fetched.
+ * The arguments will be re-ordered, which means that non-option arguments will
+ * be moved to the end of the argument list. After all options have been
+ * fetched, all non-option arguments will be positioned after the index of
+ * the context.
+ *
+ * @param context The context from which we will fetch the option.
+ * @return Returns true if there was another option or false if the end is
+ * reached.
+ */
+CAG_PUBLIC bool cag_option_fetch(cag_option_context *context);
+
+/**
+ * @brief Gets the identifier of the option.
+ *
+ * This function gets the identifier of the option, which should be unique to
+ * this option and can be used to determine what kind of option this is.
+ *
+ * @param context The context from which the option was fetched.
+ * @return Returns the identifier of the option.
+ */
+CAG_PUBLIC char cag_option_get(const cag_option_context *context);
+
+/**
+ * @brief Gets the value from the option.
+ *
+ * This function gets the value from the option, if any. If the option does not
+ * contain a value, this function will return NULL.
+ *
+ * @param context The context from which the option was fetched.
+ * @return Returns a pointer to the value or NULL if there is no value.
+ */
+CAG_PUBLIC const char *cag_option_get_value(const cag_option_context *context);
+
+/**
+ * @brief Gets the current index of the context.
+ *
+ * This function gets the index within the argv arguments of the context. The
+ * context always points to the next item which it will inspect. This is
+ * particularly useful to inspect the original argument array, or to get
+ * non-option arguments after option fetching has finished.
+ *
+ * @param context The context from which the option was fetched.
+ * @return Returns the current index of the context.
+ */
+CAG_PUBLIC int cag_option_get_index(const cag_option_context *context);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif
diff --git a/src/honeysuckle/honeysuckle.h b/src/honeysuckle/honeysuckle.h
deleted file mode 100644
index 96243ba..0000000
--- a/src/honeysuckle/honeysuckle.h
+++ /dev/null
@@ -1,523 +0,0 @@
-#ifndef HONEYSUCKLE_H
-#define HONEYSUCKLE_H
-
-#include <stdbool.h>
-
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-
-/* variadic macro argument counting */
-#ifndef VA_NARGS
-
-#define VA_NARGS(...) VA_NARGS_SHIFT_COUNT(__VA_ARGS__, VA_NARGS_COUNTS)
-
-#define VA_NARGS_COUNTS \
- 255, 254, 253, 252, 251, 250, 249, 248, \
- 247, 246, 245, 244, 243, 242, 241, 240, \
- 239, 238, 237, 236, 235, 234, 233, 232, \
- 231, 230, 229, 228, 227, 226, 225, 224, \
- 223, 222, 221, 220, 219, 218, 217, 216, \
- 215, 214, 213, 212, 211, 210, 209, 208, \
- 207, 206, 205, 204, 203, 202, 201, 200, \
- 199, 198, 197, 196, 195, 194, 193, 192, \
- 191, 190, 189, 188, 187, 186, 185, 184, \
- 183, 182, 181, 180, 179, 178, 177, 176, \
- 175, 174, 173, 172, 171, 170, 169, 168, \
- 167, 166, 165, 164, 163, 162, 161, 160, \
- 159, 158, 157, 156, 155, 154, 153, 152, \
- 151, 150, 149, 148, 147, 146, 145, 144, \
- 143, 142, 141, 140, 139, 138, 137, 136, \
- 135, 134, 133, 132, 131, 130, 129, 128, \
- 127, 126, 125, 124, 123, 122, 121, 120, \
- 119, 118, 117, 116, 115, 114, 113, 112, \
- 111, 110, 109, 108, 107, 106, 105, 104, \
- 103, 102, 101, 100, 99, 98, 97, 96, \
- 95, 94, 93, 92, 91, 90, 89, 88, \
- 87, 86, 85, 84, 83, 82, 81, 80, \
- 79, 78, 77, 76, 75, 74, 73, 72, \
- 71, 70, 69, 68, 67, 66, 65, 64, \
- 63, 62, 61, 60, 59, 58, 57, 56, \
- 55, 54, 53, 52, 51, 50, 49, 48, \
- 47, 46, 45, 44, 43, 42, 41, 40, \
- 39, 38, 37, 36, 35, 34, 33, 32, \
- 31, 30, 29, 28, 27, 26, 25, 24, \
- 23, 22, 21, 20, 19, 18, 17, 16, \
- 15, 14, 13, 12, 11, 10, 9, 8, \
- 7, 6, 5, 4, 3, 2, 1, 0
-
-#define VA_NARGS_GET_COUNT( \
- _255, _254, _253, _252, _251, _250, _249, _248, \
- _247, _246, _245, _244, _243, _242, _241, _240, \
- _239, _238, _237, _236, _235, _234, _233, _232, \
- _231, _230, _229, _228, _227, _226, _225, _224, \
- _223, _222, _221, _220, _219, _218, _217, _216, \
- _215, _214, _213, _212, _211, _210, _209, _208, \
- _207, _206, _205, _204, _203, _202, _201, _200, \
- _199, _198, _197, _196, _195, _194, _193, _192, \
- _191, _190, _189, _188, _187, _186, _185, _184, \
- _183, _182, _181, _180, _179, _178, _177, _176, \
- _175, _174, _173, _172, _171, _170, _169, _168, \
- _167, _166, _165, _164, _163, _162, _161, _160, \
- _159, _158, _157, _156, _155, _154, _153, _152, \
- _151, _150, _149, _148, _147, _146, _145, _144, \
- _143, _142, _141, _140, _139, _138, _137, _136, \
- _135, _134, _133, _132, _131, _130, _129, _128, \
- _127, _126, _125, _124, _123, _122, _121, _120, \
- _119, _118, _117, _116, _115, _114, _113, _112, \
- _111, _110, _109, _108, _107, _106, _105, _104, \
- _103, _102, _101, _100, _99, _98, _97, _96, \
- _95, _94, _93, _92, _91, _90, _89, _88, \
- _87, _86, _85, _84, _83, _82, _81, _80, \
- _79, _78, _77, _76, _75, _74, _73, _72, \
- _71, _70, _69, _68, _67, _66, _65, _64, \
- _63, _62, _61, _60, _59, _58, _57, _56, \
- _55, _54, _53, _52, _51, _50, _49, _48, \
- _47, _46, _45, _44, _43, _42, _41, _40, \
- _39, _38, _37, _36, _35, _34, _33, _32, \
- _31, _30, _29, _28, _27, _26, _25, _24, \
- _23, _22, _21, _20, _19, _18, _17, _16, \
- _15, _14, _13, _12, _11, _10, _9, _8, \
- _7, _6, _5, _4, _3, _2, _1, N, ...) N
-
-#define VA_NARGS_SHIFT_COUNT(...) VA_NARGS_GET_COUNT(__VA_ARGS__)
-
-#endif
-
-
-/* type constants */
-typedef enum
- { HS_BOOL,
- HS_INT,
- HS_NUM,
- HS_STR,
- HS_TBL,
- HS_FUNC,
- HS_CFUNC,
- HS_USER,
- HS_LIGHT,
- HS_NIL,
- HS_ANY,
- } hs_type;
-
-const char* hs_type_to_string(hs_type type);
-
-/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * hs_parse_args and hs_parse_overloaded
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
-
-struct hs_arg {
- hs_type type;
- union {
- bool *boolean;
- lua_Integer *integer;
- lua_Number *number;
- char **string;
- int *stack_index;
- lua_CFunction *function;
- void **userdata;
- } ptr;
-};
-
-#define hs_bool(x) { .type=HS_BOOL, .ptr.boolean = &(x) }
-#define hs_int(x) { .type=HS_INT, .ptr.integer = &(x) }
-#define hs_num(x) { .type=HS_NUM, .ptr.number = &(x) }
-#define hs_str(x) { .type=HS_STR, .ptr.string = &(x) }
-#define hs_tbl(x) { .type=HS_TBL, .ptr.stack_index = &(x) }
-#define hs_func(x) { .type=HS_FUNC, .ptr.stack_index = &(x) }
-#define hs_cfunc(x) { .type=HS_CFUNC, .ptr.function = &(x) }
-#define hs_user(x) { .type=HS_USER, .ptr.userdata = &(x) }
-#define hs_light(x) { .type=HS_LIGHT, .ptr.userdata = &(x) }
-#define hs_nil(x) { .type=HS_NIL, .ptr.stack_index = &(x) }
-#define hs_any(x) { .type=HS_ANY, .ptr.stack_index = &(x) }
-
-void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments);
-
-#define hs_parse_args(L, ...) \
- hs_parse_args_(L, \
- VA_NARGS(__VA_ARGS__)/2, \
- (struct hs_arg[]) { __VA_ARGS__ })
-
-
-#define hs_overload(...) VA_NARGS(__VA_ARGS__)/2, (struct hs_arg[]) { __VA_ARGS__ }
-
-int hs_parse_overloaded_(lua_State *L, ...);
-
-#define hs_parse_overloaded(L, ...) \
- hs_parse_overloaded_(L, __VA_ARGS__, -1)
-
-/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * hs_create_table
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
-
-typedef union {
- bool boolean;
- lua_Integer integer;
- lua_Number number;
- char *string;
- int stack_index;
- lua_CFunction function;
- void *userdata;
-} hs_value;
-
-struct hs_tbl_entry {
- hs_type key_type;
- hs_value key;
- hs_type value_type;
- hs_value value;
-};
-
-#define hs_bool_bool(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_bool_int(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_bool_num(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_bool_str(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_bool_tbl(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_bool_func(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_bool_cfunc(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_bool_user(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_bool_light(k, v) \
- { .key_type=HS_BOOL, .key.boolean=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_int_bool(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_int_int(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_int_num(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_int_str(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_int_tbl(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_int_func(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_int_cfunc(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_int_user(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_int_light(k, v) \
- { .key_type=HS_INT, .key.integer=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_num_bool(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_num_int(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_num_num(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_num_str(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_num_tbl(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_num_func(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_num_cfunc(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_num_user(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_num_light(k, v) \
- { .key_type=HS_NUM, .key.number=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_str_bool(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_str_int(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_str_num(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_str_str(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_str_tbl(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_str_func(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_str_cfunc(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_str_user(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_str_light(k, v) \
- { .key_type=HS_STR, .key.string=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-
-#define hs_tbl_bool(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_tbl_int(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_tbl_num(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_tbl_str(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_tbl_tbl(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_tbl_func(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_tbl_cfunc(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_tbl_user(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_tbl_light(k, v) \
- { .key_type=HS_TBL, .key.stack_index=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_func_bool(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_func_int(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_func_num(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_func_str(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_func_tbl(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_func_func(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_func_cfunc(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_func_user(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_func_light(k, v) \
- { .key_type=HS_FUNC, .key.stack_index=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_cfunc_bool(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_cfunc_int(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_cfunc_num(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_cfunc_str(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_cfunc_tbl(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_cfunc_func(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_cfunc_cfunc(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_cfunc_user(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_cfunc_light(k, v) \
- { .key_type=HS_CFUNC, .key.function=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_user_bool(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_user_int(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_user_num(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_user_str(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_user_tbl(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_user_func(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_user_cfunc(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_user_user(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_user_light(k, v) \
- { .key_type=HS_USER, .key.stack_index=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-#define hs_light_bool(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_BOOL, .value.boolean=v }
-#define hs_light_int(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_INT, .value.integer=v }
-#define hs_light_num(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_NUM, .value.number=v }
-#define hs_light_str(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_STR, .value.string=v }
-#define hs_light_tbl(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_TBL, .value.stack_index=v }
-#define hs_light_func(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_FUNC, .value.stack_index=v }
-#define hs_light_cfunc(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_CFUNC, .value.function=v }
-#define hs_light_user(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_USER, .value.stack_index=v }
-#define hs_light_light(k, v) \
- { .key_type=HS_LIGHT, .key.userdata=k, \
- .value_type=HS_LIGHT, .value.userdata=v }
-
-int hs_create_table_(lua_State *L, int n_elements, struct hs_tbl_entry *elements);
-
-#define hs_create_table(L, ...) \
- hs_create_table_(L, \
- VA_NARGS(__VA_ARGS__)/4, \
- (struct hs_tbl_entry[]) { __VA_ARGS__ })
-
-
-/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * hs_process_table
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
-
-typedef void (*hs_pt_bool_callback)(bool, void *);
-typedef void (*hs_pt_int_callback)(lua_Integer, void *);
-typedef void (*hs_pt_num_callback)(lua_Number, void *);
-typedef void (*hs_pt_str_callback)(const char *, void *);
-
-
-// default processors
-void hs_pt_set_boolean(bool value, void *data);
-void hs_pt_set_integer(lua_Integer value, void *data);
-void hs_pt_set_number(lua_Number value, void *data);
-void hs_pt_set_string(const char *value, void *data);
-
-
-struct hs_table_processor {
- const char *key;
- hs_type type;
- union {
- hs_pt_bool_callback boolean;
- hs_pt_int_callback integer;
- hs_pt_num_callback number;
- hs_pt_str_callback string;
- } func;
- void *data;
-};
-
-#define hs_process_bool(str, f, d) \
- { .key=(str), .type=HS_BOOL, .func.boolean=(f), .data=(d) }
-#define hs_process_int(str, f, d) \
- { .key=(str), .type=HS_INT, .func.integer=(f), .data=(d) }
-#define hs_process_num(str, f, d) \
- { .key=(str), .type=HS_NUM, .func.number=(f), .data=(d) }
-#define hs_process_str(str, f, d) \
- { .key=(str), .type=HS_STR, .func.string=(f), .data=(d) }
-
-void hs_process_table_(lua_State *L,
- int table_index,
- int n_processors,
- struct hs_table_processor *processors);
-
-#define hs_process_table(L, table_index, ...) \
- hs_process_table_(L, table_index, \
- VA_NARGS(__VA_ARGS__)/4, \
- (struct hs_table_processor[]) {__VA_ARGS__})
-
-
-/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * hs_pushstring
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
-
-void hs_vpushstring(lua_State *L, const char *format_string, va_list args);
-void hs_pushstring(lua_State *L, const char *format_string, ...);
-
-
-/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * error creation and handling
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
-
-void hs_throw_error(lua_State *L, const char *format_string, ...);
-int hs_traceback(lua_State *L);
-int hs_call(lua_State *L, int nargs, int nret);
-
-
-/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- *
- * registry operations
- *
- * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- */
-
-#define hs_rstore(L) luaL_ref(L, LUA_REGISTRYINDEX);
-#define hs_rload(L, ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref)
-#define hs_rdel(L, ref) luaL_unref(L, LUA_REGISTRYINDEX, ref)
-
-#endif
diff --git a/src/honeysuckle/hs_create_table.c b/src/honeysuckle/hs_create_table.c
deleted file mode 100644
index 422a480..0000000
--- a/src/honeysuckle/hs_create_table.c
+++ /dev/null
@@ -1,123 +0,0 @@
-#include <stdlib.h>
-
-#include "honeysuckle.h"
-
-static void push_value(lua_State *L, hs_type type, hs_value value)
-{
- switch (type) {
- case HS_BOOL:
- lua_pushboolean(L, value.boolean);
- break;
-
- case HS_INT:
- lua_pushinteger(L, value.integer);
- break;
-
- case HS_NUM:
- lua_pushnumber(L, value.number);
- break;
-
- case HS_STR:
- lua_pushstring(L, value.string);
- break;
-
- case HS_TBL:
- lua_pushvalue(L, value.stack_index);
- break;
-
- case HS_FUNC:
- lua_pushvalue(L, value.stack_index);
- break;
-
- case HS_CFUNC:
- lua_pushcfunction(L, value.function);
- break;
-
- case HS_USER:
- lua_pushvalue(L, value.stack_index);
- break;
-
- case HS_LIGHT:
- lua_pushlightuserdata(L, value.userdata);
- break;
-
- default:
- hs_throw_error(L, "attempted to push data of invalid type %d", type);
- break;
- }
-}
-
-
-static inline bool poppable(hs_type type)
-{
- if (type == HS_TBL ||
- type == HS_FUNC ||
- type == HS_USER) {
- return true;
- }
- return false;
-}
-
-
-static void print_stack(lua_State *L)
-{
- printf("stack: %d [", lua_gettop(L));
- for (int i=0; i<lua_gettop(L); i++) {
- printf(" %s, ", lua_typename(L, lua_type(L, i+1)));
- }
- printf("]\n");
-}
-
-
-int descending(const void *a, const void *b)
-{
- return (*(int*)b - *(int*)a);
-}
-
-int hs_create_table_(lua_State *L,
- int n_elements,
- struct hs_tbl_entry *elements)
-{
- // printf("start\n");
-
- // print_stack(L);
- lua_createtable(L, 0, n_elements);
- int index = lua_gettop(L);
- // print_stack(L);
-
- for (int i=0; i<n_elements; i++) {
- // printf("pushing element %d...\n", i);
- struct hs_tbl_entry *e = elements + i;
- push_value(L, e->key_type, e->key);
- // print_stack(L);
- push_value(L, e->value_type, e->value);
- // print_stack(L);
- lua_rawset(L, index);
- // print_stack(L);
- }
-
- int n_poppable = 0;
- int pop_indices[n_elements];
-
- for (int i=0; i<n_elements; i++) {
- struct hs_tbl_entry *e = elements + i;
- if (poppable(e->key_type))
- pop_indices[n_poppable++] = e->key.stack_index;
- if (poppable(e->value_type))
- pop_indices[n_poppable++] = e->value.stack_index;
- }
-
- // printf("cleaning up\n");
-
- qsort(pop_indices, n_poppable, sizeof(int), descending);
- for (int i=0; i<n_poppable; i++) {
- lua_remove(L, pop_indices[i]);
- index -= 1;
- // print_stack(L);
- }
-
-
- // printf("exit\n");
-
- return index;
-}
diff --git a/src/honeysuckle/hs_parse_args.c b/src/honeysuckle/hs_parse_args.c
deleted file mode 100644
index d6d3b3c..0000000
--- a/src/honeysuckle/hs_parse_args.c
+++ /dev/null
@@ -1,149 +0,0 @@
-#include <stdio.h>
-
-#include "honeysuckle.h"
-
-static bool check_parse(lua_State *L, int index, struct hs_arg *expected)
-{
- switch(expected->type) {
- case HS_BOOL:
- if (!lua_isboolean(L, index))
- return false;
- *(expected->ptr.boolean) = lua_toboolean(L, index);
- return true;
-
- case HS_INT:
- if (!lua_isnumber(L, index))
- return false;
- *(expected->ptr.integer) = lua_tointeger(L, index);
- return true;
-
- case HS_NUM:
- if (!lua_isnumber(L, index))
- return false;
- *(expected->ptr.number) = lua_tonumber(L, index);
- return true;
-
- case HS_STR:
- if (!lua_isstring(L, index) || lua_isnumber(L, index))
- return false;
- *(expected->ptr.string) = (char *) lua_tostring(L, index);
- return true;
-
- case HS_TBL:
- if (!lua_istable(L, index))
- return false;
- *(expected->ptr.stack_index) = index;
- return true;
-
- case HS_FUNC:
- if (!lua_isfunction(L, index))
- return false;
- *(expected->ptr.stack_index) = index;
- return true;
-
- case HS_CFUNC:
- if (!lua_iscfunction(L, index))
- return false;
- *(expected->ptr.function) = lua_tocfunction(L, index);
- return true;
-
- case HS_USER:
- if (!lua_isuserdata(L, index))
- return false;
- *(expected->ptr.userdata) = lua_touserdata(L, index);
- return true;
-
- case HS_LIGHT:
- if (!lua_islightuserdata(L, index))
- return false;
- *(expected->ptr.userdata) = lua_touserdata(L, index);
- return true;
-
- case HS_NIL:
- if (!lua_isnil(L, index))
- return false;
- *(expected->ptr.stack_index) = index;
-
- case HS_ANY:
- *(expected->ptr.stack_index) = index;
- return true;
-
- default:
- return false;
- }
-}
-
-
-static bool try_parse_args(lua_State *L, int n_args, struct hs_arg *arguments)
-{
- int args_provided = lua_gettop(L);
- if (args_provided != n_args)
- return false;
-
- for (int i=0; i<n_args; i++) {
- bool success = check_parse(L, i+1, arguments + i);
- if (!success)
- return false;
- }
- return true;
-}
-
-
-void hs_parse_args_(lua_State *L, int n_args, struct hs_arg *arguments)
-{
- bool success = try_parse_args(L, n_args, arguments);
- if (!success) {
- lua_pushstring(L, "expected arguments of the form (");
- for (int i=0; i<n_args; i++) {
- lua_pushstring(L, hs_type_to_string(arguments[i].type));
- lua_pushstring(L, ", ");
- }
- lua_pop(L, 1);
- lua_pushstring(L, "); received (");
- const int n_provided = lua_gettop(L);
- for (int i=0; i<n_provided; i++) {
- lua_pushstring(L, lua_typename(L, lua_type(L, i+1)));
- lua_pushstring(L, ", ");
- }
- lua_pop(L, 1);
- lua_pushstring(L, ") instead");
- lua_concat(L, 1 + (2*n_args) + (2*n_provided));
- lua_error(L);
- }
-}
-
-
-// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-static void hs_overloaded_error(lua_State *L, va_list args)
-{
- lua_pushstring(L, "hs_overloaded failed");
- lua_error(L);
-}
-
-
-int hs_parse_overloaded_(lua_State *L, ...)
-{
- va_list args, args_error;
- va_start(args, L);
- va_copy(args_error, args);
-
- int choice = 0;
-
- while(true) {
- int n_args = va_arg(args, int);
- if (n_args == -1)
- break;
- else {
- struct hs_arg *arguments = va_arg(args, struct hs_arg *);
- if (try_parse_args(L, n_args, arguments))
- return choice;
- }
- choice++;
- }
-
- hs_overloaded_error(L, args_error);
-
- va_end(args);
- va_end(args_error);
-}
diff --git a/src/honeysuckle/hs_process_table.c b/src/honeysuckle/hs_process_table.c
deleted file mode 100644
index d07d815..0000000
--- a/src/honeysuckle/hs_process_table.c
+++ /dev/null
@@ -1,86 +0,0 @@
-#include "honeysuckle.h"
-
-void hs_pt_set_boolean(bool value, void *data)
-{
- *(bool *) data = value;
-}
-
-void hs_pt_set_integer(lua_Integer value, void *data)
-{
- *(lua_Integer *) data = value;
-}
-
-void hs_pt_set_number(lua_Number value, void *data)
-{
- *(lua_Number *) data = value;
-}
-
-void hs_pt_set_string(const char *value, void *data)
-{
- *(const char **) data = value;
-}
-
-
-static bool process_key(lua_State *L, struct hs_table_processor *p)
-{
- switch (p->type) {
- case HS_BOOL:
- if (!lua_isboolean(L, -1))
- hs_throw_error(L,
- "expected key '%s' to be of type boolean, "\
- "but got type %s instead",
- p->key,
- lua_typename(L, lua_type(L, -1)));
- p->func.boolean(lua_toboolean(L, -1), p->data);
- break;
-
- case HS_INT:
- if (!lua_isnumber(L, -1))
- hs_throw_error(L,
- "expected key '%s' to be of type integer, "\
- "but got type %s instead",
- p->key,
- lua_typename(L, lua_type(L, -1)));
- p->func.integer(lua_tointeger(L, -1), p->data);
- break;
-
- case HS_NUM:
- if (!lua_isnumber(L, -1))
- hs_throw_error(L,
- "expected key '%s' to be of type number, "\
- "but got type %s instead",
- p->key,
- lua_typename(L, lua_type(L, -1)));
- p->func.number(lua_tonumber(L, -1), p->data);
- break;
-
- case HS_STR:
- if (!lua_isstring(L, -1))
- hs_throw_error(L,
- "expected key '%s' to be of type string, "\
- "but got type %s instead",
- p->key,
- lua_typename(L, lua_type(L, -1)));
- p->func.string(lua_tostring(L, -1), p->data);
- break;
-
- default:
- // bad type value, throw error
- hs_throw_error(L, "bad expected type '%d' for key '%s'",
- p->type, p->key);
- break;
- }
-}
-
-void hs_process_table_(lua_State *L,
- int table_index,
- int n_processors,
- struct hs_table_processor *processors)
-{
- for (int i=0; i<n_processors; i++) {
- lua_getfield(L, table_index, processors[i].key);
- if (!lua_isnil(L, -1))
- process_key(L, processors+i);
- lua_pop(L, 1);
- }
-}
diff --git a/src/honeysuckle/hs_pushstring.c b/src/honeysuckle/hs_pushstring.c
deleted file mode 100644
index f7da0d0..0000000
--- a/src/honeysuckle/hs_pushstring.c
+++ /dev/null
@@ -1,31 +0,0 @@
-#include <stdlib.h>
-
-#include "honeysuckle.h"
-
-void hs_vpushstring(lua_State *L, const char *format_string, va_list args)
-{
- va_list args_;
- va_copy(args_, args);
-
- int string_size = vsnprintf(NULL, 0, format_string, args_);
- va_end(args_);
-
- char *string = malloc((string_size+1) * sizeof(char));
- if (string == NULL) {
- lua_pushstring(L, "there was an error allocating memory for a string");
- lua_error(L);
- }
-
- vsnprintf(string, string_size+1, format_string, args);
- lua_pushstring(L, string);
- free(string);
-}
-
-
-void hs_pushstring(lua_State *L, const char *format_string, ...)
-{
- va_list args;
- va_start(args, format_string);
- hs_vpushstring(L, format_string, args);
- va_end(args);
-}
diff --git a/src/honeysuckle/hs_throw_error.c b/src/honeysuckle/hs_throw_error.c
deleted file mode 100644
index 20a4c6d..0000000
--- a/src/honeysuckle/hs_throw_error.c
+++ /dev/null
@@ -1,12 +0,0 @@
-#include <stdlib.h>
-
-#include "honeysuckle.h"
-
-void hs_throw_error(lua_State *L, const char *format_string, ...)
-{
- va_list args;
- va_start(args, format_string);
- hs_vpushstring(L, format_string, args);
- va_end(args);
- lua_error(L);
-}
diff --git a/src/honeysuckle/hs_traceback.c b/src/honeysuckle/hs_traceback.c
deleted file mode 100644
index 1830721..0000000
--- a/src/honeysuckle/hs_traceback.c
+++ /dev/null
@@ -1,37 +0,0 @@
-#include "honeysuckle.h"
-
-int hs_traceback(lua_State *L)
-{
- if (!lua_isstring(L, 1))
- /* 'message' is not a string, keep intact */
- return 1;
-
- lua_getglobal(L, "debug");
- if (!lua_istable(L, -1)) {
- lua_pop(L, 1);
- return 1;
- }
-
- lua_getfield(L, -1, "traceback");
- if (!lua_isfunction(L, -1)) {
- lua_pop(L, 2);
- return 1;
- }
-
- lua_pushvalue(L, 1);
- lua_pushinteger(L, 2);
- lua_call(L, 2, 1);
- return 1;
-}
-
-
-int hs_call(lua_State *L, int nargs, int nret)
-{
- int traceback_pos = lua_gettop(L) - nargs;
- lua_pushcfunction(L, hs_traceback);
- lua_insert(L, traceback_pos);
-
- int result = lua_pcall(L, nargs, nret, traceback_pos);
- lua_remove(L, traceback_pos);
- return result;
-}
diff --git a/src/honeysuckle/hs_type_to_string.c b/src/honeysuckle/hs_type_to_string.c
deleted file mode 100644
index d0a580c..0000000
--- a/src/honeysuckle/hs_type_to_string.c
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "honeysuckle.h"
-
-const char * hs_type_to_string(hs_type type)
-{
- switch(type) {
- case HS_BOOL:
- return "boolean";
- case HS_INT:
- return "integer";
- case HS_NUM:
- return "number";
- case HS_STR:
- return "string";
- case HS_TBL:
- return "table";
- case HS_FUNC:
- return "function";
- case HS_CFUNC:
- return "C function";
- case HS_USER:
- return "userdata";
- case HS_LIGHT:
- return "light userdata";
- case HS_NIL:
- return "nil";
- case HS_ANY:
- return "any";
- default:
- return "(unknown)";
- }
-}
diff --git a/src/options.c b/src/options.c
index fa6b999..61a74b6 100644
--- a/src/options.c
+++ b/src/options.c
@@ -1,18 +1,51 @@
#include <stdio.h>
-#include <unistd.h>
+#include "cargs/cargs.h"
#include "options.h"
#include "logging.h"
+static struct cag_option options[] = {
+ {
+ .identifier = 'q',
+ .access_letters = "q",
+ .access_name = NULL,
+ .value_name = NULL,
+ .description = "Decrease output verbosity (-qqq suppresses even fatal errors)",
+ },
+ {
+ .identifier = 'v',
+ .access_letters = "v",
+ .access_name = NULL,
+ .value_name = NULL,
+ .description = "Increase output verbosity (-vvv displays every log message)",
+ },
+ {
+ .identifier = 'c',
+ .access_letters = "c",
+ .access_name = "config",
+ .value_name = "CONF_FILE",
+ .description = "Specify configuration file to read (default: config.lua)",
+ },
+ {
+ .identifier = 's',
+ .access_letters = "s",
+ .access_name = "script",
+ .value_name = "SCRIPT_FILE",
+ .description = "Override the built-in build script",
+ },
+ {
+ .identifier = 'h',
+ .access_letters = "h",
+ .access_name = "help",
+ .value_name = NULL,
+ .description = "Print this help message and exit",
+ },
+};
+
static void print_usage(const char *progname)
{
- printf("Usage: %s [-c config_file] [-v[v[v]]] [-q[q[q]]] [-h] [-s script]\n"
- " -v Increase output verbosity (-vvv displays every log message)\n"
- " -q Decrease output verbosity (-qqq suppresses even fatal errors)\n"
- " -c Specify configuration file to read (default 'config.lua')\n"
- " -s Override the built-in Lua script\n"
- " -h Print this help message and exit\n",
- progname);
+ printf("Usage: %s [OPTIONS]\n", progname);
+ cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
}
int parse_options(struct argent_options *opts, int argc, char **argv)
@@ -21,32 +54,35 @@ int parse_options(struct argent_options *opts, int argc, char **argv)
opts->conf_filename = "config.lua";
opts->script_filename = NULL;
- int opt;
- while ((opt = getopt(argc, argv, "hqvc:s:")) != -1) {
+ char opt;
+ cag_option_context context;
+ cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
+ while (cag_option_fetch(&context)) {
+ opt = cag_option_get(&context);
switch (opt) {
case 'q':
- opts->log_level -= 1;
- break;
-
+ opts->log_level -= 1;
+ break;
+
case 'v':
- opts->log_level += 1;
- break;
-
+ opts->log_level += 1;
+ break;
+
case 'c':
- opts->conf_filename = optarg;
- break;
+ opts->conf_filename = cag_option_get_value(&context);
+ break;
case 's':
- opts->script_filename = optarg;
- break;
+ opts->script_filename = cag_option_get_value(&context);
+ break;
case 'h':
- print_usage(argv[0]);
- return 2;
+ print_usage(argv[0]);
+ return 2;
default:
- print_usage(argv[0]);
- return 1;
+ print_usage(argv[0]);
+ return 1;
}
}