diff options
Diffstat (limited to 'lib/cli++')
-rw-r--r-- | lib/cli++/ChangeLog | 123 | ||||
-rw-r--r-- | lib/cli++/Makefile.am | 9 | ||||
-rw-r--r-- | lib/cli++/Makefile.in | 554 | ||||
-rw-r--r-- | lib/cli++/cli++.h | 69 | ||||
-rw-r--r-- | lib/cli++/cli++topod.pl | 213 | ||||
-rw-r--r-- | lib/cli++/clitest.cc | 47 | ||||
-rw-r--r-- | lib/cli++/main.cc | 377 | ||||
-rw-r--r-- | lib/cli++/messages.cc | 68 | ||||
-rw-r--r-- | lib/cli++/only_long.cc | 3 |
9 files changed, 1463 insertions, 0 deletions
diff --git a/lib/cli++/ChangeLog b/lib/cli++/ChangeLog new file mode 100644 index 0000000..7cd88de --- /dev/null +++ b/lib/cli++/ChangeLog @@ -0,0 +1,123 @@ +2001-03-07 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * Renamed cli.h to cli++.h + +2000-10-25 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * cli2pod.pl (parse_options): Ignore {0,} as well as {0}. + +2000-08-15 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * cli2pod.pl: Replaced the CLI documentation programs with this + script which outputs POD, which can be translated to man pages or + HTML (or LaTeX, or text, or FM). + +2000-08-14 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * cli_parse.pl (parse_header_line): Rewrote the parsing to deal + with multi-line strings. + + * cli2man.pl (synopsis): Add usage string. + + * cli2html.pl (synopsis): Add usage string. + +2000-08-12 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * cli_parse.pl, cli2html.pl, cli2man.pl: Created these programs. + +2000-08-01 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * main.cc (show_option): Fixed several width glitches. + +2000-07-18 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * cli.h (struct cli_option): Added new uinteger type. + + * main.cc (fill): Removed use of mystring. + +2000-07-13 Bruce Guenter <bruce@bruce-guenter.dyndns.org> + + * Removed include of mystring. + +2000-01-09 Bruce Guenter <bruceg@daedalus.bfsmedia.com> + + * main.cc (parse_short): Modified the logic here to treat a string + value immediately following a string option as the value for that + option rather than as more flags. This makes it behave much more + like the standard getopt library. + +1999-09-30 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (show_option): Changed stringlist option string from + "=LIST" to "=ITEM". + +1999-09-29 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (cli_option::set): Fixed problem with setting a string + list option. + +1999-09-11 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (show_help): Split off two parts of this routine into + calc_max_width and show_option. + (show_option): Add "=INT" for integer options, and don't add extra + space for non-value long options. + (set): Use strtol instead of atoi to parse the integer string, to + allow for error checking. + (show_option): Fixed handling of string lists. + +1999-08-14 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (set_argv0): Sets argv0 to the complete value of + argv[0], argv0dir to the part of argv[0] up to and including the + last '/' (or blank if there is none), and argv0base to the + remainder of argv[0]. This is for use in programs that determine + what to do based on the value of the program name. + +1999-07-14 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (parse_long_eq): Fixed to account for "counter" flag + type. + (parse_long_noeq): Fixed to account for "counter" flag type. + (parse_long_eq): set() will return one, but this shouldn't return + one, so subtract one from its result. + (show_help): Added a mechanism to display default values on a + second line. + (show_help): Output a blank line before the "--help" option line. + +1999-07-04 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (show_help): Only show a "=VALUE" for string and integer + option types. + +1999-06-30 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * messages.cc (cli_error): Moved this routine into a separate + module, and added a "cli_warning" routine. + +1999-06-25 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc (set): Added handling for two new option types: string + list and counters. A stringlist maintains a linked list of all + the given option arguments. A counter adds the flag_value to the + dataptr each time it is encountered. + (parse_short): Fixed faulty logic regarding options with values. + Need to merge parts into cli_option::set(). + +1999-06-24 Bruce Guenter <bguenter@mikhail.qcc.sk.ca> + + * main.cc: Rewrote the "help" option handling to stop it being a + special case, by making an internal option list that includes a + "help" option at the end of it. + (show_help): Cleaned up the option formatting to produce more + correct output. + (build_options): Removed the need to count the options. Note + that this breaks compatibility with previous versions. + (cli_error): Added this convenience function for the CLI program + to report errors and optionally exit. + (set): Added functionality to call functions when an option is + parsed, and moved some of the option parsing into class methods + from the structure. + (main): Moved the test for showing the usage information before + the test for counting command-line arguments. + diff --git a/lib/cli++/Makefile.am b/lib/cli++/Makefile.am new file mode 100644 index 0000000..7cb7392 --- /dev/null +++ b/lib/cli++/Makefile.am @@ -0,0 +1,9 @@ +noinst_LIBRARIES = libcli++.a +EXTRA_DIST = clitest.cc cli++topod.pl + +AM_CPPFLAGS = -I$(top_srcdir)/lib +#LIBS = @LIBS@ -L. -lcli++ -L../lib -lvmailmgr + +libcli___a_SOURCES = cli++.h main.cc messages.cc only_long.cc +#clitest_SOURCES = clitest.cc + diff --git a/lib/cli++/Makefile.in b/lib/cli++/Makefile.in new file mode 100644 index 0000000..a133e95 --- /dev/null +++ b/lib/cli++/Makefile.in @@ -0,0 +1,554 @@ +# Makefile.in generated by automake 1.15 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994-2014 Free Software Foundation, Inc. + +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +VPATH = @srcdir@ +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} +am__make_running_with_option = \ + case $${target_option-} in \ + ?) ;; \ + *) echo "am__make_running_with_option: internal error: invalid" \ + "target option '$${target_option-}' specified" >&2; \ + exit 1;; \ + esac; \ + has_opt=no; \ + sane_makeflags=$$MAKEFLAGS; \ + if $(am__is_gnu_make); then \ + sane_makeflags=$$MFLAGS; \ + else \ + case $$MAKEFLAGS in \ + *\\[\ \ ]*) \ + bs=\\; \ + sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \ + | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \ + esac; \ + fi; \ + skip_next=no; \ + strip_trailopt () \ + { \ + flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \ + }; \ + for flg in $$sane_makeflags; do \ + test $$skip_next = yes && { skip_next=no; continue; }; \ + case $$flg in \ + *=*|--*) continue;; \ + -*I) strip_trailopt 'I'; skip_next=yes;; \ + -*I?*) strip_trailopt 'I';; \ + -*O) strip_trailopt 'O'; skip_next=yes;; \ + -*O?*) strip_trailopt 'O';; \ + -*l) strip_trailopt 'l'; skip_next=yes;; \ + -*l?*) strip_trailopt 'l';; \ + -[dEDm]) skip_next=yes;; \ + -[JT]) skip_next=yes;; \ + esac; \ + case $$flg in \ + *$$target_option*) has_opt=yes; break;; \ + esac; \ + done; \ + test $$has_opt = yes +am__make_dryrun = (target_option=n; $(am__make_running_with_option)) +am__make_keepgoing = (target_option=k; $(am__make_running_with_option)) +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +subdir = lib/cli++ +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \ + $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +LIBRARIES = $(noinst_LIBRARIES) +AR = ar +ARFLAGS = cru +AM_V_AR = $(am__v_AR_@AM_V@) +am__v_AR_ = $(am__v_AR_@AM_DEFAULT_V@) +am__v_AR_0 = @echo " AR " $@; +am__v_AR_1 = +libcli___a_AR = $(AR) $(ARFLAGS) +libcli___a_LIBADD = +am_libcli___a_OBJECTS = main.$(OBJEXT) messages.$(OBJEXT) \ + only_long.$(OBJEXT) +libcli___a_OBJECTS = $(am_libcli___a_OBJECTS) +AM_V_P = $(am__v_P_@AM_V@) +am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) +am__v_P_0 = false +am__v_P_1 = : +AM_V_GEN = $(am__v_GEN_@AM_V@) +am__v_GEN_ = $(am__v_GEN_@AM_DEFAULT_V@) +am__v_GEN_0 = @echo " GEN " $@; +am__v_GEN_1 = +AM_V_at = $(am__v_at_@AM_V@) +am__v_at_ = $(am__v_at_@AM_DEFAULT_V@) +am__v_at_0 = @ +am__v_at_1 = +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) +AM_V_CXX = $(am__v_CXX_@AM_V@) +am__v_CXX_ = $(am__v_CXX_@AM_DEFAULT_V@) +am__v_CXX_0 = @echo " CXX " $@; +am__v_CXX_1 = +CXXLD = $(CXX) +CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \ + -o $@ +AM_V_CXXLD = $(am__v_CXXLD_@AM_V@) +am__v_CXXLD_ = $(am__v_CXXLD_@AM_DEFAULT_V@) +am__v_CXXLD_0 = @echo " CXXLD " $@; +am__v_CXXLD_1 = +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +AM_V_CC = $(am__v_CC_@AM_V@) +am__v_CC_ = $(am__v_CC_@AM_DEFAULT_V@) +am__v_CC_0 = @echo " CC " $@; +am__v_CC_1 = +CCLD = $(CC) +LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +AM_V_CCLD = $(am__v_CCLD_@AM_V@) +am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) +am__v_CCLD_0 = @echo " CCLD " $@; +am__v_CCLD_1 = +SOURCES = $(libcli___a_SOURCES) +DIST_SOURCES = $(libcli___a_SOURCES) +am__can_run_installinfo = \ + case $$AM_UPDATE_INFO_DIR in \ + n|no|NO) false;; \ + *) (install-info --version) >/dev/null 2>&1;; \ + esac +am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) +# Read a list of newline-separated strings from the standard input, +# and print each of them once, without duplicates. Input order is +# *not* preserved. +am__uniquify_input = $(AWK) '\ + BEGIN { nonempty = 0; } \ + { items[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in items) print i; }; } \ +' +# Make sure the list of sources is unique. This is necessary because, +# e.g., the same source file might be shared among _SOURCES variables +# for different programs/libraries. +am__define_uniq_tagged_files = \ + list='$(am__tagged_files)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | $(am__uniquify_input)` +ETAGS = etags +CTAGS = ctags +am__DIST_COMMON = $(srcdir)/Makefile.in $(top_srcdir)/depcomp \ + ChangeLog +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPPFLAGS = @CPPFLAGS@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +CXXDEPMODE = @CXXDEPMODE@ +CXXFLAGS = @CXXFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +GREP = @GREP@ +HAVE_GETADDRINFO = @HAVE_GETADDRINFO@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LDFLAGS = @LDFLAGS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MKDIR = @MKDIR@ +MKDIR_P = @MKDIR_P@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +RM = @RM@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_CXX = @ac_ct_CXX@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build_alias = @build_alias@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host_alias = @host_alias@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +noinst_LIBRARIES = libcli++.a +EXTRA_DIST = clitest.cc cli++topod.pl +AM_CPPFLAGS = -I$(top_srcdir)/lib +#LIBS = @LIBS@ -L. -lcli++ -L../lib -lvmailmgr +libcli___a_SOURCES = cli++.h main.cc messages.cc only_long.cc +all: all-am + +.SUFFIXES: +.SUFFIXES: .cc .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu lib/cli++/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --gnu lib/cli++/Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-noinstLIBRARIES: + -test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES) + +libcli++.a: $(libcli___a_OBJECTS) $(libcli___a_DEPENDENCIES) $(EXTRA_libcli___a_DEPENDENCIES) + $(AM_V_at)-rm -f libcli++.a + $(AM_V_AR)$(libcli___a_AR) libcli++.a $(libcli___a_OBJECTS) $(libcli___a_LIBADD) + $(AM_V_at)$(RANLIB) libcli++.a + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/messages.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/only_long.Po@am__quote@ + +.cc.o: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ $< + +.cc.obj: +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'` + +ID: $(am__tagged_files) + $(am__define_uniq_tagged_files); mkid -fID $$unique +tags: tags-am +TAGS: tags + +tags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + set x; \ + here=`pwd`; \ + $(am__define_uniq_tagged_files); \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: ctags-am + +CTAGS: ctags +ctags-am: $(TAGS_DEPENDENCIES) $(am__tagged_files) + $(am__define_uniq_tagged_files); \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" +cscopelist: cscopelist-am + +cscopelist-am: $(am__tagged_files) + list='$(am__tagged_files)'; \ + case "$(srcdir)" in \ + [\\/]* | ?:[\\/]*) sdir="$(srcdir)" ;; \ + *) sdir=$(subdir)/$(srcdir) ;; \ + esac; \ + for i in $$list; do \ + if test -f "$$i"; then \ + echo "$(subdir)/$$i"; \ + else \ + echo "$$sdir/$$i"; \ + fi; \ + done >> $(top_builddir)/cscope.files + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(LIBRARIES) +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + if test -z '$(STRIP)'; then \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + install; \ + else \ + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; \ + fi +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-noinstLIBRARIES mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: install-am install-strip + +.PHONY: CTAGS GTAGS TAGS all all-am check check-am clean clean-generic \ + clean-noinstLIBRARIES cscopelist-am ctags ctags-am distclean \ + distclean-compile distclean-generic distclean-tags distdir dvi \ + dvi-am html html-am info info-am install install-am \ + install-data install-data-am install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic pdf pdf-am ps ps-am tags tags-am uninstall \ + uninstall-am + +.PRECIOUS: Makefile + +#clitest_SOURCES = clitest.cc + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/lib/cli++/cli++.h b/lib/cli++/cli++.h new file mode 100644 index 0000000..3dfeb14 --- /dev/null +++ b/lib/cli++/cli++.h @@ -0,0 +1,69 @@ +#ifndef VMAILMGR__CLIPP__CLIPP__H__ +#define VMAILMGR__CLIPP__CLIPP__H__ + +typedef bool (*cli_funcptr)(void*); + +struct cli_stringlist +{ + const char* string; + cli_stringlist* next; + + cli_stringlist(const char* s) + : string(s), next(0) + { + } +}; + +struct cli_option +{ + char ch; + const char* name; + enum { flag, counter, integer, string, stringlist, ulong } type; + int flag_value; + void* dataptr; + const char* helpstr; + const char* defaultstr; + + int set(const char* arg); + int parse_long_eq(const char* arg, int as_short); + int parse_long_noeq(const char* arg, int as_short); +}; + +#define CLI_OPTION_END {0, 0, cli_option::flag, 0, 0, 0, 0} + +/* The following are required from the CLI program */ +extern const char* cli_program; +extern const char* cli_help_prefix; +extern const char* cli_help_suffix; +extern const char* cli_args_usage; +extern const int cli_args_min; +extern const int cli_args_max; +extern cli_option cli_options[]; +extern const bool cli_only_long; +extern int cli_main(int argc, char* argv[]); + +/* The following are provided to the CLI program */ +extern const char* argv0; +extern const char* argv0base; +extern const char* argv0dir; +extern void usage(int exit_value, const char* errorstr = 0); +extern int cli_parse_args(int argc, char* argv[]); + +extern void cli_error(int exit_value, + const char*, + const char* = 0, + const char* = 0, + const char* = 0); + +extern void cli_syserror(int exit_value, + const char*, + const char* = 0, + const char* = 0, + const char* = 0); + +extern void cli_warning(const char*, + const char* = 0, + const char* = 0, + const char* = 0); + +#endif diff --git a/lib/cli++/cli++topod.pl b/lib/cli++/cli++topod.pl new file mode 100644 index 0000000..5d2ef13 --- /dev/null +++ b/lib/cli++/cli++topod.pl @@ -0,0 +1,213 @@ +#!/usr/bin/perl + +sub cstr2pod { + local($_) = shift; + s/\\"/"/go; + s/"([^\"]*)"/"C<$1>"/go; + $_; +} + +$section = 1; + +@section_order = ( + 'NAME', + 'SYNOPSIS', + 'DESCRIPTION', + 'OPTIONS', + 'RETURN VALUE', + 'ERRORS', + 'EXAMPLES', + 'ENVIRONMENT', + 'FILES', + 'SEE ALSO', + 'NOTES', + 'CAVEATS', + 'WARNINGS', + 'DIAGNOSTICS', + 'BUGS', + 'RESTRICTIONS', + 'AUTHOR', + 'AUTHORS', + 'HISTORY' + ); + +sub type2word { + my($type) = shift; + return 'INT' if $type eq 'integer'; + return 'UINT' if $type eq 'ulong'; + return 'STR' if $type eq 'string' || $type eq 'stringlist'; + return '' if $type eq 'flag' || $type eq 'counter'; + die "Invalid cli option type '$type'"; +} + +sub add_option { + my($short, $long, $type, $desc) = @_; + + my $s = '[B<'; + my $o = '=item B<'; + if($short) { + $s .= "-$short"; + $o .= "-$short"; + if($type) { + $s .= " $type"; + $o .= " $type"; + } + } + if($short && $long) { + $s .= ">]\n[B<"; + $o .= ">, B<"; + } + if($long) { + $s .= "--$long"; + $o .= "--$long"; + if($type) { + $s .= "=$type"; + $o .= "=$type"; + } + } + $s .= ">]\n"; + $o .= ">\n\n$desc\n\n"; + + $synopsis .= $s; + $options = "=over 8\n\n" unless $options; + $options .= $o; +} + +sub parse_option { + local($_) = shift; + s/^\s*\{\s*//o; + s/\s*\},?\s*/ /o; + + my $short = $1 if s/^'([^\'])',\s*//o; + die "Invalid cli option" unless $short || s/^0,\s*//o; + + my $long = $1 if s/^"([^\"]+)",\s*//o; + die "Invalid cli_option" unless $long || s/^0,\s*//o; + + my $type = $1 if s/^cli_option::(\S+),\s*//o; + die "Invalid cli_option" unless $type; + $type = &type2word($type); + + my $val = $1 if s/^([^,]+),\s*//o; + my $var = $1 if s/^&([^,]+),\s*//o; + + my $desc = cstr2pod($1) if s/^"([^,]+)",\s*//o; + die "Invalid cli_option" unless $desc; + $desc =~ s/\.?$/./o if $desc; + + my $default = $1 if s/^"([^\"]+)"\s+//o; + die "Invalid cli_option" unless $default || s/^0\s+//o; + $desc .= " Defaults to $default." if $default; + + s/\s*\/\/\s*/ /go; + s/^\s*//o; + + add_option($short, $long, $type, $_ || $desc); +} + +sub parse_options { + $synopsis = "B<$program>\n"; + + my $line; + while(<>) { + s/^\s+//o; + s/\s+$//o; + if($line && /^\{/o) { + &parse_option($line); + $line = ""; + } + next if /^\{\s*0\s*\},?/o; + next if /^\{\s*0\s*,\s*\},?/o; + last if /^\s*\};/o; + $line =~ s/$/ $_/; + } + &parse_option($line) if $line; + + $synopsis .= "I<$usage>" if $usage; + $options .= "=back" if $options; + $sections{'SYNOPSIS'} = $synopsis; + $sections{'OPTIONS'} = $options; +} + +sub parse_notes { + my $section; + my $title; + while(<>) { + chomp; + last unless /^$/o || s/^\/\/\s*//o; + if(/^[\sA-Z]+$/o) { + $sections{$title} = $section if $title && $section; + undef $section; + $title = $_; + } else { + $section .= "$_\n"; + } + } + $sections{$title} = $section if $title && $section; +} + +sub parse_header_line { + local($_, $comment) = @_; + if(s/^\s*const\s+char\s*\*\s*cli_(\S+)\s*=\s*//o) { + my $name = $1; + s/;\s*$//o; + s/^\"//; + s/\"$//o; + s/\\n$//o; + s/\\n""/\n/go; + $program = $_ if $name eq 'program'; + $prefix = $_ if $name eq 'help_prefix'; + $usage = $_ if $name eq 'args_usage'; + $suffix = $_ if $name eq 'help_suffix'; + } +} + +sub parse_header { + my $comment = ''; + my $line = ''; + while(<>) { + s/^\s+//o; + s/\s+$//o; + if(s/^.*Copyright\s*\(C\)\s*[\d,]+\s*//o) { + $author = $_; + } else { + last if ($program && $prefix && /^$/o); + next if /^#/o; + $comment .= "$1\n" if s|\s*//\s*(.*)$||o; + $line =~ s/$/\n$_/; + if(/;$/o) { + &parse_header_line($line, $comment); + undef $line; + undef $comment; + } + } + } +} + +sub parse_description { + while(<>) { + s/^\s+//o; + s/\s+$//o; + last if / cli_options\[\]\s*=\s*\{/o; + next unless s/^\/\/\s*//o; + $description .= "$_\n"; + } +} + +&parse_header; +&parse_description; +&parse_options; +&parse_notes; + +$description .= "\n\n$suffix\n" if $suffix; + +$sections{'NAME'} = "$program - $prefix"; +$sections{'DESCRIPTION'} = $description; +$sections{'AUTHORS'} = $author if $author; + +foreach $section (@section_order) { + print "=head1 $section\n\n$sections{$section}\n\n" + if $sections{$section}; +} + +1; diff --git a/lib/cli++/clitest.cc b/lib/cli++/clitest.cc new file mode 100644 index 0000000..97f703d --- /dev/null +++ b/lib/cli++/clitest.cc @@ -0,0 +1,47 @@ +// Copyright (C) 2017 Bruce Guenter <bruce@untroubled.org> +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include <config.h> +#include "cli++.h" +#include "fdbuf/fdbuf.h" + +const char* cli_program = "clitest"; +const char* cli_help_prefix = "Does nothing but set flags\n"; +const char* cli_help_suffix = ""; +const char* cli_args_usage = ""; +const int cli_args_min = 0; +const int cli_args_max = -1; +int o_flag = 0; +int o_int = 0; +char* o_string = "nostring"; +cli_option cli_options[] = { + { 'f', "flag", cli_option::flag, 1, &o_flag, "Sets a flag", 0 }, + { 'i', "int", cli_option::integer, 0, &o_int, "Sets an integer", 0 }, + { 's', "str", cli_option::string, 0, &o_string, "Sets a string", 0}, + {0} }; + +int cli_main(int argc, char* argv[]) +{ + fout << "argv0=" << argv0 << endl + << " argv0dir=" << argv0dir << endl + << " argv0base=" << argv0base << endl; + fout << "The flag is set to " << o_flag << endl; + fout << "The integer is set to " << o_int << endl; + fout << "The string is set to " << o_string << endl; + for(int i = 0; i < argc; i++) + fout << "argv[" << i << "] = '" << argv[i] << "'\n"; + return 0; +} diff --git a/lib/cli++/main.cc b/lib/cli++/main.cc new file mode 100644 index 0000000..83c26e2 --- /dev/null +++ b/lib/cli++/main.cc @@ -0,0 +1,377 @@ +// Copyright (C) 2017 Bruce Guenter <bruce@untroubled.org> +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include <config.h> +#include "ac/time.h" +#include "fdbuf/fdbuf.h" +#include <limits.h> +#include <stdlib.h> +#include <string.h> +#include "cli++.h" + +#ifndef HAVE_SRANDOM +void srandom(unsigned int seed); +#endif + +static bool do_show_usage = false; +const char* argv0; +const char* argv0base; +const char* argv0dir; + +static cli_option help_option = { 'h', "help", cli_option::flag, + true, &do_show_usage, + "Display this help and exit", 0 }; + +static cli_option** options; +static unsigned optionc; +static const char* short_options; + +static void build_options() +{ + for(optionc = 0; + cli_options[optionc].ch || cli_options[optionc].name; + optionc++) ; + optionc++; + options = new cli_option*[optionc]; + for(unsigned i = 0; i < optionc-1; i++) + options[i] = &cli_options[i]; + options[optionc-1] = &help_option; + + char* so; + short_options = so = new char[optionc+1]; + for (unsigned i = 0; i < optionc; i++) + if (options[i]->ch != 0) + *so++ = options[i]->ch; + *so = 0; +} + +static inline int max(int a, int b) +{ + return (a>b) ? a : b; +} + +static const char* fill(int i) +{ + static int lastlen = 0; + static char* buf = 0; + if(i > lastlen) { + delete[] buf; + buf = new char[i+1]; + lastlen = i; + } + memset(buf, ' ', i); + buf[i] = 0; + return buf; +} + +static void show_usage() +{ + fout << "usage: " << cli_program << " [flags] " << cli_args_usage << endl; +} + +static int calc_width(const cli_option* o) +{ + int width = (o->ch || !cli_only_long) ? 4 : 2; + if (o->name) { + width += strlen(o->name) + 2 + !cli_only_long; + switch (o->type) { + case cli_option::string: width += 6; break; + case cli_option::integer: width += 4; break; + case cli_option::ulong: width += 4; break; + case cli_option::stringlist: width += 5; break; + case cli_option::flag: break; + case cli_option::counter: break; + } + } + return width; +} + +static int calc_max_width() +{ + // maxwidth is the maximum width of the option text prefix + int maxwidth = 0; + for(unsigned i = 0; i < optionc; i++) + maxwidth = max(maxwidth, calc_width(options[i])); + return maxwidth; +} + +static void show_option(const cli_option* o, int maxwidth) +{ + if(o == &help_option) + fout << '\n'; + fout << " "; + if(o->ch) + fout << '-' << o->ch; + else if (!cli_only_long) + fout << " "; + if (o->ch || !cli_only_long) + fout << (o->ch && o->name ? ", " : " "); + if(o->name) { + const char* extra = ""; + switch(o->type) { + case cli_option::string: extra = "=VALUE"; break; + case cli_option::integer: extra = "=INT"; break; + case cli_option::ulong: extra = "=UNS"; break; + case cli_option::stringlist: extra = "=ITEM"; break; + case cli_option::flag: break; + case cli_option::counter: break; + } + fout << (cli_only_long ? "-" : "--") << o->name << extra + << fill(maxwidth - strlen(o->name) - strlen(extra) - !cli_only_long + - (o->ch || !cli_only_long ? 4 : 0)); + } + else + fout << fill(maxwidth-3); + fout << o->helpstr << '\n'; + if(o->defaultstr) + fout << fill(maxwidth+3) << "(Defaults to " << o->defaultstr << ")\n"; +} + +static void show_help() +{ + if(cli_help_prefix) + fout << cli_help_prefix; + int maxwidth = calc_max_width(); + for(unsigned i = 0; i < optionc; i++) + show_option(options[i], maxwidth); + if(cli_help_suffix) + fout << cli_help_suffix; +} + +void usage(int exit_value, const char* errorstr) +{ + if(errorstr) + ferr << cli_program << ": " << errorstr << endl; + show_usage(); + show_help(); + exit(exit_value); +} + +cli_stringlist* stringlist_append(cli_stringlist* node, const char* newstr) +{ + cli_stringlist* newnode = new cli_stringlist(newstr); + if(node) { + cli_stringlist* head = node; + while(node->next) + node = node->next; + node->next = newnode; + return head; + } + else + return newnode; +} + +int cli_option::set(const char* arg) +{ + char* endptr; + switch(type) { + case flag: + *(int*)dataptr = flag_value; + return 0; + case counter: + *(int*)dataptr += flag_value; + return 0; + case integer: + { + long longresult = strtol(arg, &endptr, 10); + if(*endptr || longresult < INT_MIN || longresult > INT_MAX) { + ferr << argv0 << ": invalid integer: " << arg << endl; + return -1; + } + *(int*)dataptr = (int) longresult; + return 1; + } + case ulong: + *(unsigned long*)dataptr = strtoul(arg, &endptr, 10); + if(*endptr) { + ferr << argv0 << ": invalid unsigned integer: " << arg << endl; + return -1; + } + return 1; + case stringlist: + *(cli_stringlist**)dataptr = + stringlist_append(*(cli_stringlist**)dataptr, arg); + return 1; + default: // string + *(const char**)dataptr = arg; + return 1; + } +} + +static int parse_short(int argc, char* argv[]) +{ + int end = strlen(argv[0]) - 1; + for(int i = 1; i <= end; i++) { + int ch = argv[0][i]; + unsigned j; + for(j = 0; j < optionc; j++) { + cli_option* o = options[j]; + if(o->ch == ch) { + if(o->type != cli_option::flag && + o->type != cli_option::counter) { + if(i < end) { + if(o->set(argv[0]+i+1) != -1) + return 0; + } + else if(argc <= 1) { + ferr << argv0 << ": option -" << o->ch + << " requires a value." << endl; + } + else + if(o->set(argv[1]) != -1) + return 1; + } + else if(o->set(0) != -1) + break; + return -1; + } + } + if(j >= optionc) { + ferr << argv0 << ": unknown option letter -" << argv[0][i] << endl; + return -1; + } + } + return 0; +} + +static void option_error(const cli_option* o, int as_short, const char* text) +{ + ferr << argv0 << ": option "; + if (as_short) + ferr << '-' << o->ch; + else + ferr << (cli_only_long ? "-" : "--") << o->name; + ferr << text << endl; +} + +int cli_option::parse_long_eq(const char* arg, int as_short) +{ + if(type == flag || type == counter) { + option_error(this, as_short, " does not take a value."); + return -1; + } + else + return set(arg)-1; +} + +int cli_option::parse_long_noeq(const char* arg, int as_short) +{ + if(type == flag || type == counter) + return set(0); + else if(arg) + return set(arg); + else { + option_error(this, as_short, " requires a value."); + return -1; + } +} + +static int parse_long(int, char* argv[]) +{ + const char* arg = argv[0]+1; + // Handle both short and long args + if (arg[0] == '-') + ++arg; + for(unsigned j = 0; j < optionc; j++) { + cli_option* o = options[j]; + if(o->name) { + size_t len = strlen(o->name); + if(!memcmp(arg, o->name, len)) { + if(arg[len] == '\0') + return o->parse_long_noeq(argv[1], false); + else if(arg[len] == '=') + return o->parse_long_eq(arg+len+1, false); + } + } + } + ferr << argv0 << ": unknown option string: '" << argv[0] << "'" << endl; + return -1; +} + +static int parse_either(int argc, char* argv[]) +{ + return (strchr(short_options, argv[0][1]) != 0) + ? parse_short(argc, argv) + : parse_long(argc, argv); +} + +int cli_parse_args(int argc, char* argv[]) +{ + int i; + for(i = 1; i < argc; i++) { + const char* arg = argv[i]; + // Stop at the first non-option argument + if(arg[0] != '-') + break; + // Stop after the first "-" or "--" + if(arg[1] == '\0' || + (arg[1] == '-' && arg[2] == '\0')) { + i++; + break; + } + int j = (arg[1] == '-') ? parse_long(argc-i, argv+i) + : cli_only_long ? parse_either(argc-i, argv+i) + : parse_short(argc-i, argv+i); + if(j < 0) + usage(1); + else + i += j; + } + return i; +} + +static void set_argv0(const char* p) +{ + argv0 = p; + static const char* empty = ""; + const char* s = strrchr(p, '/'); + if(s) { + ++s; + argv0base = s; + size_t length = s-p; + char* tmp = new char[length+1]; + memcpy(tmp, p, length); + tmp[length] = 0; + argv0dir = tmp; + } + else { + argv0base = p; + argv0dir = empty; + } +} + +int main(int argc, char* argv[]) +{ + struct timeval tv; + gettimeofday(&tv, 0); + srandom(tv.tv_usec ^ tv.tv_sec); + + set_argv0(argv[0]); + build_options(); + int lastarg = cli_parse_args(argc, argv); + + if(do_show_usage) + usage(0); + + argc -= lastarg; + argv += lastarg; + if(argc < cli_args_min) + usage(1, "Too few command-line arguments"); + if(cli_args_max >= cli_args_min && argc > cli_args_max) + usage(1, "Too many command-line arguments"); + + return cli_main(argc, argv); +} diff --git a/lib/cli++/messages.cc b/lib/cli++/messages.cc new file mode 100644 index 0000000..d9e053d --- /dev/null +++ b/lib/cli++/messages.cc @@ -0,0 +1,68 @@ +// Copyright (C) 2017 Bruce Guenter <bruce@untroubled.org> +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include <errno.h> +#include <config.h> +#include "fdbuf/fdbuf.h" +#include <stdlib.h> +#include <string.h> +#include "cli++.h" + +extern const char* argv0; + +static void cli_msg(const char* prefix, + const char* a, + const char* b, + const char* c, + const char* d, + bool add_error = false) +{ + ferr << cli_program << ": " << prefix << a; + if(b) ferr << b; + if(c) ferr << c; + if(d) ferr << d; + if (add_error) + ferr << ": " << strerror(errno); + ferr << endl; +} + +void cli_error(int exit_value, + const char* a, + const char* b, + const char* c, + const char* d) +{ + cli_msg("Error: ", a, b, c, d, false); + exit(exit_value); +} + +void cli_syserror(int exit_value, + const char* a, + const char* b, + const char* c, + const char* d) +{ + cli_msg("Error: ", a, b, c, d, true); + exit(exit_value); +} + +void cli_warning(const char* a, + const char* b, + const char* c, + const char* d) +{ + cli_msg("Warning: ", a, b, c, d, false); +} diff --git a/lib/cli++/only_long.cc b/lib/cli++/only_long.cc new file mode 100644 index 0000000..3800a25 --- /dev/null +++ b/lib/cli++/only_long.cc @@ -0,0 +1,3 @@ +#include "cli++.h" + +const bool cli_only_long = false; |