Magellan Linux

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

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

revision 1746 by niro, Sat Feb 18 01:06:20 2012 UTC revision 1802 by niro, Mon Apr 16 17:49:04 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 264  int sizeOfSingleLine(struct singleLine * Line 265  int sizeOfSingleLine(struct singleLine *
265    return count;    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) {  char *grub2ExtractTitle(struct singleLine * line) {
276      char * current;      char * current;
277      char * current_indent;      char * current_indent;
# Line 280  char *grub2ExtractTitle(struct singleLin Line 288  char *grub2ExtractTitle(struct singleLin
288      current_len = strlen(current);      current_len = strlen(current);
289    
290      /* if second word is quoted, strip the quotes and return single word */      /* if second word is quoted, strip the quotes and return single word */
291      if ((*current == '\'') && (*(current + current_len - 1) == '\'')) {      if (isquote(*current) && isquote(current[current_len - 1])) {
292        char *tmp;   char *tmp;
293    
294        tmp = strdup(current);   tmp = strdup(current);
295        *(tmp + current_len - 1) = '\0';   *(tmp + current_len - 1) = '\0';
296        return ++tmp;   return ++tmp;
297      }      }
298    
299      /* if no quotes, return second word verbatim */      /* if no quotes, return second word verbatim */
300      if (*current != '\'') {      if (!isquote(*current))
301        return current;   return current;
     }  
302    
303      /* second element start with a quote, so we have to find the element      /* 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) */       * whose last character is also quote (assuming it's the closing one) */
305      if (*current == '\'') {      int resultMaxSize;
306        int resultMaxSize;      char * result;
307        char * result;      
308        resultMaxSize = sizeOfSingleLine(line);
309        resultMaxSize = sizeOfSingleLine(line);      result = malloc(resultMaxSize);
310        result = malloc(resultMaxSize);      snprintf(result, resultMaxSize, "%s", ++current);
311        snprintf(result, resultMaxSize, "%s", ++current);      
312        i++;
313        i++;      for (; i < line->numElements; ++i) {
       for (; i < line->numElements; ++i) {  
314   current = line->elements[i].item;   current = line->elements[i].item;
315   current_len = strlen(current);   current_len = strlen(current);
316   current_indent = line->elements[i].indent;   current_indent = line->elements[i].indent;
317   current_indent_len = strlen(current_indent);   current_indent_len = strlen(current_indent);
318    
319   strncat(result, current_indent, current_indent_len);   strncat(result, current_indent, current_indent_len);
320   if (*(current + current_len - 1) != '\'') {   if (!isquote(current[current_len-1])) {
321    strncat(result, current, current_len);      strncat(result, current, current_len);
322   } else {   } else {
323    strncat(result, current, current_len - 1);      strncat(result, current, current_len - 1);
324    break;      break;
325   }   }
       }  
       return result;  
326      }      }
327        return result;
     return NULL;  
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 728  static int lineWrite(FILE * out, struct Line 731  static int lineWrite(FILE * out, struct
731      if (fprintf(out, "%s", line->indent) == -1) return -1;      if (fprintf(out, "%s", line->indent) == -1) return -1;
732    
733      for (i = 0; i < line->numElements; i++) {      for (i = 0; i < line->numElements; i++) {
734     /* Need to handle this, because we strip the quotes from
735     * menuentry when read it. */
736     if (line->type == LT_MENUENTRY && i == 1) {
737        if(!isquote(*line->elements[i].item))
738     fprintf(out, "\'%s\'", line->elements[i].item);
739        else
740     fprintf(out, "%s", line->elements[i].item);
741        fprintf(out, "%s", line->elements[i].indent);
742    
743        continue;
744     }
745    
746   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)   if (i == 1 && line->type == LT_KERNELARGS && cfi->argsInQuotes)
747      if (fputc('"', out) == EOF) return -1;      if (fputc('"', out) == EOF) return -1;
748    
# Line 1049  static struct grubConfig * readConfig(co Line 1064  static struct grubConfig * readConfig(co
1064      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1065      line->elements[1].item = buf;      line->elements[1].item = buf;
1066      line->numElements = 2;      line->numElements = 2;
1067     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1068        /* let --remove-kernel="TITLE=what" work */
1069        len = 0;
1070        char *extras;
1071        char *title;
1072    
1073        for (i = 1; i < line->numElements; i++) {
1074     len += strlen(line->elements[i].item);
1075     len += strlen(line->elements[i].indent);
1076        }
1077        buf = malloc(len + 1);
1078        *buf = '\0';
1079    
1080        /* allocate mem for extra flags. */
1081        extras = malloc(len + 1);
1082        *extras = '\0';
1083    
1084        /* get title. */
1085        for (i = 0; i < line->numElements; i++) {
1086     if (!strcmp(line->elements[i].item, "menuentry"))
1087        continue;
1088     if (isquote(*line->elements[i].item))
1089        title = line->elements[i].item + 1;
1090     else
1091        title = line->elements[i].item;
1092    
1093     len = strlen(title);
1094            if (isquote(title[len-1])) {
1095        strncat(buf, title,len-1);
1096        break;
1097     } else {
1098        strcat(buf, title);
1099        strcat(buf, line->elements[i].indent);
1100     }
1101        }
1102    
1103        /* get extras */
1104        int count = 0;
1105        for (i = 0; i < line->numElements; i++) {
1106     if (count == 2) {
1107        strcat(extras, line->elements[i].item);
1108        strcat(extras, line->elements[i].indent);
1109     }
1110    
1111     if (!strcmp(line->elements[i].item, "menuentry"))
1112        continue;
1113    
1114     /* count ' or ", there should be two in menuentry line. */
1115     if (isquote(*line->elements[i].item))
1116        count++;
1117    
1118     len = strlen(line->elements[i].item);
1119    
1120     if (isquote(line->elements[i].item[len -1]))
1121        count++;
1122    
1123     /* ok, we get the final ' or ", others are extras. */
1124                }
1125        line->elements[1].indent =
1126     line->elements[line->numElements - 2].indent;
1127        line->elements[1].item = buf;
1128        line->elements[2].indent =
1129     line->elements[line->numElements - 2].indent;
1130        line->elements[2].item = extras;
1131        line->numElements = 3;
1132   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1133      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1134         on write. This is one of the few (the only?) places that grubby         on write. This is one of the few (the only?) places that grubby
# Line 1058  static struct grubConfig * readConfig(co Line 1137  static struct grubConfig * readConfig(co
1137      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1138   int last, len;   int last, len;
1139    
1140   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1141      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1142      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1143    
1144   last = line->numElements - 1;   last = line->numElements - 1;
1145   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1146   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1147      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1148      }      }
1149   }   }
# Line 1123  static struct grubConfig * readConfig(co Line 1202  static struct grubConfig * readConfig(co
1202    
1203      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");      dbgPrintf("defaultLine is %s\n", defaultLine ? "set" : "unset");
1204      if (defaultLine) {      if (defaultLine) {
1205   if (cfi->defaultIsVariable) {          if (defaultLine->numElements > 2 &&
1206        cfi->defaultSupportSaved &&
1207        !strncmp(defaultLine->elements[2].item,"\"${saved_entry}\"", 16)) {
1208        cfg->defaultImage = DEFAULT_SAVED_GRUB2;
1209     } else if (cfi->defaultIsVariable) {
1210      char *value = defaultLine->elements[2].item;      char *value = defaultLine->elements[2].item;
1211      while (*value && (*value == '"' || *value == '\'' ||      while (*value && (*value == '"' || *value == '\'' ||
1212      *value == ' ' || *value == '\t'))      *value == ' ' || *value == '\t'))
# Line 1180  static void writeDefault(FILE * out, cha Line 1263  static void writeDefault(FILE * out, cha
1263    
1264      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1265   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
1266        else if (cfg->defaultImage == DEFAULT_SAVED_GRUB2)
1267     fprintf(out, "%sset default=\"${saved_entry}\"\n", indent);
1268      else if (cfg->defaultImage > -1) {      else if (cfg->defaultImage > -1) {
1269   if (cfg->cfi->defaultIsIndex) {   if (cfg->cfi->defaultIsIndex) {
1270      if (cfg->cfi->defaultIsVariable) {      if (cfg->cfi->defaultIsVariable) {
# Line 1430  void printEntry(struct singleEntry * ent Line 1515  void printEntry(struct singleEntry * ent
1515      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1516   fprintf(stderr, "DBG: %s", line->indent);   fprintf(stderr, "DBG: %s", line->indent);
1517   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1518   fprintf(stderr, "%s%s",      /* Need to handle this, because we strip the quotes from
1519         * menuentry when read it. */
1520        if (line->type == LT_MENUENTRY && i == 1) {
1521     if(!isquote(*line->elements[i].item))
1522        fprintf(stderr, "\'%s\'", line->elements[i].item);
1523     else
1524        fprintf(stderr, "%s", line->elements[i].item);
1525     fprintf(stderr, "%s", line->elements[i].indent);
1526    
1527     continue;
1528        }
1529        
1530        fprintf(stderr, "%s%s",
1531      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1532   }   }
1533   fprintf(stderr, "\n");   fprintf(stderr, "\n");
# Line 1457  static int endswith(const char *s, char Line 1554  static int endswith(const char *s, char
1554  {  {
1555   int slen;   int slen;
1556    
1557   if (!s && !s[0])   if (!s || !s[0])
1558   return 0;   return 0;
1559   slen = strlen(s) - 1;   slen = strlen(s) - 1;
1560    
# Line 1646  struct singleEntry * findEntryByPath(str Line 1743  struct singleEntry * findEntryByPath(str
1743    
1744   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
1745      prefix = "";      prefix = "";
1746      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
1747      kernel += 6;      kernel += 6;
1748   }   }
1749    
# Line 1662  struct singleEntry * findEntryByPath(str Line 1759  struct singleEntry * findEntryByPath(str
1759       checkType, line);       checkType, line);
1760   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1761    
1762   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
1763     line->numElements >= 2) {
1764      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1765      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
1766   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
1767   kernel + strlen(prefix)))   kernel + strlen(prefix)))
1768   break;   break;
1769   }   }
1770     if(line->type == LT_MENUENTRY &&
1771     !strcmp(line->elements[1].item, kernel))
1772        break;
1773      }      }
1774    
1775      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
# Line 1760  void markRemovedImage(struct grubConfig Line 1861  void markRemovedImage(struct grubConfig
1861        const char * prefix) {        const char * prefix) {
1862      struct singleEntry * entry;      struct singleEntry * entry;
1863    
1864      if (!image) return;      if (!image)
1865     return;
1866    
1867        /* check and see if we're removing the default image */
1868        if (isdigit(*image)) {
1869     entry = findEntryByPath(cfg, image, prefix, NULL);
1870     if(entry)
1871        entry->skip = 1;
1872     return;
1873        }
1874    
1875      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
1876   entry->skip = 1;   entry->skip = 1;
# Line 1787  void setDefaultImage(struct grubConfig * Line 1897  void setDefaultImage(struct grubConfig *
1897    
1898      /* 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
1899         changes */         changes */
1900      if (config->defaultImage == DEFAULT_SAVED)      if ((config->defaultImage == DEFAULT_SAVED) ||
1901     (config->defaultImage == DEFAULT_SAVED_GRUB2))
1902        /* default is set to saved, we don't want to change it */        /* default is set to saved, we don't want to change it */
1903        return;        return;
1904    
# Line 1854  void displayEntry(struct singleEntry * e Line 1965  void displayEntry(struct singleEntry * e
1965          return;          return;
1966      }      }
1967    
1968      printf("kernel=%s\n", line->elements[1].item);      printf("kernel=%s%s\n", prefix, line->elements[1].item);
1969    
1970      if (line->numElements >= 3) {      if (line->numElements >= 3) {
1971   printf("args=\"");   printf("args=\"");
# Line 1918  void displayEntry(struct singleEntry * e Line 2029  void displayEntry(struct singleEntry * e
2029      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2030   printf("\n");   printf("\n");
2031      }      }
2032    
2033        line = getLineByType(LT_TITLE, entry->lines);
2034        if (line) {
2035     printf("title=%s\n", line->elements[1].item);
2036        } else {
2037     char * title;
2038     line = getLineByType(LT_MENUENTRY, entry->lines);
2039     title = grub2ExtractTitle(line);
2040     if (title)
2041        printf("title=%s\n", title);
2042        }
2043  }  }
2044    
2045  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 2172  void removeLine(struct singleEntry * ent Line 2294  void removeLine(struct singleEntry * ent
2294      free(line);      free(line);
2295  }  }
2296    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2297  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2298  {  {
2299      struct singleLine newLine = {      struct singleLine newLine = {
# Line 3472  int main(int argc, const char ** argv) { Line 3587  int main(int argc, const char ** argv) {
3587      }      }
3588    
3589      if (!cfi) {      if (!cfi) {
3590            if (grub2FindConfig(&grub2ConfigType))
3591        cfi = &grub2ConfigType;
3592     else
3593        #ifdef __ia64__        #ifdef __ia64__
3594   cfi = &eliloConfigType;      cfi = &eliloConfigType;
3595        #elif __powerpc__        #elif __powerpc__
3596   cfi = &yabootConfigType;      cfi = &yabootConfigType;
3597        #elif __sparc__        #elif __sparc__
3598          cfi = &siloConfigType;              cfi = &siloConfigType;
3599        #elif __s390__        #elif __s390__
3600          cfi = &ziplConfigType;              cfi = &ziplConfigType;
3601        #elif __s390x__        #elif __s390x__
3602          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
3603        #else        #else
         if (grub2FindConfig(&grub2ConfigType))  
     cfi = &grub2ConfigType;  
  else  
3604      cfi = &grubConfigType;      cfi = &grubConfigType;
3605        #endif        #endif
3606      }      }

Legend:
Removed from v.1746  
changed lines
  Added in v.1802