efl/src/lib/ecore/ecore_getopt.c

1964 lines
51 KiB
C
Raw Normal View History

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif !defined alloca
# ifdef __GNUC__
# define alloca __builtin_alloca
# elif defined _AIX
# define alloca __alloca
# elif defined _MSC_VER
# include <malloc.h>
# define alloca _alloca
# elif !defined HAVE_ALLOCA
# ifdef __cplusplus
extern "C"
# endif
void *alloca (size_t);
# endif
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#ifdef ENABLE_NLS
# include <libintl.h>
#else
# define gettext(x) (x)
# define dgettext(domain, x) (x)
#endif
#define _(x) dgettext("ecore", x)
#ifdef _WIN32_WCE
# include <Evil.h>
#endif
#ifdef HAVE_EXOTIC
# include <Exotic.h>
#endif
#include "Ecore.h"
#include "Ecore_Getopt.h"
static const char *prog = NULL;
static char **_argv = NULL;
static int _argc = 0;
static int cols = 80;
static int helpcol = 80 / 3;
static void
_ecore_getopt_help_print_replace_program(FILE *fp,
const Ecore_Getopt *parser EINA_UNUSED,
const char *text)
{
do
{
2010-09-29 23:09:20 -07:00
const char *d = strchr(text, '%');
if (!d)
{
fputs(text, fp);
break;
}
if (fwrite(text, 1, d - text, fp) != (size_t)(d - text))
return;
d++;
if (strncmp(d, "prog", sizeof("prog") - 1) == 0)
{
fputs(prog ? prog : "???", fp);
d += sizeof("prog") - 1;
}
else
{
if (d[0] == '%')
d++;
fputc('%', fp);
}
text = d;
}
while (text[0] != '\0');
fputc('\n', fp);
}
static void
_ecore_getopt_version(FILE *fp,
const Ecore_Getopt *parser)
{
fputs(_("Version:"), fp);
fputc(' ', fp);
_ecore_getopt_help_print_replace_program(fp, parser, parser->version);
}
static void
_ecore_getopt_help_usage(FILE *fp,
const Ecore_Getopt *parser)
{
fputs(_("Usage:"), fp);
fputc(' ', fp);
if (!parser->usage)
{
2010-09-29 23:09:20 -07:00
fprintf(fp, _("%s [options]\n"), prog);
return;
}
_ecore_getopt_help_print_replace_program(fp, parser, gettext(parser->usage));
}
static int
_ecore_getopt_help_line(FILE *fp,
const int base,
const int total,
int used,
const char *text,
int len)
{
int linebreak = 0;
do
{
2010-09-29 23:09:20 -07:00
/* process line considering spaces (new line and tabs are spaces!) */
while ((used < total) && (len > 0))
{
const char *space = NULL;
int i, todo;
todo = total - used;
if (todo > len)
todo = len;
for (i = 0; i < todo; i++)
if (isspace((unsigned char)text[i]))
{
space = text + i;
break;
}
if (space)
{
i = fwrite(text, 1, i, fp);
i++;
text += i;
len -= i;
used += i;
if (linebreak)
{
linebreak = 0;
continue;
}
if (space[0] == '\n')
break;
else if (space[0] == '\t')
{
int c;
used--;
c = ((used / 8) + 1) * 8;
if (c < total)
{
for (; used < c; used++)
fputc(' ', fp);
}
else
{
text--;
len++;
break;
}
}
else if (used < total)
fputc(space[0], fp);
}
else
{
i = fwrite(text, 1, i, fp);
text += i;
len -= i;
used += i;
}
linebreak = 0;
}
if (len <= 0)
break;
linebreak = 1;
fputc('\n', fp);
for (used = 0; used < base; used++)
fputc(' ', fp);
}
while (1);
return used;
}
static void
_ecore_getopt_help_description(FILE *fp,
const Ecore_Getopt *parser)
{
const char *p, *prg, *ver;
int used, prglen, verlen;
p = gettext(parser->description);
if (!p)
return;
fputc('\n', fp);
prg = prog ? prog : "???";
ver = parser->version ? parser->version : "???";
prglen = strlen(prg);
verlen = strlen(ver);
used = 0;
do
{
2010-09-29 23:09:20 -07:00
const char *d = strchr(p, '%');
if (!d)
{
_ecore_getopt_help_line(fp, 0, cols, used, p, strlen(p));
break;
}
used = _ecore_getopt_help_line(fp, 0, cols, used, p, d - p);
d++;
if (strncmp(d, "prog", sizeof("prog") - 1) == 0)
{
used = _ecore_getopt_help_line(fp, 0, cols, used, prg, prglen);
d += sizeof("prog") - 1;
}
else if (strncmp(d, "version", sizeof("version") - 1) == 0)
{
used = _ecore_getopt_help_line(fp, 0, cols, used, ver, verlen);
d += sizeof("version") - 1;
}
else
{
if (d[0] == '%')
d++;
used = _ecore_getopt_help_line(fp, 0, cols, used, "%", 1);
}
p = d;
}
while (p[0] != '\0');
fputs("\n\n", fp);
}
static void
_ecore_getopt_copyright(FILE *fp,
const Ecore_Getopt *parser)
{
const char *txt = gettext(parser->copyright);
fputs(_("Copyright:"), fp);
fputs("\n ", fp);
_ecore_getopt_help_line
(fp, 3, cols, 3, txt, strlen(txt));
fputc('\n', fp);
}
static void
_ecore_getopt_license(FILE *fp,
const Ecore_Getopt *parser)
{
const char *txt = gettext(parser->license);
fputs(_("License:"), fp);
fputs("\n ", fp);
_ecore_getopt_help_line
(fp, 3, cols, 3, txt, strlen(txt));
fputc('\n', fp);
}
static Ecore_Getopt_Desc_Arg_Requirement
_ecore_getopt_desc_arg_requirement(const Ecore_Getopt_Desc *desc)
{
switch (desc->action)
{
case ECORE_GETOPT_ACTION_STORE:
return desc->action_param.store.arg_req;
case ECORE_GETOPT_ACTION_STORE_CONST:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
case ECORE_GETOPT_ACTION_STORE_TRUE:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
case ECORE_GETOPT_ACTION_STORE_FALSE:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
case ECORE_GETOPT_ACTION_CHOICE:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES;
case ECORE_GETOPT_ACTION_APPEND:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES;
case ECORE_GETOPT_ACTION_COUNT:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
case ECORE_GETOPT_ACTION_CALLBACK:
return desc->action_param.callback.arg_req;
case ECORE_GETOPT_ACTION_HELP:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
case ECORE_GETOPT_ACTION_VERSION:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
default:
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
}
}
static void
_ecore_getopt_help_desc_setup_metavar(const Ecore_Getopt_Desc *desc,
char *metavar,
int *metavarlen,
int maxsize)
{
if (desc->metavar)
{
2010-09-29 23:09:20 -07:00
const char *txt = gettext(desc->metavar);
*metavarlen = strlen(txt);
if (*metavarlen > maxsize - 1)
*metavarlen = maxsize - 1;
2010-09-29 23:09:20 -07:00
memcpy(metavar, txt, *metavarlen);
metavar[*metavarlen] = '\0';
}
else if (desc->longname)
{
2010-09-29 23:09:20 -07:00
int i;
2010-09-29 23:09:20 -07:00
*metavarlen = strlen(desc->longname);
if (*metavarlen > maxsize - 1)
*metavarlen = maxsize - 1;
2010-09-29 23:09:20 -07:00
for (i = 0; i < *metavarlen; i++)
metavar[i] = toupper((int) desc->longname[i]);
2010-09-29 23:09:20 -07:00
metavar[i] = '\0';
}
}
static int
_ecore_getopt_help_desc_show_arg(FILE *fp,
Ecore_Getopt_Desc_Arg_Requirement requirement,
const char *metavar,
int metavarlen)
{
int used;
if (requirement == ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
return 0;
used = 0;
if (requirement == ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL)
{
2010-09-29 23:09:20 -07:00
fputc('[', fp);
used++;
}
if (requirement != ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
{
2010-09-29 23:09:20 -07:00
fputc('=', fp);
fputs(metavar, fp);
used += metavarlen + 1;
}
if (requirement == ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL)
{
2010-09-29 23:09:20 -07:00
fputc(']', fp);
used++;
}
return used;
}
static int
_ecore_getopt_help_desc_store(FILE *fp,
const int base,
const int total,
int used,
const Ecore_Getopt_Desc *desc)
{
const Ecore_Getopt_Desc_Store *store = &desc->action_param.store;
char buf[64];
const char *str;
size_t len;
fputc('\n', fp);
for (used = 0; used < base; used++)
fputc(' ', fp);
switch (store->type)
{
case ECORE_GETOPT_TYPE_STR:
str = "STR";
len = sizeof("STR") - 1;
break;
case ECORE_GETOPT_TYPE_BOOL:
str = "BOOL";
len = sizeof("BOOL") - 1;
break;
case ECORE_GETOPT_TYPE_SHORT:
str = "SHORT";
len = sizeof("SHORT") - 1;
break;
case ECORE_GETOPT_TYPE_INT:
str = "INT";
len = sizeof("INT") - 1;
break;
case ECORE_GETOPT_TYPE_LONG:
str = "LONG";
len = sizeof("LONG") - 1;
break;
case ECORE_GETOPT_TYPE_USHORT:
str = "USHORT";
len = sizeof("USHORT") - 1;
break;
case ECORE_GETOPT_TYPE_UINT:
str = "UINT";
len = sizeof("UINT") - 1;
break;
case ECORE_GETOPT_TYPE_ULONG:
str = "ULONG";
len = sizeof("ULONG") - 1;
break;
case ECORE_GETOPT_TYPE_DOUBLE:
str = "DOUBLE";
len = sizeof("DOUBLE") - 1;
break;
default:
str = "???";
len = sizeof("???") - 1;
}
used = _ecore_getopt_help_line
(fp, base, total, used, _("Type: "), strlen(_("Type: ")));
used = _ecore_getopt_help_line(fp, base, total, used, str, len);
if (store->arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES)
goto end;
used = _ecore_getopt_help_line
(fp, base, total, used, ". ", sizeof(". ") - 1);
switch (store->type)
{
case ECORE_GETOPT_TYPE_STR:
str = store->def.strv;
len = str ? strlen(str) : 0;
break;
case ECORE_GETOPT_TYPE_BOOL:
str = store->def.boolv ? "true" : "false";
len = strlen(str);
break;
case ECORE_GETOPT_TYPE_SHORT:
str = buf;
len = snprintf(buf, sizeof(buf), "%hd", store->def.shortv);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
case ECORE_GETOPT_TYPE_INT:
str = buf;
len = snprintf(buf, sizeof(buf), "%d", store->def.intv);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
case ECORE_GETOPT_TYPE_LONG:
str = buf;
len = snprintf(buf, sizeof(buf), "%ld", store->def.longv);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
case ECORE_GETOPT_TYPE_USHORT:
str = buf;
len = snprintf(buf, sizeof(buf), "%hu", store->def.ushortv);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
case ECORE_GETOPT_TYPE_UINT:
str = buf;
len = snprintf(buf, sizeof(buf), "%u", store->def.uintv);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
case ECORE_GETOPT_TYPE_ULONG:
str = buf;
len = snprintf(buf, sizeof(buf), "%lu", store->def.ulongv);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
case ECORE_GETOPT_TYPE_DOUBLE:
str = buf;
len = snprintf(buf, sizeof(buf), "%f", store->def.doublev);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
break;
default:
str = "???";
len = sizeof("???") - 1;
}
used = _ecore_getopt_help_line
(fp, base, total, used, _("Default: "), strlen(_("Default: ")));
used = _ecore_getopt_help_line(fp, base, total, used, str, len);
end:
return _ecore_getopt_help_line(fp, base, total, used, ".", 1);
}
static int
_ecore_getopt_help_desc_choices(FILE *fp,
const int base,
const int total,
int used,
const Ecore_Getopt_Desc *desc)
{
const char *const *itr;
const char sep[] = ", ";
const int seplen = sizeof(sep) - 1;
if (used > 0)
{
2010-09-29 23:09:20 -07:00
fputc('\n', fp);
used = 0;
}
for (; used < base; used++)
fputc(' ', fp);
used = _ecore_getopt_help_line
(fp, base, total, used, _("Choices: "), strlen(_("Choices: ")));
for (itr = desc->action_param.choices; *itr; itr++)
{
2010-09-29 23:09:20 -07:00
used = _ecore_getopt_help_line
(fp, base, total, used, *itr, strlen(*itr));
2010-09-29 23:09:20 -07:00
if (itr[1])
used = _ecore_getopt_help_line(fp, base, total, used, sep, seplen);
}
return _ecore_getopt_help_line(fp, base, total, used, ".", 1);
}
static void
_ecore_getopt_help_desc(FILE *fp,
const Ecore_Getopt_Desc *desc)
{
Ecore_Getopt_Desc_Arg_Requirement arg_req;
char metavar[32] = "ARG";
int metavarlen = 3;
int used;
arg_req = _ecore_getopt_desc_arg_requirement(desc);
if (arg_req != ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
_ecore_getopt_help_desc_setup_metavar
(desc, metavar, &metavarlen, sizeof(metavar));
fputs(" ", fp);
used = 2;
if (desc->shortname)
{
2010-09-29 23:09:20 -07:00
fputc('-', fp);
fputc(desc->shortname, fp);
used += 2;
used += _ecore_getopt_help_desc_show_arg
(fp, arg_req, metavar, metavarlen);
}
if (desc->shortname && desc->longname)
{
2010-09-29 23:09:20 -07:00
fputs(", ", fp);
used += 2;
}
if (desc->longname)
{
2010-09-29 23:09:20 -07:00
int namelen = strlen(desc->longname);
2010-09-29 23:09:20 -07:00
fputs("--", fp);
fputs(desc->longname, fp);
used += 2 + namelen;
used += _ecore_getopt_help_desc_show_arg
(fp, arg_req, metavar, metavarlen);
}
if (!desc->help)
goto end;
if (used + 3 >= helpcol)
{
2010-09-29 23:09:20 -07:00
fputc('\n', fp);
used = 0;
}
for (; used < helpcol; used++)
fputc(' ', fp);
used = _ecore_getopt_help_line
(fp, helpcol, cols, used, desc->help, strlen(desc->help));
switch (desc->action)
{
case ECORE_GETOPT_ACTION_STORE:
_ecore_getopt_help_desc_store(fp, helpcol, cols, used, desc);
break;
case ECORE_GETOPT_ACTION_CHOICE:
_ecore_getopt_help_desc_choices(fp, helpcol, cols, used, desc);
break;
default:
break;
}
end:
fputc('\n', fp);
}
static Eina_Bool
_ecore_getopt_desc_is_sentinel(const Ecore_Getopt_Desc *desc)
{
return (desc->shortname == '\0') && (!desc->longname);
}
static void
_ecore_getopt_help_options(FILE *fp,
const Ecore_Getopt *parser)
{
const Ecore_Getopt_Desc *desc;
fputs(_("Options:\n"), fp);
for (desc = parser->descs; !_ecore_getopt_desc_is_sentinel(desc); desc++)
_ecore_getopt_help_desc(fp, desc);
fputc('\n', fp);
}
/**
* Show nicely formatted help message for the given parser.
*
* @param fp The file the message will be printed on.
* @param parser The parser to be used.
*/
EAPI void
ecore_getopt_help(FILE *fp,
const Ecore_Getopt *parser)
{
const char *var;
EINA_MAIN_LOOP_CHECK_RETURN;
if (!parser) return;
if (_argc < 1)
{
ecore_app_args_get(&_argc, &_argv);
if ((_argc > 0) && (_argv[0]))
prog = _argv[0];
2010-09-29 23:09:20 -07:00
else
prog = parser->prog;
}
var = getenv("COLUMNS");
if (var)
{
2010-09-29 23:09:20 -07:00
cols = atoi(var);
if (cols < 20)
cols = 20;
2010-09-29 23:09:20 -07:00
helpcol = cols / 3;
}
_ecore_getopt_help_usage(fp, parser);
_ecore_getopt_help_description(fp, parser);
_ecore_getopt_help_options(fp, parser);
}
static const Ecore_Getopt_Desc *
_ecore_getopt_parse_find_long(const Ecore_Getopt *parser,
const char *name)
{
const Ecore_Getopt_Desc *desc = parser->descs;
const char *p = strchr(name, '=');
int len = 0;
if (p)
len = p - name;
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
{
2010-09-29 23:09:20 -07:00
if (!desc->longname)
continue;
if (p)
{
if ((strncmp(name, desc->longname, len) == 0) &&
(desc->longname[len] == '\0'))
return desc;
}
else
{
if (strcmp(name, desc->longname) == 0)
return desc;
}
}
return NULL;
}
static const Ecore_Getopt_Desc *
_ecore_getopt_parse_find_short(const Ecore_Getopt *parser,
char name)
{
const Ecore_Getopt_Desc *desc = parser->descs;
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
if (name == desc->shortname)
return desc;
return NULL;
}
static int
_ecore_getopt_parse_find_nonargs_base(const Ecore_Getopt *parser,
int argc,
char **argv)
{
char **nonargs;
int src, dst, used, base, abreak;
nonargs = alloca(sizeof(char *) * argc);
src = 1;
dst = 1;
used = 0;
base = 0;
abreak = 0;
while (src < argc)
{
2010-09-29 23:09:20 -07:00
const Ecore_Getopt_Desc *desc;
Ecore_Getopt_Desc_Arg_Requirement arg_req;
char *arg = argv[src];
if (abreak)
{
base = 1;
break;
}
2010-09-29 23:09:20 -07:00
if (arg[0] != '-')
goto found_nonarg;
if (arg[1] == '-')
{
if (arg[2] == '\0') /* explicit end of options, "--" */
{
base = 1;
break;
}
desc = _ecore_getopt_parse_find_long(parser, arg + 2);
}
else
desc = _ecore_getopt_parse_find_short(parser, arg[1]);
if (!desc)
{
if (arg[1] == '-')
fprintf(stderr, _("ERROR: unknown option --%s.\n"), arg + 2);
else
fprintf(stderr, _("ERROR: unknown option -%c.\n"), arg[1]);
if (parser->strict)
{
memmove(argv + dst, nonargs, used * sizeof(char *));
return -1;
}
else
goto found_nonarg;
}
if (desc->action == ECORE_GETOPT_ACTION_BREAK)
abreak = 1;
2010-09-29 23:09:20 -07:00
if (src != dst)
argv[dst] = argv[src];
src++;
dst++;
arg_req = _ecore_getopt_desc_arg_requirement(desc);
if (arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
continue;
if (strchr(arg, '='))
continue;
if ((src >= argc) || (argv[src][0] == '-'))
continue;
if (src != dst)
argv[dst] = argv[src];
src++;
dst++;
continue;
found_nonarg:
2010-09-29 23:09:20 -07:00
nonargs[used] = arg;
used++;
src++;
}
if (!base) /* '--' not found */
base = dst;
else
{
2010-09-29 23:09:20 -07:00
base = dst;
if (src != dst)
argv[dst] = argv[src];
dst++;
}
memmove(argv + dst, nonargs, used * sizeof(char *));
return base;
}
static void
_ecore_getopt_desc_print_error(const Ecore_Getopt_Desc *desc,
const char *fmt,
...)
{
va_list ap;
fputs(_("ERROR: "), stderr);
if (desc->shortname)
{
2010-09-29 23:09:20 -07:00
fputc('-', stderr);
fputc(desc->shortname, stderr);
}
if (desc->shortname && desc->longname)
fputs(", ", stderr);
if (desc->longname)
{
2010-09-29 23:09:20 -07:00
fputs("--", stderr);
fputs(desc->longname, stderr);
}
fputs(": ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
static Eina_Bool
_ecore_getopt_parse_bool(const char *str,
Eina_Bool *v)
{
if ((strcmp(str, "0") == 0) ||
(strcasecmp(str, "f") == 0) ||
(strcasecmp(str, "false") == 0) ||
(strcasecmp(str, "no") == 0) ||
(strcasecmp(str, "off<