fmt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "lib9.h"
  15. #include "fmtdef.h"
  16. enum
  17. {
  18. Maxfmt = 64
  19. };
  20. typedef struct Convfmt Convfmt;
  21. struct Convfmt
  22. {
  23. int c;
  24. volatile Fmts fmt; /* for spin lock in fmtfmt; avoids race due to write order */
  25. };
  26. struct
  27. {
  28. /* lock by calling _fmtlock, _fmtunlock */
  29. int nfmt;
  30. Convfmt fmt[Maxfmt];
  31. } fmtalloc;
  32. static Convfmt knownfmt[] = {
  33. ' ', _flagfmt,
  34. '#', _flagfmt,
  35. '%', _percentfmt,
  36. '+', _flagfmt,
  37. ',', _flagfmt,
  38. '-', _flagfmt,
  39. 'C', _runefmt,
  40. 'S', _runesfmt,
  41. 'X', _ifmt,
  42. 'b', _ifmt,
  43. 'c', _charfmt,
  44. 'd', _ifmt,
  45. 'h', _flagfmt,
  46. 'l', _flagfmt,
  47. 'n', _countfmt,
  48. 'o', _ifmt,
  49. 'p', _ifmt,
  50. 'r', errfmt,
  51. 's', _strfmt,
  52. 'u', _flagfmt,
  53. 'x', _ifmt,
  54. 0, nil,
  55. };
  56. int (*doquote)(int);
  57. static Fmts
  58. fmtfmt(int c)
  59. {
  60. Convfmt *p, *ep;
  61. ep = &fmtalloc.fmt[fmtalloc.nfmt];
  62. for(p=fmtalloc.fmt; p<ep; p++)
  63. if(p->c == c)
  64. return p->fmt;
  65. /* is this a predefined format char? */
  66. for(p=knownfmt; p->c; p++)
  67. if(p->c == c){
  68. /* no need to lock; fmtinstall is idempotent */
  69. fmtinstall(p->c, p->fmt);
  70. while(p->fmt == nil) /* loop until value is updated */
  71. ;
  72. return p->fmt;
  73. }
  74. return _badfmt;
  75. }
  76. int
  77. fmtinstall(int c, Fmts f)
  78. {
  79. Convfmt *p, *ep;
  80. if(c<=0 || c>=65536)
  81. return -1;
  82. if(!f)
  83. f = _badfmt;
  84. _fmtlock();
  85. ep = &fmtalloc.fmt[fmtalloc.nfmt];
  86. for(p=fmtalloc.fmt; p<ep; p++)
  87. if(p->c == c)
  88. break;
  89. if(p == &fmtalloc.fmt[Maxfmt]){
  90. _fmtunlock();
  91. return -1;
  92. }
  93. p->fmt = f;
  94. if(p == ep){ /* installing a new format character */
  95. fmtalloc.nfmt++;
  96. p->c = c;
  97. }
  98. _fmtunlock();
  99. return 0;
  100. }
  101. void*
  102. _fmtdispatch(Fmt *f, void *fmt, int isrunes)
  103. {
  104. Rune rune, r;
  105. int i, n;
  106. f->flags = 0;
  107. f->width = f->prec = 0;
  108. for(;;){
  109. if(isrunes){
  110. r = *(Rune*)fmt;
  111. fmt = (Rune*)fmt + 1;
  112. }else{
  113. fmt = (char*)fmt + chartorune(&rune, fmt);
  114. r = rune;
  115. }
  116. f->r = r;
  117. switch(r){
  118. case '\0':
  119. return nil;
  120. case '.':
  121. f->flags |= FmtWidth|FmtPrec;
  122. continue;
  123. case '0':
  124. if(!(f->flags & FmtWidth)){
  125. f->flags |= FmtZero;
  126. continue;
  127. }
  128. /* fall through */
  129. case '1': case '2': case '3': case '4':
  130. case '5': case '6': case '7': case '8': case '9':
  131. i = 0;
  132. while(r >= '0' && r <= '9'){
  133. i = i * 10 + r - '0';
  134. if(isrunes){
  135. r = *(Rune*)fmt;
  136. fmt = (Rune*)fmt + 1;
  137. }else{
  138. r = *(char*)fmt;
  139. fmt = (char*)fmt + 1;
  140. }
  141. }
  142. if(isrunes)
  143. fmt = (Rune*)fmt - 1;
  144. else
  145. fmt = (char*)fmt - 1;
  146. numflag:
  147. if(f->flags & FmtWidth){
  148. f->flags |= FmtPrec;
  149. f->prec = i;
  150. }else{
  151. f->flags |= FmtWidth;
  152. f->width = i;
  153. }
  154. continue;
  155. case '*':
  156. i = va_arg(f->args, int);
  157. if(i < 0){
  158. i = -i;
  159. f->flags |= FmtLeft;
  160. }
  161. goto numflag;
  162. }
  163. n = (*fmtfmt(r))(f);
  164. if(n < 0)
  165. return nil;
  166. if(n == 0)
  167. return fmt;
  168. }
  169. }