b27abb8a |
1 | /* .---------------------------------------------------------------------. |
005431b8 |
2 | / C<<1 header file - C language style sheet - GNU C/ANSI C99 compliant |
3 | / Cumbersome Extraction and Report Language - http://cll1h.arachne.cz/ |
f5725936 |
4 | '---------------------------------------------------------------------- */ |
5 | /* |
6 | Copyright (c) 2004-2008 Michael xChaos Polak - x(at)n.cz |
30628092 |
7 | |
8 | The C<<1 header file is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU Lesser General Public |
10 | License as published by the Free Software Foundation; either |
11 | version 2.1 of the License, or (at your option) any later version. |
12 | |
13 | The C<<1 header file is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Lesser General Public License for more details. |
17 | |
ceb7f88d |
18 | You should have received a copy of the GNU Lesser General Public License |
19 | along with C<<1 header file; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
097bac9a |
21 | */ |
097bac9a |
22 | #ifndef _CLL1_H |
23 | #define _CLL1_H |
f5725936 |
24 | #define CLL1H_VERSION "0.7.1" |
005431b8 |
25 | /* .----------------------------------------------------------------------. |
f5725936 |
26 | / 0. Library definitions not parsed by @CC, updated 2008-01-26 xCh. |
27 | '---------------------------------------------------------------------- */ |
28 | /* |
29 | @define USE_LIBRARY_MYSQL -lmysqlclient |
30 | @define USE_LIBRARY_FASTCGI -lfcgi |
31 | @define USE_PREPROCESSOR cake *.html *.xml > _include.c |
32 | @define USE_POSTPROCESSOR strip @ |
33 | */ |
005431b8 |
34 | /* .----------------------------------------------------------------------. |
f5725936 |
35 | / 1. C<<1 header auto include, updated 2008-01-26 xCh. |
36 | '---------------------------------------------------------------------- */ |
bc44f2cc |
37 | #ifndef DISABLE_MODULE_AUTOINCLUDE |
30628092 |
38 | #include <string.h> |
25a3f430 |
39 | #include <strings.h> |
04a13a6f |
40 | #include <malloc.h> |
30628092 |
41 | #include <time.h> |
04a13a6f |
42 | #include <stdlib.h> |
420bb771 |
43 | #include <stdarg.h> |
04a13a6f |
44 | #include <unistd.h> |
eac5d6c7 |
45 | #include <setjmp.h> |
49df11df |
46 | #include <errno.h> |
4643aeaf |
47 | #include <locale.h> |
04a13a6f |
48 | #ifdef USE_LIBRARY_FASTCGI |
49 | #include <fcgi_stdio.h> |
50 | #else |
51 | #include <stdio.h> |
52 | #endif |
bce4b05d |
53 | #endif |
54 | #ifdef USE_LIBRARY_MYSQL |
55 | #include <mysql/mysql.h> |
56 | #endif |
005431b8 |
57 | /* .----------------------------------------------------------------------. |
99cd47b1 |
58 | / 0. C<<1 typedefs and public global variables, updated 2008-02-03 xCh. |
4643aeaf |
59 | '----------------------------------------------------------------------- */ |
99cd47b1 |
60 | typedef void * any; |
bc44f2cc |
61 | typedef unsigned char * buf; /* this will be always 8-bit usigned char * */ |
4643aeaf |
62 | typedef FILE * file; |
63 | typedef int bool; |
99cd47b1 |
64 | #ifdef USE_WCHAR_T |
65 | typedef wchar_t * str; |
66 | #else |
67 | typedef char * str; /* Warning: full wchar_t support not yet implemented! */ |
68 | #endif |
69 | extern str NIL, EOL, OFS, IFS, LANG, errstr; /* used for: perror(errstr) */ |
4643aeaf |
70 | extern unsigned RANDOM_SEED; |
99cd47b1 |
71 | #define _EXPORT_GLOBALS \ |
72 | str NIL="(null)", EOL="\n", OFS=" ", IFS=" \t\n", LANG=NULL; \ |
73 | str errstr="C<<1 0xdead"; \ |
74 | unsigned RANDOM_SEED=0; |
4643aeaf |
75 | /* .----------------------------------------------------------------------. |
bc44f2cc |
76 | / 1. C<<1 then buddhist section, updated 2008-02-04 xCh. |
f5725936 |
77 | '----------------------------------------------------------------------- */ |
78 | #ifndef DISABLE_MODULE_THEN_BUDDHISM |
04a13a6f |
79 | #define then |
25a3f430 |
80 | #define MAYBE (int)((long)(RANDOM_SEED?random():((RANDOM_SEED=(unsigned)time(NULL)),srandom(RANDOM_SEED),random()))%2) |
81 | #define WHOKNOWS (int)(srandom((unsigned)time(NULL)),random()%2) |
bc44f2cc |
82 | #define for_samsara(MIND,SOUL) for(MIND=SOUL; MIND || ((MIND->__next=SOUL),0) ; MIND=MIND->__next) |
4643aeaf |
83 | #define samsara(MIND,SOUL) for(MIND=SOUL; MIND || ((MIND->__next=SOUL),0) ; MIND=MIND->__next) |
25a3f430 |
84 | //#define atob(EXPR) (word_in_stri(YES,(EXPR))?1:0) |
4643aeaf |
85 | //#define _EXPORT_YES str YES="TRUE\nYES\nJA\nOUI\nDA\nANO\nY\n1"; |
25a3f430 |
86 | //#else |
b27abb8a |
87 | #define atob(EXPR) (eqi((EXPR),YES)?TRUE:FALSE) |
4643aeaf |
88 | #define _EXPORT_YES str YES="TRUE"; |
f5725936 |
89 | #endif |
005431b8 |
90 | /* .----------------------------------------------------------------------. |
61ad8a2b |
91 | / 2. C<<1 almost then buddhist section, updated 2008-02-02 xCh. |
f5725936 |
92 | '----------------------------------------------------------------------- */ |
30628092 |
93 | #define not ! |
4a48705a |
94 | #define and && |
95 | #define or || |
30628092 |
96 | #define TRUE 1 |
97 | #define FALSE 0 |
e0467a3b |
98 | #define PI 3.141592654 |
9ae3b221 |
99 | #define WEIRDINT (1<<(sizeof(int)*8-1)) |
25a3f430 |
100 | #define btoa(EXPR) ((EXPR)?"TRUE":"FALSE") |
eeff2724 |
101 | #define coalesce(VAR,EXPR) (VAR?VAR:(VAR=0,(EXPR))) |
102 | #define coalesce_str(VAR,EXPR) (VAR && *VAR?VAR:((VAR=NULL),(EXPR))) |
005431b8 |
103 | /* .----------------------------------------------------------------------. |
547332b7 |
104 | / 4. C<<1 data declaration and allocation, updated 2008-01-31 xCh. |
f5725936 |
105 | '----------------------------------------------------------------------- */ |
9abd9073 |
106 | #define def_type(TYPE) typedef struct _##TYPE##_struct * TYPE |
107 | #define def_mem(TYPE) def_type(TYPE); struct _##TYPE##_struct |
1a8d78d9 |
108 | #define get_mem(TYPE) _GET_MEM(TYPE) |
24cfa6bd |
109 | #define get_buf(SIZE) (buf)malloc(SIZE) /* temporary solution */ |
9abd9073 |
110 | #define size_of_mem(TYPE) sizeof(struct _##TYPE##_struct) |
111 | #define copy_of_mem(TYPE,SRC) _cll1_mem_cpy(sizeof(struct _##TYPE##_struct),SRC) |
4643aeaf |
112 | #define list(TYPE) TYPE __next |
113 | #define tree(TYPE) TYPE __next; TYPE __seek |
818209bd |
114 | #define dict(TYPE) tree(TYPE); str __key |
115 | #define array(TYPE) tree(TYPE); int __key |
eac5d6c7 |
116 | /* ---- variable declarators -------------------------------------------- */ |
24cfa6bd |
117 | #define Get_mem(ID,TYPE) TYPE ID=get_mem(TYPE) |
118 | #define Get_buf(ID,SIZE) buf ID=get_buf(SIZE) |
005431b8 |
119 | /* .----------------------------------------------------------------------. |
f5725936 |
120 | / 5. C<<1 string manipulation, updated 2008-01-26 xCh. |
121 | '----------------------------------------------------------------------- */ |
61ad8a2b |
122 | #define get_str(...) _cll1_get_str(__VA_ARGS__,NIL) |
bc44f2cc |
123 | #define eq(A,B) (!_cll1_str_cmp(A,B)) |
124 | #define eqi(A,B) (!_cll1_str_cmpi(A,B)) |
125 | #define str_order(A,B) _cll1_str_coll(A,B) |
126 | #define str_ascii(A,B) _cll1_str_cmp(A,B) |
24cfa6bd |
127 | #define chars_in_str(...) _cll1_strlen(__VA_ARGS__,NIL) |
128 | #define size_of_str(A) mbstowcs(NULL,A,0) |
bc44f2cc |
129 | #define str_in(A,B) _cll1_str_str(A,B) |
ce26b4aa |
130 | #define word_in(A,B) _cll1_wordinstr(A,B,0) |
bce4b05d |
131 | #define print(...) _cll1_print(__VA_ARGS__,NIL) |
132 | #define fprint(...) _cll1_fprint(__VA_ARGS__,NIL) |
133 | #define file_print(...) _cll1_fprint(_F,__VA_ARGS__,NIL) |
134 | #define echo(...) _cll1_echo(__VA_ARGS__,NIL) |
135 | #define fecho(...) _cll1_fecho(__VA_ARGS__,NIL) |
136 | #define file_echo(...) _cll1_fecho(_F,__VA_ARGS__,NIL) |
005431b8 |
137 | #define suffix(A,B,C) (((A=strrchr(B,C))&&!(*(A++)=0))||(A=B)) |
138 | #define prefix(A,B,C) ((A=B),((B=strchr(B,C))&&!(*(B++)=0)||(B=A))) |
547332b7 |
139 | #define tr(STR,B,C) _NEW(S,STR) while(S && *_S) { if(*_S==B)*_S=C; /* <-' */ _S++; } |
eac5d6c7 |
140 | /* ---- variable declarators -------------------------------------------- */ |
24cfa6bd |
141 | #define Get_str(ID,...) str ID=get_str(__VA_ARGS__) |
005431b8 |
142 | /* .----------------------------------------------------------------------. |
f5725936 |
143 | / 6. C<<1 data container manipulation, updated 2008-01-26 xCh. |
144 | '----------------------------------------------------------------------- */ |
005431b8 |
145 | #define push(NODE,HEAD) ( NODE ? ((NODE->__next=HEAD),(HEAD=NODE)) : NULL ) |
146 | #define pop(NODE,HEAD) ( HEAD ? ((NODE=HEAD->__next),free(HEAD),(HEAD=NODE)) : (NODE=NULL) ) |
96ec74e5 |
147 | #define append(NODE,HEAD) { void *_PTR=NODE; if(NODE) NODE->__next=NULL; if(HEAD) { for_search(NODE,HEAD,!NODE->__next) { NODE->__next=_PTR; break; } } else HEAD=NODE; } |
06ab91f0 |
148 | #define remove(NODE,HEAD,EXPR) { void **_D=NULL; /* <-' */ for_search(NODE,HEAD,EXPR) { if(_D)*_D=NODE->__next; else HEAD=NODE->__next; free(NODE); } else _D=(void *)&(NODE->__next); } |
f5725936 |
149 | #define drop(NODE,HEAD) for( NODE=HEAD; NODE || (HEAD=NULL); HEAD=NODE, NODE=NODE->__next, free(HEAD) ) |
005431b8 |
150 | //to do: seek |
151 | //to do: store |
4643aeaf |
152 | #define count(NODE,HEAD,VAR) { _NEW(I,0) for(NODE=HEAD; NODE || ((VAR=_I),0) ; (NODE=NODE->__next)?(_I++):0 ); } |
ea514b9e |
153 | #define order_by_num(K1,K2) (K1>K2) |
154 | #define order_by_num_desc(K1,K2) (K1<K2) |
ce26b4aa |
155 | #define order_by_str(K1,K2) (str_order(K1,K2)>0) |
156 | #define order_by_str_desc(K1,K2) (str_order(K1,K2)<0) |
157 | #define order_by_ascii(K1,K2) (str_ascii(K1,K2)>0) |
158 | #define order_by_ascii_desc(K1,K2) (str_ascii(K1,K2)<0) |
159 | #define insert(NODE,HEAD,ORDERBY,KEY) { if(HEAD) { void **_L=NULL, *H=HEAD; /* <-' */ for_search(HEAD,H,ORDERBY(HEAD->KEY,NODE->KEY)) { if(_L) {*_L=NODE; NODE->__next=HEAD; } else push(NODE,H); break; } else _L=(void *)&(HEAD->__next); if(!HEAD)*_L=NODE; HEAD=H; } else if (NODE) { push(NODE,HEAD); HEAD->__next=NULL; } } |
160 | #define sort(NODE,HEAD,ORDERBY,KEY) { void *_NEXT; /* <-' */ NODE=HEAD; HEAD=NULL; do { _NEXT=NODE->__next; NODE->__next=NULL; insert(NODE,HEAD,ORDERBY,KEY); NODE=_NEXT; } while(_NEXT); } |
005431b8 |
161 | /* .----------------------------------------------------------------------. |
f5725936 |
162 | / 7. C<<1 builtin iterators and conditionals, updated 2008-01-26 xCh. |
163 | '----------------------------------------------------------------------- */ |
04a13a6f |
164 | #define loop while(TRUE) |
eac5d6c7 |
165 | #define repeat(N) _NEW(I,(_J=(N),0)) while(_I++<_J) /* C99 version would be: for(int _i=0,_j=(N);_i++<_j;) */ |
166 | #define for_range(VAR,FROM,TO) _NEW(I,((_J=(TO)),FROM)) for( VAR=_I ; _I<=_J?VAR<=_J:VAR>=_J ; _I<=_J?VAR++:VAR--) |
f5725936 |
167 | #define for_each(NODE,HEAD) for(NODE=HEAD; NODE; NODE=NODE->__next) |
168 | #define for_search(NODE,HEAD,EXPR) for_each(NODE,HEAD) if(EXPR) |
bce4b05d |
169 | #define for_file_lines(STR,FNAME) /* _NEW(CTX,get_context()) file_read(FNAME) for(use_context(_CTX),STR=file_line(_F);release_context(_M),(*S && !feof(_F));STR=file_line(_F),end_context()) _CATCH */ |
170 | #define for_input_lines(S,L) /* _NEW(CTX,get_context()) for(use_context(_CTX),STR=file_get_line(stdin);release_context(_M),(*S && !feof(stdin));STR=file_get_line(stdin)) _CATCH */ |
9ae3b221 |
171 | #define for_ints(VAR,...) _NEW(I,0) while( (VAR=_cll1_for_ints(_I++,__VA_ARGS__,WEIRDINT))!=WEIRDINT ) |
25a3f430 |
172 | #define for_strs(VAR,...) _NEW(I,0) while( (VAR=(char *)_cll1_for_ptrs(_I++,__VA_ARGS__,NIL))!=NIL ) |
96df7894 |
173 | #define for_args(STR) _NEW(I,1) while(_I++<argc) |
8a93c4ed |
174 | #define this_arg argv[_I] |
420bb771 |
175 | #define next_arg ((_I+1<argc)?argv[_I]:NULL) |
96df7894 |
176 | #define continue_args _I++ |
61ad8a2b |
177 | #define if_exists(NODE,HEAD,EXPR) for_search(NODE,HEAD,EXPR) break; if(NODE) |
bce4b05d |
178 | #define if_args if(argc>1) |
eac5d6c7 |
179 | #define switch_for_args for_args with_str(this_arg) |
180 | #define with_str(STR) _NEW(S,STR) |
181 | #define if_case(VAL) if(eq(_S,VAL)) |
25a3f430 |
182 | #define for_fields(FIELD,STR) for_valid_split(FIELD,STR,*OFS) |
bce4b05d |
183 | #define for_split_str(A,B,C) for(prefix(A,B,C);A;(A!=B)&&prefix(A,B,C)||(A=NULL)) |
184 | #define for_valid_split(A,B,C) for_split_str(A,B,C) if(*A) |
185 | #define switch_for_columns(A,B,C) _FOR_COLUMNS(A,B,C) switch(__I) |
186 | #define switch_for_valid_columns(A,B,C) _FOR_VALID_COLUMNS(A,B,C) switch (_I) |
187 | #define if_column_number(A,B,C,V) _FOR_COLUMNS(A,B,C) if(_I==V) |
188 | #define if_valid_column_number(A,B,C,V) _FOR_VALID_COLUMNS(A,B,C) if(_I==V) |
005431b8 |
189 | /* .----------------------------------------------------------------------. |
bc44f2cc |
190 | / 8. C<<1 object oriented infrastructure, updated 2008-02-04 |
8db501a0 |
191 | '----------------------------------------------------------------------- */ |
9abd9073 |
192 | #define def_interface(TYPE,INTERFACE) def_type(TYPE); def_mem(INTERFACE) |
193 | #define def_obj(TYPE) struct _##TYPE##_struct |
96ec74e5 |
194 | #define interface(TYPE) TYPE __interface |
195 | #define interface_of(SELF) SELF->__interface |
b27abb8a |
196 | #define method(NAME) (*NAME) |
24cfa6bd |
197 | #define construct(TYPE,INTERFACE) TYPE _init_##TYPE(TYPE o,INTERFACE i) { o->__interface=i; return o; } TYPE __init__##TYPE |
1a8d78d9 |
198 | #define get_obj(TYPE,INTERFACE,...) __init__##TYPE(_init_##TYPE(get_mem(TYPE),INTERFACE),__VA_ARGS__) |
96ec74e5 |
199 | #define get_obj_as(COMMUNITY,MEM,INTERFACE,...) (COMMUNITY)get_obj(MEM,INTERFACE,__VA_ARGS__) |
eeff2724 |
200 | #define _(METHOD,...) ((((_ARG1(__VA_ARGS__,)) || /* will not compile if object is not lvalue: */(_ARG1(__VA_ARGS__,)=NULL)) && (_ARG1(__VA_ARGS__,))->__interface->METHOD)?(*( (_ARG1(__VA_ARGS__,))->__interface->METHOD )) ((void *)__VA_ARGS__):0) |
eac5d6c7 |
201 | //#define __(METHOD,...) ( _ASSERT(ARG1(__VA_ARGS__,)), (*( __cll1_seekmethod(ARG1(__VA_ARGS__,),METHOD) )) ( __VA_ARGS__ ) ) |
47e4883a |
202 | #define call(OBJTYPE,METHOD,...) { int _N=0; OBJTYPE _OBJ; /* <-' */ while( (_OBJ=(OBJTYPE)_cll1_for_ptrs(_N++,__VA_ARGS__,NIL))!=(OBJTYPE)NIL ) _(_OBJ,METHOD); } |
96ec74e5 |
203 | #define def_community(TYPE,INTERFACE) def_obj(TYPE) { INTERFACE __interface; }; |
eac5d6c7 |
204 | /* ---- variable declarators -------------------------------------------- */ |
24cfa6bd |
205 | #define Get_obj(ID,MEM,...) MEM ID=get_obj(MEM,__VA_ARGS__) |
206 | #define Get_obj_as(ID,COMMUNITY,MEM,...) MEM ID=get_obj_as(COMMUNITY,MEM,INTERFACE,__VA_ARGS__) |
e0467a3b |
207 | #define I_am(SELF,MEM) MEM SELF=(MEM)community |
8db501a0 |
208 | /* .----------------------------------------------------------------------. |
7e4d7575 |
209 | / 9. C<<1 setjmp()/longjmp() based exceptions, updated 2008-09-30 |
210 | '----------------------------------------------------------------------- */ |
eac5d6c7 |
211 | #define FAILED_TO_FAIL -273 |
212 | #define SYSTEM_FAILURE -1 |
213 | #define LOCAL_FAILURE 13 |
214 | #define LIBRARY_FAILURE 23 |
215 | #define EVIL_FAILURE 666 |
49df11df |
216 | #define die(MSG) longjmp(_EENV,(errstr=MSG,LOCAL_FAILURE)) |
eac5d6c7 |
217 | #define try _NEW(E,0) if(!(_E=setjmp(_EENV))) |
218 | #define fail(E) longjmp(_EENV,((E)>0?(E):FAILED_TO_FAIL)) |
219 | #define pass_failure (_Eatom->__next?longjmp((pop(_Eatom,_Estack),_EENV),_E):_SYSFAIL(FAILED_TO_FAIL)) |
220 | #define else_switch_failures else switch(_E) |
221 | #define else_catch(E) else if (_E==(E)) |
7e4d7575 |
222 | /* .----------------------------------------------------------------------. |
223 | / 10. implementation of C<<1 library functions, updated 2008-01-26 xCh. |
f5725936 |
224 | '----------------------------------------------------------------------- */ |
96ec74e5 |
225 | /* later... #define _MALLOC void *_cll1_malloc(size_t size) { return malloc(size); } */ |
bce4b05d |
226 | #define _CLL1_FPRINT(IOSTREAM) { int ofs=0; va_list ap; /* <-' */ for(va_start(ap,s);s!=NIL;s=va_arg(ap,char *)) { if(ofs)fputs(OFS,IOSTREAM);else ofs=1;/**/fputs(coalesce(s,NIL),IOSTREAM); } va_end(ap); fputs(EOL,IOSTREAM); } |
eb0d8f8c |
227 | #define _DECLARE_PRINT void _cll1_print(const char *s,...) |
4643aeaf |
228 | #define _PRINT _DECLARE_PRINT _CLL1_FPRINT(stdout) |
eb0d8f8c |
229 | #define _DECLARE_FPRINT void _cll1_fprint(FILE *f,const char *s,...) |
4643aeaf |
230 | #define _FPRINT _DECLARE_FPRINT _CLL1_FPRINT(f) |
bce4b05d |
231 | #define _CLL1_FECHO(IOSTREAM) { va_list ap; /* <-' */ for(va_start(ap,s);s!=NIL;s=va_arg(ap,char *)) fputs(coalesce(s,NIL),IOSTREAM); va_end(ap); } |
232 | #define _ECHO void _cll1_echo(char *s,...) _CLL1_FECHO(stdout) |
233 | #define _FECHO void _cll1_fecho(FILE *f,char *s,...) _CLL1_FECHO(f) |
9ae3b221 |
234 | #define _FOR_INTS int _cll1_for_ints(int i, int n, ...) { int j=0;va_list ap; /* <-' */ va_start(ap,n); while(n!=WEIRDINT && j++<i) n=va_arg(ap,int); return n; } |
bce4b05d |
235 | #define _FOR_PTRS void *_cll1_for_ptrs(int i, void *v, ...) { int j=0;va_list ap; /* <-' */ va_start(ap,v); while(v!=(void *)NIL && j++<i) v=va_arg(ap,void *); return v; } |
bc44f2cc |
236 | #define _DECLARE_STRCOLL int _cll1_str_coll(str a, str b) |
237 | #define _STRCOLL _DECLARE_STRCOLL { if(a && b) return strcoll(a,b); else return (int)(a-b); } |
238 | #define _DECLARE_STRCMP int _cll1_str_cmp(str a, str b) |
239 | #define _STRCMP _DECLARE_STRCMP { if(a && b) return strcmp(a,b); else return (int)(a-b); } |
240 | #define _DECLARE_STRCMPI int _cll1_str_cmpi(str a, str b) |
241 | #define _STRCMPI _DECLARE_STRCMPI { if(a && b) return strcasecmp(a,b); else return (int)(a-b); } |
242 | #define _DECLARE_STRSTR str _cll1_str_str(str h, str n) |
243 | #define _STRSTR _DECLARE_STRSTR { if(h && n) return strstr(h,n); else return NULL; } |
96ec74e5 |
244 | // later... #define _WORDINSTR char *_cll1_wordinstr(char *a,char *b,int ign) { char *IFS=a; for(;a&&*a;a++) for(;*ifs||ifs=IFS;ifs++) if(*a=b);) for return strcasestr(a,b); else return NULL; } |
4643aeaf |
245 | extern _DECLARE_PRINT; |
246 | extern _DECLARE_FPRINT; |
bc44f2cc |
247 | extern _DECLARE_STRCOLL; |
248 | extern _DECLARE_STRCMP; |
249 | extern _DECLARE_STRCMPI; |
250 | extern _DECLARE_STRSTR; |
4643aeaf |
251 | #define _EXPORT_OUTS _PRINT _FPRINT _ECHO _FECHO |
bc44f2cc |
252 | #define _EXPORT_STRS _STRCOLL _STRCMP _STRCMPI _STRSTR //_WORDINSTR |
4643aeaf |
253 | #define _EXPORT_ARGS _FOR_INTS _FOR_PTRS |
005431b8 |
254 | /* .----------------------------------------------------------------------. |
4643aeaf |
255 | / 10. C<<1 private infrastructure, updated 2008-02-03 by xCh. |
f5725936 |
256 | '----------------------------------------------------------------------- */ |
bce4b05d |
257 | #define _I (_Iatom->I) |
eac5d6c7 |
258 | #define _J (_Iatom->J) |
bce4b05d |
259 | #define _S (_Satom->S) |
4643aeaf |
260 | #define _F (_Fatom->F) |
bce4b05d |
261 | #define _CTX (_CTXatom->CTX) |
eac5d6c7 |
262 | #define _E (_Eatom->E) |
263 | #define _EENV (_Eatom->ENV) |
49df11df |
264 | #define _ERRNO (_Eatom->ERRNO) |
4643aeaf |
265 | #define _ARG1(ARG1,...) ARG1 |
eac5d6c7 |
266 | //#define _ASSERT(EXPR) (expr?expr:fail(CONSISTENCY_FAILURE)) |
49df11df |
267 | #define _SYSFAIL(E) longjmp(_EENV,(_ERRNO=errno,(E))) |
9abd9073 |
268 | #define _GET_MEM(TYPE) ((TYPE)malloc(sizeof(struct _##TYPE##_struct))) /* later... ((TYPE)_cll1_malloc(sizeof(struct _##TYPE##_struct))) */ |
eac5d6c7 |
269 | #define _ANYATOM(NAME,DECL) def_mem(_##NAME##Atom ) { DECL; int c; list( _##NAME##Atom ); } |
270 | //#define _FOR_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C)||(A=NULL) ),_I++ ) |
271 | //#define _FOR_VALID_COLUMNS(A,B,C) _NEW(I,0) for( prefix(A,B,C) ; A ; ((A!=B)&&prefix(A,B,C))||(A=NULL) ) if(*A&&++_I) |
4643aeaf |
272 | #define _NEW(NAME,VALUE) for(_##NAME##atom=get_mem(_##NAME##Atom),_##NAME##atom->NAME=(VALUE),_##NAME##atom->c=1,push(_##NAME##atom,_##NAME##stack);_##NAME##atom->c||(pop(_##NAME##atom,_##NAME##stack),0);_##NAME##atom->c--) |
99cd47b1 |
273 | #define _ANYSTACK(NAME) extern _##NAME##Atom _##NAME##atom, _##NAME##stack; _##NAME##Atom _##NAME##atom,_##NAME##stack=NULL; |
eac5d6c7 |
274 | #define _EXPORT_STACKS _ANYSTACK(I) _ANYSTACK(S) _ANYSTACK(F) _ANYSTACK(CTX) _ANYSTACK(E) |
275 | _ANYATOM(I, int I;int J); |
276 | _ANYATOM(S, str S); |
277 | _ANYATOM(F, file F); |
278 | _ANYATOM(CTX, void *CTX); |
49df11df |
279 | _ANYATOM(E, int E;jmp_buf ENV;int ERRNO); |
4643aeaf |
280 | /* .----------------------------------------------------------------------. |
281 | / 11. C<<1 memory contexts - alternative to garbage collection |
282 | '----------------------------------------------------------------------- */ |
283 | typedef _CTXAtom * context; |
284 | #define create_context(ID) create_mem(ID,context) |
005431b8 |
285 | /* .----------------------------------------------------------------------. |
4643aeaf |
286 | / 12. C<<1 CGI and FASTCGI handling, updated 2008-01-16 by xCh. |
f5725936 |
287 | '----------------------------------------------------------------------- */ |
04a13a6f |
288 | #ifdef USE_LIBRARY_FASTCGI |
289 | #define while_fastcgi_accept while(FCGI_Accept()>=0) |
7f13bdad |
290 | #define USE_MODULE_CGI |
291 | #endif |
292 | #ifdef USE_MODULE_CGI |
bce4b05d |
293 | #define cgi(MIME) { char *PTR; /* <-' */ recycle_context(_CGI); _CGIdict=NULL; for_split_str(PTR,getenv("QUERY_STRING"),'&') /* _init_cgi */ if(MIME) printf("Content-type: %s\r\n\r\n",MIME); } |
547332b7 |
294 | #define _GETCGI char *getcgi(VAR) { } |
b27abb8a |
295 | #define _CGI_GLOBALS def_context(_CGI); struct _CGIVariable { char *V; dict(_CGIVariable); } *_CGIvariable,*_CGIdict; |
8db501a0 |
296 | #else |
547332b7 |
297 | #define _GETCGI |
bce4b05d |
298 | #define _CGI_GLOBALS |
04a13a6f |
299 | #endif |
005431b8 |
300 | /* .----------------------------------------------------------------------. |
4643aeaf |
301 | / 13. MySQL wrappers + SQL object interface, updated 2007-12-19 by xCh. |
f5725936 |
302 | '----------------------------------------------------------------------- */ |
7f13bdad |
303 | #ifdef USE_LIBRARY_MYSQL |
25a3f430 |
304 | #define def_sql(CONN) MYSQL *CONN |
bce4b05d |
305 | #define create_sql(CONN) MYSQL *ID=mysql_init(NULL) |
306 | #define sql_connect(CONN,S,U,P,D) mysql_real_connect(CONN,S,U,P,D,0,NULL,0) |
307 | //todo sql_query |
308 | //todo sql_queryf |
309 | //todo for_sql_query_results(ROW,CONN,QUERY) |
310 | //todo for_sql_queryf_results |
311 | #define if_db_connect(U,P,D) _NEW(SQLC,mysql_init(NULL)) for(;_SQLC && mysql_real_connect(_SQLC,"localhost",U,P,D,0,NULL,0);mysql_close(SQLC),SQLC=NULL) |
312 | #define for_db_query_results(ROW,QUERY) _NEW(MYSQLR,mysql_query(_MYSQLC,QUERY)) for( (_SQLR=(_MYSQLR?0:mysql_use_result(_MYSQLC))),(ROW=(_MYSQLR?mysql_fetch_row(_MYSQLR):NULL)) ; ROW ; ROW=mysql_fetch_row(_MYSQLR) ) |
313 | //todo db_query |
314 | //todo for_db_results |
315 | //todo for_db_queryf_results(ROW,...) |
316 | //todo db_queryf |
317 | #define _MYSQLC (_MYSQLCatom->MYSQLC) |
318 | #define _MYSQLR (_MYSQLRatom->MYSQLR) |
319 | #define _SQL_GLOBALS _ANYSTACK(MYSQL,MYSQLC) _ANYSTACK(MYSQL_RES,MYSQLR) |
7f13bdad |
320 | #else |
bce4b05d |
321 | #define _SQL_GLOBALS |
7f13bdad |
322 | #endif |
bce4b05d |
323 | //#ifdef USE_INTERFACE_SQL |
324 | //todo #define _SQL_OBJECT mem Sql { interface(SqlInterface); char *hostname; char *username; char *password; char *database; } |
25a3f430 |
325 | //todo #define _SQL_CONSTRUCT def_construct(Sql) { self->hostname="localhost"; } |
bce4b05d |
326 | //#endif |
4643aeaf |
327 | |
04a13a6f |
328 | /* ------------------------------------------------------------------- */ |
7f13bdad |
329 | /* old - from version 0.6 */ |
f5725936 |
330 | /* =================================================================== */ |
7f13bdad |
331 | /* |
332 | obsolete |
30628092 |
333 | #define strlwr(A) {char *_S=A; while(_&&*_S){*_S=tolower(*_S);_S++;}} |
334 | #define strupr(A) {char *_S=A; while(_&&*_S){*_S=toupper(*_S);_S++;}} |
335 | #define string(S,L) (S=(char *)malloc(L),*S=0) |
336 | #define duplicate(A,B) if(A) { string(B,strlen(A)+1); strcpy(B,A); } |
337 | #define concatenate(A,B,C) if (A && B) { string(C,strlen(A)+strlen(B)+1); strcpy(C,A); strcat(C,B); } |
30628092 |
338 | #define gotoalpha(CHAR) if(CHAR)while(*CHAR && !isalpha(*CHAR))CHAR++ |
339 | #define goto_alpha(CHAR) if(CHAR)while(*CHAR && !isalpha(*CHAR) && *CHAR!='_')CHAR++ |
340 | #define gotoalnum(CHAR) if(CHAR)while(*CHAR && !isalnum(*CHAR))CHAR++ |
341 | #define goto_alnum(CHAR) if(CHAR)while(*CHAR && !isalnum(*CHAR) && *CHAR!='_')CHAR++ |
342 | #define skipalpha(CHAR) if(CHAR)while(*CHAR && isalpha(*CHAR))CHAR++ |
343 | #define skip_alpha(CHAR) if(CHAR)while(*CHAR && (isalpha(*CHAR) || *CHAR=='_'))CHAR++ |
344 | #define skipalnum(CHAR) if(CHAR)while(*CHAR && isalnum(*CHAR))CHAR++ |
345 | #define skip_alnum(CHAR) if(CHAR)while(*CHAR && (isalnum(*CHAR) || *CHAR=='_'))CHAR++ |
346 | #define skipspaces(CHAR) if(CHAR)while(*CHAR==' ')CHAR++ |
347 | #define cutspaces(CHAR) if(CHAR){int _L=strlen(CHAR); while(--_L>0 && CHAR[_L]==' ')CHAR[_L]=0;} |
348 | #define gotochr(CHAR,C) if(CHAR)while(*CHAR && *CHAR!=C)CHAR++ |
30628092 |
349 | #define strswitch(CHAR) {char *_K=CHAR; FILE *_F=NULL; {{ |
350 | #define stroption(STR) if(eq(STR,_K)) |
351 | #define match(KEY,VAL) {char *_K=KEY, *_V=VAL; FILE *_F=NULL; {{ |
352 | #define assign(STR,SETVAR) stroption(STR) SETVAR=_V |
7f13bdad |
353 | */ |
30628092 |
354 | /* I/O iterations, updated 2004-04-19 by xCh. */ |
355 | |
bce4b05d |
356 | //#define fparse(S,L,F) for(fgets(S,L,F);*S && !feof(F);fgets(S,L,F)) |
357 | //#define input(S,L) fparse(S,L,stdin) |
358 | //#define fstring(S,F) { int _C=0,_L=0; fpos_t _P; fgetpos(F,&_P); while(_C!='\n' && !feof(F)){ _C=fgetc(F); _L++; } string(S,_L); fsetpos(F,&_P);fgets(S,_L,F);fgetc(F);} |
359 | //#define parses(S,F) {FILE *_F=fopen(F,"r"); if(_F) { while(!feof(_F)) { fstring(S,_F); |
f5725936 |
360 | //obsolete |
361 | //#define parse(F) {char *_; FILE *_F=fopen(F,"r"); if(_F) { while(!feof(_F)) { fstring(_,_F); |
362 | //#define fail }} else {{ |
363 | //#define done }} if(_F)fclose(_F);} |
005431b8 |
364 | //to do #define option(STR,SETVAR) if(_){char *_K,*_V,*_O,*_Q; duplicate(_,_Q); _O=_Q; tr(_O,'\t',' '); prefix(_K,_O,' '); if(eq(STR,_K)) {skipspaces(_O); prefix(_V,_O,'#'); cutspaces(_V); SETVAR=_V; _=NULL;} else free(_Q);} |
365 | //to do #define ioption(STR,SETVAR) if(_){char *_K,*_V,*_O,*_Q; duplicate(_,_Q); _O=_Q; tr(_O,'\t',' '); prefix(_K,_O,' '); if(eq(STR,_K)) {skipspaces(_O); prefix(_V,_O,'#'); cutspaces(_V); SETVAR=atoi(_V); _=NULL;} free(_Q);} |
366 | //to do #define loption(STR,SETVAR) if(_){char *_K,*_V,*_O,*_Q; duplicate(_,_Q); _O=_Q; tr(_O,'\t',' '); prefix(_K,_O,' '); if(eq(STR,_K)) {skipspaces(_O); prefix(_V,_O,'#'); cutspaces(_V); SETVAR=atol(_V); _=NULL;} free(_Q);} |
30628092 |
367 | |
30628092 |
368 | /* I/O sequences, updated 2003-05-29 by xCh. */ |
369 | |
370 | #define nullreopen(F) F=freopen("/dev/null","r",F) |
371 | #define stdinredir(CMD) {int _r[2];pipe(_r);if(fork()==0){dup2(_r[1],1);close(_r[0]);CMD;exit(0);}nullreopen(stdin);dup2(_r[0],0);close(_r[1]);} |
372 | #define shell(CMD) stdinredir(system(CMD)) |
373 | #define paste(STR) stdinredir(fputs(STR,stdout)) |
374 | |
30628092 |
375 | /* Useful structures, updated 2003-05-29 by xCh. */ |
376 | |
f5725936 |
377 | //#define date(S) { time_t _T; _T=time(NULL); duplicate(ctime(&_T),S); } |
4643aeaf |
378 | |
097bac9a |
379 | /* some general ideas, unfinished */ |
f5725936 |
380 | //bind_exception, unbind_exception |
7f13bdad |
381 | //#define _CLL1_THROW(ERR) |
f5725936 |
382 | //#define _CLL1_TRY _NEW_E for(bind_exception(0,NULL);;) if () {.... if(_Eatom->SYS) { if(_Eatom->RET) return; else break;} else { } |
47e4883a |
383 | |
005431b8 |
384 | /* .----------------------------------------------------------------------. |
ce26b4aa |
385 | / 98. We have have done our homework - now let's have some fun: |
f5725936 |
386 | '----------------------------------------------------------------------- */ |
4643aeaf |
387 | #define _INIT_VARIABLES _EXPORT_GLOBALS _EXPORT_YES _EXPORT_STACKS _SQL_GLOBALS _CGI_GLOBALS |
47e4883a |
388 | #define _IMPL_FUNCTIONS /*_MALLOC*/ _EXPORT_OUTS _EXPORT_ARGS _EXPORT_STRS |
4643aeaf |
389 | #define _IMPL_INTERFACE //_SQL_IFC _CAKE_IFC _STD_IFC _X_IFC |
390 | #define _IMPLEMENT_CLL1_LIBRARY _INIT_VARIABLES _IMPL_FUNCTIONS _IMPL_INTERFACE |
eac5d6c7 |
391 | //this will be probably accomplished some other way... |
99cd47b1 |
392 | #ifndef DISABLE_LIBRARY_CLL1 |
eac5d6c7 |
393 | _IMPLEMENT_CLL1_LIBRARY |
99cd47b1 |
394 | #endif |
49df11df |
395 | #ifdef USE_LC_ALL |
396 | #define _UNSET_LC_NUMERIC setlocale(LC_NUMERIC, "C"); /* LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, LC_TIME */ |
99cd47b1 |
397 | #else |
49df11df |
398 | #define _UNSET_LC_NUMERIC |
99cd47b1 |
399 | #endif |
ce26b4aa |
400 | #define _DECLARE_PROGRAM void _PROGRAM(int argc, char **argv) |
99cd47b1 |
401 | #define _PROGRAM _cll1_program |
eac5d6c7 |
402 | #define program \ |
403 | _DECLARE_PROGRAM; \ |
404 | int main(int argc, char **argv) \ |
405 | { \ |
49df11df |
406 | LANG=setlocale(LC_ALL, ""); \ |
407 | _UNSET_LC_NUMERIC \ |
eac5d6c7 |
408 | try \ |
409 | { \ |
410 | _PROGRAM(argc,argv); \ |
411 | return 0; \ |
412 | } \ |
99cd47b1 |
413 | else_switch_failures \ |
414 | { \ |
415 | case SYSTEM_FAILURE: \ |
49df11df |
416 | errno=_ERRNO; \ |
99cd47b1 |
417 | perror(errstr); /* and return -1 */ \ |
418 | default: \ |
419 | return -1; \ |
420 | } \ |
eac5d6c7 |
421 | } \ |
422 | _DECLARE_PROGRAM |
f5725936 |
423 | #ifdef USE_PREPROCESSOR |
424 | #include "_include.c" |
425 | #endif |
ce26b4aa |
426 | /* .----------------------------------------------------------------------. |
427 | / 99. C<<1 is up and running. It's your turn now to show something new. |
428 | '----------------------------------------------------------------------- */ |
420bb771 |
429 | #endif |