Magellan Linux

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

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

trunk/mkinitrd-magellan/grubby/grubby.c revision 1304 by niro, Fri May 27 16:19:11 2011 UTC trunk/grubby/grubby.c revision 1717 by niro, Sat Feb 18 00:47:17 2012 UTC
# Line 36  Line 36 
36  #include <signal.h>  #include <signal.h>
37  #include <blkid/blkid.h>  #include <blkid/blkid.h>
38    
39    #ifndef DEBUG
40  #define DEBUG 0  #define DEBUG 0
41    #endif
42    
43  #if DEBUG  #if DEBUG
44  #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)  #define dbgPrintf(format, args...) fprintf(stderr, format , ## args)
# Line 56  struct lineElement { Line 58  struct lineElement {
58  };  };
59    
60  enum lineType_e {  enum lineType_e {
61      LT_WHITESPACE = 1 << 0,      LT_WHITESPACE   = 1 << 0,
62      LT_TITLE      = 1 << 1,      LT_TITLE        = 1 << 1,
63      LT_KERNEL     = 1 << 2,      LT_KERNEL       = 1 << 2,
64      LT_INITRD     = 1 << 3,      LT_INITRD       = 1 << 3,
65      LT_HYPER      = 1 << 4,      LT_HYPER        = 1 << 4,
66      LT_DEFAULT    = 1 << 5,      LT_DEFAULT      = 1 << 5,
67      LT_MBMODULE   = 1 << 6,      LT_MBMODULE     = 1 << 6,
68      LT_ROOT       = 1 << 7,      LT_ROOT         = 1 << 7,
69      LT_FALLBACK   = 1 << 8,      LT_FALLBACK     = 1 << 8,
70      LT_KERNELARGS = 1 << 9,      LT_KERNELARGS   = 1 << 9,
71      LT_BOOT       = 1 << 10,      LT_BOOT         = 1 << 10,
72      LT_BOOTROOT   = 1 << 11,      LT_BOOTROOT     = 1 << 11,
73      LT_LBA        = 1 << 12,      LT_LBA          = 1 << 12,
74      LT_OTHER      = 1 << 13,      LT_OTHER        = 1 << 13,
75      LT_GENERIC    = 1 << 14,      LT_GENERIC      = 1 << 14,
76      LT_UNKNOWN    = 1 << 15,      LT_ECHO    = 1 << 16,
77        LT_MENUENTRY    = 1 << 17,
78        LT_ENTRY_END    = 1 << 18,
79        LT_SET_VARIABLE = 1 << 19,
80        LT_UNKNOWN      = 1 << 20,
81  };  };
82    
83  struct singleLine {  struct singleLine {
# Line 99  struct singleEntry { Line 105  struct singleEntry {
105  #define NEED_TITLE   (1 << 2)  #define NEED_TITLE   (1 << 2)
106  #define NEED_ARGS    (1 << 3)  #define NEED_ARGS    (1 << 3)
107  #define NEED_MB      (1 << 4)  #define NEED_MB      (1 << 4)
108    #define NEED_END     (1 << 5)
109    
110  #define MAIN_DEFAULT    (1 << 0)  #define MAIN_DEFAULT    (1 << 0)
111  #define DEFAULT_SAVED       -2  #define DEFAULT_SAVED       -2
# Line 110  struct keywordTypes { Line 117  struct keywordTypes {
117      char separatorChar;      char separatorChar;
118  };  };
119    
120    struct configFileInfo;
121    
122    typedef const char *(*findConfigFunc)(struct configFileInfo *);
123    typedef const int (*writeLineFunc)(struct configFileInfo *,
124     struct singleLine *line);
125    
126  struct configFileInfo {  struct configFileInfo {
127      char * defaultConfig;      char * defaultConfig;
128        findConfigFunc findConfig;
129        writeLineFunc writeLine;
130      struct keywordTypes * keywords;      struct keywordTypes * keywords;
131      int defaultIsIndex;      int defaultIsIndex;
132        int defaultIsVariable;
133      int defaultSupportSaved;      int defaultSupportSaved;
134      enum lineType_e entrySeparator;      enum lineType_e entryStart;
135        enum lineType_e entryEnd;
136      int needsBootPrefix;      int needsBootPrefix;
137      int argsInQuotes;      int argsInQuotes;
138      int maxTitleLength;      int maxTitleLength;
139      int titleBracketed;      int titleBracketed;
140        int titlePosition;
141      int mbHyperFirst;      int mbHyperFirst;
142      int mbInitRdIsModule;      int mbInitRdIsModule;
143      int mbConcatArgs;      int mbConcatArgs;
# Line 138  struct keywordTypes grubKeywords[] = { Line 156  struct keywordTypes grubKeywords[] = {
156      { NULL,    0, 0 },      { NULL,    0, 0 },
157  };  };
158    
159    const char *grubFindConfig(struct configFileInfo *cfi) {
160        static const char *configFiles[] = {
161     "/etc/grub.conf",
162     "/boot/grub/grub.conf",
163     "/boot/grub/menu.lst",
164     NULL
165        };
166        static int i = -1;
167    
168        if (i == -1) {
169     for (i = 0; configFiles[i] != NULL; i++) {
170        dbgPrintf("Checking \"%s\": ", configFiles[i]);
171        if (!access(configFiles[i], R_OK)) {
172     dbgPrintf("found\n");
173     return configFiles[i];
174        }
175        dbgPrintf("not found\n");
176     }
177        }
178        return configFiles[i];
179    }
180    
181  struct configFileInfo grubConfigType = {  struct configFileInfo grubConfigType = {
182      "/boot/grub/grub.conf",    /* defaultConfig */      .findConfig = grubFindConfig,
183      grubKeywords,    /* keywords */      .keywords = grubKeywords,
184      1,    /* defaultIsIndex */      .defaultIsIndex = 1,
185      1,    /* defaultSupportSaved */      .defaultSupportSaved = 1,
186      LT_TITLE,    /* entrySeparator */      .entryStart = LT_TITLE,
187      1,    /* needsBootPrefix */      .needsBootPrefix = 1,
188      0,    /* argsInQuotes */      .mbHyperFirst = 1,
189      0,    /* maxTitleLength */      .mbInitRdIsModule = 1,
190      0,                                      /* titleBracketed */      .mbAllowExtraInitRds = 1,
191      1,                                      /* mbHyperFirst */  };
192      1,                                      /* mbInitRdIsModule */  
193      0,                                      /* mbConcatArgs */  struct keywordTypes grub2Keywords[] = {
194      1,                                      /* mbAllowExtraInitRds */      { "menuentry",  LT_MENUENTRY,   ' ' },
195        { "}",          LT_ENTRY_END,   ' ' },
196        { "echo",       LT_ECHO,        ' ' },
197        { "set",        LT_SET_VARIABLE,' ', '=' },
198        { "root",       LT_BOOTROOT,    ' ' },
199        { "default",    LT_DEFAULT,     ' ' },
200        { "fallback",   LT_FALLBACK,    ' ' },
201        { "linux",      LT_KERNEL,      ' ' },
202        { "initrd",     LT_INITRD,      ' ', ' ' },
203        { "module",     LT_MBMODULE,    ' ' },
204        { "kernel",     LT_HYPER,       ' ' },
205        { NULL, 0, 0 },
206    };
207    
208    const char *grub2FindConfig(struct configFileInfo *cfi) {
209        static const char *configFiles[] = {
210     "/boot/grub/grub-efi.cfg",
211     "/boot/grub/grub.cfg",
212     NULL
213        };
214        static int i = -1;
215        static const char *grub_cfg = "/boot/grub/grub.cfg";
216    
217        if (i == -1) {
218     for (i = 0; configFiles[i] != NULL; i++) {
219        dbgPrintf("Checking \"%s\": ", configFiles[i]);
220        if (!access(configFiles[i], R_OK)) {
221     dbgPrintf("found\n");
222     return configFiles[i];
223        }
224     }
225        }
226    
227        /* Ubuntu renames grub2 to grub, so check for the grub.d directory
228         * that isn't in grub1, and if it exists, return the config file path
229         * that they use. */
230        if (configFiles[i] == NULL && !access("/etc/grub.d/", R_OK)) {
231     dbgPrintf("found\n");
232     return grub_cfg;
233        }
234    
235        dbgPrintf("not found\n");
236        return configFiles[i];
237    }
238    
239    struct configFileInfo grub2ConfigType = {
240        .findConfig = grub2FindConfig,
241        .keywords = grub2Keywords,
242        .defaultIsIndex = 1,
243        .defaultSupportSaved = 0,
244        .defaultIsVariable = 1,
245        .entryStart = LT_MENUENTRY,
246        .entryEnd = LT_ENTRY_END,
247        .titlePosition = 1,
248        .needsBootPrefix = 1,
249        .mbHyperFirst = 1,
250        .mbInitRdIsModule = 1,
251        .mbAllowExtraInitRds = 1,
252  };  };
253    
254  struct keywordTypes yabootKeywords[] = {  struct keywordTypes yabootKeywords[] = {
# Line 249  struct keywordTypes extlinuxKeywords[] = Line 346  struct keywordTypes extlinuxKeywords[] =
346  };  };
347  int useextlinuxmenu;  int useextlinuxmenu;
348  struct configFileInfo eliloConfigType = {  struct configFileInfo eliloConfigType = {
349      "/boot/efi/EFI/redhat/elilo.conf",    /* defaultConfig */      .defaultConfig = "/boot/efi/EFI/redhat/elilo.conf",
350      eliloKeywords,    /* keywords */      .keywords = eliloKeywords,
351      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
352      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
353      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
354      1,                    /* needsBootPrefix */      .mbConcatArgs = 1,
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     1,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
355  };  };
356    
357  struct configFileInfo liloConfigType = {  struct configFileInfo liloConfigType = {
358      "/etc/lilo.conf",    /* defaultConfig */      .defaultConfig = "/etc/lilo.conf",
359      liloKeywords,    /* keywords */      .keywords = liloKeywords,
360      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
361      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
362      LT_KERNEL,    /* entrySeparator */      .maxTitleLength = 15,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
363  };  };
364    
365  struct configFileInfo yabootConfigType = {  struct configFileInfo yabootConfigType = {
366      "/etc/yaboot.conf",    /* defaultConfig */      .defaultConfig = "/etc/yaboot.conf",
367      yabootKeywords,    /* keywords */      .keywords = yabootKeywords,
368      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
369      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
370      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
371      1,    /* needsBootPrefix */      .maxTitleLength = 15,
372      1,    /* argsInQuotes */      .mbAllowExtraInitRds = 1,
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
373  };  };
374    
375  struct configFileInfo siloConfigType = {  struct configFileInfo siloConfigType = {
376      "/etc/silo.conf",    /* defaultConfig */      .defaultConfig = "/etc/silo.conf",
377      siloKeywords,    /* keywords */      .keywords = siloKeywords,
378      0,    /* defaultIsIndex */      .entryStart = LT_KERNEL,
379      0,    /* defaultSupportSaved */      .needsBootPrefix = 1,
380      LT_KERNEL,    /* entrySeparator */      .argsInQuotes = 1,
381      1,    /* needsBootPrefix */      .maxTitleLength = 15,
     1,    /* argsInQuotes */  
     15,    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
382  };  };
383    
384  struct configFileInfo ziplConfigType = {  struct configFileInfo ziplConfigType = {
385      "/etc/zipl.conf",    /* defaultConfig */      .defaultConfig = "/etc/zipl.conf",
386      ziplKeywords,    /* keywords */      .keywords = ziplKeywords,
387      0,    /* defaultIsIndex */      .entryStart = LT_TITLE,
388      0,    /* defaultSupportSaved */      .argsInQuotes = 1,
389      LT_TITLE,    /* entrySeparator */      .titleBracketed = 1,
     0,    /* needsBootPrefix */  
     1,    /* argsInQuotes */  
     0,    /* maxTitleLength */  
     1,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     0,                                      /* mbAllowExtraInitRds */  
390  };  };
391    
392  struct configFileInfo extlinuxConfigType = {  struct configFileInfo extlinuxConfigType = {
393      "/boot/extlinux/extlinux.conf",         /* defaultConfig */      .defaultConfig = "/boot/extlinux/extlinux.conf",
394      extlinuxKeywords,                       /* keywords */      .keywords = extlinuxKeywords,
395      0,                                      /* defaultIsIndex */      .entryStart = LT_TITLE,
396      0,                                      /* defaultSupportSaved */      .needsBootPrefix = 1,
397      LT_TITLE,                               /* entrySeparator */      .maxTitleLength = 255,
398      1,                                      /* needsBootPrefix */      .mbAllowExtraInitRds = 1,
     0,                                      /* argsInQuotes */  
     255,                                    /* maxTitleLength */  
     0,                                      /* titleBracketed */  
     0,                                      /* mbHyperFirst */  
     0,                                      /* mbInitRdIsModule */  
     0,                                      /* mbConcatArgs */  
     1,                                      /* mbAllowExtraInitRds */  
399  };  };
400    
401  struct grubConfig {  struct grubConfig {
# Line 371  static int lineWrite(FILE * out, struct Line 425  static int lineWrite(FILE * out, struct
425  static int getNextLine(char ** bufPtr, struct singleLine * line,  static int getNextLine(char ** bufPtr, struct singleLine * line,
426         struct configFileInfo * cfi);         struct configFileInfo * cfi);
427  static char * getRootSpecifier(char * str);  static char * getRootSpecifier(char * str);
428    static void requote(struct singleLine *line, struct configFileInfo * cfi);
429  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
430    const char * item, int insertHere,    const char * item, int insertHere,
431    struct configFileInfo * cfi);    struct configFileInfo * cfi);
# Line 435  static struct keywordTypes * getKeywordB Line 490  static struct keywordTypes * getKeywordB
490      return NULL;      return NULL;
491  }  }
492    
493    static char *getKeyByType(enum lineType_e type, struct configFileInfo * cfi) {
494        struct keywordTypes *kt = getKeywordByType(type, cfi);
495        if (kt)
496     return kt->key;
497        return "unknown";
498    }
499    
500  static char * getpathbyspec(char *device) {  static char * getpathbyspec(char *device) {
501      if (!blkid)      if (!blkid)
502          blkid_get_cache(&blkid, NULL);          blkid_get_cache(&blkid, NULL);
# Line 484  static int isBracketedTitle(struct singl Line 546  static int isBracketedTitle(struct singl
546      return 0;      return 0;
547  }  }
548    
549  static int isEntrySeparator(struct singleLine * line,  static int isEntryStart(struct singleLine * line,
550                              struct configFileInfo * cfi) {                              struct configFileInfo * cfi) {
551      return line->type == cfi->entrySeparator || line->type == LT_OTHER ||      return line->type == cfi->entryStart || line->type == LT_OTHER ||
552   (cfi->titleBracketed && isBracketedTitle(line));   (cfi->titleBracketed && isBracketedTitle(line));
553  }  }
554    
# Line 809  static struct grubConfig * readConfig(co Line 871  static struct grubConfig * readConfig(co
871      cfg->secondaryIndent = strdup(line->indent);      cfg->secondaryIndent = strdup(line->indent);
872   }   }
873    
874   if (isEntrySeparator(line, cfi)) {   if (isEntryStart(line, cfi)) {
875      sawEntry = 1;      sawEntry = 1;
876      if (!entry) {      if (!entry) {
877   cfg->entries = malloc(sizeof(*entry));   cfg->entries = malloc(sizeof(*entry));
# Line 825  static struct grubConfig * readConfig(co Line 887  static struct grubConfig * readConfig(co
887      entry->next = NULL;      entry->next = NULL;
888   }   }
889    
890   if (line->type == LT_DEFAULT && line->numElements == 2) {   if (line->type == LT_SET_VARIABLE) {
891        int i;
892        dbgPrintf("found 'set' command (%d elements): ", line->numElements);
893        dbgPrintf("%s", line->indent);
894        for (i = 0; i < line->numElements; i++)
895     dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);
896        dbgPrintf("\n");
897        struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
898        if (kwType && line->numElements == 3 &&
899        !strcmp(line->elements[1].item, kwType->key)) {
900     dbgPrintf("Line sets default config\n");
901     cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
902     defaultLine = line;
903        }
904     } else if (line->type == LT_DEFAULT && line->numElements == 2) {
905      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;      cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
906      defaultLine = line;      defaultLine = line;
907    
# Line 938  static struct grubConfig * readConfig(co Line 1014  static struct grubConfig * readConfig(co
1014   entry->lines = line;   entry->lines = line;
1015      else      else
1016   last->next = line;   last->next = line;
1017      dbgPrintf("readConfig added %d to %p\n", line->type, entry);      dbgPrintf("readConfig added %s to %p\n", getKeyByType(line->type, cfi), entry);
1018    
1019        /* we could have seen this outside of an entry... if so, we
1020         * ignore it like any other line we don't grok */
1021        if (line->type == LT_ENTRY_END && sawEntry)
1022     sawEntry = 0;
1023   } else {   } else {
1024      if (!cfg->theLines)      if (!cfg->theLines)
1025   cfg->theLines = line;   cfg->theLines = line;
1026      else      else
1027   last->next = line;   last->next = line;
1028      dbgPrintf("readConfig added %d to cfg\n", line->type);      dbgPrintf("readConfig added %s to cfg\n", getKeyByType(line->type, cfi));
1029   }   }
1030    
1031   last = line;   last = line;
# Line 952  static struct grubConfig * readConfig(co Line 1033  static struct grubConfig * readConfig(co
1033    
1034      free(incoming);      free(incoming);
1035    
1036        dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1037      if (defaultLine) {      if (defaultLine) {
1038   if (cfi->defaultSupportSaved &&   if (cfi->defaultIsVariable) {
1039        char *value = defaultLine->elements[2].item;
1040        while (*value && (*value == '"' || *value == '\'' ||
1041        *value == ' ' || *value == '\t'))
1042     value++;
1043        cfg->defaultImage = strtol(value, &end, 10);
1044        while (*end && (*end == '"' || *end == '\'' ||
1045        *end == ' ' || *end == '\t'))
1046     end++;
1047        if (*end) cfg->defaultImage = -1;
1048     } else if (cfi->defaultSupportSaved &&
1049   !strncmp(defaultLine->elements[1].item, "saved", 5)) {   !strncmp(defaultLine->elements[1].item, "saved", 5)) {
1050      cfg->defaultImage = DEFAULT_SAVED;      cfg->defaultImage = DEFAULT_SAVED;
1051   } else if (cfi->defaultIsIndex) {   } else if (cfi->defaultIsIndex) {
# Line 1002  static void writeDefault(FILE * out, cha Line 1094  static void writeDefault(FILE * out, cha
1094   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1095      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1096   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1097      fprintf(out, "%sdefault%s%d\n", indent, separator,      if (cfg->cfi->defaultIsVariable) {
1098      cfg->defaultImage);          fprintf(out, "%sset default=\"%d\"\n", indent,
1099     cfg->defaultImage);
1100        } else {
1101     fprintf(out, "%sdefault%s%d\n", indent, separator,
1102     cfg->defaultImage);
1103        }
1104   } else {   } else {
1105      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1106    
# Line 1093  static int writeConfig(struct grubConfig Line 1190  static int writeConfig(struct grubConfig
1190      }      }
1191    
1192      line = cfg->theLines;      line = cfg->theLines;
1193        struct keywordTypes *defaultKw = getKeywordByType(LT_DEFAULT, cfg->cfi);
1194      while (line) {      while (line) {
1195   if (line->type == LT_DEFAULT) {          if (line->type == LT_SET_VARIABLE && defaultKw &&
1196     line->numElements == 3 &&
1197     !strcmp(line->elements[1].item, defaultKw->key)) {
1198        writeDefault(out, line->indent, line->elements[0].indent, cfg);
1199        needs &= ~MAIN_DEFAULT;
1200     } else if (line->type == LT_DEFAULT) {
1201      writeDefault(out, line->indent, line->elements[0].indent, cfg);      writeDefault(out, line->indent, line->elements[0].indent, cfg);
1202      needs &= ~MAIN_DEFAULT;      needs &= ~MAIN_DEFAULT;
1203   } else if (line->type == LT_FALLBACK) {   } else if (line->type == LT_FALLBACK) {
# Line 1817  struct singleLine *  addLine(struct sing Line 1920  struct singleLine *  addLine(struct sing
1920   sprintf(tmpl.elements[0].item, "[%s]", val);   sprintf(tmpl.elements[0].item, "[%s]", val);
1921   tmpl.elements[0].indent = "";   tmpl.elements[0].indent = "";
1922   val = NULL;   val = NULL;
1923        } else if (type == LT_MENUENTRY) {
1924     char *lineend = "--class gnu-linux --class gnu --class os {";
1925     if (!val) {
1926        fprintf(stderr, "Line type LT_MENUENTRY requires a value\n");
1927        abort();
1928     }
1929     kw = getKeywordByType(type, cfi);
1930     if (!kw) {
1931        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1932        abort();
1933     }
1934     tmpl.indent = "";
1935     tmpl.type = type;
1936     tmpl.numElements = 3;
1937     tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
1938     tmpl.elements[0].item = kw->key;
1939     tmpl.elements[0].indent = alloca(2);
1940     sprintf(tmpl.elements[0].indent, "%c", kw->nextChar);
1941     tmpl.elements[1].item = (char *)val;
1942     tmpl.elements[1].indent = alloca(2);
1943     sprintf(tmpl.elements[1].indent, "%c", kw->nextChar);
1944     tmpl.elements[2].item = alloca(strlen(lineend)+1);
1945     strcpy(tmpl.elements[2].item, lineend);
1946     tmpl.elements[2].indent = "";
1947      } else {      } else {
1948   kw = getKeywordByType(type, cfi);   kw = getKeywordByType(type, cfi);
1949   if (!kw) abort();   if (!kw) {
1950        fprintf(stderr, "Looking up keyword for unknown type %d\n", type);
1951        abort();
1952     }
1953   tmpl.type = type;   tmpl.type = type;
1954   tmpl.numElements = val ? 2 : 1;   tmpl.numElements = val ? 2 : 1;
1955   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);   tmpl.elements = alloca(sizeof(*tmpl.elements) * tmpl.numElements);
# Line 1843  struct singleLine *  addLine(struct sing Line 1973  struct singleLine *  addLine(struct sing
1973   if (!line->next && !prev) prev = line;   if (!line->next && !prev) prev = line;
1974      }      }
1975    
1976      if (prev == entry->lines)      struct singleLine *menuEntry;
1977   tmpl.indent = defaultIndent ?: "";      menuEntry = getLineByType(LT_MENUENTRY, entry->lines);
1978      else      if (tmpl.type == LT_ENTRY_END) {
1979   tmpl.indent = prev->indent;   if (menuEntry)
1980        tmpl.indent = menuEntry->indent;
1981     else
1982        tmpl.indent = defaultIndent ?: "";
1983        } else if (tmpl.type != LT_MENUENTRY) {
1984     if (menuEntry)
1985        tmpl.indent = "\t";
1986     else if (prev == entry->lines)
1987        tmpl.indent = defaultIndent ?: "";
1988     else
1989        tmpl.indent = prev->indent;
1990        }
1991    
1992      return addLineTmpl(entry, &tmpl, prev, val, cfi);      return addLineTmpl(entry, &tmpl, prev, val, cfi);
1993  }  }
# Line 1873  void removeLine(struct singleEntry * ent Line 2014  void removeLine(struct singleEntry * ent
2014      free(line);      free(line);
2015  }  }
2016    
2017    static int isquote(char q)
2018    {
2019        if (q == '\'' || q == '\"')
2020     return 1;
2021        return 0;
2022    }
2023    
2024    static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2025    {
2026        struct singleLine newLine = {
2027     .indent = tmplLine->indent,
2028     .type = tmplLine->type,
2029     .next = tmplLine->next,
2030        };
2031        int firstQuotedItem = -1;
2032        int quoteLen = 0;
2033        int j;
2034        int element = 0;
2035        char *c;
2036    
2037        c = malloc(strlen(tmplLine->elements[0].item) + 1);
2038        strcpy(c, tmplLine->elements[0].item);
2039        insertElement(&newLine, c, element++, cfi);
2040        free(c);
2041        c = NULL;
2042    
2043        for (j = 1; j < tmplLine->numElements; j++) {
2044     if (firstQuotedItem == -1) {
2045        quoteLen += strlen(tmplLine->elements[j].item);
2046        
2047        if (isquote(tmplLine->elements[j].item[0])) {
2048     firstQuotedItem = j;
2049            quoteLen += strlen(tmplLine->elements[j].indent);
2050        } else {
2051     c = malloc(quoteLen + 1);
2052     strcpy(c, tmplLine->elements[j].item);
2053     insertElement(&newLine, c, element++, cfi);
2054     free(c);
2055     quoteLen = 0;
2056        }
2057     } else {
2058        int itemlen = strlen(tmplLine->elements[j].item);
2059        quoteLen += itemlen;
2060        quoteLen += strlen(tmplLine->elements[j].indent);
2061        
2062        if (isquote(tmplLine->elements[j].item[itemlen - 1])) {
2063     c = malloc(quoteLen + 1);
2064     c[0] = '\0';
2065     for (int i = firstQuotedItem; i < j+1; i++) {
2066        strcat(c, tmplLine->elements[i].item);
2067        strcat(c, tmplLine->elements[i].indent);
2068     }
2069     insertElement(&newLine, c, element++, cfi);
2070     free(c);
2071    
2072     firstQuotedItem = -1;
2073     quoteLen = 0;
2074        }
2075     }
2076        }
2077        while (tmplLine->numElements)
2078     removeElement(tmplLine, 0);
2079        if (tmplLine->elements)
2080     free(tmplLine->elements);
2081    
2082        tmplLine->numElements = newLine.numElements;
2083        tmplLine->elements = newLine.elements;
2084    }
2085    
2086  static void insertElement(struct singleLine * line,  static void insertElement(struct singleLine * line,
2087    const char * item, int insertHere,    const char * item, int insertHere,
2088    struct configFileInfo * cfi)    struct configFileInfo * cfi)
# Line 2200  int updateImage(struct grubConfig * cfg, Line 2410  int updateImage(struct grubConfig * cfg,
2410  int updateInitrd(struct grubConfig * cfg, const char * image,  int updateInitrd(struct grubConfig * cfg, const char * image,
2411                   const char * prefix, const char * initrd) {                   const char * prefix, const char * initrd) {
2412      struct singleEntry * entry;      struct singleEntry * entry;
2413      struct singleLine * line, * kernelLine;      struct singleLine * line, * kernelLine, *endLine = NULL;
2414      int index = 0;      int index = 0;
2415    
2416      if (!image) return 0;      if (!image) return 0;
# Line 2217  int updateInitrd(struct grubConfig * cfg Line 2427  int updateInitrd(struct grubConfig * cfg
2427              if (!strncmp(initrd, prefix, prefixLen))              if (!strncmp(initrd, prefix, prefixLen))
2428                  initrd += prefixLen;                  initrd += prefixLen;
2429          }          }
2430     endLine = getLineByType(LT_ENTRY_END, entry->lines);
2431     if (endLine)
2432        removeLine(entry, endLine);
2433          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);          line = addLine(entry, cfg->cfi, LT_INITRD, kernelLine->indent, initrd);
2434          if (!line) return 1;          if (!line)
2435        return 1;
2436     if (endLine) {
2437        line = addLine(entry, cfg->cfi, LT_ENTRY_END, "", NULL);
2438                if (!line)
2439     return 1;
2440     }
2441    
2442          break;          break;
2443      }      }
2444    
# Line 2395  int checkForLilo(struct grubConfig * con Line 2615  int checkForLilo(struct grubConfig * con
2615      return checkDeviceBootloader(line->elements[1].item, boot);      return checkDeviceBootloader(line->elements[1].item, boot);
2616  }  }
2617    
2618    int checkForGrub2(struct grubConfig * config) {
2619        if (!access("/etc/grub.d/", R_OK))
2620     return 2;
2621    
2622        return 1;
2623    }
2624    
2625  int checkForGrub(struct grubConfig * config) {  int checkForGrub(struct grubConfig * config) {
2626      int fd;      int fd;
2627      unsigned char bootSect[512];      unsigned char bootSect[512];
# Line 2570  int addNewKernel(struct grubConfig * con Line 2797  int addNewKernel(struct grubConfig * con
2797      if (*chptr == '#') continue;      if (*chptr == '#') continue;
2798    
2799      if (tmplLine->type == LT_KERNEL &&      if (tmplLine->type == LT_KERNEL &&
2800   tmplLine->numElements >= 2) {      tmplLine->numElements >= 2) {
2801   if (!template->multiboot && (needs & NEED_MB)) {   if (!template->multiboot && (needs & NEED_MB)) {
2802      /* it's not a multiboot template and this is the kernel      /* it's not a multiboot template and this is the kernel
2803       * line.  Try to be intelligent about inserting the       * line.  Try to be intelligent about inserting the
# Line 2696  int addNewKernel(struct grubConfig * con Line 2923  int addNewKernel(struct grubConfig * con
2923      needs &= ~NEED_INITRD;      needs &= ~NEED_INITRD;
2924   }   }
2925    
2926        } else if (tmplLine->type == LT_MENUENTRY &&
2927           (needs & NEED_TITLE)) {
2928     requote(tmplLine, config->cfi);
2929     char *nkt = malloc(strlen(newKernelTitle)+3);
2930     strcpy(nkt, "'");
2931     strcat(nkt, newKernelTitle);
2932     strcat(nkt, "'");
2933     newLine = addLineTmpl(new, tmplLine, newLine, nkt, config->cfi);
2934     free(nkt);
2935     needs &= ~NEED_TITLE;
2936      } else if (tmplLine->type == LT_TITLE &&      } else if (tmplLine->type == LT_TITLE &&
2937         (needs & NEED_TITLE)) {         (needs & NEED_TITLE)) {
2938   if (tmplLine->numElements >= 2) {   if (tmplLine->numElements >= 2) {
# Line 2709  int addNewKernel(struct grubConfig * con Line 2946  int addNewKernel(struct grubConfig * con
2946        tmplLine->indent, newKernelTitle);        tmplLine->indent, newKernelTitle);
2947      needs &= ~NEED_TITLE;      needs &= ~NEED_TITLE;
2948   }   }
2949        } else if (tmplLine->type == LT_ECHO) {
2950        requote(tmplLine, config->cfi);
2951        if (tmplLine->numElements > 1 &&
2952        strstr(tmplLine->elements[1].item, "'Loading Linux ")) {
2953     char *prefix = "'Loading ";
2954     char *newTitle = malloc(strlen(prefix) +
2955     strlen(newKernelTitle) + 2);
2956    
2957     strcpy(newTitle, prefix);
2958     strcat(newTitle, newKernelTitle);
2959     strcat(newTitle, "'");
2960     newLine = addLine(new, config->cfi, LT_ECHO,
2961     tmplLine->indent, newTitle);
2962     free(newTitle);
2963        } else {
2964     /* pass through other lines from the template */
2965     newLine = addLineTmpl(new, tmplLine, newLine, NULL,
2966     config->cfi);
2967        }
2968      } else {      } else {
2969   /* pass through other lines from the template */   /* pass through other lines from the template */
2970   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);   newLine = addLineTmpl(new, tmplLine, newLine, NULL, config->cfi);
# Line 2720  int addNewKernel(struct grubConfig * con Line 2975  int addNewKernel(struct grubConfig * con
2975   /* don't have a template, so start the entry with the   /* don't have a template, so start the entry with the
2976   * appropriate starting line   * appropriate starting line
2977   */   */
2978   switch (config->cfi->entrySeparator) {   switch (config->cfi->entryStart) {
2979      case LT_KERNEL:      case LT_KERNEL:
2980   if (new->multiboot && config->cfi->mbHyperFirst) {   if (new->multiboot && config->cfi->mbHyperFirst) {
2981      /* fall through to LT_HYPER */      /* fall through to LT_HYPER */
# Line 2739  int addNewKernel(struct grubConfig * con Line 2994  int addNewKernel(struct grubConfig * con
2994   needs &= ~NEED_MB;   needs &= ~NEED_MB;
2995   break;   break;
2996    
2997        case LT_MENUENTRY: {
2998     char *nkt = malloc(strlen(newKernelTitle)+3);
2999     strcpy(nkt, "'");
3000     strcat(nkt, newKernelTitle);
3001     strcat(nkt, "'");
3002            newLine = addLine(new, config->cfi, LT_MENUENTRY,
3003      config->primaryIndent, nkt);
3004     free(nkt);
3005     needs &= ~NEED_TITLE;
3006     needs |= NEED_END;
3007     break;
3008        }
3009      case LT_TITLE:      case LT_TITLE:
3010   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)   if( useextlinuxmenu != 0 ){ // We just need useextlinuxmenu to not be zero (set above)
3011   char * templabel;   char * templabel;
# Line 2772  int addNewKernel(struct grubConfig * con Line 3039  int addNewKernel(struct grubConfig * con
3039    
3040      /* add the remainder of the lines, i.e. those that either      /* add the remainder of the lines, i.e. those that either
3041       * weren't present in the template, or in the case of no template,       * weren't present in the template, or in the case of no template,
3042       * all the lines following the entrySeparator.       * all the lines following the entryStart.
3043       */       */
3044      if (needs & NEED_TITLE) {      if (needs & NEED_TITLE) {
3045   newLine = addLine(new, config->cfi, LT_TITLE,   newLine = addLine(new, config->cfi, LT_TITLE,
# Line 2813  int addNewKernel(struct grubConfig * con Line 3080  int addNewKernel(struct grubConfig * con
3080   free(initrdVal);   free(initrdVal);
3081   needs &= ~NEED_INITRD;   needs &= ~NEED_INITRD;
3082      }      }
3083        if (needs & NEED_END) {
3084     newLine = addLine(new, config->cfi, LT_ENTRY_END,
3085     config->secondaryIndent, NULL);
3086     needs &= ~NEED_END;
3087        }
3088    
3089      if (needs) {      if (needs) {
3090   printf(_("grubby: needs=%d, aborting\n"), needs);   printf(_("grubby: needs=%d, aborting\n"), needs);
# Line 2842  static void traceback(int signum) Line 3114  static void traceback(int signum)
3114    
3115  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
3116      poptContext optCon;      poptContext optCon;
3117      char * grubConfig = NULL;      const char * grubConfig = NULL;
3118      char * outputFile = NULL;      char * outputFile = NULL;
3119      int arg = 0;      int arg = 0;
3120      int flags = 0;      int flags = 0;
3121      int badImageOkay = 0;      int badImageOkay = 0;
3122        int configureGrub2 = 0;
3123      int configureLilo = 0, configureELilo = 0, configureGrub = 0;      int configureLilo = 0, configureELilo = 0, configureGrub = 0;
3124      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;      int configureYaboot = 0, configureSilo = 0, configureZipl = 0;
3125      int configureExtLinux = 0;      int configureExtLinux = 0;
# Line 2912  int main(int argc, const char ** argv) { Line 3185  int main(int argc, const char ** argv) {
3185      _("configure extlinux bootloader (from syslinux)") },      _("configure extlinux bootloader (from syslinux)") },
3186   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,   { "grub", 0, POPT_ARG_NONE, &configureGrub, 0,
3187      _("configure grub bootloader") },      _("configure grub bootloader") },
3188     { "grub2", 0, POPT_ARG_NONE, &configureGrub2, 0,
3189        _("configure grub2 bootloader") },
3190   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,   { "info", 0, POPT_ARG_STRING, &kernelInfo, 0,
3191      _("display boot information for specified kernel"),      _("display boot information for specified kernel"),
3192      _("kernel-path") },      _("kernel-path") },
# Line 2991  int main(int argc, const char ** argv) { Line 3266  int main(int argc, const char ** argv) {
3266   return 1;   return 1;
3267      }      }
3268    
3269      if ((configureLilo + configureGrub + configureELilo +      if ((configureLilo + configureGrub2 + configureGrub + configureELilo +
3270   configureYaboot + configureSilo + configureZipl +   configureYaboot + configureSilo + configureZipl +
3271   configureExtLinux ) > 1) {   configureExtLinux ) > 1) {
3272   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));   fprintf(stderr, _("grubby: cannot specify multiple bootloaders\n"));
# Line 3000  int main(int argc, const char ** argv) { Line 3275  int main(int argc, const char ** argv) {
3275   fprintf(stderr,   fprintf(stderr,
3276      _("grubby: cannot specify config file with --bootloader-probe\n"));      _("grubby: cannot specify config file with --bootloader-probe\n"));
3277   return 1;   return 1;
3278        } else if (configureGrub2) {
3279     cfi = &grub2ConfigType;
3280      } else if (configureLilo) {      } else if (configureLilo) {
3281   cfi = &liloConfigType;   cfi = &liloConfigType;
3282      } else if (configureGrub) {      } else if (configureGrub) {
# Line 3029  int main(int argc, const char ** argv) { Line 3306  int main(int argc, const char ** argv) {
3306        #elif __s390x__        #elif __s390x__
3307          cfi = &ziplConfigtype;          cfi = &ziplConfigtype;
3308        #else        #else
3309   cfi = &grubConfigType;          if (grub2FindConfig(&grub2ConfigType))
3310        cfi = &grub2ConfigType;
3311     else
3312        cfi = &grubConfigType;
3313        #endif        #endif
3314      }      }
3315    
3316      if (!grubConfig)      if (!grubConfig) {
3317   grubConfig = cfi->defaultConfig;   if (cfi->findConfig)
3318        grubConfig = cfi->findConfig(cfi);
3319     if (!grubConfig)
3320        grubConfig = cfi->defaultConfig;
3321        }
3322    
3323      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||      if (bootloaderProbe && (displayDefault || kernelInfo || newKernelVersion ||
3324    newKernelPath || removeKernelPath || makeDefault ||    newKernelPath || removeKernelPath || makeDefault ||
# Line 3081  int main(int argc, const char ** argv) { Line 3365  int main(int argc, const char ** argv) {
3365   defaultKernel = NULL;   defaultKernel = NULL;
3366      }      }
3367    
3368      if (!strcmp(grubConfig, "-") && !outputFile) {      if (grubConfig && !strcmp(grubConfig, "-") && !outputFile) {
3369   fprintf(stderr, _("grubby: output file must be specified if stdin "   fprintf(stderr, _("grubby: output file must be specified if stdin "
3370   "is used\n"));   "is used\n"));
3371   return 1;   return 1;
# Line 3116  int main(int argc, const char ** argv) { Line 3400  int main(int argc, const char ** argv) {
3400      }      }
3401    
3402      if (bootloaderProbe) {      if (bootloaderProbe) {
3403   int lrc = 0, grc = 0, erc = 0;   int lrc = 0, grc = 0, gr2c = 0, erc = 0;
3404   struct grubConfig * lconfig, * gconfig;   struct grubConfig * lconfig, * gconfig;
3405    
3406   if (!access(grubConfigType.defaultConfig, F_OK)) {   const char *grub2config = grub2FindConfig(&grub2ConfigType);
3407      gconfig = readConfig(grubConfigType.defaultConfig, &grubConfigType);   if (grub2config) {
3408        gconfig = readConfig(grub2config, &grub2ConfigType);
3409        if (!gconfig)
3410     gr2c = 1;
3411        else
3412     gr2c = checkForGrub2(gconfig);
3413     }
3414    
3415     const char *grubconfig = grubFindConfig(&grubConfigType);
3416     if (!access(grubconfig, F_OK)) {
3417        gconfig = readConfig(grubconfig, &grubConfigType);
3418      if (!gconfig)      if (!gconfig)
3419   grc = 1;   grc = 1;
3420      else      else
# Line 3143  int main(int argc, const char ** argv) { Line 3437  int main(int argc, const char ** argv) {
3437   erc = checkForExtLinux(lconfig);   erc = checkForExtLinux(lconfig);
3438   }   }
3439    
3440   if (lrc == 1 || grc == 1) return 1;   if (lrc == 1 || grc == 1 || gr2c == 1) return 1;
3441    
3442   if (lrc == 2) printf("lilo\n");   if (lrc == 2) printf("lilo\n");
3443     if (gr2c == 2) printf("grub2\n");
3444   if (grc == 2) printf("grub\n");   if (grc == 2) printf("grub\n");
3445   if (erc == 2) printf("extlinux\n");   if (erc == 2) printf("extlinux\n");
3446    
# Line 3205  int main(int argc, const char ** argv) { Line 3500  int main(int argc, const char ** argv) {
3500      }      }
3501    
3502      if (!outputFile)      if (!outputFile)
3503   outputFile = grubConfig;   outputFile = (char *)grubConfig;
3504    
3505      return writeConfig(config, outputFile, bootPrefix);      return writeConfig(config, outputFile, bootPrefix);
3506  }  }

Legend:
Removed from v.1304  
changed lines
  Added in v.1717