2008-12-19 12:54:13 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2012-11-18 22:26:48 -08:00
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stddef.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_STDLIB_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
2010-04-05 10:48:08 -07:00
|
|
|
|
2011-12-06 08:09:05 -08:00
|
|
|
#include <stdlib.h>
|
2008-11-23 16:49:04 -08:00
|
|
|
#include <stdio.h>
|
2008-12-19 12:54:13 -08:00
|
|
|
#include <string.h>
|
2008-11-23 16:49:04 -08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2009-09-01 10:27:23 -07:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
# include <libintl.h>
|
2009-09-01 07:46:16 -07:00
|
|
|
#else
|
2011-10-20 22:40:39 -07:00
|
|
|
# define gettext(x) (x)
|
2009-09-01 10:27:23 -07:00
|
|
|
# define dgettext(domain, x) (x)
|
2009-09-01 07:15:37 -07:00
|
|
|
#endif
|
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
#define _(x) dgettext("ecore", x)
|
2009-09-01 07:15:37 -07:00
|
|
|
|
2009-01-31 10:33:39 -08:00
|
|
|
#include "Ecore.h"
|
2008-12-19 12:54:13 -08:00
|
|
|
#include "Ecore_Getopt.h"
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
static const char *prog = NULL;
|
2012-03-29 01:56:10 -07:00
|
|
|
static char **_argv = NULL;
|
|
|
|
static int _argc = 0;
|
2008-11-23 16:49:04 -08:00
|
|
|
static int cols = 80;
|
|
|
|
static int helpcol = 80 / 3;
|
|
|
|
|
2013-12-17 15:06:53 -08:00
|
|
|
|
|
|
|
static Eina_Bool _ecore_getopt_desc_is_sentinel(const Ecore_Getopt_Desc *desc);
|
|
|
|
static Ecore_Getopt_Desc_Arg_Requirement _ecore_getopt_desc_arg_requirement(const Ecore_Getopt_Desc *desc);
|
|
|
|
static void _ecore_getopt_help_desc_setup_metavar(const Ecore_Getopt_Desc *desc, char *metavar, int *metavarlen, int maxsize);
|
|
|
|
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_print_replace_program(FILE *fp,
|
2012-11-25 01:55:32 -08:00
|
|
|
const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const char *text)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
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;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
while (text[0] != '\0');
|
|
|
|
|
|
|
|
fputc('\n', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_version(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2009-09-01 07:15:37 -07:00
|
|
|
fputs(_("Version:"), fp);
|
|
|
|
fputc(' ', fp);
|
2008-11-23 16:49:04 -08:00
|
|
|
_ecore_getopt_help_print_replace_program(fp, parser, parser->version);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_usage(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2009-09-01 07:15:37 -07:00
|
|
|
fputs(_("Usage:"), fp);
|
|
|
|
fputc(' ', fp);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
if (!parser->usage)
|
|
|
|
{
|
2013-12-17 15:06:53 -08:00
|
|
|
const Ecore_Getopt_Desc *d;
|
|
|
|
|
|
|
|
fprintf(fp, _("%s [options]"), prog);
|
|
|
|
|
|
|
|
for (d = parser->descs; !_ecore_getopt_desc_is_sentinel(d); d++);
|
|
|
|
|
|
|
|
if (d->metavar)
|
|
|
|
{
|
|
|
|
for (; d->metavar != NULL; d++)
|
|
|
|
{
|
|
|
|
Ecore_Getopt_Desc_Arg_Requirement ar;
|
|
|
|
char metavar[32];
|
|
|
|
int metavarlen = 0;
|
|
|
|
|
|
|
|
ar = _ecore_getopt_desc_arg_requirement(d);
|
|
|
|
_ecore_getopt_help_desc_setup_metavar(d, metavar,
|
|
|
|
&metavarlen,
|
|
|
|
sizeof(metavar));
|
|
|
|
|
|
|
|
fputc(' ', fp);
|
|
|
|
if (ar != ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES)
|
|
|
|
fputc('[', fp);
|
|
|
|
fputs(metavar, fp);
|
|
|
|
|
|
|
|
if (ar != ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES)
|
|
|
|
fputc(']', fp);
|
|
|
|
|
|
|
|
if (d->action == ECORE_GETOPT_ACTION_APPEND)
|
|
|
|
{
|
|
|
|
fprintf(fp, " [%s] ...", metavar);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', fp);
|
2010-09-29 23:09:20 -07:00
|
|
|
return;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2009-09-01 07:15:37 -07:00
|
|
|
_ecore_getopt_help_print_replace_program(fp, parser, gettext(parser->usage));
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_line(FILE *fp,
|
|
|
|
const int base,
|
|
|
|
const int total,
|
|
|
|
int used,
|
|
|
|
const char *text,
|
|
|
|
int len)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
int linebreak = 0;
|
|
|
|
do
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
/* process line considering spaces (new line and tabs are spaces!) */
|
2011-10-20 22:40:39 -07:00
|
|
|
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++)
|
2011-12-01 19:33:19 -08:00
|
|
|
if (isspace((unsigned char)text[i]))
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
space = text + i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space)
|
|
|
|
{
|
|
|
|
i = fwrite(text, 1, i, fp);
|
|
|
|
i++;
|
|
|
|
text += i;
|
|
|
|
len -= i;
|
|
|
|
used += i;
|
|
|
|
|
2013-12-17 11:51:48 -08:00
|
|
|
/* if we start the line (linebreak is true) with a space
|
|
|
|
* other than newline or tab, ignore it.
|
|
|
|
*/
|
|
|
|
if ((linebreak) && (i == 1) &&
|
|
|
|
(space[0] != '\n') && (space[0] != '\t'))
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2013-12-17 11:51:48 -08:00
|
|
|
else if ((!linebreak) && (todo < len))
|
|
|
|
{
|
|
|
|
/* if not start of line and not last line, wrap line
|
|
|
|
* and try again. This avoids spliting words unless they
|
|
|
|
* are bigger than the available line width.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
2011-10-20 22:40:39 -07:00
|
|
|
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);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_description(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const char *p, *prg, *ver;
|
|
|
|
int used, prglen, verlen;
|
|
|
|
|
2009-09-01 07:15:37 -07:00
|
|
|
p = gettext(parser->description);
|
2008-11-23 16:49:04 -08:00
|
|
|
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;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
while (p[0] != '\0');
|
|
|
|
|
|
|
|
fputs("\n\n", fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_copyright(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2009-09-01 07:15:37 -07:00
|
|
|
const char *txt = gettext(parser->copyright);
|
|
|
|
fputs(_("Copyright:"), fp);
|
2009-10-27 11:36:54 -07:00
|
|
|
fputs("\n ", fp);
|
2008-11-23 16:49:04 -08:00
|
|
|
_ecore_getopt_help_line
|
2009-09-01 07:15:37 -07:00
|
|
|
(fp, 3, cols, 3, txt, strlen(txt));
|
2008-11-23 16:49:04 -08:00
|
|
|
fputc('\n', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_license(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2009-09-01 07:15:37 -07:00
|
|
|
const char *txt = gettext(parser->license);
|
|
|
|
fputs(_("License:"), fp);
|
|
|
|
fputs("\n ", fp);
|
2008-11-23 16:49:04 -08:00
|
|
|
_ecore_getopt_help_line
|
2009-09-01 07:15:37 -07:00
|
|
|
(fp, 3, cols, 3, txt, strlen(txt));
|
2008-11-23 16:49:04 -08:00
|
|
|
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:
|
2011-10-20 22:40:39 -07:00
|
|
|
return desc->action_param.store.arg_req;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_STORE_CONST:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_STORE_TRUE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_STORE_FALSE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_CHOICE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_APPEND:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_COUNT:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_CALLBACK:
|
2011-10-20 22:40:39 -07:00
|
|
|
return desc->action_param.callback.arg_req;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_HELP:
|
2014-01-20 09:10:21 -08:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL;
|
2011-10-20 22:40:39 -07:00
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_VERSION:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
default:
|
2011-10-20 22:40:39 -07:00
|
|
|
return ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc_setup_metavar(const Ecore_Getopt_Desc *desc,
|
|
|
|
char *metavar,
|
|
|
|
int *metavarlen,
|
|
|
|
int maxsize)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
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;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
memcpy(metavar, txt, *metavarlen);
|
|
|
|
metavar[*metavarlen] = '\0';
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
else if (desc->longname)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
int i;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
*metavarlen = strlen(desc->longname);
|
|
|
|
if (*metavarlen > maxsize - 1)
|
|
|
|
*metavarlen = maxsize - 1;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
for (i = 0; i < *metavarlen; i++)
|
2011-11-20 06:44:05 -08:00
|
|
|
metavar[i] = toupper((int) desc->longname[i]);
|
2010-09-29 23:09:20 -07:00
|
|
|
metavar[i] = '\0';
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc_show_arg(FILE *fp,
|
|
|
|
Ecore_Getopt_Desc_Arg_Requirement requirement,
|
|
|
|
const char *metavar,
|
2013-12-17 15:06:53 -08:00
|
|
|
int metavarlen,
|
|
|
|
Eina_Bool show_attr)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
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++;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (requirement != ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
|
|
|
|
{
|
2013-12-17 15:06:53 -08:00
|
|
|
if (show_attr)
|
|
|
|
{
|
|
|
|
fputc('=', fp);
|
|
|
|
used++;
|
|
|
|
}
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs(metavar, fp);
|
2013-12-17 15:06:53 -08:00
|
|
|
used += metavarlen;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (requirement == ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputc(']', fp);
|
|
|
|
used++;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc_store(FILE *fp,
|
|
|
|
const int base,
|
|
|
|
const int total,
|
|
|
|
int used,
|
|
|
|
const Ecore_Getopt_Desc *desc)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc_Store *store = &desc->action_param.store;
|
|
|
|
char buf[64];
|
|
|
|
const char *str;
|
2009-01-31 10:33:39 -08:00
|
|
|
size_t len;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
fputc('\n', fp);
|
|
|
|
for (used = 0; used < base; used++)
|
|
|
|
fputc(' ', fp);
|
|
|
|
|
|
|
|
switch (store->type)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_TYPE_STR:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "STR";
|
|
|
|
len = sizeof("STR") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_BOOL:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "BOOL";
|
|
|
|
len = sizeof("BOOL") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_SHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "SHORT";
|
|
|
|
len = sizeof("SHORT") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_INT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "INT";
|
|
|
|
len = sizeof("INT") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_LONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "LONG";
|
|
|
|
len = sizeof("LONG") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_USHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "USHORT";
|
|
|
|
len = sizeof("USHORT") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_UINT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "UINT";
|
|
|
|
len = sizeof("UINT") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_ULONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "ULONG";
|
|
|
|
len = sizeof("ULONG") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_DOUBLE:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "DOUBLE";
|
|
|
|
len = sizeof("DOUBLE") - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
default:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "???";
|
|
|
|
len = sizeof("???") - 1;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
used = _ecore_getopt_help_line
|
2011-10-20 22:40:39 -07:00
|
|
|
(fp, base, total, used, _("Type: "), strlen(_("Type: ")));
|
2008-11-23 16:49:04 -08:00
|
|
|
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
|
2011-10-20 22:40:39 -07:00
|
|
|
(fp, base, total, used, ". ", sizeof(". ") - 1);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
switch (store->type)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_TYPE_STR:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = store->def.strv;
|
|
|
|
len = str ? strlen(str) : 0;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_BOOL:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = store->def.boolv ? "true" : "false";
|
|
|
|
len = strlen(str);
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_SHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%hd", store->def.shortv);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_INT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%d", store->def.intv);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_LONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%ld", store->def.longv);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_USHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%hu", store->def.ushortv);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_UINT:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%u", store->def.uintv);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_ULONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%lu", store->def.ulongv);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_DOUBLE:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = buf;
|
|
|
|
len = snprintf(buf, sizeof(buf), "%f", store->def.doublev);
|
|
|
|
if (len > sizeof(buf) - 1)
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
default:
|
2011-10-20 22:40:39 -07:00
|
|
|
str = "???";
|
|
|
|
len = sizeof("???") - 1;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
used = _ecore_getopt_help_line
|
2011-10-20 22:40:39 -07:00
|
|
|
(fp, base, total, used, _("Default: "), strlen(_("Default: ")));
|
2008-11-23 16:49:04 -08:00
|
|
|
used = _ecore_getopt_help_line(fp, base, total, used, str, len);
|
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
end:
|
2008-11-23 16:49:04 -08:00
|
|
|
return _ecore_getopt_help_line(fp, base, total, used, ".", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc_choices(FILE *fp,
|
|
|
|
const int base,
|
|
|
|
const int total,
|
|
|
|
int used,
|
|
|
|
const Ecore_Getopt_Desc *desc)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
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;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
for (; used < base; used++)
|
|
|
|
fputc(' ', fp);
|
|
|
|
|
|
|
|
used = _ecore_getopt_help_line
|
2011-10-20 22:40:39 -07:00
|
|
|
(fp, base, total, used, _("Choices: "), strlen(_("Choices: ")));
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-08-21 06:52:25 -07:00
|
|
|
for (itr = desc->action_param.choices; *itr; itr++)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
used = _ecore_getopt_help_line
|
2011-10-20 22:40:39 -07:00
|
|
|
(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);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _ecore_getopt_help_line(fp, base, total, used, ".", 1);
|
|
|
|
}
|
|
|
|
|
2014-01-20 09:10:21 -08:00
|
|
|
static int
|
|
|
|
_ecore_getopt_help_desc_categories(FILE *fp,
|
|
|
|
const int base,
|
|
|
|
const int total,
|
|
|
|
int used,
|
|
|
|
const Ecore_Getopt_Desc *desc)
|
|
|
|
{
|
|
|
|
const char sep[] = ", ";
|
|
|
|
const int seplen = sizeof(sep) - 1;
|
|
|
|
Eina_Bool cat_before = EINA_FALSE;
|
|
|
|
|
|
|
|
if (used > 0)
|
|
|
|
{
|
|
|
|
fputc('\n', fp);
|
|
|
|
used = 0;
|
|
|
|
}
|
|
|
|
for (; used < base; used++)
|
|
|
|
fputc(' ', fp);
|
|
|
|
|
|
|
|
/* do not print available categories if none available */
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
2014-01-24 02:12:41 -08:00
|
|
|
if (desc->action == ECORE_GETOPT_ACTION_CATEGORY && desc->longname)
|
|
|
|
goto hascat;
|
2014-01-20 09:10:21 -08:00
|
|
|
|
|
|
|
return _ecore_getopt_help_line(fp, base, total, used,
|
|
|
|
_("No categories available."),
|
|
|
|
strlen(_("No categories available.")));
|
|
|
|
|
|
|
|
hascat:
|
|
|
|
used = _ecore_getopt_help_line
|
|
|
|
(fp, base, total, used, _("Categories: "), strlen(_("Categories: ")));
|
|
|
|
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
{
|
|
|
|
if (desc->action != ECORE_GETOPT_ACTION_CATEGORY || !desc->longname)
|
|
|
|
continue;
|
|
|
|
if (cat_before)
|
|
|
|
used = _ecore_getopt_help_line(fp, base, total, used, sep, seplen);
|
|
|
|
used = _ecore_getopt_help_line
|
|
|
|
(fp, base, total, used, desc->longname, strlen(desc->longname));
|
|
|
|
cat_before = EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _ecore_getopt_help_line(fp, base, total, used, ".", 1);
|
|
|
|
}
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc(FILE *fp,
|
2014-01-20 09:10:21 -08:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
Ecore_Getopt_Desc_Arg_Requirement arg_req;
|
|
|
|
char metavar[32] = "ARG";
|
|
|
|
int metavarlen = 3;
|
|
|
|
int used;
|
|
|
|
|
2014-01-15 12:22:27 -08:00
|
|
|
if (desc->action == ECORE_GETOPT_ACTION_CATEGORY)
|
|
|
|
{
|
|
|
|
fprintf(fp, "\n%s\n", desc->help);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
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
|
2013-12-17 15:06:53 -08:00
|
|
|
(fp, arg_req, metavar, metavarlen, EINA_TRUE);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->shortname && desc->longname)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs(", ", fp);
|
|
|
|
used += 2;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->longname)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
int namelen = strlen(desc->longname);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs("--", fp);
|
|
|
|
fputs(desc->longname, fp);
|
|
|
|
used += 2 + namelen;
|
|
|
|
used += _ecore_getopt_help_desc_show_arg
|
2013-12-17 15:06:53 -08:00
|
|
|
(fp, arg_req, metavar, metavarlen, EINA_TRUE);
|
|
|
|
}
|
|
|
|
else if ((!desc->shortname) && (desc->metavar))
|
|
|
|
{
|
|
|
|
used += _ecore_getopt_help_desc_show_arg
|
|
|
|
(fp, arg_req, metavar, metavarlen, EINA_FALSE);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!desc->help)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (used + 3 >= helpcol)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputc('\n', fp);
|
|
|
|
used = 0;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (; used < helpcol; used++)
|
|
|
|
fputc(' ', fp);
|
|
|
|
|
|
|
|
used = _ecore_getopt_help_line
|
2011-10-20 22:40:39 -07:00
|
|
|
(fp, helpcol, cols, used, desc->help, strlen(desc->help));
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
switch (desc->action)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_ACTION_STORE:
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc_store(fp, helpcol, cols, used, desc);
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_CHOICE:
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_desc_choices(fp, helpcol, cols, used, desc);
|
|
|
|
break;
|
|
|
|
|
2014-01-20 09:10:21 -08:00
|
|
|
case ECORE_GETOPT_ACTION_HELP:
|
|
|
|
_ecore_getopt_help_desc_categories(fp, helpcol, cols, used,
|
|
|
|
parser->descs);
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
default:
|
2011-10-20 22:40:39 -07:00
|
|
|
break;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
end:
|
2008-11-23 16:49:04 -08:00
|
|
|
fputc('\n', fp);
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2008-11-23 16:49:04 -08:00
|
|
|
_ecore_getopt_desc_is_sentinel(const Ecore_Getopt_Desc *desc)
|
|
|
|
{
|
2014-01-20 09:10:21 -08:00
|
|
|
return (desc->shortname == '\0') && (!desc->longname)
|
|
|
|
&& (desc->action != ECORE_GETOPT_ACTION_CATEGORY);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_help_options(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc;
|
|
|
|
|
2009-09-01 07:15:37 -07:00
|
|
|
fputs(_("Options:\n"), fp);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
for (desc = parser->descs; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
2014-01-20 09:10:21 -08:00
|
|
|
_ecore_getopt_help_desc(fp, desc, parser);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
fputc('\n', fp);
|
2013-12-17 15:06:53 -08:00
|
|
|
|
|
|
|
if (!desc->metavar) return;
|
|
|
|
|
|
|
|
fputs(_("Positional arguments:\n"), fp);
|
|
|
|
for (; desc->metavar != NULL; desc++)
|
2014-01-20 09:10:21 -08:00
|
|
|
_ecore_getopt_help_desc(fp, desc, parser);
|
2013-12-17 15:06:53 -08:00
|
|
|
|
|
|
|
fputc('\n', fp);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2014-01-20 09:10:21 -08:00
|
|
|
static Eina_Bool
|
|
|
|
_ecore_getopt_help_prepare(const Ecore_Getopt *parser)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const char *var;
|
|
|
|
|
2014-04-17 12:08:43 -07:00
|
|
|
EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);
|
2014-01-20 09:10:21 -08:00
|
|
|
if (!parser) return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2012-03-29 01:56:10 -07:00
|
|
|
if (_argc < 1)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2012-03-29 01:56:10 -07:00
|
|
|
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;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var = getenv("COLUMNS");
|
|
|
|
if (var)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
cols = atoi(var);
|
|
|
|
if (cols < 20)
|
|
|
|
cols = 20;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
helpcol = cols / 3;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2014-01-20 09:10:21 -08:00
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API void
|
2014-01-20 09:10:21 -08:00
|
|
|
ecore_getopt_help(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser)
|
|
|
|
{
|
|
|
|
if (!_ecore_getopt_help_prepare(parser))
|
|
|
|
return;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
_ecore_getopt_help_usage(fp, parser);
|
|
|
|
_ecore_getopt_help_description(fp, parser);
|
|
|
|
_ecore_getopt_help_options(fp, parser);
|
|
|
|
}
|
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API Eina_Bool
|
2014-01-20 09:10:21 -08:00
|
|
|
ecore_getopt_help_category(FILE *fp,
|
|
|
|
const Ecore_Getopt *parser,
|
|
|
|
const char *category)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc;
|
|
|
|
Eina_Bool found = EINA_FALSE;
|
|
|
|
|
|
|
|
if (!category || !_ecore_getopt_help_prepare(parser)) return EINA_FALSE;
|
|
|
|
for (desc = parser->descs; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
{
|
|
|
|
if (desc->action != ECORE_GETOPT_ACTION_CATEGORY) continue;
|
|
|
|
if (!desc->longname || strcmp(desc->longname, category)) continue;
|
|
|
|
found = EINA_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
fprintf(stderr, _("ERROR: unknown category '%s'.\n"), category);
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ecore_getopt_help_usage(fp, parser);
|
|
|
|
_ecore_getopt_help_description(fp, parser);
|
|
|
|
|
|
|
|
fprintf(fp, "%s\n", (desc++)->help);
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
{
|
|
|
|
if (desc->action == ECORE_GETOPT_ACTION_CATEGORY) break;
|
|
|
|
_ecore_getopt_help_desc(fp, desc, parser);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
static const Ecore_Getopt_Desc *
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_find_long(const Ecore_Getopt *parser,
|
|
|
|
const char *name)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
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++)
|
|
|
|
{
|
2014-01-20 09:10:21 -08:00
|
|
|
if (!desc->longname || desc->action == ECORE_GETOPT_ACTION_CATEGORY)
|
2010-09-29 23:09:20 -07:00
|
|
|
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;
|
|
|
|
}
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Ecore_Getopt_Desc *
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_find_short(const Ecore_Getopt *parser,
|
|
|
|
char name)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
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
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_find_nonargs_base(const Ecore_Getopt *parser,
|
|
|
|
int argc,
|
|
|
|
char **argv)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2010-04-07 15:09:26 -07:00
|
|
|
char **nonargs;
|
2012-12-10 10:43:36 -08:00
|
|
|
int src, dst, used, base, abreak;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
nonargs = alloca(sizeof(char *) * argc);
|
2008-11-23 16:49:04 -08:00
|
|
|
src = 1;
|
|
|
|
dst = 1;
|
|
|
|
used = 0;
|
|
|
|
base = 0;
|
2012-12-10 10:43:36 -08:00
|
|
|
abreak = 0;
|
2008-11-23 16:49:04 -08:00
|
|
|
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];
|
|
|
|
|
2012-12-10 10:43:36 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-20 09:10:21 -08:00
|
|
|
|
2012-12-10 10:43:36 -08:00
|
|
|
|
|
|
|
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;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
found_nonarg:
|
2010-09-29 23:09:20 -07:00
|
|
|
nonargs[used] = arg;
|
|
|
|
used++;
|
|
|
|
src++;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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++;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-23 06:46:12 -08:00
|
|
|
memmove(argv + dst, nonargs, used * sizeof(char *));
|
2008-11-23 16:49:04 -08:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_desc_print_error(const Ecore_Getopt_Desc *desc,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2009-09-01 07:15:37 -07:00
|
|
|
fputs(_("ERROR: "), stderr);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
if (desc->shortname)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputc('-', stderr);
|
|
|
|
fputc(desc->shortname, stderr);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->shortname && desc->longname)
|
|
|
|
fputs(", ", stderr);
|
|
|
|
|
|
|
|
if (desc->longname)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs("--", stderr);
|
|
|
|
fputs(desc->longname, stderr);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
2013-12-17 15:06:53 -08:00
|
|
|
else if ((!desc->shortname) && (desc->metavar))
|
|
|
|
{
|
|
|
|
char metavar[32];
|
|
|
|
int metavarlen = 0;
|
|
|
|
_ecore_getopt_help_desc_setup_metavar(desc, metavar, &metavarlen,
|
|
|
|
sizeof(metavar));
|
|
|
|
fputs(metavar, stderr);
|
|
|
|
}
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
fputs(": ", stderr);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_bool(const char *str,
|
|
|
|
Eina_Bool *v)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2014-08-22 07:13:44 -07:00
|
|
|
if (!str)
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
if ((strcmp(str, "0") == 0) ||
|
|
|
|
(strcasecmp(str, "f") == 0) ||
|
|
|
|
(strcasecmp(str, "false") == 0) ||
|
|
|
|
(strcasecmp(str, "no") == 0) ||
|
2009-09-01 07:35:59 -07:00
|
|
|
(strcasecmp(str, "off") == 0)
|
|
|
|
)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2010-11-13 04:01:55 -08:00
|
|
|
*v = EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
else if ((strcmp(str, "1") == 0) ||
|
2010-09-29 23:09:20 -07:00
|
|
|
(strcasecmp(str, "t") == 0) ||
|
|
|
|
(strcasecmp(str, "true") == 0) ||
|
|
|
|
(strcasecmp(str, "yes") == 0) ||
|
|
|
|
(strcasecmp(str, "on") == 0)
|
|
|
|
)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2010-11-13 04:01:55 -08:00
|
|
|
*v = EINA_TRUE;
|
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_long(const char *str,
|
|
|
|
long int *v)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
2013-07-16 00:58:45 -07:00
|
|
|
if (!str) return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
*v = strtol(str, &endptr, 0);
|
|
|
|
return endptr > str;
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_double(const char *str,
|
|
|
|
double *v)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
2013-07-16 00:58:45 -07:00
|
|
|
if (!str) return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
*v = strtod(str, &endptr);
|
|
|
|
return endptr > str;
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_store(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *value,
|
|
|
|
const char *arg_val)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc_Store *store = &desc->action_param.store;
|
|
|
|
long int v;
|
|
|
|
double d;
|
2010-11-13 04:01:55 -08:00
|
|
|
Eina_Bool b;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
if (!value->ptrp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (store->arg_req)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO:
|
2011-10-20 22:40:39 -07:00
|
|
|
goto use_optional;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!arg_val)
|
|
|
|
goto use_optional;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES:
|
2011-10-20 22:40:39 -07:00
|
|
|
break;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (store->type)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_TYPE_STR:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->strp = (char *)arg_val;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_BOOL:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (_ecore_getopt_parse_bool(arg_val, &b))
|
|
|
|
{
|
|
|
|
*value->boolp = b;
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ecore_getopt_desc_print_error
|
|
|
|
(desc, _("unknown boolean value %s.\n"), arg_val);
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_SHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
*value->shortp = v;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_INT:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
*value->intp = v;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_LONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
*value->longp = v;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_USHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
*value->ushortp = v;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_UINT:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
*value->uintp = v;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_ULONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
*value->ulongp = v;
|
|
|
|
return EINA_TRUE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_DOUBLE:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!_ecore_getopt_parse_double(arg_val, &d))
|
|
|
|
goto error;
|
|
|
|
*value->doublep = d;
|
|
|
|
break;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
error:
|
2009-09-01 07:15:37 -07:00
|
|
|
_ecore_getopt_desc_print_error
|
|
|
|
(desc, _("invalid number format %s\n"), arg_val);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
use_optional:
|
2008-11-23 16:49:04 -08:00
|
|
|
switch (store->type)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_TYPE_STR:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->strp = (char *)store->def.strv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_BOOL:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->boolp = store->def.boolv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_SHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->shortp = store->def.shortv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_INT:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->intp = store->def.intv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_LONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->longp = store->def.longv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_USHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->ushortp = store->def.ushortv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_UINT:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->uintp = store->def.uintv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_ULONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->ulongp = store->def.ulongv;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_DOUBLE:
|
2011-10-20 22:40:39 -07:00
|
|
|
*value->doublep = store->def.doublev;
|
|
|
|
break;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_store_const(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (!val->ptrp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*val->ptrp = (void *)desc->action_param.store_const;
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_store_true(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (!val->boolp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
2010-11-13 04:01:55 -08:00
|
|
|
*val->boolp = EINA_TRUE;
|
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_store_false(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (!val->boolp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
2010-11-13 04:01:55 -08:00
|
|
|
*val->boolp = EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_choice(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
|
|
|
const char *arg_val)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2011-10-20 22:40:39 -07:00
|
|
|
const char *const *pchoice;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2014-08-27 03:57:19 -07:00
|
|
|
if (!arg_val)
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
if (!val->strp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pchoice = desc->action_param.choices;
|
2010-08-21 06:52:25 -07:00
|
|
|
for (; *pchoice; pchoice++)
|
2008-11-23 16:49:04 -08:00
|
|
|
if (strcmp(*pchoice, arg_val) == 0)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
*val->strp = (char *)*pchoice;
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_ecore_getopt_desc_print_error
|
2009-09-01 07:15:37 -07:00
|
|
|
(desc, _("invalid choice \"%s\". Valid values are: "), arg_val);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
pchoice = desc->action_param.choices;
|
2010-08-21 06:52:25 -07:00
|
|
|
for (; *pchoice; pchoice++)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs(*pchoice, stderr);
|
|
|
|
if (pchoice[1])
|
|
|
|
fputs(", ", stderr);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fputs(".\n", stderr);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_append(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
|
|
|
const char *arg_val)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
long int v;
|
|
|
|
double d;
|
2010-11-13 04:01:55 -08:00
|
|
|
Eina_Bool b;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
if (!arg_val)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error
|
|
|
|
(desc, _("missing parameter to append.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!val->listp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (desc->action_param.append_type)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_TYPE_STR:
|
2011-10-20 22:40:39 -07:00
|
|
|
data = strdup(arg_val);
|
2010-09-29 23:09:20 -07:00
|
|
|
break;
|
2011-10-20 22:40:39 -07:00
|
|
|
|
|
|
|
case ECORE_GETOPT_TYPE_BOOL:
|
|
|
|
{
|
|
|
|
if (_ecore_getopt_parse_bool(arg_val, &b))
|
|
|
|
{
|
|
|
|
data = malloc(sizeof(Eina_Bool));
|
|
|
|
if (data)
|
|
|
|
*(Eina_Bool *)data = b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ecore_getopt_desc_print_error(desc, _("unknown boolean value %s.\n"), arg_val);
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_SHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(short));
|
|
|
|
if (data)
|
|
|
|
*(short *)data = (short)v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_INT:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(int));
|
|
|
|
if (data)
|
|
|
|
*(int *)data = (int)v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_LONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(long));
|
|
|
|
if (data)
|
|
|
|
*(long *)data = v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_USHORT:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(unsigned short));
|
|
|
|
if (data)
|
|
|
|
*(unsigned short *)data = (unsigned short)v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_UINT:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(unsigned int));
|
|
|
|
if (data)
|
|
|
|
*(unsigned int *)data = (unsigned int)v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_ULONG:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_long(arg_val, &v))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(unsigned long));
|
|
|
|
if (data)
|
|
|
|
*(unsigned long *)data = v;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_TYPE_DOUBLE:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
if (!_ecore_getopt_parse_double(arg_val, &d))
|
|
|
|
goto error;
|
|
|
|
data = malloc(sizeof(double));
|
|
|
|
if (data)
|
|
|
|
*(double *)data = d;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-02-26 14:02:00 -08:00
|
|
|
default:
|
2011-10-20 22:40:39 -07:00
|
|
|
{
|
|
|
|
_ecore_getopt_desc_print_error(desc, _("could not parse value.\n"));
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
*val->listp = eina_list_append(*val->listp, data);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
error:
|
2009-09-01 07:15:37 -07:00
|
|
|
_ecore_getopt_desc_print_error
|
|
|
|
(desc, _("invalid number format %s\n"), arg_val);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
_ecore_getopt_parse_count(const Ecore_Getopt *parser EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (!val->intp)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(*val->intp)++;
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_callback(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
|
|
|
const char *arg_val)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc_Callback *cb = &desc->action_param.callback;
|
|
|
|
|
|
|
|
switch (cb->arg_req)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO:
|
2011-10-20 22:40:39 -07:00
|
|
|
arg_val = cb->def;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_DESC_ARG_REQUIREMENT_OPTIONAL:
|
2011-10-20 22:40:39 -07:00
|
|
|
if (!arg_val)
|
|
|
|
arg_val = cb->def;
|
|
|
|
break;
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES:
|
2011-10-20 22:40:39 -07:00
|
|
|
break;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cb->arg_req != ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
if ((!arg_val) || (arg_val[0] == '\0'))
|
|
|
|
{
|
|
|
|
_ecore_getopt_desc_print_error(desc, _("missing parameter.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!val->ptrp)
|
|
|
|
{
|
2010-11-13 04:01:55 -08:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("value has no pointer set.\n"));
|
|
|
|
return EINA_FALSE;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cb->func)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("missing callback function!\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb->func(parser, desc, arg_val, (void *)cb->data, val);
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_help(const Ecore_Getopt *parser,
|
2012-11-25 01:55:32 -08:00
|
|
|
const Ecore_Getopt_Desc *desc EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
Ecore_Getopt_Value *val,
|
2014-01-20 09:10:21 -08:00
|
|
|
const char *arg_val)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (val->boolp)
|
2010-11-13 04:01:55 -08:00
|
|
|
(*val->boolp) = EINA_TRUE;
|
2014-01-20 09:10:21 -08:00
|
|
|
if (arg_val)
|
|
|
|
return ecore_getopt_help_category(stdout, parser, arg_val);
|
2008-11-23 16:49:04 -08:00
|
|
|
ecore_getopt_help(stdout, parser);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_version(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (val->boolp)
|
2010-11-13 04:01:55 -08:00
|
|
|
(*val->boolp) = EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
if (!parser->version)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("no version was defined.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
_ecore_getopt_version(stdout, parser);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_copyright(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (val->boolp)
|
2010-11-13 04:01:55 -08:00
|
|
|
(*val->boolp) = EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
if (!parser->copyright)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("no copyright was defined.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
_ecore_getopt_copyright(stdout, parser);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_license(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *val,
|
2012-11-25 01:55:32 -08:00
|
|
|
const char *arg_val EINA_UNUSED)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
if (val->boolp)
|
2010-11-13 04:01:55 -08:00
|
|
|
(*val->boolp) = EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
if (!parser->license)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
_ecore_getopt_desc_print_error(desc, _("no license was defined.\n"));
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
_ecore_getopt_license(stdout, parser);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2012-12-10 10:43:36 -08:00
|
|
|
static Eina_Bool
|
|
|
|
_ecore_getopt_parse_break(const Ecore_Getopt *parser EINA_UNUSED,
|
|
|
|
const Ecore_Getopt_Desc *desc EINA_UNUSED,
|
|
|
|
Ecore_Getopt_Value *val,
|
|
|
|
const char *arg_val EINA_UNUSED)
|
|
|
|
{
|
|
|
|
if (val->boolp)
|
|
|
|
(*val->boolp) = EINA_TRUE;
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_desc_handle(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *desc,
|
|
|
|
Ecore_Getopt_Value *value,
|
|
|
|
const char *arg_val)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
switch (desc->action)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_ACTION_STORE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_store(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_STORE_CONST:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_store_const(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_STORE_TRUE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_store_true(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_STORE_FALSE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_store_false(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_CHOICE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_choice(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_APPEND:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_append(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_COUNT:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_count(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_CALLBACK:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_callback(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_HELP:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_help(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_VERSION:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_version(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_COPYRIGHT:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_copyright(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
case ECORE_GETOPT_ACTION_LICENSE:
|
2011-10-20 22:40:39 -07:00
|
|
|
return _ecore_getopt_parse_license(parser, desc, value, arg_val);
|
|
|
|
|
2012-12-10 10:43:36 -08:00
|
|
|
case ECORE_GETOPT_ACTION_BREAK:
|
|
|
|
return _ecore_getopt_parse_break(parser, desc, value, arg_val);
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
default:
|
2011-10-20 22:40:39 -07:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_arg_long(const Ecore_Getopt *parser,
|
|
|
|
Ecore_Getopt_Value *values,
|
2012-11-25 01:55:32 -08:00
|
|
|
int argc EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
char **argv,
|
|
|
|
int *idx,
|
|
|
|
int *nonargs,
|
|
|
|
const char *arg)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc;
|
|
|
|
Ecore_Getopt_Desc_Arg_Requirement arg_req;
|
|
|
|
const char *arg_val;
|
|
|
|
int desc_idx;
|
|
|
|
Ecore_Getopt_Value *value;
|
2010-11-13 04:01:55 -08:00
|
|
|
Eina_Bool ret;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
desc = _ecore_getopt_parse_find_long(parser, arg);
|
|
|
|
if (!desc)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fprintf(stderr, _("ERROR: unknown option --%s, ignored.\n"), arg);
|
|
|
|
if (parser->strict)
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
(*idx)++;
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
(*idx)++;
|
|
|
|
|
|
|
|
arg_req = _ecore_getopt_desc_arg_requirement(desc);
|
|
|
|
if (arg_req != ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
arg_val = strchr(arg, '=');
|
|
|
|
if (arg_val)
|
|
|
|
arg_val++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((*idx < *nonargs) && (argv[*idx][0] != '-'))
|
|
|
|
{
|
|
|
|
arg_val = argv[*idx];
|
|
|
|
(*idx)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
arg_val = NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:57:47 -07:00
|
|
|
/* empty strings are still valid values */
|
|
|
|
if (!(((desc->action == ECORE_GETOPT_ACTION_STORE) &&
|
|
|
|
(desc->action_param.store.type == ECORE_GETOPT_TYPE_STR)) ||
|
|
|
|
((desc->action == ECORE_GETOPT_ACTION_APPEND) &&
|
|
|
|
(desc->action_param.append_type == ECORE_GETOPT_TYPE_STR))))
|
|
|
|
{
|
|
|
|
if (arg_val && arg_val[0] == '\0')
|
|
|
|
arg_val = NULL;
|
|
|
|
}
|
2010-09-29 23:09:20 -07:00
|
|
|
|
|
|
|
if ((!arg_val) && (arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES))
|
|
|
|
{
|
|
|
|
fprintf
|
|
|
|
(stderr, _("ERROR: option --%s requires an argument!\n"), arg);
|
|
|
|
if (parser->strict)
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
arg_val = NULL;
|
|
|
|
|
|
|
|
desc_idx = desc - parser->descs;
|
|
|
|
value = values + desc_idx;
|
2014-08-27 03:48:14 -07:00
|
|
|
ret = _ecore_getopt_desc_handle(parser, desc, value, arg_val);
|
2008-11-23 16:49:04 -08:00
|
|
|
if ((!ret) && parser->strict)
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_arg_short(const Ecore_Getopt *parser,
|
|
|
|
Ecore_Getopt_Value *values,
|
2012-11-25 01:55:32 -08:00
|
|
|
int argc EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
char **argv,
|
|
|
|
int *idx,
|
|
|
|
int *nonargs,
|
|
|
|
const char *arg)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
int run = 1;
|
|
|
|
while (run && (arg[0] != '\0'))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
int opt = arg[0];
|
|
|
|
const Ecore_Getopt_Desc *desc;
|
|
|
|
Ecore_Getopt_Desc_Arg_Requirement arg_req;
|
|
|
|
const char *arg_val;
|
|
|
|
int desc_idx;
|
|
|
|
Ecore_Getopt_Value *value;
|
2010-11-13 04:01:55 -08:00
|
|
|
Eina_Bool ret;
|
2010-09-29 23:09:20 -07:00
|
|
|
|
|
|
|
desc = _ecore_getopt_parse_find_short(parser, arg[0]);
|
|
|
|
if (!desc)
|
|
|
|
{
|
|
|
|
fprintf
|
|
|
|
(stderr, _("ERROR: unknown option -%c, ignored.\n"), arg[0]);
|
|
|
|
if (parser->strict)
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2010-09-29 23:09:20 -07:00
|
|
|
|
|
|
|
arg++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg++;
|
|
|
|
|
|
|
|
arg_req = _ecore_getopt_desc_arg_requirement(desc);
|
|
|
|
if (arg_req != ECORE_GETOPT_DESC_ARG_REQUIREMENT_NO)
|
|
|
|
{
|
|
|
|
(*idx)++;
|
|
|
|
run = 0;
|
|
|
|
|
|
|
|
if (arg[0] == '=')
|
|
|
|
arg_val = arg + 1;
|
|
|
|
else if (arg[0] != '\0')
|
|
|
|
arg_val = arg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((*idx < *nonargs) && (argv[*idx][0] != '-'))
|
|
|
|
{
|
|
|
|
arg_val = argv[*idx];
|
|
|
|
(*idx)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
arg_val = NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:57:47 -07:00
|
|
|
/* empty strings are still valid values */
|
|
|
|
if (!(((desc->action == ECORE_GETOPT_ACTION_STORE) &&
|
|
|
|
(desc->action_param.store.type == ECORE_GETOPT_TYPE_STR)) ||
|
|
|
|
((desc->action == ECORE_GETOPT_ACTION_APPEND) &&
|
|
|
|
(desc->action_param.append_type == ECORE_GETOPT_TYPE_STR))))
|
|
|
|
{
|
|
|
|
if (arg_val && arg_val[0] == '\0')
|
|
|
|
arg_val = NULL;
|
|
|
|
}
|
2010-09-29 23:09:20 -07:00
|
|
|
|
|
|
|
if ((!arg_val) &&
|
|
|
|
(arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES))
|
|
|
|
{
|
|
|
|
fprintf
|
|
|
|
(stderr, _("ERROR: option -%c requires an argument!\n"),
|
2011-10-20 22:40:39 -07:00
|
|
|
opt);
|
2010-09-29 23:09:20 -07:00
|
|
|
if (parser->strict)
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
|
|
|
return EINA_TRUE;
|
2010-09-29 23:09:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
arg_val = NULL;
|
|
|
|
|
|
|
|
desc_idx = desc - parser->descs;
|
|
|
|
value = values + desc_idx;
|
2014-08-27 03:48:43 -07:00
|
|
|
ret = _ecore_getopt_desc_handle(parser, desc, value, arg_val);
|
2010-09-29 23:09:20 -07:00
|
|
|
if ((!ret) && parser->strict)
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (run)
|
|
|
|
(*idx)++;
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
static Eina_Bool
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_arg(const Ecore_Getopt *parser,
|
|
|
|
Ecore_Getopt_Value *values,
|
|
|
|
int argc,
|
|
|
|
char **argv,
|
|
|
|
int *idx,
|
|
|
|
int *nonargs)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
char *arg = argv[*idx];
|
|
|
|
|
|
|
|
if (arg[0] != '-')
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
char **dst, **src, **src_end;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
dst = argv + *idx;
|
|
|
|
src = dst + 1;
|
|
|
|
src_end = src + *nonargs - *idx - 1;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
for (; src < src_end; src++, dst++)
|
|
|
|
*dst = *src;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
*dst = arg;
|
|
|
|
(*nonargs)--;
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg[1] == '-')
|
2010-11-13 04:01:55 -08:00
|
|
|
return _ecore_getopt_parse_arg_long(parser, values, argc, argv, idx, nonargs, arg + 2);
|
2008-11-23 16:49:04 -08:00
|
|
|
else
|
2010-11-13 04:01:55 -08:00
|
|
|
return _ecore_getopt_parse_arg_short(parser, values, argc, argv, idx, nonargs, arg + 1);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2013-12-17 15:06:53 -08:00
|
|
|
static Eina_Bool
|
|
|
|
_ecore_getopt_parse_pos(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc **p_desc,
|
|
|
|
Ecore_Getopt_Value *values,
|
|
|
|
int argc,
|
|
|
|
char **argv,
|
|
|
|
int *idx,
|
|
|
|
int *nonargs)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc = *p_desc;
|
|
|
|
Ecore_Getopt_Desc_Arg_Requirement arg_req;
|
|
|
|
char metavar[32];
|
|
|
|
int metavarlen = 0;
|
|
|
|
const char *arg_val;
|
|
|
|
int desc_idx;
|
|
|
|
Ecore_Getopt_Value *value;
|
|
|
|
Eina_Bool ret;
|
|
|
|
|
|
|
|
_ecore_getopt_help_desc_setup_metavar
|
|
|
|
(desc, metavar, &metavarlen, sizeof(metavar));
|
|
|
|
|
|
|
|
desc_idx = desc - parser->descs;
|
|
|
|
value = values + desc_idx;
|
|
|
|
|
|
|
|
arg_req = _ecore_getopt_desc_arg_requirement(desc);
|
|
|
|
if (*idx >= argc)
|
|
|
|
{
|
|
|
|
(*p_desc)++;
|
|
|
|
if (arg_req == ECORE_GETOPT_DESC_ARG_REQUIREMENT_YES)
|
|
|
|
{
|
|
|
|
/* TODO: should we consider callback here as well? */
|
|
|
|
if ((desc->action == ECORE_GETOPT_ACTION_APPEND) &&
|
|
|
|
(value->listp) && (*value->listp))
|
|
|
|
{
|
|
|
|
printf("append desc: %s (%d), value: %p\n",
|
|
|
|
desc->metavar, desc_idx, value->listp);
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
_("ERROR: missing required positional argument %s.\n"),
|
|
|
|
metavar);
|
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg_val = argv[*idx];
|
|
|
|
|
|
|
|
switch (desc->action)
|
|
|
|
{
|
|
|
|
case ECORE_GETOPT_ACTION_STORE:
|
|
|
|
ret = _ecore_getopt_parse_store(parser, desc, value, arg_val);
|
|
|
|
(*p_desc)++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ECORE_GETOPT_ACTION_CHOICE:
|
|
|
|
ret = _ecore_getopt_parse_choice(parser, desc, value, arg_val);
|
|
|
|
(*p_desc)++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ECORE_GETOPT_ACTION_APPEND:
|
|
|
|
ret = _ecore_getopt_parse_append(parser, desc, value, arg_val);
|
|
|
|
/* no changes to p_desc, we keep appending until the end */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ECORE_GETOPT_ACTION_CALLBACK:
|
|
|
|
ret = _ecore_getopt_parse_callback(parser, desc, value, arg_val);
|
|
|
|
(*p_desc)++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, _("ERROR: unsupported action type %d "
|
|
|
|
"for positional argument %s\n"),
|
|
|
|
desc->action, metavar);
|
|
|
|
(*p_desc)++;
|
|
|
|
ret = EINA_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
(*idx)++;
|
|
|
|
(*nonargs)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!ret) && parser->strict)
|
|
|
|
return EINA_FALSE;
|
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-23 16:49:04 -08:00
|
|
|
static const Ecore_Getopt_Desc *
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_find_short_other(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *orig)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc = parser->descs;
|
|
|
|
const char c = orig->shortname;
|
|
|
|
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
if (desc == orig)
|
|
|
|
return NULL;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
if (c == desc->shortname)
|
|
|
|
return desc;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Ecore_Getopt_Desc *
|
2011-10-20 22:40:39 -07:00
|
|
|
_ecore_getopt_parse_find_long_other(const Ecore_Getopt *parser,
|
|
|
|
const Ecore_Getopt_Desc *orig)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc = parser->descs;
|
|
|
|
const char *name = orig->longname;
|
|
|
|
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
if (desc == orig)
|
|
|
|
return NULL;
|
2014-01-20 09:10:21 -08:00
|
|
|
if (desc->action == ECORE_GETOPT_ACTION_CATEGORY)
|
|
|
|
continue;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
if (desc->longname && (strcmp(name, desc->longname) == 0))
|
|
|
|
return desc;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API Eina_Bool
|
2008-11-23 16:49:04 -08:00
|
|
|
ecore_getopt_parser_has_duplicates(const Ecore_Getopt *parser)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc = parser->descs;
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
{
|
2011-10-20 22:40:39 -07:00
|
|
|
if (desc->shortname)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *other;
|
|
|
|
other = _ecore_getopt_parse_find_short_other(parser, desc);
|
|
|
|
if (other)
|
|
|
|
{
|
|
|
|
_ecore_getopt_desc_print_error(desc, "short name -%c already exists.", desc->shortname);
|
|
|
|
|
|
|
|
if (other->longname)
|
|
|
|
fprintf(stderr, " Other is --%s.\n", other->longname);
|
|
|
|
else
|
|
|
|
fputc('\n', stderr);
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc->longname)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *other;
|
|
|
|
other = _ecore_getopt_parse_find_long_other(parser, desc);
|
|
|
|
if (other)
|
|
|
|
{
|
|
|
|
_ecore_getopt_desc_print_error(desc, "long name --%s already exists.", desc->longname);
|
|
|
|
|
|
|
|
if (other->shortname)
|
|
|
|
fprintf(stderr, " Other is -%c.\n", other->shortname);
|
|
|
|
else
|
|
|
|
fputc('\n', stderr);
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-01-21 23:03:04 -08:00
|
|
|
static const Ecore_Getopt_Desc *
|
2008-11-23 16:49:04 -08:00
|
|
|
_ecore_getopt_find_help(const Ecore_Getopt *parser)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc = parser->descs;
|
|
|
|
for (; !_ecore_getopt_desc_is_sentinel(desc); desc++)
|
|
|
|
if (desc->action == ECORE_GETOPT_ACTION_HELP)
|
|
|
|
return desc;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API int
|
2011-10-20 22:40:39 -07:00
|
|
|
ecore_getopt_parse(const Ecore_Getopt *parser,
|
|
|
|
Ecore_Getopt_Value *values,
|
|
|
|
int argc,
|
|
|
|
char **argv)
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
int i, nonargs;
|
|
|
|
|
|
|
|
if (!parser)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs(_("ERROR: no parser provided.\n"), stderr);
|
|
|
|
return -1;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
if (!values)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs(_("ERROR: no values provided.\n"), stderr);
|
|
|
|
return -1;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-08-21 06:52:25 -07:00
|
|
|
if ((argc < 1) || (!argv))
|
2008-11-23 16:49:04 -08:00
|
|
|
ecore_app_args_get(&argc, &argv);
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fputs(_("ERROR: no arguments provided.\n"), stderr);
|
|
|
|
return -1;
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
2010-08-21 06:52:25 -07:00
|
|
|
if (argv[0])
|
2008-11-23 16:49:04 -08:00
|
|
|
prog = argv[0];
|
|
|
|
else
|
|
|
|
prog = parser->prog;
|
|
|
|
|
|
|
|
nonargs = _ecore_getopt_parse_find_nonargs_base(parser, argc, argv);
|
|
|
|
if (nonargs < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (nonargs > argc)
|
|
|
|
nonargs = argc;
|
|
|
|
|
|
|
|
i = 1;
|
2010-11-23 06:46:12 -08:00
|
|
|
while (i < nonargs)
|
|
|
|
if (!_ecore_getopt_parse_arg(parser, values, argc, argv, &i, &nonargs))
|
|
|
|
goto error;
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
return nonargs;
|
|
|
|
|
2011-10-20 22:40:39 -07:00
|
|
|
error:
|
2008-11-23 16:49:04 -08:00
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *help;
|
2009-09-01 07:15:37 -07:00
|
|
|
fputs(_("ERROR: invalid options found."), stderr);
|
2008-11-23 16:49:04 -08:00
|
|
|
|
|
|
|
help = _ecore_getopt_find_help(parser);
|
|
|
|
if (!help)
|
2010-09-29 23:09:20 -07:00
|
|
|
fputc('\n', stderr);
|
2008-11-23 16:49:04 -08:00
|
|
|
else if (help->longname)
|
2010-09-29 23:09:20 -07:00
|
|
|
fprintf(stderr, _(" See --%s.\n"), help->longname);
|
2008-11-23 16:49:04 -08:00
|
|
|
else
|
2010-09-29 23:09:20 -07:00
|
|
|
fprintf(stderr, _(" See -%c.\n"), help->shortname);
|
2008-11-23 16:49:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API int
|
2013-12-17 15:06:53 -08:00
|
|
|
ecore_getopt_parse_positional(const Ecore_Getopt *parser,
|
|
|
|
Ecore_Getopt_Value *values,
|
|
|
|
int argc,
|
|
|
|
char **argv,
|
|
|
|
int start)
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *desc;
|
|
|
|
int nonargs;
|
|
|
|
|
|
|
|
if (!parser)
|
|
|
|
{
|
|
|
|
fputs(_("ERROR: no parser provided.\n"), stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!values)
|
|
|
|
{
|
|
|
|
fputs(_("ERROR: no values provided.\n"), stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((argc < 1) || (!argv))
|
|
|
|
ecore_app_args_get(&argc, &argv);
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
{
|
|
|
|
fputs(_("ERROR: no arguments provided.\n"), stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[0])
|
|
|
|
prog = argv[0];
|
|
|
|
else
|
|
|
|
prog = parser->prog;
|
|
|
|
|
|
|
|
if (start > argc)
|
|
|
|
start = argc;
|
|
|
|
else if (start < 1)
|
|
|
|
start = _ecore_getopt_parse_find_nonargs_base(parser, argc, argv);
|
2019-10-18 10:28:54 -07:00
|
|
|
if (start < 0) goto error;
|
2013-12-17 15:06:53 -08:00
|
|
|
|
|
|
|
nonargs = start;
|
|
|
|
for (desc = parser->descs; !_ecore_getopt_desc_is_sentinel(desc); desc++);
|
|
|
|
while (desc->metavar)
|
|
|
|
if (!_ecore_getopt_parse_pos
|
|
|
|
(parser, &desc, values, argc, argv, &start, &nonargs))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return nonargs;
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
const Ecore_Getopt_Desc *help;
|
|
|
|
fputs(_("ERROR: invalid positional arguments found."), stderr);
|
|
|
|
|
|
|
|
help = _ecore_getopt_find_help(parser);
|
|
|
|
if (!help)
|
|
|
|
fputc('\n', stderr);
|
|
|
|
else if (help->longname)
|
|
|
|
fprintf(stderr, _(" See --%s.\n"), help->longname);
|
|
|
|
else
|
|
|
|
fprintf(stderr, _(" See -%c.\n"), help->shortname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API Eina_List *
|
2008-11-23 16:49:04 -08:00
|
|
|
ecore_getopt_list_free(Eina_List *list)
|
|
|
|
{
|
* estickies,
* etk,
* PROTO/exalt,
* E-MODULES-EXTRA/diskio,
* E-MODULES-EXTRA/drawer,
* E-MODULES-EXTRA/penguins,
* E-MODULES-EXTRA/slideshow,
* E-MODULES-EXTRA/mail,
* E-MODULES-EXTRA/forecasts,
* E-MODULES-EXTRA/iiirk,
* E-MODULES-EXTRA/places,
* e,
* ewl,
* ecore,
* elitaire,
* entrance,
* e_dbus,
* efreet: Here we go, move from Ecore_List to Eina_List.
NOTE: This patch is huge, I did test it a lot, and I hope nothing is
broken. But if you think something change after this commit, please
contact me ASAP.
SVN revision: 39200
2009-02-25 03:03:47 -08:00
|
|
|
void *data;
|
|
|
|
|
|
|
|
EINA_LIST_FREE(list, data)
|
|
|
|
free(data);
|
2008-11-23 16:49:04 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-20 06:29:45 -08:00
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
ecore_getopt_callback_geometry_parse(const Ecore_Getopt *parser EINA_UNUSED,
|
|
|
|
const Ecore_Getopt_Desc *desc EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const char *str,
|
2012-11-25 01:55:32 -08:00
|
|
|
void *data EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
Ecore_Getopt_Value *storage)
|
2008-12-20 06:29:45 -08:00
|
|
|
{
|
|
|
|
Eina_Rectangle *v = (Eina_Rectangle *)storage->ptrp;
|
|
|
|
|
|
|
|
if (sscanf(str, "%d:%d:%d:%d", &v->x, &v->y, &v->w, &v->h) != 4)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fprintf(stderr, _("ERROR: incorrect geometry value '%s'\n"), str);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2008-12-20 06:29:45 -08:00
|
|
|
}
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2008-12-20 06:29:45 -08:00
|
|
|
}
|
2009-04-13 13:29:57 -07:00
|
|
|
|
2020-09-20 06:23:26 -07:00
|
|
|
ECORE_API Eina_Bool
|
2012-11-25 01:55:32 -08:00
|
|
|
ecore_getopt_callback_size_parse(const Ecore_Getopt *parser EINA_UNUSED,
|
|
|
|
const Ecore_Getopt_Desc *desc EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
const char *str,
|
2012-11-25 01:55:32 -08:00
|
|
|
void *data EINA_UNUSED,
|
2011-10-20 22:40:39 -07:00
|
|
|
Ecore_Getopt_Value *storage)
|
2009-04-13 13:29:57 -07:00
|
|
|
{
|
|
|
|
Eina_Rectangle *v = (Eina_Rectangle *)storage->ptrp;
|
|
|
|
|
|
|
|
if (sscanf(str, "%dx%d", &v->w, &v->h) != 2)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
fprintf(stderr, _("ERROR: incorrect size value '%s'\n"), str);
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_FALSE;
|
2009-04-13 13:29:57 -07:00
|
|
|
}
|
|
|
|
v->x = 0;
|
|
|
|
v->y = 0;
|
|
|
|
|
2010-11-13 04:01:55 -08:00
|
|
|
return EINA_TRUE;
|
2009-04-13 13:29:57 -07:00
|
|
|
}
|