nabaztag_hack_kit 0.1.0.beta3 → 0.1.0.beta8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -2
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +2 -2
  5. data/CHANGELOG.md +17 -2
  6. data/LICENSE +21 -0
  7. data/README.md +115 -44
  8. data/Rakefile +2 -6
  9. data/bytecode/{Commands.md → _docs/commands.md} +0 -0
  10. data/bytecode/{Docs.md → _docs/grammar.md} +0 -0
  11. data/bytecode/_docs/grammar.pdf +0 -0
  12. data/bytecode/{src → _original}/README.md +0 -0
  13. data/bytecode/{src → _original}/lib/arp.mtl +0 -0
  14. data/bytecode/{src → _original}/lib/cfg.mtl +0 -0
  15. data/bytecode/{src → _original}/lib/choreos.mtl +0 -0
  16. data/bytecode/{src → _original}/lib/dhcp.mtl +0 -0
  17. data/bytecode/{src → _original}/lib/dns.mtl +0 -0
  18. data/bytecode/{src → _original}/lib/http.mtl +0 -0
  19. data/bytecode/{src → _original}/lib/tcp.mtl +0 -0
  20. data/bytecode/{src → _original}/lib/udp.mtl +0 -0
  21. data/bytecode/{src → _original}/lib/util.mtl +0 -0
  22. data/bytecode/{src → _original}/lib/var.mtl +0 -0
  23. data/bytecode/{src → _original}/lib/wav.mtl +0 -0
  24. data/bytecode/{src → _original}/lib/wifi.mtl +0 -0
  25. data/bytecode/{src → _original}/nominal-ping.mtl +0 -0
  26. data/bytecode/main.mtl +11 -11
  27. data/bytecode/test/test.mtl +8 -8
  28. data/examples/basic/Gemfile +1 -3
  29. data/examples/basic/config.ru +14 -4
  30. data/examples/record/Gemfile +0 -5
  31. data/examples/record/{config.ru.example → config.ru} +2 -2
  32. data/examples/record/server.rb +0 -2
  33. data/ext/Makefile +10 -0
  34. data/ext/bin/mtl_comp +29 -0
  35. data/{bytecode → ext}/bin/mtl_merge +0 -0
  36. data/ext/bin/mtl_simu +29 -0
  37. data/{bytecode/src/mtl → ext}/extconf.rb +0 -0
  38. data/ext/mtl_linux/.gitignore +7 -0
  39. data/ext/mtl_linux/LICENSE +21 -0
  40. data/ext/mtl_linux/Makefile +116 -0
  41. data/ext/mtl_linux/MetalC.project +196 -0
  42. data/ext/mtl_linux/conf.bin +0 -0
  43. data/{bytecode/src/mtl → ext/mtl_linux}/conf.bin.sans_password +0 -0
  44. data/{bytecode/src/mtl → ext/mtl_linux}/config.txt +0 -0
  45. data/ext/mtl_linux/inc/log.h +21 -0
  46. data/{bytecode/src/mtl → ext/mtl_linux/inc}/properties.h +0 -0
  47. data/{bytecode/src/mtl/linux_simuaudio.h → ext/mtl_linux/inc/simu/linux/simuaudio.h} +0 -0
  48. data/{bytecode/src/mtl/linux_simunet.h → ext/mtl_linux/inc/simu/linux/simunet.h} +0 -0
  49. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simuaudio.h +18 -18
  50. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simunet.h +0 -0
  51. data/ext/mtl_linux/inc/vcomp/compiler.h +200 -0
  52. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/file.h +11 -11
  53. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/filesystem.h +0 -0
  54. data/ext/mtl_linux/inc/vcomp/interpreter.h +121 -0
  55. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/memory.h +120 -92
  56. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/param.h +0 -0
  57. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/parser.h +27 -27
  58. data/ext/mtl_linux/inc/vcomp/prodbuffer.h +42 -0
  59. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/resource.h +1 -1
  60. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/terminal.h +8 -8
  61. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/util.h +7 -7
  62. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vaudio.h +0 -0
  63. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vbc.h +1 -1
  64. data/ext/mtl_linux/inc/vm/vbc_str.h +167 -0
  65. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vinterp.h +0 -0
  66. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vloader.h +0 -0
  67. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vlog.h +0 -0
  68. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vmem.h +0 -0
  69. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vnet.h +0 -0
  70. data/ext/mtl_linux/nominal.mtl +3959 -0
  71. data/ext/mtl_linux/src/comp/main.cpp +123 -0
  72. data/ext/mtl_linux/src/simu/dumpbc.c +2566 -0
  73. data/ext/mtl_linux/src/simu/linux/main.cpp +288 -0
  74. data/ext/mtl_linux/src/simu/linux/simu.c +309 -0
  75. data/ext/mtl_linux/src/simu/linux/simuaudio.c +44 -0
  76. data/ext/mtl_linux/src/simu/linux/simunet.c +707 -0
  77. data/ext/mtl_linux/src/simu/log.c +340 -0
  78. data/ext/mtl_linux/src/simu/properties.c +368 -0
  79. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/GTKANAL.H +0 -0
  80. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/LAYER3.C +0 -0
  81. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/TABINIT.C +0 -0
  82. data/ext/mtl_linux/src/simu/win/mp3/common.c +302 -0
  83. data/ext/mtl_linux/src/simu/win/mp3/dct64_i386.c +316 -0
  84. data/ext/mtl_linux/src/simu/win/mp3/decode_i386.c +160 -0
  85. data/ext/mtl_linux/src/simu/win/mp3/huffman.h +332 -0
  86. data/ext/mtl_linux/src/simu/win/mp3/interface.c +276 -0
  87. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/mpg123.h +34 -30
  88. data/ext/mtl_linux/src/simu/win/mp3/mpglib.h +47 -0
  89. data/ext/mtl_linux/src/simu/win/simu.c +819 -0
  90. data/ext/mtl_linux/src/simu/win/simuaudio.c +787 -0
  91. data/ext/mtl_linux/src/simu/win/simunet.c +489 -0
  92. data/ext/mtl_linux/src/vcomp/bootstrap.cpp +95 -0
  93. data/ext/mtl_linux/src/vcomp/compiler.cpp +587 -0
  94. data/ext/mtl_linux/src/vcomp/compiler_file.cpp +1253 -0
  95. data/ext/mtl_linux/src/vcomp/compiler_prog.cpp +437 -0
  96. data/ext/mtl_linux/src/vcomp/compiler_term.cpp +1500 -0
  97. data/ext/mtl_linux/src/vcomp/compiler_type.cpp +1238 -0
  98. data/ext/mtl_linux/src/vcomp/compiler_var.cpp +502 -0
  99. data/ext/mtl_linux/src/vcomp/file.cpp +100 -0
  100. data/ext/mtl_linux/src/vcomp/interpreter.cpp +93 -0
  101. data/ext/mtl_linux/src/vcomp/memory.cpp +313 -0
  102. data/ext/mtl_linux/src/vcomp/parser.cpp +584 -0
  103. data/ext/mtl_linux/src/vcomp/parser_xml.cpp +137 -0
  104. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/prodbuffer.cpp +57 -36
  105. data/ext/mtl_linux/src/vcomp/stdlib_core.cpp +130 -0
  106. data/ext/mtl_linux/src/vcomp/terminal.cpp +80 -0
  107. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/util.cpp +18 -11
  108. data/ext/mtl_linux/src/vm/vaudio.c +830 -0
  109. data/ext/mtl_linux/src/vm/vinterp.c +1780 -0
  110. data/ext/mtl_linux/src/vm/vloader.c +139 -0
  111. data/ext/mtl_linux/src/vm/vlog.c +782 -0
  112. data/ext/mtl_linux/src/vm/vmem.c +461 -0
  113. data/ext/mtl_linux/src/vm/vnet.c +298 -0
  114. data/{bytecode/src/mtl → ext/mtl_linux}/utils/correct_const.sh +0 -0
  115. data/lib/nabaztag_hack_kit/server.rb +4 -7
  116. data/lib/nabaztag_hack_kit/version.rb +1 -1
  117. data/lib/nabaztag_hack_kit.rb +2 -2
  118. data/nabaztag_hack_kit.gemspec +10 -11
  119. data/spec/integration/server_spec.rb +3 -3
  120. data/spec/spec_helper.rb +2 -0
  121. data/spec/unit/message_spec.rb +12 -18
  122. data/spec/unit/server_spec.rb +13 -10
  123. metadata +145 -137
  124. data/bytecode/bin/mtl_comp +0 -50
  125. data/bytecode/bin/mtl_simu +0 -49
  126. data/bytecode/src/mtl/Makefile +0 -42
  127. data/bytecode/src/mtl/README.md +0 -13
  128. data/bytecode/src/mtl/bc.cpp +0 -1891
  129. data/bytecode/src/mtl/dumpbc.c +0 -2566
  130. data/bytecode/src/mtl/linux_simu.c +0 -271
  131. data/bytecode/src/mtl/linux_simuaudio.c +0 -16
  132. data/bytecode/src/mtl/linux_simunet.c +0 -620
  133. data/bytecode/src/mtl/log.c +0 -297
  134. data/bytecode/src/mtl/log.h +0 -20
  135. data/bytecode/src/mtl/main_compiler.cpp +0 -104
  136. data/bytecode/src/mtl/main_simu.cpp +0 -221
  137. data/bytecode/src/mtl/mp3/common.c +0 -265
  138. data/bytecode/src/mtl/mp3/dct64_i386.c +0 -316
  139. data/bytecode/src/mtl/mp3/decode_i386.c +0 -155
  140. data/bytecode/src/mtl/mp3/huffman.h +0 -332
  141. data/bytecode/src/mtl/mp3/interface.c +0 -258
  142. data/bytecode/src/mtl/mp3/mpglib.h +0 -44
  143. data/bytecode/src/mtl/properties.c +0 -293
  144. data/bytecode/src/mtl/simu.c +0 -750
  145. data/bytecode/src/mtl/simuaudio.c +0 -662
  146. data/bytecode/src/mtl/simunet.c +0 -400
  147. data/bytecode/src/mtl/vaudio.c +0 -677
  148. data/bytecode/src/mtl/vbc_str.h +0 -166
  149. data/bytecode/src/mtl/vcomp/Makefile +0 -29
  150. data/bytecode/src/mtl/vcomp/bootstrap.cpp +0 -89
  151. data/bytecode/src/mtl/vcomp/compiler.cpp +0 -470
  152. data/bytecode/src/mtl/vcomp/compiler.h +0 -200
  153. data/bytecode/src/mtl/vcomp/compiler_file.cpp +0 -929
  154. data/bytecode/src/mtl/vcomp/compiler_prog.cpp +0 -250
  155. data/bytecode/src/mtl/vcomp/compiler_term.cpp +0 -1053
  156. data/bytecode/src/mtl/vcomp/compiler_type.cpp +0 -872
  157. data/bytecode/src/mtl/vcomp/compiler_var.cpp +0 -289
  158. data/bytecode/src/mtl/vcomp/file.cpp +0 -79
  159. data/bytecode/src/mtl/vcomp/interpreter.cpp +0 -85
  160. data/bytecode/src/mtl/vcomp/interpreter.h +0 -121
  161. data/bytecode/src/mtl/vcomp/memory.cpp +0 -241
  162. data/bytecode/src/mtl/vcomp/parser.cpp +0 -427
  163. data/bytecode/src/mtl/vcomp/parser_xml.cpp +0 -124
  164. data/bytecode/src/mtl/vcomp/prodbuffer.h +0 -42
  165. data/bytecode/src/mtl/vcomp/stdlib_core.cpp +0 -122
  166. data/bytecode/src/mtl/vcomp/terminal.cpp +0 -73
  167. data/bytecode/src/mtl/vinterp.c +0 -1349
  168. data/bytecode/src/mtl/vloader.c +0 -127
  169. data/bytecode/src/mtl/vlog.c +0 -589
  170. data/bytecode/src/mtl/vmem.c +0 -424
  171. data/bytecode/src/mtl/vnet.c +0 -255
  172. data/examples/basic/Procfile +0 -2
  173. data/examples/basic/server.rb +0 -14
@@ -0,0 +1,340 @@
1
+ /**
2
+ Routines simples pour pouvoir logger les sorties du compilateur
3
+ */
4
+ #include <stdlib.h>
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdarg.h>
8
+ #include <sys/stat.h>
9
+ #include <time.h>
10
+
11
+ #include "log.h"
12
+
13
+ static int my_msk = LOG_INIT | LOG_VM | LOG_SIMUNET;
14
+ #define BUFSIZE 16384
15
+ static char tmpbuf[BUFSIZE];
16
+
17
+ // siml : Stopped In the Midle of a Line. A 1 quand on a pas affiché
18
+ // tout le buffer parce qu'il ne se terminait pas par '\n'
19
+ static int siml=0;
20
+ static e_logtypes siml_t=LOG_INIT;
21
+ static int siml_idx=0;
22
+
23
+ // infos concernant le log
24
+ static FILE *outstream=NULL; // flux vers lequel on logge
25
+ static char *outfilename=NULL; // nom de fichier correspondant
26
+ static int max_log_size=0; // taille max d'un fichier de log
27
+ static int max_log_time=0; // temps max de log par fichier, en secondes
28
+ static time_t last_log_roll=0; // depuis quand on logge dans le fichier de log courant
29
+ static int current_log_size=0; // la taille courant du log
30
+ static time_t time_first_log=0; // la date du premier log enregistre
31
+ static int do_log_time=0; // si 1, on ajoute le nombre de secondes depuis le reveil du lapin au debut de chaque ligne
32
+
33
+ static void my_printf_check_log_roll(void);
34
+
35
+ /**
36
+ Renvoie, selon le type passé en argument, le préfixe à utiliser dans le log
37
+ */
38
+ static const char *get_prefix(e_logtypes t)
39
+ {
40
+ const char *res = NULL;
41
+ int i;
42
+ switch (t)
43
+ {
44
+ case LOG_INIT:
45
+ res = "init";
46
+ break;
47
+ case LOG_VM:
48
+ res = "vm";
49
+ break;
50
+ case LOG_SIMUNET:
51
+ res = "simunet";
52
+ break;
53
+ case LOG_SIMUAUDIO:
54
+ res = "simuaudio";
55
+ break;
56
+ case LOG_SIMULEDS:
57
+ res = "simuleds";
58
+ break;
59
+ case LOG_SIMUMOTORS:
60
+ res = "simumotors";
61
+ break;
62
+ }
63
+ return res;
64
+ }
65
+
66
+ /**
67
+ Affiche s en ajoutant [pfx] au début de chaque ligne.
68
+
69
+ Si force==1, on affiche même s'il n'y a pas de '\n' (et on en rajoute un)
70
+ */
71
+ static int print_line_by_line(e_logtypes t, char *s, int force)
72
+ {
73
+ char *tmp;
74
+ int nbwritten=0;
75
+ char pfx[256];
76
+ if (do_log_time)
77
+ {
78
+ snprintf(pfx,255, "%06d %s", time(NULL) - time_first_log, get_prefix(t));
79
+ }
80
+ else
81
+ {
82
+ strncpy(pfx, get_prefix(t), 255);
83
+ }
84
+ while (*s!='\0')
85
+ {
86
+ tmp = strchr(s, '\n');
87
+ if (NULL!=tmp)
88
+ {
89
+ *tmp='\0';
90
+ current_log_size += fprintf(outstream, "[%s] %s\n", pfx, s);
91
+ *tmp='\n';
92
+ nbwritten+=(tmp-s)+1;
93
+ s=tmp+1;
94
+ }
95
+ else
96
+ {
97
+ if (force)
98
+ {
99
+ current_log_size += fprintf(outstream, "[%s] %s\n", pfx, s);
100
+ nbwritten+=strlen(s);
101
+ }
102
+ break;
103
+ }
104
+ }
105
+ return nbwritten;
106
+ }
107
+
108
+ /**
109
+ Comme printf, filtré par les paramètrages du log.
110
+
111
+ t : le type de message affiché
112
+ */
113
+ void my_printf(e_logtypes t, const char *fmt, ...)
114
+ {
115
+ if (NULL==outstream)
116
+ {
117
+ outstream = stdout;
118
+ }
119
+
120
+ my_printf_check_log_roll();
121
+
122
+ if (0==time_first_log && do_log_time)
123
+ {
124
+ char tmp[256];
125
+ time_first_log = time(NULL);
126
+ snprintf(tmp, 255, "First log time: %s", ctime(&time_first_log));
127
+ print_line_by_line(LOG_INIT, tmp, 0);
128
+ }
129
+
130
+ int len, nbwritten;
131
+
132
+ if (0==((int)t & my_msk))
133
+ {
134
+ return;
135
+ }
136
+
137
+ if (siml && siml_t!=t)
138
+ {
139
+ // il faut "flusher" ce qu'il reste dans le buffer
140
+ print_line_by_line(siml_t, tmpbuf, 1);
141
+ siml=0;
142
+ siml_idx=0;
143
+ }
144
+
145
+ va_list args;
146
+ va_start(args, fmt);
147
+ len=vsnprintf(tmpbuf+siml_idx, BUFSIZE-1-siml_idx, fmt, args);
148
+ va_end(args);
149
+
150
+ // make_writeable(tmpbuf); ? s'il y a des caractères non imprimables ?
151
+ nbwritten=print_line_by_line(t, tmpbuf, 0);
152
+
153
+ len += siml_idx;
154
+ if(nbwritten<len)
155
+ {
156
+ siml=1;
157
+ siml_t=t;
158
+ siml_idx=len-nbwritten; // +siml_idx au cas ou ce n'est pas la premiere fois qu'on rajoute qqe chose dans le buffer.
159
+ if (nbwritten!=0)
160
+ // on déplace ce qu'il reste à écrire au début de tmpbuf
161
+ {
162
+ memmove(tmpbuf, tmpbuf+nbwritten, len-nbwritten);
163
+ }
164
+ }
165
+ else
166
+ {
167
+ siml=0;
168
+ siml_idx=0;
169
+ }
170
+ }
171
+
172
+ /**
173
+ Permet de contrôler les logs qu'on veut afficher (ils s'affichent
174
+ tous par défaut)
175
+ */
176
+ void my_printf_set_options(const char *s)
177
+ {
178
+ my_msk=0;
179
+
180
+ char *token, *cpy;
181
+
182
+ cpy = token = strdup(s);
183
+ token = strtok(cpy, ",");
184
+ while (NULL!=token)
185
+ {
186
+ if (!strcmp(token,"init"))
187
+ {
188
+ my_msk |= LOG_INIT;
189
+ }
190
+ else if (!strcmp(token,"vm"))
191
+ {
192
+ my_msk |= LOG_VM;
193
+ }
194
+ else if (!strcmp(token,"simunet"))
195
+ {
196
+ my_msk |= LOG_SIMUNET;
197
+ }
198
+ else if (!strcmp(token,"simuaudio"))
199
+ {
200
+ my_msk |= LOG_SIMUAUDIO;
201
+ }
202
+ else if (!strcmp(token,"simuleds"))
203
+ {
204
+ my_msk |= LOG_SIMULEDS;
205
+ }
206
+ else if (!strcmp(token,"simumotors"))
207
+ {
208
+ my_msk |= LOG_SIMUMOTORS;
209
+ }
210
+ else
211
+ {
212
+ fprintf(stderr, "erreur: %s n'est pas une option valide pour les logs\n");
213
+ break;
214
+ }
215
+ token = strtok(NULL, ",");
216
+ }
217
+ free(cpy);
218
+ }
219
+
220
+ /**
221
+ Spécifie le nom du fichier dans lequel on veut logger la sortie du lapin
222
+
223
+ retourne 0 si tout s'est bien passé, 1 sinon
224
+ */
225
+ int my_printf_set_logfile(const char *filename)
226
+ {
227
+ FILE *f = fopen(filename, "w");
228
+
229
+ if (NULL==f)
230
+ {
231
+ fprintf(stderr, "Could not open file %s to log in it.\n", filename);
232
+ return 1;
233
+ }
234
+
235
+ if (NULL!=outstream)
236
+ {
237
+ fclose(outstream);
238
+ outstream=NULL;
239
+ }
240
+ if (NULL!=outfilename)
241
+ {
242
+ free(outfilename);
243
+ outfilename=NULL;
244
+ }
245
+
246
+ outstream=f;
247
+ outfilename=strdup(filename);
248
+ current_log_size=0;
249
+ last_log_roll=time(NULL);
250
+
251
+ return 0;
252
+ }
253
+
254
+
255
+ static int my_printf_rotate_log_n(int n);
256
+
257
+ /**
258
+ Effectue la rotation des logs
259
+
260
+ - on decale tous les <log>.<n> existants en <log>.<n+1>
261
+ - le log courant devient <log>.0
262
+ - on reouvre le fichier <log> pour logger dedans
263
+ */
264
+ static int my_printf_rotate_log(void)
265
+ {
266
+ // décaler tous les logs existants et <log> en <log>.0
267
+ my_printf_rotate_log_n(-1);
268
+ char *tmp;
269
+ tmp=strdup(outfilename);
270
+ my_printf_set_logfile(tmp);
271
+ free(tmp);
272
+ }
273
+
274
+ /**
275
+ si n==-1, alors déplace <log> en <log>.0
276
+
277
+ renomme le fichier <log>.<n> en fichier <log>.<n+1>, et pareil pour
278
+ tous les suivants.
279
+
280
+ /!\ fonction réentrante
281
+ */
282
+ static int my_printf_rotate_log_n(int n)
283
+ {
284
+ char tmpn[255];
285
+ char tmpnplusun[255];
286
+ struct stat ststat;
287
+
288
+ if (n<0)
289
+ {
290
+ strcpy(tmpn, outfilename);
291
+ }
292
+ else
293
+ {
294
+ snprintf(tmpn, 255, "%s.%d", outfilename, n);
295
+ }
296
+ snprintf(tmpnplusun, 255, "%s.%d", outfilename, n+1);
297
+
298
+ if (!stat(tmpnplusun, &ststat))
299
+ {
300
+ my_printf_rotate_log_n(n+1);
301
+ }
302
+ rename(tmpn, tmpnplusun);
303
+ }
304
+
305
+ /**
306
+ Effectue la rotation des logs si besoin
307
+ */
308
+ static void my_printf_check_log_roll(void)
309
+ {
310
+ int need_roll=0;
311
+
312
+ if (0!=max_log_size)
313
+ {
314
+ need_roll += (current_log_size > max_log_size);
315
+ }
316
+ if (0!=max_log_time)
317
+ {
318
+ need_roll += (((int)time(NULL) - last_log_roll) > max_log_time);
319
+ }
320
+
321
+ if (need_roll)
322
+ {
323
+ my_printf_rotate_log();
324
+ }
325
+ }
326
+
327
+ void my_printf_set_max_log_size(int n)
328
+ {
329
+ max_log_size=n;
330
+ }
331
+
332
+ void my_printf_set_max_log_time(int n)
333
+ {
334
+ max_log_time=n;
335
+ }
336
+
337
+ void my_printf_set_do_log_time(int n)
338
+ {
339
+ do_log_time=n;
340
+ }
@@ -0,0 +1,368 @@
1
+ // VLISP Virtual Machine - 2006 - by Sylvain Huet
2
+ // Lowcost IS Powerfull
3
+
4
+ //#include"vlow.h"
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+ #include<stdio.h>
8
+ #include"vmem.h"
9
+ #include"properties.h"
10
+ #include "log.h"
11
+
12
+ struct Prop
13
+ {
14
+ char* name;
15
+ char* val;
16
+ struct Prop* nxt;
17
+ };
18
+ typedef struct Prop* prop;
19
+
20
+ char* emptystring="";
21
+ struct Prop *Properties;
22
+
23
+ // retourne le contenu d'un fichier dans un buffer
24
+ // il ne faudra pas oublier de d�sallou� le buffer, par un appel � fileReleaseBuffer
25
+ char* fileLoadL(const char *filename,int *length)
26
+ {
27
+ FILE* file;
28
+ int size;
29
+ char *buffer;
30
+
31
+ file=fopen(filename,"rb");
32
+ if (!file)
33
+ {
34
+ return NULL;
35
+ }
36
+
37
+ fseek(file,0,SEEK_END);
38
+ size=ftell(file);
39
+ fseek(file,0,SEEK_SET);
40
+
41
+ buffer=(char*)malloc(size+1);
42
+ size=fread((void*)buffer,1,size,file);
43
+ buffer[size]=0;
44
+ fclose(file);
45
+ if (length)
46
+ {
47
+ *length=size;
48
+ }
49
+ return buffer;
50
+ }
51
+
52
+ char* fileLoad(const char *filename)
53
+ {
54
+ return fileLoadL(filename,NULL);
55
+ }
56
+
57
+ // lib�re le buffer retourn� par fileLoad
58
+ void fileReleaseBuffer(char* buffer)
59
+ {
60
+ if (buffer)
61
+ {
62
+ free((void*)buffer);
63
+ }
64
+ }
65
+
66
+ // lecture d'une cha�ne hexad�cimale (s'arr�te au premier caract�re incorrect)
67
+ int htoi(const char* src)
68
+ {
69
+ int x,c;
70
+ x=0;
71
+ while(c=*src++)
72
+ {
73
+ if ((c>='0')&&(c<='9'))
74
+ {
75
+ x=(x<<4)+c-'0';
76
+ }
77
+ else if ((c>='A')&&(c<='F'))
78
+ {
79
+ x=(x<<4)+c-'A'+10;
80
+ }
81
+ else if ((c>='a')&&(c<='f'))
82
+ {
83
+ x=(x<<4)+c-'a'+10;
84
+ }
85
+ else
86
+ {
87
+ return x;
88
+ }
89
+ }
90
+ return x;
91
+ }
92
+
93
+
94
+ // alloue une cha�ne
95
+ char* stringStore(const char *src,int len)
96
+ {
97
+ char* s=(char*)malloc(len+1);
98
+ memcpy(s,src,len);
99
+ s[len]=0;
100
+ return s;
101
+ }
102
+
103
+ // d�salloue une cha�ne
104
+ void stringRelease(char *src)
105
+ {
106
+ if (src)
107
+ {
108
+ free((void*)src);
109
+ }
110
+ }
111
+
112
+
113
+ // fonction d'identification du premier mot d'une cha�ne
114
+ // ignore les espaces initiaux, et s'arr�te au premier espace ou caract�re sp�cial rencontr�
115
+ void suppspace(char* begin,int len, char **wordbegin, int *wordlen)
116
+ {
117
+ int i=0;
118
+ int j;
119
+
120
+ while((i<len)&&(begin[i]>0)&&(begin[i]<=32))
121
+ {
122
+ i++; // suppression des espaces
123
+ }
124
+ *wordbegin=begin+i;
125
+
126
+ j=i;
127
+ while((j<len)&&((begin[j]>32)||(begin[j]<0)))
128
+ {
129
+ j++; // aller jusqu'au bout du tag
130
+ }
131
+ *wordlen=j-i;
132
+ }
133
+
134
+
135
+ // allocation de m�moire pour la structure prop
136
+ prop propAlloc()
137
+ {
138
+ prop pnt=(prop)malloc(sizeof(struct Prop));
139
+ return pnt;
140
+ }
141
+
142
+ // d�sallocation de m�moire pour la structure prop
143
+ void propFree(prop pnt)
144
+ {
145
+ if (pnt)
146
+ {
147
+ free((void*)pnt);
148
+ }
149
+ }
150
+
151
+ // d�truit une prop, avec tous ses champs
152
+ void propRelease(prop pnt)
153
+ {
154
+ if (pnt)
155
+ {
156
+ propRelease(pnt->nxt);
157
+ stringRelease(pnt->name);
158
+ stringRelease(pnt->val);
159
+ propFree(pnt);
160
+ }
161
+ }
162
+
163
+ // ajout d'un champ � une structure prop 'nxt'
164
+ prop propAdd(const char *name,int namelen,const char *val,int vallen,prop nxt)
165
+ {
166
+ char *sname,*sval;
167
+
168
+ if (name[0] == ';')
169
+ {
170
+ return nxt;
171
+ }
172
+
173
+ prop pnt=propAlloc();
174
+ sname=stringStore(name,namelen);
175
+ sval=stringStore(val,vallen);
176
+
177
+ pnt->name=sname;
178
+ pnt->val=sval;
179
+ pnt->nxt=nxt;
180
+ return pnt;
181
+ }
182
+
183
+ // inverse la liste de propri�t�s
184
+ prop propReverse(prop pnt)
185
+ {
186
+ prop last=NULL;
187
+ prop nxt=NULL;
188
+ while(pnt)
189
+ {
190
+ nxt=pnt->nxt;
191
+ pnt->nxt=last;
192
+ if (!nxt)
193
+ {
194
+ return pnt;
195
+ }
196
+ last=pnt;
197
+ pnt=nxt;
198
+ }
199
+ return NULL;
200
+ }
201
+
202
+ // parsing d'un buffer rempli avec le contenu d'un fichier de prop
203
+ prop propParseInv(char *src)
204
+ {
205
+ prop pnt=NULL; // pointeur vers la property en construction
206
+ int i,name_b,name_e,val_b,val_e;
207
+
208
+ i=0;
209
+ while(src[i])
210
+ {
211
+ while((src[i])&&(src[i]>0)&&(src[i]<=32))
212
+ {
213
+ i++;
214
+ }
215
+ if (!src[i])
216
+ {
217
+ return pnt;
218
+ }
219
+ name_b=i;
220
+ while(src[i]>32)
221
+ {
222
+ i++;
223
+ }
224
+ if (!src[i])
225
+ {
226
+ return pnt;
227
+ }
228
+ name_e=i;
229
+ while((src[i])&&((src[i]==32)||(src[i]==9)))
230
+ {
231
+ i++;
232
+ }
233
+ if (!src[i])
234
+ {
235
+ return pnt;
236
+ }
237
+ if (src[i]>32) // il y a bien un argument
238
+ {
239
+ val_b=i;
240
+ while((src[i]>=32)||(src[i]<0))
241
+ {
242
+ i++;
243
+ }
244
+ val_e=i;
245
+ pnt=propAdd(src+name_b,name_e-name_b,src+val_b,val_e-val_b,pnt);
246
+ }
247
+ else
248
+ {
249
+ pnt=propAdd(src+name_b,name_e-name_b,src,0,pnt);
250
+ }
251
+ }
252
+ return pnt;
253
+ }
254
+
255
+ // parsing d'un buffer rempli avec le contenu d'un fichier de prop
256
+ prop propParse(char *src)
257
+ {
258
+ return propReverse(propParseInv(src));
259
+ }
260
+
261
+ // retourne le premier �l�ment
262
+ prop propFirst(prop pnt)
263
+ {
264
+ return pnt;
265
+ }
266
+
267
+ // retourne l'�l�ment suivant
268
+ prop propNext(prop pnt)
269
+ {
270
+ return pnt->nxt;
271
+ }
272
+
273
+ // charge un fichier de prop
274
+ // retourne un pointeur vers la structure prop du dernier champ
275
+ prop propLoad(const char *file)
276
+ {
277
+ prop pnt=NULL; // pointeur vers la property en construction
278
+ char* src=fileLoad(file);
279
+
280
+ if (!src)
281
+ {
282
+ return NULL;
283
+ }
284
+ pnt=propParse(src);
285
+ fileReleaseBuffer(src);
286
+ return pnt;
287
+ }
288
+
289
+
290
+ // dump d'une prop sur la sortie standard
291
+ void propDump(prop pnt)
292
+ {
293
+ while(pnt)
294
+ {
295
+ my_printf(LOG_INIT, "%s := %s\n",pnt->name,pnt->val);
296
+ pnt=pnt->nxt;
297
+ }
298
+ }
299
+
300
+
301
+ // retourne la valeur d'un champ
302
+ char* propGet(prop pnt,const char *field)
303
+ {
304
+ while(pnt)
305
+ {
306
+ if (!strcmp(pnt->name,field))
307
+ {
308
+ return pnt->val;
309
+ }
310
+ pnt=pnt->nxt;
311
+ }
312
+ return emptystring;
313
+ }
314
+
315
+ // retourne la valeur enti�re d'un champ
316
+ int propGetInt(prop pnt,const char *field)
317
+ {
318
+ char *p=propGet(pnt,field);
319
+ if ((p[0]=='0')&&((p[1]=='x')||(p[1]=='X')))
320
+ {
321
+ return htoi(p+2);
322
+ }
323
+ return atoi(p);
324
+ }
325
+
326
+ int PropLoad(const char* file)
327
+ {
328
+ Properties=propLoad(file);
329
+ return 0;
330
+ }
331
+
332
+ // retourne la valeur d'un champ
333
+ char* PropGet(const char *field)
334
+ {
335
+ return propGet(Properties,field);
336
+ }
337
+
338
+ // retourne la valeur enti�re d'un champ
339
+ int PropGetInt(const char *field)
340
+ {
341
+ return propGetInt(Properties,field);
342
+ }
343
+
344
+ // efface la valeur d'un champ s'il est pr�sent, chaine
345
+ void PropSet(const char *field, const char *val)
346
+ {
347
+ prop tmp = Properties;
348
+
349
+ while (tmp && strcmp(tmp->name, field))
350
+ {
351
+ tmp = tmp->nxt;
352
+ }
353
+ if (!tmp)
354
+ {
355
+ Properties = propAdd(field,strlen(field),val,strlen(val),Properties);
356
+ }
357
+ else
358
+ {
359
+ stringRelease(tmp->val);
360
+ tmp->val = stringStore(val, strlen(val));
361
+ }
362
+ }
363
+
364
+ // affiche toutes les prop sur la sortie standard
365
+ void PropDump()
366
+ {
367
+ propDump(Properties);
368
+ }