autolist.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG 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. * BURG 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 BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/types.h>
  19. #include <grub/list.h>
  20. #include <grub/font.h>
  21. #include <grub/lib.h>
  22. #include <grub/mm.h>
  23. #include <grub/env.h>
  24. #include <grub/misc.h>
  25. GRUB_EXPORT (grub_autolist_load);
  26. GRUB_EXPORT (grub_autolist_font);
  27. grub_autolist_t grub_autolist_font;
  28. static void
  29. insert_item (grub_autolist_t *list, char *name, char *file)
  30. {
  31. grub_autolist_t p;
  32. p = grub_malloc (sizeof (*p));
  33. if (! p)
  34. return;
  35. p->name = grub_malloc (grub_strlen (name) + grub_strlen (file) + 2);
  36. if (! p->name)
  37. {
  38. grub_free (p);
  39. return;
  40. }
  41. grub_strcpy (p->name, name);
  42. p->value = p->name + grub_strlen (name) + 1;
  43. grub_strcpy (p->value, file);
  44. grub_list_push (GRUB_AS_LIST_P (list), GRUB_AS_LIST (p));
  45. }
  46. grub_autolist_t
  47. grub_autolist_load (const char *name)
  48. {
  49. grub_autolist_t result = 0;
  50. const char *prefix;
  51. prefix = grub_env_get ("prefix");
  52. if (prefix)
  53. {
  54. char *filename;
  55. filename = grub_xasprintf ("%s/%s", prefix, name);
  56. if (filename)
  57. {
  58. grub_file_t file;
  59. file = grub_file_open (filename);
  60. if (file)
  61. {
  62. char *buf = NULL;
  63. for (;; grub_free (buf))
  64. {
  65. char *p;
  66. buf = grub_getline (file);
  67. if (! buf)
  68. break;
  69. if (! grub_isgraph (buf[0]))
  70. continue;
  71. p = grub_strchr (buf, ':');
  72. if (! p)
  73. continue;
  74. *p = '\0';
  75. while (*++p == ' ')
  76. ;
  77. insert_item (&result, buf, p);
  78. }
  79. grub_file_close (file);
  80. }
  81. grub_free (filename);
  82. }
  83. }
  84. return result;
  85. }