argv_nextword.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * argv_nextword.c
  3. */
  4. #include "private.h"
  5. #include "lub/types.h"
  6. #include "lub/ctype.h"
  7. #include <stddef.h>
  8. /*--------------------------------------------------------- */
  9. const char *
  10. lub_argv_nextword(const char *string,
  11. size_t *len,
  12. size_t *offset,
  13. bool_t *quoted)
  14. {
  15. const char *word;
  16. bool_t quote = BOOL_FALSE;
  17. *quoted = BOOL_FALSE;
  18. /* find the start of a word (not including an opening quote) */
  19. while(*string && lub_ctype_isspace(*string))
  20. {
  21. string++;
  22. (*offset)++;
  23. }
  24. if(*string == '\\')
  25. {
  26. string++;
  27. if (*string)
  28. string++;
  29. }
  30. /* is this the start of a quoted string ? */
  31. if(*string == '"')
  32. {
  33. quote = BOOL_TRUE;
  34. string++;
  35. }
  36. word = string;
  37. *len = 0;
  38. /* find the end of the word */
  39. while(*string)
  40. {
  41. if(*string == '\\')
  42. {
  43. string++;
  44. (*len)++;
  45. if (*string) {
  46. (*len)++;
  47. string++;
  48. }
  49. continue;
  50. }
  51. if((BOOL_FALSE == quote) && lub_ctype_isspace(*string))
  52. {
  53. /* end of word */
  54. break;
  55. }
  56. if(*string == '"')
  57. {
  58. /* end of a quoted string */
  59. *quoted = BOOL_TRUE;
  60. break;
  61. }
  62. (*len)++;
  63. string++;
  64. }
  65. return word;
  66. }
  67. /*--------------------------------------------------------- */