brewco/rdrecipes.c

changeset 459
1f88be70f253
child 460
0b2ea0ec165c
equal deleted inserted replaced
458:43a8ecb53637 459:1f88be70f253
1 /*****************************************************************************
2 * Copyright (C) 2015
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "brewco.h"
24 #include "rdrecipes.h"
25 #include "util.h"
26 #include "xutil.h"
27
28 extern int debug;
29
30 a_recipe *recipes = NULL;
31
32
33 const char SKIPYN[2][4] = { "NO", "YES" };
34
35
36 #define MY_ENCODING "utf-8"
37
38
39 int do_wrrecipes(void);
40 int do_wrrecipes(void)
41 {
42 int i, rc = 0;
43 FILE *fp;
44 char *mypath = NULL;
45 a_recipe *recipe;
46 xmlTextWriterPtr writer;
47 xmlBufferPtr buf;
48
49 /*
50 * Create a new XML buffer, to which the XML document will be written
51 */
52 if ((buf = xmlBufferCreate()) == NULL) {
53 syslog(LOG_NOTICE, "wrrecipes: error creating the xml buffer");
54 return 1;
55 }
56
57 /*
58 * Create a new XmlWriter for memory, with no compression.
59 */
60 if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) {
61 syslog(LOG_NOTICE, "wrrecipes: error creating the xml writer");
62 return 1;
63 }
64
65 /*
66 * Use indentation instead of one long line
67 */
68 if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) {
69 syslog(LOG_NOTICE, "wrrecipes: error setting Indent");
70 return 1;
71 }
72
73 /*
74 * Start the document with the xml default for the version,
75 * encoding ISO 8859-1 and the default for the standalone
76 * declaration.
77 */
78 if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) {
79 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartDocument");
80 return 1;
81 }
82
83 /*
84 * Start an element named "BREWCO". Since thist is the first
85 * element, this will be the root element of the document.
86 */
87 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO")) < 0) {
88 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
89 return 1;
90 }
91
92 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "RECIPES")) < 0) {
93 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
94 return 1;
95 }
96
97 for (recipe = recipes; recipe; recipe = recipe->next) {
98
99 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "RECIPE")) < 0) {
100 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
101 return 1;
102 }
103
104 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID", "%s", recipe->uuid)) < 0) {
105 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
106 return 1;
107 }
108 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CODE", "%s", recipe->code)) < 0) {
109 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
110 return 1;
111 }
112 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", recipe->name)) < 0) {
113 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
114 return 1;
115 }
116 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIME", "%d", recipe->boiltime)) < 0) {
117 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
118 return 1;
119 }
120 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)recipe->starttime)) < 0) {
121 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
122 return 1;
123 }
124 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)recipe->endtime)) < 0) {
125 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
126 return 1;
127 }
128
129 /*
130 * 8 Mash steps
131 */
132 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "MASHSTEPS")) < 0) {
133 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
134 return 1;
135 }
136 for (i =0; i < 8; i++) {
137 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "MASHSTEP")) < 0) {
138 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
139 return 1;
140 }
141 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", recipe->mash[i].name)) < 0) {
142 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
143 return 1;
144 }
145 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MIN", "%d", recipe->mash[i].min)) < 0) {
146 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
147 return 1;
148 }
149 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MAX", "%d", recipe->mash[i].max)) < 0) {
150 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
151 return 1;
152 }
153 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "CANSKIP", "%s", recipe->mash[i].canskip ? "YES":"NO")) < 0) {
154 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
155 return 1;
156 }
157 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "SETPOINT", "%f", recipe->mash[i].setpoint)) < 0) {
158 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
159 return 1;
160 }
161 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "SKIP", "%s", recipe->mash[i].skip ? "YES":"NO")) < 0) {
162 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
163 return 1;
164 }
165 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DURATION", "%d", recipe->mash[i].duration)) < 0) {
166 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
167 return 1;
168 }
169 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
170 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
171 return 1;
172 }
173 }
174 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
175 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
176 return 1;
177 }
178
179 /*
180 * 10 Hop additions
181 */
182 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "HOPADDITIONS")) < 0) {
183 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
184 return 1;
185 }
186 for (i = 0; i < 10; i++) {
187 if (recipe->hops[i].name) {
188 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "HOPADDITION")) < 0) {
189 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
190 return 1;
191 }
192 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", recipe->hops[i].name)) < 0) {
193 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
194 return 1;
195 }
196 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIME", "%d", recipe->hops[i].boiltime)) < 0) {
197 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
198 return 1;
199 }
200 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
201 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
202 return 1;
203 }
204 }
205 }
206 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
207 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
208 return 1;
209 }
210
211 /*
212 * 3 Hop stands
213 */
214 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "HOPSTANDS")) < 0) {
215 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
216 return 1;
217 }
218 for (i = 0; i < 3; i++) {
219 if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "HOPSTAND")) < 0) {
220 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterStartElement");
221 return 1;
222 }
223 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", recipe->hopstand[i].name)) < 0) {
224 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
225 return 1;
226 }
227 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MIN", "%d", recipe->hopstand[i].min)) < 0) {
228 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
229 return 1;
230 }
231 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MAX", "%d", recipe->hopstand[i].max)) < 0) {
232 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
233 return 1;
234 }
235 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "HOLD", "%s", recipe->hopstand[i].hold ? "YES":"NO")) < 0) {
236 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
237 return 1;
238 }
239 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "SETPOINT", "%f", recipe->hopstand[i].setpoint)) < 0) {
240 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
241 return 1;
242 }
243 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "SKIP", "%s", recipe->hopstand[i].skip ? "YES":"NO")) < 0) {
244 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
245 return 1;
246 }
247 if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "DURATION", "%d", recipe->hopstand[i].duration)) < 0) {
248 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterWriteFormatElement");
249 return 1;
250 }
251 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
252 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
253 return 1;
254 }
255 }
256 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
257 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
258 return 1;
259 }
260
261 /*
262 * Close the element named RECIPE.
263 */
264 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
265 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
266 return 1;
267 }
268 }
269 /*
270 * Close the element named RECIPES.
271 */
272 if ((rc = xmlTextWriterEndElement(writer)) < 0) {
273 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
274 return 1;
275 }
276
277 /*
278 * All done, close any open elements
279 */
280 if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
281 syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndDocument");
282 return 1;
283 }
284 xmlFreeTextWriter(writer);
285
286 /*
287 * Now write the XML recipes
288 */
289 if (getenv((char *)"USER") == NULL) {
290 mypath = xstrcpy((char *)"/root");
291 } else {
292 mypath = xstrcpy(getenv((char *)"HOME"));
293 }
294 mypath = xstrcat(mypath, (char *)"/.brewco/etc/");
295 mkdirs(mypath, 0755);
296 mypath = xstrcat(mypath, (char *)"recipes.xml");
297
298 if (debug)
299 fprintf(stdout, "Writing %s\n", mypath);
300
301 if ((fp = fopen(mypath, "w")) == NULL) {
302 syslog(LOG_NOTICE, "could not rewrite %s", mypath);
303 free(mypath);
304 return 1;
305 }
306 free(mypath);
307
308 fprintf(fp, "%s", (const char *) buf->content);
309 fclose(fp);
310 xmlBufferFree(buf);
311
312 return 0;
313 }
314
315
316
317 int wrrecipes(void)
318 {
319 int rc;
320
321 rc = do_wrrecipes();
322 syslog(LOG_NOTICE, "Rewritten recipes, rc=%d", rc);
323 return rc;
324 }
325
326
327
328 int parseRecipe(xmlDocPtr doc, xmlNodePtr cur)
329 {
330 xmlChar *key;
331 xmlNodePtr s1cur, s2cur;
332 a_recipe *recipe, *tmp;
333 int i, j, ival;
334 float fval;
335
336 recipe = (a_recipe *)malloc(sizeof(a_recipe));
337 memset(recipe, 0, sizeof(a_recipe));
338 recipe->next = NULL;
339
340 cur = cur->xmlChildrenNode;
341 while (cur != NULL) {
342 if ((!xmlStrcmp(cur->name, (const xmlChar *)"UUID"))) {
343 recipe->uuid = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
344 }
345 if ((!xmlStrcmp(cur->name, (const xmlChar *)"CODE"))) {
346 recipe->code = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
347 }
348 if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) {
349 recipe->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
350 }
351 if ((!xmlStrcmp(cur->name, (const xmlChar *)"BOILTIME"))) {
352 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
353 if (sscanf((const char *)key, "%d", &ival) == 1)
354 recipe->boiltime = ival;
355 xmlFree(key);
356 }
357 if ((!xmlStrcmp(cur->name, (const xmlChar *)"STARTTIME"))) {
358 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
359 if (sscanf((const char *)key, "%d", &ival) == 1)
360 recipe->starttime = (time_t)ival;
361 xmlFree(key);
362 }
363 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ENDTIME"))) {
364 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
365 if (sscanf((const char *)key, "%d", &ival) == 1)
366 recipe->endtime = (time_t)ival;
367 xmlFree(key);
368 }
369 fprintf(stdout, "cur %s\n", cur->name);
370 if ((!xmlStrcmp(cur->name, (const xmlChar *)"MASHSTEPS"))) {
371 s1cur = cur->xmlChildrenNode;
372 i = 0;
373 while (s1cur != NULL) {
374 fprintf(stdout, " s1cur %s\n", s1cur->name);
375
376 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"MASHSTEP"))) {
377 s2cur = s1cur->xmlChildrenNode;
378 while (s2cur != NULL) {
379 fprintf(stdout, " s2cur %s\n", s2cur->name);
380 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"NAME"))) {
381 recipe->mash[i].name = (char *)xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
382 }
383 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"MIN"))) {
384 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
385 if (sscanf((const char *)key, "%d", &ival) == 1)
386 recipe->mash[i].min = ival;
387 xmlFree(key);
388 }
389 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"MAX"))) {
390 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
391 if (sscanf((const char *)key, "%d", &ival) == 1)
392 recipe->mash[i].max = ival;
393 xmlFree(key);
394 }
395 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"CANSKIP"))) {
396 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
397 for (j = 0; j < 2; j++) {
398 if (! xmlStrcmp(key, (const xmlChar *)SKIPYN[j])) {
399 recipe->mash[i].canskip = j;
400 break;
401 }
402 }
403 xmlFree(key);
404 }
405 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"SETPOINT"))) {
406 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
407 if (sscanf((const char *)key, "%f", &fval) == 1)
408 recipe->mash[i].setpoint = fval;
409 xmlFree(key);
410 }
411 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"SKIP"))) {
412 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
413 for (j = 0; j < 2; j++) {
414 if (! xmlStrcmp(key, (const xmlChar *)SKIPYN[j])) {
415 recipe->mash[i].skip = j;
416 break;
417 }
418 }
419 xmlFree(key);
420 }
421 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"DURATION"))) {
422 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
423 if (sscanf((const char *)key, "%d", &ival) == 1)
424 recipe->mash[i].duration = ival;
425 xmlFree(key);
426 }
427 s2cur = s2cur->next;
428 }
429 i++;
430 }
431 s1cur = s1cur->next;
432 }
433 }
434
435 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HOPADDITIONS"))) {
436 s1cur = cur->xmlChildrenNode;
437 i = 0;
438 while (s1cur != NULL) {
439 fprintf(stdout, " s1cur %s\n", s1cur->name);
440
441 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"HOPADDITION"))) {
442 s2cur = s1cur->xmlChildrenNode;
443 while (s2cur != NULL) {
444 fprintf(stdout, " s2cur %s\n", s2cur->name);
445 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"NAME"))) {
446 recipe->hops[i].name = (char *)xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
447 }
448 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"BOILTIME"))) {
449 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
450 if (sscanf((const char *)key, "%d", &ival) == 1)
451 recipe->hops[i].boiltime = ival;
452 xmlFree(key);
453 }
454 s2cur = s2cur->next;
455 }
456 i++;
457 }
458 s1cur = s1cur->next;
459 }
460 }
461
462 if ((!xmlStrcmp(cur->name, (const xmlChar *)"HOPSTANDS"))) {
463 s1cur = cur->xmlChildrenNode;
464 i = 0;
465 while (s1cur != NULL) {
466 fprintf(stdout, " s1cur %s\n", s1cur->name);
467
468 if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"HOPSTAND"))) {
469 s2cur = s1cur->xmlChildrenNode;
470 while (s2cur != NULL) {
471 fprintf(stdout, " s2cur %s\n", s2cur->name);
472 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"NAME"))) {
473 recipe->hopstand[i].name = (char *)xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
474 }
475 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"MIN"))) {
476 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
477 if (sscanf((const char *)key, "%d", &ival) == 1)
478 recipe->hopstand[i].min = ival;
479 xmlFree(key);
480 }
481 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"MAX"))) {
482 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
483 if (sscanf((const char *)key, "%d", &ival) == 1)
484 recipe->hopstand[i].max = ival;
485 xmlFree(key);
486 }
487 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"HOLD"))) {
488 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
489 for (j = 0; j < 2; j++) {
490 if (! xmlStrcmp(key, (const xmlChar *)SKIPYN[j])) {
491 recipe->hopstand[i].hold = j;
492 break;
493 }
494 }
495 xmlFree(key);
496 }
497 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"SETPOINT"))) {
498 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
499 if (sscanf((const char *)key, "%f", &fval) == 1)
500 recipe->hopstand[i].setpoint = fval;
501 xmlFree(key);
502 }
503 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"SKIP"))) {
504 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
505 for (j = 0; j < 2; j++) {
506 if (! xmlStrcmp(key, (const xmlChar *)SKIPYN[j])) {
507 recipe->hopstand[i].skip = j;
508 break;
509 }
510 }
511 xmlFree(key);
512 }
513 if ((!xmlStrcmp(s2cur->name, (const xmlChar *)"DURATION"))) {
514 key = xmlNodeListGetString(doc, s2cur->xmlChildrenNode, 1);
515 if (sscanf((const char *)key, "%d", &ival) == 1)
516 recipe->hopstand[i].duration = ival;
517 xmlFree(key);
518 }
519 s2cur = s2cur->next;
520 }
521 i++;
522 }
523 s1cur = s1cur->next;
524 }
525 }
526 cur = cur->next;
527 }
528
529 if (recipes == NULL) {
530 recipes = recipe;
531 } else {
532 for (tmp = recipes; tmp; tmp = tmp->next) {
533 if (tmp->next == NULL) {
534 tmp->next = recipe;
535 break;
536 }
537 }
538 }
539
540 return 0;
541 }
542
543
544
545 int parseRecipes(xmlDocPtr doc, xmlNodePtr cur)
546 {
547 cur = cur->xmlChildrenNode;
548 while (cur != NULL) {
549 if ((!xmlStrcmp(cur->name, (const xmlChar *)"RECIPE"))) {
550 parseRecipe(doc, cur);
551 }
552 cur = cur->next;
553 }
554 return 0;
555 }
556
557
558
559 /*
560 * Returns:
561 * 0 - All is well, recipes loaded.
562 * 1 - Something went wrong
563 */
564 int rdrecipes(void)
565 {
566 int i;
567 char *mypath;
568 xmlDocPtr doc;
569 xmlNodePtr cur;
570 a_recipe *recipe;
571
572 syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME")));
573
574 /*
575 * Search config file
576 */
577 if (getenv((char *)"USER") == NULL) {
578 mypath = xstrcpy((char *)"/root");
579 } else {
580 mypath = xstrcpy(getenv((char *)"HOME"));
581 }
582 mypath = xstrcat(mypath, (char *)"/.brewco/etc/");
583 mkdirs(mypath, 0755);
584 mypath = xstrcat(mypath, (char *)"recipes.xml");
585
586 /*
587 * Free possible old recipes list
588 */
589 for (recipe = recipes; recipe; recipe = recipe->next) {
590 if (recipe->uuid)
591 free(recipe->uuid);
592 if (recipe->code)
593 free(recipe->code);
594 if (recipe->name)
595 free(recipe->name);
596 for (i = 0; i < 8; i++)
597 if (recipe->mash[i].name)
598 free(recipe->mash[i].name);
599 for (i = 0; i < 10; i++)
600 if (recipe->hops[i].name)
601 free(recipe->hops[i].name);
602 for (i = 0; i < 3; i++)
603 if (recipe->hopstand[i].name)
604 free(recipe->hopstand[i].name);
605 free(recipe);
606 }
607 recipes = NULL;
608
609 /*
610 * See if we have a recipes file.
611 */
612 if (file_exist(mypath, W_OK)) {
613 syslog(LOG_NOTICE, "rdrecipes: %s not found, good.", mypath);
614 free(mypath);
615 return 0;
616 }
617
618 if ((doc = xmlParseFile(mypath)) == NULL) {
619 syslog(LOG_NOTICE, "rdrecipes: %s not found, we may need some.", mypath);
620 free(mypath);
621 return 0;
622 }
623 syslog(LOG_NOTICE, "rdrecipes: using %s", mypath);
624
625 if ((cur = xmlDocGetRootElement(doc)) == NULL) {
626 syslog(LOG_NOTICE, "XML file %s empty.", mypath);
627 xmlFreeDoc(doc);
628 return 1;
629 }
630 if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) {
631 syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath);
632 xmlFreeDoc(doc);
633 return 1;
634 }
635
636 /*
637 * Parse recipes
638 */
639 cur = cur->xmlChildrenNode;
640 while (cur != NULL) {
641 if ((!xmlStrcmp(cur->name, (const xmlChar *)"RECIPES"))) {
642 parseRecipes(doc, cur);
643 }
644 cur = cur->next;
645 }
646 xmlFreeDoc(doc);
647
648 free(mypath);
649 mypath = NULL;
650
651 return 0;
652 }
653
654
655

mercurial