diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-04-19 16:33:12 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-04-20 09:51:08 (EDT) |
commit | 851d501c2f8e9378417472aa8e083bddb6a59450 (patch) | |
tree | 1d5130aff6cd6f98457138372ebbedce635393b8 | |
parent | bbc6371dfbf42185cf8545c8cf33276c7ce52fd4 (diff) |
main: Handle options
Missing: -h, -V, and -?
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/main.c | 113 |
3 files changed, 101 insertions, 15 deletions
@@ -1,2 +1 @@ -Add option handling I18n? diff --git a/configure.ac b/configure.ac index c2a4411..afb70ab 100644 --- a/configure.ac +++ b/configure.ac @@ -45,6 +45,8 @@ AC_CHECK_FUNCS( for func in log10 ceil lrint; do AC_SEARCH_LIBS([${func}], [m], [], [funcs_missing=true]) done +AC_CHECK_FUNCS([getopt_long], [], + [AC_CHECK_FUNCS([getopt], [], [funcs_missing=true])]) if ${funcs_missing}; then AC_MSG_ERROR([required functions are missing]) fi @@ -22,45 +22,130 @@ #include "opk.h" #include "ustar.h" +#include "config.h" + +#ifdef HAVE_GETOPT_LONG +#include <getopt.h> +#else +#include <unistd.h> +#endif + +const char *_optstring = "I:fchV"; +#ifdef HAVE_GETOPT_LONG +struct option _longopts[] = { + { + .name = "info", + .has_arg = 1, + .flag = NULL, + .val = 'I', + }, + { + .name = "control", + .has_arg = 0, + .flag = NULL, + .val = 'f', + }, + { + .name = "contents", + .has_arg = 0, + .flag = NULL, + .val = 'c', + }, + { + .name = "help", + .has_arg = 0, + .flag = NULL, + .val = 'h', + }, + { + .name = "version", + .has_arg = 0, + .flag = NULL, + .val = 'V', + }, +}; +#endif + int main(int argc, char *argv[]) { struct opkg_opk_ustar_seek_name *control_files; + int list_members; + int opt; struct opkg_opk_opk *opk; control_files = NULL; - if (opkg_opk_ustar_add_seek_name(&control_files, "control") != - OPKG_OPK_OK) { - goto error0; - } - if (opkg_opk_ustar_add_seek_name(&control_files, "md5sums") != - OPKG_OPK_OK) { - goto error0; + list_members = 0; +#ifdef HAVE_GETOPT_LONG + while ((opt = getopt_long(argc, argv, _optstring, _longopts, NULL)) + != -1) { +#else + while ((opt = getopt(argc, argv, _optstring)) != -1) { +#endif + switch(opt) { + case 'I': + if (opkg_opk_ustar_add_seek_name(&control_files, + optarg) != + OPKG_OPK_OK) { + goto error0; + } + break; + case 'f': + if (opkg_opk_ustar_add_seek_name(&control_files, + "control") != + OPKG_OPK_OK) { + goto error0; + } + break; + case 'c': + list_members = 1; + break; + case 'h': + /* TODO */ + break; + case 'V': + /* TODO */ + break; + default: + /* TODO */ + break; + } } + argc -= optind; + argv += optind; /* Initialize outer archive. */ - opk = opkg_opk_opk_init(argv[1]); + opk = opkg_opk_opk_init(argv[0]); if (opk == NULL) { goto error0; } /* Read control file. */ - if (opkg_opk_opk_read_control(opk, control_files) != OPKG_OPK_OK) { - goto error1; + if (control_files != NULL) { + if (opkg_opk_opk_read_control(opk, control_files) != + OPKG_OPK_OK) { + goto error1; + } } /* List data files. */ - if (opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) { - goto error1; + if (list_members == 1) { + if (opkg_opk_opk_list_members(opk) != OPKG_OPK_OK) { + goto error1; + } } opkg_opk_opk_free(opk); - opkg_opk_ustar_free_seek_names(control_files); + if (control_files != NULL) { + opkg_opk_ustar_free_seek_names(control_files); + } return EXIT_SUCCESS; error1: opkg_opk_opk_free(opk); error0: - opkg_opk_ustar_free_seek_names(control_files); + if (control_files != NULL) { + opkg_opk_ustar_free_seek_names(control_files); + } return EXIT_FAILURE; } |