2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2013-01-01 00:37:37 +00:00
|
|
|
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
|
2011-11-30 10:48:53 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-11-30 10:48:53 +00:00
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-11-30 10:48:53 +00:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-11-30 10:48:53 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-04-21 21:33:35 +00:00
|
|
|
#include "getopt_rarch.h"
|
2012-03-16 23:56:16 +00:00
|
|
|
#include "strl.h"
|
|
|
|
#include "posix_string.h"
|
2011-11-30 10:48:53 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_GETOPT_LONG
|
|
|
|
|
|
|
|
#include <string.h>
|
2012-03-16 22:31:26 +00:00
|
|
|
#include "../boolean.h"
|
2011-12-02 21:56:11 +00:00
|
|
|
#include <stddef.h>
|
2011-12-24 23:59:46 +00:00
|
|
|
#include <stdlib.h>
|
2012-03-17 21:51:06 +00:00
|
|
|
#ifdef _MSC_VER
|
2012-03-16 22:31:26 +00:00
|
|
|
#include "../msvc/msvc_compat.h"
|
2012-03-17 21:51:06 +00:00
|
|
|
#endif
|
2012-03-16 22:31:26 +00:00
|
|
|
#include "../general.h"
|
2011-11-30 10:48:53 +00: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 21:56:11 +00:00
|
|
|
static int find_short_index(char * const *argv)
|
2011-11-30 10:48:53 +00:00
|
|
|
{
|
|
|
|
for (int index = 0; argv[index]; index++)
|
|
|
|
{
|
|
|
|
if (is_short_option(argv[index]))
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-02 21:56:11 +00:00
|
|
|
static int find_long_index(char * const *argv)
|
2011-11-30 10:48:53 +00:00
|
|
|
{
|
|
|
|
for (int index = 0; argv[index]; index++)
|
|
|
|
{
|
|
|
|
if (is_long_option(argv[index]))
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-02 21:56:11 +00:00
|
|
|
static int parse_short(const char *optstring, char * const *argv)
|
2011-11-30 10:48:53 +00:00
|
|
|
{
|
|
|
|
char arg = argv[0][1];
|
|
|
|
if (arg == ':')
|
|
|
|
return '?';
|
|
|
|
|
|
|
|
const char *opt = strchr(optstring, arg);
|
|
|
|
if (!opt)
|
|
|
|
return '?';
|
|
|
|
|
2012-01-21 15:27:51 +00:00
|
|
|
bool extra_opt = argv[0][2];
|
2011-11-30 10:48:53 +00:00
|
|
|
bool takes_arg = opt[1] == ':';
|
2012-01-21 15:27:51 +00: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).
|
|
|
|
bool embedded_arg = extra_opt && takes_arg;
|
2011-11-30 10:48:53 +00:00
|
|
|
|
|
|
|
if (takes_arg)
|
|
|
|
{
|
2012-01-21 15:27:51 +00:00
|
|
|
if (embedded_arg)
|
|
|
|
{
|
|
|
|
optarg = argv[0] + 2;
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
optarg = argv[1];
|
|
|
|
optind += 2;
|
|
|
|
}
|
|
|
|
|
2011-11-30 10:48:53 +00:00
|
|
|
return optarg ? opt[0] : '?';
|
|
|
|
}
|
2012-01-21 15:27:51 +00:00
|
|
|
else if (embedded_arg) // If we see additional characters, and they don't take arguments, this means we have multiple flags in one.
|
2011-11-30 10:48:53 +00:00
|
|
|
{
|
|
|
|
memmove(&argv[0][1], &argv[0][2], strlen(&argv[0][2]) + 1);
|
|
|
|
return opt[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
optind++;
|
|
|
|
return opt[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 21:56:11 +00:00
|
|
|
static int parse_long(const struct option *longopts, char * const *argv)
|
2011-11-30 10:48:53 +00:00
|
|
|
{
|
|
|
|
const struct option *opt = NULL;
|
|
|
|
for (size_t indice = 0; longopts[indice].name; indice++)
|
|
|
|
{
|
|
|
|
if (strcmp(longopts[indice].name, &argv[0][2]) == 0)
|
|
|
|
{
|
|
|
|
opt = &longopts[indice];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!opt)
|
|
|
|
return '?';
|
|
|
|
|
|
|
|
// getopt_long has an "optional" arg, but we don't bother with that.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return opt->val;
|
|
|
|
}
|
|
|
|
|
2011-12-02 21:56:11 +00:00
|
|
|
static void shuffle_block(char **begin, char **last, char **end)
|
|
|
|
{
|
|
|
|
ptrdiff_t len = last - begin;
|
2011-12-24 23:59:46 +00:00
|
|
|
const char **tmp = (const char**)calloc(len, sizeof(const char*));
|
2012-04-21 21:25:32 +00:00
|
|
|
rarch_assert(tmp);
|
2011-12-24 23:59:46 +00:00
|
|
|
|
2013-04-12 13:37:52 +00: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-24 23:59:46 +00:00
|
|
|
|
|
|
|
free(tmp);
|
2011-12-02 21:56:11 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 10:48:53 +00:00
|
|
|
int getopt_long(int argc, char *argv[],
|
|
|
|
const char *optstring, const struct option *longopts, int *longindex)
|
|
|
|
{
|
|
|
|
(void)longindex;
|
|
|
|
|
|
|
|
if (optind == 0)
|
|
|
|
optind = 1;
|
|
|
|
|
2013-05-03 23:03:46 +00:00
|
|
|
if (argc == 1)
|
|
|
|
return -1;
|
|
|
|
|
2011-12-02 21:56:11 +00:00
|
|
|
int short_index = find_short_index(&argv[optind]);
|
|
|
|
int long_index = find_long_index(&argv[optind]);
|
2011-11-30 10:48:53 +00:00
|
|
|
|
|
|
|
// We're done here.
|
|
|
|
if (short_index == -1 && long_index == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Reorder argv so that non-options come last.
|
|
|
|
// Non-POSIXy, but that's what getopt does by default.
|
|
|
|
if ((short_index > 0) && ((short_index < long_index) || (long_index == -1)))
|
|
|
|
{
|
2011-12-02 21:56:11 +00:00
|
|
|
shuffle_block(&argv[optind], &argv[optind + short_index], &argv[argc]);
|
2011-11-30 10:48:53 +00:00
|
|
|
short_index = 0;
|
|
|
|
}
|
|
|
|
else if ((long_index > 0) && ((long_index < short_index) || (short_index == -1)))
|
|
|
|
{
|
2011-12-02 21:56:11 +00:00
|
|
|
shuffle_block(&argv[optind], &argv[optind + long_index], &argv[argc]);
|
2011-11-30 10:48:53 +00:00
|
|
|
long_index = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
rarch_assert(short_index == 0 || long_index == 0);
|
2011-11-30 10:48:53 +00:00
|
|
|
|
|
|
|
if (short_index == 0)
|
|
|
|
return parse_short(optstring, &argv[optind]);
|
|
|
|
else if (long_index == 0)
|
|
|
|
return parse_long(longopts, &argv[optind]);
|
|
|
|
else
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-03-16 23:56:16 +00:00
|
|
|
#ifndef HAVE_STRL
|
|
|
|
|
|
|
|
// Implementation of strlcpy()/strlcat() based on OpenBSD.
|
|
|
|
|
|
|
|
size_t strlcpy(char *dest, const char *source, size_t size)
|
|
|
|
{
|
|
|
|
size_t src_size = 0;
|
|
|
|
size_t n = size;
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
while (--n && (*dest++ = *source++)) src_size++;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
if (size) *dest = '\0';
|
|
|
|
while (*source++) src_size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return src_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t strlcat(char *dest, const char *source, size_t size)
|
|
|
|
{
|
|
|
|
size_t len = strlen(dest);
|
|
|
|
dest += len;
|
|
|
|
|
|
|
|
if (len > size)
|
|
|
|
size = 0;
|
|
|
|
else
|
|
|
|
size -= len;
|
|
|
|
|
|
|
|
return len + strlcpy(dest, source, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
#undef strcasecmp
|
|
|
|
#undef strdup
|
|
|
|
#undef isblank
|
2012-06-24 00:11:22 +00:00
|
|
|
#undef strtok_r
|
2012-03-16 23:56:16 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "strl.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-05-03 22:59:59 +00:00
|
|
|
int rarch_strcasecmp__(const char *a, const char *b)
|
2012-03-16 23:56:16 +00:00
|
|
|
{
|
|
|
|
while (*a && *b)
|
|
|
|
{
|
|
|
|
int a_ = tolower(*a);
|
|
|
|
int b_ = tolower(*b);
|
|
|
|
if (a_ != b_)
|
|
|
|
return a_ - b_;
|
|
|
|
|
|
|
|
a++;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tolower(*a) - tolower(*b);
|
|
|
|
}
|
|
|
|
|
2013-05-03 22:59:59 +00:00
|
|
|
char *rarch_strdup__(const char *orig)
|
2012-03-16 23:56:16 +00:00
|
|
|
{
|
|
|
|
size_t len = strlen(orig) + 1;
|
|
|
|
char *ret = (char*)malloc(len);
|
|
|
|
if (!ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
strlcpy(ret, orig, len);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-03 22:59:59 +00:00
|
|
|
int rarch_isblank__(int c)
|
2012-03-16 23:56:16 +00:00
|
|
|
{
|
|
|
|
return (c == ' ') || (c == '\t');
|
|
|
|
}
|
|
|
|
|
2013-05-03 22:59:59 +00:00
|
|
|
char *rarch_strtok_r__(char *str, const char *delim, char **saveptr)
|
2012-06-24 00:11:22 +00:00
|
|
|
{
|
|
|
|
if (!saveptr || !delim)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
*saveptr = str;
|
|
|
|
|
|
|
|
char *first = NULL;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
first = *saveptr;
|
|
|
|
while (*first && strchr(delim, *first))
|
|
|
|
*first++ = '\0';
|
|
|
|
|
|
|
|
if (*first == '\0')
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *ptr = first + 1;
|
|
|
|
|
|
|
|
while (*ptr && !strchr(delim, *ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
*saveptr = ptr + (*ptr ? 1 : 0);
|
|
|
|
*ptr = '\0';
|
|
|
|
} while (strlen(first) == 0);
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2012-03-16 23:56:16 +00:00
|
|
|
#endif
|
|
|
|
|