• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geographika / mapserver / 18183672903

02 Oct 2025 04:47AM UTC coverage: 41.556% (+0.001%) from 41.555%
18183672903

push

github

geographika
Updates following code review

53 of 101 new or added lines in 4 files covered. (52.48%)

12 existing lines in 1 file now uncovered.

62299 of 149917 relevant lines covered (41.56%)

25033.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.14
/src/mapserv-config.cpp
1
/**********************************************************************
2
 * $id$
3
 *
4
 * Project:  MapServer
5
 * Purpose:  Config File Support
6
 * Author:   Steve Lime and the MapServer team.
7
 *
8
 **********************************************************************
9
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a
12
 * copy of this software and associated documentation files (the "Software"),
13
 * to deal in the Software without restriction, including without limitation
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
 * and/or sell copies of the Software, and to permit persons to whom the
16
 * Software is furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies of this Software or works derived from this Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
 ****************************************************************************/
28
#include "mapserver.h"
29
#include "mapfile.h" // format shares a couple of tokens
30

31
#include "cpl_conv.h"
32

33
#include <string>
34

35
extern "C" int msyylex(void);
36
extern "C" void msyyrestart(FILE *);
37

38
extern "C" FILE *msyyin;
39
extern "C" int msyystate;
40
extern "C" int msyylineno;
41
extern "C" char *msyystring_buffer;
42

43
static int initConfig(configObj *config) {
2,607✔
44
  if (config == NULL) {
2,607✔
45
    msSetError(MS_MEMERR, "Config object is NULL.", "initConfig()");
×
46
    return MS_FAILURE;
×
47
  }
48

49
  if (initHashTable(&(config->env)) != MS_SUCCESS)
2,607✔
50
    return MS_FAILURE;
51
  if (initHashTable(&(config->maps)) != MS_SUCCESS)
2,607✔
52
    return MS_FAILURE;
53
  if (initHashTable(&(config->plugins)) != MS_SUCCESS)
2,607✔
54
    return MS_FAILURE;
55

56
  return MS_SUCCESS;
57
}
58

59
void msFreeConfig(configObj *config) {
2,608✔
60
  if (config == NULL)
2,608✔
61
    return;
62
  msFreeHashItems(&(config->env));
2,607✔
63
  msFreeHashItems(&(config->maps));
2,607✔
64
  msFreeHashItems(&(config->plugins));
2,607✔
65

66
  msFree(config);
2,607✔
67
}
68

69
static int loadConfig(configObj *config) {
2,606✔
70
  int token;
71

72
  if (config == NULL)
2,606✔
73
    return MS_FAILURE;
74

75
  token = msyylex();
2,606✔
76
  if (token != MS_CONFIG_SECTION) {
2,606✔
77
    msSetError(MS_IDENTERR,
1✔
78
               "First token must be CONFIG, this doesn't look like a mapserver "
79
               "config file.",
80
               "msLoadConfig()");
81
    return MS_FAILURE;
1✔
82
  }
83

84
  for (;;) {
85
    switch (msyylex()) {
7,805✔
86
    case (MS_CONFIG_SECTION_ENV): {
2,603✔
87
      if (loadHashTable(&(config->env)) != MS_SUCCESS)
2,603✔
88
        return MS_FAILURE;
89
      break;
90
    }
91
    case (MS_CONFIG_SECTION_MAPS):
20✔
92
      if (loadHashTable(&config->maps) != MS_SUCCESS)
20✔
93
        return MS_FAILURE;
94
      break;
95
    case (MS_CONFIG_SECTION_PLUGINS):
2,577✔
96
      if (loadHashTable(&config->plugins) != MS_SUCCESS)
2,577✔
97
        return MS_FAILURE;
98
      break;
99
    case (EOF):
1✔
100
      msSetError(MS_EOFERR, NULL, "msLoadConfig()");
1✔
101
      return MS_FAILURE;
1✔
102
    case (END):
2,603✔
103
      if (msyyin) {
2,603✔
104
        fclose(msyyin);
2,603✔
105
        msyyin = NULL;
2,603✔
106
      }
107
      return MS_SUCCESS;
108
      break;
109
    default:
1✔
110
      msSetError(MS_IDENTERR, "Parsing error near (%s):(line %d)",
1✔
111
                 "msLoadConfig()", msyystring_buffer, msyylineno);
112
      return MS_FAILURE;
1✔
113
    }
114
  }
115
}
116

117
static void msConfigSetConfigOption(const char *key, const char *value) {
10,374✔
118
  CPLSetConfigOption(key, value);
10,374✔
119
  if (strcasecmp(key, "PROJ_DATA") == 0 || strcasecmp(key, "PROJ_LIB") == 0) {
10,374✔
120
    msSetPROJ_DATA(value, nullptr);
×
121
  }
122
}
10,374✔
123

124
std::string msGetMapserverConfigFilePath() {
2,612✔
125

126
  const char *config_path = CPLGetConfigOption("MAPSERVER_CONFIG_FILE", NULL);
2,612✔
127
  if (config_path != nullptr) {
2,612✔
128
    return std::string(config_path);
2,612✔
129
  }
130

NEW
131
  const char *env_file = getenv("MAPSERVER_CONFIG_FILE");
×
NEW
132
  if (env_file != nullptr) {
×
133
    // get config filename from environment
NEW
134
    return std::string(env_file);
×
135
  }
136

137
  if (MAPSERVER_CONFIG_FILE[0] != '\0') {
138
    // fallback to hardcoded file name
NEW
139
    return std::string(MAPSERVER_CONFIG_FILE);
×
140
  }
141
  return std::string();
142
}
143

144
configObj *msLoadConfig(const char *ms_config_file) {
2,607✔
145
  configObj *config = NULL;
146

147
  std::string config_path;
148

149
  if (ms_config_file == NULL) {
2,607✔
150
    config_path = msGetMapserverConfigFilePath();
5,152✔
151
    if (config_path.empty()) {
2,576✔
NEW
152
      msSetError(MS_MISCERR, "No config file set.", "msLoadConfig()");
×
153
      return NULL;
154
    }
155
    ms_config_file = config_path.c_str();
156
  }
157

158
  config = (configObj *)calloc(1, sizeof(configObj));
2,607✔
159
  MS_CHECK_ALLOC(config, sizeof(configObj), NULL);
2,607✔
160

161
  if (initConfig(config) != MS_SUCCESS) {
2,607✔
162
    msFreeConfig(config);
×
163
    return NULL;
164
  }
165

166
  msAcquireLock(TLOCK_PARSER);
2,607✔
167

168
  if ((msyyin = fopen(ms_config_file, "r")) == NULL) {
2,607✔
169
    msDebug("Cannot open configuration file %s.\n", ms_config_file);
1✔
170
    msSetError(MS_IOERR,
1✔
171
               "See mapserver.org/mapfile/config.html for more information.",
172
               "msLoadConfig()");
173
    msReleaseLock(TLOCK_PARSER);
1✔
174
    msFreeConfig(config);
1✔
175
    return NULL;
176
  }
177

178
  msyystate = MS_TOKENIZE_CONFIG;
2,606✔
179
  msyylex(); // sets things up, but doesn't process any tokens
2,606✔
180

181
  msyyrestart(msyyin); // start at line 1
2,606✔
182
  msyylineno = 1;
2,606✔
183

184
  if (loadConfig(config) != MS_SUCCESS) {
2,606✔
185
    msFreeConfig(config);
3✔
186
    msReleaseLock(TLOCK_PARSER);
3✔
187
    if (msyyin) {
3✔
188
      fclose(msyyin);
3✔
189
      msyyin = NULL;
3✔
190
    }
191
    return NULL;
3✔
192
  }
193
  msReleaseLock(TLOCK_PARSER);
2,603✔
194

195
  // load all env key/values using CPLSetConfigOption() - only do this *after*
196
  // we have a good config
197
  const char *key = msFirstKeyFromHashTable(&config->env);
2,603✔
198
  if (key != NULL) {
2,603✔
199
    msConfigSetConfigOption(key, msLookupHashTable(&config->env, key));
2,602✔
200

201
    const char *last_key = key;
202
    while ((key = msNextKeyFromHashTable(&config->env, last_key)) != NULL) {
7,771✔
203
      msConfigSetConfigOption(key, msLookupHashTable(&config->env, key));
5,169✔
204
      last_key = key;
205
    }
206
  }
207

208
  // also store the path to the CONFIG file
209
  msConfigSetConfigOption("MAPSERVER_CONFIG_FILE", ms_config_file);
2,603✔
210

211
  return config;
212
}
213

214
const char *msConfigGetEnv(const configObj *config, const char *key) {
15✔
215
  if (config == NULL || key == NULL)
15✔
216
    return NULL;
217
  return msLookupHashTable(&config->env, key);
15✔
218
}
219

220
/**
221
 * Get the path to a Mapfile, from the MapServer CONFIG file using
222
 * its key. If the path is relative then the CONFIG file location
223
 * is used to determine the absolute path.
224
 * The provided pszReturnPath must be at least MS_MAXPATHLEN long.
225
 */
226
const char *msConfigGetMap(const configObj *config, const char *key,
1,869✔
227
                           char *pszReturnPath) {
228
  if (config == NULL || key == NULL || pszReturnPath == NULL)
1,869✔
229
    return NULL;
230

231
  const char *mapFile = msLookupHashTable(&config->maps, key);
1,869✔
232
  if (!mapFile)
1,869✔
233
    return NULL;
234

235
  // check for relative Mapfile paths
236
  if (CPLIsFilenameRelative(mapFile)) {
36✔
237

238
    std::string config_path = msGetMapserverConfigFilePath();
36✔
239
    if (config_path.empty()) {
36✔
240
      return NULL;
241
    }
242
    char *configPath = msGetPath(config_path.c_str());
36✔
243

244
    if (!configPath)
36✔
245
      return NULL;
246

247
    if (msBuildPath(pszReturnPath, configPath, mapFile) == NULL) {
36✔
248
      free(configPath);
×
249
      return NULL;
×
250
    }
251
    free(configPath);
36✔
252
  } else {
253
    // just copy absolute path directly
254
    strlcpy(pszReturnPath, mapFile, MS_MAXPATHLEN);
255
  }
256
  return pszReturnPath;
257
}
258

259
const char *msConfigGetPlugin(const configObj *config, const char *key) {
×
260
  if (config == NULL || key == NULL)
×
261
    return NULL;
262
  return msLookupHashTable(&config->plugins, key);
×
263
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc