Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parseconf.c 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * core/parseconf.c
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "config.h"
  8. extern int verbosity;
  9. extern Configuration config;
  10. #define CMP(a) (!strcmp(keyptr, a))
  11. /* ---------------------------------------------------------------- */
  12. int parse_config(char *fname, int flags)
  13. {
  14. FILE *fp;
  15. char line[SZ_STRINGS+1],
  16. *keyptr, *typeptr, *cptr;
  17. int numligne;
  18. #if DEBUG_LEVEL
  19. fprintf(stderr, ">>> %s ( '%s' $%x )\n", fname, flags);
  20. #endif
  21. config.valid = 49;
  22. if (NULL==(fp=fopen(fname, "r"))) {
  23. perror(fname);
  24. return -2;
  25. }
  26. numligne = 0;
  27. while (fgets(line, SZ_STRINGS, fp))
  28. {
  29. numligne++;
  30. if ('\0'==line[0]) {
  31. fprintf(stderr, "%s : short read line %d\n",
  32. fname, numligne);
  33. fclose(fp);
  34. return -1;
  35. }
  36. /* massage the end of line */
  37. line[strlen(line)-1] = '\0'; /* kill EOL */
  38. if (verbosity) {
  39. fprintf(stderr, "%3d :\t%s\n", numligne, line);
  40. }
  41. /* seek for the first token in this line */
  42. if (NULL==(keyptr = strtok(line, " \t"))) {
  43. /* Got an empty line */
  44. continue;
  45. }
  46. /* skip comments */
  47. if ('#'==*keyptr) continue;
  48. /* seek for the type field */
  49. if (NULL==(typeptr = strtok(NULL, " \t"))) {
  50. /* we can(t get a type flag ? wtf ? */
  51. fprintf(stderr, "ERROR line %d : no type\n", numligne);
  52. continue;
  53. }
  54. if(verbosity)
  55. fprintf(stderr, "[%s] type %s\n", keyptr, typeptr);
  56. if CMP("abort") {
  57. fprintf(stderr, "abort in config file\n");
  58. }
  59. if (CMP("input_device")) {
  60. config.input_device = strdup(strtok(NULL, " \t"));
  61. continue;
  62. }
  63. if (CMP("input_speed")) {
  64. config.input_speed = atoi(strtok(NULL, " \t"));
  65. #if DEBUG_LEVEL
  66. fprintf(stderr, "input speed = %d\n", config.input_speed);
  67. #endif
  68. }
  69. }
  70. fclose(fp);
  71. return 0;
  72. }
  73. /* ---------------------------------------------------------------- */
  74. int show_config(char *title)
  75. {
  76. if (verbosity) {
  77. printf("********** %s **********\n", title);
  78. }
  79. printf("valid : %d\n", config.valid);
  80. printf("input device : %s\n", config.input_device);
  81. printf("input speed : %d\n", config.input_speed);
  82. puts("");
  83. return 0;
  84. }
  85. /* ---------------------------------------------------------------- */