config.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <string.h>
  20. #include <grub/emu/config.h>
  21. #include <grub/util/misc.h>
  22. void
  23. grub_util_parse_config (FILE *f, struct grub_util_config *cfg, int simple)
  24. {
  25. char *buffer = NULL;
  26. size_t sz = 0;
  27. while (getline (&buffer, &sz, f) >= 0)
  28. {
  29. const char *ptr;
  30. for (ptr = buffer; *ptr && grub_isspace (*ptr); ptr++);
  31. if (grub_strncmp (ptr, "GRUB_ENABLE_CRYPTODISK=",
  32. sizeof ("GRUB_ENABLE_CRYPTODISK=") - 1) == 0)
  33. {
  34. ptr += sizeof ("GRUB_ENABLE_CRYPTODISK=") - 1;
  35. if (*ptr == '"' || *ptr == '\'')
  36. ptr++;
  37. if (*ptr == 'y')
  38. cfg->is_cryptodisk_enabled = 1;
  39. continue;
  40. }
  41. if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
  42. sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
  43. {
  44. char *optr;
  45. enum { NONE, SNGLQUOT, DBLQUOT } state;
  46. ptr += sizeof ("GRUB_DISTRIBUTOR=") - 1;
  47. if (simple)
  48. {
  49. char *ptr2;
  50. free (cfg->grub_distributor);
  51. cfg->grub_distributor = xstrdup (ptr);
  52. for (ptr2 = cfg->grub_distributor
  53. + grub_strlen (cfg->grub_distributor) - 1;
  54. ptr2 >= cfg->grub_distributor
  55. && (*ptr2 == '\r' || *ptr2 == '\n'); ptr2--);
  56. ptr2[1] = '\0';
  57. continue;
  58. }
  59. free (cfg->grub_distributor);
  60. cfg->grub_distributor = xmalloc (strlen (ptr) + 1);
  61. optr = cfg->grub_distributor;
  62. state = NONE;
  63. for (; *ptr; ptr++)
  64. switch (*ptr)
  65. {
  66. case '\\':
  67. if (state == SNGLQUOT)
  68. {
  69. *optr++ = *ptr;
  70. continue;
  71. }
  72. if (ptr[1])
  73. {
  74. *optr++ = ptr[1];
  75. ptr++;
  76. continue;
  77. }
  78. ptr++;
  79. break;
  80. case '"':
  81. if (state == NONE)
  82. {
  83. state = DBLQUOT;
  84. continue;
  85. }
  86. if (state == DBLQUOT)
  87. {
  88. state = NONE;
  89. continue;
  90. }
  91. *optr++ = *ptr;
  92. continue;
  93. case '\'':
  94. if (state == SNGLQUOT)
  95. {
  96. state = NONE;
  97. continue;
  98. }
  99. if (state == NONE)
  100. {
  101. state = SNGLQUOT;
  102. continue;
  103. }
  104. *optr++ = *ptr;
  105. continue;
  106. default:
  107. *optr++ = *ptr;
  108. continue;
  109. }
  110. *optr = '\0';
  111. }
  112. }
  113. }