RetroArch/libretro-common/compat/compat_getopt.c

220 lines
5.4 KiB
C
Raw Normal View History

2015-01-07 18:31:19 +01:00
/* Copyright (C) 2010-2015 The RetroArch team
2011-11-30 11:48:53 +01:00
*
2014-10-21 06:15:19 +02:00
* ---------------------------------------------------------------------------------------
2015-10-26 02:41:41 +01:00
* The following license statement only applies to this file (compat_getopt.c).
2014-10-21 06:15:19 +02:00
* ---------------------------------------------------------------------------------------
2011-11-30 11:48:53 +01:00
*
2014-10-21 06:15:19 +02:00
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2011-11-30 11:48:53 +01:00
*/
2015-11-23 20:26:32 +01:00
#include <stdio.h>
#include <ctype.h>
2011-11-30 11:48:53 +01:00
#include <string.h>
#include <boolean.h>
2011-12-02 22:56:11 +01:00
#include <stddef.h>
2011-12-25 00:59:46 +01:00
#include <stdlib.h>
#include <retro_miscellaneous.h>
2011-11-30 11:48:53 +01:00
2015-09-01 12:04:08 +02:00
#include <compat/getopt.h>
#include <compat/strl.h>
#include <compat/strcasestr.h>
#include <compat/posix_string.h>
#include <retro_assert.h>
2011-11-30 11:48:53 +01:00
char *optarg;
int optind, opterr, optopt;
static bool is_short_option(const char *str)
{
return str[0] == '-' && str[1] != '-';
}
static bool is_long_option(const char *str)
{
return str[0] == '-' && str[1] == '-';
}
2011-12-02 22:56:11 +01:00
static int find_short_index(char * const *argv)
2011-11-30 11:48:53 +01:00
{
2014-10-21 00:11:50 +02:00
int idx;
for (idx = 0; argv[idx]; idx++)
2011-11-30 11:48:53 +01:00
{
2014-10-21 00:11:50 +02:00
if (is_short_option(argv[idx]))
return idx;
2011-11-30 11:48:53 +01:00
}
return -1;
}
2011-12-02 22:56:11 +01:00
static int find_long_index(char * const *argv)
2011-11-30 11:48:53 +01:00
{
2014-10-21 00:11:50 +02:00
int idx;
for (idx = 0; argv[idx]; idx++)
2011-11-30 11:48:53 +01:00
{
2014-10-21 00:11:50 +02:00
if (is_long_option(argv[idx]))
return idx;
2011-11-30 11:48:53 +01:00
}
return -1;
}
2011-12-02 22:56:11 +01:00
static int parse_short(const char *optstring, char * const *argv)
2011-11-30 11:48:53 +01:00
{
2014-10-21 08:13:50 +02:00
bool extra_opt, takes_arg, embedded_arg;
const char *opt = NULL;
2015-06-13 00:44:47 +02:00
char arg = argv[0][1];
2011-11-30 11:48:53 +01:00
if (arg == ':')
return '?';
2014-10-21 08:13:50 +02:00
opt = strchr(optstring, arg);
2011-11-30 11:48:53 +01:00
if (!opt)
return '?';
2014-10-21 08:13:50 +02:00
extra_opt = argv[0][2];
takes_arg = opt[1] == ':';
2014-09-09 05:24:32 +02:00
/* If we take an argument, and we see additional characters,
* this is in fact the argument (i.e. -cfoo is same as -c foo). */
2014-10-21 08:13:50 +02:00
embedded_arg = extra_opt && takes_arg;
2011-11-30 11:48:53 +01:00
if (takes_arg)
{
if (embedded_arg)
{
optarg = argv[0] + 2;
optind++;
}
else
{
optarg = argv[1];
optind += 2;
}
2011-11-30 11:48:53 +01:00
return optarg ? opt[0] : '?';
}
2015-10-26 02:41:41 +01:00
if (embedded_arg)
2011-11-30 11:48:53 +01:00
{
2014-09-09 05:24:32 +02:00
/* If we see additional characters,
* and they don't take arguments, this
* means we have multiple flags in one. */
2011-11-30 11:48:53 +01:00
memmove(&argv[0][1], &argv[0][2], strlen(&argv[0][2]) + 1);
return opt[0];
}
2015-10-26 02:41:41 +01:00
optind++;
return opt[0];
2011-11-30 11:48:53 +01:00
}
2011-12-02 22:56:11 +01:00
static int parse_long(const struct option *longopts, char * const *argv)
2011-11-30 11:48:53 +01:00
{
size_t indice;
2011-11-30 11:48:53 +01:00
const struct option *opt = NULL;
for (indice = 0; longopts[indice].name; indice++)
2011-11-30 11:48:53 +01:00
{
2015-06-15 05:00:39 +02:00
if (!strcmp(longopts[indice].name, &argv[0][2]))
2011-11-30 11:48:53 +01:00
{
opt = &longopts[indice];
break;
}
}
if (!opt)
return '?';
2014-09-09 05:24:32 +02:00
/* getopt_long has an "optional" arg, but we don't bother with that. */
2011-11-30 11:48:53 +01:00
if (opt->has_arg && !argv[1])
return '?';
if (opt->has_arg)
{
optarg = argv[1];
optind += 2;
}
else
optind++;
if (opt->flag)
{
*opt->flag = opt->val;
return 0;
}
2014-09-09 05:24:32 +02:00
return opt->val;
2011-11-30 11:48:53 +01:00
}
2011-12-02 22:56:11 +01:00
static void shuffle_block(char **begin, char **last, char **end)
{
2015-06-13 00:44:47 +02:00
ptrdiff_t len = last - begin;
2011-12-25 00:59:46 +01:00
const char **tmp = (const char**)calloc(len, sizeof(const char*));
2015-06-13 00:44:47 +02:00
retro_assert(tmp);
2011-12-25 00:59:46 +01:00
2013-04-12 16:37:52 +03:00
memcpy(tmp, begin, len * sizeof(const char*));
memmove(begin, last, (end - last) * sizeof(const char*));
memcpy(end - len, tmp, len * sizeof(const char*));
2011-12-25 00:59:46 +01:00
free(tmp);
2011-12-02 22:56:11 +01:00
}
2011-11-30 11:48:53 +01:00
int getopt_long(int argc, char *argv[],
const char *optstring, const struct option *longopts, int *longindex)
{
2014-10-21 08:13:50 +02:00
int short_index, long_index;
2015-06-13 00:44:47 +02:00
2011-11-30 11:48:53 +01:00
(void)longindex;
if (optind == 0)
optind = 1;
if (argc == 1)
return -1;
2014-10-21 08:13:50 +02:00
short_index = find_short_index(&argv[optind]);
long_index = find_long_index(&argv[optind]);
2011-11-30 11:48:53 +01:00
2014-09-09 05:24:32 +02:00
/* We're done here. */
2011-11-30 11:48:53 +01:00
if (short_index == -1 && long_index == -1)
return -1;
2014-09-09 05:24:32 +02:00
/* Reorder argv so that non-options come last.
* Non-POSIXy, but that's what getopt does by default. */
2011-11-30 11:48:53 +01:00
if ((short_index > 0) && ((short_index < long_index) || (long_index == -1)))
{
2011-12-02 22:56:11 +01:00
shuffle_block(&argv[optind], &argv[optind + short_index], &argv[argc]);
2011-11-30 11:48:53 +01:00
short_index = 0;
}
2014-09-09 05:24:32 +02:00
else if ((long_index > 0) && ((long_index < short_index)
|| (short_index == -1)))
2011-11-30 11:48:53 +01:00
{
2011-12-02 22:56:11 +01:00
shuffle_block(&argv[optind], &argv[optind + long_index], &argv[argc]);
2011-11-30 11:48:53 +01:00
long_index = 0;
}
retro_assert(short_index == 0 || long_index == 0);
2011-11-30 11:48:53 +01:00
if (short_index == 0)
return parse_short(optstring, &argv[optind]);
2015-10-26 02:41:41 +01:00
if (long_index == 0)
2011-11-30 11:48:53 +01:00
return parse_long(longopts, &argv[optind]);
2015-10-26 02:41:41 +01:00
return '?';
}