fmt.c 4.1 KB

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