Altmost compilable, weak symbol definitions still missing.
[vblib.git] / vblib.h
1 #ifndef _M1D_COMMON_H
2 #define _M1D_COMMON_H 1
3
4 #include <string.h>
5 #include <ctype.h>
6 #include <slang.h>
7 #include <sys/time.h>
8 #include <unistd.h>
9 #include <termios.h>
10 #include <sys/poll.h>
11 #include <setjmp.h>
12
13 /********************************
14  * Configuration section follows
15  */
16
17 /*
18  * PSTR_16BIT: whether PSTR strings should be preceded
19  *   by 16-bit or 8-bit length (thus also limiting the maximum
20  *   allowed length of PSTR string)
21  */
22 //#define PSTR_16BIT
23
24 /*
25  * INLINE_PUTGETCHAR: inline vb{put,get}char()
26  */
27 #define INLINE_PUTGETCHAR
28
29 /*
30  * FATALSIG_INSANE: if defined, no vbcheck()s are done after some fatal
31  *   signal has been caught as the data structures may be corrupted
32  *   already thereafter
33  */
34 //#define FATALSIG_INSANE
35
36 /*
37  * HAVE_PTHREAD: vbcheck() serialization between threds
38  *   Please note that VB library in general is NOT multithreading safe.
39  */
40 //#define HAVE_PTHREAD
41
42 /*
43  * VB_DEFSIZE: Default size of buffer of "vb" object. Large sizes may
44  *   slow down vbcopy() significantly.
45  */
46 #define VB_DEFSIZE 256 //>=min of n in vsnprintf()
47
48 /*
49  * LOGF_FGETS_BUF: Buffer size for vbgetlinef(), doesn't hard limit anything,
50  *   it has only performance impact.
51  */
52 #define LOGF_FGETS_BUF VB_DEFSIZE
53
54 /*
55  * VB_MAXSIZE: Maximum size of one "vbn" segment to prevent circular
56  *   use of "vb" to eat up the whole memory. It doesn't limit the maximum
57  *   data storage of "vb" object in no way, it has only performance impact.
58  */
59 #define VB_MAXSIZE 128*1024 //recommended limit, not hard (probably)
60
61 /*
62  * CHAT_CMD_MAXPARS: Size of chat_cmdpars[] array, maximum # of parameters
63  *   a user command can have during chat_proccmd() parse.
64  */
65 #define CHAT_CMD_MAXPARS 10
66
67 /*
68  * Configuration section ends
69  ****************************/
70
71 #ifndef VB_LOG
72 #define VB_LOG(lev,msg...) fprintf(stderr,VB_LOG_##lev ##msg)
73 #endif
74 #ifndef VB_FATAL
75 #define VB_FATAL(lev,msg...) do { VB_LOG(lev, ##msg); exit(EXIT_FAILURE); } while (0)
76 #endif
77
78 #define VB_LOG_DEBUG "VB_Debug: "
79 #define VB_LOG_ERR   "VB_Error: "
80 #define VB_LOG_CRIT  "VB_Critical: "
81
82 #define strncpydst(d,s) strncpy(d,s,sizeof(d))
83 #ifdef NDEBUG
84 #undef DEBUG
85 #endif
86 #ifdef DEBUG
87 #define dbg(msg...) fprintf(stderr, ##msg)
88 #else
89 #define dbg(msg...)
90 #endif
91
92 #ifndef max
93 #define max(a,b) ((a)>(b)?(a):(b))
94 #endif
95 #ifndef min
96 #define min(a,b) ((a)<(b)?(a):(b))
97 #endif
98 #ifndef __NORETURN
99 #define __NORETURN __attribute__((__noreturn__))
100 #endif
101
102 #define NELEM(a) (sizeof((a))/sizeof(*(a)))
103 #define XRAWDIGITI(n) ((n)-'0'-((n)>='A'?'A'-('9'+1):0))
104 #define XRAWDIGITO(n) ((n)+'0'+((n)>=0xA?'A'-('9'+1):0))
105 #define PK __attribute__((packed))
106
107 extern const int safesigs[];
108
109 struct varbufnode {
110         struct varbufnode *next;
111         size_t size;
112         char buf[0];
113         };
114 struct varbuf {
115 #ifndef NDEBUG
116         struct varbuf *checknext; //MUST be first in the struct
117 #endif
118         struct varbufnode *f,*l;
119         size_t done,free;
120         };
121
122 #define VBNSIZEL(vb,vbn) ((vbn)->size-((vb)->l==(vbn)?(vb)->free:0))
123 #define VBNSTART(vb,vbn)              ((vb)->f==(vbn)?(vb)->done:0)
124 #define VBNSIZE(vb,vbn) (VBNSIZEL((vb),(vbn))-VBNSTART((vb),(vbn)))
125
126 #define VBEMPTY(vb) (!(vb)->f || ((vb)->l==(vb)->f&&(vb)->l->size==(vb)->free))
127
128 #ifndef NDEBUG
129 extern void vbcheck(struct varbuf *vb);
130 extern void vbsanity(void);
131 #else
132 #define vbcheck(vb)
133 #define vbsanity()
134 #endif
135
136 #ifdef VBDEBUG
137 extern void vbdebug(struct varbuf *vb,const char *msg);
138 #else
139 #define vbdebug(vb,msg) vbcheck(vb)
140 #endif
141
142 struct chat_cmdtab {
143         const char *name;
144         const unsigned char npars;
145         void (*fnc)(void);
146         const char *help;
147 };
148
149 #define VBMEM_REM  (1<<0)
150 #define VBMEM_ZERO (1<<1)
151
152 extern void chk(const void *p);
153
154 extern struct varbufnode *vbnnew(size_t sugsize);
155 extern void vbgrow(struct varbuf *vb,size_t sugsize);
156 extern void vbinit(struct varbuf *vb);
157 extern struct varbufnode *vbremone(struct varbuf *vb);
158 extern void vbclear(struct varbuf *vb);
159 extern void vbrem(struct varbuf *vb);
160 extern size_t vbsize(struct varbuf *vb,size_t maxsize);
161 extern void vbwrite(struct varbuf *vb,const void *buf,size_t count);
162 extern size_t vbputstring(struct varbuf *vb,const char *s);
163 extern ssize_t vbwritefd(struct varbuf *vb,int fd);
164 extern size_t vbpeek(struct varbuf *vb,ssize_t offs,void *buf,size_t count);
165 #define vbread(vbp,buf,count) vbpeek(vbp,-1,buf,count)
166 extern ssize_t vbchrn(struct varbuf *vb,ssize_t offs,char c,char dir);
167 extern ssize_t vbreadfd(struct varbuf *vb,int fd);
168 extern char *vbgetline(struct varbuf *vb,char term);
169 extern size_t chkvsprintf(char **sp,const char *fmt,va_list ap);
170 extern size_t vbvprintf(struct varbuf *vb,const char *fmt,va_list ap,char remlf) __attribute__((format(printf,2,0)));
171 extern size_t vbprintf(struct varbuf *vb,const char *fmt,...) __attribute__((format(printf,2,3)));
172 extern void vbpstrputstring(struct varbuf *vb,const char *s);
173 extern void vbpstrprintf(struct varbuf *vb,const char *fmt,...) __attribute__((format(printf,2,3)));
174 extern char *vbpstrread(struct varbuf *vb);
175 extern size_t vbcopy(struct varbuf *vbd,struct varbuf *vbs,ssize_t count);
176 extern unsigned short vbxsum(struct varbuf *vb);
177 extern char vbremlast(struct varbuf *vb);
178 extern void vbdrop(struct varbuf *vb,size_t len);
179 extern void vbgetlinef(struct varbuf *vb,FILE *f);
180 extern ssize_t vbcopyline(struct varbuf *vbd,struct varbuf *vbs);
181 extern char vbdropline(struct varbuf *vb);
182 extern char *vbmemorize(struct varbuf *vb,unsigned flg,size_t *sizep);
183 extern char *vbstringify(struct varbuf *vb,char rem);
184 extern void chat_proccmd(const char *who,char *buf);
185 extern void crfilter(char *sr);
186 extern char *gmctime(time_t tm);
187 extern unsigned char *bitshuffle(unsigned char *dst,unsigned char dstb,const unsigned char *src,unsigned char srcb,size_t bits,char lr);
188 extern char *enbase64(char *dst,const unsigned char *src,size_t bits);
189 extern unsigned char *debase64(unsigned char *dst,const char *src,size_t *dstlp);
190
191 extern char vbnexact,quiet_null;
192
193 extern struct varbuf *chat_vbout;
194 extern int chat_cmdpar;
195 extern char *chat_cmdpars[CHAT_CMD_MAXPARS];
196 extern const struct chat_cmdtab chat_cmdtable[];
197
198 extern const char *signames[];
199 extern void abortsignal(int signo);
200
201 #define VBCHKSIZE(vb,size) (vbsize((vb),(size))>=(size))
202
203 #define GENVARBUFP(argmid) \
204         extern void vbput##argmid(struct varbuf *vb,const argmid arg); \
205         extern char vbget##argmid(struct varbuf *vb,argmid *argp);
206
207 #ifndef INLINE_PUTGETCHAR
208 GENVARBUFP(char)
209 #endif
210 GENVARBUFP(short)
211 GENVARBUFP(int)
212
213 #undef GENVARBUFP
214
215 #ifdef INLINE_PUTGETCHAR
216 static inline void vbputchar(struct varbuf *vb,const char arg) __attribute__((unused));
217 static inline void vbputchar(struct varbuf *vb,const char arg)
218 {
219         vbcheck(vb);
220         if (vb->free) vb->l->buf[vb->l->size-(vb->free--)]=arg;
221         else vbwrite(vb,&arg,sizeof(arg));
222 }
223
224 static inline char vbgetchar(struct varbuf *vb,char *argp) __attribute__((unused));
225 static inline char vbgetchar(struct varbuf *vb,char *argp)
226 {
227         vbcheck(vb);
228         if (vb->f && vb->done+1<VBNSIZEL(vb,vb->f)) { *argp=vb->f->buf[vb->done++]; return(1); }
229         else return vbread(vb,argp,sizeof(*argp));
230 }
231 #endif
232
233 #endif /* !_M1D_COMMON_H */