fmt.c 4.0 KB

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