tokenize.c 1.7 KB

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