argv_nextword.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 *lub_argv_nextword(const char *string,
  10. size_t * len, size_t * offset, bool_t * quoted)
  11. {
  12. const char *word;
  13. bool_t quote = BOOL_FALSE;
  14. *quoted = BOOL_FALSE;
  15. /* find the start of a word (not including an opening quote) */
  16. while (*string && lub_ctype_isspace(*string)) {
  17. string++;
  18. (*offset)++;
  19. }
  20. if (*string == '\\') {
  21. string++;
  22. if (*string)
  23. string++;
  24. }
  25. /* is this the start of a quoted string ? */
  26. if (*string == '"') {
  27. quote = BOOL_TRUE;
  28. string++;
  29. }
  30. word = string;
  31. *len = 0;
  32. /* find the end of the word */
  33. while (*string) {
  34. if (*string == '\\') {
  35. string++;
  36. (*len)++;
  37. if (*string) {
  38. (*len)++;
  39. string++;
  40. }
  41. continue;
  42. }
  43. if ((BOOL_FALSE == quote) && lub_ctype_isspace(*string)) {
  44. /* end of word */
  45. break;
  46. }
  47. if (*string == '"') {
  48. /* end of a quoted string */
  49. *quoted = BOOL_TRUE;
  50. break;
  51. }
  52. (*len)++;
  53. string++;
  54. }
  55. return word;
  56. }
  57. /*--------------------------------------------------------- */