Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 1736 by niro, Sat Feb 18 01:02:17 2012 UTC revision 1800 by niro, Mon Apr 16 17:46:40 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    static int isquote(char q)
269    {
270        if (q == '\'' || q == '\"')
271     return 1;
272        return 0;
273    }
274    
275    char *grub2ExtractTitle(struct singleLine * line) {
276        char * current;
277        char * current_indent;
278        int current_len;
279        int current_indent_len;
280        int i;
281    
282        /* bail out if line does not start with menuentry */
283        if (strcmp(line->elements[0].item, "menuentry"))
284          return NULL;
285    
286        i = 1;
287        current = line->elements[i].item;
288        current_len = strlen(current);
289    
290        /* if second word is quoted, strip the quotes and return single word */
291        if (isquote(*current) && isquote(current[current_len - 1])) {
292     char *tmp;
293    
294     tmp = strdup(current);
295     *(tmp + current_len - 1) = '\0';
296     return ++tmp;
297        }
298    
299        /* if no quotes, return second word verbatim */
300        if (!isquote(*current))
301     return current;
302    
303        /* second element start with a quote, so we have to find the element
304         * whose last character is also quote (assuming it's the closing one) */
305        int resultMaxSize;
306        char * result;
307        
308        resultMaxSize = sizeOfSingleLine(line);
309        result = malloc(resultMaxSize);
310        snprintf(result, resultMaxSize, "%s", ++current);
311        
312        i++;
313        for (; i < line->numElements; ++i) {
314     current = line->elements[i].item;
315     current_len = strlen(current);
316     current_indent = line->elements[i].indent;
317     current_indent_len = strlen(current_indent);
318    
319     strncat(result, current_indent, current_indent_len);
320     if (!isquote(current[current_len-1])) {
321        strncat(result, current, current_len);
322     } else {
323        strncat(result, current, current_len - 1);
324        break;
325     }
326        }
327        return result;
328    }
329    
330  struct configFileInfo grub2ConfigType = {  struct configFileInfo grub2ConfigType = {
331      .findConfig = grub2FindConfig,      .findConfig = grub2FindConfig,
332      .keywords = grub2Keywords,      .keywords = grub2Keywords,
333      .defaultIsIndex = 1,      .defaultIsIndex = 1,
334      .defaultSupportSaved = 0,      .defaultSupportSaved = 1,
335      .defaultIsVariable = 1,      .defaultIsVariable = 1,
336      .entryStart = LT_MENUENTRY,      .entryStart = LT_MENUENTRY,
337      .entryEnd = LT_ENTRY_END,      .entryEnd = LT_ENTRY_END,
# Line 1040  static struct grubConfig * readConfig(co Line 1126  static struct grubConfig * readConfig(co
1126    
1127      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1128      if (defaultLine) {      if (defaultLine) {
1129   if (cfi->defaultIsVariable) {          if (defaultLine->numElements > 2 &&
1130        cfi->defaultSupportSaved &&
1131        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1132        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1133     } else if (cfi->defaultIsVariable) {
1134      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1135      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
1136      *value == ' ' || *value == '\t'))      *value == ' ' || *value == '\t'))
# Line 1097  static void writeDefault(FILE * out, cha Line 1187  static void writeDefault(FILE * out, cha
1187    
1188      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1189   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1190        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1191     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1192      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1193   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1194      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
# Line 1368  void notSuitablePrintf(struct singleEntr Line 1460  void notSuitablePrintf(struct singleEntr
1460      va_end(argp);      va_end(argp);
1461  }  }
1462    
1463    #define beginswith(s, c) ((s) && (s)[0] == (c))
1464    
1465    static int endswith(const char *s, char c)
1466    {
1467     int slen;
1468    
1469     if (!s || !s[0])
1470     return 0;
1471     slen = strlen(s) - 1;
1472    
1473     return s[slen] == c;
1474    }
1475    
1476  int suitableImage(struct singleEntry * entry, const char * bootPrefix,  int suitableImage(struct singleEntry * entry, const char * bootPrefix,
1477    int skipRemoved, int flags) {    int skipRemoved, int flags) {
1478      struct singleLine * line;      struct singleLine * line;
# Line 1398  int suitableImage(struct singleEntry * e Line 1503  int suitableImage(struct singleEntry * e
1503      fullName = alloca(strlen(bootPrefix) +      fullName = alloca(strlen(bootPrefix) +
1504        strlen(line->elements[1].item) + 1);        strlen(line->elements[1].item) + 1);
1505      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1506      sprintf(fullName, "%s%s", bootPrefix,      int rootspec_offset = rootspec ? strlen(rootspec) : 0;
1507              line->elements[1].item + (rootspec ? strlen(rootspec) : 0));      int hasslash = endswith(bootPrefix, '/') ||
1508         beginswith(line->elements[1].item + rootspec_offset, '/');
1509        sprintf(fullName, "%s%s%s", bootPrefix, hasslash ? "" : "/",
1510                line->elements[1].item + rootspec_offset);
1511      if (access(fullName, R_OK)) {      if (access(fullName, R_OK)) {
1512   notSuitablePrintf(entry, "access to %s failed\n", fullName);   notSuitablePrintf(entry, "access to %s failed\n", fullName);
1513   return 0;   return 0;
# Line 1688  void setDefaultImage(struct grubConfig * Line 1796  void setDefaultImage(struct grubConfig *
1796    
1797      /* 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
1798         changes */         changes */
1799      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1800     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1801        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1802        return;        return;
1803    
# Line 1755  void displayEntry(struct singleEntry * e Line 1864  void displayEntry(struct singleEntry * e
1864          return;          return;
1865      }      }
1866    
1867      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1868    
1869      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1870   printf("args=\"");   printf("args=\"");
# Line 1819  void displayEntry(struct singleEntry * e Line 1928  void displayEntry(struct singleEntry * e
1928      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
1929   printf("\n");   printf("\n");
1930      }      }
1931    
1932        line = getLineByType(LT_TITLE, entry->lines);
1933        if (line) {
1934     printf("title=%s\n", line->elements[1].item);
1935        } else {
1936     char * title;
1937     line = getLineByType(LT_MENUENTRY, entry->lines);
1938     title = grub2ExtractTitle(line);
1939     if (title)
1940        printf("title=%s\n", title);
1941        }
1942  }  }
1943    
1944  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 2073  void removeLine(struct singleEntry * ent Line 2193  void removeLine(struct singleEntry * ent
2193      free(line);      free(line);
2194  }  }
2195    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2196  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2197  {  {
2198      struct singleLine newLine = {      struct singleLine newLine = {
# Line 3561  int main(int argc, const char ** argv) { Line 3674  int main(int argc, const char ** argv) {
3674    printf("%s\n", line->elements[1].item);    printf("%s\n", line->elements[1].item);
3675    
3676   } else {   } else {
3677    int i;    char * title;
   size_t len;  
   char * start;  
   char * tmp;  
3678    
3679    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
3680    line = getLineByType(LT_MENUENTRY, entry->lines);    line = getLineByType(LT_MENUENTRY, entry->lines);
3681    if (!line) return 0;    if (!line) return 0;
3682      title = grub2ExtractTitle(line);
3683    for (i = 0; i < line->numElements; i++) {    if (title)
3684        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");  
3685   }   }
3686   return 0;   return 0;
3687    

Legend:
Removed from v.1736  
changed lines
  Added in v.1800