PUD: posFile: add comments
[olsrd.git] / lib / pud / src / posFile.c
1 #include "posFile.h"
2
3 /* Plugin includes */
4 #include "pud.h"
5 #include "configTools.h"
6
7 /* OLSR includes */
8
9 /* System includes */
10 #include <stdio.h>
11 #include <string.h>
12 #include <regex.h>
13 #include <sys/stat.h>
14
15 /** the maximal length of a line that is read from the file */
16 #define LINE_LENGTH 256
17
18 /** regular expression describing a comment */
19 static const char * regexCommentString = "^([[:space:]]*|[[:space:]#]+.*)$";
20
21 /** regular expression describing a key/value pair */
22 static const char * regexNameValueString =
23                 "^[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*([^[:space:]]+)[[:space:]]*$";
24
25 /** the number of matches in regexNameValueString */
26 static const size_t regexNameValuematchCount = 3;
27
28 /** the compiled regular expression describing a comment */
29 static regex_t regexComment;
30
31 /** the compiled regular expression describing a key/value pair */
32 static regex_t regexNameValue;
33
34 /** true when the plugin has been started */
35 static bool started = false;
36
37 /** type to hold the cached stat result */
38 typedef struct _CachedStat {
39         struct timespec st_mtim; /* Time of last modification.  */
40 } CachedStat;
41
42 /** the cached stat result */
43 static CachedStat cachedStat;
44
45 /**
46  * Initialises the positionFile reader.
47  * @return true upon success, false otherwise
48  */
49 bool startPositionFile(void) {
50         if (started) {
51                 return true;
52         }
53
54         if (regcomp(&regexComment, regexCommentString, REG_EXTENDED)) {
55                 pudError(false, "Could not compile regex \"%s\"", regexCommentString);
56                 return false;
57         }
58
59         if (regcomp(&regexNameValue, regexNameValueString, REG_EXTENDED)) {
60                 pudError(false, "Could not compile regex \"%s\"", regexNameValueString);
61                 return false;
62         }
63
64         cachedStat.st_mtim.tv_sec = -1;
65         cachedStat.st_mtim.tv_nsec = -1;
66
67         started = true;
68         return true;
69 }
70
71 /**
72  * Cleans up the positionFile reader.
73  */
74 void stopPositionFile(void) {
75         if (started) {
76                 regfree(&regexNameValue);
77                 regfree(&regexComment);
78                 started = false;
79         }
80 }
81
82 /**
83  * Performs a regex match
84  * @param regex the compiled regex to match against
85  * @param line the line to match
86  * @param nmatch the number of matches to produce
87  * @param pmatch the array with match information
88  * @return true upon success, false otherwise
89  */
90 static bool regexMatch(regex_t * regex, char * line, size_t nmatch, regmatch_t pmatch[]) {
91         int result = regexec(regex, line, nmatch, pmatch, 0);
92         if (!result) {
93                 return true;
94         }
95
96         if (result == REG_NOMATCH) {
97                 return false;
98         }
99
100         {
101                 char msgbuf[256];
102                 regerror(result, regex, msgbuf, sizeof(msgbuf));
103                 pudError(false, "Regex match failed: %s", msgbuf);
104         }
105
106         return false;
107 }
108
109 /** the buffer in which to store a line read from the file */
110 static char line[LINE_LENGTH];
111
112 /**
113  * Read the position file
114  * @param fileName the filename
115  */
116 bool readPositionFile(char * fileName, nmeaINFO * nmeaInfo) {
117         bool retval = false;
118         struct stat statBuf;
119         nmeaINFO result;
120         FILE * fd = NULL;
121         unsigned int lineNumber = 0;
122         char * name = NULL;
123         char * value = NULL;
124
125         if (stat(fileName, &statBuf)) {
126                 /* could not access the file */
127                 goto out;
128         }
129
130         if (!memcmp(&cachedStat.st_mtim, &statBuf.st_mtim, sizeof(cachedStat.st_mtim))) {
131                 /* file did not change since last read */
132                 goto out;
133         }
134
135         fd = fopen(fileName, "r");
136         if (!fd) {
137                 goto out;
138         }
139
140         nmea_zero_INFO(&result);
141         result.sig = POSFILE_DEFAULT_SIG;
142         result.fix = POSFILE_DEFAULT_FIX;
143         result.HDOP = POSFILE_DEFAULT_HDOP;
144         result.VDOP = POSFILE_CALCULATED_VDOP(result.HDOP);
145         result.PDOP = POSFILE_CALCULATED_PDOP(result.HDOP);
146         result.lat = POSFILE_DEFAULT_LAT;
147         result.lon = POSFILE_DEFAULT_LON;
148         result.elv = POSFILE_DEFAULT_ELV;
149         result.speed = POSFILE_DEFAULT_SPEED;
150         result.direction = POSFILE_DEFAULT_DIRECTION;
151
152         memcpy(&cachedStat.st_mtim, &statBuf.st_mtim, sizeof(cachedStat.st_mtim));
153
154         while (fgets(line, LINE_LENGTH, fd)) {
155                 regmatch_t pmatch[regexNameValuematchCount];
156
157                 lineNumber++;
158
159                 if (regexMatch(&regexComment, line, 0, NULL)) {
160                         continue;
161                 }
162
163                 if (!regexMatch(&regexNameValue, line, regexNameValuematchCount, pmatch)) {
164                         pudError(false, "Position file \"%s\", line %d uses invalid syntax: %s", fileName, lineNumber, line);
165                         goto out;
166                 }
167
168                 /* determine name/value */
169                 name = &line[pmatch[1].rm_so];
170                 line[pmatch[1].rm_eo] = '\0';
171                 value = &line[pmatch[2].rm_so];
172                 line[pmatch[2].rm_eo] = '\0';
173
174                 if (!strncasecmp(POSFILE_NAME_SIG, name, sizeof(line))) {
175                         if (!strncasecmp(POSFILE_VALUE_SIG_BAD, value, sizeof(line))) {
176                                 result.sig = NMEA_SIG_BAD;
177                         } else if (!strncasecmp(POSFILE_VALUE_SIG_LOW, value, sizeof(line))) {
178                                 result.sig = NMEA_SIG_LOW;
179                         } else if (!strncasecmp(POSFILE_VALUE_SIG_MID, value, sizeof(line))) {
180                                 result.sig = NMEA_SIG_MID;
181                         } else if (!strncasecmp(POSFILE_VALUE_SIG_HIGH, value, sizeof(line))) {
182                                 result.sig = NMEA_SIG_HIGH;
183                         } else {
184                                 pudError(false, "Position file \"%s\", line %d uses an invalid value for \"%s\","
185                                                 " valid values are [%s|%s\%s|%s]", fileName, lineNumber, POSFILE_NAME_SIG,
186                                                 POSFILE_VALUE_SIG_BAD, POSFILE_VALUE_SIG_LOW, POSFILE_VALUE_SIG_MID, POSFILE_VALUE_SIG_HIGH);
187                                 goto out;
188                         }
189                 } else if (!strncasecmp(POSFILE_NAME_FIX, name, sizeof(line))) {
190                         if (!strncasecmp(POSFILE_VALUE_FIX_BAD, value, sizeof(line))) {
191                                 result.fix = NMEA_FIX_BAD;
192                         } else if (!strncasecmp(POSFILE_VALUE_FIX_2D, value, sizeof(line))) {
193                                 result.fix = NMEA_FIX_2D;
194                         } else if (!strncasecmp(POSFILE_VALUE_FIX_3D, value, sizeof(line))) {
195                                 result.fix = NMEA_FIX_3D;
196                         } else {
197                                 pudError(false, "Position file \"%s\", line %d uses an invalid value for \"%s\","
198                                                 " valid values are [%s\%s|%s]", fileName, lineNumber, POSFILE_NAME_FIX, POSFILE_VALUE_FIX_BAD,
199                                                 POSFILE_VALUE_FIX_2D, POSFILE_VALUE_FIX_3D);
200                                 goto out;
201                         }
202                 } else if (!strncasecmp(POSFILE_NAME_HDOP, name, sizeof(line))) {
203                         double val;
204                         if (!readDouble(POSFILE_NAME_HDOP, value, &val)) {
205                                 goto out;
206                         }
207
208                         result.HDOP = val;
209                         result.VDOP = POSFILE_CALCULATED_VDOP(result.HDOP);
210                         result.PDOP = POSFILE_CALCULATED_PDOP(result.HDOP);
211                 } else if (!strncasecmp(POSFILE_NAME_LAT, name, sizeof(line))) {
212                         double val;
213                         if (!readDouble(POSFILE_NAME_LAT, value, &val)) {
214                                 goto out;
215                         }
216
217                         result.lat = val;
218                 } else if (!strncasecmp(POSFILE_NAME_LON, name, sizeof(line))) {
219                         double val;
220                         if (!readDouble(POSFILE_NAME_LON, value, &val)) {
221                                 goto out;
222                         }
223
224                         result.lon = val;
225                 } else if (!strncasecmp(POSFILE_NAME_ELV, name, sizeof(line))) {
226                         double val;
227                         if (!readDouble(POSFILE_NAME_ELV, value, &val)) {
228                                 goto out;
229                         }
230
231                         result.elv = val;
232                 } else if (!strncasecmp(POSFILE_NAME_SPEED, name, sizeof(line))) {
233                         double val;
234                         if (!readDouble(POSFILE_NAME_SPEED, value, &val)) {
235                                 goto out;
236                         }
237
238                         result.speed = val;
239                 } else if (!strncasecmp(POSFILE_NAME_DIRECTION, name, sizeof(line))) {
240                         double val;
241                         if (!readDouble(POSFILE_NAME_DIRECTION, value, &val)) {
242                                 goto out;
243                         }
244
245                         result.direction = val;
246                 } else {
247                         pudError(false, "Position file \"%s\", line %d uses an invalid option \"%s\","
248                                         " valid options are [%s|%s|%s|%s|%s|%s|%s|%s]", fileName, lineNumber, name, POSFILE_NAME_SIG,
249                                         POSFILE_NAME_FIX, POSFILE_NAME_HDOP, POSFILE_NAME_LAT, POSFILE_NAME_LON, POSFILE_NAME_ELV,
250                                         POSFILE_NAME_SPEED, POSFILE_NAME_DIRECTION);
251                         goto out;
252                 }
253         }
254
255         fclose(fd);
256
257         result.smask = POSFILE_SANITISE_SMASK;
258         nmea_INFO_sanitise(&result);
259         nmea_INFO_unit_conversion(&result);
260         if (result.fix == NMEA_FIX_BAD) {
261                 result.smask = 0;
262         } else {
263                 result.smask = POSFILE_DEFAULT_SMASK;
264         }
265
266         memcpy(nmeaInfo, &result, sizeof(result));
267         retval = true;
268
269         out: return retval;
270 }