ieee1275: split up grub_machine_get_bootlocation
[grub.git] / util / config.c
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
19 #include <config.h>
20
21 #include <string.h>
22
23 #include <grub/emu/config.h>
24 #include <grub/util/misc.h>
25
26 void
27 grub_util_parse_config (FILE *f, struct grub_util_config *cfg, int simple)
28 {
29   char *buffer = NULL;
30   size_t sz = 0;
31   while (getline (&buffer, &sz, f) >= 0)
32     {
33       const char *ptr;
34       for (ptr = buffer; *ptr && grub_isspace (*ptr); ptr++);
35       if (grub_strncmp (ptr, "GRUB_ENABLE_CRYPTODISK=",
36                         sizeof ("GRUB_ENABLE_CRYPTODISK=") - 1) == 0)
37         {
38           ptr += sizeof ("GRUB_ENABLE_CRYPTODISK=") - 1;
39           if (*ptr == '"' || *ptr == '\'')
40             ptr++;
41           if (*ptr == 'y')
42             cfg->is_cryptodisk_enabled = 1;
43           continue;
44         }
45       if (grub_strncmp (ptr, "GRUB_DISTRIBUTOR=",
46                         sizeof ("GRUB_DISTRIBUTOR=") - 1) == 0)
47         {
48           char *optr;
49           enum { NONE, SNGLQUOT, DBLQUOT } state;
50
51           ptr += sizeof ("GRUB_DISTRIBUTOR=") - 1;
52
53           if (simple)
54             {
55               char *ptr2;
56               free (cfg->grub_distributor);
57               cfg->grub_distributor = xstrdup (ptr);
58               for (ptr2 = cfg->grub_distributor
59                      + grub_strlen (cfg->grub_distributor) - 1;
60                    ptr2 >= cfg->grub_distributor
61                      && (*ptr2 == '\r' || *ptr2 == '\n'); ptr2--);
62               ptr2[1] = '\0';
63               continue;
64             }
65           free (cfg->grub_distributor);
66           cfg->grub_distributor = xmalloc (strlen (ptr) + 1);
67           optr = cfg->grub_distributor;
68           state = NONE;
69
70           for (; *ptr; ptr++)
71             switch (*ptr)
72               {
73               case '\\':
74                 if (state == SNGLQUOT)
75                   {
76                     *optr++ = *ptr;
77                     continue;
78                   }
79                 if (ptr[1])
80                   {
81                     *optr++ = ptr[1];
82                     ptr++;
83                     continue;
84                   }
85                 ptr++;
86                 break;
87               case '"':
88                 if (state == NONE)
89                   {
90                     state = DBLQUOT;
91                     continue;
92                   }
93                 if (state == DBLQUOT)
94                   {
95                     state = NONE;
96                     continue;
97                   }
98                 *optr++ = *ptr;
99                 continue;
100               case '\'':
101                 if (state == SNGLQUOT)
102                   {
103                     state = NONE;
104                     continue;
105                   }
106                 if (state == NONE)
107                   {
108                     state = SNGLQUOT;
109                     continue;
110                   }
111                 *optr++ = *ptr;
112                 continue;
113               default:
114                 *optr++ = *ptr;
115                 continue;
116               }
117           *optr = '\0';
118         }
119     }
120 }
121