getfields 1.7 KB

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