chiark / gitweb /
publish total amount buffered in an obc
[trains.git] / hostside / daemons.h
1 /*
2  * declarations common to
3  *  - realtime daemon
4  *  - gui-plan-* displayers
5  */
6
7 #ifndef DAEMONS_H
8 #define DAEMONS_H
9
10 #include <oop.h>
11 #include <oop-read.h>
12
13 #include "common.h"
14
15 typedef struct CommandInput CommandInput;
16
17 /*---------- from obc.c ----------*/
18
19 typedef struct OutBuffer OutBuffer;
20 typedef struct OutBufferChain OutBufferChain;
21 typedef void OutBufferError(OutBufferChain*, const char *e1, const char *e2
22                             /* on error: both e1 and e2 non-0. say `$e1: $e2'
23                              * on eof: both e1 and e2 =0. */);
24 typedef void OutBufferEmpty(OutBufferChain*);
25
26 typedef void CopyCallBack(char *m, size_t l, void *u);
27
28 struct OutBufferChain {
29   /* set by user: */
30   char *desc;
31   int fd;
32   int limit; /* 0 means obc_init will set a default */
33   OutBufferError *error;
34   OutBufferEmpty *empty; /* may be 0 */
35   /* set/used by obc_... but may be read by user */
36   int total; /* amount buffered */
37   /* set/used by obc_..., oprintf, etc., only */
38   int done_of_head;
39   struct { OutBuffer *head, *tail; } obs;
40 };
41
42 void obc_init(OutBufferChain *ch);
43 void obc_init_core(OutBufferChain *ch); /* doesn't mess with fd */
44 int obc_tryflush(OutBufferChain *ch);
45  /* returns 0 for all flushed or errno, including particularly EWOULDBLOCK */
46
47 void ovprintf_ccb(OutBufferChain *ch, CopyCallBack *ccb, void *ccbu,
48                    const char *fmt, va_list al)
49      __attribute__((format(printf,4,0)));
50 void ovprintf(OutBufferChain *ch, const char *fmt, va_list al)
51      __attribute__((format(printf,2,0)));
52 void oprintf(OutBufferChain *ch, const char *msg, ...)
53      __attribute__((format(printf,2,3)));
54 void owrite(OutBufferChain *ch, const char *data, int l);
55 void voerror(OutBufferChain *ch, const char *fmt, va_list al)
56      __attribute__((format(printf,2,0)));
57
58 /*---------- from cmdinput.c ----------*/
59
60 extern oop_source *events;
61
62 typedef void CommandInputCallback(ParseState *ps, CommandInput *cmdi);
63
64 struct CommandInput {
65   OutBufferChain out;
66   CommandInputCallback *doline;
67   oop_read *rd;
68 };
69
70 void cmdin_new(CommandInput *cmdi, int readfd);
71   /* fill in cmdi->out's `set by user' fields before calling cmdin_new,
72    * as cmdin_new will call obc_init and will use cmdi->out->fd. */
73
74 #endif /*DAEMONS_H*/