Magellan Linux

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

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

revision 1748 by niro, Sat Feb 18 01:07:15 2012 UTC revision 1844 by niro, Mon Jul 2 12:59:07 2012 UTC
# Line 265  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 281  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 = {
# Line 729  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 887  static int getNextLine(char ** bufPtr, s Line 901  static int getNextLine(char ** bufPtr, s
901   break;   break;
902   }   }
903    
904   free(line->elements[i].indent);   line->elements[i + 1].indent = line->elements[i].indent;
905   line->elements[i].indent = strdup(indent);   line->elements[i].indent = strdup(indent);
906   *p++ = '\0';   *p++ = '\0';
907   i++;   i++;
908   line->elements[i].item = strdup(p);   line->elements[i].item = strdup(p);
  line->elements[i].indent = strdup("");  
  p = line->elements[i].item;  
909   }   }
910      }      }
911   }   }
# Line 981  static struct grubConfig * readConfig(co Line 993  static struct grubConfig * readConfig(co
993      dbgPrintf("found 'set' command (%d elements): ", line->numElements);      dbgPrintf("found 'set' command (%d elements): ", line->numElements);
994      dbgPrintf("%s", line->indent);      dbgPrintf("%s", line->indent);
995      for (i = 0; i < line->numElements; i++)      for (i = 0; i < line->numElements; i++)
996   dbgPrintf("%s\"%s\"", line->elements[i].indent, line->elements[i].item);   dbgPrintf("\"%s\"%s", line->elements[i].item, line->elements[i].indent);
997      dbgPrintf("\n");      dbgPrintf("\n");
998      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);      struct keywordTypes *kwType = getKeywordByType(LT_DEFAULT, cfi);
999      if (kwType && line->numElements == 3 &&      if (kwType && line->numElements == 3 &&
# Line 1050  static struct grubConfig * readConfig(co Line 1062  static struct grubConfig * readConfig(co
1062      line->elements[line->numElements - 1].indent;      line->elements[line->numElements - 1].indent;
1063      line->elements[1].item = buf;      line->elements[1].item = buf;
1064      line->numElements = 2;      line->numElements = 2;
1065     } else if (line->type == LT_MENUENTRY && line->numElements > 3) {
1066        /* let --remove-kernel="TITLE=what" work */
1067        len = 0;
1068        char *extras;
1069        char *title;
1070    
1071        for (i = 1; i < line->numElements; i++) {
1072     len += strlen(line->elements[i].item);
1073     len += strlen(line->elements[i].indent);
1074        }
1075        buf = malloc(len + 1);
1076        *buf = '\0';
1077    
1078        /* allocate mem for extra flags. */
1079        extras = malloc(len + 1);
1080        *extras = '\0';
1081    
1082        /* get title. */
1083        for (i = 0; i < line->numElements; i++) {
1084     if (!strcmp(line->elements[i].item, "menuentry"))
1085        continue;
1086     if (isquote(*line->elements[i].item))
1087        title = line->elements[i].item + 1;
1088     else
1089        title = line->elements[i].item;
1090    
1091     len = strlen(title);
1092            if (isquote(title[len-1])) {
1093        strncat(buf, title,len-1);
1094        break;
1095     } else {
1096        strcat(buf, title);
1097        strcat(buf, line->elements[i].indent);
1098     }
1099        }
1100    
1101        /* get extras */
1102        int count = 0;
1103        for (i = 0; i < line->numElements; i++) {
1104     if (count >= 2) {
1105        strcat(extras, line->elements[i].item);
1106        strcat(extras, line->elements[i].indent);
1107     }
1108    
1109     if (!strcmp(line->elements[i].item, "menuentry"))
1110        continue;
1111    
1112     /* count ' or ", there should be two in menuentry line. */
1113     if (isquote(*line->elements[i].item))
1114        count++;
1115    
1116     len = strlen(line->elements[i].item);
1117    
1118     if (isquote(line->elements[i].item[len -1]))
1119        count++;
1120    
1121     /* ok, we get the final ' or ", others are extras. */
1122                }
1123        line->elements[1].indent =
1124     line->elements[line->numElements - 2].indent;
1125        line->elements[1].item = buf;
1126        line->elements[2].indent =
1127     line->elements[line->numElements - 2].indent;
1128        line->elements[2].item = extras;
1129        line->numElements = 3;
1130   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {   } else if (line->type == LT_KERNELARGS && cfi->argsInQuotes) {
1131      /* Strip off any " which may be present; they'll be put back      /* Strip off any " which may be present; they'll be put back
1132         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 1059  static struct grubConfig * readConfig(co Line 1135  static struct grubConfig * readConfig(co
1135      if (line->numElements >= 2) {      if (line->numElements >= 2) {
1136   int last, len;   int last, len;
1137    
1138   if (*line->elements[1].item == '"')   if (isquote(*line->elements[1].item))
1139      memmove(line->elements[1].item, line->elements[1].item + 1,      memmove(line->elements[1].item, line->elements[1].item + 1,
1140      strlen(line->elements[1].item + 1) + 1);      strlen(line->elements[1].item + 1) + 1);
1141    
1142   last = line->numElements - 1;   last = line->numElements - 1;
1143   len = strlen(line->elements[last].item) - 1;   len = strlen(line->elements[last].item) - 1;
1144   if (line->elements[last].item[len] == '"')   if (isquote(line->elements[last].item[len]))
1145      line->elements[last].item[len] = '\0';      line->elements[last].item[len] = '\0';
1146      }      }
1147   }   }
# Line 1385  static char *findDiskForRoot() Line 1461  static char *findDiskForRoot()
1461      buf[rc] = '\0';      buf[rc] = '\0';
1462      chptr = buf;      chptr = buf;
1463    
1464        char *foundanswer = NULL;
1465    
1466      while (chptr && chptr != buf+rc) {      while (chptr && chptr != buf+rc) {
1467          devname = chptr;          devname = chptr;
1468    
# Line 1412  static char *findDiskForRoot() Line 1490  static char *findDiskForRoot()
1490           * for '/' obviously.           * for '/' obviously.
1491           */           */
1492          if (*(++chptr) == '/' && *(++chptr) == ' ') {          if (*(++chptr) == '/' && *(++chptr) == ' ') {
1493              /*              /* remember the last / entry in mtab */
1494               * Move back 2, which is the first space after the device name, set             foundanswer = devname;
              * it to \0 so strdup will just get the devicename.  
              */  
             chptr -= 2;  
             *chptr = '\0';  
             return strdup(devname);  
1495          }          }
1496    
1497          /* Next line */          /* Next line */
# Line 1427  static char *findDiskForRoot() Line 1500  static char *findDiskForRoot()
1500              chptr++;              chptr++;
1501      }      }
1502    
1503        /* Return the last / entry found */
1504        if (foundanswer) {
1505            chptr = strchr(foundanswer, ' ');
1506            *chptr = '\0';
1507            return strdup(foundanswer);
1508        }
1509    
1510      return NULL;      return NULL;
1511  }  }
1512    
# Line 1437  void printEntry(struct singleEntry * ent Line 1517  void printEntry(struct singleEntry * ent
1517      for (line = entry->lines; line; line = line->next) {      for (line = entry->lines; line; line = line->next) {
1518   fprintf(stderr, "DBG: %s", line->indent);   fprintf(stderr, "DBG: %s", line->indent);
1519   for (i = 0; i < line->numElements; i++) {   for (i = 0; i < line->numElements; i++) {
1520   fprintf(stderr, "%s%s",      /* Need to handle this, because we strip the quotes from
1521         * menuentry when read it. */
1522        if (line->type == LT_MENUENTRY && i == 1) {
1523     if(!isquote(*line->elements[i].item))
1524        fprintf(stderr, "\'%s\'", line->elements[i].item);
1525     else
1526        fprintf(stderr, "%s", line->elements[i].item);
1527     fprintf(stderr, "%s", line->elements[i].indent);
1528    
1529     continue;
1530        }
1531        
1532        fprintf(stderr, "%s%s",
1533      line->elements[i].item, line->elements[i].indent);      line->elements[i].item, line->elements[i].indent);
1534   }   }
1535   fprintf(stderr, "\n");   fprintf(stderr, "\n");
# Line 1464  static int endswith(const char *s, char Line 1556  static int endswith(const char *s, char
1556  {  {
1557   int slen;   int slen;
1558    
1559   if (!s && !s[0])   if (!s || !s[0])
1560   return 0;   return 0;
1561   slen = strlen(s) - 1;   slen = strlen(s) - 1;
1562    
# Line 1653  struct singleEntry * findEntryByPath(str Line 1745  struct singleEntry * findEntryByPath(str
1745    
1746   if (!strncmp(kernel, "TITLE=", 6)) {   if (!strncmp(kernel, "TITLE=", 6)) {
1747      prefix = "";      prefix = "";
1748      checkType = LT_TITLE;      checkType = LT_TITLE|LT_MENUENTRY;
1749      kernel += 6;      kernel += 6;
1750   }   }
1751    
# Line 1669  struct singleEntry * findEntryByPath(str Line 1761  struct singleEntry * findEntryByPath(str
1761       checkType, line);       checkType, line);
1762   if (!line) break;  /* not found in this entry */   if (!line) break;  /* not found in this entry */
1763    
1764   if (line && line->numElements >= 2) {   if (line && line->type != LT_MENUENTRY &&
1765     line->numElements >= 2) {
1766      rootspec = getRootSpecifier(line->elements[1].item);      rootspec = getRootSpecifier(line->elements[1].item);
1767      if (!strcmp(line->elements[1].item +      if (!strcmp(line->elements[1].item +
1768   ((rootspec != NULL) ? strlen(rootspec) : 0),   ((rootspec != NULL) ? strlen(rootspec) : 0),
1769   kernel + strlen(prefix)))   kernel + strlen(prefix)))
1770   break;   break;
1771   }   }
1772     if(line->type == LT_MENUENTRY &&
1773     !strcmp(line->elements[1].item, kernel))
1774        break;
1775      }      }
1776    
1777      /* make sure this entry has a kernel identifier; this skips      /* make sure this entry has a kernel identifier; this skips
# Line 1767  void markRemovedImage(struct grubConfig Line 1863  void markRemovedImage(struct grubConfig
1863        const char * prefix) {        const char * prefix) {
1864      struct singleEntry * entry;      struct singleEntry * entry;
1865    
1866      if (!image) return;      if (!image)
1867     return;
1868    
1869        /* check and see if we're removing the default image */
1870        if (isdigit(*image)) {
1871     entry = findEntryByPath(cfg, image, prefix, NULL);
1872     if(entry)
1873        entry->skip = 1;
1874     return;
1875        }
1876    
1877      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))      while ((entry = findEntryByPath(cfg, image, prefix, NULL)))
1878   entry->skip = 1;   entry->skip = 1;
# Line 1926  void displayEntry(struct singleEntry * e Line 2031  void displayEntry(struct singleEntry * e
2031      printf("%s%s", line->elements[i].item, line->elements[i].indent);      printf("%s%s", line->elements[i].item, line->elements[i].indent);
2032   printf("\n");   printf("\n");
2033      }      }
2034    
2035        line = getLineByType(LT_TITLE, entry->lines);
2036        if (line) {
2037     printf("title=%s\n", line->elements[1].item);
2038        } else {
2039     char * title;
2040     line = getLineByType(LT_MENUENTRY, entry->lines);
2041     title = grub2ExtractTitle(line);
2042     if (title)
2043        printf("title=%s\n", title);
2044        }
2045  }  }
2046    
2047  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {  int parseSysconfigGrub(int * lbaPtr, char ** bootPtr) {
# Line 2180  void removeLine(struct singleEntry * ent Line 2296  void removeLine(struct singleEntry * ent
2296      free(line);      free(line);
2297  }  }
2298    
 static int isquote(char q)  
 {  
     if (q == '\'' || q == '\"')  
  return 1;  
     return 0;  
 }  
   
2299  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)  static void requote(struct singleLine *tmplLine, struct configFileInfo * cfi)
2300  {  {
2301      struct singleLine newLine = {      struct singleLine newLine = {
# Line 2870  static char * getRootSpecifier(char * st Line 2979  static char * getRootSpecifier(char * st
2979  static char * getInitrdVal(struct grubConfig * config,  static char * getInitrdVal(struct grubConfig * config,
2980     const char * prefix, struct singleLine *tmplLine,     const char * prefix, struct singleLine *tmplLine,
2981     const char * newKernelInitrd,     const char * newKernelInitrd,
2982     char ** extraInitrds, int extraInitrdCount)     const char ** extraInitrds, int extraInitrdCount)
2983  {  {
2984      char *initrdVal, *end;      char *initrdVal, *end;
2985      int i;      int i;
# Line 2915  static char * getInitrdVal(struct grubCo Line 3024  static char * getInitrdVal(struct grubCo
3024    
3025  int addNewKernel(struct grubConfig * config, struct singleEntry * template,  int addNewKernel(struct grubConfig * config, struct singleEntry * template,
3026           const char * prefix,           const char * prefix,
3027   char * newKernelPath, char * newKernelTitle,   const char * newKernelPath, const char * newKernelTitle,
3028   char * newKernelArgs, char * newKernelInitrd,   const char * newKernelArgs, const char * newKernelInitrd,
3029   char ** extraInitrds, int extraInitrdCount,   const char ** extraInitrds, int extraInitrdCount,
3030                   char * newMBKernel, char * newMBKernelArgs) {                   const char * newMBKernel, const char * newMBKernelArgs) {
3031      struct singleEntry * new;      struct singleEntry * new;
3032      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;      struct singleLine * newLine = NULL, * tmplLine = NULL, * masterLine = NULL;
3033      int needs;      int needs;
# Line 3480  int main(int argc, const char ** argv) { Line 3589  int main(int argc, const char ** argv) {
3589      }      }
3590    
3591      if (!cfi) {      if (!cfi) {
3592            if (grub2FindConfig(&grub2ConfigType))
3593        cfi = &grub2ConfigType;
3594     else
3595        #ifdef __ia64__        #ifdef __ia64__
3596   cfi = &eliloConfigType;      cfi = &eliloConfigType;
3597        #elif __powerpc__        #elif __powerpc__
3598   cfi = &yabootConfigType;      cfi = &yabootConfigType;
3599        #elif __sparc__        #elif __sparc__
3600          cfi = &siloConfigType;              cfi = &siloConfigType;
3601        #elif __s390__        #elif __s390__
3602          cfi = &ziplConfigType;              cfi = &ziplConfigType;
3603        #elif __s390x__        #elif __s390x__
3604          cfi = &ziplConfigtype;              cfi = &ziplConfigtype;
3605        #else        #else
         if (grub2FindConfig(&grub2ConfigType))  
     cfi = &grub2ConfigType;  
  else  
3606      cfi = &grubConfigType;      cfi = &grubConfigType;
3607        #endif        #endif
3608      }      }
# Line 3704  int main(int argc, const char ** argv) { Line 3813  int main(int argc, const char ** argv) {
3813      }      }
3814      if (addNewKernel(config, template, bootPrefix, newKernelPath,      if (addNewKernel(config, template, bootPrefix, newKernelPath,
3815                       newKernelTitle, newKernelArgs, newKernelInitrd,                       newKernelTitle, newKernelArgs, newKernelInitrd,
3816                       extraInitrds, extraInitrdCount,                       (const char **)extraInitrds, extraInitrdCount,
3817                       newMBKernel, newMBKernelArgs)) return 1;                       newMBKernel, newMBKernelArgs)) return 1;
3818            
3819    

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