| @@ -45,6 +45,7 @@ libcaca_la_SOURCES = \ | |||
| event.c \ | |||
| time.c \ | |||
| prof.c \ | |||
| getopt.c \ | |||
| $(codec_source) \ | |||
| $(driver_source) \ | |||
| $(NULL) | |||
| @@ -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 | |||
| @@ -11,39 +11,51 @@ | |||
| */ | |||
| /* | |||
| * mygetopt.c: getopt_long reimplementation | |||
| * getopt.c: getopt_long reimplementation | |||
| */ | |||
| #include "config.h" | |||
| #if defined HAVE_STDINT_H | |||
| # include <stdint.h> | |||
| #elif defined HAVE_INTTYPES_H | |||
| # include <inttypes.h> | |||
| #if !defined __KERNEL__ | |||
| # include <stdio.h> | |||
| # include <string.h> | |||
| # if defined HAVE_GETOPT_H && defined HAVE_GETOPT_LONG | |||
| # include <getopt.h> | |||
| # endif | |||
| #endif | |||
| #include <stdio.h> | |||
| #include <string.h> | |||
| #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 | |||
| } | |||
| @@ -468,6 +468,10 @@ | |||
| RelativePath="frame.c" | |||
| > | |||
| </File> | |||
| <File | |||
| RelativePath="getopt.c" | |||
| > | |||
| </File> | |||
| <File | |||
| RelativePath="graphics.c" | |||
| > | |||
| @@ -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) | |||
| @@ -40,9 +40,5 @@ else | |||
| fcntl_programs = | |||
| endif | |||
| if NEED_GETOPT_LONG | |||
| GETOPT = mygetopt.c mygetopt.h | |||
| endif | |||
| echo-sources: ; echo $(SOURCES) | |||
| @@ -19,18 +19,6 @@ | |||
| # include <stdlib.h> | |||
| #endif | |||
| #if !defined HAVE_GETOPT_LONG | |||
| # include "mygetopt.h" | |||
| #elif defined HAVE_GETOPT_H | |||
| # include <getopt.h> | |||
| #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); | |||
| @@ -1,29 +0,0 @@ | |||
| /* | |||
| * libcaca Colour ASCII-Art library | |||
| * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net> | |||
| * 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 *); | |||