Hello,
I have applied some cosmetic changes to the string functions
grub_strXXX() in the file stage2/char_io.c, with some cleaner and
straight-forward code. Attached is the patch.
There is a declaration of "init_cmdline()" in shared.h, but I couldn't
find the definition of the function "init_cmdline" anywhere. What does it
do ?
Thanks,
--------------------
Ashutosh S. Rajekar
*** char_io.c Wed Jul 14 02:09:46 1999
--- /root/char_io.c~ Thu Aug 12 19:49:51 1999
***************
*** 4,9 ****
--- 4,12 ----
* Copyright (C) 1996 Erich Boleyn <[EMAIL PROTECTED]>
* Copyright (C) 1999 Free Software Foundation, Inc.
*
+ * Cosmetic changes to the grub_strXXX() functions,
+ * by Ashutosh S. Rajekar <[EMAIL PROTECTED]>
+ *
* 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
***************
*** 23,29 ****
#include "shared.h"
! void
print_error (void)
{
if (errnum > ERR_NONE && errnum < MAX_ERR_NUM)
--- 26,32 ----
#include "shared.h"
! void
print_error (void)
{
if (errnum > ERR_NONE && errnum < MAX_ERR_NUM)
***************
*** 81,87 ****
}
! void
grub_printf (const char *format,...)
{
int *dataptr = (int *) &format;
--- 84,90 ----
}
! void
grub_printf (const char *format,...)
{
int *dataptr = (int *) &format;
***************
*** 128,134 ****
#ifndef STAGE1_5
! void
init_page (void)
{
cls ();
--- 131,137 ----
#ifndef STAGE1_5
! void
init_page (void)
{
cls ();
***************
*** 146,152 ****
or zero-length).
If ECHO_CHAR is nonzero, echo it instead of the typed character. */
! int
get_cmdline (char *prompt, char *commands, char *cmdline, int maxlen,
int echo_char)
{
--- 149,155 ----
or zero-length).
If ECHO_CHAR is nonzero, echo it instead of the typed character. */
! int
get_cmdline (char *prompt, char *commands, char *cmdline, int maxlen,
int echo_char)
{
***************
*** 160,166 ****
while (*str != 0)
{
putchar (echo_char ? echo_char : *str);
! str ++;
if (++xend > 78)
{
xend = 0;
--- 163,169 ----
while (*str != 0)
{
putchar (echo_char ? echo_char : *str);
! str++;
if (++xend > 78)
{
xend = 0;
***************
*** 172,177 ****
--- 175,181 ----
}
}
}
+
/* nested function definition for code simplicity */
static void cl_setcpos (void)
{
***************
*** 199,205 ****
static void cl_kill_to_end ()
{
int i;
! cmdline[lpos] = 0;
for (i = lpos; i <= llen; i++)
{
if (i && ((i + plen) % 79) == 0)
--- 203,209 ----
static void cl_kill_to_end ()
{
int i;
! cmdline[lpos] = 0;
for (i = lpos; i <= llen; i++)
{
if (i && ((i + plen) % 79) == 0)
***************
*** 309,315 ****
case 6: /* C-f forward one character */
if (lpos < llen)
{
! lpos ++;
cl_setcpos ();
}
break;
--- 313,319 ----
case 6: /* C-f forward one character */
if (lpos < llen)
{
! lpos++;
cl_setcpos ();
}
break;
***************
*** 323,333 ****
case 4: /* C-d delete character under cursor */
if (lpos == llen)
break;
! lpos ++;
/* fallthrough is on purpose! */
case 8: /* C-h backspace */
#ifdef GRUB_UTIL
! case 127: /* also backspace */
#endif
if (lpos > 0)
{
--- 327,337 ----
case 4: /* C-d delete character under cursor */
if (lpos == llen)
break;
! lpos++;
/* fallthrough is on purpose! */
case 8: /* C-h backspace */
#ifdef GRUB_UTIL
! case 127: /* also backspace */
#endif
if (lpos > 0)
{
***************
*** 380,386 ****
cmdline[lpos] = c;
cmdline[lpos + 1] = 0;
cl_print (cmdline + lpos, echo_char);
! lpos ++;
cl_setcpos ();
}
else
--- 384,390 ----
cmdline[lpos] = c;
cmdline[lpos + 1] = 0;
cl_print (cmdline + lpos, echo_char);
! lpos++;
cl_setcpos ();
}
else
***************
*** 422,435 ****
/* Zero-terminate the string. */
cmdline[c] = 0;
}
-
return 0;
}
#endif /* STAGE1_5 */
! int
safe_parse_maxint (char **str_ptr, int *myint_ptr)
{
char *ptr = *str_ptr;
--- 426,438 ----
/* Zero-terminate the string. */
cmdline[c] = 0;
}
return 0;
}
#endif /* STAGE1_5 */
! int
safe_parse_maxint (char **str_ptr, int *myint_ptr)
{
char *ptr = *str_ptr;
***************
*** 444,457 ****
ptr += 2;
mult = 16;
}
-
while (1)
{
/* A bit tricky. This below makes use of the equivalence:
! (A >= B && A <= C) <=> ((A - B) <= (C - B))
! when C > B and A is unsigned. */
unsigned int digit;
!
digit = tolower (*ptr) - '0';
if (digit > 9)
{
--- 447,459 ----
ptr += 2;
mult = 16;
}
while (1)
{
/* A bit tricky. This below makes use of the equivalence:
! (A >= B && A <= C) <=> ((A - B) <= (C - B))
! when C > B and A is unsigned. */
unsigned int digit;
!
digit = tolower (*ptr) - '0';
if (digit > 9)
{
***************
*** 460,466 ****
break;
digit += 10;
}
-
found = 1;
if (myint > ((MAXINT - digit) / mult))
{
--- 462,467 ----
***************
*** 476,482 ****
errnum = ERR_NUMBER_PARSING;
return 0;
}
-
*str_ptr = ptr;
*myint_ptr = myint;
--- 477,482 ----
***************
*** 484,490 ****
}
! int
grub_tolower (int c)
{
if (c >= 'A' && c <= 'Z')
--- 484,490 ----
}
! int
grub_tolower (int c)
{
if (c >= 'A' && c <= 'Z')
***************
*** 494,503 ****
}
! int
grub_isspace (int c)
{
! if (c == ' ' || c == '\t' || c == '\n')
return 1;
return 0;
--- 494,503 ----
}
! int
grub_isspace (int c)
{
! if (c == ' ' || c == '\t' || c == '\v' || c == '\f' || c == '\n')
return 1;
return 0;
***************
*** 505,556 ****
#ifndef STAGE1_5
! int
grub_strncat (char *s1, const char *s2, int n)
{
! int i = -1;
!
! while (++i < n && s1[i] != 0);
! while (i < n && (s1[i++] = *(s2++)) != 0);
!
! s1[n - 1] = 0;
!
! if (i >= n)
! return 0;
!
! s1[i] = 0;
! return 1;
}
!
! int
grub_strcmp (const char *s1, const char *s2)
{
! while (*s1 || *s2)
! {
! if (*s1 < *s2)
! return -1;
! else if (*s1 > *s2)
! return 1;
! s1 ++;
! s2 ++;
! }
! return 0;
}
#endif /* ! STAGE1_5 */
! int
substring (char *s1, char *s2)
{
while (*s1 == *s2)
{
/* The strings match exactly. */
! if (! *(s1++))
return 0;
! s2 ++;
}
/* S1 is a substring of S2. */
--- 505,554 ----
#ifndef STAGE1_5
! int
grub_strncat (char *s1, const char *s2, int n)
{
! char *tmp = s2;
! if (n)
! {
! while (*s1)
! s1++;
! while ((*s1++ = *s2++))
! {
! if (!(--n))
! {
! *s1 = '\0';
! break;
! }
! }
! }
! return tmp;
}
! int
grub_strcmp (const char *s1, const char *s2)
{
! for (; *s1++ == *s2++;)
! ;
! if ((!*s2) && (!*s1))
! return 0;
!
! return (*s1 - *s2);
}
#endif /* ! STAGE1_5 */
! int
substring (char *s1, char *s2)
{
while (*s1 == *s2)
{
/* The strings match exactly. */
! if (!*(s1++))
return 0;
! s2++;
}
/* S1 is a substring of S2. */
***************
*** 566,601 ****
char *
grub_strstr (const char *s1, const char *s2)
{
! const char *ptr, *tmp;
! while (*s1)
{
! ptr = s1;
! tmp = s2;
! while (*s1 && *s1++ == *tmp++);
!
! if (tmp > s2 && !*(tmp - 1))
! return (char *) ptr;
}
! return 0;
}
! int
grub_strlen (const char *str)
{
int len = 0;
!
while (*str++)
len++;
!
return len;
}
#endif /* ! STAGE1_5 */
! int
memcheck (int start, int len)
{
#ifdef GRUB_UTIL
--- 564,597 ----
char *
grub_strstr (const char *s1, const char *s2)
{
! int i, j, k;
! for (i = 0; s1[i]; i++)
{
! for (j = i, k = 0; s2[k] && s1[j] == s2[k]; j++, k++)
! ;
! if (k > 0 && !s2[k])
! return (char *) (s1 + i);
}
! return (char *) NULL;
}
! int
grub_strlen (const char *str)
{
int len = 0;
!
while (*str++)
len++;
!
return len;
}
#endif /* ! STAGE1_5 */
! int
memcheck (int start, int len)
{
#ifdef GRUB_UTIL
***************
*** 605,611 ****
grub_scratch_mem). */
extern char cur_part_desc[];
if (start >= (int) cur_part_desc && start + len <= (int) cur_part_desc + 16)
! return ! errnum;
#endif /* GRUB_UTIL */
if ((start < RAW_ADDR (0x1000)) ||
--- 601,607 ----
grub_scratch_mem). */
extern char cur_part_desc[];
if (start >= (int) cur_part_desc && start + len <= (int) cur_part_desc + 16)
! return !errnum;
#endif /* GRUB_UTIL */
if ((start < RAW_ADDR (0x1000)) ||
***************
*** 615,658 ****
RAW_ADDR (mbi.mem_upper * 1024) < ((start - 0x100000) + len)))
errnum = ERR_WONT_FIT;
! return ! errnum;
}
char *
grub_memmove (char *to, const char *from, int len)
{
! if (memcheck ((int) to, len))
! {
! /* This assembly code is stolen from
! linux-2.2.2/include/asm-i386/string.h. This is not very fast
! but compact. */
! int d0, d1, d2;
!
! if (to < from)
! {
! asm volatile ("cld\n\t"
! "rep\n\t"
! "movsb"
! : "=&c" (d0), "=&S" (d1), "=&D" (d2)
! : "0" (len),"1" (from),"2" (to)
! : "memory");
! }
! else
! {
! asm volatile ("std\n\t"
! "rep\n\t"
! "movsb\n\t"
! "cld"
! : "=&c" (d0), "=&S" (d1), "=&D" (d2)
! : "0" (len),
! "1" (len - 1 + from),
! "2" (len - 1 + to)
! : "memory");
! }
! }
! return errnum ? NULL : to;
}
--- 611,653 ----
RAW_ADDR (mbi.mem_upper * 1024) < ((start - 0x100000) + len)))
errnum = ERR_WONT_FIT;
! return !errnum;
}
char *
grub_memmove (char *to, const char *from, int len)
{
! if (memcheck ((int) to, len))
! {
! /* This assembly code is stolen from
! linux-2.2.2/include/asm-i386/string.h. This is not very fast
! but compact. */
! int d0, d1, d2;
! if (to < from)
! {
! asm volatile ("cld\n\t"
! "rep\n\t"
! "movsb"
! :"=&c" (d0), "=&S" (d1), "=&D" (d2)
! :"0" (len), "1" (from), "2" (to)
! :"memory");
! }
! else
! {
! asm volatile ("std\n\t"
! "rep\n\t"
! "movsb\n\t"
! "cld"
! :"=&c" (d0), "=&S" (d1), "=&D" (d2)
! :"0" (len),
! "1" (len - 1 + from),
! "2" (len - 1 + to)
! :"memory");
! }
! }
! return errnum ? NULL : to;
}
***************
*** 664,673 ****
if (memcheck ((int) start, len))
{
! while (len -- > 0)
! *p ++ = c;
}
-
return errnum ? NULL : start;
}
#endif /* ! STAGE1_5 */
--- 659,667 ----
if (memcheck ((int) start, len))
{
! while (len-- > 0)
! *p++ = c;
}
return errnum ? NULL : start;
}
#endif /* ! STAGE1_5 */