fmt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "fmtdef.h"
  4. enum
  5. {
  6. Maxfmt = 64
  7. };
  8. typedef struct Convfmt Convfmt;
  9. struct Convfmt
  10. {
  11. int c;
  12. volatile Fmts fmt; /* for spin lock in fmtfmt; avoids race due to write order */
  13. };
  14. struct
  15. {
  16. /* lock by calling __fmtlock, __fmtunlock */
  17. int nfmt;
  18. Convfmt fmt[Maxfmt];
  19. } fmtalloc;
  20. static Convfmt knownfmt[] = {
  21. ' ', __flagfmt,
  22. '#', __flagfmt,
  23. '%', __percentfmt,
  24. '+', __flagfmt,
  25. ',', __flagfmt,
  26. '-', __flagfmt,
  27. 'C', __runefmt, /* Plan 9 addition */
  28. 'E', __efgfmt,
  29. #ifndef PLAN9PORT
  30. 'F', __efgfmt, /* ANSI only */
  31. #endif
  32. 'G', __efgfmt,
  33. #ifndef PLAN9PORT
  34. 'L', __flagfmt, /* ANSI only */
  35. #endif
  36. 'S', __runesfmt, /* Plan 9 addition */
  37. 'X', __ifmt,
  38. 'b', __ifmt, /* Plan 9 addition */
  39. 'c', __charfmt,
  40. 'd', __ifmt,
  41. 'e', __efgfmt,
  42. 'f', __efgfmt,
  43. 'g', __efgfmt,
  44. 'h', __flagfmt,
  45. #ifndef PLAN9PORT
  46. 'i', __ifmt, /* ANSI only */
  47. #endif
  48. 'l', __flagfmt,
  49. 'n', __countfmt,
  50. 'o', __ifmt,
  51. 'p', __ifmt,
  52. 'r', __errfmt,
  53. 's', __strfmt,
  54. #ifdef PLAN9PORT
  55. 'u', __flagfmt,
  56. #else
  57. 'u', __ifmt,
  58. #endif
  59. 'x', __ifmt,
  60. 0, 0,
  61. };
  62. int (*fmtdoquote)(int);
  63. /*
  64. * __fmtlock() must be set
  65. */
  66. static int
  67. __fmtinstall(int c, Fmts f)
  68. {
  69. Convfmt *p, *ep;
  70. if(c<=0 || c>=65536)
  71. return -1;
  72. if(!f)
  73. f = __badfmt;
  74. ep = &fmtalloc.fmt[fmtalloc.nfmt];
  75. for(p=fmtalloc.fmt; p<ep; p++)
  76. if(p->c == c)
  77. break;
  78. if(p == &fmtalloc.fmt[Maxfmt])
  79. return -1;
  80. p->fmt = f;
  81. if(p == ep){ /* installing a new format character */
  82. fmtalloc.nfmt++;
  83. p->c = c;
  84. }
  85. return 0;
  86. }
  87. int
  88. fmtinstall(int c, int (*f)(Fmt*))
  89. {
  90. int ret;
  91. __fmtlock();
  92. ret = __fmtinstall(c, f);
  93. __fmtunlock();
  94. return ret;
  95. }
  96. static Fmts
  97. fmtfmt(int c)
  98. {
  99. Convfmt *p, *ep;
  100. ep = &fmtalloc.fmt[fmtalloc.nfmt];
  101. for(p=fmtalloc.fmt; p<ep; p++)
  102. if(p->c == c){
  103. while(p->fmt == 0) /* loop until value is updated */
  104. ;
  105. return p->fmt;
  106. }
  107. /* is this a predefined format char? */
  108. __fmtlock();
  109. for(p=knownfmt; p->c; p++)
  110. if(p->c == c){
  111. __fmtinstall(p->c, p->fmt);
  112. __fmtunlock();
  113. return p->fmt;
  114. }
  115. __fmtunlock();
  116. return __badfmt;
  117. }
  118. void*
  119. __fmtdispatch(Fmt *f, void *fmt, int isrunes)
  120. {
  121. Rune rune, r;
  122. int i, n;
  123. f->flags = 0;
  124. f->width = f->prec = 0;
  125. for(;;){
  126. if(isrunes){
  127. r = *(Rune*)fmt;
  128. fmt = (Rune*)fmt + 1;
  129. }else{
  130. fmt = (char*)fmt + chartorune(&rune, (char*)fmt);
  131. r = rune;
  132. }
  133. f->r = r;
  134. switch(r){
  135. case '\0':
  136. return nil;
  137. case '.':
  138. f->flags |= FmtWidth|FmtPrec;
  139. continue;
  140. case '0':
  141. if(!(f->flags & FmtWidth)){
  142. f->flags |= FmtZero;
  143. continue;
  144. }
  145. /* fall through */
  146. case '1': case '2': case '3': case '4':
  147. case '5': case '6': case '7': case '8': case '9':
  148. i = 0;
  149. while(r >= '0' && r <= '9'){
  150. i = i * 10 + r - '0';
  151. if(isrunes){
  152. r = *(Rune*)fmt;
  153. fmt = (Rune*)fmt + 1;
  154. }else{
  155. r = *(char*)fmt;
  156. fmt = (char*)fmt + 1;
  157. }
  158. }
  159. if(isrunes)
  160. fmt = (Rune*)fmt - 1;
  161. else
  162. fmt = (char*)fmt - 1;
  163. numflag:
  164. if(f->flags & FmtWidth){
  165. f->flags |= FmtPrec;
  166. f->prec = i;
  167. }else{
  168. f->flags |= FmtWidth;
  169. f->width = i;
  170. }
  171. continue;
  172. case '*':
  173. i = va_arg(f->args, int);
  174. if(i < 0){
  175. /*
  176. * negative precision =>
  177. * ignore the precision.
  178. */
  179. if(f->flags & FmtPrec){
  180. f->flags &= ~FmtPrec;
  181. f->prec = 0;
  182. continue;
  183. }
  184. i = -i;
  185. f->flags |= FmtLeft;
  186. }
  187. goto numflag;
  188. }
  189. n = (*fmtfmt(r))(f);
  190. if(n < 0)
  191. return nil;
  192. if(n == 0)
  193. return fmt;
  194. }
  195. }