grub-render-label.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2012,2013 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/util/misc.h>
  20. #include <grub/i18n.h>
  21. #include <grub/term.h>
  22. #include <grub/font.h>
  23. #include <grub/gfxmenu_view.h>
  24. #include <grub/color.h>
  25. #include <grub/util/install.h>
  26. #include <grub/emu/hostdisk.h>
  27. #define _GNU_SOURCE 1
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #pragma GCC diagnostic ignored "-Wmissing-prototypes"
  34. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  35. #include <argp.h>
  36. #pragma GCC diagnostic error "-Wmissing-prototypes"
  37. #pragma GCC diagnostic error "-Wmissing-declarations"
  38. #include "progname.h"
  39. struct arguments
  40. {
  41. char *input;
  42. char *text;
  43. char *output;
  44. char *font;
  45. char *fgcolor;
  46. char *bgcolor;
  47. int verbosity;
  48. };
  49. static struct argp_option options[] = {
  50. {"input", 'i', N_("FILE"), 0,
  51. N_("read text from FILE."), 0},
  52. {"color", 'c', N_("COLOR"), 0,
  53. N_("use COLOR for text"), 0},
  54. {"bgcolor", 'b', N_("COLOR"), 0,
  55. N_("use COLOR for background"), 0},
  56. {"text", 't', N_("STRING"), 0,
  57. /* TRANSLATORS: The result is always stored to file and
  58. never shown directly, so don't use "show" as synonym for render. Use "create" if
  59. "render" doesn't translate directly. */
  60. N_("set the label to render"), 0},
  61. {"output", 'o', N_("FILE"), 0,
  62. N_("set output filename. Default is STDOUT"), 0},
  63. {"font", 'f', N_("FILE"), 0,
  64. N_("use FILE as font (PF2)."), 0},
  65. {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
  66. { 0, 0, 0, 0, 0, 0 }
  67. };
  68. #include <grub/err.h>
  69. #include <grub/types.h>
  70. #include <grub/dl.h>
  71. #include <grub/misc.h>
  72. #include <grub/mm.h>
  73. #include <grub/video.h>
  74. #include <grub/video_fb.h>
  75. static error_t
  76. argp_parser (int key, char *arg, struct argp_state *state)
  77. {
  78. /* Get the input argument from argp_parse, which we
  79. know is a pointer to our arguments structure. */
  80. struct arguments *arguments = state->input;
  81. switch (key)
  82. {
  83. case 'i':
  84. arguments->input = xstrdup (arg);
  85. break;
  86. case 'b':
  87. arguments->bgcolor = xstrdup (arg);
  88. break;
  89. case 'c':
  90. arguments->fgcolor = xstrdup (arg);
  91. break;
  92. case 'f':
  93. arguments->font = xstrdup (arg);
  94. break;
  95. case 't':
  96. arguments->text = xstrdup (arg);
  97. break;
  98. case 'o':
  99. arguments->output = xstrdup (arg);
  100. break;
  101. case 'v':
  102. arguments->verbosity++;
  103. break;
  104. default:
  105. return ARGP_ERR_UNKNOWN;
  106. }
  107. return 0;
  108. }
  109. static struct argp argp = {
  110. options, argp_parser, N_("[OPTIONS]"),
  111. /* TRANSLATORS: This file takes a text and creates a graphical representation of it,
  112. putting the result into .disk_label file. The result is always stored to file and
  113. never shown directly, so don't use "show" as synonym for render. Use "create" if
  114. "render" doesn't translate directly. */
  115. N_("Render Apple .disk_label."),
  116. NULL, NULL, NULL
  117. };
  118. int
  119. main (int argc, char *argv[])
  120. {
  121. char *text;
  122. struct arguments arguments;
  123. grub_util_host_init (&argc, &argv);
  124. /* Check for options. */
  125. memset (&arguments, 0, sizeof (struct arguments));
  126. if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
  127. {
  128. fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
  129. exit(1);
  130. }
  131. if ((!arguments.input && !arguments.text) || !arguments.font)
  132. {
  133. fprintf (stderr, "%s", _("Missing arguments\n"));
  134. exit(1);
  135. }
  136. if (arguments.text)
  137. text = arguments.text;
  138. else
  139. {
  140. FILE *in = grub_util_fopen (arguments.input, "r");
  141. size_t s;
  142. if (!in)
  143. grub_util_error (_("cannot open `%s': %s"), arguments.input,
  144. strerror (errno));
  145. fseek (in, 0, SEEK_END);
  146. s = ftell (in);
  147. fseek (in, 0, SEEK_SET);
  148. text = xmalloc (s + 1);
  149. if (fread (text, 1, s, in) != s)
  150. grub_util_error (_("cannot read `%s': %s"), arguments.input,
  151. strerror (errno));
  152. text[s] = 0;
  153. fclose (in);
  154. }
  155. grub_init_all ();
  156. grub_hostfs_init ();
  157. grub_host_init ();
  158. grub_util_render_label (arguments.font,
  159. arguments.bgcolor,
  160. arguments.fgcolor,
  161. text,
  162. arguments.output);
  163. return 0;
  164. }