getfields 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. .TH GETFIELDS 2
  2. .SH NAME
  3. getfields, gettokens, tokenize \- break a string into fields
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .ta \w'\fLchar* \fP'u
  10. .B
  11. int getfields(char *str, char **args, int maxargs, int multiflag,
  12. .br
  13. .B
  14. char *delims)
  15. .PP
  16. .B
  17. int gettokens(char *str, char **args, int maxargs, char *delims)
  18. .PP
  19. .B
  20. int tokenize(char *str, char **args, int maxargs)
  21. .SH DESCRIPTION
  22. .I Getfields
  23. places into the array
  24. .I args
  25. pointers to the first
  26. .I maxargs
  27. fields of the null terminated
  28. .SM UTF
  29. string
  30. .IR str .
  31. Delimiters between these fields are set to
  32. .SM NUL
  33. bytes.
  34. .PP
  35. Fields are substrings of
  36. .I str
  37. whose definition depends on the value of
  38. .IR multiflag.
  39. If
  40. .I multiflag
  41. is zero,
  42. adjacent fields are separated by exactly one delimiter.
  43. For example
  44. .EX
  45. getfields("#alice#bob##charles###", arg, 3, 0, "#");
  46. .EE
  47. yields three substrings:
  48. null-string ,
  49. .BR "alice" ,
  50. and
  51. .BR "bob##charles###" .
  52. If the
  53. .I multiflag
  54. argument is not zero,
  55. a field is a non-empty string of non-delimiters.
  56. For example
  57. .EX
  58. getfields("#alice#bob##charles###", arg, 3, 1, "#");
  59. .EE
  60. yields the three substrings:
  61. .BR "alice" ,
  62. .BR "bob" ,
  63. and
  64. .BR "charles###" .
  65. .PP
  66. Getfields returns the number of fields pointed to.
  67. .PP
  68. .I Gettokens
  69. is the same as
  70. .I getfields
  71. with
  72. .I multiflag
  73. non-zero,
  74. except that fields may be quoted using single quotes, in the manner
  75. of
  76. .IR rc (1).
  77. Any such quotes remain in the resulting
  78. .IR args .
  79. See
  80. .IR quote (2)
  81. for related quote-handling software.
  82. .PP
  83. .I Tokenize
  84. is similar to
  85. .I gettokens
  86. with
  87. .I delims
  88. set to \f5"\et\er\en\ "\fP,
  89. except that quotes are interpreted but do not appear in the resulting
  90. .IR args .
  91. .SH SOURCE
  92. .B /sys/src/libc/port/tokenize.c
  93. .SH SEE ALSO
  94. .I strtok
  95. in
  96. .IR strcat (2),
  97. .IR quote (2).