argv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* argv.c - methods for constructing argument vector */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  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
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/misc.h>
  21. #include <grub/script_sh.h>
  22. #include <grub/safemath.h>
  23. /* Return nearest power of two that is >= v. */
  24. static unsigned
  25. round_up_exp (unsigned v)
  26. {
  27. COMPILE_TIME_ASSERT (sizeof (v) == 4);
  28. v--;
  29. v |= v >> 1;
  30. v |= v >> 2;
  31. v |= v >> 4;
  32. v |= v >> 8;
  33. v |= v >> 16;
  34. v++;
  35. v += (v == 0);
  36. return v;
  37. }
  38. void
  39. grub_script_argv_free (struct grub_script_argv *argv)
  40. {
  41. unsigned i;
  42. if (argv->args)
  43. {
  44. for (i = 0; i < argv->argc; i++)
  45. grub_free (argv->args[i]);
  46. grub_free (argv->args);
  47. }
  48. argv->argc = 0;
  49. argv->args = 0;
  50. argv->script = 0;
  51. }
  52. /* Make argv from argc, args pair. */
  53. int
  54. grub_script_argv_make (struct grub_script_argv *argv, int argc, char **args)
  55. {
  56. int i;
  57. struct grub_script_argv r = { 0, 0, 0 };
  58. for (i = 0; i < argc; i++)
  59. if (grub_script_argv_next (&r)
  60. || grub_script_argv_append (&r, args[i], grub_strlen (args[i])))
  61. {
  62. grub_script_argv_free (&r);
  63. return 1;
  64. }
  65. *argv = r;
  66. return 0;
  67. }
  68. /* Prepare for next argc. */
  69. int
  70. grub_script_argv_next (struct grub_script_argv *argv)
  71. {
  72. char **p = argv->args;
  73. grub_size_t sz;
  74. if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0)
  75. return 0;
  76. if (grub_add (argv->argc, 2, &sz) ||
  77. grub_mul (sz, sizeof (char *), &sz))
  78. return 1;
  79. p = grub_realloc (p, round_up_exp (sz));
  80. if (! p)
  81. return 1;
  82. argv->argc++;
  83. argv->args = p;
  84. if (argv->argc == 1)
  85. argv->args[0] = 0;
  86. argv->args[argv->argc] = 0;
  87. return 0;
  88. }
  89. /* Append `s' to the last argument. */
  90. int
  91. grub_script_argv_append (struct grub_script_argv *argv, const char *s,
  92. grub_size_t slen)
  93. {
  94. grub_size_t a;
  95. char *p = argv->args[argv->argc - 1];
  96. grub_size_t sz;
  97. if (! s)
  98. return 0;
  99. a = p ? grub_strlen (p) : 0;
  100. if (grub_add (a, slen, &sz) ||
  101. grub_add (sz, 1, &sz) ||
  102. grub_mul (sz, sizeof (char), &sz))
  103. return 1;
  104. p = grub_realloc (p, round_up_exp (sz));
  105. if (! p)
  106. return 1;
  107. grub_memcpy (p + a, s, slen);
  108. p[a+slen] = 0;
  109. argv->args[argv->argc - 1] = p;
  110. return 0;
  111. }
  112. /* Split `s' and append words as multiple arguments. */
  113. int
  114. grub_script_argv_split_append (struct grub_script_argv *argv, const char *s)
  115. {
  116. const char *p;
  117. int errors = 0;
  118. if (! s)
  119. return 0;
  120. while (*s && grub_isspace (*s))
  121. s++;
  122. while (! errors && *s)
  123. {
  124. p = s;
  125. while (*s && ! grub_isspace (*s))
  126. s++;
  127. errors += grub_script_argv_append (argv, p, s - p);
  128. while (*s && grub_isspace (*s))
  129. s++;
  130. if (*s)
  131. errors += grub_script_argv_next (argv);
  132. }
  133. return errors;
  134. }