argv_nextword.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /* is this the start of a quoted string ? */
  25. if(*string == '"')
  26. {
  27. quote = BOOL_TRUE;
  28. string++;
  29. }
  30. word = string;
  31. *len = 0;
  32. /* find the end of the word */
  33. while(*string)
  34. {
  35. if((BOOL_FALSE == quote) && lub_ctype_isspace(*string))
  36. {
  37. /* end of word */
  38. break;
  39. }
  40. if(*string == '"')
  41. {
  42. /* end of a quoted string */
  43. *quoted = BOOL_TRUE;
  44. break;
  45. }
  46. (*len)++;
  47. string++;
  48. }
  49. return word;
  50. }
  51. /*--------------------------------------------------------- */