argify.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (C) 2001-2003 Hewlett-Packard Co.
  3. * Contributed by Stephane Eranian <eranian@hpl.hp.com>
  4. *
  5. * Copyright (C) 2001 Silicon Graphics, Inc.
  6. * Contributed by Brent Casavant <bcasavan@sgi.com>
  7. *
  8. * Copyright (C) 2006-2009 Intel Corporation
  9. * Contributed by Fenghua Yu <fenghua.yu@intel.com>
  10. * Contributed by Bibo Mao <bibo.mao@intel.com>
  11. * Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
  12. *
  13. * This file is part of the ELILO, the EFI Linux boot loader.
  14. *
  15. * ELILO is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2, or (at your option)
  18. * any later version.
  19. *
  20. * ELILO is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with ELILO; see the file COPYING. If not, write to the Free
  27. * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  28. * 02111-1307, USA.
  29. *
  30. * Please check out the elilo.txt for complete documentation on how
  31. * to use this program.
  32. */
  33. #include <efi.h>
  34. #include <efilib.h>
  35. #include <argify.h>
  36. #define MAX_ARGS 256
  37. #define CHAR_SPACE L' '
  38. #define DEBUG 0
  39. INTN
  40. argify(CHAR16 *buf, UINTN len, CHAR16 **argv)
  41. {
  42. UINTN i=0, j=0;
  43. CHAR16 *p = buf;
  44. if (buf == 0) {
  45. argv[0] = NULL;
  46. return 0;
  47. }
  48. /* len represents the number of bytes, not the number of 16 bytes chars */
  49. len = len >> 1;
  50. /*
  51. * Here we use CHAR_NULL as the terminator rather than the length
  52. * because it seems like the EFI shell return rather bogus values for it.
  53. * Apparently, we are guaranteed to find the '\0' character in the buffer
  54. * where the real input arguments stop, so we use it instead.
  55. */
  56. for(;;) {
  57. while (buf[i] == CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
  58. if (buf[i] == CHAR_NULL || i == len) goto end;
  59. p = buf+i;
  60. i++;
  61. while (buf[i] != CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
  62. argv[j++] = p;
  63. if (buf[i] == CHAR_NULL) goto end;
  64. buf[i] = CHAR_NULL;
  65. if (i == len) goto end;
  66. i++;
  67. if (j == MAX_ARGS-1) {
  68. Print(L"too many arguments (%d) truncating\n", j);
  69. goto end;
  70. }
  71. }
  72. end:
  73. argv[j] = NULL;
  74. return j;
  75. }