term.c 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2002,2003,2005,2007,2008,2009  Free Software Foundation, Inc.
 *
 *  GRUB 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

19 20 21
#include <grub/term.h>
#include <grub/misc.h>
#include <grub/mm.h>
22 23 24 25
#include <grub/file.h>
#include <grub/dl.h>
#include <grub/env.h>
#include <grub/normal.h>
26
#include <grub/charset.h>
27
#include <grub/i18n.h>
28

29 30 31
struct term_state
{
  struct term_state *next;
32
  struct grub_unicode_glyph *backlog_glyphs;
33
  const grub_uint32_t *backlog_ucs4;
34
  int backlog_fixed_tab;
35 36
  grub_size_t backlog_len;

37 38 39 40
  int bidi_stack_depth;
  grub_uint8_t bidi_stack[GRUB_BIDI_MAX_EXPLICIT_LEVEL];
  int invalid_pushes;

41 42
  void *free;
  int num_lines;
43 44 45
  char *term_name;
};

46 47 48 49 50
static int
print_ucs4_real (const grub_uint32_t * str,
		 const grub_uint32_t * last_position,
		 int margin_left, int margin_right,
		 struct grub_term_output *term, int backlog,
51 52
		 int dry_run, int fixed_tab, unsigned skip_lines,
		 unsigned max_lines,
53 54
		 grub_uint32_t contchar, int fill_right,
		 struct grub_term_pos *pos);
55

56 57
static struct term_state *term_states = NULL;

58 59 60
/* If the more pager is active.  */
static int grub_more;

61
static void
62
putcode_real (grub_uint32_t code, struct grub_term_output *term, int fixed_tab);
63

64 65 66 67 68 69 70 71
void
grub_normal_reset_more (void)
{
  static struct term_state *state;
  for (state = term_states; state; state = state->next)
    state->num_lines = 0;
}

72
static void
73
print_more (void)
74
{
75
  char key;
76
  struct grub_term_coordinate *pos;
77 78
  grub_term_output_t term;
  grub_uint32_t *unicode_str, *unicode_last_position;
79

80 81 82
  /* TRANSLATORS: This has to fit on one line.  It's ok to include few
     words but don't write poems.  */
  grub_utf8_to_ucs4_alloc (_("--MORE--"), &unicode_str,
83
			   &unicode_last_position);
84

85
  if (!unicode_str)
86
    {
87 88 89
      grub_errno = GRUB_ERR_NONE;
      return;
    }
90

91 92
  pos = grub_term_save_pos ();

93 94 95 96 97 98
  grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);

  FOR_ACTIVE_TERM_OUTPUTS(term)
  {
    grub_print_ucs4 (unicode_str, unicode_last_position, 0, 0, term);
  }
99
  grub_setcolorstate (GRUB_TERM_COLOR_NORMAL);
100

101 102 103
  grub_free (unicode_str);
  
  key = grub_getkey ();
104

105 106 107 108 109
  /* Remove the message.  */
  grub_term_restore_pos (pos);
  FOR_ACTIVE_TERM_OUTPUTS(term)
    grub_print_spaces (term, 8);
  grub_term_restore_pos (pos);
110
  grub_free (pos);
111

112
  /* Scroll one line or an entire page, depending on the key.  */
113

114 115 116 117
  if (key == '\r' || key =='\n')
    {
      static struct term_state *state;
      for (state = term_states; state; state = state->next)
118
	state->num_lines--;
119
    }
120 121
  else
    grub_normal_reset_more ();
122 123 124 125 126
}

void
grub_set_more (int onoff)
{
127
  grub_more = !!onoff;
128
  grub_normal_reset_more ();
129 130
}

131 132
enum
  {
133
    GRUB_CP437_UPDOWNARROW     = 0x12,
134 135 136 137 138 139 140 141 142 143 144 145
    GRUB_CP437_UPARROW         = 0x18,
    GRUB_CP437_DOWNARROW       = 0x19,
    GRUB_CP437_RIGHTARROW      = 0x1a,
    GRUB_CP437_LEFTARROW       = 0x1b,
    GRUB_CP437_VLINE           = 0xb3,
    GRUB_CP437_CORNER_UR       = 0xbf,
    GRUB_CP437_CORNER_LL       = 0xc0,
    GRUB_CP437_HLINE           = 0xc4,
    GRUB_CP437_CORNER_LR       = 0xd9,
    GRUB_CP437_CORNER_UL       = 0xda,
  };

146 147 148 149 150 151 152 153
static grub_uint32_t
map_code (grub_uint32_t in, struct grub_term_output *term)
{
  if (in <= 0x7f)
    return in;

  switch (term->flags & GRUB_TERM_CODE_TYPE_MASK)
    {
154
    case GRUB_TERM_CODE_TYPE_CP437:
155 156
      switch (in)
	{
157
	case GRUB_UNICODE_LEFTARROW:
158
	  return GRUB_CP437_LEFTARROW;
159
	case GRUB_UNICODE_UPARROW:
160
	  return GRUB_CP437_UPARROW;
161 162
	case GRUB_UNICODE_UPDOWNARROW:
	  return GRUB_CP437_UPDOWNARROW;
163
	case GRUB_UNICODE_RIGHTARROW:
164
	  return GRUB_CP437_RIGHTARROW;
165
	case GRUB_UNICODE_DOWNARROW:
166
	  return GRUB_CP437_DOWNARROW;
167
	case GRUB_UNICODE_HLINE:
168
	  return GRUB_CP437_HLINE;
169
	case GRUB_UNICODE_VLINE:
170
	  return GRUB_CP437_VLINE;
171
	case GRUB_UNICODE_CORNER_UL:
172
	  return GRUB_CP437_CORNER_UL;
173
	case GRUB_UNICODE_CORNER_UR:
174
	  return GRUB_CP437_CORNER_UR;
175
	case GRUB_UNICODE_CORNER_LL:
176
	  return GRUB_CP437_CORNER_LL;
177
	case GRUB_UNICODE_CORNER_LR:
178
	  return GRUB_CP437_CORNER_LR;
179 180 181 182 183 184
	}
      return '?';
    case GRUB_TERM_CODE_TYPE_ASCII:
      /* Better than nothing.  */
      switch (in)
	{
185
	case GRUB_UNICODE_LEFTARROW:
186 187
	  return '<';
		
188
	case GRUB_UNICODE_UPARROW:
189 190
	  return '^';
	  
191
	case GRUB_UNICODE_RIGHTARROW:
192 193
	  return '>';
		
194
	case GRUB_UNICODE_DOWNARROW:
195 196
	  return 'v';
		  
197
	case GRUB_UNICODE_HLINE:
198 199
	  return '-';
		  
200
	case GRUB_UNICODE_VLINE:
201 202
	  return '|';
		  
203 204 205 206
	case GRUB_UNICODE_CORNER_UL:
	case GRUB_UNICODE_CORNER_UR:
	case GRUB_UNICODE_CORNER_LL:
	case GRUB_UNICODE_CORNER_LR:
207 208 209 210 211 212 213 214
	  return '+';
		
	}
      return '?';
    }
  return in;
}

215 216 217
void
grub_puts_terminal (const char *str, struct grub_term_output *term)
{
218
  grub_uint32_t *unicode_str, *unicode_last_position;
219
  grub_error_push ();
220 221
  grub_utf8_to_ucs4_alloc (str, &unicode_str,
			   &unicode_last_position);
222
  grub_error_pop ();
223 224
  if (!unicode_str)
    {
225
      for (; *str; str++)
226
	{
227 228 229 230 231 232 233 234
	  struct grub_unicode_glyph c =
	    {
	      .variant = 0,
	      .attributes = 0,
	      .ncomb = 0,
	      .estimated_width = 1,
	      .base = *str
	    };
235

236 237 238 239 240 241 242 243 244 245 246 247
	  FOR_ACTIVE_TERM_OUTPUTS(term)
	  {
	    (term->putchar) (term, &c);
	  }
	  if (*str == '\n')
	    {
	      c.base = '\r';
	      FOR_ACTIVE_TERM_OUTPUTS(term)
	      {
		(term->putchar) (term, &c);
	      }
	    }
248 249 250
	}
      return;
    }
251

252
  print_ucs4_real (unicode_str, unicode_last_position, 0, 0, term,
253
		   0, 0, 0, 0, -1, 0, 0, 0);
254
  grub_free (unicode_str);
255 256
}

257
struct grub_term_coordinate *
258 259 260 261
grub_term_save_pos (void)
{
  struct grub_term_output *cur;
  unsigned cnt = 0;
262
  struct grub_term_coordinate *ret, *ptr;
263
  
264 265
  FOR_ACTIVE_TERM_OUTPUTS(cur)
    cnt++;
266 267 268 269 270

  ret = grub_malloc (cnt * sizeof (ret[0]));
  if (!ret)
    return NULL;

271
  ptr = ret;
272 273
  FOR_ACTIVE_TERM_OUTPUTS(cur)
    *ptr++ = grub_term_getxy (cur);
274 275 276 277 278

  return ret;
}

void
279
grub_term_restore_pos (struct grub_term_coordinate *pos)
280 281
{
  struct grub_term_output *cur;
282
  struct grub_term_coordinate *ptr = pos;
283 284 285 286

  if (!pos)
    return;

287 288
  FOR_ACTIVE_TERM_OUTPUTS(cur)
  {
289
    grub_term_gotoxy (cur, *ptr);
290 291
    ptr++;
  }
292
}
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

static void 
grub_terminal_autoload_free (void)
{
  struct grub_term_autoload *cur, *next;
  unsigned i;
  for (i = 0; i < 2; i++)
    for (cur = i ? grub_term_input_autoload : grub_term_output_autoload;
	 cur; cur = next)
      {
	next = cur->next;
	grub_free (cur->name);
	grub_free (cur->modname);
	grub_free (cur);
      }
  grub_term_input_autoload = NULL;
  grub_term_output_autoload = NULL;
}

/* Read the file terminal.lst for auto-loading.  */
void
314
read_terminal_list (const char *prefix)
315 316 317 318 319 320 321 322 323 324 325
{
  char *filename;
  grub_file_t file;
  char *buf = NULL;

  if (!prefix)
    {
      grub_errno = GRUB_ERR_NONE;
      return;
    }
  
326 327
  filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM
			     "/terminal.lst", prefix);
328 329 330 331 332 333 334
  if (!filename)
    {
      grub_errno = GRUB_ERR_NONE;
      return;
    }

  file = grub_file_open (filename);
335
  grub_free (filename);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  if (!file)
    {
      grub_errno = GRUB_ERR_NONE;
      return;
    }

  /* Override previous terminal.lst.  */
  grub_terminal_autoload_free ();

  for (;; grub_free (buf))
    {
      char *p, *name;
      struct grub_term_autoload *cur;
      struct grub_term_autoload **target = NULL;
      
      buf = grub_file_getline (file);
	
      if (! buf)
	break;

356 357 358 359 360
      p = buf;
      while (grub_isspace (p[0]))
	p++;

      switch (p[0])
361 362 363 364 365 366 367 368 369 370 371 372
	{
	case 'i':
	  target = &grub_term_input_autoload;
	  break;

	case 'o':
	  target = &grub_term_output_autoload;
	  break;
	}
      if (!target)
	continue;
      
373
      name = p + 1;
374 375 376 377
            
      p = grub_strchr (name, ':');
      if (! p)
	continue;
378
      *p = 0;
379 380 381 382

      p++;
      while (*p == ' ' || *p == '\t')
	p++;
383 384 385 386 387 388 389 390 391

      cur = grub_malloc (sizeof (*cur));
      if (!cur)
	{
	  grub_errno = GRUB_ERR_NONE;
	  continue;
	}
      
      cur->name = grub_strdup (name);
392
      if (! cur->name)
393 394 395 396 397 398 399 400 401 402 403
	{
	  grub_errno = GRUB_ERR_NONE;
	  grub_free (cur);
	  continue;
	}
	
      cur->modname = grub_strdup (p);
      if (! cur->modname)
	{
	  grub_errno = GRUB_ERR_NONE;
	  grub_free (cur->name);
404
	  grub_free (cur);
405 406 407 408 409 410 411 412 413 414
	  continue;
	}
      cur->next = *target;
      *target = cur;
    }
  
  grub_file_close (file);

  grub_errno = GRUB_ERR_NONE;
}
415 416

static void
417 418
putglyph (const struct grub_unicode_glyph *c, struct grub_term_output *term,
	  int fixed_tab)
419 420 421 422 423 424 425 426 427
{
  struct grub_unicode_glyph c2 =
    {
      .variant = 0,
      .attributes = 0,
      .ncomb = 0,
      .estimated_width = 1
    };

428 429 430 431 432 433 434 435 436 437 438 439
  if (c->base == '\t' && fixed_tab)
    {
      int n;

      n = GRUB_TERM_TAB_WIDTH;
      c2.base = ' ';
      while (n--)
	(term->putchar) (term, &c2);

      return;
    }

440 441 442 443
  if (c->base == '\t' && term->getxy)
    {
      int n;

444
      n = GRUB_TERM_TAB_WIDTH - ((term->getxy (term).x)
445
				 % GRUB_TERM_TAB_WIDTH);
446 447
      c2.base = ' ';
      while (n--)
448
	(term->putchar) (term, &c2);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

      return;
    }

  if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
      == GRUB_TERM_CODE_TYPE_UTF8_LOGICAL 
      || (term->flags & GRUB_TERM_CODE_TYPE_MASK)
      == GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
    {
      int i;
      c2.estimated_width = grub_term_getcharwidth (term, c);
      for (i = -1; i < (int) c->ncomb; i++)
	{
	  grub_uint8_t u8[20], *ptr;
	  grub_uint32_t code;
	      
	  if (i == -1)
	    {
	      code = c->base;
	      if ((term->flags & GRUB_TERM_CODE_TYPE_MASK)
469 470 471 472 473 474
		  == GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
		{
		  if ((c->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR))
		    code = grub_unicode_mirror_code (code);
		  code = grub_unicode_shape_code (code, c->attributes);
		}
475 476
	    }
	  else
477
	    code = grub_unicode_get_comb (c) [i].code;
478 479 480 481 482 483

	  grub_ucs4_to_utf8 (&code, 1, u8, sizeof (u8));

	  for (ptr = u8; *ptr; ptr++)
	    {
	      c2.base = *ptr;
484
	      (term->putchar) (term, &c2);
485 486 487 488 489 490
	      c2.estimated_width = 0;
	    }
	}
      c2.estimated_width = 1;
    }
  else
491
    (term->putchar) (term, c);
492 493 494 495

  if (c->base == '\n')
    {
      c2.base = '\r';
496
      (term->putchar) (term, &c2);
497 498 499 500
    }
}

static void
501
putcode_real (grub_uint32_t code, struct grub_term_output *term, int fixed_tab)
502 503 504 505 506 507 508 509 510 511
{
  struct grub_unicode_glyph c =
    {
      .variant = 0,
      .attributes = 0,
      .ncomb = 0,
      .estimated_width = 1
    };

  c.base = map_code (code, term);
512
  putglyph (&c, term, fixed_tab);
513 514 515 516 517 518 519 520 521 522
}

/* Put a Unicode character.  */
void
grub_putcode (grub_uint32_t code, struct grub_term_output *term)
{
  /* Combining character by itself?  */
  if (grub_unicode_get_comb_type (code) != GRUB_UNICODE_COMB_NONE)
    return;

523
  putcode_real (code, term, 0);
524 525
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
static grub_ssize_t
get_maxwidth (struct grub_term_output *term,
	      int margin_left, int margin_right)
{
  struct grub_unicode_glyph space_glyph = {
    .base = ' ',
    .variant = 0,
    .attributes = 0,
    .ncomb = 0,
  };
  return (grub_term_width (term)
	  - grub_term_getcharwidth (term, &space_glyph) 
	  * (margin_left + margin_right) - 1);
}

static grub_ssize_t
get_startwidth (struct grub_term_output *term,
		int margin_left)
544
{
545
  return (term->getxy (term).x) - margin_left;
546 547
}

548 549 550
static void
fill_margin (struct grub_term_output *term, int r)
{
551 552
  int sp = (term->getwh (term).x)
    - (term->getxy (term).x) - r;
553 554 555 556
  if (sp > 0)
    grub_print_spaces (term, sp);
}

557 558 559 560 561
static int
print_ucs4_terminal (const grub_uint32_t * str,
		     const grub_uint32_t * last_position,
		     int margin_left, int margin_right,
		     struct grub_term_output *term,
562
		     struct term_state *state,
563 564 565
		     int dry_run, int fixed_tab, unsigned skip_lines,
		     unsigned max_lines,
		     grub_uint32_t contchar,
566
		     int primitive_wrap, int fill_right, struct grub_term_pos *pos)
567 568
{
  const grub_uint32_t *ptr;
569
  grub_ssize_t startwidth = dry_run ? 0 : get_startwidth (term, margin_left);
570 571 572 573
  grub_ssize_t line_width = startwidth;
  grub_ssize_t lastspacewidth = 0;
  grub_ssize_t max_width = get_maxwidth (term, margin_left, margin_right);
  const grub_uint32_t *line_start = str, *last_space = str - 1;
574
  int lines = 0;
575 576 577 578 579 580 581 582 583 584 585 586
  int i;
  struct term_state local_state;

  if (!state)
    {
      grub_memset (&local_state, 0, sizeof (local_state));
      state = &local_state;
    }

  for (i = 0; i < state->bidi_stack_depth; i++)
    putcode_real (state->bidi_stack[i] | (GRUB_UNICODE_LRE & ~0xff),
		  term, fixed_tab);
587 588 589 590

  for (ptr = str; ptr < last_position; ptr++)
    {
      grub_ssize_t last_width = 0;
591

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
      switch (*ptr)
	{
	case GRUB_UNICODE_LRE:
	case GRUB_UNICODE_RLE:
	case GRUB_UNICODE_LRO:
	case GRUB_UNICODE_RLO:
	  if (state->bidi_stack_depth >= (int) ARRAY_SIZE (state->bidi_stack))
	    state->invalid_pushes++;
	  else
	    state->bidi_stack[state->bidi_stack_depth++] = *ptr;
	  break;
	case GRUB_UNICODE_PDF:
	  if (state->invalid_pushes)
	    state->invalid_pushes--;
	  else if (state->bidi_stack_depth)
	    state->bidi_stack_depth--;
	  break;
	}
610 611 612 613 614 615 616 617
      if (grub_unicode_get_comb_type (*ptr) == GRUB_UNICODE_COMB_NONE)
	{
	  struct grub_unicode_glyph c = {
	    .variant = 0,
	    .attributes = 0,
	    .ncomb = 0,
	  };
	  c.base = *ptr;
618 619 620 621 622 623
	  if (pos)
	    {
	      pos[ptr - str].x = line_width;
	      pos[ptr - str].y = lines;
	      pos[ptr - str].valid = 1;
	    }
624 625 626
	  line_width += last_width = grub_term_getcharwidth (term, &c);
	}

627
      if (*ptr == ' ' && !primitive_wrap)
628 629 630 631 632 633 634 635
	{
	  lastspacewidth = line_width;
	  last_space = ptr;
	}

      if (line_width > max_width || *ptr == '\n')
	{
	  const grub_uint32_t *ptr2;
636 637 638 639 640 641 642
	  int wasn = (*ptr == '\n');

	  if (wasn)
	    {
	      state->bidi_stack_depth = 0;
	      state->invalid_pushes = 0;
	    }
643 644 645 646

	  if (line_width > max_width && last_space > line_start)
	    ptr = last_space;
	  else if (line_width > max_width 
647
		   && line_start == str && line_width - lastspacewidth < max_width - 5)
648 649 650 651 652 653 654
	    {
	      ptr = str;
	      lastspacewidth = startwidth;
	    }
	  else
	    lastspacewidth = line_width - last_width;

655
	  lines++;
656

657
	  if (!skip_lines && !dry_run)
658
	    {
659 660 661 662 663 664 665 666
	      for (ptr2 = line_start; ptr2 < ptr; ptr2++)
		{
		  /* Skip combining characters on non-UTF8 terminals.  */
		  if ((term->flags & GRUB_TERM_CODE_TYPE_MASK) 
		      != GRUB_TERM_CODE_TYPE_UTF8_LOGICAL
		      && grub_unicode_get_comb_type (*ptr2)
		      != GRUB_UNICODE_COMB_NONE)
		    continue;
667
		  putcode_real (*ptr2, term, fixed_tab);
668 669
		}

670 671
	      if (!wasn && contchar)
		putcode_real (contchar, term, fixed_tab);
672
	      if (fill_right)
673
		fill_margin (term, margin_right);
674

675 676
	      if (!contchar || max_lines != 1)
		grub_putcode ('\n', term);
677
	      if (state != &local_state && ++state->num_lines
678 679 680 681 682
		  >= (grub_ssize_t) grub_term_height (term) - 2)
		{
		  state->backlog_ucs4 = (ptr == last_space || *ptr == '\n') 
		    ? ptr + 1 : ptr;
		  state->backlog_len = last_position - state->backlog_ucs4;
683
		  state->backlog_fixed_tab = fixed_tab;
684 685
		  return 1;
		}
686 687 688 689 690
	    }

	  line_width -= lastspacewidth;
	  if (ptr == last_space || *ptr == '\n')
	    ptr++;
691 692 693 694 695 696 697
	  else if (pos)
	      {
		pos[ptr - str].x = line_width - last_width;
		pos[ptr - str].y = lines;
		pos[ptr - str].valid = 1;
	      }

698
	  line_start = ptr;
699 700 701 702 703 704 705 706 707 708 709 710 711 712

	  if (skip_lines)
	    skip_lines--;
	  else if (max_lines != (unsigned) -1)
	    {
	      max_lines--;
	      if (!max_lines)
		break;
	    }
	  if (!skip_lines && !dry_run)
	    {
	      if (!contchar)
		grub_print_spaces (term, margin_left);
	      else
713 714
		grub_term_gotoxy (term, (struct grub_term_coordinate)
				  { margin_left, grub_term_getxy (term).y });
715 716 717 718
	      for (i = 0; i < state->bidi_stack_depth; i++)
		putcode_real (state->bidi_stack[i] | (GRUB_UNICODE_LRE & ~0xff),
			      term, fixed_tab);
	    }
719 720
	}
    }
721

722 723 724 725 726 727 728
  if (pos)
    {
      pos[ptr - str].x = line_width;
      pos[ptr - str].y = lines;
      pos[ptr - str].valid = 1;
    }

729
  if (line_start < last_position)
730
      lines++;
731
  if (!dry_run && !skip_lines && max_lines)
732 733
    {
      const grub_uint32_t *ptr2;
734

735 736 737 738 739 740 741 742
      for (ptr2 = line_start; ptr2 < last_position; ptr2++)
	{
	  /* Skip combining characters on non-UTF8 terminals.  */
	  if ((term->flags & GRUB_TERM_CODE_TYPE_MASK) 
	      != GRUB_TERM_CODE_TYPE_UTF8_LOGICAL
	      && grub_unicode_get_comb_type (*ptr2)
	      != GRUB_UNICODE_COMB_NONE)
	    continue;
743
	  putcode_real (*ptr2, term, fixed_tab);
744
	}
745

746 747
      if (fill_right)
	fill_margin (term, margin_right);
748 749
    }
  return dry_run ? lines : 0;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
}

static struct term_state *
find_term_state (struct grub_term_output *term)
{
  struct term_state *state;
  for (state = term_states; state; state = state->next)
    if (grub_strcmp (state->term_name, term->name) == 0)
      return state;

  state = grub_zalloc (sizeof (*state));
  if (!state)
    {
      grub_errno = GRUB_ERR_NONE;
      return NULL;
    }

  state->term_name = grub_strdup (term->name);
  state->next = term_states;
  term_states = state;

  return state;
}

static int
775
put_glyphs_terminal (struct grub_unicode_glyph *visual,
776 777 778
		     grub_ssize_t visual_len,
		     int margin_left, int margin_right,
		     struct grub_term_output *term,
779
		     struct term_state *state, int fixed_tab,
780 781
		     grub_uint32_t contchar,
		     int fill_right)
782
{
783
  struct grub_unicode_glyph *visual_ptr;
784
  int since_last_nl = 1;
785 786
  for (visual_ptr = visual; visual_ptr < visual + visual_len; visual_ptr++)
    {
787
      if (visual_ptr->base == '\n' && fill_right)
788 789
	fill_margin (term, margin_right);

790
      putglyph (visual_ptr, term, fixed_tab);
791
      since_last_nl++;
792
      if (visual_ptr->base == '\n')
793
	{
794
	  since_last_nl = 0;
795 796 797 798
	  if (state && ++state->num_lines
	      >= (grub_ssize_t) grub_term_height (term) - 2)
	    {
	      state->backlog_glyphs = visual_ptr + 1;
799
	      state->backlog_len = visual_len - (visual_ptr - visual) - 1;
800
	      state->backlog_fixed_tab = fixed_tab;
801 802 803
	      return 1;
	    }

804 805 806
	  if (!contchar)
	    grub_print_spaces (term, margin_left);
	  else
807 808 809
	    grub_term_gotoxy (term,
			      (struct grub_term_coordinate)
			      { margin_left, grub_term_getxy (term).y });
810
	}
811
      grub_unicode_destroy_glyph (visual_ptr);
812
    }
813
  if (fill_right && since_last_nl)
814 815
	fill_margin (term, margin_right);

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  return 0;
}

static int
print_backlog (struct grub_term_output *term,
	       int margin_left, int margin_right)
{
  struct term_state *state = find_term_state (term);

  if (!state)
    return 0;

  if (state->backlog_ucs4)
    {
      int ret;
      ret = print_ucs4_terminal (state->backlog_ucs4,
				 state->backlog_ucs4 + state->backlog_len,
833
				 margin_left, margin_right, term, state, 0,
834
				 state->backlog_fixed_tab, 0, -1, 0, 0,
835
				 0, 0);
836 837 838 839 840
      if (!ret)
	{
	  grub_free (state->free);
	  state->free = NULL;
	  state->backlog_len = 0;
841
	  state->backlog_ucs4 = 0;
842 843 844 845 846 847 848 849 850
	}
      return ret;
    }

  if (state->backlog_glyphs)
    {
      int ret;
      ret = put_glyphs_terminal (state->backlog_glyphs,
				 state->backlog_len,
851
				 margin_left, margin_right, term, state,
852
				 state->backlog_fixed_tab, 0, 0);
853 854 855 856 857
      if (!ret)
	{
	  grub_free (state->free);
	  state->free = NULL;
	  state->backlog_len = 0;
858
	  state->backlog_glyphs = 0;
859 860 861 862 863 864 865
	}
      return ret;
    }

  return 0;
}

866
static grub_size_t
867 868 869 870 871
getcharwidth (const struct grub_unicode_glyph *c, void *term)
{
  return grub_term_getcharwidth (term, c);
}

872 873 874 875
static int
print_ucs4_real (const grub_uint32_t * str,
		 const grub_uint32_t * last_position,
		 int margin_left, int margin_right,
876
		 struct grub_term_output *term, int backlog,
877 878
		 int dry_run, int fixed_tab, unsigned skip_lines,
		 unsigned max_lines,
879 880
		 grub_uint32_t contchar, int fill_right,
		 struct grub_term_pos *pos)
881 882 883
{
  struct term_state *state = NULL;

884 885
  if (!dry_run)
    {
886
      struct grub_term_coordinate xy;
887 888
      if (backlog)
	state = find_term_state (term);
889

890 891
      xy = term->getxy (term);
      
892
      if (xy.x < margin_left)
893 894
	{
	  if (!contchar)
895
	    grub_print_spaces (term, margin_left - xy.x);
896
	  else
897 898
	    grub_term_gotoxy (term, (struct grub_term_coordinate) {margin_left,
		  xy.y});
899
	}
900
    }
901 902 903 904 905 906 907 908

  if ((term->flags & GRUB_TERM_CODE_TYPE_MASK) 
      == GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS
      || (term->flags & GRUB_TERM_CODE_TYPE_MASK) 
      == GRUB_TERM_CODE_TYPE_UTF8_VISUAL)
    {
      grub_ssize_t visual_len;
      struct grub_unicode_glyph *visual;
909 910
      grub_ssize_t visual_len_show;
      struct grub_unicode_glyph *visual_show;
911
      int ret;
912
      struct grub_unicode_glyph *vptr;
913 914

      visual_len = grub_bidi_logical_to_visual (str, last_position - str,
915
						&visual, getcharwidth, term,
916 917 918
						get_maxwidth (term, 
							      margin_left,
							      margin_right),
919 920
						dry_run ? 0 : get_startwidth (term, 
									      margin_left),
921
						contchar, pos, !!contchar);
922 923 924
      if (visual_len < 0)
	{
	  grub_print_error ();
925
	  return 0;
926
	}
927 928 929 930 931 932 933 934
      visual_show = visual;
      for (; skip_lines && visual_show < visual + visual_len; visual_show++)
	if (visual_show->base == '\n')
	  skip_lines--;
      if (max_lines != (unsigned) -1)
	{
	  for (vptr = visual_show;
	       max_lines && vptr < visual + visual_len; vptr++)
935
	    if (vptr->base == '\n')
936 937 938 939 940 941 942
	      max_lines--;

	  visual_len_show = vptr - visual_show;	  
	}
      else
	visual_len_show = visual + visual_len - visual_show;

943 944 945
      if (dry_run)
	{
	  ret = 0;
946
	  for (vptr = visual_show; vptr < visual_show + visual_len_show; vptr++)
947 948
	    if (vptr->base == '\n')
	      ret++;
949
	  if (visual_len_show && visual[visual_len_show - 1].base != '\n')
950
	    ret++;
951
	  for (vptr = visual; vptr < visual + visual_len; vptr++)
952
	    grub_unicode_destroy_glyph (vptr);
953 954
	  grub_free (visual);
	}
955
      else
956
	{
957
	  ret = put_glyphs_terminal (visual_show, visual_len_show, margin_left,
958
				     margin_right,
959
				     term, state, fixed_tab, contchar, fill_right);
960

961 962 963 964 965
	  if (!ret)
	    grub_free (visual);
	  else
	    state->free = visual;
	}
966
      return ret;
967
    }
968
  return print_ucs4_terminal (str, last_position, margin_left, margin_right,
969
			      term, state, dry_run, fixed_tab, skip_lines,
970
			      max_lines, contchar, !!contchar, fill_right, pos);
971 972 973 974 975 976 977 978 979 980 981 982 983
}

void
grub_print_ucs4_menu (const grub_uint32_t * str,
		      const grub_uint32_t * last_position,
		      int margin_left, int margin_right,
		      struct grub_term_output *term,
		      int skip_lines, int max_lines,
		      grub_uint32_t contchar,
		      struct grub_term_pos *pos)
{
  print_ucs4_real (str, last_position, margin_left, margin_right,
		   term, 0, 0, 1, skip_lines, max_lines,
984
		   contchar, 1, pos);
985
}
986

987 988 989 990 991 992 993
void
grub_print_ucs4 (const grub_uint32_t * str,
		 const grub_uint32_t * last_position,
		 int margin_left, int margin_right,
		 struct grub_term_output *term)
{
  print_ucs4_real (str, last_position, margin_left, margin_right,
994
		   term, 0, 0, 1, 0, -1, 0, 0, 0);
995 996
}

997 998 999 1000 1001 1002 1003
int
grub_ucs4_count_lines (const grub_uint32_t * str,
		       const grub_uint32_t * last_position,
		       int margin_left, int margin_right,
		       struct grub_term_output *term)
{
  return print_ucs4_real (str, last_position, margin_left, margin_right,
1004
			  term, 0, 1, 1, 0, -1, 0, 0, 0);
1005
}
1006

1007
void
1008
grub_xnputs (const char *str, grub_size_t msg_len)
1009
{
1010
  grub_uint32_t *unicode_str = NULL, *unicode_last_position;
1011
  int backlog = 0;
1012 1013 1014
  grub_term_output_t term;

  grub_error_push ();
1015 1016 1017

  unicode_str = grub_malloc (msg_len * sizeof (grub_uint32_t));
 
1018
  grub_error_pop ();
1019

1020 1021
  if (!unicode_str)
    {
1022
      for (; msg_len--; str++, msg_len++)
1023
	{
1024 1025 1026 1027 1028 1029 1030 1031
	  struct grub_unicode_glyph c =
	    {
	      .variant = 0,
	      .attributes = 0,
	      .ncomb = 0,
	      .estimated_width = 1,
	      .base = *str
	    };
1032 1033 1034

	  FOR_ACTIVE_TERM_OUTPUTS(term)
	  {
1035
	    (term->putchar) (term, &c);
1036
	  }
1037 1038 1039 1040 1041 1042 1043 1044
	  if (*str == '\n')
	    {
	      c.base = '\r';
	      FOR_ACTIVE_TERM_OUTPUTS(term)
	      {
		(term->putchar) (term, &c);
	      }
	    }
1045 1046
	}

1047 1048 1049
      return;
    }

1050 1051 1052 1053
  msg_len = grub_utf8_to_ucs4 (unicode_str, msg_len,
			       (grub_uint8_t *) str, -1, 0);
  unicode_last_position = unicode_str + msg_len;

1054 1055
  FOR_ACTIVE_TERM_OUTPUTS(term)
  {
1056 1057
    int cur;
    cur = print_ucs4_real (unicode_str, unicode_last_position, 0, 0,
1058
			   term, grub_more, 0, 0, 0, -1, 0, 0, 0);
1059 1060
    if (cur)
      backlog = 1;
1061
  }
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
  while (backlog)
    {
      print_more ();
      backlog = 0;
      FOR_ACTIVE_TERM_OUTPUTS(term)
      {
	int cur;
	cur = print_backlog (term, 0, 0);
	if (cur)
	  backlog = 1;
      }
    }
1074
  grub_free (unicode_str);
1075
}
1076

1077 1078 1079 1080 1081 1082
void
grub_xputs_normal (const char *str)
{
  grub_xnputs (str, grub_strlen (str));
}

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
void
grub_cls (void)
{
  struct grub_term_output *term;

  FOR_ACTIVE_TERM_OUTPUTS(term)  
  {
    if ((term->flags & GRUB_TERM_DUMB) || (grub_env_get ("debug")))
      {
	grub_putcode ('\n', term);
	grub_term_refresh (term);
      }
    else
      (term->cls) (term);
  }
}