Freeciv-3.2
Loading...
Searching...
No Matches
fc_cmdline.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include "fc_prehdrs.h"
19
20#include <string.h>
21
22/* utility */
23#include "fciconv.h"
24#include "fcintl.h"
25#include "mem.h"
26#include "support.h"
27
28#include "fc_cmdline.h"
29
30/* get 'struct cmdline_value_list' and related functions: */
31#define SPECLIST_TAG cmdline_value
32#define SPECLIST_TYPE char
33#include "speclist.h"
34
35#define cmdline_value_list_iterate(vallist, pvalue) \
36 TYPED_LIST_ITERATE(char *, vallist, pvalue)
37#define cmdline_value_list_iterate_end LIST_ITERATE_END
38
40
41/**********************************************************************/
51 char **argv, int *i, int argc,
52 bool gc)
53{
54 int len = strlen(option_name);
55
56 if (gc && cmdline_values == NULL) {
58 }
59
60 if (!strcmp(option_name, argv[*i])
61 || (!fc_strncmp(option_name, argv[*i], len) && argv[*i][len] == '=')
62 || !fc_strncmp(option_name + 1, argv[*i], 2)) {
63 char *opt = argv[*i] + (argv[*i][1] != '-' ? 0 : len);
64 char *ret;
65
66 if (*opt == '=') {
67 opt++;
68 } else {
69 if (*i < argc - 1) {
70 (*i)++;
71 opt = argv[*i];
72 if (strlen(opt) == 0) {
73 fc_fprintf(stderr, _("Empty argument for \"%s\".\n"), option_name);
75 }
76 } else {
77 fc_fprintf(stderr, _("Missing argument for \"%s\".\n"), option_name);
79 }
80 }
81
83
84 if (gc) {
86 }
87
88 return ret;
89 }
90
91 return NULL;
92}
93
94/**********************************************************************/
107
108/**********************************************************************/
112bool is_option(const char *option_name, char *option)
113{
114 return (!strcmp(option_name, option)
115 || !fc_strncmp(option_name + 1, option, 2));
116}
117
118/**********************************************************************/
122static size_t fc_strcspn(const char *s, const char *reject)
123{
125 size_t i, len = strlen(s);
126
127 for (i = 0; i < len; i++) {
128 if (s[i] == '"' && !in_single_quotes) {
130 } else if (s[i] == '\'' && !in_double_quotes) {
132 }
133
135 continue;
136 }
137
138 if (strchr(reject, s[i])) {
139 break;
140 }
141 }
142
143 return i;
144}
145
146/**********************************************************************/
166int get_tokens(const char *str, char **tokens, size_t num_tokens,
167 const char *delimiterset)
168{
169 unsigned int token;
170
171 fc_assert_ret_val(NULL != str, -1);
172
173 for (token = 0; token < num_tokens && *str != '\0'; token++) {
174 size_t len, padlength = 0;
175
176 /* skip leading delimiters */
178
180
181 /* strip start/end quotes if they exist */
182 if (len >= 2) {
183 if ((str[0] == '"' && str[len - 1] == '"')
184 || (str[0] == '\'' && str[len - 1] == '\'')) {
185 len -= 2;
186 padlength = 1; /* to set the string past the end quote */
187 str++;
188 }
189 }
190
191 tokens[token] = fc_malloc(len + 1);
192 (void) fc_strlcpy(tokens[token], str, len + 1); /* adds the '\0' */
193
194 str += len + padlength;
195 }
196
197 return token;
198}
199
200/**********************************************************************/
203void free_tokens(char **tokens, size_t ntokens)
204{
205 size_t i;
206
207 for (i = 0; i < ntokens; i++) {
208 if (tokens[i]) {
209 free(tokens[i]);
210 }
211 }
212}
#define str
Definition astring.c:76
char * incite_cost
Definition comments.c:75
#define cmdline_value_list_iterate(vallist, pvalue)
Definition fc_cmdline.c:35
static struct cmdline_value_list * cmdline_values
Definition fc_cmdline.c:39
void free_tokens(char **tokens, size_t ntokens)
Definition fc_cmdline.c:203
#define cmdline_value_list_iterate_end
Definition fc_cmdline.c:37
int get_tokens(const char *str, char **tokens, size_t num_tokens, const char *delimiterset)
Definition fc_cmdline.c:166
static size_t fc_strcspn(const char *s, const char *reject)
Definition fc_cmdline.c:122
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
char * get_option_malloc(const char *option_name, char **argv, int *i, int argc, bool gc)
Definition fc_cmdline.c:50
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
char * local_to_internal_string_malloc(const char *text)
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define _(String)
Definition fcintl.h:67
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_malloc(sz)
Definition mem.h:34
const char * option_name(const struct option *poption)
Definition options.c:628
int len
Definition packhand.c:127
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:791
#define FALSE
Definition support.h:47
#define fc_strncmp(_s1_, _s2_, _len_)
Definition support.h:160