2008-07-08 14:29:05 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-07-08 14:29:05 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-07-09 15:31:35 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
2008-07-08 14:29:05 +00:00
|
|
|
*/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <ctype.h> /* isspace () */
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <stdio.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdlib.h> /* exit () */
|
2008-07-08 14:29:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tr-getopt.h"
|
|
|
|
|
|
|
|
#ifndef MAX
|
2012-12-05 17:29:46 +00:00
|
|
|
#define MAX(a, b)(((a) > (b)) ? (a) : (b))
|
2008-07-08 14:29:05 +00:00
|
|
|
#endif
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
int tr_optind = 1;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
|
|
|
static const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
getArgName (const tr_option * opt)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2009-01-23 18:44:15 +00:00
|
|
|
const char * arg;
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!opt->has_arg)
|
2008-07-09 15:31:35 +00:00
|
|
|
arg = "";
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (opt->argName)
|
2008-07-09 15:31:35 +00:00
|
|
|
arg = opt->argName;
|
|
|
|
else
|
|
|
|
arg = "<args>";
|
|
|
|
|
|
|
|
return arg;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:05:18 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
get_next_line_len (const char * description, int maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
{
|
|
|
|
int end;
|
2012-12-05 17:29:46 +00:00
|
|
|
int len = strlen (description);
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (len < maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
return len;
|
|
|
|
|
|
|
|
end = maxlen < len ? maxlen : len;
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((end > 0) && !isspace (description[end]))
|
2010-06-16 14:05:18 +00:00
|
|
|
--end;
|
|
|
|
|
|
|
|
return end ? end : len;
|
|
|
|
}
|
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
getopts_usage_line (const tr_option * opt,
|
2008-09-23 19:11:04 +00:00
|
|
|
int longWidth,
|
|
|
|
int shortWidth,
|
2012-12-05 17:29:46 +00:00
|
|
|
int argWidth)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2010-12-12 16:43:19 +00:00
|
|
|
int len;
|
2008-07-08 14:29:05 +00:00
|
|
|
const char * longName = opt->longName ? opt->longName : "";
|
|
|
|
const char * shortName = opt->shortName ? opt->shortName : "";
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * arg = getArgName (opt);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-06-16 14:05:18 +00:00
|
|
|
const int d_indent = shortWidth + longWidth + argWidth + 7;
|
|
|
|
const int d_width = 80 - d_indent;
|
|
|
|
const char * d = opt->description;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
printf (" %s%-*s %s%-*s %-*s ",
|
|
|
|
(shortName && *shortName ? "-" : " "), shortWidth, shortName,
|
|
|
|
(longName && *longName ? "--" : " "), longWidth, longName,
|
|
|
|
argWidth, arg);
|
|
|
|
len = get_next_line_len (d, d_width);
|
|
|
|
printf ("%*.*s\n", len, len, d);
|
2010-06-16 14:05:18 +00:00
|
|
|
|
|
|
|
d += len;
|
2012-12-05 17:29:46 +00:00
|
|
|
while (isspace (*d)) ++d;
|
2010-06-16 14:05:18 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while ((len = get_next_line_len (d, d_width))) {
|
|
|
|
printf ("%*.*s%*.*s\n", d_indent, d_indent, "", len, len, d);
|
2010-06-16 14:05:18 +00:00
|
|
|
d += len;
|
2012-12-05 17:29:46 +00:00
|
|
|
while (isspace (*d)) ++d;
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
maxWidth (const struct tr_option * o,
|
2008-09-23 19:11:04 +00:00
|
|
|
int * longWidth,
|
|
|
|
int * shortWidth,
|
2012-12-05 17:29:46 +00:00
|
|
|
int * argWidth)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
|
|
|
const char * arg;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (o->longName)
|
|
|
|
*longWidth = MAX (*longWidth, (int)strlen (o->longName));
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (o->shortName)
|
|
|
|
*shortWidth = MAX (*shortWidth, (int)strlen (o->shortName));
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((arg = getArgName (o)))
|
|
|
|
*argWidth = MAX (*argWidth, (int)strlen (arg));
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getopt_usage (const char * progName,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * description,
|
2012-12-05 17:29:46 +00:00
|
|
|
const struct tr_option opts[])
|
2008-07-09 15:31:35 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int longWidth = 0;
|
|
|
|
int shortWidth = 0;
|
|
|
|
int argWidth = 0;
|
|
|
|
struct tr_option help;
|
2008-07-09 15:31:35 +00:00
|
|
|
const struct tr_option * o;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (o = opts; o->val; ++o)
|
|
|
|
maxWidth (o, &longWidth, &shortWidth, &argWidth);
|
2008-07-09 15:31:35 +00:00
|
|
|
|
|
|
|
help.val = -1;
|
|
|
|
help.longName = "help";
|
|
|
|
help.description = "Display this help page and exit";
|
|
|
|
help.shortName = "h";
|
|
|
|
help.has_arg = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
maxWidth (&help, &longWidth, &shortWidth, &argWidth);
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (description == NULL)
|
2008-07-09 15:31:35 +00:00
|
|
|
description = "Usage: %s [options]";
|
2012-12-05 17:29:46 +00:00
|
|
|
printf (description, progName);
|
|
|
|
printf ("\n\nOptions:\n");
|
|
|
|
getopts_usage_line (&help, longWidth, shortWidth, argWidth);
|
|
|
|
for (o = opts; o->val; ++o)
|
|
|
|
getopts_usage_line (o, longWidth, shortWidth, argWidth);
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const tr_option *
|
2012-12-05 17:29:46 +00:00
|
|
|
findOption (const tr_option * opts,
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * str,
|
2012-12-05 17:29:46 +00:00
|
|
|
const char ** setme_arg)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t matchlen = 0;
|
|
|
|
const char * arg = NULL;
|
2008-07-08 14:29:05 +00:00
|
|
|
const tr_option * o;
|
2008-07-21 19:24:35 +00:00
|
|
|
const tr_option * match = NULL;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* find the longest matching option */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (o = opts; o->val; ++o)
|
2008-07-09 19:39:40 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
size_t len = o->longName ? strlen (o->longName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2013-04-13 18:29:56 +00:00
|
|
|
if ((matchlen < len)
|
|
|
|
&& (str[0] == '-')
|
|
|
|
&& (str[1] == '-')
|
|
|
|
&& !strncmp (str+2, o->longName, len)
|
2012-12-05 17:29:46 +00:00
|
|
|
&& (str[len + 2] == '\0' || (o->has_arg && str[len + 2] == '=')))
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2008-09-23 19:11:04 +00:00
|
|
|
arg = str[len + 2] == '=' ? str + len + 3 : NULL;
|
2008-07-21 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
len = o->shortName ? strlen (o->shortName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2013-04-13 18:29:56 +00:00
|
|
|
if ((matchlen < len)
|
|
|
|
&& (str[0] == '-')
|
|
|
|
&& !strncmp (str+1, o->shortName, len)
|
2012-12-05 17:29:46 +00:00
|
|
|
&& (str[len + 1] == '\0' || o->has_arg))
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (str[len + 1])
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
|
|
|
case '\0':
|
|
|
|
arg = NULL; break;
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
arg = str + len + 2; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
arg = str + len + 1; break;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (setme_arg)
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_arg = arg;
|
|
|
|
|
|
|
|
return match;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getopt (const char * usage,
|
2008-09-23 19:11:04 +00:00
|
|
|
int argc,
|
|
|
|
const char ** argv,
|
|
|
|
const tr_option * opts,
|
2012-12-05 17:29:46 +00:00
|
|
|
const char ** setme_optarg)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
const char * arg = NULL;
|
2008-07-08 14:29:05 +00:00
|
|
|
const tr_option * o = NULL;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
*setme_optarg = NULL;
|
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
/* handle the builtin 'help' option */
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i = 1; i < argc; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!strcmp (argv[i], "-h") || !strcmp (argv[i], "--help"))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_getopt_usage (argv[0], usage, opts);
|
|
|
|
exit (0);
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
/* out of options? */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (argc == 1 || tr_optind >= argc)
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_DONE;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
o = findOption (opts, argv[tr_optind], &arg);
|
|
|
|
if (!o)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* let the user know we got an unknown option... */
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_UNK;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!o->has_arg)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* no argument needed for this option, so we're done */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (arg)
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
|
|
|
*setme_optarg = NULL;
|
2008-07-21 19:24:35 +00:00
|
|
|
++tr_optind;
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* option needed an argument, and it was embedded in this string */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (arg)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_optarg = arg;
|
|
|
|
++tr_optind;
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* throw an error if the option needed an argument but didn't get one */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (++tr_optind >= argc)
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2012-12-05 17:29:46 +00:00
|
|
|
if (findOption (opts, argv[tr_optind], NULL))
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|