xref: /inferno-os/lib9/fmtdef.h (revision 1f44c82a26ff60e012a2ff697cb036a25c0c7f97)
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 /*
15  * dofmt -- format to a buffer
16  * the number of characters formatted is returned,
17  * or -1 if there was an error.
18  * if the buffer is ever filled, flush is called.
19  * it should reset the buffer and return whether formatting should continue.
20  */
21 
22 typedef int (*Fmts)(Fmt*);
23 
24 typedef struct Quoteinfo Quoteinfo;
25 struct Quoteinfo
26 {
27 	int	quoted;		/* if set, string must be quoted */
28 	int	nrunesin;	/* number of input runes that can be accepted */
29 	int	nbytesin;	/* number of input bytes that can be accepted */
30 	int	nrunesout;	/* number of runes that will be generated */
31 	int	nbytesout;	/* number of bytes that will be generated */
32 };
33 
34 void	*_fmtflush(Fmt*, void*, int);
35 void	*_fmtdispatch(Fmt*, void*, int);
36 int	_floatfmt(Fmt*, double);
37 int	_fmtpad(Fmt*, int);
38 int	_rfmtpad(Fmt*, int);
39 int	_fmtFdFlush(Fmt*);
40 
41 int	_efgfmt(Fmt*);
42 int	_charfmt(Fmt*);
43 int	_countfmt(Fmt*);
44 int	_flagfmt(Fmt*);
45 int	_percentfmt(Fmt*);
46 int	_ifmt(Fmt*);
47 int	_runefmt(Fmt*);
48 int	_runesfmt(Fmt*);
49 int	_strfmt(Fmt*);
50 int	_badfmt(Fmt*);
51 int	_fmtcpy(Fmt*, void*, int, int);
52 int	_fmtrcpy(Fmt*, void*, int n);
53 
54 void	_fmtlock(void);
55 void	_fmtunlock(void);
56 
57 #define FMTCHAR(f, t, s, c)\
58 	do{\
59 	if(t + 1 > (char*)s){\
60 		t = _fmtflush(f, t, 1);\
61 		if(t != nil)\
62 			s = f->stop;\
63 		else\
64 			return -1;\
65 	}\
66 	*t++ = c;\
67 	}while(0)
68 
69 #define FMTRCHAR(f, t, s, c)\
70 	do{\
71 	if(t + 1 > (Rune*)s){\
72 		t = _fmtflush(f, t, sizeof(Rune));\
73 		if(t != nil)\
74 			s = f->stop;\
75 		else\
76 			return -1;\
77 	}\
78 	*t++ = c;\
79 	}while(0)
80 
81 #define FMTRUNE(f, t, s, r)\
82 	do{\
83 	Rune _rune;\
84 	int _runelen;\
85 	if(t + UTFmax > (char*)s && t + (_runelen = runelen(r)) > (char*)s){\
86 		t = _fmtflush(f, t, _runelen);\
87 		if(t != nil)\
88 			s = f->stop;\
89 		else\
90 			return -1;\
91 	}\
92 	if(r < Runeself)\
93 		*t++ = r;\
94 	else{\
95 		_rune = r;\
96 		t += runetochar(t, &_rune);\
97 	}\
98 	}while(0)
99 
100 #ifndef va_copy
101 #define	va_copy(a, b) ((a) = (b))
102 #endif
103