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
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue