grub/kern/misc.c
okuji a5ffe96617 2003-01-06 Yoshinori K. Okuji <okuji@enbug.org>
* util/i386/pc/pupa-setup.c: Include pupa/machine/kernel.h.
	(setup): Configure the installed partition information and the
	dl prefix.

	* loader/i386/pc/chainloader.c (my_mod): New variable.
	(pupa_chainloader_unload): New function.
	(pupa_rescue_cmd_chainloader): Refer itself.
	(PUPA_MOD_INIT): Save its own module in MY_MOD.

	* kern/i386/pc/startup.S (install_partition): Removed.
	(version_string): Likewise.
	(config_file): Likewise.
	(pupa_install_dos_part): New variable.
	(pupa_install_bsd_part): Likewise.
	(pupa_prefix): Likewise.
	(pupa_chainloader_real_boot): Call pupa_dl_unload_all.

	* kern/i386/pc/init.c: Include pupa/machine/kernel.h, pupa/dl.h
	and pupa/misc.h.
	(make_install_device): New function.
	(pupa_machine_init): Set the dl prefix.

	* kern/rescue.c: Include pupa/rescue.h and pupa/dl.h.
	(buf): Renamed to ...
	(linebuf): ... this.
	(pupa_rescue_cmd_prefix): New function.
	(pupa_rescue_cmd_insmod): Likewise.
	(pupa_rescue_cmd_rmmod): Likewise.
	(pupa_rescue_cmd_lsmod): Likewise.
	(pupa_enter_rescue_mode): Register new commands: prefix, insmod,
	rmmod and lsmod.

	* kern/mm.c (pupa_memalign): If failed even after invalidating
	disk caches, unload unneeded modules and retry.

	* kern/misc.c (pupa_memmove): New function.
	(pupa_memcpy): Removed.
	(pupa_strcpy): New function.
	(pupa_itoa): Made static.

	* kern/dl.c (pupa_dl_iterate): New function.
	(pupa_dl_ref): Likewise.
	(pupa_dl_unref): Likewise.
	(pupa_dl_unload): Return if succeeded or not.
	(pupa_dl_unload_unneeded): New function.
	(pupa_dl_unload_all): Likewise.
	(pupa_dl_init): Renamed to ...
	(pupa_dl_set_prefix): ... this.
	(pupa_dl_get_prefix): New function.

	* include/pupa/i386/pc/kernel.h: Include pupa/types.h.
	(PUPA_KERNEL_MACHINE_INSTALL_DOS_PART): New macro.
	(PUPA_KERNEL_MACHINE_INSTALL_BSD_PART): Likewise.
	(PUPA_KERNEL_MACHINE_PREFIX): Likewise.
	(pupa_install_dos_part): Declared.
	(pupa_install_bsd_part): Likewise.
	(pupa_prefix): Likewise.
	(pupa_boot_drive): Likewise.

	* include/pupa/types.h: Fix a typo.

	* include/pupa/misc.h (pupa_memcpy): New macro. Just an alias to
	pupa_memmove.
	(pupa_memmove): Declared.
	(pupa_strcpy): Likewise.

	* include/pupa/dl.h (PUPA_MOD_INIT): Change the prototype. Now
	pupa_mod_init takes one argument, its own module.
	(pupa_dl_unload_unneeded): Declared.
	(pupa_dl_unload_all): Likewise.
	(pupa_dl_ref): Likewise.
	(pupa_dl_unref): Likewise.
	(pupa_dl_iterate): Likewise.
	(pupa_dl_init): Renamed to ...
	(pupa_dl_set_prefix): ... this.
	(pupa_dl_get_prefix): Declared.

	* fs/fat.c [!PUPA_UTIL] (my_mod): New variable.
	(pupa_fat_dir) [!PUPA_UTIL]: Prevent the fat module from being
	unloaded.
	(pupa_fat_open) [!PUPA_UTIL]: Refer itself if succeeded.
	(pupa_fat_close) [!PUPA_UTIL]: Unrefer itself.

	* configure.ac (tmp_CFLAGS): Added -Wshadow, -Wpointer-arith,
	-Wmissing-prototypes, -Wundef and -Wstrict-prototypes.
2003-01-06 00:01:35 +00:00

413 lines
6.2 KiB
C

/* misc.c - definitions of misc functions */
/*
* PUPA -- Preliminary Universal Programming Architecture for GRUB
* Copyright (C) 1999,2000,2001,2002 Free Software Foundation, Inc.
* Copyright (C) 2002 Yoshinori K. Okuji <okuji@enbug.org>
*
* 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/misc.h>
#include <pupa/err.h>
#include <pupa/mm.h>
#include <stdarg.h>
#include <pupa/term.h>
void *
pupa_memmove (void *dest, const void *src, pupa_size_t n)
{
char *d = (char *) dest;
const char *s = (const char *) src;
if (d < s)
while (n--)
*d++ = *s++;
else
{
d += n;
s += n;
while (n--)
*--d = *--s;
}
return dest;
}
char *
pupa_strcpy (char *dest, const char *src)
{
char *p = dest;
while ((*p++ = *src++) != '\0')
;
return dest;
}
#if 0
char *
pupa_strcat (char *dest, const char *src)
{
char *p = dest;
while (*p)
p++;
while ((*p++ = *src++) != '\0')
;
return dest;
}
#endif
int
pupa_printf (const char *fmt, ...)
{
va_list ap;
int ret;
va_start (ap, fmt);
ret = pupa_vprintf (fmt, ap);
va_end (ap);
return ret;
}
int
pupa_vprintf (const char *fmt, va_list args)
{
return pupa_vsprintf (0, fmt, args);
}
int
pupa_memcmp (const void *s1, const void *s2, pupa_size_t n)
{
const char *t1 = s1;
const char *t2 = s2;
while (n--)
{
if (*t1 != *t2)
return (int) *t1 - (int) *t2;
t1++;
t2++;
}
return 0;
}
int
pupa_strcmp (const char *s1, const char *s2)
{
while (*s1 && *s2)
{
if (*s1 != *s2)
return (int) *s1 - (int) *s2;
s1++;
s2++;
}
return (int) *s1 - (int) *s2;
}
char *
pupa_strchr (const char *s, int c)
{
while (*s)
{
if (*s == c)
return (char *) s;
s++;
}
return 0;
}
char *
pupa_strrchr (const char *s, int c)
{
char *p = 0;
while (*s)
{
if (*s == c)
p = (char *) s;
s++;
}
return p;
}
int
pupa_isspace (int c)
{
return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
}
int
pupa_isprint (int c)
{
return (c >= ' ' && c <= '~');
}
int
pupa_isalpha (int c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
int
pupa_tolower (int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
return c;
}
unsigned long
pupa_strtoul (const char *str, char **end, int base)
{
unsigned long num = 0;
int found = 0;
/* Skip white spaces. */
while (*str && pupa_isspace (*str))
str++;
/* Guess the base, if not specified. The prefix `0x' means 16, and
the prefix `0' means 8. */
if (str[0] == '0')
{
if (str[1] == 'x')
{
if (base == 0 || base == 16)
{
base = 16;
str += 2;
}
}
else if (str[1] >= '0' && str[1] <= '7')
base = 8;
}
if (base == 0)
base = 10;
while (*str)
{
unsigned long digit;
digit = pupa_tolower (*str) - '0';
if (digit > 9)
{
digit += '0' - 'a' + 10;
if (digit >= (unsigned long) base)
break;
}
found = 1;
if (num > (~0UL - digit) / base)
{
pupa_error (PUPA_ERR_OUT_OF_RANGE, "overflow is detected");
return 0;
}
num += num * base + digit;
str++;
}
if (! found)
{
pupa_error (PUPA_ERR_BAD_NUMBER, "unrecognized number");
return 0;
}
if (end)
*end = (char *) str;
return num;
}
char *
pupa_strdup (const char *s)
{
pupa_size_t len;
char *p;
len = pupa_strlen (s) + 1;
p = (char *) pupa_malloc (len);
if (! p)
return 0;
return pupa_memcpy (p, s, len);
}
void *
pupa_memset (void *s, int c, pupa_size_t n)
{
unsigned char *p = (unsigned char *) s;
while (n--)
*p++ = (unsigned char) c;
return s;
}
pupa_size_t
pupa_strlen (const char *s)
{
const char *p = s;
while (*p)
p++;
return p - s;
}
static inline void
pupa_reverse (char *str)
{
char *p = str + pupa_strlen (str) - 1;
while (str < p)
{
char tmp;
tmp = *str;
*str = *p;
*p = tmp;
str++;
p--;
}
}
static char *
pupa_itoa (char *str, int c, unsigned n)
{
unsigned base = (c == 'x') ? 16 : 10;
char *p;
if ((int) n < 0 && c == 'd')
{
n = (unsigned) (-((int) n));
*str++ = '-';
}
p = str;
do
{
unsigned d = n % base;
*p++ = (d > 9) ? d + 'a' - 10 : d + '0';
}
while (n /= base);
*p = 0;
pupa_reverse (str);
return p;
}
int
pupa_vsprintf (char *str, const char *fmt, va_list args)
{
char c;
int count = 0;
auto void write_char (char ch);
auto void write_str (const char *s);
void write_char (char ch)
{
if (str)
*str++ = ch;
else
pupa_putchar (ch);
count++;
}
void write_str (const char *s)
{
while (*s)
write_char (*s++);
}
while ((c = *fmt++) != 0)
{
if (c != '%')
write_char (c);
else
{
char tmp[16];
char *p;
int n;
c = *fmt++;
switch (c)
{
case 'p':
write_str ("0x");
c = 'x';
/* fall through */
case 'x':
case 'u':
case 'd':
n = va_arg (args, int);
pupa_itoa (tmp, c, n);
write_str (tmp);
break;
case 'c':
n = va_arg (args, int);
write_char (n);
break;
case 's':
p = va_arg (args, char *);
if (p)
write_str (p);
else
write_str ("(null)");
break;
default:
write_char (c);
break;
}
}
}
if (str)
*str = '\0';
return count;
}
int
pupa_sprintf (char *str, const char *fmt, ...)
{
va_list ap;
int ret;
va_start (ap, fmt);
ret = pupa_vsprintf (str, fmt, ap);
va_end (ap);
return ret;
}