Magellan Linux

Diff of /trunk/grubby/grubby.c

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

revision 3002 by niro, Tue Jun 27 14:11:58 2017 UTC revision 3014 by niro, Tue Jun 27 14:34:11 2017 UTC
# Line 75  struct lineElement { Line 75  struct lineElement {
75  };  };
76    
77  enum lineType_e {  enum lineType_e {
78     LT_UNIDENTIFIED = 0,
79   LT_WHITESPACE = 1 << 0,   LT_WHITESPACE = 1 << 0,
80   LT_TITLE = 1 << 1,   LT_TITLE = 1 << 1,
81   LT_KERNEL = 1 << 2,   LT_KERNEL = 1 << 2,
# Line 750  static char *sdupprintf(const char *form Line 751  static char *sdupprintf(const char *form
751   return buf;   return buf;
752  }  }
753    
754    static inline int
755    kwcmp(struct keywordTypes *kw, const char * label, int case_insensitive)
756    {
757        int kwl = strlen(kw->key);
758        int ll = strlen(label);
759        int rc;
760        int (*snc)(const char *s1, const char *s2, size_t n) =
761               case_insensitive ? strncasecmp : strncmp;
762        int (*sc)(const char *s1, const char *s2) =
763               case_insensitive ? strcasecmp : strcmp;
764    
765        rc = snc(kw->key, label, kwl);
766        if (rc)
767           return rc;
768    
769        for (int i = kwl; i < ll; i++) {
770           if (isspace(label[i]))
771               return 0;
772           if (kw->separatorChar && label[i] == kw->separatorChar)
773               return 0;
774           else if (kw->nextChar && label[i] == kw->nextChar)
775               return 0;
776           return sc(kw->key+kwl, label+kwl);
777        }
778        return 0;
779    }
780    
781  static enum lineType_e preferredLineType(enum lineType_e type,  static enum lineType_e preferredLineType(enum lineType_e type,
782   struct configFileInfo *cfi)   struct configFileInfo *cfi)
783  {  {
# Line 815  static enum lineType_e getTypeByKeyword( Line 843  static enum lineType_e getTypeByKeyword(
843   struct configFileInfo *cfi)   struct configFileInfo *cfi)
844  {  {
845   for (struct keywordTypes * kw = cfi->keywords; kw->key; kw++) {   for (struct keywordTypes * kw = cfi->keywords; kw->key; kw++) {
846   if (cfi->caseInsensitive) {   if (!kwcmp(kw, keyword, cfi->caseInsensitive))
847   if (!strcasecmp(keyword, kw->key))   return kw->type;
  return kw->type;  
  } else {  
  if (!strcmp(keyword, kw->key))  
  return kw->type;  
  }  
848   }   }
849   return LT_UNKNOWN;   return LT_UNKNOWN;
850  }  }
# Line 916  static int readFile(int fd, char **bufPt Line 939  static int readFile(int fd, char **bufPt
939    
940  static void lineInit(struct singleLine *line)  static void lineInit(struct singleLine *line)
941  {  {
942     line->type = LT_UNIDENTIFIED;
943   line->indent = NULL;   line->indent = NULL;
944   line->elements = NULL;   line->elements = NULL;
945   line->numElements = 0;   line->numElements = 0;
# Line 998  static int lineWrite(FILE * out, struct Line 1022  static int lineWrite(FILE * out, struct
1022    
1023   if (fprintf(out, "%s", line->elements[i].item) == -1)   if (fprintf(out, "%s", line->elements[i].item) == -1)
1024   return -1;   return -1;
1025   if (i < line->numElements - 1)   if (i < line->numElements - 1 || line->type == LT_SET_VARIABLE)
1026   if (fprintf(out, "%s", line->elements[i].indent) == -1)   if (fprintf(out, "%s", line->elements[i].indent) == -1)
1027   return -1;   return -1;
1028   }   }
# Line 1053  static int getNextLine(char **bufPtr, st Line 1077  static int getNextLine(char **bufPtr, st
1077   break;   break;
1078   chptr++;   chptr++;
1079   }   }
1080     if (line->type == LT_UNIDENTIFIED)
1081     line->type = getTypeByKeyword(start, cfi);
1082   element->item = strndup(start, chptr - start);   element->item = strndup(start, chptr - start);
1083   start = chptr;   start = chptr;
1084    
# Line 1118  static int getNextLine(char **bufPtr, st Line 1144  static int getNextLine(char **bufPtr, st
1144   line->type = LT_WHITESPACE;   line->type = LT_WHITESPACE;
1145   line->numElements = 0;   line->numElements = 0;
1146   }   }
1147   } else {   } else if (line->type == LT_INITRD) {
1148   struct keywordTypes *kw;   struct keywordTypes *kw;
1149    
1150   kw = getKeywordByType(line->type, cfi);   kw = getKeywordByType(line->type, cfi);
# Line 1180  static int getNextLine(char **bufPtr, st Line 1206  static int getNextLine(char **bufPtr, st
1206   }   }
1207   }   }
1208   }   }
1209     } else if (line->type == LT_SET_VARIABLE) {
1210     /* and if it's a "set blah=" we need to split it
1211     * yet a third way to avoid rhbz# XXX FIXME :/
1212     */
1213     char *eq;
1214     int l;
1215     int numElements = line->numElements;
1216     struct lineElement *newElements;
1217     eq = strchr(line->elements[1].item, '=');
1218     if (!eq)
1219     return 0;
1220     l = eq - line->elements[1].item;
1221     if (eq[1] != 0)
1222     numElements++;
1223     newElements = calloc(numElements,sizeof (*newElements));
1224     memcpy(&newElements[0], &line->elements[0],
1225           sizeof (newElements[0]));
1226     newElements[1].item =
1227     strndup(line->elements[1].item, l);
1228     newElements[1].indent = "=";
1229     *(eq++) = '\0';
1230     newElements[2].item = strdup(eq);
1231     free(line->elements[1].item);
1232     if (line->elements[1].indent)
1233     newElements[2].indent = line->elements[1].indent;
1234     for (int i = 2; i < line->numElements; i++) {
1235     newElements[i+1].item = line->elements[i].item;
1236     newElements[i+1].indent =
1237     line->elements[i].indent;
1238     }
1239     free(line->elements);
1240     line->elements = newElements;
1241     line->numElements = numElements;
1242   }   }
1243   }   }
1244    
# Line 1285  static struct grubConfig *readConfig(con Line 1344  static struct grubConfig *readConfig(con
1344      getKeywordByType(LT_DEFAULT, cfi);      getKeywordByType(LT_DEFAULT, cfi);
1345   if (kwType && line->numElements == 3   if (kwType && line->numElements == 3
1346      && !strcmp(line->elements[1].item, kwType->key)      && !strcmp(line->elements[1].item, kwType->key)
1347      && !is_special_grub2_variable(line->elements[2].      && !is_special_grub2_variable(
1348    item)) {   line->elements[2].item)) {
1349   dbgPrintf("Line sets default config\n");   dbgPrintf("Line sets default config\n");
1350   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;   cfg->flags &= ~GRUB_CONFIG_NO_DEFAULT;
1351   defaultLine = line;   defaultLine = line;
1352   }   }
   
1353   } else if (iskernel(line->type)) {   } else if (iskernel(line->type)) {
1354   /* if by some freak chance this is multiboot and the   /* if by some freak chance this is multiboot and the
1355   * "module" lines came earlier in the template, make   * "module" lines came earlier in the template, make
# Line 1545  static struct grubConfig *readConfig(con Line 1603  static struct grubConfig *readConfig(con
1603   }   }
1604   }   }
1605   } else if (cfi->defaultIsVariable) {   } else if (cfi->defaultIsVariable) {
1606   char *value = defaultLine->elements[2].item;   if (defaultLine->numElements == 2) {
1607   while (*value && (*value == '"' || *value == '\'' ||   char *value = defaultLine->elements[1].item + 8;
1608    *value == ' ' || *value == '\t'))   while (*value && (*value == '"' ||
1609   value++;    *value == '\'' ||
1610   cfg->defaultImage = strtol(value, &end, 10);    *value == ' ' ||
1611   while (*end && (*end == '"' || *end == '\'' ||    *value == '\t'))
1612   *end == ' ' || *end == '\t'))   value++;
1613   end++;   cfg->defaultImage = strtol(value, &end, 10);
1614   if (*end)   while (*end && (*end == '"' || *end == '\'' ||
1615   cfg->defaultImage = -1;   *end == ' ' || *end == '\t'))
1616     end++;
1617     if (*end)
1618     cfg->defaultImage = -1;
1619     } else if (defaultLine->numElements == 3) {
1620     char *value = defaultLine->elements[2].item;
1621     while (*value && (*value == '"' ||
1622      *value == '\'' ||
1623      *value == ' ' ||
1624      *value == '\t'))
1625     value++;
1626     cfg->defaultImage = strtol(value, &end, 10);
1627     while (*end && (*end == '"' || *end == '\'' ||
1628     *end == ' ' || *end == '\t'))
1629     end++;
1630     if (*end)
1631     cfg->defaultImage = -1;
1632     }
1633   } else if (cfi->defaultSupportSaved &&   } else if (cfi->defaultSupportSaved &&
1634     !strncmp(defaultLine->elements[1].item, "saved",     !strncmp(defaultLine->elements[1].item, "saved",
1635      5)) {      5)) {
# Line 4135  int addNewKernel(struct grubConfig *conf Line 4210  int addNewKernel(struct grubConfig *conf
4210   const char *newKernelArgs, const char *newKernelInitrd,   const char *newKernelArgs, const char *newKernelInitrd,
4211   const char **extraInitrds, int extraInitrdCount,   const char **extraInitrds, int extraInitrdCount,
4212   const char *newMBKernel, const char *newMBKernelArgs,   const char *newMBKernel, const char *newMBKernelArgs,
4213   const char *newDevTreePath)   const char *newDevTreePath, int newIndex)
4214  {  {
4215   struct singleEntry *new;   struct singleEntry *new, *entry, *prev = NULL;
4216   struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL;   struct singleLine *newLine = NULL, *tmplLine = NULL, *masterLine = NULL;
4217   int needs;   int needs;
4218     char *indexs;
4219   char *chptr;   char *chptr;
4220     int rc;
4221    
4222   if (!newKernelPath)   if (!newKernelPath)
4223   return 0;   return 0;
4224    
4225     rc = asprintf(&indexs, "%d", newIndex);
4226     if (rc < 0)
4227     return 1;
4228    
4229   /* if the newKernelTitle is too long silently munge it into something   /* if the newKernelTitle is too long silently munge it into something
4230   * we can live with. truncating is first check, then we'll just mess with   * we can live with. truncating is first check, then we'll just mess with
4231   * it until it looks better */   * it until it looks better */
# Line 4167  int addNewKernel(struct grubConfig *conf Line 4248  int addNewKernel(struct grubConfig *conf
4248   new = malloc(sizeof(*new));   new = malloc(sizeof(*new));
4249   new->skip = 0;   new->skip = 0;
4250   new->multiboot = 0;   new->multiboot = 0;
  new->next = config->entries;  
4251   new->lines = NULL;   new->lines = NULL;
4252   config->entries = new;   entry = config->entries;
4253     for (unsigned int i = 0; i < newIndex; i++) {
4254     if (!entry)
4255     break;
4256     prev = entry;
4257     entry = entry->next;
4258     }
4259     new->next = entry;
4260    
4261     if (prev)
4262     prev->next = new;
4263     else
4264     config->entries = new;
4265    
4266   /* copy/update from the template */   /* copy/update from the template */
4267   needs = NEED_KERNEL | NEED_TITLE;   needs = NEED_KERNEL | NEED_TITLE;
# Line 4632  int addNewKernel(struct grubConfig *conf Line 4724  int addNewKernel(struct grubConfig *conf
4724   abort();   abort();
4725   }   }
4726    
4727   if (updateImage(config, "0", prefix, newKernelArgs, NULL,   if (updateImage(config, indexs, prefix, newKernelArgs, NULL,
4728   newMBKernelArgs, NULL))   newMBKernelArgs, NULL))
4729   return 1;   return 1;
4730    
# Line 4662  int main(int argc, const char **argv) Line 4754  int main(int argc, const char **argv)
4754   char *newDevTreePath = NULL;   char *newDevTreePath = NULL;
4755   char *newMBKernel = NULL;   char *newMBKernel = NULL;
4756   char *newMBKernelArgs = NULL;   char *newMBKernelArgs = NULL;
4757     int newIndex = 0;
4758   char *removeMBKernelArgs = NULL;   char *removeMBKernelArgs = NULL;
4759   char *removeMBKernel = NULL;   char *removeMBKernel = NULL;
4760   char *bootPrefix = NULL;   char *bootPrefix = NULL;
# Line 4696  int main(int argc, const char **argv) Line 4789  int main(int argc, const char **argv)
4789   NULL},   NULL},
4790   {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,   {"boot-filesystem", 0, POPT_ARG_STRING, &bootPrefix, 0,
4791   _   _
4792   ("filestystem which contains /boot directory (for testing only)"),   ("filesystem which contains /boot directory (for testing only)"),
4793   _("bootfs")},   _("bootfs")},
4794  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)  #if defined(__i386__) || defined(__x86_64__) || defined (__powerpc64__) || defined (__ia64__)
4795   {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,   {"bootloader-probe", 0, POPT_ARG_NONE, &bootloaderProbe, 0,
# Line 4768  int main(int argc, const char **argv) Line 4861  int main(int argc, const char **argv)
4861   {"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,   {"set-default-index", 0, POPT_ARG_INT, &defaultIndex, 0,
4862   _("make the given entry index the default entry"),   _("make the given entry index the default entry"),
4863   _("entry-index")},   _("entry-index")},
4864     {"set-index", 0, POPT_ARG_INT, &newIndex, 0,
4865     _("use the given index when creating a new entry"),
4866     _("entry-index")},
4867   {"silo", 0, POPT_ARG_NONE, &configureSilo, 0,   {"silo", 0, POPT_ARG_NONE, &configureSilo, 0,
4868   _("configure silo bootloader")},   _("configure silo bootloader")},
4869   {"title", 0, POPT_ARG_STRING, &newKernelTitle, 0,   {"title", 0, POPT_ARG_STRING, &newKernelTitle, 0,
# Line 5183  int main(int argc, const char **argv) Line 5279  int main(int argc, const char **argv)
5279   if (addNewKernel(config, template, bootPrefix, newKernelPath,   if (addNewKernel(config, template, bootPrefix, newKernelPath,
5280   newKernelTitle, newKernelArgs, newKernelInitrd,   newKernelTitle, newKernelArgs, newKernelInitrd,
5281   (const char **)extraInitrds, extraInitrdCount,   (const char **)extraInitrds, extraInitrdCount,
5282   newMBKernel, newMBKernelArgs, newDevTreePath))   newMBKernel, newMBKernelArgs, newDevTreePath,
5283     newIndex))
5284   return 1;   return 1;
5285    
5286   if (numEntries(config) == 0) {   if (numEntries(config) == 0) {

Legend:
Removed from v.3002  
changed lines
  Added in v.3014