grub-menulst2cfg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 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 <grub/legacy_parse.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <grub/util/misc.h>
  24. #include <grub/misc.h>
  25. #include <grub/i18n.h>
  26. int
  27. main (int argc, char **argv)
  28. {
  29. FILE *in, *out;
  30. char *entryname = NULL;
  31. char *buf = NULL;
  32. size_t bufsize = 0;
  33. char *suffix = xstrdup ("");
  34. int suffixlen = 0;
  35. const char *out_fname = 0;
  36. grub_util_host_init (&argc, &argv);
  37. if (argc >= 2 && argv[1][0] == '-')
  38. {
  39. fprintf (stdout, _("Usage: %s [INFILE [OUTFILE]]\n"), argv[0]);
  40. return 0;
  41. }
  42. if (argc >= 2)
  43. {
  44. in = grub_util_fopen (argv[1], "r");
  45. if (!in)
  46. {
  47. fprintf (stderr, _("cannot open `%s': %s"),
  48. argv[1], strerror (errno));
  49. return 1;
  50. }
  51. }
  52. else
  53. in = stdin;
  54. if (argc >= 3)
  55. {
  56. out = grub_util_fopen (argv[2], "w");
  57. if (!out)
  58. {
  59. if (in != stdin)
  60. fclose (in);
  61. fprintf (stderr, _("cannot open `%s': %s"),
  62. argv[2], strerror (errno));
  63. return 1;
  64. }
  65. out_fname = argv[2];
  66. }
  67. else
  68. out = stdout;
  69. while (1)
  70. {
  71. char *parsed;
  72. if (getline (&buf, &bufsize, in) < 0)
  73. break;
  74. {
  75. char *oldname = NULL;
  76. char *newsuffix;
  77. oldname = entryname;
  78. parsed = grub_legacy_parse (buf, &entryname, &newsuffix);
  79. if (newsuffix)
  80. {
  81. suffixlen += strlen (newsuffix);
  82. suffix = xrealloc (suffix, suffixlen + 1);
  83. strcat (suffix, newsuffix);
  84. }
  85. if (oldname != entryname && oldname)
  86. fprintf (out, "}\n\n");
  87. if (oldname != entryname)
  88. {
  89. char *escaped = grub_legacy_escape (entryname, strlen (entryname));
  90. fprintf (out, "menuentry \'%s\' {\n", escaped);
  91. free (escaped);
  92. free (oldname);
  93. }
  94. }
  95. if (parsed)
  96. fprintf (out, "%s%s", entryname ? " " : "", parsed);
  97. free (parsed);
  98. parsed = NULL;
  99. }
  100. if (entryname)
  101. fprintf (out, "}\n\n");
  102. if (fwrite (suffix, 1, suffixlen, out) != suffixlen)
  103. {
  104. if (out_fname)
  105. grub_util_error ("cannot write to `%s': %s",
  106. out_fname, strerror (errno));
  107. else
  108. grub_util_error ("cannot write to the stdout: %s", strerror (errno));
  109. }
  110. free (buf);
  111. free (suffix);
  112. free (entryname);
  113. if (in != stdin)
  114. fclose (in);
  115. if (out != stdout)
  116. fclose (out);
  117. return 0;
  118. }