db1771cfbe
* commands/boot.c: New file. * commands/cat.c: Likewise. * commands/cmp.c: Likewise. * commands/ls.c: Likewise. * commands/terminal.c: Likewise. * normal/command.c: Include <pupa/env.h> and <pupa/dl.h>. (pupa_register_command): Changed interface to match the new argument parser. (pupa_command_execute): Changed (almost rewritten) so it uses pupa_split_command. Added support for setting variables using the syntax `foo=bar'. (rescue_command): Changed to work with the new argument parser. (terminal_command): Moved from here to commands/terminal.c. (set_command): New function. (unset_command): New function. (insmod_command): New function. (rmmod_command): New function. (lsmod_command): New function. (pupa_command_init): Don't initialize the command terminal anymore. Initialize the commands set, unset, insmod, rmmod and lsmod. * conf/i386-pc.rmk (kernel_img_SOURCES): Add kern/env.c. (kernel_img_HEADERS): Add arg.h and env.h. (pupa_mkimage_LDFLAGS): Add kern/env.c. (pupa_emu_SOURCES): Add kern/env.c, commands/ls.c, commands/terminal.c commands/boot.c commands/cmp.c commands/cat.c, normal/arg.c. (pkgdata_MODULES): Add ls.mod, boot.mod, cmp.mod, cat.mod and terminal.mod. (normal_mod_SOURCES): Add normal/arg.c and normal/arg.c. (boot_mod_SOURCES): New variable. (terminal_mod_SOURCES): Likewise. (ls_mod_SOURCES): Likewise. (cmp_mod_SOURCES): Likewise. (cat_mod_SOURCES): Likewise. * normal/arg.c: New file. * kern/env.c: Likewise. * include/pupa/arg.h: Likewise. * include/pupa/env.h: Likewise. * font/manager.c (font_command): Changed to match argument parsing interface changes. (PUPA_MOD_INIT): Likewise. * hello/hello.c (pupa_cmd_hello): Likewise. (PUPA_MOD_INIT): Likewise. * include/pupa/disk.h: Include <pupa/device.h>. (pupa_print_partinfo): New prototype. * include/pupa/dl.h (pupa_dl_set_prefix): Prototype removed. (pupa_dl_get_prefix): Likewise. * include/pupa/misc.h: Include <pupa/err.h>. (pupa_isgraph): New prototype. (pupa_isdigit): Likewise. (pupa_split_cmdline): Likewise. * include/pupa/normal.h: Include <pupa/arg.h>. (pupa_command): Changed the prototype of the member `func' to match the argument parsing interface. Added member `options'. (pupa_register_command): Updated to match function. (pupa_arg_parse): New prototype. (pupa_hello_init) [PUPA_UTIL]: New prototype. (pupa_hello_fini) [PUPA_UTIL]: Likewise. (pupa_ls_init) [PUPA_UTIL]: Likewise. (pupa_ls_fini) [PUPA_UTIL]: Likewise. (pupa_cat_init) [PUPA_UTIL]: Likewise. (pupa_cat_fini) [PUPA_UTIL]: Likewise. (pupa_boot_init) [PUPA_UTIL]: Likewise. (pupa_boot_fini) [PUPA_UTIL]: Likewise. (pupa_cmp_init) [PUPA_UTIL]: Likewise. (pupa_cmp_fini) [PUPA_UTIL]: Likewise. (pupa_terminal_init) [PUPA_UTIL]: Likewise. (pupa_terminal_fini) [PUPA_UTIL]: Likewise. * kern/disk.c: Include <pupa/file.h>. (pupa_print_partinfo): New function. * kern/dl.c: Include <pupa/env.h>. (pupa_dl_dir): Variable removed. (pupa_dl_load): Use the environment variable `prefix' instead of the variable pupa_dl_dir. (pupa_dl_set_prefix): Function removed. (pupa_dl_get_prefix): Likewise. * kern/i386/pc/init.c: Include <pupa/env.h>. (pupa_machine_init): Use the environment variable `prefix' instead of using pupa_dl_set_prefix to set the prefix. * kern/main.c: Include <pupa/env.h>. (pupa_set_root_dev): Use the environment variable `prefix' instead of using pupa_dl_get_prefix to get the prefix. * kern/misc.c: Include <pupa/env.h>. (pupa_isdigit): New function. (pupa_isgraph): Likewise. (pupa_ftoa): Likewise. (pupa_vsprintf): Added support for printing values of the type `double'. Make it possible to format variable output when using formatting like `%1.2%f'. (pupa_split_cmdline): New function. * kern/rescue.c: Include <pupa/env.h>. (next_word): Removed function. (pupa_rescue_cmd_prefix): Likewise. (pupa_rescue_cmd_set): New function. (pupa_rescue_cmd_unset): New function. (pupa_enter_rescue_mode): Use the `pupa_split_cmdline' function to split the command line instead of splitting it here. Added support for setting variables using the syntax `foo=bar'. Don't initialize the prefix command anymore. Initialized the set and unset commands. * normal/cmdline.c: Include <pupa/env.h>. (pupa_tab_complete): Added prototypes for print_simple_completion, print_partition_completion, add_completion, iterate_commands, iterate_dev, iterate_part and iterate_dir. Moved code to print partition information from here to kern/disk.c. (pupa_cmdline_run): Don't check if the funtion exists anymore. * normal/main.c: Include <pupa/env.h>. (pupa_rescue_cmd_normal): Use the environment variable `prefix' instead of using pupa_dl_get_prefix to get the prefix. * term/i386/pc/vga.c: Include <pupa/arg.h>. (check_vga_mem): Cast pointers to `void *' to silence a gcc warning. (pupa_vga_putchar) [! DEBUG_VGA]: Removed for this case. (pupa_vga_setcolor): Declare unused variables with `__attribute__ ((unused))' to silence a gcc warning. (pupa_vga_setcolor): Likewise. (debug_command): Changed to match argument parsing interface changes. * util/pupa-emu.c: Include <pupa/env.h>. (options): Added 0's for unused fields to silence a gcc warning. (argp): Likewise. (main): Use the environment variable `prefix' instead of using pupa_dl_set_prefix to set the prefix. Initialize the commands ls, boot, cmp, cat and terminal. Finish the commands boot, cmp, cat and terminal. * util/i386/pc/getroot.c: Include <pupa/i386/pc/util/biosdisk.h>. * util/misc.c: Include <malloc.h>. (pupa_malloc): Rewritten so errors are correctly reported. (pupa_realloc): Likewise. (pupa_memalign): Likewise. (pupa_mm_init_region): Declare unused variables with `__attribute__ ((unused))' to silence a gcc warning. * normal/i386/setjmp.S: Remove tab at the end of the file to silence a gcc warning. * loader/i386/pc/linux.c (pupa_rescue_cmd_initrd): Declare unused variables with `__attribute__ ((unused))' to silence a gcc warning. * loader/i386/pc/multiboot.c (pupa_multiboot_unload): Make the local variable i unsigned to silence a gcc warning. * kern/term.c: Include <pupa/misc.h>. (pupa_more_lines): New variable. (pupa_more): Likewise. (pupa_putcode): When the pager is active pause at the end of every screen. (pupa_set_more): New function. * include/pupa/term.h (pupa_set_more): New prototype.
380 lines
7.7 KiB
C
380 lines
7.7 KiB
C
/* main.c - the normal mode main routine */
|
|
/*
|
|
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
|
* Copyright (C) 2000,2001,2002,2003 Free Software Foundation, Inc.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
*/
|
|
|
|
#include <pupa/kernel.h>
|
|
#include <pupa/normal.h>
|
|
#include <pupa/dl.h>
|
|
#include <pupa/rescue.h>
|
|
#include <pupa/misc.h>
|
|
#include <pupa/file.h>
|
|
#include <pupa/mm.h>
|
|
#include <pupa/term.h>
|
|
#include <pupa/env.h>
|
|
|
|
pupa_jmp_buf pupa_exit_env;
|
|
|
|
#define PUPA_DEFAULT_HISTORY_SIZE 50
|
|
|
|
/* Read a line from the file FILE. */
|
|
static int
|
|
get_line (pupa_file_t file, char cmdline[], int max_len)
|
|
{
|
|
char c;
|
|
int pos = 0;
|
|
int literal = 0;
|
|
int comment = 0;
|
|
|
|
while (1)
|
|
{
|
|
if (pupa_file_read (file, &c, 1) != 1)
|
|
break;
|
|
|
|
/* Skip all carriage returns. */
|
|
if (c == '\r')
|
|
continue;
|
|
|
|
/* Replace tabs with spaces. */
|
|
if (c == '\t')
|
|
c = ' ';
|
|
|
|
/* The previous is a backslash, then... */
|
|
if (literal)
|
|
{
|
|
/* If it is a newline, replace it with a space and continue. */
|
|
if (c == '\n')
|
|
{
|
|
c = ' ';
|
|
|
|
/* Go back to overwrite the backslash. */
|
|
if (pos > 0)
|
|
pos--;
|
|
}
|
|
|
|
literal = 0;
|
|
}
|
|
|
|
if (c == '\\')
|
|
literal = 1;
|
|
|
|
if (comment)
|
|
{
|
|
if (c == '\n')
|
|
comment = 0;
|
|
}
|
|
else if (pos == 0)
|
|
{
|
|
if (c == '#')
|
|
comment = 1;
|
|
else if (! pupa_isspace (c))
|
|
cmdline[pos++] = c;
|
|
}
|
|
else
|
|
{
|
|
if (c == '\n')
|
|
break;
|
|
|
|
if (pos < max_len)
|
|
cmdline[pos++] = c;
|
|
}
|
|
}
|
|
|
|
cmdline[pos] = '\0';
|
|
|
|
return pos;
|
|
}
|
|
|
|
static void
|
|
free_menu (pupa_menu_t menu)
|
|
{
|
|
pupa_menu_entry_t entry = menu->entry_list;
|
|
|
|
while (entry)
|
|
{
|
|
pupa_menu_entry_t next_entry = entry->next;
|
|
pupa_command_list_t cmd = entry->command_list;
|
|
|
|
while (cmd)
|
|
{
|
|
pupa_command_list_t next_cmd = cmd->next;
|
|
|
|
pupa_free ((void *) cmd->command);
|
|
cmd = next_cmd;
|
|
}
|
|
|
|
pupa_free ((void *) entry->title);
|
|
entry = next_entry;
|
|
}
|
|
|
|
pupa_free (menu);
|
|
}
|
|
|
|
/* Read the config file CONFIG and return a menu. If no entry is present,
|
|
return NULL. */
|
|
static pupa_menu_t
|
|
read_config_file (const char *config)
|
|
{
|
|
pupa_file_t file;
|
|
static char cmdline[PUPA_MAX_CMDLINE];
|
|
pupa_menu_t menu;
|
|
pupa_menu_entry_t *next_entry, cur_entry = 0;
|
|
pupa_command_list_t *next_cmd, cur_cmd;
|
|
|
|
/* Try to open the config file. */
|
|
file = pupa_file_open (config);
|
|
if (! file)
|
|
return 0;
|
|
|
|
/* Initialize the menu. */
|
|
menu = (pupa_menu_t) pupa_malloc (sizeof (*menu));
|
|
if (! menu)
|
|
{
|
|
pupa_file_close (file);
|
|
return 0;
|
|
}
|
|
menu->default_entry = 0;
|
|
menu->fallback_entry = -1;
|
|
menu->timeout = -1;
|
|
menu->size = 0;
|
|
menu->entry_list = 0;
|
|
|
|
next_entry = &(menu->entry_list);
|
|
next_cmd = 0;
|
|
|
|
/* Read each line. */
|
|
while (get_line (file, cmdline, sizeof (cmdline)))
|
|
{
|
|
pupa_command_t cmd;
|
|
|
|
cmd = pupa_command_find (cmdline);
|
|
pupa_errno = PUPA_ERR_NONE;
|
|
if (! cmd)
|
|
{
|
|
pupa_printf ("Unknown command `%s' is ignored.\n", cmdline);
|
|
continue;
|
|
}
|
|
|
|
if (cmd->flags & PUPA_COMMAND_FLAG_TITLE)
|
|
{
|
|
char *p;
|
|
|
|
cur_entry = (pupa_menu_entry_t) pupa_malloc (sizeof (*cur_entry));
|
|
if (! cur_entry)
|
|
goto fail;
|
|
|
|
p = pupa_strchr (cmdline, ' ');
|
|
if (p)
|
|
cur_entry->title = pupa_strdup (p);
|
|
else
|
|
cur_entry->title = pupa_strdup ("");
|
|
|
|
if (! cur_entry->title)
|
|
{
|
|
pupa_free (cur_entry);
|
|
goto fail;
|
|
}
|
|
|
|
cur_entry->num = 0;
|
|
cur_entry->command_list = 0;
|
|
cur_entry->next = 0;
|
|
|
|
*next_entry = cur_entry;
|
|
next_entry = &(cur_entry->next);
|
|
|
|
next_cmd = &(cur_entry->command_list);
|
|
|
|
menu->size++;
|
|
}
|
|
else if (! cur_entry)
|
|
{
|
|
/* Run the command if possible. */
|
|
if (cmd->flags & PUPA_COMMAND_FLAG_MENU)
|
|
{
|
|
pupa_command_execute (cmdline);
|
|
pupa_print_error ();
|
|
pupa_errno = PUPA_ERR_NONE;
|
|
}
|
|
else
|
|
{
|
|
pupa_printf ("Invalid command `%s' is ignored.\n", cmdline);
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
cur_cmd = (pupa_command_list_t) pupa_malloc (sizeof (*cur_cmd));
|
|
if (! cur_cmd)
|
|
goto fail;
|
|
|
|
cur_cmd->command = pupa_strdup (cmdline);
|
|
if (! cur_cmd->command)
|
|
{
|
|
pupa_free (cur_cmd);
|
|
goto fail;
|
|
}
|
|
|
|
cur_cmd->next = 0;
|
|
|
|
*next_cmd = cur_cmd;
|
|
next_cmd = &(cur_cmd->next);
|
|
|
|
cur_entry->num++;
|
|
}
|
|
}
|
|
|
|
fail:
|
|
|
|
pupa_file_close (file);
|
|
|
|
/* If no entry was found or any error occurred, return NULL. */
|
|
if (menu->size == 0 || pupa_errno != PUPA_ERR_NONE)
|
|
{
|
|
free_menu (menu);
|
|
return 0;
|
|
}
|
|
|
|
/* Check values of the default entry and the fallback one. */
|
|
if (menu->fallback_entry >= menu->size)
|
|
menu->fallback_entry = -1;
|
|
|
|
if (menu->default_entry < 0 || menu->default_entry >= menu->size)
|
|
{
|
|
if (menu->fallback_entry < 0)
|
|
menu->default_entry = 0;
|
|
else
|
|
{
|
|
menu->default_entry = menu->fallback_entry;
|
|
menu->fallback_entry = -1;
|
|
}
|
|
}
|
|
|
|
return menu;
|
|
}
|
|
|
|
/* This starts the normal mode. */
|
|
void
|
|
pupa_enter_normal_mode (const char *config)
|
|
{
|
|
if (pupa_setjmp (pupa_exit_env) == 0)
|
|
pupa_normal_execute (config, 0);
|
|
}
|
|
|
|
/* Initialize the screen. */
|
|
void
|
|
pupa_normal_init_page (void)
|
|
{
|
|
pupa_cls ();
|
|
pupa_printf ("\n\
|
|
PUPA version %s\n\n",
|
|
PACKAGE_VERSION);
|
|
}
|
|
|
|
/* Read the config file CONFIG and execute the menu interface or
|
|
the command-line interface. */
|
|
void
|
|
pupa_normal_execute (const char *config, int nested)
|
|
{
|
|
pupa_menu_t menu = 0;
|
|
|
|
if (config)
|
|
{
|
|
menu = read_config_file (config);
|
|
|
|
/* Ignore any error. */
|
|
pupa_errno = PUPA_ERR_NONE;
|
|
}
|
|
|
|
if (menu)
|
|
pupa_menu_run (menu, nested);
|
|
else
|
|
pupa_cmdline_run (nested);
|
|
}
|
|
|
|
/* Enter normal mode from rescue mode. */
|
|
static void
|
|
pupa_rescue_cmd_normal (int argc, char *argv[])
|
|
{
|
|
if (argc == 0)
|
|
{
|
|
/* Guess the config filename. */
|
|
char *config;
|
|
const char *prefix;
|
|
|
|
prefix = pupa_env_get ("prefix");
|
|
if (prefix)
|
|
{
|
|
config = pupa_malloc (pupa_strlen (prefix) + sizeof ("/pupa.cfg"));
|
|
if (! config)
|
|
return;
|
|
|
|
pupa_sprintf (config, "%s/pupa.cfg", prefix);
|
|
pupa_enter_normal_mode (config);
|
|
pupa_free (config);
|
|
}
|
|
else
|
|
pupa_enter_normal_mode (0);
|
|
}
|
|
else
|
|
pupa_enter_normal_mode (argv[0]);
|
|
}
|
|
|
|
#ifdef PUPA_UTIL
|
|
void
|
|
pupa_normal_init (void)
|
|
{
|
|
pupa_set_history (PUPA_DEFAULT_HISTORY_SIZE);
|
|
|
|
/* Register a command "normal" for the rescue mode. */
|
|
pupa_rescue_register_command ("normal", pupa_rescue_cmd_normal,
|
|
"enter normal mode");
|
|
|
|
/* This registers some built-in commands. */
|
|
pupa_command_init ();
|
|
|
|
}
|
|
|
|
void
|
|
pupa_normal_fini (void)
|
|
{
|
|
pupa_set_history (0);
|
|
pupa_rescue_unregister_command ("normal");
|
|
|
|
}
|
|
#else /* ! PUPA_UTIL */
|
|
PUPA_MOD_INIT
|
|
{
|
|
/* Normal mode shouldn't be unloaded. */
|
|
pupa_dl_ref (mod);
|
|
|
|
pupa_set_history (PUPA_DEFAULT_HISTORY_SIZE);
|
|
|
|
/* Register a command "normal" for the rescue mode. */
|
|
pupa_rescue_register_command ("normal", pupa_rescue_cmd_normal,
|
|
"enter normal mode");
|
|
|
|
/* This registers some built-in commands. */
|
|
pupa_command_init ();
|
|
}
|
|
|
|
PUPA_MOD_FINI
|
|
{
|
|
pupa_set_history (0);
|
|
pupa_rescue_unregister_command ("normal");
|
|
}
|
|
#endif /* ! PUPA_UTIL */
|