tokenize.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "lib9.h"
  2. static char qsep[] = " \t\r\n";
  3. static char*
  4. qtoken(char *s, char *sep)
  5. {
  6. int quoting;
  7. char *t;
  8. quoting = 0;
  9. t = s; /* s is output string, t is input string */
  10. while(*t!='\0' && (quoting || utfrune(sep, *t)==nil)){
  11. if(*t != '\''){
  12. *s++ = *t++;
  13. continue;
  14. }
  15. /* *t is a quote */
  16. if(!quoting){
  17. quoting = 1;
  18. t++;
  19. continue;
  20. }
  21. /* quoting and we're on a quote */
  22. if(t[1] != '\''){
  23. /* end of quoted section; absorb closing quote */
  24. t++;
  25. quoting = 0;
  26. continue;
  27. }
  28. /* doubled quote; fold one quote into two */
  29. t++;
  30. *s++ = *t++;
  31. }
  32. if(*s != '\0'){
  33. *s = '\0';
  34. if(t == s)
  35. t++;
  36. }
  37. return t;
  38. }
  39. static char*
  40. etoken(char *t, char *sep)
  41. {
  42. int quoting;
  43. /* move to end of next token */
  44. quoting = 0;
  45. while(*t!='\0' && (quoting || utfrune(sep, *t)==nil)){
  46. if(*t != '\''){
  47. t++;
  48. continue;
  49. }
  50. /* *t is a quote */
  51. if(!quoting){
  52. quoting = 1;
  53. t++;
  54. continue;
  55. }
  56. /* quoting and we're on a quote */
  57. if(t[1] != '\''){
  58. /* end of quoted section; absorb closing quote */
  59. t++;
  60. quoting = 0;
  61. continue;
  62. }
  63. /* doubled quote; fold one quote into two */
  64. t += 2;
  65. }
  66. return t;
  67. }
  68. int
  69. gettokens(char *s, char **args, int maxargs, char *sep)
  70. {
  71. int nargs;
  72. for(nargs=0; nargs<maxargs; nargs++){
  73. while(*s!='\0' && utfrune(sep, *s)!=nil)
  74. *s++ = '\0';
  75. if(*s == '\0')
  76. break;
  77. args[nargs] = s;
  78. s = etoken(s, sep);
  79. }
  80. return nargs;
  81. }
  82. int
  83. tokenize(char *s, char **args, int maxargs)
  84. {
  85. int nargs;
  86. for(nargs=0; nargs<maxargs; nargs++){
  87. while(*s!='\0' && utfrune(qsep, *s)!=nil)
  88. s++;
  89. if(*s == '\0')
  90. break;
  91. args[nargs] = s;
  92. s = qtoken(s, qsep);
  93. }
  94. return nargs;
  95. }