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