* grub-core/normal/cmdline.c (grub_cmdline_get):

Remove nested functions.
This commit is contained in:
Vladimir Serbinenko 2013-11-07 02:42:38 +01:00
parent 87a04a2df6
commit a67c755ef1
2 changed files with 157 additions and 143 deletions

View file

@ -1,3 +1,8 @@
2013-11-07 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/normal/cmdline.c (grub_cmdline_get):
Remove nested functions.
2013-11-07 Vladimir Serbinenko <phcoder@gmail.com> 2013-11-07 Vladimir Serbinenko <phcoder@gmail.com>
* tests/test_sha512sum.in: Make it work on emu. * tests/test_sha512sum.in: Make it work on emu.

View file

@ -212,50 +212,31 @@ struct cmdline_term
struct grub_term_output *term; struct grub_term_output *term;
}; };
/* Get a command-line. If ESC is pushed, return zero, static inline void
otherwise return command line. */ cl_set_pos (struct cmdline_term *cl_term, grub_size_t lpos)
/* FIXME: The dumb interface is not supported yet. */
char *
grub_cmdline_get (const char *prompt_translated)
{ {
grub_size_t lpos, llen;
grub_uint32_t *buf;
grub_size_t max_len = 256;
int key;
int histpos = 0;
auto void cl_insert (const grub_uint32_t *str);
auto void cl_delete (unsigned len);
auto inline void __attribute__ ((always_inline)) cl_print (struct cmdline_term *cl_term, int pos,
grub_uint32_t c);
auto void cl_set_pos (struct cmdline_term *cl_term);
auto void cl_print_all (int pos, grub_uint32_t c);
auto void cl_set_pos_all (void);
auto void init_clterm (struct cmdline_term *cl_term_cur);
auto void init_clterm_all (void);
struct cmdline_term *cl_terms;
char *ret;
unsigned nterms;
void cl_set_pos (struct cmdline_term *cl_term)
{
cl_term->pos.x = (cl_term->prompt_len + lpos) % cl_term->width; cl_term->pos.x = (cl_term->prompt_len + lpos) % cl_term->width;
cl_term->pos.y = cl_term->ystart cl_term->pos.y = cl_term->ystart
+ (cl_term->prompt_len + lpos) / cl_term->width; + (cl_term->prompt_len + lpos) / cl_term->width;
grub_term_gotoxy (cl_term->term, cl_term->pos); grub_term_gotoxy (cl_term->term, cl_term->pos);
} }
void cl_set_pos_all (void) static void
{ cl_set_pos_all (struct cmdline_term *cl_terms, unsigned nterms,
grub_size_t lpos)
{
unsigned i; unsigned i;
for (i = 0; i < nterms; i++) for (i = 0; i < nterms; i++)
cl_set_pos (&cl_terms[i]); cl_set_pos (&cl_terms[i], lpos);
} }
inline void __attribute__ ((always_inline)) cl_print (struct cmdline_term *cl_term, int pos, grub_uint32_t c) static inline void __attribute__ ((always_inline))
{ cl_print (struct cmdline_term *cl_term, grub_uint32_t c,
grub_uint32_t *start, grub_uint32_t *end)
{
grub_uint32_t *p; grub_uint32_t *p;
for (p = buf + pos; p < buf + llen; p++) for (p = start; p < end; p++)
{ {
if (c) if (c)
grub_putcode (c, cl_term->term); grub_putcode (c, cl_term->term);
@ -272,83 +253,101 @@ grub_cmdline_get (const char *prompt_translated)
grub_putcode ('\n', cl_term->term); grub_putcode ('\n', cl_term->term);
} }
} }
} }
void cl_print_all (int pos, grub_uint32_t c) static void
{ cl_print_all (struct cmdline_term *cl_terms, unsigned nterms,
grub_uint32_t c, grub_uint32_t *start, grub_uint32_t *end)
{
unsigned i; unsigned i;
for (i = 0; i < nterms; i++) for (i = 0; i < nterms; i++)
cl_print (&cl_terms[i], pos, c); cl_print (&cl_terms[i], c, start, end);
} }
void cl_insert (const grub_uint32_t *str) static void
{ init_clterm (struct cmdline_term *cl_term_cur)
grub_size_t len = strlen_ucs4 (str); {
if (len + llen >= max_len)
{
grub_uint32_t *nbuf;
max_len *= 2;
nbuf = grub_realloc (buf, sizeof (grub_uint32_t) * max_len);
if (nbuf)
buf = nbuf;
else
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
max_len /= 2;
}
}
if (len + llen < max_len)
{
grub_memmove (buf + lpos + len, buf + lpos,
(llen - lpos + 1) * sizeof (grub_uint32_t));
grub_memmove (buf + lpos, str, len * sizeof (grub_uint32_t));
llen += len;
cl_set_pos_all ();
cl_print_all (lpos, 0);
lpos += len;
cl_set_pos_all ();
}
}
void cl_delete (unsigned len)
{
if (lpos + len <= llen)
{
grub_size_t saved_lpos = lpos;
lpos = llen - len;
cl_set_pos_all ();
cl_print_all (lpos, ' ');
lpos = saved_lpos;
cl_set_pos_all ();
grub_memmove (buf + lpos, buf + lpos + len,
sizeof (grub_uint32_t) * (llen - lpos + 1));
llen -= len;
cl_print_all (lpos, 0);
cl_set_pos_all ();
}
}
void init_clterm (struct cmdline_term *cl_term_cur)
{
cl_term_cur->pos.x = cl_term_cur->prompt_len; cl_term_cur->pos.x = cl_term_cur->prompt_len;
cl_term_cur->pos.y = grub_term_getxy (cl_term_cur->term).y; cl_term_cur->pos.y = grub_term_getxy (cl_term_cur->term).y;
cl_term_cur->ystart = cl_term_cur->pos.y; cl_term_cur->ystart = cl_term_cur->pos.y;
cl_term_cur->width = grub_term_width (cl_term_cur->term); cl_term_cur->width = grub_term_width (cl_term_cur->term);
cl_term_cur->height = grub_term_height (cl_term_cur->term); cl_term_cur->height = grub_term_height (cl_term_cur->term);
}
static void
cl_delete (struct cmdline_term *cl_terms, unsigned nterms,
grub_uint32_t *buf,
grub_size_t lpos, grub_size_t *llen, unsigned len)
{
if (lpos + len <= (*llen))
{
cl_set_pos_all (cl_terms, nterms, (*llen) - len);
cl_print_all (cl_terms, nterms, ' ', buf + (*llen) - len, buf + (*llen));
cl_set_pos_all (cl_terms, nterms, lpos);
grub_memmove (buf + lpos, buf + lpos + len,
sizeof (grub_uint32_t) * ((*llen) - lpos + 1));
(*llen) -= len;
cl_print_all (cl_terms, nterms, 0, buf + lpos, buf + (*llen));
cl_set_pos_all (cl_terms, nterms, lpos);
}
}
static void
cl_insert (struct cmdline_term *cl_terms, unsigned nterms,
grub_size_t *lpos, grub_size_t *llen,
grub_size_t *max_len, grub_uint32_t **buf,
const grub_uint32_t *str)
{
grub_size_t len = strlen_ucs4 (str);
if (len + (*llen) >= (*max_len))
{
grub_uint32_t *nbuf;
(*max_len) *= 2;
nbuf = grub_realloc ((*buf), sizeof (grub_uint32_t) * (*max_len));
if (nbuf)
(*buf) = nbuf;
else
{
grub_print_error ();
grub_errno = GRUB_ERR_NONE;
(*max_len) /= 2;
}
} }
void init_clterm_all (void) if (len + (*llen) < (*max_len))
{ {
unsigned i; grub_memmove ((*buf) + (*lpos) + len, (*buf) + (*lpos),
for (i = 0; i < nterms; i++) ((*llen) - (*lpos) + 1) * sizeof (grub_uint32_t));
init_clterm (&cl_terms[i]); grub_memmove ((*buf) + (*lpos), str, len * sizeof (grub_uint32_t));
(*llen) += len;
cl_set_pos_all (cl_terms, nterms, (*lpos));
cl_print_all (cl_terms, nterms, 0, *buf + (*lpos), *buf + (*llen));
(*lpos) += len;
cl_set_pos_all (cl_terms, nterms, (*lpos));
} }
}
/* Get a command-line. If ESC is pushed, return zero,
otherwise return command line. */
/* FIXME: The dumb interface is not supported yet. */
char *
grub_cmdline_get (const char *prompt_translated)
{
grub_size_t lpos, llen;
grub_uint32_t *buf;
grub_size_t max_len = 256;
int key;
int histpos = 0;
struct cmdline_term *cl_terms;
char *ret;
unsigned nterms;
buf = grub_malloc (max_len * sizeof (grub_uint32_t)); buf = grub_malloc (max_len * sizeof (grub_uint32_t));
if (!buf) if (!buf)
@ -414,7 +413,7 @@ grub_cmdline_get (const char *prompt_translated)
case GRUB_TERM_CTRL | 'a': case GRUB_TERM_CTRL | 'a':
case GRUB_TERM_KEY_HOME: case GRUB_TERM_KEY_HOME:
lpos = 0; lpos = 0;
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
break; break;
case GRUB_TERM_CTRL | 'b': case GRUB_TERM_CTRL | 'b':
@ -422,14 +421,14 @@ grub_cmdline_get (const char *prompt_translated)
if (lpos > 0) if (lpos > 0)
{ {
lpos--; lpos--;
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
} }
break; break;
case GRUB_TERM_CTRL | 'e': case GRUB_TERM_CTRL | 'e':
case GRUB_TERM_KEY_END: case GRUB_TERM_KEY_END:
lpos = llen; lpos = llen;
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
break; break;
case GRUB_TERM_CTRL | 'f': case GRUB_TERM_CTRL | 'f':
@ -437,7 +436,7 @@ grub_cmdline_get (const char *prompt_translated)
if (lpos < llen) if (lpos < llen)
{ {
lpos++; lpos++;
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
} }
break; break;
@ -469,12 +468,17 @@ grub_cmdline_get (const char *prompt_translated)
if (restore) if (restore)
{ {
unsigned i;
/* Restore the prompt. */ /* Restore the prompt. */
grub_xputs ("\n"); grub_xputs ("\n");
grub_xputs (prompt_translated); grub_xputs (prompt_translated);
grub_xputs (" "); grub_xputs (" ");
init_clterm_all ();
cl_print_all (0, 0); for (i = 0; i < nterms; i++)
init_clterm (&cl_terms[i]);
cl_print_all (cl_terms, nterms, 0, buf, buf + llen);
} }
if (insertu8) if (insertu8)
@ -501,20 +505,20 @@ grub_cmdline_get (const char *prompt_translated)
{ {
insert[t-1] = 0; insert[t-1] = 0;
if (t != 1) if (t != 1)
cl_insert (insert); cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, insert);
lpos++; lpos++;
} }
else else
{ {
insert[t] = 0; insert[t] = 0;
cl_insert (insert); cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, insert);
} }
} }
grub_free (insertu8); grub_free (insertu8);
grub_free (insert); grub_free (insert);
} }
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
} }
break; break;
@ -537,7 +541,8 @@ grub_cmdline_get (const char *prompt_translated)
kill_buf[llen - lpos] = 0; kill_buf[llen - lpos] = 0;
} }
cl_delete (llen - lpos); cl_delete (cl_terms, nterms,
buf, lpos, &llen, llen - lpos);
} }
break; break;
@ -554,9 +559,10 @@ grub_cmdline_get (const char *prompt_translated)
histpos--; histpos--;
} }
cl_delete (llen); cl_delete (cl_terms, nterms,
buf, lpos, &llen, llen);
hist = grub_history_get (histpos); hist = grub_history_get (histpos);
cl_insert (hist); cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, hist);
break; break;
} }
@ -574,10 +580,11 @@ grub_cmdline_get (const char *prompt_translated)
histpos++; histpos++;
} }
cl_delete (llen); cl_delete (cl_terms, nterms,
buf, lpos, &llen, llen);
hist = grub_history_get (histpos); hist = grub_history_get (histpos);
cl_insert (hist); cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, hist);
} }
break; break;
@ -601,14 +608,15 @@ grub_cmdline_get (const char *prompt_translated)
} }
lpos = 0; lpos = 0;
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
cl_delete (n); cl_delete (cl_terms, nterms,
buf, lpos, &llen, n);
} }
break; break;
case GRUB_TERM_CTRL | 'y': case GRUB_TERM_CTRL | 'y':
if (kill_buf) if (kill_buf)
cl_insert (kill_buf); cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, kill_buf);
break; break;
case '\e': case '\e':
@ -619,7 +627,7 @@ grub_cmdline_get (const char *prompt_translated)
if (lpos > 0) if (lpos > 0)
{ {
lpos--; lpos--;
cl_set_pos_all (); cl_set_pos_all (cl_terms, nterms, lpos);
} }
else else
break; break;
@ -628,7 +636,8 @@ grub_cmdline_get (const char *prompt_translated)
case GRUB_TERM_CTRL | 'd': case GRUB_TERM_CTRL | 'd':
case GRUB_TERM_KEY_DC: case GRUB_TERM_KEY_DC:
if (lpos < llen) if (lpos < llen)
cl_delete (1); cl_delete (cl_terms, nterms,
buf, lpos, &llen, 1);
break; break;
default: default:
@ -638,7 +647,7 @@ grub_cmdline_get (const char *prompt_translated)
str[0] = key; str[0] = key;
str[1] = '\0'; str[1] = '\0';
cl_insert (str); cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, str);
} }
break; break;
} }