optimising includes
[cilux] / src / include / kernelapi.h
1 #ifndef KERNEL_H
2 #define KERNEL_H
3
4 /* 
5    This API is the equivalent of a system call API and thereby
6    insulates its user from a GPL implementation.
7 */
8
9 #include <platform.h>
10
11 /* -------------------------------------------------------------------------- */
12
13 PUBLIC char*   k_version;
14 PUBLIC char*   k_ciux;
15 PUBLIC void  (*k_terminate)(void);
16
17 /* -------------------------------------------------------------------------- */
18
19 #define NAME_OF_MODULE_LOADED_FN "_module_loaded"
20
21 typedef int (*k_module_loaded_fn)(void);
22
23 /* -------------------------------------------------------------------------- */
24
25 #define NAME_OF_MODULE_TICK_FN "_module_tick"
26
27 typedef void (*k_module_tick_fn)(void);
28
29 /* -------------------------------------------------------------------------- */
30
31 #define NAME_OF_MODULE_EVENT_FN  "_module_event"
32
33 typedef int (*k_module_event_fn)(void* data);
34
35 PUBLIC int k_event_post(char* module, void* data);
36
37 /* -------------------------------------------------------------------------- */
38
39 typedef void (*k_gl_reshape_event)(int width, int height);
40 typedef void (*k_gl_draw_event)(void);
41 typedef void (*k_gl_key_event)(unsigned char k, int down);
42 PUBLIC  void   k_gl_register_reshape(k_gl_reshape_event);
43 PUBLIC  void   k_gl_register_draw(   k_gl_draw_event);
44 PUBLIC  void   k_gl_register_key(    k_gl_key_event);
45 PUBLIC  void gluxSwapBuffers(void);
46
47 /* -------------------------------------------------------------------------- */
48
49 #define CRLF "\015\012"
50 #define CHAN_LISTEN 1
51 #define CHAN_ESTABL 2
52 #define CHAN_ANY    3
53
54 typedef struct k_channel      k_channel;
55 typedef struct k_channel_priv k_channel_priv;
56
57 typedef int (*k_channel_event)(k_channel* chan,
58                                int        bufpos, 
59                                int        size);
60
61 struct k_channel{ struct k_channel* next;
62
63         char*           name;
64         char*           listenhost;
65         int             listenport;
66         char*           banner;
67         k_channel_event rdcallback;
68         k_channel_event wrcallback;
69         void*           context;
70         int             type;
71         struct in_addr  clientip;
72         int             linger;
73         k_channel_priv* priv;
74 };
75
76 PUBLIC char*      k_channel_name(char* type, char* ip, int port, char* other);
77 PUBLIC k_channel* k_channel_connect_name(char*           chanm,
78                                          k_channel_event rdcallback,
79                                          k_channel_event wrcallback);
80 PUBLIC k_channel* k_channel_get_name(char* chanm);
81 PUBLIC void       k_channel_show_all(void);
82
83 PUBLIC k_channel* k_channel_listen(char*           name,
84                                    int             listenport,
85                                    char*           banner,
86                                    k_channel_event rdcallback,
87                                    k_channel_event wrcallback,
88                                    void*           context);
89
90 PUBLIC k_channel* k_channel_connect(char*           name,
91                                     char*           listenhost,
92                                     int             listenport,
93                                     k_channel_event rdcallback,
94                                     k_channel_event wrcallback,
95                                     void*           context);
96
97 #define FREE_ON_SENT 1
98 PUBLIC k_channel* k_channel_send(k_channel*  chan,
99                                  char*       base,
100                                  size_t      size,
101                                  int         free);
102
103 #define BUFFER_ALREADY_SET -1
104 #define BUFFER_FILLED       1
105 #define BUFFER_SET          0
106 PUBLIC char* k_channel_chop_div( k_channel* chan, char*  divider);
107 PUBLIC char* k_channel_chop_len( k_channel* chan, size_t size);
108 PUBLIC int   k_channel_setbuf(   k_channel* chan, char*  rdbuffer, size_t size);
109 PUBLIC char* k_channel_getbuf(   k_channel* chan);
110
111 PUBLIC void k_channel_close(k_channel* chan);
112
113 /* -------------------------------------------------------------------------- */
114
115 #define STAT_F 0100000
116 #define STAT_D 0040000
117 #define STAT_L 0120000
118 #define MAX_FILESIZE (1024*1024*1024)
119 #define USE_MMAP 0
120 #define USE_MALL MAX_FILESIZE
121
122 typedef struct k_stat{
123         int    type;
124         size_t size;
125         time_t time;
126         mode_t perm;
127 } k_stat;
128
129 typedef void (*k_file_event)(char*  basedir,
130                              char*  filename,
131                              char*  data,
132                              int    usedmmap,
133                              k_stat stat,
134                              void*  context);
135
136 PUBLIC void k_file_stat( char* basedir, char* filename,
137                                           k_file_event callback, void* context);
138 PUBLIC void k_file_read( char* basedir, char* filename, size_t msz, size_t size,
139                                           k_file_event callback, void* context);
140 PUBLIC void k_file_write(char* basedir, char* filename, char* data, size_t size,
141                                           k_file_event callback, void* context);
142 PUBLIC void k_dir_list(  char* basedir, char* dirname,
143                          char* pattern[], char* format[],  
144                                           k_file_event callback, void* context);
145 PUBLIC int  k_file_sync(char* data, size_t size);
146
147 /* -------------------------------------------------------------------------- */
148
149 typedef struct k_hashtable{ struct k_hashtable* next; } k_hashtable;
150
151 typedef void (*k_hashtable_apply_fn)(void* arg, char* key, void* val);
152
153 PUBLIC k_hashtable* k_hashtable_new(char* name, int ignorecase);
154 PUBLIC k_hashtable* k_hashtable_dup( k_hashtable* tab);
155 PUBLIC k_hashtable* k_hashtable_deep(k_hashtable* tab);
156 PUBLIC k_hashtable* k_hashtable_dip( k_hashtable* tab, char* includes[]);
157 PUBLIC void  k_hashtable_merge(  k_hashtable* tab, k_hashtable* tam);
158 PUBLIC void  k_hashtable_set(    k_hashtable* tab, char* key, void* val);
159 PUBLIC void  k_hashtable_put(    k_hashtable* tab, char* key, void* val);
160 PUBLIC void  k_hashtable_put_dup(k_hashtable* tab, char* key, char* val);
161 PUBLIC void  k_hashtable_put_int(k_hashtable* tab, char* key, int   val);
162 PUBLIC void  k_hashtable_sub(    k_hashtable* tab, char* key, k_hashtable* sub);
163 PUBLIC void* k_hashtable_get(    k_hashtable* tab, char* key);
164 PUBLIC char* k_hashtable_get_dup(k_hashtable* tab, char* key);
165 PUBLIC int   k_hashtable_get_int(k_hashtable* tab, char* key);
166 PUBLIC int   k_hashtable_is(     k_hashtable* tab, char* key, char* val);
167 PUBLIC int   k_hashtable_isi(    k_hashtable* tab, char* key, char* val);
168 PUBLIC int   k_hashtable_isn(    k_hashtable* tab, char* key, char* val,size_t);
169 PUBLIC void  k_hashtable_remove( k_hashtable* tab, char* key);
170 PUBLIC void* k_hashtable_extract(k_hashtable* tab, char* key);
171 PUBLIC void  k_hashtable_apply(  k_hashtable* tab, k_hashtable_apply_fn, void*);
172 PUBLIC void  k_hashtable_show(      k_hashtable* tab);
173 PUBLIC void  k_hashtable_show_chars(k_hashtable* tab);
174 PUBLIC int   k_hashtable_snprintf(  k_hashtable* tab, char* buf, size_t size);
175 PUBLIC int   k_hashtable_snprintf_i(k_hashtable* tab, char* buf, size_t size,
176                                                             char* includes[]);
177 PUBLIC int   k_hashtable_snprintf_x(k_hashtable* tab, char* buf, size_t size,
178                                                             char* excludes[]);
179 PUBLIC void  k_hashtable_delete(k_hashtable* tab);
180
181 /* -------------------------------------------------------------------------- */
182
183 PUBLIC int  k_string_matches_pattern(char* string, char* pattern);
184 PUBLIC int  k_string_ends_with(      char* string, char* postfix);
185 PUBLIC void k_string_url_decode(     char* string);
186
187 /* -------------------------------------------------------------------------- */
188
189 PUBLIC char*  k_time_to_rfc(time_t t);
190 PUBLIC char*  k_time_to_rfc_relative(int plus);
191 PUBLIC time_t k_time_from_rfc(char* s);
192 PUBLIC void   k_time_get_now(char* buf, size_t size, char* format);
193
194 /* -------------------------------------------------------------------------- */
195
196 PUBLIC void k_random_bytes(char* buf, size_t size);
197
198 /* -------------------------------------------------------------------------- */
199
200 PUBLIC void* k_malloc(size_t size);
201 PUBLIC void* k_realloc(void* o, size_t size);
202 PUBLIC char* k_strdup(char* s);
203 PUBLIC void* k_memdup(void* s, size_t size);
204 PUBLIC void  k_free(void* o);
205
206 /* -------------------------------------------------------------------------- */
207
208 PUBLIC void k_log_out(char* format, ...);
209 PUBLIC void k_log_err(char* format, ...);
210 PUBLIC void k_fatal(  char* format, ...);
211
212 /* -------------------------------------------------------------------------- */
213
214 #endif