ieee1275: split up grub_machine_get_bootlocation
[grub.git] / util / grub-render-label.c
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
19
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>
27 #include <grub/color.h>
28 #include <grub/util/install.h>
29 #include <grub/emu/hostdisk.h>
30
31 #define _GNU_SOURCE     1
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <errno.h>
38
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
45 #include "progname.h"
46
47 struct arguments
48 {
49   char *input;
50   char *text;
51   char *output;
52   char *font;
53   char *fgcolor;
54   char *bgcolor;
55   int verbosity;
56 };
57
58 static 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},
65   {"text",  't', N_("STRING"), 0,
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},
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
86 static error_t
87 argp_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;
92
93   switch (key)
94     {
95     case 'i':
96       arguments->input = xstrdup (arg);
97       break;
98
99     case 'b':
100       arguments->bgcolor = xstrdup (arg);
101       break;
102
103     case 'c':
104       arguments->fgcolor = xstrdup (arg);
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
130 static struct argp argp = {
131   options, argp_parser, N_("[OPTIONS]"),
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.  */
136   N_("Render Apple .disk_label."),
137   NULL, NULL, NULL
138 };
139
140 int
141 main (int argc, char *argv[])
142 {
143   char *text;
144   struct arguments arguments;
145
146   grub_util_host_init (&argc, &argv);
147
148   /* Check for options.  */
149   memset (&arguments, 0, sizeof (struct arguments));
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     {
166       FILE *in = grub_util_fopen (arguments.input, "r");
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
182   grub_init_all ();
183   grub_hostfs_init ();
184   grub_host_init ();
185
186   grub_util_render_label (arguments.font,
187                           arguments.bgcolor,
188                           arguments.fgcolor,
189                           text,
190                           arguments.output);
191
192   return 0;
193 }