summaryrefslogtreecommitdiff
blob: a6920b43c44e95dcb9be0d7123a5fb42da6c8601 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**** POSIX *******************************************************************/
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#include <sys/time.h>

/**** RELABSD *****************************************************************/
#include <relabsd/config.h>
#include <relabsd/debug.h>

#include <relabsd/config/parameters.h>

#include <relabsd/device/axis.h>

#include <relabsd/util/string.h>


/* TODO: move this to relabsd/config.h */
#ifndef RELABSD_OPTION_MAX_SIZE
   #define RELABSD_OPTION_MAX_SIZE 64
#endif

/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/

/*
 * Returns -1 on error,
 *          0 on EOF,
 *          1 on newline.
 */
static int read_axis_options
(
   FILE file [const restrict static 1],
   const char axis_name [const restrict static 1],
   struct relabsd_axis axis [const restrict static 1]
)
{
   char option[(RELABSD_OPTION_MAX_SIZE + 1)];
   int i;
   char c;

   option[RELABSD_OPTION_MAX_SIZE] = '\0';

   i = 0;

   while (i <= RELABSD_OPTION_MAX_SIZE)
   {
      c = (char) getc(file);

      if (c == EOF)
      {
         if (ferror(file))
         {
            RELABSD_FATAL
            (
               "Reading error while parsing an option in the configuration file"
               " (axis '%s').",
               axis_name
            );
         }
         else
         {
            RELABSD_FATAL
            (
               "End of file reached while parsing an option in the"
               " configuration file (axis '%s').",
               axis_name
            );
         }

         return -1;
      }

      switch (c)
      {
         case ' ':
         case '\t':
            break;

         case ',':
            i = 0;
            option[i] = '\n';
            /* We parsed a new option and there is a least another. */
            (void)
               relabsd_axis_enable_option_from_name(option, axis_name, axis);

            break;

         case '\n':
            option[i] = '\n';
            (void)
               relabsd_axis_enable_option_from_name(option, axis_name, axis);

            return 1;

         case EOF:
            option[i] = '\n';
            (void)
               relabsd_axis_enable_option_from_name(option, axis_name, axis);

            return 0;

         default:
            option[i] = c;
            i++;

            break;
      }
   }

   RELABSD_FATAL
   (
      "[CONFIG] Option name '%s[...]' (axis '%s') is too long (%d chars max).",
      option,
      axis_name,
      RELABSD_OPTION_MAX_SIZE
   );

   return -1;
}

/*
static int parse_timeout_option
(
   struct relabsd_config * const conf,
   const char * const param
)
{
   int timeout_msec;
   const int prev_errno = errno;

   conf->enable_timeout = 1;

   errno = 0;

   timeout_msec = atoi(param);

   if (timeout_msec <= 0)
   {
      RELABSD_FATAL
      (
         "Illegal value for timeout \"%d\": accepted range is [1, %d].",
         timeout_msec,
         INT_MAX
      );

      return -1;
   }

   memset((void *) &(conf->timeout), 0, sizeof(struct timeval));

   conf->timeout.tv_sec = (time_t) (timeout_msec / 1000);

   conf->timeout.tv_usec =
      (
         ((suseconds_t) timeout_msec)
         * ((suseconds_t) 1000)
      );

   return 0;
}
*/

/*
 * Returns -1 on (fatal) error,
 *          0 on succes.
 */
static int parse_axis_configuration_line
(
   FILE file [const restrict static 1],
   const char axis_name [const restrict static 1],
   struct relabsd_parameters parameters [const static 1]
)
{
   int read_count;
   enum relabsd_axis_name axis_index;
   struct relabsd_axis * axis;

   axis_index = relabsd_axis_parse_name(axis_name);

   if (axis_index == RELABSD_UNKNOWN)
   {
      RELABSD_FATAL("Unknown axis '%s' in the configuration file.", axis_name);

      return -1;

   }

   axis = (parameters->axes + axis_index);

   errno = 0;

   read_count =
      fscanf
      (
         file,
         "%d%d%d%d%d",
         &(axis->min),
         &(axis->max),
         &(axis->fuzz),
         &(axis->flat),
         &(axis->resolution)
      );

   if (read_count == EOF)
   {
      if (errno == 0)
      {
         RELABSD_FATAL
         (
            "Unexpected end of file while reading the '%s' axis' parameters in"
            " the configuration file.",
            axis_name
         );
      }
      else
      {
         RELABSD_FATAL
         (
            "An error occured while reading the '%s' axis' parameters in the"
            " configuration file: %s.",
            axis_name,
            strerror(errno)
         );
      }

      return -1;
   }
   else if (read_count < 5)
   {
      RELABSD_FATAL
      (
         "Invalid parameter count for the '%s' axis in the configuration file.",
         axis_name
      );

      return -1;
   }

   relabsd_axis_enable(axis);

   return read_axis_options(file, axis_name, axis);
}

/*
 * Returns -1 on (fatal) error,
 *          0 on EOF,
 *          1 on newline.
 */
static int read_config_line
(
   FILE file [const restrict static 1],
   const char prefix [const restrict static 1],
   struct relabsd_parameters parameters [const restrict static 1]
)
{
   if (RELABSD_IS_PREFIX("#", prefix))
   {
      return relabsd_util_reach_next_line_or_eof(file);
   }

   return parse_axis_configuration_line(file, prefix, parameters);
}

/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
/******************************************************************************/
int relabsd_parameters_parse_config_file
(
   const char filename [const restrict static 1],
   struct relabsd_parameters parameters [const restrict static 1]
)
{
   FILE * file;
   char buffer[(RELABSD_CONF_AXIS_CODE_SIZE + 1)];
   int continue_reading;

   buffer[RELABSD_CONF_AXIS_CODE_SIZE] = '\0';

   errno = 0;
   file = fopen(filename, "r");

   if (file == (FILE *) NULL)
   {
      RELABSD_FATAL("Could not open file %s: %s.", filename, strerror(errno));

      return -1;
   }

   errno = 0;
   continue_reading = 1;

   while
   (
      (continue_reading == 1)
      &&
      (
         fscanf
         (
            file,
            "%" RELABSD_TO_STRING(RELABSD_CONF_AXIS_CODE_SIZE) "s",
            buffer
         )
         != EOF
      )
   )
   {
      switch (read_config_line(file, buffer, parameters))
      {
         case 1:
            /* Everything is going well. */
            break;

         case 0:
            /* EOF reached. */
            continue_reading = 0;
            break;

         case -1:
            /* A fatal error occured. */
            fclose(file);
            return -1;
      }
   }

   if (errno != 0)
   {
      /* An error happened in the while loop condition. */
      RELABSD_FATAL
      (
         "[CONFIG] Error while reading file: %s, last read '%s'.",
         strerror(errno),
         buffer
      );

      fclose(file);

      return -1;
   }

   fclose(file);

   return 0;
}