summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Drake <tlsa@netsurf-browser.org>2022-02-26 14:42:30 +0000
committerMichael Drake <tlsa@netsurf-browser.org>2022-02-26 16:19:51 +0000
commitfe85229b8eb2ec6a160e8dd8bf44a3d563b53c1e (patch)
treebfbdde050bfebfccbe35f0c8a648a6b049d3a7b4
parent370c2a783b67350143c18a07463835887d2b1847 (diff)
downloadlibnsgif-fe85229b8eb2ec6a160e8dd8bf44a3d563b53c1e.tar.gz
libnsgif-fe85229b8eb2ec6a160e8dd8bf44a3d563b53c1e.tar.bz2
Test: Add support for gif info dump and loop count to nsgif tool.
-rw-r--r--test/cli.c763
-rw-r--r--test/cli.h94
-rw-r--r--test/nsgif.c162
-rwxr-xr-xtest/runtest.sh2
4 files changed, 980 insertions, 41 deletions
diff --git a/test/cli.c b/test/cli.c
new file mode 100644
index 0000000..031cd97
--- /dev/null
+++ b/test/cli.c
@@ -0,0 +1,763 @@
+/*
+ * SPDX-License-Identifier: ISC
+ *
+ * Copyright (C) 2021 Michael Drake <tlsa@netsurf-browser.org>
+ */
+
+/**
+ * \file
+ * \brief Command line argument handling.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cli.h"
+
+/**
+ * Check whether a CLI argument type should have a numerical value.
+ *
+ * \param[in] type An argument type.
+ * \return true if the argument needs a numerical value, or false otherwise.
+ */
+static inline bool cli__arg_is_numerical(enum cli_arg_type type)
+{
+ return (type != CLI_STRING && type != CLI_BOOL);
+}
+
+/**
+ * Parse a signed integer value from an argument.
+ *
+ * \param[in] str String containing value to parse.
+ * \param[out] i Pointer to place to store parsed value.
+ * \param[in,out] pos Current position in str, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_value_int(
+ const char *str,
+ int64_t *i,
+ size_t *pos)
+{
+ long long temp;
+ char *end = NULL;
+
+ str += *pos;
+ errno = 0;
+ temp = strtoll(str, &end, 0);
+
+ if (end == str || errno == ERANGE ||
+ temp > INT64_MAX || temp < INT64_MIN) {
+ fprintf(stderr, "Failed to parse integer from '%s'\n", str);
+ return false;
+ }
+
+ *i = (int64_t)temp;
+ *pos += (size_t)(end - str);
+ return true;
+}
+
+/**
+ * Parse an unsigned integer value from an argument.
+ *
+ * \param[in] str String containing value to parse.
+ * \param[out] u Pointer to place to store parsed value.
+ * \param[in,out] pos Current position in str, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_value_uint(
+ const char *str,
+ uint64_t *u,
+ size_t *pos)
+{
+ unsigned long long temp;
+ char *end = NULL;
+
+ str += *pos;
+ errno = 0;
+ temp = strtoull(str, &end, 0);
+
+ if (end == str || errno == ERANGE || temp > UINT64_MAX) {
+ fprintf(stderr, "Failed to parse unsigned from '%s'\n", str);
+ return false;
+ }
+
+ *u = (uint64_t)temp;
+ *pos += (size_t)(end - str);
+ return true;
+}
+
+/**
+ * Parse an enum value from an argument.
+ *
+ * \param[in] str String containing value to parse.
+ * \param[out] e Enum details.
+ * \param[in,out] pos Current position in str, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_value_enum(
+ const char *str,
+ const struct cli_enum *e,
+ size_t *pos)
+{
+ str += *pos;
+ *pos += strlen(str);
+
+ for (const struct cli_str_val *sv = e->desc; sv->str != NULL; sv++) {
+ if (strcmp(str, sv->str) == 0) {
+ *e->e = sv->val;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Parse a string value from an argument.
+ *
+ * \param[in] str String containing value to parse.
+ * \param[out] s Pointer to place to store parsed value.
+ * \param[in,out] pos Current position in str, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_value_string(
+ const char *str,
+ const char **s,
+ size_t *pos)
+{
+ *s = str + *pos;
+ *pos += strlen(*s);
+ return true;
+}
+
+/**
+ * Parse a value from an argument.
+ *
+ * \param[in] entry Client command line interface argument specification.
+ * \param[in] arg Argument to parse a value from.
+ * \param[in,out] pos Current position in argument, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_value(
+ const struct cli_table_entry *entry,
+ const char *arg,
+ size_t *pos)
+{
+ switch (entry->t) {
+ case CLI_CMD:
+ if (strcmp(arg + *pos, entry->l) == 0) {
+ *pos += strlen(arg);
+ return true;
+ }
+ return false;
+
+ case CLI_INT:
+ return cli__parse_value_int(arg, entry->v.i, pos);
+
+ case CLI_UINT:
+ return cli__parse_value_uint(arg, entry->v.u, pos);
+
+ case CLI_ENUM:
+ return cli__parse_value_enum(arg, &entry->v.e, pos);
+
+ case CLI_STRING:
+ return cli__parse_value_string(arg, entry->v.s, pos);
+
+ default:
+ fprintf(stderr, "Unexpected value for '%s': %s\n",
+ entry->l, arg);
+ break;
+ }
+
+ return false;
+}
+
+/**
+ * Parse a value from an argument.
+ *
+ * \param[in] entry Client command line interface argument specification.
+ * \param[in] argc Number of command line arguments.
+ * \param[in] argv String vector containing command line arguments.
+ * \param[in] arg_pos Current position in argv.
+ * \param[in,out] pos Current pos in current argument, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_argv_value(const struct cli_table_entry *entry,
+ int argc, const char **argv,
+ int arg_pos, size_t *pos)
+{
+ const char *arg = argv[arg_pos];
+
+ if (arg_pos >= argc) {
+ fprintf(stderr, "Value not given for '%s'\n", entry->l);
+ return false;
+ }
+
+ return cli__parse_value(entry, arg, pos);
+}
+
+/**
+ * Check whether a CLI argument is a positional value.
+ *
+ * \param[in] entry Client command line interface argument specification.
+ * \return true if the argument is positional, or false otherwise.
+ */
+static inline bool cli__entry_is_positional(const struct cli_table_entry *entry)
+{
+ return entry->p;
+}
+
+/**
+ * Look up a short argument flag.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] s Argument flag to look up in client CLI spec.
+ * \return Client CLI spec entry on success, or NULL otherwise.
+ */
+static const struct cli_table_entry *cli__lookup_short(
+ const struct cli_table *cli, char s)
+{
+ for (size_t i = 0; i < cli->count; i++) {
+ if (cli__entry_is_positional(&cli->entries[i])) {
+ continue;
+ }
+ if (cli->entries[i].s == s) {
+ return &cli->entries[i];
+ }
+ }
+
+ fprintf(stderr, "Unknown flag: '%c'\n", s);
+ return NULL;
+}
+
+/**
+ * Handle an argument with a type that requires a value.
+ *
+ * This can handle the value being in the current argument, optionally split by
+ * a separator, or in the next argument.
+ *
+ * \param[in] entry Client command line interface argument specification.
+ * \param[in] argc Number of command line arguments.
+ * \param[in] argv String vector containing command line arguments.
+ * \param[in,out] arg_pos Current position in argv, updated on exit.
+ * \param[in] pos Current position in current argument string.
+ * \param[in] sep Name/value separator character, or '\0' if none.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__handle_arg_value(const struct cli_table_entry *entry,
+ int argc, const char **argv, int *arg_pos, size_t pos, char sep)
+{
+ const char *arg = argv[*arg_pos];
+ size_t orig_pos;
+ bool ret;
+
+ if (arg[pos] == '\0') {
+ (*arg_pos)++;
+ pos = 0;
+ } else if (arg[pos] == sep) {
+ pos++;
+ } else if (cli__arg_is_numerical(entry->t) == false) {
+ fprintf(stderr, "Separator required for non-numerical value\n");
+ return false;
+ }
+
+ if (isspace(argv[*arg_pos][pos])) {
+ fprintf(stderr, "Unexpected white space in '%s' "
+ "for argument '%s'\n",
+ &argv[*arg_pos][pos], entry->l);
+ return false;
+ }
+
+ orig_pos = pos;
+ ret = cli__parse_argv_value(entry, argc, argv, *arg_pos, &pos);
+ if (ret != true) {
+ return ret;
+ }
+
+ if (argv[*arg_pos][pos] != '\0') {
+ fprintf(stderr, "Invalid value '%s' for argument '%s'\n",
+ &argv[*arg_pos][orig_pos], entry->l);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Parse a flags argument.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] argc Number of command line arguments.
+ * \param[in] argv String vector containing command line arguments.
+ * \param[out] arg_pos Current position in argv, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_short(const struct cli_table *cli,
+ int argc, const char **argv, int *arg_pos)
+{
+ const char *arg = argv[*arg_pos];
+ size_t pos = 1;
+
+ if (arg[0] != '-') {
+ return false;
+ }
+
+ while (arg[pos] != '\0') {
+ const struct cli_table_entry *entry;
+
+ entry = cli__lookup_short(cli, arg[pos]);
+ if (entry == NULL) {
+ return false;
+ }
+
+ if (entry->t == CLI_BOOL) {
+ *entry->v.b = true;
+ } else {
+ return cli__handle_arg_value(entry, argc, argv,
+ arg_pos, pos + 1, '\0');
+ }
+
+ pos++;
+ }
+
+ return true;
+}
+
+/**
+ * Look up a long argument name.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] arg Argument name to look up in cli spec.
+ * \param[in,out] pos Current position in arg, updated on exit.
+ * \return Client CLI spec entry on success, or NULL otherwise.
+ */
+static const struct cli_table_entry *cli__lookup_long(
+ const struct cli_table *cli,
+ const char *arg,
+ size_t *pos)
+{
+ arg += *pos;
+
+ for (size_t i = 0; i < cli->count; i++) {
+ if (cli__entry_is_positional(&cli->entries[i]) == false) {
+ const char *name = cli->entries[i].l;
+ size_t name_len = strlen(cli->entries[i].l);
+
+ if (strncmp(name, arg, name_len) == 0) {
+ if (arg[name_len] != '\0' &&
+ arg[name_len] != '=') {
+ continue;
+ }
+ *pos += name_len;
+ return &cli->entries[i];
+ }
+ }
+ }
+
+ fprintf(stderr, "Unknown argument: '%s'\n", arg);
+ return NULL;
+}
+
+/**
+ * Parse a long argument.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] argc Number of command line arguments.
+ * \param[in] argv String vector containing command line arguments.
+ * \param[out] arg_pos Current position in argv, updated on exit.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_long(const struct cli_table *cli,
+ int argc, const char **argv, int *arg_pos)
+{
+ const struct cli_table_entry *entry;
+ const char *arg = argv[*arg_pos];
+ size_t pos = 2;
+
+ if (arg[0] != '-' ||
+ arg[1] != '-') {
+ return false;
+ }
+
+ entry = cli__lookup_long(cli, arg, &pos);
+ if (entry == NULL) {
+ return false;
+ }
+
+ if (entry->t == CLI_BOOL) {
+ if (arg[pos] != '\0') {
+ fprintf(stderr, "Unexpected value for argument '%s'\n",
+ arg);
+ return false;
+ }
+ *entry->v.b = true;
+ } else {
+ bool ret;
+
+ ret = cli__handle_arg_value(entry, argc, argv,
+ arg_pos, pos, '=');
+ if (ret != true) {
+ return ret;
+ }
+ }
+
+ return true;
+}
+
+/**
+ * Parse a positional argument according to the given CLI spec entry.
+ *
+ * \param[in] entry Client command line interface argument specification.
+ * \param[in] arg Argument to parse.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_positional_entry(
+ const struct cli_table_entry *entry,
+ const char *arg)
+{
+ size_t pos = 0;
+ bool ret;
+
+ ret = cli__parse_value(entry, arg, &pos);
+ if (ret != true) {
+ return ret;
+ } else if (arg[pos] != '\0') {
+ fprintf(stderr, "Failed to parse value '%s' for arg '%s'\n",
+ arg, entry->l);
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Parse a positional argument.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] arg Argument to parse.
+ * \param[in] count Number of positional arguments parsed already.
+ * \return true on success, or false otherwise.
+ */
+static bool cli__parse_positional(const struct cli_table *cli,
+ const char *arg, size_t count)
+{
+ size_t positional = 0;
+
+ for (size_t i = 0; i < cli->count; i++) {
+ if (cli__entry_is_positional(&cli->entries[i])) {
+ if (positional == count) {
+ return cli__parse_positional_entry(
+ &cli->entries[i], arg);
+ }
+
+ positional++;
+ }
+ }
+
+ fprintf(stderr, "Unexpected positional argument: '%s'\n", arg);
+ return false;
+}
+
+/**
+ * Get the string to indicate type of value expected for an argument.
+ *
+ * \param[in] type The argument type.
+ * \return String for value type.
+ */
+static const char *cli__string_from_type(enum cli_arg_type type)
+{
+ static const char *const strings[] = {
+ [CLI_BOOL] = "",
+ [CLI_INT] = "INT",
+ [CLI_UINT] = "UINT",
+ [CLI_ENUM] = "ENUM",
+ [CLI_STRING] = "STRING",
+ };
+
+ if (type >= CLI_ARRAY_LEN(strings) || strings[type] == NULL) {
+ return "";
+ }
+
+ return strings[type];
+}
+
+/**
+ * Helper to update a maximum adjusted string length if new values is greater.
+ *
+ * \param[in] str String to check.
+ * \param[in] adjustment Amount to modify length of string by (bytes).
+ * \param[out] len Returns the maximum of existing and this length.
+ */
+static void cli__max_len(const char *str, size_t adjustment, size_t *len)
+{
+ size_t str_len = strlen(str) + adjustment;
+
+ if (str_len > *len) {
+ *len = str_len;
+ }
+}
+
+/**
+ * Count up various properties of the client CLI interface specification.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[out] count Returns number of non-positional arguments.
+ * \param[out] pcount Returns number of positional arguments.
+ * \param[out] max_len Returns max string length of non-positional arguments.
+ * \param[out] pmax_len Returns max string length of positional arguments.
+ * \param[out] phas_desc Returns number of positional args with descriptions.
+ */
+static void cli__count(const struct cli_table *cli,
+ size_t *count,
+ size_t *pcount,
+ size_t *max_len,
+ size_t *pmax_len,
+ size_t *phas_desc)
+{
+ if (count != NULL) *count = 0;
+ if (pcount != NULL) *pcount = 0;
+ if (max_len != NULL) *max_len = 0;
+ if (pmax_len != NULL) *pmax_len = 0;
+ if (phas_desc != NULL) *phas_desc = 0;
+
+ for (size_t i = 0; i < cli->count; i++) {
+ const struct cli_table_entry *entry = &cli->entries[i];
+
+ if (cli__entry_is_positional(entry)) {
+ if (pcount != NULL) {
+ (*pcount)++;
+ }
+ if (pmax_len != NULL) {
+ cli__max_len(entry->l, 0, pmax_len);
+ }
+ if (phas_desc != NULL) {
+ (*phas_desc)++;
+ }
+ } else {
+ if (count != NULL) {
+ (*count)++;
+ }
+ if (max_len != NULL) {
+ const char *type_str;
+ size_t type_len;
+
+ type_str = cli__string_from_type(entry->t);
+ type_len = strlen(type_str);
+
+ cli__max_len(entry->l, type_len, max_len);
+ }
+ }
+ }
+}
+
+static inline bool cli__is_negative(const char *arg)
+{
+ int64_t i;
+ size_t pos = 0;
+
+ return cli__parse_value_int(arg, &i, &pos)
+ && pos == strlen(arg)
+ && i < 0;
+}
+
+/* Documented in cli.h */
+bool cli_parse(const struct cli_table *cli, int argc, const char **argv)
+{
+ size_t pos_count = 0;
+ enum {
+ ARG_PROG_NAME,
+ ARG_FIRST,
+ };
+
+ for (int i = ARG_FIRST; i < argc; i++) {
+ const char *arg = argv[i];
+ size_t pos_inc = 0;
+ bool ret;
+
+ if (arg[0] == '-') {
+ if (arg[1] == '-') {
+ ret = cli__parse_long(cli, argc, argv, &i);
+ } else {
+ ret = cli__parse_short(cli, argc, argv, &i);
+ if (ret != true) {
+ if (cli__is_negative(argv[i])) {
+ pos_inc = 1;
+ ret = cli__parse_positional(
+ cli, argv[i],
+ pos_count);
+ }
+ }
+ }
+ } else {
+ pos_inc = 1;
+ ret = cli__parse_positional(cli, argv[i], pos_count);
+ }
+
+ if (ret != true) {
+ return ret;
+ }
+
+ pos_count += pos_inc;
+ }
+
+ if (pos_count < cli->min_positional) {
+ fprintf(stderr, "Insufficient positional arguments found.\n");
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * Get terminal width.
+ *
+ * \return terminal width in characters.
+ */
+static size_t cli__terminal_width(void)
+{
+ return 80;
+}
+
+/**
+ * Print an entry's description, with a given indent.
+ *
+ * The indent is assumed to already be applied for the first line of the
+ * output by the caller.
+ *
+ * \param[in] entry The entry to print the description for.
+ * \param[in] indent The number of spaces to pad the left margin with.
+ */
+static void cli__print_description(const struct cli_table_entry *entry,
+ size_t indent)
+{
+ size_t terminal_width = cli__terminal_width();
+ size_t avail = (indent > terminal_width) ? 0 : terminal_width - indent;
+ size_t space = avail;
+ const char *desc = entry->d;
+
+ if (desc != NULL) {
+ while (*desc != '\0') {
+ size_t word_len = strcspn(desc, " \n\t");
+ if (word_len <= space || space == avail) {
+ fprintf(stderr, "%*.*s",
+ (int)word_len,
+ (int)word_len, desc);
+ desc += word_len;
+ if (word_len <= space) {
+ space -= word_len;
+ }
+ if (space > 0) {
+ fprintf(stderr, " ");
+ space--;
+ }
+ } else {
+ fprintf(stderr, "\n%*s", (int)indent, "");
+ space = avail;
+ }
+ desc += strspn(desc, " \n\t");
+ }
+ }
+
+ fprintf(stderr, "\n");
+}
+
+/* Documented in cli.h */
+void cli_help(const struct cli_table *cli, const char *prog_name)
+{
+ size_t count;
+ size_t pcount;
+ size_t max_len;
+ size_t pmax_len;
+ size_t phas_desc;
+ size_t required = 0;
+ enum {
+ ARG_PROG_NAME,
+ };
+
+ cli__count(cli, &count, &pcount, &max_len, &pmax_len, &phas_desc);
+
+ fprintf(stderr, "\nUsage: %s", prog_name);
+
+ if (pcount > 0) {
+ for (size_t i = 0; i < cli->count; i++) {
+ if (cli__entry_is_positional(&cli->entries[i])) {
+ const char *punctuation =
+ (required == cli->min_positional) ?
+ " [" : " ";
+
+ if (cli->entries[i].t == CLI_CMD) {
+ fprintf(stderr, "%s%s", punctuation,
+ cli->entries[i].l);
+ } else {
+ fprintf(stderr, "%s<%s>", punctuation,
+ cli->entries[i].l);
+ }
+ required++;
+ }
+ }
+ if (required == pcount && required > cli->min_positional) {
+ fprintf(stderr, "]");
+ }
+ }
+
+ if (count > 0) {
+ fprintf(stderr, " [options]");
+ }
+
+ fprintf(stderr, "\n\n");
+
+ if (phas_desc > 0) {
+ fprintf(stderr, "Where:\n\n");
+
+ for (size_t i = 0; i < cli->count; i++) {
+ const struct cli_table_entry *entry = &cli->entries[i];
+
+ if (entry->d == NULL) {
+ continue;
+ }
+
+ if (cli__entry_is_positional(entry)) {
+ fprintf(stderr, " %*.*s ",
+ (int)pmax_len,
+ (int)pmax_len,
+ entry->l);
+ cli__print_description(entry, pmax_len + 4);
+ fprintf(stderr, "\n");
+ }
+ }
+ }
+
+ if (count > 0) {
+ fprintf(stderr, "Options:\n\n");
+
+ for (size_t i = 0; i < cli->count; i++) {
+ const struct cli_table_entry *entry = &cli->entries[i];
+ const char *type_str;
+ size_t type_len;
+ size_t arg_len;
+
+ if (cli__entry_is_positional(entry)) {
+ continue;
+ }
+
+ if (entry->s != '\0') {
+ fprintf(stderr, " -%c", entry->s);
+ } else {
+ fprintf(stderr, " ");
+ }
+
+ type_str = cli__string_from_type(entry->t);
+ type_len = strlen(type_str);
+ arg_len = strlen(entry->l);
+
+ fprintf(stderr, " --%s %s%*.s ", entry->l, type_str,
+ (int)(max_len - arg_len - type_len),
+ "");
+ cli__print_description(entry, max_len + 11);
+ fprintf(stderr, "\n");
+ }
+ }
+}
diff --git a/test/cli.h b/test/cli.h
new file mode 100644
index 0000000..91db086
--- /dev/null
+++ b/test/cli.h
@@ -0,0 +1,94 @@
+/*
+ * SPDX-License-Identifier: ISC
+ *
+ * Copyright (C) 2021 Michael Drake <tlsa@netsurf-browser.org>
+ */
+
+/**
+ * \file
+ * \brief Command line argument handling API.
+ */
+
+#ifndef _PELTAR_CLI_H_
+#define _PELTAR_CLI_H_
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/**
+ * Helper to get element count for an array,
+ *
+ * \param[in] _a Array to get number of elements for.
+ */
+#define CLI_ARRAY_LEN(_a) ((sizeof(_a))/(sizeof(*(_a))))
+
+/**
+ * CLI argument type.
+ */
+enum cli_arg_type {
+ CLI_CMD, /**< A sub-command. Must match long argument name. */
+ CLI_BOOL, /**< Has no value; presence of flag indicates true. */
+ CLI_INT, /**< Has signed integer value. */
+ CLI_UINT, /**< Has unsigned integer value. */
+ CLI_ENUM, /**< Has enumeration value. */
+ CLI_STRING, /**< Has string value. */
+};
+
+struct cli_str_val {
+ const char *str;
+ int64_t val;
+};
+
+struct cli_enum {
+ const struct cli_str_val *desc;
+ int64_t *e; /**< Location to store \ref CLI_ENUM value. */
+};
+
+/**
+ * Client description for a command line argument.
+ */
+struct cli_table_entry {
+ const char *l; /**< Long argument name. */
+ const char s; /**< Short flag name. (Non-positional arguments.) */
+ bool p; /**< Whether the argument is a positional argument. */
+ enum cli_arg_type t; /**< Argument type. */
+ union {
+ bool *b; /**< Location to store \ref CLI_BOOL value. */
+ int64_t *i; /**< Location to store \ref CLI_INT value. */
+ uint64_t *u; /**< Location to store \ref CLI_UINT value. */
+ const char **s; /**< Location to store \ref CLI_STRING value. */
+ struct cli_enum e;
+ } v; /**< Where to store type-specific values. */
+ const char *d; /**< Description. */
+};
+
+/**
+ * Client command line interface specification.
+ */
+struct cli_table {
+ const struct cli_table_entry *entries;
+ size_t count;
+ size_t min_positional;
+};
+
+/**
+ * Parse the command line arguments.
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] argc Number of command line arguments.
+ * \param[in] argv String vector containing command line arguments.
+ * \return true on success, false on error.
+ */
+bool cli_parse(const struct cli_table *cli, int argc, const char **argv);
+
+/**
+ * Print usage and help output.
+ *
+ * Note: Assumes non-Unicode. (One byte per character.)
+ *
+ * \param[in] cli Client command line interface specification.
+ * \param[in] prog_name Program name.
+ */
+void cli_help(const struct cli_table *cli, const char *prog_name);
+
+#endif
diff --git a/test/nsgif.c b/test/nsgif.c
index 173f70c..94e7960 100644
--- a/test/nsgif.c
+++ b/test/nsgif.c
@@ -17,8 +17,56 @@
#include "../include/nsgif.h"
+#include "cli.h"
+#include "cli.c"
+
#define BYTES_PER_PIXEL 4
+static struct nsgif_options {
+ const char *file;
+ const char *ppm;
+ uint64_t loops;
+ bool info;
+} nsgif_options;
+
+static const struct cli_table_entry cli_entries[] = {
+ {
+ .s = 'm',
+ .l = "ppm",
+ .t = CLI_STRING,
+ .v.s = &nsgif_options.ppm,
+ .d = "Convert frames to PPM image at given path."
+ },
+ {
+ .s = 'i',
+ .l = "info",
+ .t = CLI_BOOL,
+ .v.b = &nsgif_options.info,
+ .d = "Dump GIF info to stdout."
+ },
+ {
+ .s = 'l',
+ .l = "loops",
+ .t = CLI_UINT,
+ .v.u = &nsgif_options.loops,
+ .d = "Loop through decoding all frames N times. "
+ "The default is 1."
+ },
+ {
+ .p = true,
+ .l = "FILE",
+ .t = CLI_STRING,
+ .v.s = &nsgif_options.file,
+ .d = "Path to GIF file to load."
+ },
+};
+
+const struct cli_table cli = {
+ .entries = cli_entries,
+ .count = (sizeof(cli_entries))/(sizeof(*cli_entries)),
+ .min_positional = 1,
+};
+
static void *bitmap_create(int width, int height)
{
/* Ensure a stupidly large bitmap is not created */
@@ -84,7 +132,31 @@ static void warning(const char *context, nsgif_error err)
context, nsgif_strerror(err));
}
-static void decode(FILE* fh, const char *name, nsgif *gif, bool write_ppm)
+static void print_gif_info(const nsgif_info_t *info)
+{
+ fprintf(stdout, "gif:\n");
+ fprintf(stdout, " width: %"PRIu32"\n", info->width);
+ fprintf(stdout, " height: %"PRIu32"\n", info->height);
+ fprintf(stdout, " max-loops: %"PRIu32"\n", info->loop_max);
+ fprintf(stdout, " frame-count: %"PRIu32"\n", info->frame_count);
+ fprintf(stdout, " frames:\n");
+}
+
+static void print_gif_frame_info(const nsgif_frame_info_t *info)
+{
+ const char *disposal = nsgif_str_disposal(info->disposal);
+
+ fprintf(stdout, " - disposal-method: %s\n", disposal);
+ fprintf(stdout, " display: %s\n", info->display ? "yes" : "no");
+ fprintf(stdout, " delay: %"PRIu32"\n", info->delay);
+ fprintf(stdout, " rect:\n");
+ fprintf(stdout, " x: %"PRIu32"\n", info->rect.x0);
+ fprintf(stdout, " y: %"PRIu32"\n", info->rect.y0);
+ fprintf(stdout, " w: %"PRIu32"\n", info->rect.x1 - info->rect.x0);
+ fprintf(stdout, " h: %"PRIu32"\n", info->rect.y1 - info->rect.y0);
+}
+
+static void decode(FILE* ppm, const char *name, nsgif *gif)
{
nsgif_error err;
uint32_t frame_prev = 0;
@@ -92,17 +164,21 @@ static void decode(FILE* fh, const char *name, nsgif *gif, bool write_ppm)
info = nsgif_get_info(gif);
- if (write_ppm) {
- fprintf(fh, "P3\n");
- fprintf(fh, "# %s\n", name);
- fprintf(fh, "# width %u \n", info->width);
- fprintf(fh, "# height %u \n", info->height);
- fprintf(fh, "# frame_count %u \n", info->frame_count);
- fprintf(fh, "# loop_max %u \n", info->loop_max);
- fprintf(fh, "%u %u 256\n", info->width,
+ if (ppm != NULL) {
+ fprintf(ppm, "P3\n");
+ fprintf(ppm, "# %s\n", name);
+ fprintf(ppm, "# width %u \n", info->width);
+ fprintf(ppm, "# height %u \n", info->height);
+ fprintf(ppm, "# frame_count %u \n", info->frame_count);
+ fprintf(ppm, "# loop_max %u \n", info->loop_max);
+ fprintf(ppm, "%u %u 256\n", info->width,
info->height * info->frame_count);
}
+ if (nsgif_options.info == true) {
+ print_gif_info(info);
+ }
+
/* decode the frames */
while (true) {
nsgif_bitmap_t *buffer;
@@ -131,20 +207,32 @@ static void decode(FILE* fh, const char *name, nsgif *gif, bool write_ppm)
return;
}
- if (write_ppm) {
- fprintf(fh, "# frame %u:\n", frame_new);
+ if (nsgif_options.info == true) {
+ const nsgif_frame_info_t *f_info;
+
+ f_info = nsgif_get_frame_info(gif, frame_new);
+ assert(f_info != NULL);
+ print_gif_frame_info(f_info);
+ }
+
+ if (ppm != NULL) {
+ fprintf(ppm, "# frame %u:\n", frame_new);
image = (const uint8_t *) buffer;
for (uint32_t y = 0; y != info->height; y++) {
for (uint32_t x = 0; x != info->width; x++) {
size_t z = (y * info->width + x) * 4;
- fprintf(fh, "%u %u %u ",
+ fprintf(ppm, "%u %u %u ",
image[z],
image[z + 1],
image[z + 2]);
}
- fprintf(fh, "\n");
+ fprintf(ppm, "\n");
}
}
+
+ if (delay_cs == NSGIF_INFINITE) {
+ return;
+ }
}
}
@@ -159,40 +247,32 @@ int main(int argc, char *argv[])
size_t size;
uint8_t *data;
nsgif_error err;
- FILE *outf = stdout;
- bool no_write = false;
-
- if (argc < 2) {
- fprintf(stderr, "Usage: %s image.gif [out]\n", argv[0]);
- fprintf(stderr, "\n");
- fprintf(stderr, "If [out] is NOWRITE, the gif will be docoded "
- "but not output.\n");
- fprintf(stderr, "Otherwise [out] is an output filename.\n");
- fprintf(stderr, "When [out] is unset, output is to stdout.\n");
-
- return 1;
+ FILE *ppm = NULL;
+
+ /* Override default options with any command line args */
+ if (!cli_parse(&cli, argc, (void *)argv)) {
+ cli_help(&cli, argv[0]);
+ return EXIT_FAILURE;
}
- if (argc > 2) {
- if (strcmp(argv[2], "NOWRITE") == 0) {
- no_write = true;
- } else {
- outf = fopen(argv[2], "w+");
- if (outf == NULL) {
- fprintf(stderr, "Unable to open %s for writing\n", argv[2]);
- return 2;
- }
+ if (nsgif_options.ppm != NULL) {
+ ppm = fopen(nsgif_options.ppm, "w+");
+ if (ppm == NULL) {
+ fprintf(stderr, "Unable to open %s for writing\n",
+ nsgif_options.ppm);
+ return EXIT_FAILURE;
}
}
/* create our gif animation */
err = nsgif_create(&bitmap_callbacks, &gif);
if (err != NSGIF_OK) {
- return 1;
+ warning("nsgif_create", err);
+ return EXIT_FAILURE;
}
/* load file into memory */
- data = load_file(argv[1], &size);
+ data = load_file(nsgif_options.file, &size);
/* Scan the raw data */
err = nsgif_data_scan(gif, size, data);
@@ -200,13 +280,15 @@ int main(int argc, char *argv[])
warning("nsgif_data_scan", err);
nsgif_destroy(gif);
free(data);
- return 1;
+ return EXIT_FAILURE;
}
- decode(outf, argv[1], gif, !no_write);
+ for (uint64_t i = 0; i < nsgif_options.loops; i++) {
+ decode((i == 0) ? ppm : NULL, nsgif_options.file, gif);
+ }
- if (argc > 2 && !no_write) {
- fclose(outf);
+ if (ppm != NULL) {
+ fclose(ppm);
}
/* clean up */
diff --git a/test/runtest.sh b/test/runtest.sh
index 47cec7e..fd84847 100755
--- a/test/runtest.sh
+++ b/test/runtest.sh
@@ -23,7 +23,7 @@ gifdecode()
OUTF=$(basename ${1} .gif)
CMPF=$(dirname ${1})/${OUTF}.ppm
echo "GIF:${1}" >> ${TEST_LOG}
- ${TEST_PATH}/test_nsgif ${1} ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
+ ${TEST_PATH}/test_nsgif ${1} --ppm ${TEST_OUT}/${OUTF}.ppm 2>> ${TEST_LOG}
ECODE=$?
echo "Exit code:${ECODE}" >> ${TEST_LOG}