2004-03-14 Marco Gerards <metgerards@student.han.nl>
* 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.
This commit is contained in:
parent
3b1139cb9d
commit
db1771cfbe
35 changed files with 2607 additions and 285 deletions
42
kern/disk.c
42
kern/disk.c
|
@ -24,6 +24,7 @@
|
|||
#include <pupa/machine/partition.h>
|
||||
#include <pupa/misc.h>
|
||||
#include <pupa/machine/time.h>
|
||||
#include <pupa/file.h>
|
||||
|
||||
#define PUPA_CACHE_TIMEOUT 2
|
||||
|
||||
|
@ -498,3 +499,44 @@ pupa_disk_write (pupa_disk_t disk, unsigned long sector,
|
|||
|
||||
return pupa_errno;
|
||||
}
|
||||
|
||||
pupa_err_t
|
||||
pupa_print_partinfo (pupa_device_t disk, char *partname)
|
||||
{
|
||||
pupa_fs_t fs = 0;
|
||||
pupa_device_t part;
|
||||
char devname[20];
|
||||
|
||||
pupa_sprintf (devname, "%s,%s", disk->disk->name, partname);
|
||||
part = pupa_device_open (devname);
|
||||
if (!part)
|
||||
pupa_printf ("\tPartition num:%s, Filesystem cannot be accessed",
|
||||
partname);
|
||||
else
|
||||
{
|
||||
char *label;
|
||||
|
||||
fs = pupa_fs_probe (part);
|
||||
/* Ignore all errors. */
|
||||
pupa_errno = 0;
|
||||
|
||||
pupa_printf ("\tPartition num:%s, Filesystem type %s",
|
||||
partname, fs ? fs->name : "Unknown");
|
||||
|
||||
if (fs)
|
||||
{
|
||||
(fs->label) (part, &label);
|
||||
if (pupa_errno == PUPA_ERR_NONE)
|
||||
{
|
||||
if (label && pupa_strlen (label))
|
||||
pupa_printf (", Label: %s", label);
|
||||
pupa_free (label);
|
||||
}
|
||||
pupa_errno = PUPA_ERR_NONE;
|
||||
}
|
||||
pupa_device_close (part);
|
||||
}
|
||||
|
||||
pupa_printf ("\n");
|
||||
return pupa_errno;
|
||||
}
|
||||
|
|
18
kern/dl.c
18
kern/dl.c
|
@ -1,7 +1,7 @@
|
|||
/* dl.c - loadable module support */
|
||||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Free Software Foundation, Inc.
|
||||
* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
|
||||
*
|
||||
* PUPA is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
@ -27,6 +27,7 @@
|
|||
#include <pupa/types.h>
|
||||
#include <pupa/symbol.h>
|
||||
#include <pupa/file.h>
|
||||
#include <pupa/env.h>
|
||||
|
||||
#if PUPA_HOST_SIZEOF_VOID_P == 4
|
||||
|
||||
|
@ -556,14 +557,13 @@ pupa_dl_load_file (const char *filename)
|
|||
return mod;
|
||||
}
|
||||
|
||||
static char *pupa_dl_dir;
|
||||
|
||||
/* Load a module using a symbolic name. */
|
||||
pupa_dl_t
|
||||
pupa_dl_load (const char *name)
|
||||
{
|
||||
char *filename;
|
||||
pupa_dl_t mod;
|
||||
char *pupa_dl_dir = pupa_env_get ("prefix");
|
||||
|
||||
mod = pupa_dl_get (name);
|
||||
if (mod)
|
||||
|
@ -664,15 +664,3 @@ pupa_dl_unload_all (void)
|
|||
p->mod->ref_count--;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
pupa_dl_set_prefix (const char *dir)
|
||||
{
|
||||
pupa_dl_dir = (char *) dir;
|
||||
}
|
||||
|
||||
const char *
|
||||
pupa_dl_get_prefix (void)
|
||||
{
|
||||
return pupa_dl_dir;
|
||||
}
|
||||
|
|
209
kern/env.c
Normal file
209
kern/env.c
Normal file
|
@ -0,0 +1,209 @@
|
|||
/* env.c - Environment variables */
|
||||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2003 Free Software Foundation, Inc.
|
||||
*
|
||||
* PUPA 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 PUPA; if not, write to the Free Software
|
||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <pupa/env.h>
|
||||
#include <pupa/misc.h>
|
||||
#include <pupa/mm.h>
|
||||
|
||||
/* XXX: What would be a good size for the hashtable? */
|
||||
#define HASHSZ 123
|
||||
|
||||
/* A hashtable for quick lookup of variables. */
|
||||
static struct pupa_env_var *pupa_env[HASHSZ];
|
||||
|
||||
/* The variables in a sorted list. */
|
||||
static struct pupa_env_var *pupa_env_sorted;
|
||||
|
||||
/* Return the hash representation of the string S. */
|
||||
static unsigned int pupa_env_hashval (const char *s)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
|
||||
/* XXX: This can be done much more effecient. */
|
||||
while (*s)
|
||||
i += 5 * *(s++);
|
||||
|
||||
return i % HASHSZ;
|
||||
}
|
||||
|
||||
static struct pupa_env_var *
|
||||
pupa_env_find (const char *name)
|
||||
{
|
||||
struct pupa_env_var *var;
|
||||
int idx = pupa_env_hashval (name);
|
||||
|
||||
for (var = pupa_env[idx]; var; var = var->next)
|
||||
if (! pupa_strcmp (var->name, name))
|
||||
return var;
|
||||
return 0;
|
||||
}
|
||||
|
||||
pupa_err_t
|
||||
pupa_env_set (const char *var, const char *val)
|
||||
{
|
||||
int idx = pupa_env_hashval (var);
|
||||
struct pupa_env_var *env;
|
||||
struct pupa_env_var *sort;
|
||||
struct pupa_env_var **sortp;
|
||||
|
||||
/* If the variable does already exist, just update the variable. */
|
||||
env = pupa_env_find (var);
|
||||
if (env)
|
||||
{
|
||||
char *old = env->value;
|
||||
env->value = pupa_strdup (val);
|
||||
if (! env->name)
|
||||
{
|
||||
env->value = old;
|
||||
return pupa_errno;
|
||||
}
|
||||
|
||||
if (env->write_hook)
|
||||
(env->write_hook) (env);
|
||||
|
||||
pupa_free (old);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The variable does not exist, create it. */
|
||||
env = pupa_malloc (sizeof (struct pupa_env_var));
|
||||
if (! env)
|
||||
return pupa_errno;
|
||||
|
||||
pupa_memset (env, 0, sizeof (struct pupa_env_var));
|
||||
|
||||
env->name = pupa_strdup (var);
|
||||
if (! env->name)
|
||||
goto fail;
|
||||
|
||||
env->value = pupa_strdup (val);
|
||||
if (! env->name)
|
||||
goto fail;
|
||||
|
||||
/* Insert it in the hashtable. */
|
||||
env->prevp = &pupa_env[idx];
|
||||
env->next = pupa_env[idx];
|
||||
if (pupa_env[idx])
|
||||
pupa_env[idx]->prevp = &env->next;
|
||||
pupa_env[idx] = env;
|
||||
|
||||
/* Insert it in the sorted list. */
|
||||
sortp = &pupa_env_sorted;
|
||||
sort = pupa_env_sorted;
|
||||
while (sort)
|
||||
{
|
||||
if (pupa_strcmp (sort->name, var) > 0)
|
||||
break;
|
||||
|
||||
sortp = &sort->sort_next;
|
||||
sort = sort->sort_next;
|
||||
}
|
||||
env->sort_prevp = sortp;
|
||||
env->sort_next = sort;
|
||||
if (sort)
|
||||
sort->sort_prevp = &env->sort_next;
|
||||
*sortp = env;
|
||||
|
||||
fail:
|
||||
if (pupa_errno)
|
||||
{
|
||||
pupa_free (env->name);
|
||||
pupa_free (env->value);
|
||||
pupa_free (env);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
pupa_env_get (const char *name)
|
||||
{
|
||||
struct pupa_env_var *env;
|
||||
env = pupa_env_find (name);
|
||||
if (! env)
|
||||
return 0;
|
||||
|
||||
if (env->read_hook)
|
||||
{
|
||||
char *val;
|
||||
env->read_hook (env, &val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
return env->value;
|
||||
}
|
||||
|
||||
void
|
||||
pupa_env_unset (const char *name)
|
||||
{
|
||||
struct pupa_env_var *env;
|
||||
env = pupa_env_find (name);
|
||||
if (! env)
|
||||
return;
|
||||
|
||||
/* XXX: It is not possible to unset variables with a read or write
|
||||
hook. */
|
||||
if (env->read_hook || env->write_hook)
|
||||
return;
|
||||
|
||||
*env->prevp = env->next;
|
||||
if (env->next)
|
||||
env->next->prevp = env->prevp;
|
||||
|
||||
*env->sort_prevp = env->sort_next;
|
||||
if (env->sort_next)
|
||||
env->sort_next->sort_prevp = env->sort_prevp;
|
||||
|
||||
pupa_free (env->name);
|
||||
pupa_free (env->value);
|
||||
pupa_free (env);
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
pupa_env_iterate (int (* func) (struct pupa_env_var *var))
|
||||
{
|
||||
struct pupa_env_var *env;
|
||||
|
||||
for (env = pupa_env_sorted; env; env = env->sort_next)
|
||||
if (func (env))
|
||||
return;
|
||||
}
|
||||
|
||||
pupa_err_t
|
||||
pupa_register_variable_hook (const char *var,
|
||||
pupa_err_t (*read_hook) (struct pupa_env_var *var, char **),
|
||||
pupa_err_t (*write_hook) (struct pupa_env_var *var))
|
||||
{
|
||||
struct pupa_env_var *env = pupa_env_find (var);
|
||||
|
||||
if (! env)
|
||||
if (pupa_env_set (var, "") != PUPA_ERR_NONE)
|
||||
return pupa_errno;
|
||||
|
||||
env = pupa_env_find (var);
|
||||
/* XXX Insert an assertion? */
|
||||
|
||||
env->read_hook = read_hook;
|
||||
env->write_hook = write_hook;
|
||||
|
||||
return PUPA_ERR_NONE;
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Free Software Foundation, Inc.
|
||||
* Copyright (C) 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
|
||||
|
@ -29,6 +29,7 @@
|
|||
#include <pupa/dl.h>
|
||||
#include <pupa/misc.h>
|
||||
#include <pupa/loader.h>
|
||||
#include <pupa/env.h>
|
||||
|
||||
struct mem_region
|
||||
{
|
||||
|
@ -222,6 +223,7 @@ pupa_machine_init (void)
|
|||
/* The memory system was initialized, thus register built-in devices. */
|
||||
pupa_biosdisk_init ();
|
||||
|
||||
|
||||
/* Initialize the prefix. */
|
||||
pupa_dl_set_prefix (make_install_device ());
|
||||
pupa_env_set ("prefix", make_install_device ());
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/* main.c - the kernel main routine */
|
||||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Free Software Foundation, Inc.
|
||||
* Copyright (C) 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
|
||||
|
@ -27,6 +27,7 @@
|
|||
#include <pupa/rescue.h>
|
||||
#include <pupa/file.h>
|
||||
#include <pupa/device.h>
|
||||
#include <pupa/env.h>
|
||||
|
||||
/* Return the end of the core image. */
|
||||
pupa_addr_t
|
||||
|
@ -65,7 +66,7 @@ pupa_set_root_dev (void)
|
|||
{
|
||||
const char *prefix;
|
||||
|
||||
prefix = pupa_dl_get_prefix ();
|
||||
prefix = pupa_env_get ("prefix");
|
||||
|
||||
if (prefix)
|
||||
{
|
||||
|
|
332
kern/misc.c
332
kern/misc.c
|
@ -23,6 +23,7 @@
|
|||
#include <pupa/mm.h>
|
||||
#include <stdarg.h>
|
||||
#include <pupa/term.h>
|
||||
#include <pupa/env.h>
|
||||
|
||||
void *
|
||||
pupa_memmove (void *dest, const void *src, pupa_size_t n)
|
||||
|
@ -211,6 +212,18 @@ pupa_isalpha (int c)
|
|||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
||||
}
|
||||
|
||||
int
|
||||
pupa_isdigit (int c)
|
||||
{
|
||||
return (c >= '0' && c <= '9');
|
||||
}
|
||||
|
||||
int
|
||||
pupa_isgraph (int c)
|
||||
{
|
||||
return (c >= '!' && c <= '~');
|
||||
}
|
||||
|
||||
int
|
||||
pupa_tolower (int c)
|
||||
{
|
||||
|
@ -380,6 +393,24 @@ pupa_itoa (char *str, int c, unsigned n)
|
|||
return p;
|
||||
}
|
||||
|
||||
static char *
|
||||
pupa_ftoa (char *str, double f, int round)
|
||||
{
|
||||
unsigned int intp;
|
||||
unsigned int fractp;
|
||||
unsigned int power = 1;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < round; i++)
|
||||
power *= 10;
|
||||
|
||||
intp = f;
|
||||
fractp = (f - (float) intp) * power;
|
||||
|
||||
pupa_sprintf (str, "%d.%d", intp, fractp);
|
||||
return str;
|
||||
}
|
||||
|
||||
int
|
||||
pupa_vsprintf (char *str, const char *fmt, va_list args)
|
||||
{
|
||||
|
@ -387,6 +418,7 @@ pupa_vsprintf (char *str, const char *fmt, va_list args)
|
|||
int count = 0;
|
||||
auto void write_char (unsigned char ch);
|
||||
auto void write_str (const char *s);
|
||||
auto void write_fill (const char ch, int n);
|
||||
|
||||
void write_char (unsigned char ch)
|
||||
{
|
||||
|
@ -403,6 +435,13 @@ pupa_vsprintf (char *str, const char *fmt, va_list args)
|
|||
while (*s)
|
||||
write_char (*s++);
|
||||
}
|
||||
|
||||
void write_fill (const char ch, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
write_char (ch);
|
||||
}
|
||||
|
||||
while ((c = *fmt++) != 0)
|
||||
{
|
||||
|
@ -412,10 +451,50 @@ pupa_vsprintf (char *str, const char *fmt, va_list args)
|
|||
{
|
||||
char tmp[16];
|
||||
char *p;
|
||||
unsigned int format1 = 0;
|
||||
unsigned int format2 = 3;
|
||||
char zerofill = ' ';
|
||||
int rightfill = 0;
|
||||
int n;
|
||||
|
||||
c = *fmt++;
|
||||
if (*fmt && *fmt =='-')
|
||||
{
|
||||
rightfill = 1;
|
||||
fmt++;
|
||||
}
|
||||
|
||||
p = (char *) fmt;
|
||||
/* Read formatting parameters. */
|
||||
while (*p && pupa_isdigit (*p))
|
||||
p++;
|
||||
|
||||
if (p > fmt)
|
||||
{
|
||||
char s[p - fmt];
|
||||
pupa_strncpy (s, fmt, p - fmt);
|
||||
if (s[0] == '0')
|
||||
zerofill = '0';
|
||||
format1 = pupa_strtoul (s, 0, 10);
|
||||
fmt = p;
|
||||
if (*p && *p == '.')
|
||||
{
|
||||
p++;
|
||||
fmt++;
|
||||
while (*p && pupa_isdigit (*p))
|
||||
p++;
|
||||
|
||||
if (p > fmt)
|
||||
{
|
||||
char fstr[p - fmt];
|
||||
pupa_strncpy (fstr, fmt, p - fmt);
|
||||
format2 = pupa_strtoul (fstr, 0, 10);
|
||||
fmt = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c = *fmt++;
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case 'p':
|
||||
|
@ -427,14 +506,31 @@ pupa_vsprintf (char *str, const char *fmt, va_list args)
|
|||
case 'd':
|
||||
n = va_arg (args, int);
|
||||
pupa_itoa (tmp, c, n);
|
||||
if (!rightfill && pupa_strlen (tmp) < format1)
|
||||
write_fill (zerofill, format1 - pupa_strlen (tmp));
|
||||
write_str (tmp);
|
||||
if (rightfill && pupa_strlen (tmp) < format1)
|
||||
write_fill (zerofill, format1 - pupa_strlen (tmp));
|
||||
break;
|
||||
|
||||
|
||||
case 'c':
|
||||
n = va_arg (args, int);
|
||||
write_char (n & 0xff);
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
{
|
||||
float f;
|
||||
f = va_arg (args, double);
|
||||
pupa_ftoa (tmp, f, format2);
|
||||
if (!rightfill && pupa_strlen (tmp) < format1)
|
||||
write_fill (zerofill, format1 - pupa_strlen (tmp));
|
||||
write_str (tmp);
|
||||
if (rightfill && pupa_strlen (tmp) < format1)
|
||||
write_fill (zerofill, format1 - pupa_strlen (tmp));
|
||||
break;
|
||||
}
|
||||
|
||||
case 'C':
|
||||
{
|
||||
pupa_uint32_t code = va_arg (args, pupa_uint32_t);
|
||||
|
@ -488,9 +584,18 @@ pupa_vsprintf (char *str, const char *fmt, va_list args)
|
|||
case 's':
|
||||
p = va_arg (args, char *);
|
||||
if (p)
|
||||
write_str (p);
|
||||
{
|
||||
if (!rightfill && pupa_strlen (p) < format1)
|
||||
write_fill (zerofill, format1 - pupa_strlen (p));
|
||||
|
||||
write_str (p);
|
||||
|
||||
if (rightfill && pupa_strlen (p) < format1)
|
||||
write_fill (zerofill, format1 - pupa_strlen (p));
|
||||
}
|
||||
else
|
||||
write_str ("(null)");
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -521,3 +626,224 @@ pupa_sprintf (char *str, const char *fmt, ...)
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
pupa_err_t
|
||||
pupa_split_cmdline (const char *cmdline, pupa_err_t (* getline) (char **), int *argc, char ***argv)
|
||||
{
|
||||
/* XXX: Fixed size buffer, perhaps this buffer should be dynamically
|
||||
allocated. */
|
||||
char buffer[1024];
|
||||
char *bp = buffer;
|
||||
char *rd = (char *) cmdline;
|
||||
char unputbuf;
|
||||
int unput = 0;
|
||||
char *args;
|
||||
int i;
|
||||
|
||||
auto char getchar (void);
|
||||
auto void unputc (char c);
|
||||
auto void getenvvar (void);
|
||||
auto int getarg (void);
|
||||
|
||||
/* Get one character from the commandline. If the caller reads
|
||||
beyond the end of the string a new line will be read. This
|
||||
function will not chech for errors, the caller has to check for
|
||||
pupa_errno. */
|
||||
char getchar (void)
|
||||
{
|
||||
int c;
|
||||
if (unput)
|
||||
{
|
||||
unput = 0;
|
||||
return unputbuf;
|
||||
}
|
||||
|
||||
if (! rd)
|
||||
{
|
||||
getline (&rd);
|
||||
/* Error is ignored here, the caller will check for this
|
||||
when it reads beyond the EOL. */
|
||||
c = *(rd)++;
|
||||
return c;
|
||||
}
|
||||
|
||||
c = *(rd)++;
|
||||
if (! c)
|
||||
{
|
||||
rd = 0;
|
||||
return '\n';
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void unputc (char c)
|
||||
{
|
||||
unputbuf = c;
|
||||
unput = 1;
|
||||
}
|
||||
|
||||
/* Read a variable name from the commandline and insert its content
|
||||
into the buffer. */
|
||||
void getenvvar (void)
|
||||
{
|
||||
char varname[100];
|
||||
char *p = varname;
|
||||
char *val;
|
||||
char c;
|
||||
|
||||
c = getchar ();
|
||||
if (c == '{')
|
||||
while ((c = getchar ()) != '}')
|
||||
*(p++) = c;
|
||||
else
|
||||
{
|
||||
/* XXX: An env. variable can have characters and digits in
|
||||
its name, are more characters allowed here? */
|
||||
while (c && (pupa_isalpha (c) || pupa_isdigit (c)))
|
||||
{
|
||||
*(p++) = c;
|
||||
c = getchar ();
|
||||
}
|
||||
unputc (c);
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
/* The variable does not exist. */
|
||||
val = pupa_env_get (varname);
|
||||
if (! val)
|
||||
return;
|
||||
|
||||
/* Copy the contents of the variable into the buffer. */
|
||||
for (p = val; *p; p++)
|
||||
*(bp++) = *p;
|
||||
}
|
||||
|
||||
/* Read one argument. Return 1 if no variables can be read anymore,
|
||||
otherwise return 0. If there is an error, return 1, the caller
|
||||
has to check pupa_errno. */
|
||||
int getarg (void)
|
||||
{
|
||||
char c;
|
||||
|
||||
/* Skip all whitespaces before an argument. */
|
||||
do {
|
||||
c = getchar ();
|
||||
} while (c == ' ' || c == '\t');
|
||||
|
||||
do {
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
/* Double quote. */
|
||||
while ((c = getchar ()))
|
||||
{
|
||||
if (pupa_errno)
|
||||
return 1;
|
||||
/* Read in an escaped character. */
|
||||
if (c == '\\')
|
||||
{
|
||||
c = getchar ();
|
||||
*(bp++) = c;
|
||||
continue;
|
||||
}
|
||||
else if (c == '"')
|
||||
break;
|
||||
/* Read a variable. */
|
||||
if (c == '$')
|
||||
{
|
||||
getenvvar ();
|
||||
continue;
|
||||
}
|
||||
*(bp++) = c;
|
||||
}
|
||||
break;
|
||||
|
||||
case '\'':
|
||||
/* Single quote. */
|
||||
while ((c = getchar ()) != '\'')
|
||||
{
|
||||
if (pupa_errno)
|
||||
return 1;
|
||||
|
||||
*(bp++) = c;
|
||||
}
|
||||
break;
|
||||
|
||||
case '\n':
|
||||
/* This was not a argument afterall. */
|
||||
return 1;
|
||||
|
||||
default:
|
||||
/* A normal option. */
|
||||
while (c && (pupa_isalpha (c)
|
||||
|| pupa_isdigit (c) || pupa_isgraph (c)))
|
||||
{
|
||||
/* Read in an escaped character. */
|
||||
if (c == '\\')
|
||||
{
|
||||
c = getchar ();
|
||||
*(bp++) = c;
|
||||
c = getchar ();
|
||||
continue;
|
||||
}
|
||||
/* Read a variable. */
|
||||
if (c == '$')
|
||||
{
|
||||
getenvvar ();
|
||||
c = getchar ();
|
||||
continue;
|
||||
}
|
||||
*(bp++) = c;
|
||||
c = getchar ();
|
||||
}
|
||||
unputc (c);
|
||||
|
||||
break;
|
||||
}
|
||||
} while (! pupa_isspace (c) && c != '\'' && c != '"');
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read in all arguments and count them. */
|
||||
*argc = 0;
|
||||
while (1)
|
||||
{
|
||||
if (getarg ())
|
||||
break;
|
||||
*(bp++) = '\0';
|
||||
(*argc)++;
|
||||
}
|
||||
|
||||
/* Check if there were no errors. */
|
||||
if (pupa_errno)
|
||||
return pupa_errno;
|
||||
|
||||
/* Reserve memory for the return values. */
|
||||
args = pupa_malloc (bp - buffer);
|
||||
if (! args)
|
||||
return pupa_errno;
|
||||
pupa_memcpy (args, buffer, bp - buffer);
|
||||
|
||||
*argv = pupa_malloc (sizeof (char *) * (*argc + 1));
|
||||
if (! *argv)
|
||||
{
|
||||
pupa_free (args);
|
||||
return pupa_errno;
|
||||
}
|
||||
|
||||
/* The arguments are separated with 0's, setup argv so it points to
|
||||
the right values. */
|
||||
bp = args;
|
||||
for (i = 0; i < *argc; i++)
|
||||
{
|
||||
(*argv)[i] = bp;
|
||||
while (*bp)
|
||||
bp++;
|
||||
bp++;
|
||||
}
|
||||
|
||||
(*argc)--;
|
||||
return 0;
|
||||
}
|
||||
|
|
148
kern/rescue.c
148
kern/rescue.c
|
@ -1,7 +1,7 @@
|
|||
/* rescue.c - rescue mode */
|
||||
/*
|
||||
* PUPA -- Preliminary Universal Programming Architecture for GRUB
|
||||
* Copyright (C) 2002 Free Software Foundation, Inc.
|
||||
* Copyright (C) 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
|
||||
|
@ -29,6 +29,7 @@
|
|||
#include <pupa/loader.h>
|
||||
#include <pupa/dl.h>
|
||||
#include <pupa/machine/partition.h>
|
||||
#include <pupa/env.h>
|
||||
|
||||
#define PUPA_RESCUE_BUF_SIZE 256
|
||||
#define PUPA_RESCUE_MAX_ARGS 20
|
||||
|
@ -116,29 +117,6 @@ pupa_rescue_get_command_line (const char *prompt)
|
|||
pupa_refresh ();
|
||||
}
|
||||
|
||||
/* Get the next word in STR and return a next pointer. */
|
||||
static char *
|
||||
next_word (char **str)
|
||||
{
|
||||
char *word;
|
||||
char *p = *str;
|
||||
|
||||
/* Skip spaces. */
|
||||
while (*p && pupa_isspace (*p))
|
||||
p++;
|
||||
|
||||
word = p;
|
||||
|
||||
/* Find a space. */
|
||||
while (*p && ! pupa_isspace (*p))
|
||||
p++;
|
||||
|
||||
*p = '\0';
|
||||
*str = p + 1;
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
/* boot */
|
||||
static void
|
||||
pupa_rescue_cmd_boot (int argc __attribute__ ((unused)),
|
||||
|
@ -506,27 +484,6 @@ pupa_rescue_cmd_dump (int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
/* prefix [DIR] */
|
||||
static void
|
||||
pupa_rescue_cmd_prefix (int argc, char *argv[])
|
||||
{
|
||||
static char prefix[100];
|
||||
|
||||
if (argc == 0)
|
||||
pupa_printf ("%s\n", pupa_dl_get_prefix ());
|
||||
else
|
||||
{
|
||||
if (pupa_strlen (argv[0]) >= sizeof (prefix))
|
||||
{
|
||||
pupa_error (PUPA_ERR_BAD_ARGUMENT, "too long prefix");
|
||||
return;
|
||||
}
|
||||
|
||||
pupa_strcpy (prefix, argv[0]);
|
||||
pupa_dl_set_prefix (prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/* insmod MODULE */
|
||||
static void
|
||||
pupa_rescue_cmd_insmod (int argc, char *argv[])
|
||||
|
@ -602,6 +559,52 @@ pupa_rescue_cmd_lsmod (int argc __attribute__ ((unused)),
|
|||
pupa_dl_iterate (print_module);
|
||||
}
|
||||
|
||||
/* set ENVVAR=VALUE */
|
||||
static void
|
||||
pupa_rescue_cmd_set (int argc, char *argv[])
|
||||
{
|
||||
char *var;
|
||||
char *val;
|
||||
|
||||
auto int print_env (struct pupa_env_var *env);
|
||||
|
||||
int print_env (struct pupa_env_var *env)
|
||||
{
|
||||
pupa_printf ("%s=%s\n", env->name, env->value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (argc < 1)
|
||||
{
|
||||
pupa_env_iterate (print_env);
|
||||
return;
|
||||
}
|
||||
|
||||
var = argv[0];
|
||||
val = pupa_strchr (var, '=');
|
||||
if (! val)
|
||||
{
|
||||
pupa_error (PUPA_ERR_BAD_ARGUMENT, "not an assignment");
|
||||
return;
|
||||
}
|
||||
|
||||
val[0] = 0;
|
||||
pupa_env_set (var, val + 1);
|
||||
val[0] = '=';
|
||||
}
|
||||
|
||||
static void
|
||||
pupa_rescue_cmd_unset (int argc, char *argv[])
|
||||
{
|
||||
if (argc < 1)
|
||||
{
|
||||
pupa_error (PUPA_ERR_BAD_ARGUMENT, "no environment variable specified");
|
||||
return;
|
||||
}
|
||||
|
||||
pupa_env_unset (argv[0]);
|
||||
}
|
||||
|
||||
static void
|
||||
attempt_normal_mode (void)
|
||||
{
|
||||
|
@ -621,6 +624,15 @@ attempt_normal_mode (void)
|
|||
void
|
||||
pupa_enter_rescue_mode (void)
|
||||
{
|
||||
auto pupa_err_t getline (char **line);
|
||||
|
||||
pupa_err_t getline (char **line)
|
||||
{
|
||||
pupa_rescue_get_command_line ("> ");
|
||||
*line = linebuf;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* First of all, attempt to execute the normal mode. */
|
||||
attempt_normal_mode ();
|
||||
|
||||
|
@ -638,14 +650,16 @@ pupa_enter_rescue_mode (void)
|
|||
"set the root device");
|
||||
pupa_rescue_register_command ("dump", pupa_rescue_cmd_dump,
|
||||
"dump memory");
|
||||
pupa_rescue_register_command ("prefix", pupa_rescue_cmd_prefix,
|
||||
"set the prefix");
|
||||
pupa_rescue_register_command ("insmod", pupa_rescue_cmd_insmod,
|
||||
"insert a module");
|
||||
pupa_rescue_register_command ("rmmod", pupa_rescue_cmd_rmmod,
|
||||
"remove a module");
|
||||
pupa_rescue_register_command ("lsmod", pupa_rescue_cmd_lsmod,
|
||||
"show loaded modules");
|
||||
pupa_rescue_register_command ("set", pupa_rescue_cmd_set,
|
||||
"set an environment variable");
|
||||
pupa_rescue_register_command ("unset", pupa_rescue_cmd_unset,
|
||||
"remove an environment variable");
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
@ -653,36 +667,42 @@ pupa_enter_rescue_mode (void)
|
|||
char *name;
|
||||
int n;
|
||||
pupa_rescue_command_t cmd;
|
||||
char *args[PUPA_RESCUE_MAX_ARGS + 1];
|
||||
char **args;
|
||||
|
||||
/* Print an error, if any. */
|
||||
pupa_print_error ();
|
||||
pupa_errno = PUPA_ERR_NONE;
|
||||
|
||||
/* Get a command line. */
|
||||
pupa_rescue_get_command_line ("pupa rescue> ");
|
||||
|
||||
if (pupa_split_cmdline (line, getline, &n, &args))
|
||||
continue;
|
||||
|
||||
/* In case of an assignment set the environment accordingly
|
||||
instead of calling a function. */
|
||||
if (n == 0 && pupa_strchr (line, '='))
|
||||
{
|
||||
char *val = pupa_strchr (args[0], '=');
|
||||
val[0] = 0;
|
||||
pupa_env_set (args[0], val + 1);
|
||||
val[0] = '=';
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Get the command name. */
|
||||
name = next_word (&line);
|
||||
name = args[0];
|
||||
|
||||
/* If nothing is specified, restart. */
|
||||
if (*name == '\0')
|
||||
continue;
|
||||
|
||||
/* Get arguments. */
|
||||
for (n = 0; n <= PUPA_RESCUE_MAX_ARGS; n++)
|
||||
{
|
||||
char *arg = next_word (&line);
|
||||
|
||||
if (*arg)
|
||||
args[n] = arg;
|
||||
else
|
||||
break;
|
||||
}
|
||||
args[n] = 0;
|
||||
|
||||
/* Find the command and execute it. */
|
||||
for (cmd = pupa_rescue_command_list; cmd; cmd = cmd->next)
|
||||
{
|
||||
if (pupa_strcmp (name, cmd->name) == 0)
|
||||
{
|
||||
(cmd->func) (n, args);
|
||||
(cmd->func) (n, &args[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -693,9 +713,5 @@ pupa_enter_rescue_mode (void)
|
|||
pupa_printf ("Unknown command `%s'\n", name);
|
||||
pupa_printf ("Try `help' for usage\n");
|
||||
}
|
||||
|
||||
/* Print an error, if any. */
|
||||
pupa_print_error ();
|
||||
pupa_errno = PUPA_ERR_NONE;
|
||||
}
|
||||
}
|
||||
|
|
49
kern/term.c
49
kern/term.c
|
@ -20,6 +20,7 @@
|
|||
#include <pupa/term.h>
|
||||
#include <pupa/err.h>
|
||||
#include <pupa/mm.h>
|
||||
#include <pupa/misc.h>
|
||||
|
||||
/* The list of terminals. */
|
||||
static pupa_term_t pupa_term_list;
|
||||
|
@ -27,6 +28,12 @@ static pupa_term_t pupa_term_list;
|
|||
/* The current terminal. */
|
||||
static pupa_term_t pupa_cur_term;
|
||||
|
||||
/* The amount of lines counted by the pager. */
|
||||
static int pupa_more_lines;
|
||||
|
||||
/* If the more pager is active. */
|
||||
static int pupa_more;
|
||||
|
||||
void
|
||||
pupa_term_register (pupa_term_t term)
|
||||
{
|
||||
|
@ -97,7 +104,36 @@ pupa_putcode (pupa_uint32_t code)
|
|||
(pupa_cur_term->putchar) (code);
|
||||
|
||||
if (code == '\n')
|
||||
pupa_putcode ('\r');
|
||||
{
|
||||
pupa_putcode ('\r');
|
||||
|
||||
pupa_more_lines++;
|
||||
/* XXX: Don't use a fixed height! */
|
||||
if (pupa_more && pupa_more_lines == 24 - 1)
|
||||
{
|
||||
char key;
|
||||
int pos = pupa_getxy ();
|
||||
|
||||
/* Show --MORE-- on the lower left side of the screen. */
|
||||
pupa_gotoxy (1, 24 - 1);
|
||||
pupa_setcolorstate (PUPA_TERM_COLOR_HIGHLIGHT);
|
||||
pupa_printf ("--MORE--");
|
||||
pupa_setcolorstate (PUPA_TERM_COLOR_STANDARD);
|
||||
|
||||
key = pupa_getkey ();
|
||||
|
||||
/* Remove the message. */
|
||||
pupa_gotoxy (1, 24 -1);
|
||||
pupa_printf (" ");
|
||||
pupa_gotoxy (pos >> 8, pos & 0xFF);
|
||||
|
||||
/* Scroll one lines or an entire page, depending on the key. */
|
||||
if (key == '\r' || key =='\n')
|
||||
pupa_more_lines--;
|
||||
else
|
||||
pupa_more_lines = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Put a character. C is one byte of a UTF-8 stream.
|
||||
|
@ -235,3 +271,14 @@ pupa_refresh (void)
|
|||
if (pupa_cur_term->refresh)
|
||||
(pupa_cur_term->refresh) ();
|
||||
}
|
||||
|
||||
void
|
||||
pupa_set_more (int onoff)
|
||||
{
|
||||
if (onoff == 1)
|
||||
pupa_more++;
|
||||
else
|
||||
pupa_more--;
|
||||
|
||||
pupa_more_lines = 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue