argv.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /* Return nearest power of two that is >= v. */
  23. static unsigned
  24. round_up_exp (unsigned v)
  25. {
  26. COMPILE_TIME_ASSERT (sizeof (v) == 4);
  27. v--;
  28. v |= v >> 1;
  29. v |= v >> 2;
  30. v |= v >> 4;
  31. v |= v >> 8;
  32. v |= v >> 16;
  33. v++;
  34. v += (v == 0);
  35. return v;
  36. }
  37. void
  38. grub_script_argv_free (struct grub_script_argv *argv)
  39. {
  40. unsigned i;
  41. if (argv->args)
  42. {
  43. for (i = 0; i < argv->argc; i++)
  44. grub_free (argv->args[i]);
  45. grub_free (argv->args);
  46. }
  47. argv->argc = 0;
  48. argv->args = 0;
  49. argv->script = 0;
  50. }
  51. /* Make argv from argc, args pair. */
  52. int
  53. grub_script_argv_make (struct grub_script_argv *argv, int argc, char **args)
  54. {
  55. int i;
  56. struct grub_script_argv r = { 0, 0, 0 };
  57. for (i = 0; i < argc; i++)
  58. if (grub_script_argv_next (&r)
  59. || grub_script_argv_append (&r, args[i], grub_strlen (args[i])))
  60. {
  61. grub_script_argv_free (&r);
  62. return 1;
  63. }
  64. *argv = r;
  65. return 0;
  66. }
  67. /* Prepare for next argc. */
  68. int
  69. grub_script_argv_next (struct grub_script_argv *argv)
  70. {
  71. char **p = argv->args;
  72. if (argv->args && argv->argc && argv->args[argv->argc - 1] == 0)
  73. return 0;
  74. p = grub_realloc (p, round_up_exp ((argv->argc + 2) * sizeof (char *)));
  75. if (! p)
  76. return 1;
  77. argv->argc++;
  78. argv->args = p;
  79. if (argv->argc == 1)
  80. argv->args[0] = 0;
  81. argv->args[argv->argc] = 0;
  82. return 0;
  83. }
  84. /* Append `s' to the last argument. */
  85. int
  86. grub_script_argv_append (struct grub_script_argv *argv, const char *s,
  87. grub_size_t slen)
  88. {
  89. grub_size_t a;
  90. char *p = argv->args[argv->argc - 1];
  91. if (! s)
  92. return 0;
  93. a = p ? grub_strlen (p) : 0;
  94. p = grub_realloc (p, round_up_exp ((a + slen + 1) * sizeof (char)));
  95. if (! p)
  96. return 1;
  97. grub_memcpy (p + a, s, slen);
  98. p[a+slen] = 0;
  99. argv->args[argv->argc - 1] = p;
  100. return 0;
  101. }
  102. /* Split `s' and append words as multiple arguments. */
  103. int
  104. grub_script_argv_split_append (struct grub_script_argv *argv, const char *s)
  105. {
  106. const char *p;
  107. int errors = 0;
  108. if (! s)
  109. return 0;
  110. while (*s && grub_isspace (*s))
  111. s++;
  112. while (! errors && *s)
  113. {
  114. p = s;
  115. while (*s && ! grub_isspace (*s))
  116. s++;
  117. errors += grub_script_argv_append (argv, p, s - p);
  118. while (*s && grub_isspace (*s))
  119. s++;
  120. if (*s)
  121. errors += grub_script_argv_next (argv);
  122. }
  123. return errors;
  124. }