Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 2983 by niro, Thu Jun 30 10:27:31 2016 UTC revision 2995 by niro, Thu Jun 30 10:37:52 2016 UTC
# Line 429  char *grub2ExtractTitle(struct singleLin Line 429  char *grub2ExtractTitle(struct singleLin
429    
430      /* bail out if line does not start with menuentry */      /* bail out if line does not start with menuentry */
431      if (strcmp(line->elements[0].item, "menuentry"))      if (strcmp(line->elements[0].item, "menuentry"))
432        return NULL;   return NULL;
433    
434      i = 1;      i = 1;
435      current = line->elements[i].item;      current = line->elements[i].item;
# Line 438  char *grub2ExtractTitle(struct singleLin Line 438  char *grub2ExtractTitle(struct singleLin
438      /* if second word is quoted, strip the quotes and return single word */      /* if second word is quoted, strip the quotes and return single word */
439      if (isquote(*current) && isquote(current[current_len - 1])) {      if (isquote(*current) && isquote(current[current_len - 1])) {
440   char *tmp;   char *tmp;
441    
442   tmp = strdup(current);   tmp = strdup(current+1);
443   *(tmp + current_len - 1) = '\0';   if (!tmp)
444   return ++tmp;      return NULL;
445     tmp[strlen(tmp)-1] = '\0';
446     return tmp;
447      }      }
448    
449      /* if no quotes, return second word verbatim */      /* if no quotes, return second word verbatim */
# Line 452  char *grub2ExtractTitle(struct singleLin Line 454  char *grub2ExtractTitle(struct singleLin
454       * whose last character is also quote (assuming it's the closing one) */       * whose last character is also quote (assuming it's the closing one) */
455      int resultMaxSize;      int resultMaxSize;
456      char * result;      char * result;
457            /* need to ensure that ' does not match " as we search */
458        char quote_char = *current;
459    
460      resultMaxSize = sizeOfSingleLine(line);      resultMaxSize = sizeOfSingleLine(line);
461      result = malloc(resultMaxSize);      result = malloc(resultMaxSize);
462      snprintf(result, resultMaxSize, "%s", ++current);      snprintf(result, resultMaxSize, "%s", ++current);
463        
464      i++;      i++;
465      for (; i < line->numElements; ++i) {      for (; i < line->numElements; ++i) {
466   current = line->elements[i].item;   current = line->elements[i].item;
# Line 465  char *grub2ExtractTitle(struct singleLin Line 469  char *grub2ExtractTitle(struct singleLin
469   current_indent_len = strlen(current_indent);   current_indent_len = strlen(current_indent);
470    
471   strncat(result, current_indent, current_indent_len);   strncat(result, current_indent, current_indent_len);
472   if (!isquote(current[current_len-1])) {   if (current[current_len-1] != quote_char) {
473      strncat(result, current, current_len);      strncat(result, current, current_len);
474   } else {   } else {
475      strncat(result, current, current_len - 1);      strncat(result, current, current_len - 1);
# Line 929  static int lineWrite(FILE * out, struct Line 933  static int lineWrite(FILE * out, struct
933   /* Need to handle this, because we strip the quotes from   /* Need to handle this, because we strip the quotes from
934   * menuentry when read it. */   * menuentry when read it. */
935   if (line->type == LT_MENUENTRY && i == 1) {   if (line->type == LT_MENUENTRY && i == 1) {
936      if(!isquote(*line->elements[i].item))      if(!isquote(*line->elements[i].item)) {
937   fprintf(out, "\'%s\'", line->elements[i].item);   int substring = 0;
938      else   /* If the line contains nested quotes, we did not strip
939     * the "interna" quotes and we must use the right quotes
940     * again when writing the updated file. */
941     for (int j = i; j < line->numElements; j++) {
942        if (strchr(line->elements[i].item, '\'') != NULL) {
943           substring = 1;
944           fprintf(out, "\"%s\"", line->elements[i].item);
945           break;
946        }
947     }
948     if (!substring)
949        fprintf(out, "\'%s\'", line->elements[i].item);
950        } else {
951   fprintf(out, "%s", line->elements[i].item);   fprintf(out, "%s", line->elements[i].item);
952        }
953      fprintf(out, "%s", line->elements[i].indent);      fprintf(out, "%s", line->elements[i].indent);
954    
955      continue;      continue;
# Line 1268  static struct grubConfig * readConfig(co Line 1285  static struct grubConfig * readConfig(co
1285      len = 0;      len = 0;
1286      char *extras;      char *extras;
1287      char *title;      char *title;
1288        /* initially unseen value */
1289        char quote_char = '\0';
1290    
1291      for (int i = 1; i < line->numElements; i++) {      for (int i = 1; i < line->numElements; i++) {
1292   len += strlen(line->elements[i].item);   len += strlen(line->elements[i].item);
# Line 1284  static struct grubConfig * readConfig(co Line 1303  static struct grubConfig * readConfig(co
1303      for (int i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1304   if (!strcmp(line->elements[i].item, "menuentry"))   if (!strcmp(line->elements[i].item, "menuentry"))
1305      continue;      continue;
1306   if (isquote(*line->elements[i].item))   if (isquote(*line->elements[i].item) && quote_char == '\0') {
1307        /* ensure we properly pair off quotes */
1308        quote_char = *line->elements[i].item;
1309      title = line->elements[i].item + 1;      title = line->elements[i].item + 1;
1310   else   } else {
1311      title = line->elements[i].item;      title = line->elements[i].item;
1312     }
1313    
1314   len = strlen(title);   len = strlen(title);
1315          if (isquote(title[len-1])) {          if (title[len-1] == quote_char) {
1316      strncat(buf, title,len-1);      strncat(buf, title,len-1);
1317      break;      break;
1318   } else {   } else {
# Line 1301  static struct grubConfig * readConfig(co Line 1323  static struct grubConfig * readConfig(co
1323    
1324      /* get extras */      /* get extras */
1325      int count = 0;      int count = 0;
1326        quote_char = '\0';
1327      for (int i = 0; i < line->numElements; i++) {      for (int i = 0; i < line->numElements; i++) {
1328   if (count >= 2) {   if (count >= 2) {
1329      strcat(extras, line->elements[i].item);      strcat(extras, line->elements[i].item);
# Line 1311  static struct grubConfig * readConfig(co Line 1334  static struct grubConfig * readConfig(co
1334      continue;      continue;
1335    
1336   /* count ' or ", there should be two in menuentry line. */   /* count ' or ", there should be two in menuentry line. */
1337   if (isquote(*line->elements[i].item))   if (isquote(*line->elements[i].item) && quote_char == '\0') {
1338        /* ensure we properly pair off quotes */
1339                quote_char = *line->elements[i].item;
1340      count++;      count++;
1341     }
1342    
1343   len = strlen(line->elements[i].item);   len = strlen(line->elements[i].item);
1344    
1345   if (isquote(line->elements[i].item[len -1]))   if (line->elements[i].item[len -1] == quote_char)
1346      count++;      count++;
1347    
1348   /* ok, we get the final ' or ", others are extras. */   /* ok, we get the final ' or ", others are extras. */
# Line 1485  static struct grubConfig * readConfig(co Line 1511  static struct grubConfig * readConfig(co
1511      return cfg;      return cfg;
1512  }  }
1513    
1514  static void writeDefault(FILE * out, char * indent,  static void writeDefault(FILE * out, char * indent,
1515   char * separator, struct grubConfig * cfg) {   char * separator, struct grubConfig * cfg) {
1516      struct singleEntry * entry;      struct singleEntry * entry;
1517      struct singleLine * line;      struct singleLine * line;
1518      int i;      int i;
1519    
1520      if (!cfg->defaultImage && cfg->flags == GRUB_CONFIG_NO_DEFAULT) return;      if (!cfg->defaultImage && cfg->flags == GRUB_CONFIG_NO_DEFAULT)
1521     return;
1522    
1523      if (cfg->defaultImage == DEFAULT_SAVED)      if (cfg->defaultImage == DEFAULT_SAVED)
1524   fprintf(out, "%sdefault%ssaved\n", indent, separator);   fprintf(out, "%sdefault%ssaved\n", indent, separator);
# Line 1515  static void writeDefault(FILE * out, cha Line 1542  static void writeDefault(FILE * out, cha
1542          fprintf(out, "%sset default=\"%d\"\n", indent,          fprintf(out, "%sset default=\"%d\"\n", indent,
1543   cfg->defaultImage);   cfg->defaultImage);
1544      } else {      } else {
1545   fprintf(out, "%sdefault%s%d\n", indent, separator,   fprintf(out, "%sdefault%s%d\n", indent, separator,
1546   cfg->defaultImage);   cfg->defaultImage);
1547      }      }
1548   } else {   } else {
1549      int image = cfg->defaultImage;      int image = cfg->defaultImage;
1550    
1551      entry = cfg->entries;      entry = cfg->entries;
1552      while (entry && entry->skip) entry = entry->next;      while (entry && entry->skip)
1553     entry = entry->next;
1554    
1555      i = 0;      i = 0;
1556      while (entry && i < image) {      while (entry && i < image) {
1557   entry = entry->next;   entry = entry->next;
1558    
1559   while (entry && entry->skip) entry = entry->next;   while (entry && entry->skip)
1560        entry = entry->next;
1561   i++;   i++;
1562      }      }
1563    
1564      if (!entry) return;      if (!entry)
1565     return;
1566    
1567      line = getLineByType(LT_TITLE, entry->lines);      line = getLineByType(LT_TITLE, entry->lines);
1568    
1569      if (line && line->numElements >= 2)      if (line && line->numElements >= 2)
1570   fprintf(out, "%sdefault%s%s\n", indent, separator,   fprintf(out, "%sdefault%s%s\n", indent, separator,
1571   line->elements[1].item);   line->elements[1].item);
1572              else if (line && (line->numElements == 1) &&              else if (line && (line->numElements == 1) &&
1573                       cfg->cfi->titleBracketed) {                       cfg->cfi->titleBracketed) {
1574   fprintf(out, "%sdefault%s%s\n", indent, separator,   char *title = extractTitle(cfg, line);
1575                          extractTitle(cfg, line));   if (title) {
1576        fprintf(out, "%sdefault%s%s\n", indent, separator, title);
1577        free(title);
1578     }
1579              }              }
1580   }   }
1581      }      }
# Line 4188  int addNewKernel(struct grubConfig * con Line 4221  int addNewKernel(struct grubConfig * con
4221      return 0;      return 0;
4222  }  }
4223    
 static void traceback(int signum)  
 {  
     void *array[40];  
     size_t size;  
   
     signal(SIGSEGV, SIG_DFL);  
     memset(array, '\0', sizeof (array));  
     size = backtrace(array, 40);  
   
     fprintf(stderr, "grubby received SIGSEGV!  Backtrace (%ld):\n",  
             (unsigned long)size);  
     backtrace_symbols_fd(array, size, STDERR_FILENO);  
     exit(1);  
 }  
   
4224  int main(int argc, const char ** argv) {  int main(int argc, const char ** argv) {
4225      poptContext optCon;      poptContext optCon;
4226      const char * grubConfig = NULL;      const char * grubConfig = NULL;
# Line 4345  int main(int argc, const char ** argv) { Line 4363  int main(int argc, const char ** argv) {
4363    
4364      useextlinuxmenu=0;      useextlinuxmenu=0;
4365    
     signal(SIGSEGV, traceback);  
   
4366      int i = 0;      int i = 0;
4367      for (int j = 1; j < argc; j++)      for (int j = 1; j < argc; j++)
4368   i += strlen(argv[j]) + 1;   i += strlen(argv[j]) + 1;
# Line 4424  int main(int argc, const char ** argv) { Line 4440  int main(int argc, const char ** argv) {
4440      }      }
4441    
4442      if (!cfi) {      if (!cfi) {
4443          if (grub2FindConfig(&grub2ConfigType))          if (grub2FindConfig(&grub2ConfigType)) {
4444      cfi = &grub2ConfigType;      cfi = &grub2ConfigType;
4445   else      if (envPath)
4446     cfi->envFile = envPath;
4447            } else
4448        #ifdef __ia64__        #ifdef __ia64__
4449      cfi = &eliloConfigType;      cfi = &eliloConfigType;
4450        #elif __powerpc__        #elif __powerpc__
# Line 4640  int main(int argc, const char ** argv) { Line 4658  int main(int argc, const char ** argv) {
4658   struct singleLine * line;   struct singleLine * line;
4659   struct singleEntry * entry;   struct singleEntry * entry;
4660    
4661   if (config->defaultImage == -1) return 0;   if (config->defaultImage == -1)
4662        return 0;
4663   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&   if (config->defaultImage == DEFAULT_SAVED_GRUB2 &&
4664   cfi->defaultIsSaved)   cfi->defaultIsSaved)
4665      config->defaultImage = 0;      config->defaultImage = 0;
4666   entry = findEntryByIndex(config, config->defaultImage);   entry = findEntryByIndex(config, config->defaultImage);
4667   if (!entry) return 0;   if (!entry)
4668     return 0;
4669    
4670   if (!configureGrub2) {   if (!configureGrub2) {
4671    line = getLineByType(LT_TITLE, entry->lines);      char *title;
4672    if (!line) return 0;      line = getLineByType(LT_TITLE, entry->lines);
4673    printf("%s\n", line->elements[1].item);      if (!line)
4674     return 0;
4675        title = extractTitle(config, line);
4676        if (!title)
4677     return 0;
4678        printf("%s\n", title);
4679        free(title);
4680   } else {   } else {
4681    char * title;      char * title;
4682    
4683    dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");      dbgPrintf("This is GRUB2, default title is embeded in menuentry\n");
4684    line = getLineByType(LT_MENUENTRY, entry->lines);      line = getLineByType(LT_MENUENTRY, entry->lines);
4685    if (!line) return 0;      if (!line)
4686    title = grub2ExtractTitle(line);   return 0;
4687    if (title)      title = grub2ExtractTitle(line);
4688      printf("%s\n", title);      if (title)
4689     printf("%s\n", title);
4690   }   }
4691   return 0;   return 0;
4692    

Legend:
Removed from v.2983  
changed lines
  Added in v.2995