From a1d0435da57ef0ce21074966d338c042bfcf3e09 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 7 Oct 2010 21:03:34 +0000 Subject: [PATCH] Move mygetopt() directly into libcaca, for use by other programs. --- caca/Makefile.am | 1 + caca/caca.h | 25 ++++++++++++ src/mygetopt.c => caca/getopt.c | 67 ++++++++++++++++++++------------- caca/libcaca.vcproj | 4 ++ configure.ac | 1 - src/Makefile.am | 4 -- src/img2txt.c | 35 ++++++----------- src/mygetopt.h | 29 -------------- 8 files changed, 82 insertions(+), 84 deletions(-) rename src/mygetopt.c => caca/getopt.c (58%) delete mode 100644 src/mygetopt.h diff --git a/caca/Makefile.am b/caca/Makefile.am index b56d0fa..f700d42 100644 --- a/caca/Makefile.am +++ b/caca/Makefile.am @@ -45,6 +45,7 @@ libcaca_la_SOURCES = \ event.c \ time.c \ prof.c \ + getopt.c \ $(codec_source) \ $(driver_source) \ $(NULL) diff --git a/caca/caca.h b/caca/caca.h index cd343f1..857297a 100644 --- a/caca/caca.h +++ b/caca/caca.h @@ -138,6 +138,19 @@ struct caca_event #endif }; +/** \brief Option parsing. + * + * This structure contains commandline parsing information for systems + * where getopt_long() is unavailable. + */ +struct caca_option +{ + char const *name; + int has_arg; + int *flag; + int val; +}; + /** \brief Special key values. * * Special key values returned by caca_get_event() for which there is no @@ -508,6 +521,18 @@ __extern int caca_get_event_resize_width(caca_event_t const *); __extern int caca_get_event_resize_height(caca_event_t const *); /* @} */ +/** \defgroup caca_process libcaca process management + * + * These functions help with various process handling tasks such as + * option parsing, DLL injection. + * + * @{ */ +__extern int caca_optind; +__extern char *caca_optarg; +__extern int caca_getopt(int, char * const[], char const *, + struct caca_option const *, int *); +/* @} */ + /** \brief DOS colours * * This enum lists the colour values for the DOS conio.h compatibility diff --git a/src/mygetopt.c b/caca/getopt.c similarity index 58% rename from src/mygetopt.c rename to caca/getopt.c index 294bf97..f04547c 100644 --- a/src/mygetopt.c +++ b/caca/getopt.c @@ -11,39 +11,51 @@ */ /* - * mygetopt.c: getopt_long reimplementation + * getopt.c: getopt_long reimplementation */ #include "config.h" -#if defined HAVE_STDINT_H -# include -#elif defined HAVE_INTTYPES_H -# include +#if !defined __KERNEL__ +# include +# include +# if defined HAVE_GETOPT_H && defined HAVE_GETOPT_LONG +# include +# endif #endif -#include -#include +#include "caca.h" +#include "caca_internals.h" -#include "mygetopt.h" +int caca_optind = 1; +char *caca_optarg = NULL; -int myoptind = 1; -char *myoptarg = NULL; - -/* XXX: this getopt_long implementation should not be trusted for other - * applications without any serious peer reviewing. It “just works” with - * zzuf but may fail miserably in other programs. */ -int mygetopt(int argc, char * const _argv[], const char *optstring, - const struct myoption *longopts, int *longindex) +int caca_getopt(int argc, char * const _argv[], char const *optstring, + struct caca_option const *longopts, int *longindex) { +#if defined HAVE_GETOPT_LONG + int ret; + optind = caca_optind; + optarg = caca_optarg; + ret = getopt_long(argc, _argv, optstring, + (struct option const *)longopts, longindex); + caca_optind = optind; + caca_optarg = optarg; + return ret; + +#else + /* XXX: this getopt_long implementation should not be trusted for other + * applications without any serious peer reviewing. It “just works” with + * zzuf and a few libcaca programs but may fail miserably in other + * programs. */ char **argv = (char **)(uintptr_t)_argv; char *flag; int i; - if(myoptind >= argc) + if(caca_optind >= argc) return -1; - flag = argv[myoptind]; + flag = argv[caca_optind]; if(flag[0] == '-' && flag[1] != '-') { @@ -57,21 +69,21 @@ int mygetopt(int argc, char * const _argv[], const char *optstring, if(!tmp || ret == ':') return '?'; - myoptind++; + caca_optind++; if(tmp[1] == ':') { if(flag[2] != '\0') - myoptarg = flag + 2; + caca_optarg = flag + 2; else - myoptarg = argv[myoptind++]; + caca_optarg = argv[caca_optind++]; return ret; } if(flag[2] != '\0') { flag[1] = '-'; - myoptind--; - argv[myoptind]++; + caca_optind--; + argv[caca_optind]++; } return ret; @@ -96,15 +108,15 @@ int mygetopt(int argc, char * const _argv[], const char *optstring, goto bad_opt; if(longindex) *longindex = i; - myoptind++; - myoptarg = flag + 2 + l + 1; + caca_optind++; + caca_optarg = flag + 2 + l + 1; return longopts[i].val; case '\0': if(longindex) *longindex = i; - myoptind++; + caca_optind++; if(longopts[i].has_arg) - myoptarg = argv[myoptind++]; + caca_optarg = argv[caca_optind++]; return longopts[i].val; default: break; @@ -116,5 +128,6 @@ int mygetopt(int argc, char * const _argv[], const char *optstring, } return -1; +#endif } diff --git a/caca/libcaca.vcproj b/caca/libcaca.vcproj index 4247b2a..0d5e4a6 100644 --- a/caca/libcaca.vcproj +++ b/caca/libcaca.vcproj @@ -468,6 +468,10 @@ RelativePath="frame.c" > + + diff --git a/configure.ac b/configure.ac index fea08db..2211c4e 100644 --- a/configure.ac +++ b/configure.ac @@ -132,7 +132,6 @@ AC_CHECK_FUNCS(getopt_long, if test "$ac_cv_have_getopt_long" != "no"; then AC_DEFINE(HAVE_GETOPT_LONG, 1, Define to 1 if you have the ‘getopt_long’ function.) fi -AM_CONDITIONAL(NEED_GETOPT_LONG, test "$ac_cv_have_getopt_long" = "no") AC_SUBST(GETOPT_LIBS) AC_MSG_CHECKING(for Sleep) diff --git a/src/Makefile.am b/src/Makefile.am index 446b532..4654344 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -40,9 +40,5 @@ else fcntl_programs = endif -if NEED_GETOPT_LONG -GETOPT = mygetopt.c mygetopt.h -endif - echo-sources: ; echo $(SOURCES) diff --git a/src/img2txt.c b/src/img2txt.c index ccb3390..3e04864 100644 --- a/src/img2txt.c +++ b/src/img2txt.c @@ -19,18 +19,6 @@ # include #endif -#if !defined HAVE_GETOPT_LONG -# include "mygetopt.h" -#elif defined HAVE_GETOPT_H -# include -#endif -#if defined HAVE_GETOPT_LONG -# define mygetopt getopt_long -# define myoptind optind -# define myoptarg optarg -# define myoption option -#endif - #include "caca.h" #include "common-image.h" @@ -110,7 +98,7 @@ int main(int argc, char **argv) for(;;) { int option_index = 0; - static struct myoption long_options[] = + static struct caca_option long_options[] = { { "width", 1, NULL, 'W' }, { "height", 1, NULL, 'H' }, @@ -124,38 +112,39 @@ int main(int argc, char **argv) { "help", 0, NULL, 'h' }, { "version", 0, NULL, 'v' }, }; - int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:", long_options, &option_index); + int c = caca_getopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:", + long_options, &option_index); if(c == -1) break; switch(c) { case 'W': /* --width */ - cols = atoi(myoptarg); + cols = atoi(caca_optarg); break; case 'H': /* --height */ - lines = atoi(myoptarg); + lines = atoi(caca_optarg); break; case 'x': /* --width */ - font_width = atoi(myoptarg); + font_width = atoi(caca_optarg); break; case 'y': /* --height */ - font_height = atoi(myoptarg); + font_height = atoi(caca_optarg); break; case 'f': /* --format */ - format = myoptarg; + format = caca_optarg; break; case 'd': /* --dither */ - dither = myoptarg; + dither = caca_optarg; break; case 'g': /* --gamma */ - gamma = atof(myoptarg); + gamma = atof(caca_optarg); break; case 'b': /* --brightness */ - brightness = atof(myoptarg); + brightness = atof(caca_optarg); break; case 'c': /* --contrast */ - contrast = atof(myoptarg); + contrast = atof(caca_optarg); break; case 'h': /* --help */ usage(argc, argv); diff --git a/src/mygetopt.h b/src/mygetopt.h deleted file mode 100644 index 0fbdbc9..0000000 --- a/src/mygetopt.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * libcaca Colour ASCII-Art library - * Copyright (c) 2002-2010 Sam Hocevar - * All Rights Reserved - * - * This program is free software. It comes without any warranty, to - * the extent permitted by applicable law. You can redistribute it - * and/or modify it under the terms of the Do What The Fuck You Want - * To Public License, Version 2, as published by Sam Hocevar. See - * http://sam.zoy.org/wtfpl/COPYING for more details. - */ - -/* - * mygetopt.h: getopt_long reimplementation - */ - -struct myoption -{ - const char *name; - int has_arg; - int *flag; - int val; -}; - -extern int myoptind; -extern char *myoptarg; - -int mygetopt(int, char * const[], const char *, const struct myoption *, int *); -