Freeciv-3.2: /fast/freeciv/nightly/S3_2/utility/fc_cmdline.c Source File

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
39 static struct cmdline_value_list *cmdline_values = NULL;
40
41/**********************************************************************/
50 char *get_option_malloc(const char *option_name,
51 char **argv, int *i, int argc,
52 bool gc)
53{
54 int len = strlen(option_name);
55
56 if (gc && cmdline_values == NULL) {
57 cmdline_values = cmdline_value_list_new();
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);
74 exit(EXIT_FAILURE);
75 }
76 } else {
77 fc_fprintf(stderr, _("Missing argument for \"%s\".\n"), option_name);
78 exit(EXIT_FAILURE);
79 }
80 }
81
82 ret = local_to_internal_string_malloc(opt);
83
84 if (gc) {
85 cmdline_value_list_append(cmdline_values, ret);
86 }
87
88 return ret;
89 }
90
91 return NULL;
92}
93
94/**********************************************************************/
97 void cmdline_option_values_free(void)
98{
99 if (cmdline_values != NULL) {
100 cmdline_value_list_iterate(cmdline_values, pval) {
101 free(pval);
102 } cmdline_value_list_iterate_end;
103
104 cmdline_value_list_destroy(cmdline_values);
105 }
106}
107
108/**********************************************************************/
112 bool 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/**********************************************************************/
122 static size_t fc_strcspn(const char *s, const char *reject)
123{
124 bool in_single_quotes = FALSE, in_double_quotes = FALSE;
125 size_t i, len = strlen(s);
126
127 for (i = 0; i < len; i++) {
128 if (s[i] == '"' && !in_single_quotes) {
129 in_double_quotes = !in_double_quotes;
130 } else if (s[i] == '\'' && !in_double_quotes) {
131 in_single_quotes = !in_single_quotes;
132 }
133
134 if (in_single_quotes || in_double_quotes) {
135 continue;
136 }
137
138 if (strchr(reject, s[i])) {
139 break;
140 }
141 }
142
143 return i;
144}
145
146/**********************************************************************/
166 int 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 */
177 str += strspn(str, delimiterset);
178
179 len = fc_strcspn(str, delimiterset);
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/**********************************************************************/
203 void 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}
str
#define str
Definition astring.c:76
incite_cost
char * incite_cost
Definition comments.c:74
cmdline_value_list_iterate
#define cmdline_value_list_iterate(vallist, pvalue)
Definition fc_cmdline.c:35
cmdline_values
static struct cmdline_value_list * cmdline_values
Definition fc_cmdline.c:39
free_tokens
void free_tokens(char **tokens, size_t ntokens)
Definition fc_cmdline.c:203
cmdline_value_list_iterate_end
#define cmdline_value_list_iterate_end
Definition fc_cmdline.c:37
get_tokens
int get_tokens(const char *str, char **tokens, size_t num_tokens, const char *delimiterset)
Definition fc_cmdline.c:166
fc_strcspn
static size_t fc_strcspn(const char *s, const char *reject)
Definition fc_cmdline.c:122
is_option
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
get_option_malloc
char * get_option_malloc(const char *option_name, char **argv, int *i, int argc, bool gc)
Definition fc_cmdline.c:50
cmdline_option_values_free
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
fc_cmdline.h
fc_prehdrs.h
fciconv.h
local_to_internal_string_malloc
char * local_to_internal_string_malloc(const char *text)
fc_fprintf
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
fcintl.h
_
#define _(String)
Definition fcintl.h:67
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
option_name
const char * option_name(const struct option *poption)
Definition options.c:627
len
int len
Definition packhand.c:127
speclist.h
option
Definition options.c:478
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:791
support.h
FALSE
#define FALSE
Definition support.h:47
fc_strncmp
#define fc_strncmp(_s1_, _s2_, _len_)
Definition support.h:154