resolve.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2007 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <grub/emu/misc.h>
  25. #include <grub/misc.h>
  26. #include <grub/util/misc.h>
  27. #include <grub/util/resolve.h>
  28. #include <grub/i18n.h>
  29. /* Module. */
  30. struct mod_list
  31. {
  32. const char *name;
  33. struct mod_list *next;
  34. };
  35. /* Dependency. */
  36. struct dep_list
  37. {
  38. const char *name;
  39. struct mod_list *list;
  40. struct dep_list *next;
  41. };
  42. static char buf[1024];
  43. static void
  44. free_mod_list (struct mod_list *head)
  45. {
  46. while (head)
  47. {
  48. struct mod_list *next;
  49. next = head->next;
  50. free ((void *) head->name);
  51. free (head);
  52. head = next;
  53. }
  54. }
  55. static void
  56. free_dep_list (struct dep_list *head)
  57. {
  58. while (head)
  59. {
  60. struct dep_list *next;
  61. next = head->next;
  62. free ((void *) head->name);
  63. free_mod_list (head->list);
  64. free (head);
  65. head = next;
  66. }
  67. }
  68. /* Read the list of dependencies. */
  69. static struct dep_list *
  70. read_dep_list (FILE *fp)
  71. {
  72. struct dep_list *dep_list = 0;
  73. while (fgets (buf, sizeof (buf), fp))
  74. {
  75. char *p;
  76. struct dep_list *dep;
  77. /* Get the target name. */
  78. p = strchr (buf, ':');
  79. if (! p)
  80. grub_util_error (_("invalid line format: %s"), buf);
  81. *p++ = '\0';
  82. dep = xmalloc (sizeof (*dep));
  83. dep->name = xstrdup (buf);
  84. dep->list = 0;
  85. dep->next = dep_list;
  86. dep_list = dep;
  87. /* Add dependencies. */
  88. while (*p)
  89. {
  90. struct mod_list *mod;
  91. char *name;
  92. /* Skip whitespace. */
  93. while (*p && grub_isspace (*p))
  94. p++;
  95. if (! *p)
  96. break;
  97. name = p;
  98. /* Skip non-whitespace. */
  99. while (*p && ! grub_isspace (*p))
  100. p++;
  101. *p++ = '\0';
  102. mod = (struct mod_list *) xmalloc (sizeof (*mod));
  103. mod->name = xstrdup (name);
  104. mod->next = dep->list;
  105. dep->list = mod;
  106. }
  107. }
  108. return dep_list;
  109. }
  110. static char *
  111. get_module_name (const char *str)
  112. {
  113. char *base;
  114. char *ext;
  115. base = strrchr (str, '/');
  116. if (! base)
  117. base = (char *) str;
  118. else
  119. base++;
  120. ext = strrchr (base, '.');
  121. if (ext && strcmp (ext, ".mod") == 0)
  122. {
  123. char *name;
  124. name = xmalloc (ext - base + 1);
  125. memcpy (name, base, ext - base);
  126. name[ext - base] = '\0';
  127. return name;
  128. }
  129. return xstrdup (base);
  130. }
  131. static char *
  132. get_module_path (const char *prefix, const char *str)
  133. {
  134. char *dir;
  135. char *base;
  136. char *ext;
  137. char *ret;
  138. ext = strrchr (str, '.');
  139. if (ext && strcmp (ext, ".mod") == 0)
  140. base = xstrdup (str);
  141. else
  142. {
  143. base = xmalloc (strlen (str) + 4 + 1);
  144. sprintf (base, "%s.mod", str);
  145. }
  146. dir = strchr (str, '/');
  147. if (dir)
  148. return base;
  149. ret = grub_util_get_path (prefix, base);
  150. free (base);
  151. return ret;
  152. }
  153. static void
  154. add_module (const char *dir,
  155. struct dep_list *dep_list,
  156. struct mod_list **mod_head,
  157. struct grub_util_path_list **path_head,
  158. const char *name)
  159. {
  160. char *mod_name;
  161. struct grub_util_path_list *path;
  162. struct mod_list *mod;
  163. struct dep_list *dep;
  164. mod_name = get_module_name (name);
  165. /* Check if the module has already been added. */
  166. for (mod = *mod_head; mod; mod = mod->next)
  167. if (strcmp (mod->name, mod_name) == 0)
  168. {
  169. free (mod_name);
  170. return;
  171. }
  172. /* Resolve dependencies. */
  173. for (dep = dep_list; dep; dep = dep->next)
  174. if (strcmp (dep->name, mod_name) == 0)
  175. {
  176. for (mod = dep->list; mod; mod = mod->next)
  177. add_module (dir, dep_list, mod_head, path_head, mod->name);
  178. break;
  179. }
  180. /* Add this module. */
  181. mod = (struct mod_list *) xmalloc (sizeof (*mod));
  182. mod->name = mod_name;
  183. mod->next = *mod_head;
  184. *mod_head = mod;
  185. /* Add this path. */
  186. path = (struct grub_util_path_list *) xmalloc (sizeof (*path));
  187. path->name = get_module_path (dir, name);
  188. path->next = *path_head;
  189. *path_head = path;
  190. }
  191. struct grub_util_path_list *
  192. grub_util_resolve_dependencies (const char *prefix,
  193. const char *dep_list_file,
  194. char *modules[])
  195. {
  196. char *path;
  197. FILE *fp;
  198. struct dep_list *dep_list;
  199. struct mod_list *mod_list = 0;
  200. struct grub_util_path_list *path_list = 0;
  201. path = grub_util_get_path (prefix, dep_list_file);
  202. fp = grub_util_fopen (path, "r");
  203. if (! fp)
  204. grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
  205. free (path);
  206. dep_list = read_dep_list (fp);
  207. fclose (fp);
  208. while (*modules)
  209. {
  210. add_module (prefix, dep_list, &mod_list, &path_list, *modules);
  211. modules++;
  212. }
  213. free_dep_list (dep_list);
  214. free_mod_list (mod_list);
  215. { /* Reverse the path_list */
  216. struct grub_util_path_list *p, *prev, *next;
  217. for (p = path_list, prev = NULL; p; p = next)
  218. {
  219. next = p->next;
  220. p->next = prev;
  221. prev = p;
  222. }
  223. return prev;
  224. }
  225. }
  226. void
  227. grub_util_free_path_list (struct grub_util_path_list *path_list)
  228. {
  229. struct grub_util_path_list *next;
  230. while (path_list)
  231. {
  232. next = path_list->next;
  233. free ((void *) path_list->name);
  234. free (path_list);
  235. path_list = next;
  236. }
  237. }