grub-menulst2cfg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. if (argc >= 2 && argv[1][0] == '-')
  37. {
  38. fprintf (stdout, _("Usage: %s [INFILE [OUTFILE]]\n"), argv[0]);
  39. return 0;
  40. }
  41. if (argc >= 2)
  42. {
  43. in = fopen (argv[1], "r");
  44. if (!in)
  45. {
  46. fprintf (stderr, _("cannot open `%s': %s"),
  47. argv[1], strerror (errno));
  48. return 1;
  49. }
  50. }
  51. else
  52. in = stdin;
  53. if (argc >= 3)
  54. {
  55. out = fopen (argv[2], "w");
  56. if (!out)
  57. {
  58. if (in != stdin)
  59. fclose (in);
  60. fprintf (stderr, _("cannot open `%s': %s"),
  61. argv[2], strerror (errno));
  62. return 1;
  63. }
  64. out_fname = argv[2];
  65. }
  66. else
  67. out = stdout;
  68. while (1)
  69. {
  70. char *parsed;
  71. if (getline (&buf, &bufsize, in) < 0)
  72. break;
  73. {
  74. char *oldname = NULL;
  75. char *newsuffix;
  76. oldname = entryname;
  77. parsed = grub_legacy_parse (buf, &entryname, &newsuffix);
  78. if (newsuffix)
  79. {
  80. suffixlen += strlen (newsuffix);
  81. suffix = xrealloc (suffix, suffixlen + 1);
  82. strcat (suffix, newsuffix);
  83. }
  84. if (oldname != entryname && oldname)
  85. fprintf (out, "}\n\n");
  86. if (oldname != entryname)
  87. {
  88. char *escaped = grub_legacy_escape (entryname, strlen (entryname));
  89. fprintf (out, "menuentry \'%s\' {\n", escaped);
  90. free (escaped);
  91. free (oldname);
  92. }
  93. }
  94. if (parsed)
  95. fprintf (out, "%s%s", entryname ? " " : "", parsed);
  96. free (parsed);
  97. parsed = NULL;
  98. }
  99. if (entryname)
  100. fprintf (out, "}\n\n");
  101. if (fwrite (suffix, 1, suffixlen, out) != suffixlen)
  102. {
  103. if (out_fname)
  104. grub_util_error ("cannot write to `%s': %s",
  105. out_fname, strerror (errno));
  106. else
  107. grub_util_error ("cannot write to the stdout: %s", strerror (errno));
  108. }
  109. free (buf);
  110. free (suffix);
  111. free (entryname);
  112. if (in != stdin)
  113. fclose (in);
  114. if (out != stdout)
  115. fclose (out);
  116. return 0;
  117. }