2012-11-17 19:07:00 -05:00
|
|
|
/*
|
2013-02-02 18:42:45 -05:00
|
|
|
* parser.c
|
2012-11-17 19:07:00 -05:00
|
|
|
*
|
2019-01-22 05:31:45 -05:00
|
|
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
2012-11-17 19:07:00 -05:00
|
|
|
*
|
|
|
|
* This file is part of Profanity.
|
|
|
|
*
|
|
|
|
* Profanity is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Profanity is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-07-23 20:14:49 -04:00
|
|
|
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
2012-11-17 19:07:00 -05:00
|
|
|
*
|
2014-08-24 15:57:39 -04:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link the code of portions of this program with the OpenSSL library under
|
|
|
|
* certain conditions as described in each individual source file, and
|
|
|
|
* distribute linked combinations including the two.
|
|
|
|
*
|
|
|
|
* You must obey the GNU General Public License in all respects for all of the
|
|
|
|
* code used other than OpenSSL. If you modify file(s) with this exception, you
|
|
|
|
* may extend this exception to your version of the file(s), but you are not
|
|
|
|
* obligated to do so. If you do not wish to do so, delete this exception
|
|
|
|
* statement from your version. If you delete this exception statement from all
|
|
|
|
* source files in the program, then also delete it here.
|
|
|
|
*
|
2012-11-17 19:07:00 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2014-05-22 15:07:43 -04:00
|
|
|
#include "common.h"
|
|
|
|
|
2012-11-17 21:40:49 -05:00
|
|
|
/*
|
|
|
|
* Take a full line of input and return an array of strings representing
|
|
|
|
* the arguments of a command.
|
|
|
|
* If the number of arguments found is less than min, or more than max
|
|
|
|
* NULL is returned.
|
|
|
|
*
|
|
|
|
* inp - The line of input
|
|
|
|
* min - The minimum allowed number of arguments
|
2015-02-17 04:52:37 -05:00
|
|
|
* max - The maximum allowed number of arguments
|
2012-11-17 21:40:49 -05:00
|
|
|
*
|
2015-02-17 04:52:37 -05:00
|
|
|
* Returns - An NULL terminated array of strings representing the arguments
|
2012-11-17 21:40:49 -05:00
|
|
|
* of the command, or NULL if the validation fails.
|
|
|
|
*
|
|
|
|
* E.g. the following input line:
|
|
|
|
*
|
|
|
|
* /cmd arg1 arg2
|
|
|
|
*
|
|
|
|
* Will return a pointer to the following array:
|
|
|
|
*
|
|
|
|
* { "arg1", "arg2", NULL }
|
|
|
|
*
|
|
|
|
*/
|
2015-10-25 18:47:42 -04:00
|
|
|
gchar**
|
|
|
|
parse_args(const char *const inp, int min, int max, gboolean *result)
|
2012-11-17 19:07:00 -05:00
|
|
|
{
|
|
|
|
if (inp == NULL) {
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = FALSE;
|
2012-11-17 19:07:00 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-17 04:52:37 -05:00
|
|
|
// copy and strip input of leading/trailing whitespace
|
2012-11-17 19:07:00 -05:00
|
|
|
char *copy = strdup(inp);
|
|
|
|
g_strstrip(copy);
|
|
|
|
|
2013-07-11 19:46:33 -04:00
|
|
|
int inp_size = g_utf8_strlen(copy, -1);
|
2012-11-17 19:07:00 -05:00
|
|
|
gboolean in_token = FALSE;
|
2013-01-14 18:18:50 -05:00
|
|
|
gboolean in_quotes = FALSE;
|
2012-11-17 19:07:00 -05:00
|
|
|
char *token_start = ©[0];
|
|
|
|
int token_size = 0;
|
|
|
|
GSList *tokens = NULL;
|
|
|
|
|
|
|
|
// add tokens to GSList
|
|
|
|
int i;
|
2013-07-11 19:46:33 -04:00
|
|
|
for (i = 0; i < inp_size; i++) {
|
|
|
|
gchar *curr_ch = g_utf8_offset_to_pointer(copy, i);
|
|
|
|
gunichar curr_uni = g_utf8_get_char(curr_ch);
|
2013-07-13 19:14:36 -04:00
|
|
|
|
2012-11-17 19:07:00 -05:00
|
|
|
if (!in_token) {
|
2013-07-11 19:46:33 -04:00
|
|
|
if (curr_uni == ' ') {
|
2012-11-17 19:07:00 -05:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
in_token = TRUE;
|
2013-07-11 19:46:33 -04:00
|
|
|
if (curr_uni == '"') {
|
2013-01-14 18:18:50 -05:00
|
|
|
in_quotes = TRUE;
|
|
|
|
i++;
|
2013-07-13 19:14:36 -04:00
|
|
|
gchar *next_ch = g_utf8_next_char(curr_ch);
|
|
|
|
gunichar next_uni = g_utf8_get_char(next_ch);
|
|
|
|
token_start = next_ch;
|
|
|
|
token_size += g_unichar_to_utf8(next_uni, NULL);
|
|
|
|
} else {
|
|
|
|
token_start = curr_ch;
|
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
2013-01-14 18:18:50 -05:00
|
|
|
}
|
2012-11-17 19:07:00 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-14 18:18:50 -05:00
|
|
|
if (in_quotes) {
|
2013-07-11 19:46:33 -04:00
|
|
|
if (curr_uni == '"') {
|
2013-01-14 18:18:50 -05:00
|
|
|
tokens = g_slist_append(tokens, g_strndup(token_start,
|
|
|
|
token_size));
|
|
|
|
token_size = 0;
|
|
|
|
in_token = FALSE;
|
|
|
|
in_quotes = FALSE;
|
|
|
|
} else {
|
2013-07-11 19:46:33 -04:00
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
2013-01-14 18:18:50 -05:00
|
|
|
}
|
2012-11-17 19:07:00 -05:00
|
|
|
} else {
|
2013-07-11 19:46:33 -04:00
|
|
|
if (curr_uni == ' ') {
|
2013-01-14 18:18:50 -05:00
|
|
|
tokens = g_slist_append(tokens, g_strndup(token_start,
|
|
|
|
token_size));
|
|
|
|
token_size = 0;
|
|
|
|
in_token = FALSE;
|
|
|
|
} else {
|
2013-07-11 19:46:33 -04:00
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
2013-01-14 18:18:50 -05:00
|
|
|
}
|
2012-11-17 19:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 19:25:08 -05:00
|
|
|
|
2013-07-13 19:14:36 -04:00
|
|
|
if (in_token) {
|
|
|
|
tokens = g_slist_append(tokens, g_strndup(token_start, token_size));
|
|
|
|
}
|
2013-07-11 19:46:33 -04:00
|
|
|
|
2012-11-17 21:40:49 -05:00
|
|
|
int num = g_slist_length(tokens) - 1;
|
2012-11-17 19:25:08 -05:00
|
|
|
|
|
|
|
// if num args not valid return NULL
|
2012-11-17 21:40:49 -05:00
|
|
|
if ((num < min) || (num > max)) {
|
2012-11-17 19:25:08 -05:00
|
|
|
g_slist_free_full(tokens, free);
|
2012-11-25 21:20:44 -05:00
|
|
|
g_free(copy);
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = FALSE;
|
2012-11-17 19:25:08 -05:00
|
|
|
return NULL;
|
|
|
|
|
2012-11-17 19:38:37 -05:00
|
|
|
// if min allowed is 0 and 0 found, return empty char* array
|
2012-11-17 21:40:49 -05:00
|
|
|
} else if (min == 0 && num == 0) {
|
2012-11-25 21:20:44 -05:00
|
|
|
g_slist_free_full(tokens, free);
|
2012-11-17 21:40:49 -05:00
|
|
|
gchar **args = malloc((num + 1) * sizeof(*args));
|
2012-11-17 19:38:37 -05:00
|
|
|
args[0] = NULL;
|
2012-11-25 21:20:44 -05:00
|
|
|
g_free(copy);
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = TRUE;
|
2012-11-17 19:38:37 -05:00
|
|
|
return args;
|
|
|
|
|
2012-11-17 19:25:08 -05:00
|
|
|
// otherwise return args array
|
|
|
|
} else {
|
2012-11-17 21:40:49 -05:00
|
|
|
gchar **args = malloc((num + 1) * sizeof(*args));
|
2012-11-17 19:25:08 -05:00
|
|
|
GSList *token = tokens;
|
|
|
|
token = g_slist_next(token);
|
|
|
|
int arg_count = 0;
|
|
|
|
|
2015-05-04 17:57:19 -04:00
|
|
|
while (token) {
|
2012-11-17 19:25:08 -05:00
|
|
|
args[arg_count++] = strdup(token->data);
|
|
|
|
token = g_slist_next(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
args[arg_count] = NULL;
|
|
|
|
g_slist_free_full(tokens, free);
|
2012-11-25 21:20:44 -05:00
|
|
|
g_free(copy);
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = TRUE;
|
2012-11-17 19:25:08 -05:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 21:40:49 -05:00
|
|
|
/*
|
|
|
|
* Take a full line of input and return an array of strings representing
|
|
|
|
* the arguments of a command. This function handles when the last parameter
|
|
|
|
* to the command is free text e.g.
|
|
|
|
*
|
|
|
|
* /msg user@host here is a message
|
|
|
|
*
|
|
|
|
* If the number of arguments found is less than min, or more than max
|
|
|
|
* NULL is returned.
|
|
|
|
*
|
|
|
|
* inp - The line of input
|
|
|
|
* min - The minimum allowed number of arguments
|
2015-02-17 04:52:37 -05:00
|
|
|
* max - The maximum allowed number of arguments
|
2012-11-17 21:40:49 -05:00
|
|
|
*
|
2015-02-17 04:52:37 -05:00
|
|
|
* Returns - An NULL terminated array of strings representing the arguments
|
2012-11-17 21:40:49 -05:00
|
|
|
* of the command, or NULL if the validation fails.
|
|
|
|
*
|
|
|
|
* E.g. the following input line:
|
|
|
|
*
|
|
|
|
* /cmd arg1 arg2 some free text
|
|
|
|
*
|
|
|
|
* Will return a pointer to the following array:
|
|
|
|
*
|
|
|
|
* { "arg1", "arg2", "some free text", NULL }
|
|
|
|
*
|
|
|
|
*/
|
2015-10-25 18:47:42 -04:00
|
|
|
gchar**
|
|
|
|
parse_args_with_freetext(const char *const inp, int min, int max, gboolean *result)
|
2012-11-17 19:25:08 -05:00
|
|
|
{
|
|
|
|
if (inp == NULL) {
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = FALSE;
|
2012-11-17 19:25:08 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy and strip input of leading/trailing whitepsace
|
|
|
|
char *copy = strdup(inp);
|
|
|
|
g_strstrip(copy);
|
|
|
|
|
2013-07-13 19:24:57 -04:00
|
|
|
int inp_size = g_utf8_strlen(copy, -1);
|
2012-11-17 19:25:08 -05:00
|
|
|
gboolean in_token = FALSE;
|
|
|
|
gboolean in_freetext = FALSE;
|
2013-01-14 18:18:50 -05:00
|
|
|
gboolean in_quotes = FALSE;
|
2012-11-17 19:25:08 -05:00
|
|
|
char *token_start = ©[0];
|
|
|
|
int token_size = 0;
|
|
|
|
int num_tokens = 0;
|
|
|
|
GSList *tokens = NULL;
|
|
|
|
|
|
|
|
// add tokens to GSList
|
|
|
|
int i;
|
2013-07-13 19:24:57 -04:00
|
|
|
for (i = 0; i < inp_size; i++) {
|
|
|
|
gchar *curr_ch = g_utf8_offset_to_pointer(copy, i);
|
|
|
|
gunichar curr_uni = g_utf8_get_char(curr_ch);
|
|
|
|
|
2012-11-17 19:25:08 -05:00
|
|
|
if (!in_token) {
|
2013-07-13 19:24:57 -04:00
|
|
|
if (curr_uni == ' ') {
|
2012-11-17 19:25:08 -05:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
in_token = TRUE;
|
|
|
|
num_tokens++;
|
2013-08-26 19:05:09 -04:00
|
|
|
if ((num_tokens == max + 1) && (curr_uni != '"')) {
|
2012-11-17 19:25:08 -05:00
|
|
|
in_freetext = TRUE;
|
2013-07-13 19:24:57 -04:00
|
|
|
} else if (curr_uni == '"') {
|
2013-01-14 18:18:50 -05:00
|
|
|
in_quotes = TRUE;
|
|
|
|
i++;
|
2013-07-13 19:24:57 -04:00
|
|
|
gchar *next_ch = g_utf8_next_char(curr_ch);
|
|
|
|
gunichar next_uni = g_utf8_get_char(next_ch);
|
|
|
|
token_start = next_ch;
|
|
|
|
token_size += g_unichar_to_utf8(next_uni, NULL);
|
2012-11-17 19:25:08 -05:00
|
|
|
}
|
2013-07-13 19:24:57 -04:00
|
|
|
if (curr_uni == '"') {
|
|
|
|
gchar *next_ch = g_utf8_next_char(curr_ch);
|
|
|
|
token_start = next_ch;
|
2013-07-09 17:34:55 -04:00
|
|
|
} else {
|
2013-07-13 19:24:57 -04:00
|
|
|
token_start = curr_ch;
|
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
2013-07-09 17:34:55 -04:00
|
|
|
}
|
2012-11-17 19:25:08 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-14 18:18:50 -05:00
|
|
|
if (in_quotes) {
|
2013-07-13 19:24:57 -04:00
|
|
|
if (curr_uni == '"') {
|
2013-01-14 18:18:50 -05:00
|
|
|
tokens = g_slist_append(tokens, g_strndup(token_start,
|
|
|
|
token_size));
|
|
|
|
token_size = 0;
|
|
|
|
in_token = FALSE;
|
|
|
|
in_quotes = FALSE;
|
|
|
|
} else {
|
2013-07-13 19:24:57 -04:00
|
|
|
if (curr_uni != '"') {
|
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
2013-07-09 17:34:55 -04:00
|
|
|
}
|
2013-01-14 18:18:50 -05:00
|
|
|
}
|
2012-11-17 19:25:08 -05:00
|
|
|
} else {
|
2013-07-20 16:02:07 -04:00
|
|
|
if (in_freetext) {
|
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
|
|
|
} else if (curr_uni == ' ') {
|
2013-01-14 18:18:50 -05:00
|
|
|
tokens = g_slist_append(tokens, g_strndup(token_start,
|
|
|
|
token_size));
|
|
|
|
token_size = 0;
|
|
|
|
in_token = FALSE;
|
2013-07-20 16:02:07 -04:00
|
|
|
} else if (curr_uni != '"') {
|
|
|
|
token_size += g_unichar_to_utf8(curr_uni, NULL);
|
2013-01-14 18:18:50 -05:00
|
|
|
}
|
2012-11-17 19:25:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 19:24:57 -04:00
|
|
|
if (in_token) {
|
|
|
|
tokens = g_slist_append(tokens, g_strndup(token_start, token_size));
|
|
|
|
}
|
|
|
|
|
2013-09-22 20:19:54 -04:00
|
|
|
free(copy);
|
|
|
|
|
2012-11-17 21:40:49 -05:00
|
|
|
int num = g_slist_length(tokens) - 1;
|
2012-11-17 19:07:00 -05:00
|
|
|
|
|
|
|
// if num args not valid return NULL
|
2012-11-17 21:40:49 -05:00
|
|
|
if ((num < min) || (num > max)) {
|
2012-11-17 19:07:00 -05:00
|
|
|
g_slist_free_full(tokens, free);
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = FALSE;
|
2012-11-17 19:07:00 -05:00
|
|
|
return NULL;
|
|
|
|
|
2012-11-17 19:38:37 -05:00
|
|
|
// if min allowed is 0 and 0 found, return empty char* array
|
2012-11-17 21:40:49 -05:00
|
|
|
} else if (min == 0 && num == 0) {
|
2014-06-17 17:30:51 -04:00
|
|
|
g_slist_free_full(tokens, free);
|
2012-11-17 21:40:49 -05:00
|
|
|
gchar **args = malloc((num + 1) * sizeof(*args));
|
2012-11-17 19:38:37 -05:00
|
|
|
args[0] = NULL;
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = TRUE;
|
2012-11-17 19:38:37 -05:00
|
|
|
return args;
|
|
|
|
|
2012-11-17 19:07:00 -05:00
|
|
|
// otherwise return args array
|
|
|
|
} else {
|
2012-11-17 21:40:49 -05:00
|
|
|
gchar **args = malloc((num + 1) * sizeof(*args));
|
2012-11-17 19:07:00 -05:00
|
|
|
GSList *token = tokens;
|
|
|
|
token = g_slist_next(token);
|
|
|
|
int arg_count = 0;
|
|
|
|
|
2015-05-04 17:57:19 -04:00
|
|
|
while (token) {
|
2012-11-17 19:07:00 -05:00
|
|
|
args[arg_count++] = strdup(token->data);
|
|
|
|
token = g_slist_next(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
args[arg_count] = NULL;
|
|
|
|
g_slist_free_full(tokens, free);
|
2014-04-09 16:31:43 -04:00
|
|
|
*result = TRUE;
|
2012-11-17 19:07:00 -05:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 17:57:35 -04:00
|
|
|
|
|
|
|
int
|
2015-10-25 18:47:42 -04:00
|
|
|
count_tokens(const char *const string)
|
2013-07-11 17:57:35 -04:00
|
|
|
{
|
2013-07-13 20:00:11 -04:00
|
|
|
int length = g_utf8_strlen(string, -1);
|
|
|
|
gboolean in_quotes = FALSE;
|
2013-07-11 17:57:35 -04:00
|
|
|
int num_tokens = 0;
|
2013-07-13 20:00:11 -04:00
|
|
|
int i = 0;
|
2013-07-11 17:57:35 -04:00
|
|
|
|
2013-07-13 20:00:11 -04:00
|
|
|
// include first token
|
|
|
|
num_tokens++;
|
2013-07-11 17:57:35 -04:00
|
|
|
|
2013-07-13 20:00:11 -04:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
gchar *curr_ch = g_utf8_offset_to_pointer(string, i);
|
|
|
|
gunichar curr_uni = g_utf8_get_char(curr_ch);
|
2013-07-11 17:57:35 -04:00
|
|
|
|
2013-07-13 20:00:11 -04:00
|
|
|
if (curr_uni == ' ') {
|
|
|
|
if (!in_quotes) {
|
|
|
|
num_tokens++;
|
|
|
|
}
|
|
|
|
} else if (curr_uni == '"') {
|
|
|
|
if (in_quotes) {
|
|
|
|
in_quotes = FALSE;
|
|
|
|
} else {
|
|
|
|
in_quotes = TRUE;
|
2013-07-11 17:57:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_tokens;
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
char*
|
|
|
|
get_start(const char *const string, int tokens)
|
2013-07-11 17:57:35 -04:00
|
|
|
{
|
2013-07-13 20:00:11 -04:00
|
|
|
GString *result = g_string_new("");
|
|
|
|
int length = g_utf8_strlen(string, -1);
|
|
|
|
gboolean in_quotes = FALSE;
|
2013-07-11 17:57:35 -04:00
|
|
|
char *result_str = NULL;
|
|
|
|
int num_tokens = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// include first token
|
|
|
|
num_tokens++;
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
2013-07-13 20:00:11 -04:00
|
|
|
gchar *curr_ch = g_utf8_offset_to_pointer(string, i);
|
|
|
|
gunichar curr_uni = g_utf8_get_char(curr_ch);
|
|
|
|
|
2013-07-11 17:57:35 -04:00
|
|
|
if (num_tokens < tokens) {
|
2013-07-13 20:00:11 -04:00
|
|
|
gchar *uni_char = malloc(7);
|
|
|
|
int len = g_unichar_to_utf8(curr_uni, uni_char);
|
|
|
|
uni_char[len] = '\0';
|
|
|
|
g_string_append(result, uni_char);
|
2019-10-06 11:42:44 -04:00
|
|
|
free(uni_char);
|
2013-07-11 17:57:35 -04:00
|
|
|
}
|
2013-07-13 20:00:11 -04:00
|
|
|
if (curr_uni == ' ') {
|
2013-07-11 17:57:35 -04:00
|
|
|
if (!in_quotes) {
|
|
|
|
num_tokens++;
|
|
|
|
}
|
2013-07-13 20:00:11 -04:00
|
|
|
} else if (curr_uni == '"') {
|
2013-07-11 17:57:35 -04:00
|
|
|
if (in_quotes) {
|
|
|
|
in_quotes = FALSE;
|
|
|
|
} else {
|
|
|
|
in_quotes = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result_str = result->str;
|
|
|
|
g_string_free(result, FALSE);
|
|
|
|
|
|
|
|
return result_str;
|
|
|
|
}
|
2014-04-14 17:48:18 -04:00
|
|
|
|
2015-10-25 18:47:42 -04:00
|
|
|
GHashTable*
|
2014-04-14 19:27:55 -04:00
|
|
|
parse_options(gchar **args, gchar **opt_keys, gboolean *res)
|
2014-04-14 17:48:18 -04:00
|
|
|
{
|
2014-04-14 19:13:32 -04:00
|
|
|
GList *keys = NULL;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < g_strv_length(opt_keys); i++) {
|
|
|
|
keys = g_list_append(keys, opt_keys[i]);
|
|
|
|
}
|
|
|
|
|
2014-04-14 17:48:18 -04:00
|
|
|
GHashTable *options = NULL;
|
|
|
|
|
|
|
|
// no options found, success
|
2014-04-14 19:27:55 -04:00
|
|
|
if (args[0] == NULL) {
|
2014-04-14 17:48:18 -04:00
|
|
|
options = g_hash_table_new(g_str_hash, g_str_equal);
|
|
|
|
*res = TRUE;
|
2014-04-14 19:13:32 -04:00
|
|
|
g_list_free(keys);
|
2014-04-14 17:48:18 -04:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2014-04-14 18:01:57 -04:00
|
|
|
// validate options
|
2014-04-14 17:48:18 -04:00
|
|
|
int curr;
|
2014-04-14 18:01:57 -04:00
|
|
|
GList *found_keys = NULL;
|
2014-04-14 19:27:55 -04:00
|
|
|
for (curr = 0; curr < g_strv_length(args); curr+= 2) {
|
2014-04-14 18:01:57 -04:00
|
|
|
// check if option valid
|
2014-04-14 18:36:00 -04:00
|
|
|
if (g_list_find_custom(keys, args[curr], (GCompareFunc)g_strcmp0) == NULL) {
|
2014-04-14 17:48:18 -04:00
|
|
|
*res = FALSE;
|
2019-10-06 12:12:50 -04:00
|
|
|
g_list_free(found_keys);
|
2014-04-14 19:13:32 -04:00
|
|
|
g_list_free(keys);
|
2014-04-14 17:48:18 -04:00
|
|
|
return options;
|
|
|
|
}
|
2014-04-14 18:01:57 -04:00
|
|
|
|
|
|
|
// check if duplicate
|
2015-05-04 17:57:19 -04:00
|
|
|
if (g_list_find_custom(found_keys, args[curr], (GCompareFunc)g_strcmp0)) {
|
2014-04-14 18:01:57 -04:00
|
|
|
*res = FALSE;
|
2019-10-06 12:12:50 -04:00
|
|
|
g_list_free(found_keys);
|
2014-04-14 19:13:32 -04:00
|
|
|
g_list_free(keys);
|
2014-04-14 18:01:57 -04:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check value given
|
|
|
|
if (args[curr+1] == NULL) {
|
|
|
|
*res = FALSE;
|
2019-10-06 12:12:50 -04:00
|
|
|
g_list_free(found_keys);
|
2014-04-14 19:13:32 -04:00
|
|
|
g_list_free(keys);
|
2014-04-14 18:01:57 -04:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
found_keys = g_list_append(found_keys, args[curr]);
|
2014-04-14 17:48:18 -04:00
|
|
|
}
|
2014-04-14 18:01:57 -04:00
|
|
|
g_list_free(found_keys);
|
2014-04-14 19:13:32 -04:00
|
|
|
g_list_free(keys);
|
2014-04-14 17:48:18 -04:00
|
|
|
|
|
|
|
// create map
|
|
|
|
options = g_hash_table_new(g_str_hash, g_str_equal);
|
|
|
|
*res = TRUE;
|
2014-04-14 19:27:55 -04:00
|
|
|
for (curr = 0; curr < g_strv_length(args); curr+=2) {
|
2014-04-14 17:48:18 -04:00
|
|
|
g_hash_table_insert(options, args[curr], args[curr+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
options_destroy(GHashTable *options)
|
|
|
|
{
|
2015-05-04 17:57:19 -04:00
|
|
|
if (options) {
|
2014-04-14 17:48:18 -04:00
|
|
|
g_hash_table_destroy(options);
|
|
|
|
}
|
2014-04-25 19:36:36 -04:00
|
|
|
}
|