Remove several trivially-unnecessary uses of nested functions.

* grub-core/commands/i386/pc/sendkey.c
(grub_cmd_sendkey: find_key_code, find_ascii_code): Make static
instead of nested.
* grub-core/commands/legacycfg.c (legacy_file: getline): Likewise.
Rename to ...
(legacy_file_getline): ... this.
* grub-core/commands/loadenv.c (grub_cmd_load_env: set_var):
Likewise.
* grub-core/kern/corecmd.c (grub_core_cmd_set: print_env): Likewise.
* grub-core/kern/fs.c (grub_fs_probe: dummy_func): Likewise.  Rename
to ...
(probe_dummy_iter): ... this.
* grub-core/kern/i386/coreboot/mmap.c
(grub_linuxbios_table_iterate: check_signature): Likewise.
* grub-core/kern/parser.c (grub_parser_split_cmdline:
check_varstate): Likewise.  Mark inline.
* grub-core/lib/arg.c (find_short: fnd_short): Likewise.  Pass
an additional parameter.
(find_long: fnd_long): Likewise.  Pass two additional parameters.
* grub-core/lib/crc.c (init_crc32c_table: reflect): Likewise.
* grub-core/lib/crc64.c (init_crc64_table: reflect): Likewise.
* grub-core/lib/ieee1275/cmos.c (grub_cmos_find_port: hook):
Likewise.  Rename to ...
(grub_cmos_find_port_iter): ... this.
* grub-core/lib/ieee1275/datetime.c (find_rtc: hook): Likewise.
Rename to ...
(find_rtc_iter): ... this.

* grub-core/normal/menu_entry.c (run): Fold nested editor_getsource
function directly into the function body, since it is only called
once.
This commit is contained in:
Colin Watson 2012-12-31 17:31:38 +00:00
parent 33a68ac643
commit 5c67ea6cd9
14 changed files with 273 additions and 235 deletions

View file

@ -1,3 +1,39 @@
2012-12-31 Colin Watson <cjwatson@ubuntu.com>
Remove several trivially-unnecessary uses of nested functions.
* grub-core/commands/i386/pc/sendkey.c
(grub_cmd_sendkey: find_key_code, find_ascii_code): Make static
instead of nested.
* grub-core/commands/legacycfg.c (legacy_file: getline): Likewise.
Rename to ...
(legacy_file_getline): ... this.
* grub-core/commands/loadenv.c (grub_cmd_load_env: set_var):
Likewise.
* grub-core/kern/corecmd.c (grub_core_cmd_set: print_env): Likewise.
* grub-core/kern/fs.c (grub_fs_probe: dummy_func): Likewise. Rename
to ...
(probe_dummy_iter): ... this.
* grub-core/kern/i386/coreboot/mmap.c
(grub_linuxbios_table_iterate: check_signature): Likewise.
* grub-core/kern/parser.c (grub_parser_split_cmdline:
check_varstate): Likewise. Mark inline.
* grub-core/lib/arg.c (find_short: fnd_short): Likewise. Pass
an additional parameter.
(find_long: fnd_long): Likewise. Pass two additional parameters.
* grub-core/lib/crc.c (init_crc32c_table: reflect): Likewise.
* grub-core/lib/crc64.c (init_crc64_table: reflect): Likewise.
* grub-core/lib/ieee1275/cmos.c (grub_cmos_find_port: hook):
Likewise. Rename to ...
(grub_cmos_find_port_iter): ... this.
* grub-core/lib/ieee1275/datetime.c (find_rtc: hook): Likewise.
Rename to ...
(find_rtc_iter): ... this.
* grub-core/normal/menu_entry.c (run): Fold nested editor_getsource
function directly into the function body, since it is only called
once.
2012-12-30 Colin Watson <cjwatson@ubuntu.com>
* grub-core/bus/usb/ehci.c (grub_ehci_pci_iter): Remove incorrect

View file

@ -286,16 +286,10 @@ grub_sendkey_preboot (int noret __attribute__ ((unused)))
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_sendkey (grub_extcmd_context_t ctxt, int argc, char **args)
/* Helper for grub_cmd_sendkey. */
static int
find_key_code (char *key)
{
struct grub_arg_list *state = ctxt->state;
auto int find_key_code (char *key);
auto int find_ascii_code (char *key);
int find_key_code (char *key)
{
unsigned i;
for (i = 0; i < sizeof (keysym_table) / sizeof (keysym_table[0]); i++)
@ -309,10 +303,12 @@ grub_cmd_sendkey (grub_extcmd_context_t ctxt, int argc, char **args)
}
return 0;
}
}
int find_ascii_code (char *key)
{
/* Helper for grub_cmd_sendkey. */
static int
find_ascii_code (char *key)
{
unsigned i;
for (i = 0; i < sizeof (keysym_table) / sizeof (keysym_table[0]); i++)
@ -326,7 +322,12 @@ grub_cmd_sendkey (grub_extcmd_context_t ctxt, int argc, char **args)
}
return 0;
}
}
static grub_err_t
grub_cmd_sendkey (grub_extcmd_context_t ctxt, int argc, char **args)
{
struct grub_arg_list *state = ctxt->state;
andmask = 0xffffffff;
ormask = 0;

View file

@ -35,6 +35,14 @@
GRUB_MOD_LICENSE ("GPLv3+");
/* Helper for legacy_file. */
static grub_err_t
legacy_file_getline (char **line, int cont __attribute__ ((unused)))
{
*line = 0;
return GRUB_ERR_NONE;
}
static grub_err_t
legacy_file (const char *filename)
{
@ -43,14 +51,6 @@ legacy_file (const char *filename)
grub_menu_t menu;
char *suffix = grub_strdup ("");
auto grub_err_t getline (char **line, int cont);
grub_err_t getline (char **line,
int cont __attribute__ ((unused)))
{
*line = 0;
return GRUB_ERR_NONE;
}
if (!suffix)
return grub_errno;
@ -134,7 +134,7 @@ legacy_file (const char *filename)
if (parsed && !entryname)
{
grub_normal_parse_line (parsed, getline);
grub_normal_parse_line (parsed, legacy_file_getline);
grub_print_error ();
grub_free (parsed);
parsed = NULL;
@ -180,7 +180,7 @@ legacy_file (const char *filename)
grub_free (args);
}
grub_normal_parse_line (suffix, getline);
grub_normal_parse_line (suffix, legacy_file_getline);
grub_print_error ();
grub_free (suffix);
grub_free (entrysrc);

View file

@ -114,6 +114,14 @@ read_envblk_file (grub_file_t file)
return envblk;
}
/* Helper for grub_cmd_load_env. */
static int
set_var (const char *name, const char *value)
{
grub_env_set (name, value);
return 0;
}
static grub_err_t
grub_cmd_load_env (grub_extcmd_context_t ctxt,
int argc __attribute__ ((unused)),
@ -123,13 +131,6 @@ grub_cmd_load_env (grub_extcmd_context_t ctxt,
grub_file_t file;
grub_envblk_t envblk;
auto int set_var (const char *name, const char *value);
int set_var (const char *name, const char *value)
{
grub_env_set (name, value);
return 0;
}
file = open_envblk_file ((state[0].set) ? state[0].arg : 0);
if (! file)
return grub_errno;

View file

@ -28,6 +28,14 @@
#include <grub/command.h>
#include <grub/i18n.h>
/* Helper for grub_core_cmd_set. */
static int
print_env (struct grub_env_var *env)
{
grub_printf ("%s=%s\n", env->name, env->value);
return 0;
}
/* set ENVVAR=VALUE */
static grub_err_t
grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
@ -36,14 +44,6 @@ grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
char *var;
char *val;
auto int print_env (struct grub_env_var *env);
int print_env (struct grub_env_var *env)
{
grub_printf ("%s=%s\n", env->name, env->value);
return 0;
}
if (argc < 1)
{
grub_env_iterate (print_env);

View file

@ -32,19 +32,19 @@ grub_fs_t grub_fs_list = 0;
grub_fs_autoload_hook_t grub_fs_autoload_hook = 0;
grub_fs_t
grub_fs_probe (grub_device_t device)
{
grub_fs_t p;
auto int dummy_func (const char *filename,
const struct grub_dirhook_info *info);
int dummy_func (const char *filename __attribute__ ((unused)),
/* Helper for grub_fs_probe. */
static int
probe_dummy_iter (const char *filename __attribute__ ((unused)),
const struct grub_dirhook_info *info __attribute__ ((unused)))
{
return 1;
}
grub_fs_t
grub_fs_probe (grub_device_t device)
{
grub_fs_t p;
if (device->disk)
{
/* Make it sure not to have an infinite recursive calls. */
@ -69,7 +69,7 @@ grub_fs_probe (grub_device_t device)
}
else
#endif
(p->dir) (device, "/", dummy_func);
(p->dir) (device, "/", probe_dummy_iter);
if (grub_errno == GRUB_ERR_NONE)
return p;
@ -93,7 +93,7 @@ grub_fs_probe (grub_device_t device)
{
p = grub_fs_list;
(p->dir) (device, "/", dummy_func);
(p->dir) (device, "/", probe_dummy_iter);
if (grub_errno == GRUB_ERR_NONE)
{
count--;

View file

@ -22,21 +22,22 @@
#include <grub/err.h>
#include <grub/misc.h>
/* Helper for grub_linuxbios_table_iterate. */
static int
check_signature (grub_linuxbios_table_header_t tbl_header)
{
if (! grub_memcmp (tbl_header->signature, "LBIO", 4))
return 1;
return 0;
}
static grub_err_t
grub_linuxbios_table_iterate (int (*hook) (grub_linuxbios_table_item_t))
{
grub_linuxbios_table_header_t table_header;
grub_linuxbios_table_item_t table_item;
auto int check_signature (grub_linuxbios_table_header_t);
int check_signature (grub_linuxbios_table_header_t tbl_header)
{
if (! grub_memcmp (tbl_header->signature, "LBIO", 4))
return 1;
return 0;
}
/* Assuming table_header is aligned to its size (8 bytes). */
for (table_header = (grub_linuxbios_table_header_t) 0x500;

View file

@ -96,6 +96,16 @@ grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
}
/* Helper for grub_parser_split_cmdline. */
static inline int
check_varstate (grub_parser_state_t s)
{
return (s == GRUB_PARSER_STATE_VARNAME
|| s == GRUB_PARSER_STATE_VARNAME2
|| s == GRUB_PARSER_STATE_QVARNAME
|| s == GRUB_PARSER_STATE_QVARNAME2);
}
grub_err_t
grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
int *argc, char ***argv)
@ -111,16 +121,6 @@ grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
char *args;
int i;
auto int check_varstate (grub_parser_state_t s);
int check_varstate (grub_parser_state_t s)
{
return (s == GRUB_PARSER_STATE_VARNAME
|| s == GRUB_PARSER_STATE_VARNAME2
|| s == GRUB_PARSER_STATE_QVARNAME
|| s == GRUB_PARSER_STATE_QVARNAME2);
}
auto void add_var (grub_parser_state_t newstate);
void add_var (grub_parser_state_t newstate)

View file

@ -34,14 +34,10 @@ static const struct grub_arg_option help_options[] =
{0, 0, 0, 0, 0, 0}
};
/* Helper for find_short. */
static struct grub_arg_option *
find_short (const struct grub_arg_option *options, char c)
fnd_short (const struct grub_arg_option *opt, char c)
{
struct grub_arg_option *found = 0;
auto struct grub_arg_option *fnd_short (const struct grub_arg_option *opt);
struct grub_arg_option *fnd_short (const struct grub_arg_option *opt)
{
while (opt->doc)
{
if (opt->shortarg == c)
@ -49,10 +45,15 @@ find_short (const struct grub_arg_option *options, char c)
opt++;
}
return 0;
}
}
static struct grub_arg_option *
find_short (const struct grub_arg_option *options, char c)
{
struct grub_arg_option *found = 0;
if (options)
found = fnd_short (options);
found = fnd_short (options, c);
if (! found)
{
@ -74,14 +75,10 @@ find_short (const struct grub_arg_option *options, char c)
return found;
}
/* Helper for find_long. */
static struct grub_arg_option *
find_long (const struct grub_arg_option *options, const char *s, int len)
fnd_long (const struct grub_arg_option *opt, const char *s, int len)
{
struct grub_arg_option *found = 0;
auto struct grub_arg_option *fnd_long (const struct grub_arg_option *opt);
struct grub_arg_option *fnd_long (const struct grub_arg_option *opt)
{
while (opt->doc)
{
if (opt->longarg && ! grub_strncmp (opt->longarg, s, len) &&
@ -90,13 +87,18 @@ find_long (const struct grub_arg_option *options, const char *s, int len)
opt++;
}
return 0;
}
}
static struct grub_arg_option *
find_long (const struct grub_arg_option *options, const char *s, int len)
{
struct grub_arg_option *found = 0;
if (options)
found = fnd_long (options);
found = fnd_long (options, s, len);
if (! found)
found = fnd_long (help_options);
found = fnd_long (help_options, s, len);
return found;
}

View file

@ -22,12 +22,10 @@
static grub_uint32_t crc32c_table [256];
static void
init_crc32c_table (void)
/* Helper for init_crc32c_table. */
static grub_uint32_t
reflect (grub_uint32_t ref, int len)
{
auto grub_uint32_t reflect (grub_uint32_t ref, int len);
grub_uint32_t reflect (grub_uint32_t ref, int len)
{
grub_uint32_t result = 0;
int i;
@ -39,8 +37,11 @@ init_crc32c_table (void)
}
return result;
}
}
static void
init_crc32c_table (void)
{
grub_uint32_t polynomial = 0x1edc6f41;
int i, j;

View file

@ -25,12 +25,10 @@ GRUB_MOD_LICENSE ("GPLv3+");
static grub_uint64_t crc64_table [256];
static void
init_crc64_table (void)
/* Helper for init_crc64_table. */
static grub_uint64_t
reflect (grub_uint64_t ref, int len)
{
auto grub_uint64_t reflect (grub_uint64_t ref, int len);
grub_uint64_t reflect (grub_uint64_t ref, int len)
{
grub_uint64_t result = 0;
int i;
@ -42,8 +40,11 @@ init_crc64_table (void)
}
return result;
}
}
static void
init_crc64_table (void)
{
grub_uint64_t polynomial = 0x42f0e1eba9ea3693ULL;
int i, j;

View file

@ -23,12 +23,11 @@
#include <grub/misc.h>
volatile grub_uint8_t *grub_cmos_port = 0;
grub_err_t
grub_cmos_find_port (void)
/* Helper for grub_cmos_find_port. */
static int
grub_cmos_find_port_iter (struct grub_ieee1275_devalias *alias)
{
auto int hook (struct grub_ieee1275_devalias *alias);
int hook (struct grub_ieee1275_devalias *alias)
{
grub_ieee1275_phandle_t dev;
grub_uint32_t addr[2];
grub_ssize_t actual;
@ -65,9 +64,12 @@ grub_cmos_find_port (void)
}
#endif
return 0;
}
}
grub_ieee1275_devices_iterate (hook);
grub_err_t
grub_cmos_find_port (void)
{
grub_ieee1275_devices_iterate (grub_cmos_find_port_iter);
if (!grub_cmos_port)
return grub_error (GRUB_ERR_IO, "no cmos found");

View file

@ -30,12 +30,10 @@ GRUB_MOD_LICENSE ("GPLv3+");
static char *rtc = 0;
static int no_ieee1275_rtc = 0;
static void
find_rtc (void)
/* Helper for find_rtc. */
static int
find_rtc_iter (struct grub_ieee1275_devalias *alias)
{
auto int hook (struct grub_ieee1275_devalias *alias);
int hook (struct grub_ieee1275_devalias *alias)
{
if (grub_strcmp (alias->type, "rtc") == 0)
{
grub_dprintf ("datetime", "Found RTC %s\n", alias->path);
@ -43,9 +41,12 @@ find_rtc (void)
return 1;
}
return 0;
}
}
grub_ieee1275_devices_iterate (hook);
static void
find_rtc (void)
{
grub_ieee1275_devices_iterate (find_rtc_iter);
if (!rtc)
no_ieee1275_rtc = 1;
}

View file

@ -1205,32 +1205,6 @@ run (struct screen *screen)
grub_menu_t menu = NULL;
char *dummy[1] = { NULL };
auto char * editor_getsource (void);
char * editor_getsource (void)
{
int i;
grub_size_t size = 0, tot_size = 0;
char *source;
for (i = 0; i < screen->num_lines; i++)
tot_size += grub_get_num_of_utf8_bytes (screen->lines[i].buf,
screen->lines[i].len) + 1;
source = grub_malloc (tot_size + 1);
if (! source)
return NULL;
for (i = 0; i < screen->num_lines; i++)
{
size += grub_ucs4_to_utf8 (screen->lines[i].buf, screen->lines[i].len,
(grub_uint8_t *) source + size,
tot_size - size);
source[size++] = '\n';
}
source[size] = '\0';
return source;
}
grub_cls ();
grub_printf (" ");
grub_printf_ (N_("Booting a command list"));
@ -1248,9 +1222,27 @@ run (struct screen *screen)
}
/* Execute the script, line for line. */
script = editor_getsource ();
{
int i;
grub_size_t size = 0, tot_size = 0;
for (i = 0; i < screen->num_lines; i++)
tot_size += grub_get_num_of_utf8_bytes (screen->lines[i].buf,
screen->lines[i].len) + 1;
script = grub_malloc (tot_size + 1);
if (! script)
return 0;
for (i = 0; i < screen->num_lines; i++)
{
size += grub_ucs4_to_utf8 (screen->lines[i].buf, screen->lines[i].len,
(grub_uint8_t *) script + size,
tot_size - size);
script[size++] = '\n';
}
script[size] = '\0';
}
grub_script_execute_sourcecode (script, 0, dummy);
grub_free (script);