resolve.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 < (buf + sizeof (buf)) && *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. if ((p - buf) == sizeof (buf))
  108. grub_util_error (_("line too long, length greater than %zu: module %s"), sizeof (buf), dep->name);
  109. }
  110. return dep_list;
  111. }
  112. static char *
  113. get_module_name (const char *str)
  114. {
  115. char *base;
  116. char *ext;
  117. base = strrchr (str, '/');
  118. if (! base)
  119. base = (char *) str;
  120. else
  121. base++;
  122. ext = strrchr (base, '.');
  123. if (ext && strcmp (ext, ".mod") == 0)
  124. {
  125. char *name;
  126. name = xmalloc (ext - base + 1);
  127. memcpy (name, base, ext - base);
  128. name[ext - base] = '\0';
  129. return name;
  130. }
  131. return xstrdup (base);
  132. }
  133. static char *
  134. get_module_path (const char *prefix, const char *str)
  135. {
  136. char *dir;
  137. char *base;
  138. char *ext;
  139. char *ret;
  140. ext = strrchr (str, '.');
  141. if (ext && strcmp (ext, ".mod") == 0)
  142. base = xstrdup (str);
  143. else
  144. {
  145. base = xmalloc (strlen (str) + 4 + 1);
  146. sprintf (base, "%s.mod", str);
  147. }
  148. dir = strchr (str, '/');
  149. if (dir)
  150. return base;
  151. ret = grub_util_get_path (prefix, base);
  152. free (base);
  153. return ret;
  154. }
  155. static void
  156. add_module (const char *dir,
  157. struct dep_list *dep_list,
  158. struct mod_list **mod_head,
  159. struct grub_util_path_list **path_head,
  160. const char *name)
  161. {
  162. char *mod_name;
  163. struct grub_util_path_list *path;
  164. struct mod_list *mod;
  165. struct dep_list *dep;
  166. mod_name = get_module_name (name);
  167. /* Check if the module has already been added. */
  168. for (mod = *mod_head; mod; mod = mod->next)
  169. if (strcmp (mod->name, mod_name) == 0)
  170. {
  171. free (mod_name);
  172. return;
  173. }
  174. /* Resolve dependencies. */
  175. for (dep = dep_list; dep; dep = dep->next)
  176. if (strcmp (dep->name, mod_name) == 0)
  177. {
  178. for (mod = dep->list; mod; mod = mod->next)
  179. add_module (dir, dep_list, mod_head, path_head, mod->name);
  180. break;
  181. }
  182. /* Add this module. */
  183. mod = (struct mod_list *) xmalloc (sizeof (*mod));
  184. mod->name = mod_name;
  185. mod->next = *mod_head;
  186. *mod_head = mod;
  187. /* Add this path. */
  188. path = (struct grub_util_path_list *) xmalloc (sizeof (*path));
  189. path->name = get_module_path (dir, name);
  190. path->next = *path_head;
  191. *path_head = path;
  192. }
  193. struct grub_util_path_list *
  194. grub_util_resolve_dependencies (const char *prefix,
  195. const char *dep_list_file,
  196. char *modules[])
  197. {
  198. char *path;
  199. FILE *fp;
  200. struct dep_list *dep_list;
  201. struct mod_list *mod_list = 0;
  202. struct grub_util_path_list *path_list = 0;
  203. path = grub_util_get_path (prefix, dep_list_file);
  204. fp = grub_util_fopen (path, "r");
  205. if (! fp)
  206. grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
  207. free (path);
  208. dep_list = read_dep_list (fp);
  209. fclose (fp);
  210. while (*modules)
  211. {
  212. add_module (prefix, dep_list, &mod_list, &path_list, *modules);
  213. modules++;
  214. }
  215. free_dep_list (dep_list);
  216. free_mod_list (mod_list);
  217. { /* Reverse the path_list */
  218. struct grub_util_path_list *p, *prev, *next;
  219. for (p = path_list, prev = NULL; p; p = next)
  220. {
  221. next = p->next;
  222. p->next = prev;
  223. prev = p;
  224. }
  225. return prev;
  226. }
  227. }
  228. void
  229. grub_util_free_path_list (struct grub_util_path_list *path_list)
  230. {
  231. struct grub_util_path_list *next;
  232. while (path_list)
  233. {
  234. next = path_list->next;
  235. free ((void *) path_list->name);
  236. free (path_list);
  237. path_list = next;
  238. }
  239. }