~jonathank89/burg/burg-percise

« back to all changes in this revision

Viewing changes to normal/main.c

merge mainline into mips

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <grub/menu_viewer.h>
31
31
#include <grub/auth.h>
32
32
#include <grub/i18n.h>
 
33
#include <grub/charset.h>
33
34
 
34
35
#define GRUB_DEFAULT_HISTORY_SIZE       50
35
36
 
 
37
static int nested_level = 0;
 
38
int grub_normal_exit_level = 0;
 
39
 
36
40
/* Read a line from the file FILE.  */
37
41
char *
38
42
grub_file_getline (grub_file_t file)
372
376
  if (! file)
373
377
    return 0;
374
378
 
375
 
  grub_reader_loop (getline);
 
379
  while (1)
 
380
    {
 
381
      char *line;
 
382
 
 
383
      /* Print an error, if any.  */
 
384
      grub_print_error ();
 
385
      grub_errno = GRUB_ERR_NONE;
 
386
 
 
387
      if ((getline (&line, 0)) || (! line))
 
388
        break;
 
389
 
 
390
      grub_parser_get_current ()->parse_line (line, getline);
 
391
      grub_free (line);
 
392
    }
 
393
 
376
394
  grub_file_close (file);
377
395
 
378
396
  if (old_parser)
383
401
 
384
402
/* Initialize the screen.  */
385
403
void
386
 
grub_normal_init_page (void)
 
404
grub_normal_init_page (struct grub_term_output *term)
387
405
{
388
406
  int msg_len;
389
407
  int posx;
391
409
 
392
410
  char *msg_formatted = grub_malloc (grub_strlen(msg) +
393
411
                                     grub_strlen(PACKAGE_VERSION));
394
 
 
395
 
  grub_cls ();
396
 
 
397
 
  grub_sprintf (msg_formatted, msg, PACKAGE_VERSION);
398
 
 
399
412
  grub_uint32_t *unicode_msg;
400
413
  grub_uint32_t *last_position;
401
414
 
 
415
  grub_term_cls (term);
 
416
 
 
417
  grub_sprintf (msg_formatted, msg, PACKAGE_VERSION);
 
418
 
402
419
  msg_len = grub_utf8_to_ucs4_alloc (msg_formatted,
403
420
                                     &unicode_msg, &last_position);
404
421
 
407
424
      return;
408
425
    }
409
426
 
410
 
  posx = grub_getstringwidth (unicode_msg, last_position);
411
 
  posx = (GRUB_TERM_WIDTH - posx) / 2;
412
 
  grub_gotoxy (posx, 1);
 
427
  posx = grub_getstringwidth (unicode_msg, last_position, term);
 
428
  posx = (grub_term_width (term) - posx) / 2;
 
429
  grub_term_gotoxy (term, posx, 1);
413
430
 
414
 
  grub_print_ucs4 (unicode_msg, last_position);
 
431
  grub_print_ucs4 (unicode_msg, last_position, term);
415
432
  grub_printf("\n\n");
416
433
  grub_free (unicode_msg);
417
434
}
418
435
 
419
 
static int reader_nested;
 
436
static char *
 
437
read_lists (struct grub_env_var *var __attribute__ ((unused)),
 
438
            const char *val)
 
439
{
 
440
  read_command_list ();
 
441
  read_fs_list ();
 
442
  read_crypto_list ();
 
443
  read_terminal_list ();
 
444
  return val ? grub_strdup (val) : NULL;
 
445
}
420
446
 
421
447
/* Read the config file CONFIG and execute the menu interface or
422
448
   the command line interface if BATCH is false.  */
425
451
{
426
452
  grub_menu_t menu = 0;
427
453
 
428
 
  read_command_list ();
429
 
  read_fs_list ();
 
454
  read_lists (NULL, NULL);
430
455
  read_handler_list ();
 
456
  grub_register_variable_hook ("prefix", NULL, read_lists);
431
457
  grub_command_execute ("parser.grub", 0, 0);
432
458
 
433
 
  reader_nested = nested;
434
 
 
435
459
  if (config)
436
460
    {
437
461
      menu = read_config_file (config);
444
468
    {
445
469
      if (menu && menu->size)
446
470
        {
447
 
          grub_menu_viewer_show_menu (menu, nested);
 
471
          grub_show_menu (menu, nested);
448
472
          if (nested)
449
473
            free_menu (menu);
450
474
        }
455
479
void
456
480
grub_enter_normal_mode (const char *config)
457
481
{
 
482
  nested_level++;
458
483
  grub_normal_execute (config, 0, 0);
 
484
  grub_cmdline_run (0);
 
485
  nested_level--;
 
486
  if (grub_normal_exit_level)
 
487
    grub_normal_exit_level--;
459
488
}
460
489
 
461
490
/* Enter normal mode from rescue mode.  */
462
491
static grub_err_t
463
 
grub_cmd_normal (struct grub_command *cmd,
 
492
grub_cmd_normal (struct grub_command *cmd __attribute__ ((unused)),
464
493
                 int argc, char *argv[])
465
494
{
466
 
  grub_unregister_command (cmd);
467
 
 
468
495
  if (argc == 0)
469
496
    {
470
497
      /* Guess the config filename. It is necessary to make CONFIG static,
471
498
         so that it won't get broken by longjmp.  */
472
 
      static char *config;
 
499
      char *config;
473
500
      const char *prefix;
474
501
 
475
502
      prefix = grub_env_get ("prefix");
493
520
  return 0;
494
521
}
495
522
 
496
 
void
497
 
grub_cmdline_run (int nested)
 
523
/* Exit from normal mode to rescue mode.  */
 
524
static grub_err_t
 
525
grub_cmd_normal_exit (struct grub_command *cmd __attribute__ ((unused)),
 
526
                      int argc __attribute__ ((unused)),
 
527
                      char *argv[] __attribute__ ((unused)))
498
528
{
499
 
  grub_reader_t reader;
500
 
  grub_err_t err = GRUB_ERR_NONE;
501
 
 
502
 
  err = grub_auth_check_authentication (NULL);
503
 
 
504
 
  if (err)
505
 
    {
506
 
      grub_print_error ();
507
 
      grub_errno = GRUB_ERR_NONE;
508
 
      return;
509
 
    }
510
 
 
511
 
  reader = grub_reader_get_current ();
512
 
 
513
 
  reader_nested = nested;
514
 
  if (reader->init)
515
 
    reader->init ();
516
 
  grub_reader_loop (0);
 
529
  if (nested_level <= grub_normal_exit_level)
 
530
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in normal environment");
 
531
  grub_normal_exit_level++;
 
532
  return GRUB_ERR_NONE;
517
533
}
518
534
 
519
535
static grub_err_t
520
 
grub_normal_reader_init (void)
 
536
grub_normal_reader_init (int nested)
521
537
{
522
 
  grub_normal_init_page ();
523
 
  grub_setcursor (1);
524
 
 
 
538
  struct grub_term_output *term;
525
539
  const char *msg = _("Minimal BASH-like line editing is supported. For "
526
540
                      "the first word, TAB lists possible command completions. Anywhere "
527
541
                      "else TAB lists possible device or file completions. %s");
528
 
 
529
542
  const char *msg_esc = _("ESC at any time exits.");
530
 
 
531
543
  char *msg_formatted = grub_malloc (sizeof (char) * (grub_strlen (msg) +
532
544
                grub_strlen(msg_esc) + 1));
533
545
 
534
 
  grub_sprintf (msg_formatted, msg, reader_nested ? msg_esc : "");
535
 
  grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN);
536
 
  grub_puts ("\n");
 
546
  grub_sprintf (msg_formatted, msg, nested ? msg_esc : "");
537
547
 
 
548
  FOR_ACTIVE_TERM_OUTPUTS(term)
 
549
  {
 
550
    grub_normal_init_page (term);
 
551
    grub_term_setcursor (term, 1);
 
552
    
 
553
    grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
 
554
    grub_puts ("\n");
 
555
  }
538
556
  grub_free (msg_formatted);
539
557
 
540
558
  return 0;
541
559
}
542
560
 
543
 
static char cmdline[GRUB_MAX_CMDLINE];
544
561
 
545
562
static grub_err_t
546
 
grub_normal_read_line (char **line, int cont)
 
563
grub_normal_read_line_real (char **line, int cont, int nested)
547
564
{
548
565
  grub_parser_t parser = grub_parser_get_current ();
549
566
  char prompt[sizeof(">") + grub_strlen (parser->name)];
550
567
 
551
 
  grub_sprintf (prompt, "%s>", parser->name);
 
568
  if (cont)
 
569
    grub_sprintf (prompt, ">");
 
570
  else
 
571
    grub_sprintf (prompt, "%s>", parser->name);
552
572
 
553
573
  while (1)
554
574
    {
555
 
      cmdline[0] = 0;
556
 
      if (grub_cmdline_get (prompt, cmdline, sizeof (cmdline), 0, 1, 1))
 
575
      *line = grub_cmdline_get (prompt);
 
576
      if (*line)
557
577
        break;
558
578
 
559
 
      if ((reader_nested) || (cont))
 
579
      if (cont || nested)
560
580
        {
 
581
          grub_free (*line);
561
582
          *line = 0;
562
583
          return grub_errno;
563
584
        }
564
585
    }
565
586
 
566
 
  *line = grub_strdup (cmdline);
567
587
  return 0;
568
588
}
569
589
 
570
 
static struct grub_reader grub_normal_reader =
571
 
  {
572
 
    .name = "normal",
573
 
    .init = grub_normal_reader_init,
574
 
    .read_line = grub_normal_read_line
575
 
  };
 
590
static grub_err_t
 
591
grub_normal_read_line (char **line, int cont)
 
592
{
 
593
  return grub_normal_read_line_real (line, cont, 0);
 
594
}
 
595
 
 
596
void
 
597
grub_cmdline_run (int nested)
 
598
{
 
599
  grub_err_t err = GRUB_ERR_NONE;
 
600
 
 
601
  err = grub_auth_check_authentication (NULL);
 
602
 
 
603
  if (err)
 
604
    {
 
605
      grub_print_error ();
 
606
      grub_errno = GRUB_ERR_NONE;
 
607
      return;
 
608
    }
 
609
 
 
610
  grub_normal_reader_init (nested);
 
611
 
 
612
  while (1)
 
613
    {
 
614
      char *line;
 
615
 
 
616
      if (grub_normal_exit_level)
 
617
        break;
 
618
 
 
619
      /* Print an error, if any.  */
 
620
      grub_print_error ();
 
621
      grub_errno = GRUB_ERR_NONE;
 
622
 
 
623
      grub_normal_read_line_real (&line, 0, nested);
 
624
      if (! line)
 
625
        break;
 
626
 
 
627
      grub_parser_get_current ()->parse_line (line, grub_normal_read_line);
 
628
      grub_free (line);
 
629
    }
 
630
}
576
631
 
577
632
static char *
578
633
grub_env_write_pager (struct grub_env_var *var __attribute__ ((unused)),
588
643
  if (mod)
589
644
    grub_dl_ref (mod);
590
645
 
591
 
  grub_menu_viewer_register (&grub_normal_text_menu_viewer);
592
 
 
593
646
  grub_set_history (GRUB_DEFAULT_HISTORY_SIZE);
594
647
 
595
 
  grub_reader_register ("normal", &grub_normal_reader);
596
 
  grub_reader_set_current (&grub_normal_reader);
597
648
  grub_register_variable_hook ("pager", 0, grub_env_write_pager);
598
649
 
599
650
  /* Register a command "normal" for the rescue mode.  */
600
 
  grub_register_command_prio ("normal", grub_cmd_normal,
601
 
                              0, "Enter normal mode.", 0);
 
651
  grub_register_command ("normal", grub_cmd_normal,
 
652
                         0, "Enter normal mode.");
 
653
  grub_register_command ("normal_exit", grub_cmd_normal_exit,
 
654
                         0, "Exit from normal mode.");
602
655
 
603
656
  /* Reload terminal colors when these variables are written to.  */
604
657
  grub_register_variable_hook ("color_normal", NULL, grub_env_write_color_normal);
612
665
GRUB_MOD_FINI(normal)
613
666
{
614
667
  grub_set_history (0);
615
 
  grub_reader_unregister (&grub_normal_reader);
616
668
  grub_register_variable_hook ("pager", 0, 0);
617
669
  grub_fs_autoload_hook = 0;
618
670
  free_handler_list ();