* util/grub-mkfont.c: Migrate to argp.

* util/grub-mklayout.c: Likewise.
	* util/grub-mkpasswd-pbkdf2.c: Likewise.
	* util/grub-mkrelpath.c: Likewise.
	* util/grub-probe.c: Likewise.
	* util/grub-script-check.c: Likewise.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2012-02-03 21:45:43 +01:00
parent 4459819a52
commit 8e1e4e3903
8 changed files with 632 additions and 596 deletions

View file

@ -30,7 +30,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#define _GNU_SOURCE 1
#include <argp.h>
#include <assert.h>
#include <ft2build.h>
#include FT_FREETYPE_H
@ -94,60 +97,7 @@ struct grub_font_info
int num_glyphs;
};
static struct option options[] =
{
{"output", required_argument, 0, 'o'},
{"name", required_argument, 0, 'n'},
{"index", required_argument, 0, 'i'},
{"range", required_argument, 0, 'r'},
{"size", required_argument, 0, 's'},
{"desc", required_argument, 0, 'd'},
{"asce", required_argument, 0, 'c'},
{"bold", no_argument, 0, 'b'},
{"no-bitmap", no_argument, 0, 0x100},
{"no-hinting", no_argument, 0, 0x101},
{"force-autohint", no_argument, 0, 'a'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
{"ascii-bitmaps", no_argument, 0, 0x102},
{"width-spec", no_argument, 0, 0x103},
{0, 0, 0, 0}
};
int font_verbosity;
static void
usage (int status)
{
if (status)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
printf (_("\
Usage: %s [OPTIONS] FONT_FILES\n\
\nOptions:\n\
-o, --output=FILE_NAME set output file name\n\
--ascii-bitmaps save only the ASCII bitmaps\n\
--width-spec create width summary file\n\
-i, --index=N set face index\n\
-r, --range=A-B[,C-D] set font range\n\
-n, --name=S set font family name\n\
-s, --size=N set font size\n\
-d, --desc=N set font descent\n\
-c, --asce=N set font ascent\n\
-b, --bold convert to bold font\n\
-a, --force-autohint force autohint\n\
--no-hinting disable hinting\n\
--no-bitmap ignore bitmap strikes when loading\n\
-h, --help display this message and exit\n\
-V, --version print version information and exit\n\
-v, --verbose print verbose messages\n\
\n\
Report bugs to <%s>.\n"), program_name, PACKAGE_BUGREPORT);
exit (status);
}
static int font_verbosity;
void
add_pixel (grub_uint8_t **data, int *mask, int not_blank)
@ -996,167 +946,213 @@ write_font_pf2 (struct grub_font_info *font_info, char *output_file)
fclose (file);
}
static struct argp_option options[] = {
{"output", 'o', N_("FILE"), 0, N_("set output file"), 0},
{"ascii-bitmaps", 0x102, 0, 0, N_("save only the ASCII bitmaps"), 0},
{"width-spec", 0x103, 0, 0, N_("create width summary file"), 0},
{"index", 'i', N_("NUM"), 0, N_("set face index"), 0},
{"range", 'r', N_("FROM-TO[,FROM-TO]"), 0, N_("set font range"), 0},
{"name", 'n', N_("NAME"), 0, N_("set font family name"), 0},
{"size", 's', N_("STR"), 0, N_("set font size"), 0},
{"desc", 'd', N_("NUM"), 0, N_("set font descent"), 0},
{"asce", 'c', N_("NUM"), 0, N_("set font ascent"), 0},
{"bold", 'b', 0, 0, N_("convert to bold font"), 0},
{"force-autohint", 'a', 0, 0, N_("force autohint"), 0},
{"no-hinting", 0x101, 0, 0, N_("disable hinting"), 0},
{"no-bitmap", 0x100, 0, 0, N_("ignore bitmap strikes when loading"), 0},
{"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
{ 0, 0, 0, 0, 0, 0 }
};
struct arguments
{
struct grub_font_info font_info;
size_t nfiles;
size_t files_max;
char **files;
char *output_file;
int font_index;
int font_size;
enum file_formats file_format;
};
static error_t
argp_parser (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
char *p;
switch (key)
{
case 'b':
arguments->font_info.flags |= GRUB_FONT_FLAG_BOLD;
break;
case 0x100:
arguments->font_info.flags |= GRUB_FONT_FLAG_NOBITMAP;
break;
case 0x101:
arguments->font_info.flags |= GRUB_FONT_FLAG_NOHINTING;
break;
case 'a':
arguments->font_info.flags |= GRUB_FONT_FLAG_FORCEHINT;
break;
case 'o':
arguments->output_file = xstrdup (arg);
break;
case 'n':
arguments->font_info.name = xstrdup (arg);
break;
case 'i':
arguments->font_index = strtoul (arg, NULL, 0);
break;
case 's':
arguments->font_size = strtoul (arg, NULL, 0);
break;
case 'r':
{
char *p = arg;
while (1)
{
grub_uint32_t a, b;
a = strtoul (p, &p, 0);
if (*p != '-')
grub_util_error (_("invalid font range"));
b = strtoul (p + 1, &p, 0);
if ((arguments->font_info.num_range
& (GRUB_FONT_RANGE_BLOCK - 1)) == 0)
arguments->font_info.ranges = xrealloc (arguments->font_info.ranges,
(arguments->font_info.num_range +
GRUB_FONT_RANGE_BLOCK) *
sizeof (grub_uint32_t) * 2);
arguments->font_info.ranges[arguments->font_info.num_range * 2] = a;
arguments->font_info.ranges[arguments->font_info.num_range * 2 + 1] = b;
arguments->font_info.num_range++;
if (*p)
{
if (*p != ',')
grub_util_error (_("invalid font range"));
else
p++;
}
else
break;
}
break;
}
case 'd':
arguments->font_info.desc = strtoul (arg, NULL, 0);
break;
case 'e':
arguments->font_info.asce = strtoul (arg, NULL, 0);
break;
case 'v':
font_verbosity++;
break;
case 0x102:
arguments->file_format = ASCII_BITMAPS;
break;
case 0x103:
arguments->file_format = WIDTH_SPEC;
break;
case ARGP_KEY_ARG:
assert (arguments->nfiles < arguments->files_max);
arguments->files[arguments->nfiles++] = xstrdup(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options, argp_parser, N_("[OPTIONS] FONT_FILES"),
N_("Convert common font file formats into PF2"),
NULL, NULL, NULL
};
int
main (int argc, char *argv[])
{
struct grub_font_info font_info;
FT_Library ft_lib;
int font_index = 0;
int font_size = 0;
char *output_file = NULL;
enum file_formats file_format = PF2;
memset (&font_info, 0, sizeof (font_info));
struct arguments arguments;
size_t i;
set_program_name (argv[0]);
grub_util_init_nls ();
/* Check for options. */
while (1)
memset (&arguments, 0, sizeof (struct arguments));
arguments.file_format = PF2;
arguments.files_max = argc + 1;
arguments.files = xmalloc ((arguments.files_max + 1)
* sizeof (arguments.files[0]));
memset (arguments.files, 0, (arguments.files_max + 1)
* sizeof (arguments.files[0]));
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
{
int c = getopt_long (argc, argv, "bao:n:i:s:d:r:hVv", options, 0);
if (c == -1)
break;
else
switch (c)
{
case 'b':
font_info.flags |= GRUB_FONT_FLAG_BOLD;
break;
case 0x100:
font_info.flags |= GRUB_FONT_FLAG_NOBITMAP;
break;
case 0x101:
font_info.flags |= GRUB_FONT_FLAG_NOHINTING;
break;
case 'a':
font_info.flags |= GRUB_FONT_FLAG_FORCEHINT;
break;
case 'o':
output_file = optarg;
break;
case 'n':
font_info.name = optarg;
break;
case 'i':
font_index = strtoul (optarg, NULL, 0);
break;
case 's':
font_size = strtoul (optarg, NULL, 0);
break;
case 'r':
{
char *p = optarg;
while (1)
{
grub_uint32_t a, b;
a = strtoul (p, &p, 0);
if (*p != '-')
grub_util_error (_("invalid font range"));
b = strtoul (p + 1, &p, 0);
if ((font_info.num_range & (GRUB_FONT_RANGE_BLOCK - 1)) == 0)
font_info.ranges = xrealloc (font_info.ranges,
(font_info.num_range +
GRUB_FONT_RANGE_BLOCK) *
sizeof (grub_uint32_t) * 2);
font_info.ranges[font_info.num_range * 2] = a;
font_info.ranges[font_info.num_range * 2 + 1] = b;
font_info.num_range++;
if (*p)
{
if (*p != ',')
grub_util_error (_("invalid font range"));
else
p++;
}
else
break;
}
break;
}
case 'd':
font_info.desc = strtoul (optarg, NULL, 0);
break;
case 'e':
font_info.asce = strtoul (optarg, NULL, 0);
break;
case 'h':
usage (0);
break;
case 'V':
printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
return 0;
case 'v':
font_verbosity++;
break;
case 0x102:
file_format = ASCII_BITMAPS;
break;
case 0x103:
file_format = WIDTH_SPEC;
break;
default:
usage (1);
break;
}
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
exit(1);
}
if (file_format == ASCII_BITMAPS && font_info.num_range > 0)
if (arguments.file_format == ASCII_BITMAPS
&& arguments.font_info.num_range > 0)
{
grub_util_error (_("Option --ascii-bitmaps doesn't accept ranges (use ASCII)."));
return 1;
}
else if (file_format == ASCII_BITMAPS)
else if (arguments.file_format == ASCII_BITMAPS)
{
font_info.ranges = xrealloc (font_info.ranges,
GRUB_FONT_RANGE_BLOCK *
sizeof (grub_uint32_t) * 2);
font_info.ranges[0] = (grub_uint32_t) 0x00;
font_info.ranges[1] = (grub_uint32_t) 0x7f;
font_info.num_range = 1;
arguments.font_info.ranges = xrealloc (arguments.font_info.ranges,
GRUB_FONT_RANGE_BLOCK *
sizeof (grub_uint32_t) * 2);
arguments.font_info.ranges[0] = (grub_uint32_t) 0x00;
arguments.font_info.ranges[1] = (grub_uint32_t) 0x7f;
arguments.font_info.num_range = 1;
}
if (! output_file)
if (! arguments.output_file)
grub_util_error (_("no output file is specified"));
if (FT_Init_FreeType (&ft_lib))
grub_util_error (_("FT_Init_FreeType fails"));
for (; optind < argc; optind++)
for (i = 0; i < arguments.nfiles; i++)
{
FT_Face ft_face;
int size;
FT_Error err;
err = FT_New_Face (ft_lib, argv[optind], font_index, &ft_face);
err = FT_New_Face (ft_lib, arguments.files[i],
arguments.font_index, &ft_face);
if (err)
{
grub_printf (_("can't open file %s, index %d: error %d"),
argv[optind], font_index, err);
arguments.files[i],
arguments.font_index, err);
if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
printf (": %s\n", ft_errmsgs[err]);
else
@ -1165,10 +1161,10 @@ main (int argc, char *argv[])
continue;
}
if ((! font_info.name) && (ft_face->family_name))
font_info.name = xstrdup (ft_face->family_name);
if ((! arguments.font_info.name) && (ft_face->family_name))
arguments.font_info.name = xstrdup (ft_face->family_name);
size = font_size;
size = arguments.font_size;
if (! size)
{
if ((ft_face->face_flags & FT_FACE_FLAG_SCALABLE) ||
@ -1178,12 +1174,13 @@ main (int argc, char *argv[])
size = ft_face->available_sizes[0].height;
}
font_info.style = ft_face->style_flags;
font_info.size = size;
arguments.font_info.style = ft_face->style_flags;
arguments.font_info.size = size;
if (FT_Set_Pixel_Sizes (ft_face, size, size))
grub_util_error (_("can't set %dx%d font size"), size, size);
add_font (&font_info, ft_face, file_format != PF2);
grub_util_error (_("can't set %dx%d font size"),
size, size);
add_font (&arguments.font_info, ft_face, arguments.file_format != PF2);
FT_Done_Face (ft_face);
}
@ -1196,46 +1193,52 @@ main (int argc, char *argv[])
memset (counter, 0, sizeof (counter));
for (cur = font_info.glyphs_unsorted; cur; cur = cur->next)
for (cur = arguments.font_info.glyphs_unsorted; cur; cur = cur->next)
counter[(cur->char_code & 0xffff) + 1]++;
for (i = 0; i < 0x10000; i++)
counter[i+1] += counter[i];
tmp = xmalloc (font_info.num_glyphs
tmp = xmalloc (arguments.font_info.num_glyphs
* sizeof (tmp[0]));
for (cur = font_info.glyphs_unsorted; cur; cur = cur->next)
for (cur = arguments.font_info.glyphs_unsorted; cur; cur = cur->next)
tmp[counter[(cur->char_code & 0xffff)]++] = *cur;
memset (counter, 0, sizeof (counter));
for (cur = tmp; cur < tmp + font_info.num_glyphs; cur++)
for (cur = tmp; cur < tmp + arguments.font_info.num_glyphs; cur++)
counter[((cur->char_code & 0xffff0000) >> 16) + 1]++;
for (i = 0; i < 0x10000; i++)
counter[i+1] += counter[i];
font_info.glyphs_sorted = xmalloc (font_info.num_glyphs
* sizeof (font_info.glyphs_sorted[0]));
for (cur = tmp; cur < tmp + font_info.num_glyphs; cur++)
font_info.glyphs_sorted[counter[(cur->char_code & 0xffff0000) >> 16]++]
= *cur;
arguments.font_info.glyphs_sorted = xmalloc (arguments.font_info.num_glyphs
* sizeof (arguments.font_info.glyphs_sorted[0]));
for (cur = tmp; cur < tmp + arguments.font_info.num_glyphs; cur++)
arguments.font_info.glyphs_sorted[counter[(cur->char_code & 0xffff0000)
>> 16]++] = *cur;
free (tmp);
}
switch (file_format)
switch (arguments.file_format)
{
case PF2:
write_font_pf2 (&font_info, output_file);
write_font_pf2 (&arguments.font_info, arguments.output_file);
break;
case ASCII_BITMAPS:
write_font_ascii_bitmap (&font_info, output_file);
write_font_ascii_bitmap (&arguments.font_info, arguments.output_file);
break;
case WIDTH_SPEC:
write_font_width_spec (&font_info, output_file);
write_font_width_spec (&arguments.font_info, arguments.output_file);
break;
}
if (font_verbosity > 1)
print_glyphs (&font_info);
print_glyphs (&arguments.font_info);
{
size_t i;
for (i = 0; i < arguments.nfiles; i++)
free (arguments.files[i]);
}
return 0;
}