misc.c 20 KB
Newer Older
okuji's avatar
okuji committed
1 2
/* misc.c - definitions of misc functions */
/*
3
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010  Free Software Foundation, Inc.
okuji's avatar
okuji committed
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
okuji's avatar
okuji committed
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
okuji's avatar
okuji committed
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
okuji's avatar
okuji committed
12 13 14 15 16
 *  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
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
okuji's avatar
okuji committed
18 19
 */

20 21 22
#include <grub/misc.h>
#include <grub/err.h>
#include <grub/mm.h>
okuji's avatar
okuji committed
23
#include <stdarg.h>
24 25
#include <grub/term.h>
#include <grub/env.h>
26
#include <grub/i18n.h>
okuji's avatar
okuji committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
union printf_arg
{
  /* Yes, type is also part of union as the moment we fill the value
     we don't need to store its type anymore (when we'll need it, we'll
     have format spec again. So save some space.  */
  enum
    {
      INT, LONG, LONGLONG,
      UNSIGNED_INT = 3, UNSIGNED_LONG, UNSIGNED_LONGLONG
    } type;
  long long ll;
};

struct printf_args
{
  union printf_arg prealloc[32];
  union printf_arg *ptr;
  grub_size_t count;
};

static void
parse_printf_args (const char *fmt0, struct printf_args *args,
		   va_list args_in);
51
static int
52 53 54 55 56 57 58 59 60
grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
		     struct printf_args *args);

static void
free_printf_args (struct printf_args *args)
{
  if (args->ptr != args->prealloc)
    grub_free (args->ptr);
}
61

62 63 64 65 66 67
static int
grub_iswordseparator (int c)
{
  return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
}

68
/* grub_gettext_dummy is not translating anything.  */
69
static const char *
70 71 72 73 74
grub_gettext_dummy (const char *s)
{
  return s;
}

75 76
const char* (*grub_gettext) (const char *s) = grub_gettext_dummy;

okuji's avatar
okuji committed
77
void *
78
grub_memmove (void *dest, const void *src, grub_size_t n)
okuji's avatar
okuji committed
79 80
{
  char *d = (char *) dest;
81 82 83 84 85 86 87 88 89
  const char *s = (const char *) src;

  if (d < s)
    while (n--)
      *d++ = *s++;
  else
    {
      d += n;
      s += n;
90

91 92 93
      while (n--)
	*--d = *--s;
    }
94

okuji's avatar
okuji committed
95 96
  return dest;
}
97

98
char *
99
grub_strcpy (char *dest, const char *src)
100 101 102 103 104 105 106 107 108
{
  char *p = dest;

  while ((*p++ = *src++) != '\0')
    ;

  return dest;
}

okuji's avatar
okuji committed
109
int
110
grub_printf (const char *fmt, ...)
okuji's avatar
okuji committed
111 112 113
{
  va_list ap;
  int ret;
114

okuji's avatar
okuji committed
115
  va_start (ap, fmt);
116
  ret = grub_vprintf (fmt, ap);
okuji's avatar
okuji committed
117 118 119
  va_end (ap);

  return ret;
120
}
okuji's avatar
okuji committed
121

122 123 124 125 126 127 128 129 130 131 132 133 134
int
grub_printf_ (const char *fmt, ...)
{
  va_list ap;
  int ret;

  va_start (ap, fmt);
  ret = grub_vprintf (_(fmt), ap);
  va_end (ap);

  return ret;
}

135 136 137 138 139 140
int
grub_puts_ (const char *s)
{
  return grub_puts (_(s));
}

141
#if defined (__APPLE__) && ! defined (GRUB_UTIL)
142 143 144 145 146
int
grub_err_printf (const char *fmt, ...)
{
	va_list ap;
	int ret;
147

148 149 150
	va_start (ap, fmt);
	ret = grub_vprintf (fmt, ap);
	va_end (ap);
151

152
	return ret;
153
}
154 155
#endif

156
#if ! defined (__APPLE__) && ! defined (GRUB_UTIL)
157 158 159 160
int grub_err_printf (const char *fmt, ...)
__attribute__ ((alias("grub_printf")));
#endif

161
void
162 163
grub_real_dprintf (const char *file, const int line, const char *condition,
		   const char *fmt, ...)
164 165 166
{
  va_list args;
  const char *debug = grub_env_get ("debug");
167

168 169
  if (! debug)
    return;
170

171 172
  if (grub_strword (debug, "all") || grub_strword (debug, condition))
    {
173
      grub_printf ("%s:%d: ", file, line);
174 175 176
      va_start (args, fmt);
      grub_vprintf (fmt, args);
      va_end (args);
177
      grub_refresh ();
178 179 180
    }
}

181 182
#define PREALLOC_SIZE 255

okuji's avatar
okuji committed
183
int
184
grub_vprintf (const char *fmt, va_list ap)
okuji's avatar
okuji committed
185
{
186 187 188
  grub_size_t s;
  static char buf[PREALLOC_SIZE + 1];
  char *curbuf = buf;
189
  struct printf_args args;
190

191 192 193
  parse_printf_args (fmt, &args, ap);

  s = grub_vsnprintf_real (buf, PREALLOC_SIZE, fmt, &args);
194 195 196 197 198 199 200 201 202 203
  if (s > PREALLOC_SIZE)
    {
      curbuf = grub_malloc (s + 1);
      if (!curbuf)
	{
	  grub_errno = GRUB_ERR_NONE;
	  buf[PREALLOC_SIZE - 3] = '.';
	  buf[PREALLOC_SIZE - 2] = '.';
	  buf[PREALLOC_SIZE - 1] = '.';
	  buf[PREALLOC_SIZE] = 0;
204
	  curbuf = buf;
205 206
	}
      else
207
	s = grub_vsnprintf_real (curbuf, s, fmt, &args);
208 209
    }

210
  free_printf_args (&args);
211

212 213 214 215
  grub_xputs (curbuf);

  if (curbuf != buf)
    grub_free (curbuf);
216

217
  return s;
okuji's avatar
okuji committed
218 219 220
}

int
221
grub_memcmp (const void *s1, const void *s2, grub_size_t n)
okuji's avatar
okuji committed
222
{
223 224
  const grub_uint8_t *t1 = s1;
  const grub_uint8_t *t2 = s2;
225

okuji's avatar
okuji committed
226 227 228 229 230 231 232 233 234 235 236 237 238
  while (n--)
    {
      if (*t1 != *t2)
	return (int) *t1 - (int) *t2;

      t1++;
      t2++;
    }

  return 0;
}

int
239
grub_strcmp (const char *s1, const char *s2)
okuji's avatar
okuji committed
240 241 242 243
{
  while (*s1 && *s2)
    {
      if (*s1 != *s2)
244
	break;
245

okuji's avatar
okuji committed
246 247 248 249
      s1++;
      s2++;
    }

250
  return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t) *s2;
okuji's avatar
okuji committed
251 252
}

253
int
254
grub_strncmp (const char *s1, const char *s2, grub_size_t n)
255
{
256 257
  if (n == 0)
    return 0;
258

259
  while (*s1 && *s2 && --n)
260 261
    {
      if (*s1 != *s2)
262
	break;
263

264 265 266 267
      s1++;
      s2++;
    }

268
  return (int) (grub_uint8_t) *s1 - (int) (grub_uint8_t)  *s2;
269 270
}

okuji's avatar
okuji committed
271
char *
272
grub_strchr (const char *s, int c)
okuji's avatar
okuji committed
273
{
274
  do
okuji's avatar
okuji committed
275 276 277 278
    {
      if (*s == c)
	return (char *) s;
    }
279
  while (*s++);
okuji's avatar
okuji committed
280 281 282 283 284

  return 0;
}

char *
285
grub_strrchr (const char *s, int c)
okuji's avatar
okuji committed
286
{
287
  char *p = NULL;
okuji's avatar
okuji committed
288

289
  do
okuji's avatar
okuji committed
290 291 292 293
    {
      if (*s == c)
	p = (char *) s;
    }
294
  while (*s++);
okuji's avatar
okuji committed
295 296 297 298

  return p;
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
int
grub_strword (const char *haystack, const char *needle)
{
  const char *n_pos = needle;

  while (grub_iswordseparator (*haystack))
    haystack++;

  while (*haystack)
    {
      /* Crawl both the needle and the haystack word we're on.  */
      while(*haystack && !grub_iswordseparator (*haystack)
            && *haystack == *n_pos)
        {
          haystack++;
          n_pos++;
        }

      /* If we reached the end of both words at the same time, the word
      is found. If not, eat everything in the haystack that isn't the
      next word (or the end of string) and "reset" the needle.  */
      if ( (!*haystack || grub_iswordseparator (*haystack))
         && (!*n_pos || grub_iswordseparator (*n_pos)))
        return 1;
      else
        {
          n_pos = needle;
          while (*haystack && !grub_iswordseparator (*haystack))
            haystack++;
          while (grub_iswordseparator (*haystack))
            haystack++;
        }
    }

  return 0;
}

okuji's avatar
okuji committed
336
int
337
grub_isspace (int c)
okuji's avatar
okuji committed
338 339 340 341 342
{
  return (c == '\n' || c == '\r' || c == ' ' || c == '\t');
}

unsigned long
343
grub_strtoul (const char *str, char **end, int base)
okuji's avatar
okuji committed
344
{
345 346 347
  unsigned long long num;

  num = grub_strtoull (str, end, base);
348
#if GRUB_CPU_SIZEOF_LONG != 8
349 350
  if (num > ~0UL)
    {
351
      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
352 353
      return ~0UL;
    }
354
#endif
355 356 357 358 359 360 361 362

  return (unsigned long) num;
}

unsigned long long
grub_strtoull (const char *str, char **end, int base)
{
  unsigned long long num = 0;
okuji's avatar
okuji committed
363
  int found = 0;
364

okuji's avatar
okuji committed
365
  /* Skip white spaces.  */
366 367
  /* grub_isspace checks that *str != '\0'.  */
  while (grub_isspace (*str))
okuji's avatar
okuji committed
368
    str++;
369

okuji's avatar
okuji committed
370 371
  /* Guess the base, if not specified. The prefix `0x' means 16, and
     the prefix `0' means 8.  */
372
  if (str[0] == '0')
okuji's avatar
okuji committed
373 374 375 376 377 378 379 380 381
    {
      if (str[1] == 'x')
	{
	  if (base == 0 || base == 16)
	    {
	      base = 16;
	      str += 2;
	    }
	}
382
      else if (base == 0 && str[1] >= '0' && str[1] <= '7')
okuji's avatar
okuji committed
383 384
	base = 8;
    }
385

okuji's avatar
okuji committed
386 387 388 389 390 391 392
  if (base == 0)
    base = 10;

  while (*str)
    {
      unsigned long digit;

393
      digit = grub_tolower (*str) - '0';
okuji's avatar
okuji committed
394 395 396 397 398 399 400 401
      if (digit > 9)
	{
	  digit += '0' - 'a' + 10;
	  if (digit >= (unsigned long) base)
	    break;
	}

      found = 1;
402 403 404

      /* NUM * BASE + DIGIT > ~0ULL */
      if (num > grub_divmod64 (~0ULL - digit, base, 0))
okuji's avatar
okuji committed
405
	{
406 407
	  grub_error (GRUB_ERR_OUT_OF_RANGE,
		      N_("overflow is detected"));
408
	  return ~0ULL;
okuji's avatar
okuji committed
409 410
	}

411
      num = num * base + digit;
okuji's avatar
okuji committed
412 413 414 415 416
      str++;
    }

  if (! found)
    {
417 418
      grub_error (GRUB_ERR_BAD_NUMBER,
		  N_("unrecognized number"));
okuji's avatar
okuji committed
419 420
      return 0;
    }
421

okuji's avatar
okuji committed
422 423 424 425 426 427 428
  if (end)
    *end = (char *) str;

  return num;
}

char *
429
grub_strdup (const char *s)
okuji's avatar
okuji committed
430
{
431
  grub_size_t len;
okuji's avatar
okuji committed
432
  char *p;
433

434 435
  len = grub_strlen (s) + 1;
  p = (char *) grub_malloc (len);
436 437 438
  if (! p)
    return 0;

439
  return grub_memcpy (p, s, len);
440 441 442
}

char *
443
grub_strndup (const char *s, grub_size_t n)
444
{
445 446
  grub_size_t len;
  char *p;
447

448 449 450 451
  len = grub_strlen (s);
  if (len > n)
    len = n;
  p = (char *) grub_malloc (len + 1);
okuji's avatar
okuji committed
452 453
  if (! p)
    return 0;
454

455 456 457
  grub_memcpy (p, s, len);
  p[len] = '\0';
  return p;
okuji's avatar
okuji committed
458 459
}

460 461 462 463 464 465 466 467 468
/* clang detects that we're implementing here a memset so it decides to
   optimise and calls memset resulting in infinite recursion. With volatile
   we make it not optimise in this way.  */
#ifdef __clang__
#define VOLATILE_CLANG volatile
#else
#define VOLATILE_CLANG
#endif

okuji's avatar
okuji committed
469
void *
470
grub_memset (void *s, int c, grub_size_t len)
okuji's avatar
okuji committed
471
{
472 473
  void *p = s;
  grub_uint8_t pattern8 = c;
okuji's avatar
okuji committed
474

475 476 477 478 479 480 481 482 483 484
  if (len >= 3 * sizeof (unsigned long))
    {
      unsigned long patternl = 0;
      grub_size_t i;

      for (i = 0; i < sizeof (unsigned long); i++)
	patternl |= ((unsigned long) pattern8) << (8 * i);

      while (len > 0 && (((grub_addr_t) p) & (sizeof (unsigned long) - 1)))
	{
485
	  *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
486 487 488 489 490
	  p = (grub_uint8_t *) p + 1;
	  len--;
	}
      while (len >= sizeof (unsigned long))
	{
491
	  *(VOLATILE_CLANG unsigned long *) p = patternl;
492 493 494 495 496 497 498
	  p = (unsigned long *) p + 1;
	  len -= sizeof (unsigned long);
	}
    }

  while (len > 0)
    {
499
      *(VOLATILE_CLANG grub_uint8_t *) p = pattern8;
500 501 502
      p = (grub_uint8_t *) p + 1;
      len--;
    }
okuji's avatar
okuji committed
503 504 505 506

  return s;
}

507 508
grub_size_t
grub_strlen (const char *s)
okuji's avatar
okuji committed
509
{
510
  const char *p = s;
okuji's avatar
okuji committed
511 512 513 514 515 516 517 518

  while (*p)
    p++;

  return p - s;
}

static inline void
519
grub_reverse (char *str)
okuji's avatar
okuji committed
520
{
521
  char *p = str + grub_strlen (str) - 1;
okuji's avatar
okuji committed
522 523 524 525 526 527 528 529 530 531 532 533 534

  while (str < p)
    {
      char tmp;

      tmp = *str;
      *str = *p;
      *p = tmp;
      str++;
      p--;
    }
}

535 536
/* Divide N by D, return the quotient, and store the remainder in *R.  */
grub_uint64_t
537
grub_divmod64 (grub_uint64_t n, grub_uint64_t d, grub_uint64_t *r)
538 539 540
{
  /* This algorithm is typically implemented by hardware. The idea
     is to get the highest bit in N, 64 times, by keeping
541
     upper(N * 2^i) = (Q * D + M), where upper
542 543
     represents the high 64 bits in 128-bits space.  */
  unsigned bits = 64;
544 545
  grub_uint64_t q = 0;
  grub_uint64_t m = 0;
546

547
  /* ARM and IA64 don't have a fast 32-bit division.
548 549
     Using that code would just make us use software division routines, calling
     ourselves indirectly and hence getting infinite recursion.
550
  */
551
#if !GRUB_DIVISION_IN_SOFTWARE
552
  /* Skip the slow computation if 32-bit arithmetic is possible.  */
553
  if (n < 0xffffffff && d < 0xffffffff)
554 555
    {
      if (r)
556
	*r = ((grub_uint32_t) n) % (grub_uint32_t) d;
557

558
      return ((grub_uint32_t) n) / (grub_uint32_t) d;
559
    }
560
#endif
561

562 563 564
  while (bits--)
    {
      m <<= 1;
565

566 567
      if (n & (1ULL << 63))
	m |= 1;
568

569 570
      q <<= 1;
      n <<= 1;
571

572 573 574 575 576 577 578 579 580
      if (m >= d)
	{
	  q |= 1;
	  m -= d;
	}
    }

  if (r)
    *r = m;
581

582 583 584
  return q;
}

585 586
/* Convert a long long value to a string. This function avoids 64-bit
   modular arithmetic or divisions.  */
587
static inline char *
588 589 590 591
grub_lltoa (char *str, int c, unsigned long long n)
{
  unsigned base = (c == 'x') ? 16 : 10;
  char *p;
592

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
  if ((long long) n < 0 && c == 'd')
    {
      n = (unsigned long long) (-((long long) n));
      *str++ = '-';
    }

  p = str;

  if (base == 16)
    do
      {
	unsigned d = (unsigned) (n & 0xf);
	*p++ = (d > 9) ? d + 'a' - 10 : d + '0';
      }
    while (n >>= 4);
  else
    /* BASE == 10 */
    do
      {
612
	grub_uint64_t m;
613

614
	n = grub_divmod64 (n, 10, &m);
615
	*p++ = m + '0';
616 617
      }
    while (n);
618

619 620 621 622 623 624
  *p = 0;

  grub_reverse (str);
  return p;
}

625 626 627
static void
parse_printf_args (const char *fmt0, struct printf_args *args,
		   va_list args_in)
okuji's avatar
okuji committed
628
{
629
  const char *fmt;
okuji's avatar
okuji committed
630
  char c;
631
  grub_size_t n = 0;
632 633 634 635 636 637 638 639

  args->count = 0;

  COMPILE_TIME_ASSERT (sizeof (int) == sizeof (grub_uint32_t));
  COMPILE_TIME_ASSERT (sizeof (int) <= sizeof (long long));
  COMPILE_TIME_ASSERT (sizeof (long) <= sizeof (long long));
  COMPILE_TIME_ASSERT (sizeof (long long) == sizeof (void *)
		       || sizeof (int) == sizeof (void *));
640

641
  fmt = fmt0;
okuji's avatar
okuji committed
642 643 644
  while ((c = *fmt++) != 0)
    {
      if (c != '%')
645 646
	continue;

647
      if (*fmt =='-')
648 649
	fmt++;

650
      while (grub_isdigit (*fmt))
651 652
	fmt++;

653
      if (*fmt == '$')
654 655
	fmt++;

656
      if (*fmt =='-')
657 658
	fmt++;

659
      while (grub_isdigit (*fmt))
660 661
	fmt++;

662
      if (*fmt =='.')
663 664
	fmt++;

665
      while (grub_isdigit (*fmt))
666 667 668 669
	fmt++;

      c = *fmt++;
      if (c == 'l')
670 671 672 673
	c = *fmt++;
      if (c == 'l')
	c = *fmt++;

674 675 676 677 678 679 680 681 682
      switch (c)
	{
	case 'p':
	case 'x':
	case 'u':
	case 'd':
	case 'c':
	case 'C':
	case 's':
683
	  args->count++;
684 685 686 687
	  break;
	}
    }

688 689 690 691 692 693 694 695 696 697 698 699
  if (args->count <= ARRAY_SIZE (args->prealloc))
    args->ptr = args->prealloc;
  else
    {
      args->ptr = grub_malloc (args->count * sizeof (args->ptr[0]));
      if (!args->ptr)
	{
	  grub_errno = GRUB_ERR_NONE;
	  args->ptr = args->prealloc;
	  args->count = ARRAY_SIZE (args->prealloc);
	}
    }
700

701
  grub_memset (args->ptr, 0, args->count * sizeof (args->ptr[0]));
702 703 704 705 706 707 708 709 710 711 712 713 714 715

  fmt = fmt0;
  n = 0;
  while ((c = *fmt++) != 0)
    {
      int longfmt = 0;
      grub_size_t curn;
      const char *p;

      if (c != '%')
	continue;

      curn = n++;

716
      if (*fmt =='-')
717 718
	fmt++;

Colin Watson's avatar
Colin Watson committed
719
      p = fmt;
720

721
      while (grub_isdigit (*fmt))
722 723
	fmt++;

724
      if (*fmt == '$')
725 726 727 728 729
	{
	  curn = grub_strtoull (p, 0, 10) - 1;
	  fmt++;
	}

730
      if (*fmt =='-')
Colin Watson's avatar
Colin Watson committed
731 732
	fmt++;

733
      while (grub_isdigit (*fmt))
Colin Watson's avatar
Colin Watson committed
734 735
	fmt++;

736
      if (*fmt =='.')
Colin Watson's avatar
Colin Watson committed
737 738
	fmt++;

739
      while (grub_isdigit (*fmt))
740 741 742
	fmt++;

      c = *fmt++;
743 744 745 746 747 748
      if (c == '%')
	{
	  n--;
	  continue;
	}

749
      if (c == 'l')
okuji's avatar
okuji committed
750
	{
751 752 753
	  c = *fmt++;
	  longfmt = 1;
	}
754 755 756 757 758 759
      if (c == 'l')
	{
	  c = *fmt++;
	  longfmt = 2;
	}
      if (curn >= args->count)
760 761 762 763 764
	continue;
      switch (c)
	{
	case 'x':
	case 'u':
765 766
	  args->ptr[curn].type = UNSIGNED_INT + longfmt;
	  break;
767
	case 'd':
768
	  args->ptr[curn].type = INT + longfmt;
769 770 771
	  break;
	case 'p':
	case 's':
772 773 774 775
	  if (sizeof (void *) == sizeof (long long))
	    args->ptr[curn].type = UNSIGNED_LONGLONG;
	  else
	    args->ptr[curn].type = UNSIGNED_INT;
776
	  break;
777
	case 'C':
778
	case 'c':
779
	  args->ptr[curn].type = INT;
780 781 782 783
	  break;
	}
    }

784 785
  for (n = 0; n < args->count; n++)
    switch (args->ptr[n].type)
786 787
      {
      case INT:
788
	args->ptr[n].ll = va_arg (args_in, int);
789 790
	break;
      case LONG:
791 792 793 794 795 796 797
	args->ptr[n].ll = va_arg (args_in, long);
	break;
      case UNSIGNED_INT:
	args->ptr[n].ll = va_arg (args_in, unsigned int);
	break;
      case UNSIGNED_LONG:
	args->ptr[n].ll = va_arg (args_in, unsigned long);
798 799
	break;
      case LONGLONG:
800 801
      case UNSIGNED_LONGLONG:
	args->ptr[n].ll = va_arg (args_in, long long);
802 803
	break;
      }
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
}

static inline void __attribute__ ((always_inline))
write_char (char *str, grub_size_t *count, grub_size_t max_len, unsigned char ch)
{
  if (*count < max_len)
    str[*count] = ch;

  (*count)++;
}

static int
grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
		     struct printf_args *args)
{
  char c;
  grub_size_t n = 0;
  grub_size_t count = 0;
  const char *fmt;
823 824

  fmt = fmt0;
825

826 827 828 829 830
  while ((c = *fmt++) != 0)
    {
      unsigned int format1 = 0;
      unsigned int format2 = ~ 0U;
      char zerofill = ' ';
831
      char rightfill = 0;
832
      grub_size_t curn;
833

834 835
      if (c != '%')
	{
836
	  write_char (str, &count, max_len,c);
837 838 839 840 841 842 843
	  continue;
	}

      curn = n++;

    rescan:;

844
      if (*fmt =='-')
845 846 847 848 849 850
	{
	  rightfill = 1;
	  fmt++;
	}

      /* Read formatting parameters.  */
851
      if (grub_isdigit (*fmt))
852
	{
853
	  if (fmt[0] == '0')
854
	    zerofill = '0';
855
	  format1 = grub_strtoul (fmt, (char **) &fmt, 10);
856 857
	}

858 859 860 861 862
      if (*fmt == '.')
	fmt++;

      if (grub_isdigit (*fmt))
	format2 = grub_strtoul (fmt, (char **) &fmt, 10);
863

864 865 866 867 868 869 870 871 872 873 874
      if (*fmt == '$')
	{
	  curn = format1 - 1;
	  fmt++;
	  format1 = 0;
	  format2 = ~ 0U;
	  zerofill = ' ';
	  rightfill = 0;

	  goto rescan;
	}
875

876 877
      c = *fmt++;
      if (c == 'l')
878 879 880
	c = *fmt++;
      if (c == 'l')
	c = *fmt++;
881

882 883
      if (c == '%')
	{
884
	  write_char (str, &count, max_len,c);
885
	  n--;
886 887 888
	  continue;
	}

889
      if (curn >= args->count)
890 891
	continue;

892 893
      long long curarg = args->ptr[curn].ll;

894 895 896
      switch (c)
	{
	case 'p':
897 898
	  write_char (str, &count, max_len, '0');
	  write_char (str, &count, max_len, 'x');
899 900 901 902 903
	  c = 'x';
	  /* Fall through. */
	case 'x':
	case 'u':
	case 'd':
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	  {
	    char tmp[32];
	    const char *p = tmp;
	    grub_size_t len;
	    grub_size_t fill;

	    len = grub_lltoa (tmp, c, curarg) - tmp;
	    fill = len < format1 ? format1 - len : 0;
	    if (! rightfill)
	      while (fill--)
		write_char (str, &count, max_len, zerofill);
	    while (*p)
	      write_char (str, &count, max_len, *p++);
	    if (rightfill)
	      while (fill--)
		write_char (str, &count, max_len, zerofill);
	  }
921 922 923
	  break;

	case 'c':
924
	  write_char (str, &count, max_len,curarg & 0xff);
925 926 927 928
	  break;

	case 'C':
	  {
929
	    grub_uint32_t code = curarg;
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
	    int shift;
	    unsigned mask;

	    if (code <= 0x7f)
	      {
		shift = 0;
		mask = 0;
	      }
	    else if (code <= 0x7ff)
	      {
		shift = 6;
		mask = 0xc0;
	      }
	    else if (code <= 0xffff)
	      {
		shift = 12;
		mask = 0xe0;
	      }
948
	    else if (code <= 0x10ffff)
949 950 951 952 953
	      {
		shift = 18;
		mask = 0xf0;
	      }
	    else
954
	      {
955 956 957
		code = '?';
		shift = 0;
		mask = 0;
958
	      }
959

960
	    write_char (str, &count, max_len,mask | (code >> shift));
961 962

	    for (shift -= 6; shift >= 0; shift -= 6)
963
	      write_char (str, &count, max_len,0x80 | (0x3f & (code >> shift)));
964 965 966 967
	  }
	  break;

	case 's':
968 969
	  {
	    grub_size_t len = 0;
970 971 972
	    grub_size_t fill;
	    const char *p = ((char *) (grub_addr_t) curarg) ? : "(null)";
	    grub_size_t i;
973

974 975
	    while (len < format2 && p[len])
	      len++;
976

977 978 979 980 981
	    fill = len < format1 ? format1 - len : 0;

	    if (!rightfill)
	      while (fill--)
		write_char (str, &count, max_len, zerofill);
982

983
	    for (i = 0; i < len; i++)
984
	      write_char (str, &count, max_len,*p++);
985

986 987 988
	    if (rightfill)
	      while (fill--)
		write_char (str, &count, max_len, zerofill);
989
	  }
990 991 992 993

	  break;

	default:
994
	  write_char (str, &count, max_len,c);
995
	  break;
okuji's avatar
okuji committed
996 997 998
	}
    }

999 1000 1001 1002
  if (count < max_len)
    str[count] = '\0';
  else
    str[max_len] = '\0';
okuji's avatar
okuji committed
1003 1004 1005 1006
  return count;
}

int
1007 1008 1009
grub_vsnprintf (char *str, grub_size_t n, const char *fmt, va_list ap)
{
  grub_size_t ret;
1010
  struct printf_args args;
1011 1012 1013 1014 1015 1016

  if (!n)
    return 0;

  n--;

1017 1018 1019 1020 1021
  parse_printf_args (fmt, &args, ap);

  ret = grub_vsnprintf_real (str, n, fmt, &args);

  free_printf_args (&args);
1022 1023 1024 1025 1026 1027

  return ret < n ? ret : n;
}

int
grub_snprintf (char *str, grub_size_t n, const char *fmt, ...)
okuji's avatar
okuji committed
1028 1029 1030
{
  va_list ap;
  int ret;
1031

okuji's avatar
okuji committed
1032
  va_start (ap, fmt);
1033 1034 1035 1036 1037 1038 1039
  ret = grub_vsnprintf (str, n, fmt, ap);
  va_end (ap);

  return ret;
}

char *
1040
grub_xvasprintf (const char *fmt, va_list ap)
1041 1042 1043
{
  grub_size_t s, as = PREALLOC_SIZE;
  char *ret;
1044 1045 1046
  struct printf_args args;

  parse_printf_args (fmt, &args, ap);
1047 1048 1049 1050 1051

  while (1)
    {
      ret = grub_malloc (as + 1);
      if (!ret)
1052 1053 1054 1055
	{
	  free_printf_args (&args);
	  return NULL;
	}
1056

1057
      s = grub_vsnprintf_real (ret, as, fmt, &args);
1058

1059
      if (s <= as)
1060 1061 1062 1063
	{
	  free_printf_args (&args);
	  return ret;
	}
1064 1065 1066 1067 1068 1069 1070

      grub_free (ret);
      as = s;
    }
}

char *
1071
grub_xasprintf (const char *fmt, ...)
1072 1073 1074 1075 1076
{
  va_list ap;
  char *ret;

  va_start (ap, fmt);
1077
  ret = grub_xvasprintf (fmt, ap);
okuji's avatar
okuji committed
1078 1079 1080 1081
  va_end (ap);

  return ret;
}
1082

1083
/* Abort GRUB. This function does not return.  */
1084
static void __attribute__ ((noreturn))
1085 1086
grub_abort (void)
{
1087 1088
  grub_printf ("\nAborted.");
  
1089 1090 1091
#ifndef GRUB_UTIL
  if (grub_term_inputs)
#endif
1092
    {
1093 1094
      grub_printf (" Press any key to exit.");
      grub_getkey ();
1095
    }
1096

1097 1098
  grub_exit ();
}
1099

1100 1101 1102 1103 1104 1105 1106 1107 1108
void
grub_fatal (const char *fmt, ...)
{
  va_list ap;

  va_start (ap, fmt);
  grub_vprintf (_(fmt), ap);
  va_end (ap);

1109 1110
  grub_refresh ();

1111 1112
  grub_abort ();
}
1113

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
#if BOOT_TIME_STATS

#include <grub/time.h>

struct grub_boot_time *grub_boot_time_head;
static struct grub_boot_time **boot_time_last = &grub_boot_time_head;

void
grub_real_boot_time (const char *file,
		     const int line,
		     const char *fmt, ...)
{
  struct grub_boot_time *n;
  va_list args;

  grub_error_push ();
  n = grub_malloc (sizeof (*n));
  if (!n)
    {
      grub_errno = 0;
      grub_error_pop ();
      return;
    }
  n->file = file;
  n->line = line;
  n->tp = grub_get_time_ms ();
  n->next = 0;

  va_start (args, fmt);
  n->msg = grub_xvasprintf (fmt, args);    
  va_end (args);

  *boot_time_last = n;
  boot_time_last = &n->next;

  grub_errno = 0;
  grub_error_pop ();
}
#endif