Imported Upstream version 1.6.0
[platform/upstream/augeas.git] / src / put.c
1 /*
2  * put.c:
3  *
4  * Copyright (C) 2007-2015 David Lutterkort
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
19  *
20  * Author: David Lutterkort <dlutter@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <stdarg.h>
26 #include "regexp.h"
27 #include "memory.h"
28 #include "lens.h"
29 #include "errcode.h"
30
31 /* Data structure to keep track of where we are in the tree. The split
32  * describes a sublist of the list of siblings in the current tree. The
33  * put_* functions don't operate on the tree directly, instead they operate
34  * on a split.
35  *
36  * The TREE field points to the first tree node for the current invocation
37  * of put_*, FOLLOW points to the first sibling following TREE that is not
38  * part of the split anymore (NULL if we are talking about all the siblings
39  * of TREE)
40  *
41  * ENC is a string containing the encoding of the current position in the
42  * tree.  The encoding is
43  *   <label>=<value>/<label>=<value>/.../<label>=<value>/
44  * where the label/value pairs come from TREE and its
45  * siblings. The encoding uses ENC_EQ instead of the '=' above to avoid
46  * clashes with legitimate values, and encodes NULL values as ENC_NULL.
47  */
48 struct split {
49     struct split *next;
50     struct tree  *tree;
51     struct tree  *follow;
52     char         *enc;
53     size_t        start;
54     size_t        end;
55 };
56
57 struct state {
58     FILE             *out;
59     struct split     *split;
60     const char       *key;
61     const char       *value;
62     const char       *override;
63     struct dict      *dict;
64     struct skel      *skel;
65     char             *path;   /* Position in the tree, for errors */
66     size_t            pos;
67     struct lns_error *error;
68 };
69
70 static void create_lens(struct lens *lens, struct state *state);
71 static void put_lens(struct lens *lens, struct state *state);
72
73 static void put_error(struct state *state, struct lens *lens,
74                       const char *format, ...)
75 {
76     va_list ap;
77     int r;
78
79     if (state->error != NULL)
80         return;
81
82     CALLOC(state->error, 1);
83     state->error->lens = ref(lens);
84     state->error->pos  = -1;
85     if (strlen(state->path) == 0) {
86         state->error->path = strdup("");
87     } else {
88         state->error->path = strdup(state->path);
89     }
90
91     va_start(ap, format);
92     r = vasprintf(&state->error->message, format, ap);
93     va_end(ap);
94     if (r == -1)
95         state->error->message = NULL;
96 }
97
98 ATTRIBUTE_PURE
99 static int enclen(const char *key, const char *value) {
100     return ENCLEN(key) + strlen(ENC_EQ) + ENCLEN(value)
101         + strlen(ENC_SLASH);
102 }
103
104 static char *encpcpy(char *e, const char *key, const char *value) {
105     e = stpcpy(e, ENCSTR(key));
106     e = stpcpy(e, ENC_EQ);
107     e = stpcpy(e, ENCSTR(value));
108     e = stpcpy(e, ENC_SLASH);
109     return e;
110 }
111
112 static void regexp_match_error(struct state *state, struct lens *lens,
113                                int count, struct split *split) {
114     char *text = NULL;
115     char *pat = NULL;
116
117     lns_format_atype(lens, &pat);
118     text = enc_format_indent(split->enc + split->start,
119                              split->end - split->start,
120                              4);
121
122     if (count == -1) {
123         put_error(state, lens,
124                   "Failed to match tree\n\n%s\n  with pattern\n   %s",
125                   text, pat);
126     } else if (count == -2) {
127         put_error(state, lens,
128                   "Internal error matching\n    %s\n  with tree\n   %s",
129                   pat, text);
130     } else if (count == -3) {
131         /* Should have been caught by the typechecker */
132         put_error(state, lens, "Syntax error in tree schema\n    %s", pat);
133     }
134     free(pat);
135     free(text);
136 }
137
138 static void free_split(struct split *split) {
139     if (split == NULL)
140         return;
141
142     free(split->enc);
143     free(split);
144 }
145
146 /* Encode the list of TREE's children as a string.
147  */
148 static struct split *make_split(struct tree *tree) {
149     struct split *split;
150
151     if (ALLOC(split) < 0)
152         return NULL;
153
154     split->tree = tree;
155     list_for_each(t, tree) {
156         split->end += enclen(t->label, t->value);
157     }
158
159     if (ALLOC_N(split->enc, split->end + 1) < 0)
160         goto error;
161
162     char *enc = split->enc;
163     list_for_each(t, tree) {
164         enc = encpcpy(enc, t->label, t->value);
165     }
166     return split;
167  error:
168     free_split(split);
169     return NULL;
170 }
171
172 static struct split *split_append(struct split **split, struct split *tail,
173                                   struct tree *tree, struct tree *follow,
174                                   char *enc, size_t start, size_t end) {
175     struct split *sp;
176     CALLOC(sp, 1);
177     sp->tree = tree;
178     sp->follow = follow;
179     sp->enc = enc;
180     sp->start = start;
181     sp->end = end;
182     list_tail_cons(*split, tail, sp);
183     return tail;
184 }
185
186 static struct split *next_split(struct state *state) {
187     if (state->split != NULL) {
188         state->split = state->split->next;
189         if (state->split != NULL)
190             state->pos = state->split->end;
191     }
192     return state->split;
193 }
194
195 static struct split *set_split(struct state *state, struct split *split) {
196     state->split = split;
197     if (split != NULL)
198         state->pos = split->end;
199     return split;
200 }
201
202 /* Refine a tree split OUTER according to the L_CONCAT lens LENS */
203 static struct split *split_concat(struct state *state, struct lens *lens) {
204     assert(lens->tag == L_CONCAT);
205
206     int count = 0;
207     struct split *outer = state->split;
208     struct re_registers regs;
209     struct split *split = NULL, *tail = NULL;
210     struct regexp *atype = lens->atype;
211
212     /* Fast path for leaf nodes, which will always lead to an empty split */
213     // FIXME: This doesn't match the empty encoding
214     if (outer->tree == NULL && strlen(outer->enc) == 0
215         && regexp_is_empty_pattern(atype)) {
216         for (int i=0; i < lens->nchildren; i++) {
217             tail = split_append(&split, tail, NULL, NULL,
218                                 outer->enc, 0, 0);
219         }
220         return split;
221     }
222
223     MEMZERO(&regs, 1);
224     count = regexp_match(atype, outer->enc, outer->end,
225                          outer->start, &regs);
226     if (count >= 0 && count != outer->end - outer->start)
227         count = -1;
228     if (count < 0) {
229         regexp_match_error(state, lens, count, outer);
230         goto error;
231     }
232
233     struct tree *cur = outer->tree;
234     int reg = 1;
235     for (int i=0; i < lens->nchildren; i++) {
236         assert(reg < regs.num_regs);
237         assert(regs.start[reg] != -1);
238         struct tree *follow = cur;
239         for (int j = regs.start[reg]; j < regs.end[reg]; j++) {
240             if (outer->enc[j] == ENC_SLASH_CH)
241                 follow = follow->next;
242         }
243         tail = split_append(&split, tail, cur, follow,
244                             outer->enc, regs.start[reg], regs.end[reg]);
245         cur = follow;
246         reg += 1 + regexp_nsub(lens->children[i]->atype);
247     }
248     assert(reg < regs.num_regs);
249  done:
250     free(regs.start);
251     free(regs.end);
252     return split;
253  error:
254     free_split(split);
255     split = NULL;
256     goto done;
257 }
258
259 static struct split *split_iter(struct state *state, struct lens *lens) {
260     assert(lens->tag == L_STAR);
261
262     int count = 0;
263     struct split *outer = state->split;
264     struct split *split = NULL;
265     struct regexp *atype = lens->child->atype;
266
267     struct tree *cur = outer->tree;
268     int pos = outer->start;
269     struct split *tail = NULL;
270     while (pos < outer->end) {
271         count = regexp_match(atype, outer->enc, outer->end, pos, NULL);
272         if (count == -1) {
273             break;
274         } else if (count < -1) {
275             regexp_match_error(state, lens->child, count, outer);
276             goto error;
277         }
278
279         struct tree *follow = cur;
280         for (int j = pos; j < pos + count; j++) {
281             if (outer->enc[j] == ENC_SLASH_CH)
282                 follow = follow->next;
283         }
284         tail = split_append(&split, tail, cur, follow,
285                             outer->enc, pos, pos + count);
286         cur = follow;
287         pos += count;
288     }
289     return split;
290  error:
291     free_split(split);
292     return NULL;
293 }
294
295 /* Check if LENS applies to the current split in STATE */
296 static int applies(struct lens *lens, struct state *state) {
297     int count;
298     struct split *split = state->split;
299
300     count = regexp_match(lens->atype, split->enc, split->end,
301                          split->start, NULL);
302     if (count < -1) {
303         regexp_match_error(state, lens, count, split);
304         return 0;
305     }
306
307     if (count != split->end - split->start)
308         return 0;
309     if (count == 0 && lens->value)
310         return state->value != NULL;
311     return 1;
312 }
313
314 /* Print TEXT to OUT, translating common escapes like \n */
315 static void print_escaped_chars(FILE *out, const char *text) {
316     for (const char *c = text; *c != '\0'; c++) {
317         if (*c == '\\') {
318             char x;
319             c += 1;
320             if (*c == '\0') {
321                 fputc(*c, out);
322                 break;
323             }
324             switch(*c) {
325             case 'a':
326                 x = '\a';
327                 break;
328             case 'b':
329                 x = '\b';
330                 break;
331             case 'f':
332                 x = '\f';
333                 break;
334             case 'n':
335                 x = '\n';
336                 break;
337             case 'r':
338                 x = '\r';
339                 break;
340             case 't':
341                 x = '\t';
342                 break;
343             case 'v':
344                 x = '\v';
345                 break;
346             default:
347                 x = *c;
348                 break;
349             }
350             fputc(x, out);
351         } else {
352             fputc(*c, out);
353         }
354     }
355 }
356
357 /*
358  * Check whether SKEL has the skeleton type required by LENS
359  */
360
361 static int skel_instance_of(struct lens *lens, struct skel *skel) {
362     if (skel == NULL)
363         return 0;
364
365     switch (lens->tag) {
366     case L_DEL: {
367         int count;
368         if (skel->tag != L_DEL)
369             return 0;
370         count = regexp_match(lens->regexp, skel->text, strlen(skel->text),
371                            0, NULL);
372         return count == strlen(skel->text);
373     }
374     case L_STORE:
375         return skel->tag == L_STORE;
376     case L_KEY:
377         return skel->tag == L_KEY;
378     case L_LABEL:
379         return skel->tag == L_LABEL;
380     case L_VALUE:
381         return skel->tag == L_VALUE;
382     case L_SEQ:
383         return skel->tag == L_SEQ;
384     case L_COUNTER:
385         return skel->tag == L_COUNTER;
386     case L_CONCAT:
387         {
388             struct skel *s = skel->skels;
389             for (int i=0; i < lens->nchildren; i++) {
390                 if (! skel_instance_of(lens->children[i], s))
391                     return 0;
392                 s = s->next;
393             }
394             return 1;
395         }
396         break;
397     case L_UNION:
398         {
399             for (int i=0; i < lens->nchildren; i++) {
400                 if (skel_instance_of(lens->children[i], skel))
401                     return 1;
402             }
403             return 0;
404         }
405         break;
406     case L_SUBTREE:
407         return skel->tag == L_SUBTREE;
408     case L_MAYBE:
409         return skel->tag == L_MAYBE || skel_instance_of(lens->child, skel);
410     case L_STAR:
411         if (skel->tag != lens->tag)
412             return 0;
413         if (lens->tag == L_MAYBE &&
414             skel->skels != NULL && skel->skels->next != NULL)
415             return 0;
416         list_for_each(s, skel->skels) {
417             if (! skel_instance_of(lens->child, s))
418                 return 0;
419         }
420         return 1;
421     case L_REC:
422         return skel_instance_of(lens->body, skel);
423     case L_SQUARE:
424         return skel->tag == L_SQUARE
425             && skel_instance_of(lens->child, skel->skels);
426     default:
427         BUG_ON(true, lens->info, "illegal lens tag %d", lens->tag);
428         break;
429     }
430  error:
431     return 0;
432 }
433
434 /*
435  * put
436  */
437 static void put_subtree(struct lens *lens, struct state *state) {
438     assert(lens->tag == L_SUBTREE);
439     struct state oldstate = *state;
440     struct split oldsplit = *state->split;
441     size_t oldpathlen = strlen(state->path);
442
443     struct tree *tree = state->split->tree;
444     struct split *split = NULL;
445
446     state->key = tree->label;
447     state->value = tree->value;
448     pathjoin(&state->path, 1, state->key);
449
450     split = make_split(tree->children);
451     set_split(state, split);
452
453     dict_lookup(tree->label, state->dict, &state->skel, &state->dict);
454     if (state->skel == NULL || ! skel_instance_of(lens->child, state->skel)) {
455         create_lens(lens->child, state);
456     } else {
457         put_lens(lens->child, state);
458     }
459     assert(state->error != NULL || state->split->next == NULL);
460
461     oldstate.error = state->error;
462     oldstate.path = state->path;
463     *state = oldstate;
464     *state->split= oldsplit;
465     free_split(split);
466     state->path[oldpathlen] = '\0';
467 }
468
469 static void put_del(ATTRIBUTE_UNUSED struct lens *lens, struct state *state) {
470     assert(lens->tag == L_DEL);
471     assert(state->skel != NULL);
472     assert(state->skel->tag == L_DEL);
473     if (state->override != NULL) {
474         fprintf(state->out, "%s", state->override);
475     } else {
476         fprintf(state->out, "%s", state->skel->text);
477     }
478 }
479
480 static void put_union(struct lens *lens, struct state *state) {
481     assert(lens->tag == L_UNION);
482
483     for (int i=0; i < lens->nchildren; i++) {
484         struct lens *l = lens->children[i];
485         if (applies(l, state)) {
486             if (skel_instance_of(l, state->skel))
487                 put_lens(l, state);
488             else
489                 create_lens(l, state);
490             return;
491         }
492     }
493     put_error(state, lens, "None of the alternatives in the union match");
494 }
495
496 static void put_concat(struct lens *lens, struct state *state) {
497     assert(lens->tag == L_CONCAT);
498     struct split *oldsplit = state->split;
499     struct skel *oldskel = state->skel;
500
501     struct split *split = split_concat(state, lens);
502
503     state->skel = state->skel->skels;
504     set_split(state, split);
505     for (int i=0; i < lens->nchildren; i++) {
506         if (state->split == NULL) {
507             put_error(state, lens,
508                       "Not enough components in concat");
509             list_free(split);
510             return;
511         }
512         put_lens(lens->children[i], state);
513         state->skel = state->skel->next;
514         next_split(state);
515     }
516     list_free(split);
517     set_split(state, oldsplit);
518     state->skel = oldskel;
519 }
520
521 static void error_quant_star(struct split *last_split, struct lens *lens,
522                              struct state *state) {
523     struct tree *child = NULL;
524     if (last_split != NULL) {
525         if (last_split->follow != NULL) {
526             child = last_split->follow;
527         } else {
528             for (child = last_split->tree;
529                  child != NULL && child->next != NULL;
530                  child = child->next);
531         }
532     }
533     if (child == NULL) {
534         put_error(state, lens, "Malformed child node");
535     } else {
536         put_error(state, lens, "Malformed child node '%s'", child->label);
537     }
538 }
539
540 static void put_quant_star(struct lens *lens, struct state *state) {
541     assert(lens->tag == L_STAR);
542     struct split *oldsplit = state->split;
543     struct skel *oldskel = state->skel;
544     struct split *last_split = NULL;
545
546     struct split *split = split_iter(state, lens);
547
548     state->skel = state->skel->skels;
549     set_split(state, split);
550     last_split = state->split;
551     while (state->split != NULL && state->skel != NULL) {
552         put_lens(lens->child, state);
553         state->skel = state->skel->next;
554         last_split = state->split;
555         next_split(state);
556     }
557     while (state->split != NULL) {
558         create_lens(lens->child, state);
559         last_split = state->split;
560         next_split(state);
561     }
562     if (state->pos != oldsplit->end)
563         error_quant_star(last_split, lens, state);
564     list_free(split);
565     set_split(state, oldsplit);
566     state->skel = oldskel;
567 }
568
569 static void put_quant_maybe(struct lens *lens, struct state *state) {
570     assert(lens->tag == L_MAYBE);
571     struct lens *child = lens->child;
572
573     if (applies(child, state)) {
574         if (skel_instance_of(child, state->skel))
575             put_lens(child, state);
576         else
577             create_lens(child, state);
578     }
579 }
580
581 static void put_store(struct lens *lens, struct state *state) {
582     if (state->value == NULL) {
583         put_error(state, lens,
584                   "Can not store a nonexistent (NULL) value");
585     } else if (regexp_match(lens->regexp, state->value, strlen(state->value),
586                             0, NULL) != strlen(state->value)) {
587         char *pat = regexp_escape(lens->regexp);
588         put_error(state, lens,
589                   "Value '%s' does not match regexp /%s/ in store lens",
590                   state->value, pat);
591         free(pat);
592     } else {
593         fprintf(state->out, "%s", state->value);
594     }
595 }
596
597 static void put_rec(struct lens *lens, struct state *state) {
598     put_lens(lens->body, state);
599 }
600
601 static void put_square(struct lens *lens, struct state *state) {
602     assert(lens->tag == L_SQUARE);
603     struct skel *oldskel = state->skel;
604     struct split *oldsplit = state->split;
605     struct lens *concat = lens->child;
606     struct lens *left = concat->children[0];
607     struct split *split = split_concat(state, concat);
608
609     /* skels of concat is one depth more */
610     state->skel = state->skel->skels->skels;
611     set_split(state, split);
612     for (int i=0; i < concat->nchildren; i++) {
613         if (state->split == NULL) {
614             put_error(state, concat, "Not enough components in square");
615             list_free(split);
616             return;
617         }
618         struct lens *curr = concat->children[i];
619         if (i == (concat->nchildren - 1) && left->tag == L_KEY)
620             state->override = state->key;
621         put_lens(curr, state);
622         state->override = NULL;
623         state->skel = state->skel->next;
624         next_split(state);
625     }
626     list_free(split);
627     set_split(state, oldsplit);
628     state->skel = oldskel;
629 }
630
631 static void put_lens(struct lens *lens, struct state *state) {
632     if (state->error != NULL)
633         return;
634
635     switch(lens->tag) {
636     case L_DEL:
637         put_del(lens, state);
638         break;
639     case L_STORE:
640         put_store(lens, state);
641         break;
642     case L_KEY:
643         fprintf(state->out, "%s", state->key);
644         break;
645     case L_LABEL:
646     case L_VALUE:
647         /* Nothing to do */
648         break;
649     case L_SEQ:
650         /* Nothing to do */
651         break;
652     case L_COUNTER:
653         /* Nothing to do */
654         break;
655     case L_CONCAT:
656         put_concat(lens, state);
657         break;
658     case L_UNION:
659         put_union(lens, state);
660         break;
661     case L_SUBTREE:
662         put_subtree(lens, state);
663         break;
664     case L_STAR:
665         put_quant_star(lens, state);
666         break;
667     case L_MAYBE:
668         put_quant_maybe(lens, state);
669         break;
670     case L_REC:
671         put_rec(lens, state);
672         break;
673     case L_SQUARE:
674         put_square(lens, state);
675         break;
676     default:
677         assert(0);
678         break;
679     }
680 }
681
682 static void create_subtree(struct lens *lens, struct state *state) {
683     put_subtree(lens, state);
684 }
685
686 static void create_del(struct lens *lens, struct state *state) {
687     assert(lens->tag == L_DEL);
688     if (state->override != NULL) {
689         print_escaped_chars(state->out, state->override);
690     } else {
691         print_escaped_chars(state->out, lens->string->str);
692     }
693 }
694
695 static void create_union(struct lens *lens, struct state *state) {
696     assert(lens->tag == L_UNION);
697
698     for (int i=0; i < lens->nchildren; i++) {
699         if (applies(lens->children[i], state)) {
700             create_lens(lens->children[i], state);
701             return;
702         }
703     }
704     put_error(state, lens, "None of the alternatives in the union match");
705 }
706
707 static void create_concat(struct lens *lens, struct state *state) {
708     assert(lens->tag == L_CONCAT);
709     struct split *oldsplit = state->split;
710
711     struct split *split = split_concat(state, lens);
712
713     set_split(state, split);
714     for (int i=0; i < lens->nchildren; i++) {
715         if (state->split == NULL) {
716             put_error(state, lens,
717                       "Not enough components in concat");
718             list_free(split);
719             return;
720         }
721         create_lens(lens->children[i], state);
722         next_split(state);
723     }
724     list_free(split);
725     set_split(state, oldsplit);
726 }
727
728 static void create_square(struct lens *lens, struct state *state) {
729     assert(lens->tag == L_SQUARE);
730     struct lens *concat = lens->child;
731
732     struct split *oldsplit = state->split;
733     struct split *split = split_concat(state, concat);
734     struct lens *left = concat->children[0];
735
736     set_split(state, split);
737     for (int i=0; i < concat->nchildren; i++) {
738         if (state->split == NULL) {
739             put_error(state, concat, "Not enough components in square");
740             list_free(split);
741             return;
742         }
743         struct lens *curr = concat->children[i];
744         if (i == (concat->nchildren - 1) && left->tag == L_KEY)
745             state->override = state->key;
746         create_lens(curr, state);
747         state->override = NULL;
748         next_split(state);
749     }
750     list_free(split);
751     set_split(state, oldsplit);
752 }
753
754 static void create_quant_star(struct lens *lens, struct state *state) {
755     assert(lens->tag == L_STAR);
756     struct split *oldsplit = state->split;
757     struct split *last_split = NULL;
758
759     struct split *split = split_iter(state, lens);
760
761     set_split(state, split);
762     last_split = state->split;
763     while (state->split != NULL) {
764         create_lens(lens->child, state);
765         last_split = state->split;
766         next_split(state);
767     }
768     if (state->pos != oldsplit->end)
769         error_quant_star(last_split, lens, state);
770     list_free(split);
771     set_split(state, oldsplit);
772 }
773
774 static void create_quant_maybe(struct lens *lens, struct state *state) {
775     assert(lens->tag == L_MAYBE);
776
777     if (applies(lens->child, state)) {
778         create_lens(lens->child, state);
779     }
780 }
781
782 static void create_rec(struct lens *lens, struct state *state) {
783     create_lens(lens->body, state);
784 }
785
786 static void create_lens(struct lens *lens, struct state *state) {
787     if (state->error != NULL)
788         return;
789     switch(lens->tag) {
790     case L_DEL:
791         create_del(lens, state);
792         break;
793     case L_STORE:
794         put_store(lens, state);
795         break;
796     case L_KEY:
797         fprintf(state->out, "%s", state->key);
798         break;
799     case L_LABEL:
800     case L_VALUE:
801         /* Nothing to do */
802         break;
803     case L_SEQ:
804         /* Nothing to do */
805         break;
806     case L_COUNTER:
807         /* Nothing to do */
808         break;
809     case L_CONCAT:
810         create_concat(lens, state);
811         break;
812     case L_UNION:
813         create_union(lens, state);
814         break;
815     case L_SUBTREE:
816         create_subtree(lens, state);
817         break;
818     case L_STAR:
819         create_quant_star(lens, state);
820         break;
821     case L_MAYBE:
822         create_quant_maybe(lens, state);
823         break;
824     case L_REC:
825         create_rec(lens, state);
826         break;
827     case L_SQUARE:
828         create_square(lens, state);
829         break;
830     default:
831         assert(0);
832         break;
833     }
834 }
835
836 void lns_put(FILE *out, struct lens *lens, struct tree *tree,
837              const char *text, struct lns_error **err) {
838     struct state state;
839     struct lns_error *err1;
840
841     if (err != NULL)
842         *err = NULL;
843     if (tree == NULL)
844         return;
845
846     MEMZERO(&state, 1);
847     state.path = strdup("");
848     state.skel = lns_parse(lens, text, &state.dict, &err1);
849
850     if (err1 != NULL) {
851         if (err != NULL)
852             *err = err1;
853         else
854             free_lns_error(err1);
855         return;
856     }
857     state.out = out;
858     state.split = make_split(tree);
859     state.key = tree->label;
860     put_lens(lens, &state);
861
862     free(state.path);
863     free_split(state.split);
864     free_skel(state.skel);
865     free_dict(state.dict);
866     if (err != NULL) {
867         *err = state.error;
868     } else {
869         free_lns_error(state.error);
870     }
871 }
872
873 /*
874  * Local variables:
875  *  indent-tabs-mode: nil
876  *  c-indent-level: 4
877  *  c-basic-offset: 4
878  *  tab-width: 4
879  * End:
880  */