quote.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <u.h>
  2. #include <libc.h>
  3. int (*doquote)(int);
  4. extern int _needsquotes(char*, int*);
  5. extern int _runeneedsquotes(Rune*, int*);
  6. char*
  7. unquotestrdup(char *s)
  8. {
  9. char *t, *ret;
  10. int quoting;
  11. ret = s = strdup(s); /* return unquoted copy */
  12. if(ret == nil)
  13. return ret;
  14. quoting = 0;
  15. t = s; /* s is output string, t is input string */
  16. while(*t!='\0' && (quoting || (*t!=' ' && *t!='\t'))){
  17. if(*t != '\''){
  18. *s++ = *t++;
  19. continue;
  20. }
  21. /* *t is a quote */
  22. if(!quoting){
  23. quoting = 1;
  24. t++;
  25. continue;
  26. }
  27. /* quoting and we're on a quote */
  28. if(t[1] != '\''){
  29. /* end of quoted section; absorb closing quote */
  30. t++;
  31. quoting = 0;
  32. continue;
  33. }
  34. /* doubled quote; fold one quote into two */
  35. t++;
  36. *s++ = *t++;
  37. }
  38. if(t != s)
  39. memmove(s, t, strlen(t)+1);
  40. return ret;
  41. }
  42. Rune*
  43. unquoterunestrdup(Rune *s)
  44. {
  45. Rune *t, *ret;
  46. int quoting;
  47. ret = s = runestrdup(s); /* return unquoted copy */
  48. if(ret == nil)
  49. return ret;
  50. quoting = 0;
  51. t = s; /* s is output string, t is input string */
  52. while(*t!='\0' && (quoting || (*t!=' ' && *t!='\t'))){
  53. if(*t != '\''){
  54. *s++ = *t++;
  55. continue;
  56. }
  57. /* *t is a quote */
  58. if(!quoting){
  59. quoting = 1;
  60. t++;
  61. continue;
  62. }
  63. /* quoting and we're on a quote */
  64. if(t[1] != '\''){
  65. /* end of quoted section; absorb closing quote */
  66. t++;
  67. quoting = 0;
  68. continue;
  69. }
  70. /* doubled quote; fold one quote into two */
  71. t++;
  72. *s++ = *t++;
  73. }
  74. if(t != s)
  75. memmove(s, t, (runestrlen(t)+1)*sizeof(Rune));
  76. return ret;
  77. }
  78. char*
  79. quotestrdup(char *s)
  80. {
  81. char *t, *u, *ret;
  82. int quotelen;
  83. Rune r;
  84. if(_needsquotes(s, &quotelen) == 0)
  85. return strdup(s);
  86. ret = malloc(quotelen+1);
  87. if(ret == nil)
  88. return nil;
  89. u = ret;
  90. *u++ = '\'';
  91. for(t=s; *t; t++){
  92. r = *t;
  93. if(r == L'\'')
  94. *u++ = r; /* double the quote */
  95. *u++ = r;
  96. }
  97. *u++ = '\'';
  98. *u = '\0';
  99. return ret;
  100. }
  101. Rune*
  102. quoterunestrdup(Rune *s)
  103. {
  104. Rune *t, *u, *ret;
  105. int quotelen;
  106. Rune r;
  107. if(_runeneedsquotes(s, &quotelen) == 0)
  108. return runestrdup(s);
  109. ret = malloc((quotelen+1)*sizeof(Rune));
  110. if(ret == nil)
  111. return nil;
  112. u = ret;
  113. *u++ = '\'';
  114. for(t=s; *t; t++){
  115. r = *t;
  116. if(r == L'\'')
  117. *u++ = r; /* double the quote */
  118. *u++ = r;
  119. }
  120. *u++ = '\'';
  121. *u = '\0';
  122. return ret;
  123. }