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

geographika / mapserver / 17823520225

18 Sep 2025 08:57AM UTC coverage: 41.442% (-0.02%) from 41.466%
17823520225

push

github

geographika
Use full path for CONFIG test

62112 of 149877 relevant lines covered (41.44%)

25354.57 hits per line

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

89.25
/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,597✔
44
  if (config == NULL) {
2,597✔
45
    msSetError(MS_MEMERR, "Config object is NULL.", "initConfig()");
×
46
    return MS_FAILURE;
×
47
  }
48

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

56
  return MS_SUCCESS;
57
}
58

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

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

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

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

75
  token = msyylex();
2,596✔
76
  if (token != MS_CONFIG_SECTION) {
2,596✔
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,775✔
86
    case (MS_CONFIG_SECTION_ENV): {
2,593✔
87
      if (loadHashTable(&(config->env)) != MS_SUCCESS)
2,593✔
88
        return MS_FAILURE;
89
      break;
90
    }
91
    case (MS_CONFIG_SECTION_MAPS):
10✔
92
      if (loadHashTable(&config->maps) != MS_SUCCESS)
10✔
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,593✔
103
      if (msyyin) {
2,593✔
104
        fclose(msyyin);
2,593✔
105
        msyyin = NULL;
2,593✔
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,345✔
118
  CPLSetConfigOption(key, value);
10,345✔
119
  if (strcasecmp(key, "PROJ_DATA") == 0 || strcasecmp(key, "PROJ_LIB") == 0) {
10,345✔
120
    msSetPROJ_DATA(value, nullptr);
×
121
  }
122
}
10,345✔
123

124
configObj *msLoadConfig(const char *ms_config_file) {
2,597✔
125
  configObj *config = NULL;
126

127
  if (ms_config_file == NULL) {
2,597✔
128
    // get config filename from environment
129
    ms_config_file = getenv("MAPSERVER_CONFIG_FILE");
2,576✔
130
  }
131

132
  if (ms_config_file == NULL && MAPSERVER_CONFIG_FILE[0] != '\0') {
2,576✔
133
    // Fallback to hardcoded file name
134
    ms_config_file = MAPSERVER_CONFIG_FILE;
135
  }
136

137
  if (ms_config_file == NULL) {
138
    msSetError(MS_MISCERR, "No config file set.", "msLoadConfig()");
139
    return NULL;
140
  }
141

142
  config = (configObj *)calloc(1, sizeof(configObj));
2,597✔
143
  MS_CHECK_ALLOC(config, sizeof(configObj), NULL);
2,597✔
144

145
  if (initConfig(config) != MS_SUCCESS) {
2,597✔
146
    msFreeConfig(config);
×
147
    return NULL;
×
148
  }
149

150
  msAcquireLock(TLOCK_PARSER);
2,597✔
151

152
  if ((msyyin = fopen(ms_config_file, "r")) == NULL) {
2,597✔
153
    msDebug("Cannot open configuration file %s.\n", ms_config_file);
1✔
154
    msSetError(MS_IOERR,
1✔
155
               "See mapserver.org/mapfile/config.html for more information.",
156
               "msLoadConfig()");
157
    msReleaseLock(TLOCK_PARSER);
1✔
158
    msFreeConfig(config);
1✔
159
    return NULL;
1✔
160
  }
161

162
  msyystate = MS_TOKENIZE_CONFIG;
2,596✔
163
  msyylex(); // sets things up, but doesn't process any tokens
2,596✔
164

165
  msyyrestart(msyyin); // start at line 1
2,596✔
166
  msyylineno = 1;
2,596✔
167

168
  if (loadConfig(config) != MS_SUCCESS) {
2,596✔
169
    msFreeConfig(config);
3✔
170
    msReleaseLock(TLOCK_PARSER);
3✔
171
    if (msyyin) {
3✔
172
      fclose(msyyin);
3✔
173
      msyyin = NULL;
3✔
174
    }
175
    return NULL;
3✔
176
  }
177
  msReleaseLock(TLOCK_PARSER);
2,593✔
178

179
  // load all env key/values using CPLSetConfigOption() - only do this *after*
180
  // we have a good config
181
  const char *key = msFirstKeyFromHashTable(&config->env);
2,593✔
182
  if (key != NULL) {
2,593✔
183
    msConfigSetConfigOption(key, msLookupHashTable(&config->env, key));
2,592✔
184

185
    const char *last_key = key;
186
    while ((key = msNextKeyFromHashTable(&config->env, last_key)) != NULL) {
7,752✔
187
      msConfigSetConfigOption(key, msLookupHashTable(&config->env, key));
5,160✔
188
      last_key = key;
189
    }
190
  }
191

192
  // also store the path to the CONFIG file
193
  msConfigSetConfigOption("MAPSERVER_CONFIG_FILE", ms_config_file);
2,593✔
194

195
  return config;
2,593✔
196
}
197

198
const char *msConfigGetEnv(const configObj *config, const char *key) {
5✔
199
  if (config == NULL || key == NULL)
5✔
200
    return NULL;
201
  return msLookupHashTable(&config->env, key);
5✔
202
}
203

204
const char *msConfigGetMap(const configObj *config, const char *key,
1,837✔
205
                           char *pszReturnPath) {
206
  if (config == NULL || key == NULL || pszReturnPath == NULL)
1,837✔
207
    return NULL;
208

209
  const char *mapFile = msLookupHashTable(&config->maps, key);
1,837✔
210
  if (!mapFile)
1,837✔
211
    return NULL;
212

213
  // if the mapFile path is relative, use the
214
  // location of the CONFIG file to define the full path
215
  if (CPLIsFilenameRelative(mapFile)) {
10✔
216
    char *configPath =
217
        msGetPath(CPLGetConfigOption("MAPSERVER_CONFIG_FILE", NULL));
10✔
218

219
    if (!configPath)
10✔
220
      return NULL;
221

222
    if (msBuildPath(pszReturnPath, configPath, mapFile) == NULL) {
10✔
223
      free(configPath);
×
224
      return NULL;
×
225
    }
226
    free(configPath);
10✔
227
  } else {
228
    // just copy absolute path directly
229
    strlcpy(pszReturnPath, mapFile, MS_MAXPATHLEN);
230
  }
231
  return pszReturnPath;
232
}
233

234
const char *msConfigGetPlugin(const configObj *config, const char *key) {
×
235
  if (config == NULL || key == NULL)
×
236
    return NULL;
237
  return msLookupHashTable(&config->plugins, key);
×
238
}
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