Magellan Linux

Diff of /tags/grubby-8_32/grubby.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1745 by niro, Sat Feb 18 01:05:52 2012 UTC revision 1748 by niro, Sat Feb 18 01:07:15 2012 UTC
# Line 114  struct singleEntry { Line 114  struct singleEntry {
114    
115  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
116  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
117    #define DEFAULT_SAVED_GRUB2 -3
118    
119  struct keywordTypes {  struct keywordTypes {
120      char * key;      char * key;
# Line 241  const char *grub2FindConfig(struct confi Line 242  const char *grub2FindConfig(struct confi
242      return configFiles[i];      return configFiles[i];
243  }  }
244    
245    int sizeOfSingleLine(struct singleLine * line) {
246      int i;
247      int count = 0;
248    
249      for (i = 0; i < line->numElements; i++) {
250        int indentSize = 0;
251    
252        count = count + strlen(line->elements[i].item);
253    
254        indentSize = strlen(line->elements[i].indent);
255        if (indentSize > 0)
256          count = count + indentSize;
257        else
258          /* be extra safe and add room for whitespaces */
259          count = count + 1;
260      }
261    
262      /* room for trailing terminator */
263      count = count + 1;
264    
265      return count;
266    }
267    
268    char *grub2ExtractTitle(struct singleLine * line) {
269        char * current;
270        char * current_indent;
271        int current_len;
272        int current_indent_len;
273        int i;
274    
275        /* bail out if line does not start with menuentry */
276        if (strcmp(line->elements[0].item, "menuentry"))
277          return NULL;
278    
279        i = 1;
280        current = line->elements[i].item;
281        current_len = strlen(current);
282    
283        /* if second word is quoted, strip the quotes and return single word */
284        if ((*current == '\'') && (*(current + current_len - 1) == '\'')) {
285          char *tmp;
286    
287          tmp = strdup(current);
288          *(tmp + current_len - 1) = '\0';
289          return ++tmp;
290        }
291    
292        /* if no quotes, return second word verbatim */
293        if (*current != '\'') {
294          return current;
295        }
296    
297        /* second element start with a quote, so we have to find the element
298         * whose last character is also quote (assuming it's the closing one) */
299        if (*current == '\'') {
300          int resultMaxSize;
301          char * result;
302    
303          resultMaxSize = sizeOfSingleLine(line);
304          result = malloc(resultMaxSize);
305          snprintf(result, resultMaxSize, "%s", ++current);
306    
307          i++;
308          for (; i < line->numElements; ++i) {
309     current = line->elements[i].item;
310     current_len = strlen(current);
311     current_indent = line->elements[i].indent;
312     current_indent_len = strlen(current_indent);
313    
314     strncat(result, current_indent, current_indent_len);
315     if (*(current + current_len - 1) != '\'') {
316      strncat(result, current, current_len);
317     } else {
318      strncat(result, current, current_len - 1);
319      break;
320     }
321          }
322          return result;
323        }
324    
325        return NULL;
326    }
327    
328  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
329      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
330      .keywords = grub2Keywords,      .keywords = grub2Keywords,
331      .defaultIsIndex = 1,      .defaultIsIndex = 1,
332      .defaultSupportSaved = 0,      .defaultSupportSaved = 1,
333      .defaultIsVariable = 1,      .defaultIsVariable = 1,
334      .entryStart = LT_MENUENTRY,      .entryStart = LT_MENUENTRY,
335      .entryEnd = LT_ENTRY_END,      .entryEnd = LT_ENTRY_END,
# Line 1040  static struct grubConfig * readConfig(co Line 1124  static struct grubConfig * readConfig(co
1124    
1125      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1126      if (defaultLine) {      if (defaultLine) {
1127   if (cfi->defaultIsVariable) {          if (defaultLine->numElements > 2 &&
1128        cfi->defaultSupportSaved &&
1129        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1130        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1131     } else if (cfi->defaultIsVariable) {
1132      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1133      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
1134      *value == ' ' || *value == '\t'))      *value == ' ' || *value == '\t'))
# Line 1097  static void writeDefault(FILE * out, cha Line 1185  static void writeDefault(FILE * out, cha
1185    
1186      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1187   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1188        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1189     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1190      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1191   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1192      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
# Line 1704  void setDefaultImage(struct grubConfig * Line 1794  void setDefaultImage(struct grubConfig *
1794    
1795      /* defaultImage now points to what we'd like to use, but before any order      /* defaultImage now points to what we'd like to use, but before any order
1796         changes */         changes */
1797      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1798     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1799        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1800        return;        return;
1801    
# Line 1771  void displayEntry(struct singleEntry * e Line 1862  void displayEntry(struct singleEntry * e
1862          return;          return;
1863      }      }
1864    
1865      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1866    
1867      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1868   printf("args=\"");   printf("args=\"");
# Line 3577  int main(int argc, const char ** argv) { Line 3668  int main(int argc, const char ** argv) {
3668    printf("%s\n", line->elements[1].item);    printf("%s\n", line->elements[1].item);
3669    
3670   } else {   } else {
3671    int i;    char * title;
   size_t len;  
   char * start;  
   char * tmp;  
3672    
3673    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3674    line = getLineByType(LT_MENUENTRY, entry->lines);    line = getLineByType(LT_MENUENTRY, entry->lines);
3675    if (!line) return 0;    if (!line) return 0;
3676      title = grub2ExtractTitle(line);
3677    for (i = 0; i < line->numElements; i++) {    if (title)
3678        printf("%s\n", title);
     if (!strcmp(line->elements[i].item, "menuentry"))  
       continue;  
   
     if (*line->elements[i].item == '\'')  
       start = line->elements[i].item + 1;  
     else  
       start = line->elements[i].item;  
   
     len = strlen(start);  
     if (*(start + len - 1) == '\'') {  
       tmp = strdup(start);  
       *(tmp + len - 1) = '\0';  
       printf("%s", tmp);  
       free(tmp);  
       break;  
     } else {  
       printf("%s ", start);  
     }  
   }  
   printf("\n");  
3679   }   }
3680   return 0;   return 0;
3681    

Legend:
Removed from v.1745  
changed lines
  Added in v.1748