fmtquote.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * The authors of this software are Rob Pike and Ken Thompson.
  3. * Copyright (c) 2002 by Lucent Technologies.
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose without fee is hereby granted, provided that this entire notice
  6. * is included in all copies of any software which is or includes a copy
  7. * or modification of this software and in all copies of the supporting
  8. * documentation for such software.
  9. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  10. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
  11. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <u.h>
  15. #include <libc.h>
  16. #include "fmt.h"
  17. #include "fmtdef.h"
  18. extern int (*doquote)(int);
  19. /*
  20. * How many bytes of output UTF will be produced by quoting (if necessary) this string?
  21. * How many runes? How much of the input will be consumed?
  22. * The parameter q is filled in by _quotesetup.
  23. * The string may be UTF or Runes (s or r).
  24. * Return count does not include NUL.
  25. * Terminate the scan at the first of:
  26. * NUL in input
  27. * count exceeded in input
  28. * count exceeded on output
  29. * *ninp is set to number of input bytes accepted.
  30. * nin may be <0 initially, to avoid checking input by count.
  31. */
  32. void
  33. __quotesetup(char *s, int nin, int nout, Quoteinfo *q, int sharp)
  34. {
  35. int c;
  36. q->quoted = 0;
  37. q->nbytesout = 0;
  38. q->nrunesout = 0;
  39. q->nbytesin = 0;
  40. q->nrunesin = 0;
  41. if(sharp || nin==0 || *s=='\0'){
  42. if(nout < 2)
  43. return;
  44. q->quoted = 1;
  45. q->nbytesout = 2;
  46. q->nrunesout = 2;
  47. }
  48. for(; nin!=0; nin-=1){
  49. c = *s;
  50. if(c == '\0')
  51. break;
  52. if(q->nrunesout+1 > nout)
  53. break;
  54. if((c <= L' ') || (c == L'\'') || (doquote!=nil && doquote(c))){
  55. if(!q->quoted){
  56. if(1+q->nrunesout+1+1 > nout) /* no room for quotes */
  57. break;
  58. q->nrunesout += 2; /* include quotes */
  59. q->nbytesout += 2; /* include quotes */
  60. q->quoted = 1;
  61. }
  62. if(c == '\'') {
  63. q->nbytesout++;
  64. q->nrunesout++; /* quotes reproduce as two characters */
  65. }
  66. }
  67. /* advance input */
  68. s++;
  69. q->nbytesin++;
  70. q->nrunesin++;
  71. /* advance output */
  72. q->nbytesout++;
  73. q->nrunesout++;
  74. }
  75. }
  76. static int
  77. qstrfmt(char *sin, Quoteinfo *q, Fmt *f)
  78. {
  79. int r;
  80. char *t, *s, *m, *me;
  81. ulong fl;
  82. int nc, w;
  83. m = sin;
  84. me = m + q->nbytesin;
  85. w = f->width;
  86. fl = f->flags;
  87. if(!(fl & FmtLeft) && __fmtpad(f, w - q->nbytesout) < 0)
  88. return -1;
  89. t = f->to;
  90. s = f->stop;
  91. FMTCHAR(f, t, s, '\'');
  92. for(nc = q->nrunesin; nc > 0; nc--){
  93. r = *(uchar*)m++;
  94. FMTCHAR(f, t, s, r);
  95. if(r == '\'')
  96. FMTCHAR(f, t, s, r);
  97. }
  98. FMTCHAR(f, t, s, '\'');
  99. f->nfmt += t - (char *)f->to;
  100. f->to = t;
  101. if(fl & FmtLeft && __fmtpad(f, w - q->nbytesout) < 0)
  102. return -1;
  103. return 0;
  104. }
  105. int
  106. __quotestrfmt(int runesin, Fmt *f)
  107. {
  108. int outlen;
  109. char *s;
  110. Quoteinfo q;
  111. f->flags &= ~FmtPrec; /* ignored for %q %Q, so disable for %s %S in easy case */
  112. s = va_arg(f->args, char *);
  113. if(!s)
  114. return __fmtcpy(f, "<nil>", 5, 5);
  115. if(f->flush)
  116. outlen = 0x7FFFFFFF; /* if we can flush, no output limit */
  117. else
  118. outlen = (char*)f->stop - (char*)f->to;
  119. __quotesetup(s, -1, outlen, &q, f->flags&FmtSharp);
  120. if(!q.quoted)
  121. return __fmtcpy(f, s, q.nrunesin, q.nbytesin);
  122. return qstrfmt(s, &q, f);
  123. }
  124. int
  125. quotestrfmt(Fmt *f)
  126. {
  127. return __quotestrfmt(0, f);
  128. }
  129. void
  130. quotefmtinstall(void)
  131. {
  132. fmtinstall('q', quotestrfmt);
  133. }
  134. int
  135. __needsquotes(char *s, int *quotelenp)
  136. {
  137. Quoteinfo q;
  138. __quotesetup(s, -1, 0x7FFFFFFF, &q, 0);
  139. *quotelenp = q.nbytesout;
  140. return q.quoted;
  141. }