chiark / gitweb /
Merge and end branch-hostside-wip-2008-01-25 PROPERLY; cvs up -j branch-hostside...
[trains.git] / hostside / common.h
1 /*
2  * declarations common to
3  *  - simple test program
4  *  - realtime daemon
5  *  - multiplexer daemon
6  */
7
8 #ifndef COMMON_H
9 #define COMMON_H
10
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/time.h>
14
15 typedef struct ParseState ParseState;
16 typedef struct CmdInfo CmdInfo;
17 typedef struct Client Client;
18
19 extern const char *progname;
20
21 /*---------- types ----------*/
22
23 typedef unsigned char Byte;
24
25 #define COMMAND_ENCODED_MAX 16
26 #define NMRA_PACKET_MAX ((COMMAND_ENCODED_MAX*7 - 14) / 8)
27
28 typedef struct Nmra {
29   Byte d[NMRA_PACKET_MAX];
30   int l;
31 } Nmra;
32
33 typedef struct PicInsn {
34   Byte d[COMMAND_ENCODED_MAX];
35   int l;
36 } PicInsn;
37
38 /*---------- from parseutils.c ----------*/
39
40 struct ParseState {
41   Client *cl; /* used only by multiplexer */
42   const char *remain;
43   const char *thisword;
44   int lthisword;
45 };
46
47 void vbadcmd(ParseState *ps, const char *fmt, va_list al)
48   __attribute__((format(printf,2,0)));
49 void badcmd(ParseState *ps, const char *fmt, ...)
50   __attribute__((format(printf,2,3)));
51
52 int lstrstrcmp(const char *a, int la, const char *b);
53 int thiswordstrcmp(ParseState *ps, const char *b);
54
55 int ps_word(ParseState *ps);
56 int ps_needword(ParseState *ps);
57 int ps_needhextoend(ParseState *ps, Byte *dbuf, int *len_io);
58 int ps_neednumber(ParseState *ps, long *r, long min, long max, const char *wh);
59 int ps_neednoargs(ParseState *ps);
60
61 /*---------- macro for table lookups, with help from parseutils.c ----------*/
62
63 #define some_lookup(ps, infos)                  \
64   ((const typeof(infos[0])*)                    \
65    any_lookup((ps),(infos),sizeof((infos)[0])))
66
67 #define some_needword_lookup_counted(ps, infos, ninfos, what)   \
68   ((const typeof(infos[0])*)                                    \
69    any_needword_lookup((ps),                                    \
70                        (infos), (ninfos), sizeof((infos)[0]),   \
71                        (what)))
72
73 #define some_needword_lookup(ps, infos, what)                   \
74   ((const typeof(infos[0])*)                                    \
75    any_needword_lookup((ps),(infos),INT_MAX,sizeof((infos)[0]),(what)))
76
77 const void *any_lookup(ParseState *ps,
78                        const void *infos, int ninfsmax, size_t infosz);
79 const void *any_needword_lookup(ParseState *ps, const void *infos,
80                                 int ninfsmax, size_t sz, const char *what);
81
82 /*---------- from client.c ----------*/
83
84 struct CmdInfo {
85   const char *name;
86   void (*fn)(ParseState *ps, const CmdInfo *ci);
87   int xarg;
88 };
89
90 void stdin_client(void);
91
92 extern const CmdInfo toplevel_cmds[]; /* defined in commands.c*/
93
94 /*---------- from utils.c ----------*/
95
96 void vdie(const char *fmt, int ev, va_list al)
97      __attribute__((noreturn,format(printf,1,0)));
98 void die(const char *fmt, ...) __attribute__((noreturn,format(printf,1,2)));
99 void diee(const char *fmt, ...) __attribute__((noreturn,format(printf,1,2)));
100 void diem(void) __attribute__((noreturn));
101
102 void die_hook(void);
103 void die_vprintf_hook(const char *fmt, va_list al)
104      __attribute__((format(printf,1,0)));
105
106 void *mmalloc(size_t sz);
107 void *mrealloc(void *old, size_t sz);
108 char *mstrdupl(const char *s, int l);
109 char *mstrdup(const char *s);
110 void mgettimeofday(struct timeval *tv);
111
112 void mrename(const char *old, const char *new);
113
114 void badusage(const char *why);
115
116 #define massert(x) ((x) ? (void)0 : diem())
117
118 /*---------- from serialio.c ----------*/
119
120 void serial_open(const char *device);
121 void serial_transmit_now(const Byte *command, int length);
122
123 extern int serial_fd, serial_fudge_delay;
124
125 /*---------- nmra parsing, nmra.c et al ----------*/
126
127 typedef struct NmraParseEncodeCaller NmraParseEncodeCaller;
128 void nmra_parse_encode(Nmra *out, const char *arg, int argl,
129                        int argc, NmraParseEncodeCaller *pec);
130   /* will call back to these, supplied by caller: */
131   unsigned long nmra_argnumber(NmraParseEncodeCaller *pec, int argi);
132   void nmra_problem(NmraParseEncodeCaller *pec, const char *problem);
133
134 void nmra_addchecksum(Nmra *packet);
135 void nmra_encodeforpic(const Nmra *packet, PicInsn *pi_out);
136
137
138 #define ARRAY_SIZE(a) (sizeof((a))/sizeof(*(a)))
139 #define ARRAY_END(a) ((a) + ARRAY_SIZE((a)))
140
141
142 #endif /*COMMON_H*/