Imported Upstream version 1.8.0
[platform/upstream/augeas.git] / src / put.c
1 /*
2  * put.c:
3  *
4  * Copyright (C) 2007-2016 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             if (skel->tag != L_CONCAT)
389                 return 0;
390             struct skel *s = skel->skels;
391             for (int i=0; i < lens->nchildren; i++) {
392                 if (! skel_instance_of(lens->children[i], s))
393                     return 0;
394                 s = s->next;
395             }
396             return 1;
397         }
398         break;
399     case L_UNION:
400         {
401             for (int i=0; i < lens->nchildren; i++) {
402                 if (skel_instance_of(lens->children[i], skel))
403                     return 1;
404             }
405             return 0;
406         }
407         break;
408     case L_SUBTREE:
409         return skel->tag == L_SUBTREE;
410     case L_MAYBE:
411         return skel->tag == L_MAYBE || skel_instance_of(lens->child, skel);
412     case L_STAR:
413         if (skel->tag != L_STAR)
414             return 0;
415         list_for_each(s, skel->skels) {
416             if (! skel_instance_of(lens->child, s))
417                 return 0;
418         }
419         return 1;
420     case L_REC:
421         return skel_instance_of(lens->body, skel);
422     case L_SQUARE:
423         return skel->tag == L_SQUARE
424             && skel_instance_of(lens->child, skel->skels);
425     default:
426         BUG_ON(true, lens->info, "illegal lens tag %d", lens->tag);
427         break;
428     }
429  error:
430     return 0;
431 }
432
433 /*
434  * put
435  */
436 static void put_subtree(struct lens *lens, struct state *state) {
437     assert(lens->tag == L_SUBTREE);
438     struct state oldstate = *state;
439     struct split oldsplit = *state->split;
440     size_t oldpathlen = strlen(state->path);
441
442     struct tree *tree = state->split->tree;
443     struct split *split = NULL;
444
445     state->key = tree->label;
446     state->value = tree->value;
447     pathjoin(&state->path, 1, state->key);
448
449     split = make_split(tree->children);
450     set_split(state, split);
451
452     dict_lookup(tree->label, state->dict, &state->skel, &state->dict);
453     if (state->skel == NULL || ! skel_instance_of(lens->child, state->skel)) {
454         create_lens(lens->child, state);
455     } else {
456         put_lens(lens->child, state);
457     }
458     assert(state->error != NULL || state->split->next == NULL);
459
460     oldstate.error = state->error;
461     oldstate.path = state->path;
462     *state = oldstate;
463     *state->split= oldsplit;
464     free_split(split);
465     state->path[oldpathlen] = '\0';
466 }
467
468 static void put_del(ATTRIBUTE_UNUSED struct lens *lens, struct state *state) {
469     assert(lens->tag == L_DEL);
470     assert(state->skel != NULL);
471     assert(state->skel->tag == L_DEL);
472     if (state->override != NULL) {
473         fprintf(state->out, "%s", state->override);
474     } else {
475         fprintf(state->out, "%s", state->skel->text);
476     }
477 }
478
479 static void put_union(struct lens *lens, struct state *state) {
480     assert(lens->tag == L_UNION);
481
482     for (int i=0; i < lens->nchildren; i++) {
483         struct lens *l = lens->children[i];
484         if (applies(l, state)) {
485             if (skel_instance_of(l, state->skel))
486                 put_lens(l, state);
487             else
488                 create_lens(l, state);
489             return;
490         }
491     }
492     put_error(state, lens, "None of the alternatives in the union match");
493 }
494
495 static void put_concat(struct lens *lens, struct state *state) {
496     assert(lens->tag == L_CONCAT);
497     struct split *oldsplit = state->split;
498     struct skel *oldskel = state->skel;
499
500     struct split *split = split_concat(state, lens);
501
502     state->skel = state->skel->skels;
503     set_split(state, split);
504     for (int i=0; i < lens->nchildren; i++) {
505         if (state->split == NULL) {
506             put_error(state, lens,
507                       "Not enough components in concat");
508             list_free(split);
509             return;
510         }
511         put_lens(lens->children[i], state);
512         state->skel = state->skel->next;
513         next_split(state);
514     }
515     list_free(split);
516     set_split(state, oldsplit);
517     state->skel = oldskel;
518 }
519
520 static void error_quant_star(struct split *last_split, struct lens *lens,
521                              struct state *state) {
522     struct tree *child = NULL;
523     if (last_split != NULL) {
524         if (last_split->follow != NULL) {
525             child = last_split->follow;
526         } else {
527             for (child = last_split->tree;
528                  child != NULL && child->next != NULL;
529                  child = child->next);
530         }
531     }
532     if (child == NULL) {
533         put_error(state, lens, "Malformed child node");
534     } else {
535         put_error(state, lens, "Malformed child node '%s'", child->label);
536     }
537 }
538
539 static void put_quant_star(struct lens *lens, struct state *state) {
540     assert(lens->tag == L_STAR);
541     struct split *oldsplit = state->split;
542     struct skel *oldskel = state->skel;
543     struct split *last_split = NULL;
544
545     struct split *split = split_iter(state, lens);
546
547     state->skel = state->skel->skels;
548     set_split(state, split);
549     last_split = state->split;
550     while (state->split != NULL && state->skel != NULL) {
551         put_lens(lens->child, state);
552         state->skel = state->skel->next;
553         last_split = state->split;
554         next_split(state);
555     }
556     while (state->split != NULL) {
557         create_lens(lens->child, state);
558         last_split = state->split;
559         next_split(state);
560     }
561     if (state->pos != oldsplit->end)
562         error_quant_star(last_split, lens, state);
563     list_free(split);
564     set_split(state, oldsplit);
565     state->skel = oldskel;
566 }
567
568 static void put_quant_maybe(struct lens *lens, struct state *state) {
569     assert(lens->tag == L_MAYBE);
570     struct lens *child = lens->child;
571
572     if (applies(child, state)) {
573         if (skel_instance_of(child, state->skel))
574             put_lens(child, state);
575         else
576             create_lens(child, state);
577     }
578 }
579
580 static void put_store(struct lens *lens, struct state *state) {
581     if (state->value == NULL) {
582         put_error(state, lens,
583                   "Can not store a nonexistent (NULL) value");
584     } else if (regexp_match(lens->regexp, state->value, strlen(state->value),
585                             0, NULL) != strlen(state->value)) {
586         char *pat = regexp_escape(lens->regexp);
587         put_error(state, lens,
588                   "Value '%s' does not match regexp /%s/ in store lens",
589                   state->value, pat);
590         free(pat);
591     } else {
592         fprintf(state->out, "%s", state->value);
593     }
594 }
595
596 static void put_rec(struct lens *lens, struct state *state) {
597     put_lens(lens->body, state);
598 }
599
600 static void put_square(struct lens *lens, struct state *state) {
601     assert(lens->tag == L_SQUARE);
602     struct skel *oldskel = state->skel;
603     struct split *oldsplit = state->split;
604     struct lens *concat = lens->child;
605     struct lens *left = concat->children[0];
606     struct split *split = split_concat(state, concat);
607
608     /* skels of concat is one depth more */
609     state->skel = state->skel->skels->skels;
610     set_split(state, split);
611     for (int i=0; i < concat->nchildren; i++) {
612         if (state->split == NULL) {
613             put_error(state, concat, "Not enough components in square");
614             list_free(split);
615             return;
616         }
617         struct lens *curr = concat->children[i];
618         if (i == (concat->nchildren - 1) && left->tag == L_KEY)
619             state->override = state->key;
620         put_lens(curr, state);
621         state->override = NULL;
622         state->skel = state->skel->next;
623         next_split(state);
624     }
625     list_free(split);
626     set_split(state, oldsplit);
627     state->skel = oldskel;
628 }
629
630 static void put_lens(struct lens *lens, struct state *state) {
631     if (state->error != NULL)
632         return;
633
634     switch(lens->tag) {
635     case L_DEL:
636         put_del(lens, state);
637         break;
638     case L_STORE:
639         put_store(lens, state);
640         break;
641     case L_KEY:
642         fprintf(state->out, "%s", state->key);
643         break;
644     case L_LABEL:
645     case L_VALUE:
646         /* Nothing to do */
647         break;
648     case L_SEQ:
649         /* Nothing to do */
650         break;
651     case L_COUNTER:
652         /* Nothing to do */
653         break;
654     case L_CONCAT:
655         put_concat(lens, state);
656         break;
657     case L_UNION:
658         put_union(lens, state);
659         break;
660     case L_SUBTREE:
661         put_subtree(lens, state);
662         break;
663     case L_STAR:
664         put_quant_star(lens, state);
665         break;
666     case L_MAYBE:
667         put_quant_maybe(lens, state);
668         break;
669     case L_REC:
670         put_rec(lens, state);
671         break;
672     case L_SQUARE:
673         put_square(lens, state);
674         break;
675     default:
676         assert(0);
677         break;
678     }
679 }
680
681 static void create_subtree(struct lens *lens, struct state *state) {
682     put_subtree(lens, state);
683 }
684
685 static void create_del(struct lens *lens, struct state *state) {
686     assert(lens->tag == L_DEL);
687     if (state->override != NULL) {
688         print_escaped_chars(state->out, state->override);
689     } else {
690         print_escaped_chars(state->out, lens->string->str);
691     }
692 }
693
694 static void create_union(struct lens *lens, struct state *state) {
695     assert(lens->tag == L_UNION);
696
697     for (int i=0; i < lens->nchildren; i++) {
698         if (applies(lens->children[i], state)) {
699             create_lens(lens->children[i], state);
700             return;
701         }
702     }
703     put_error(state, lens, "None of the alternatives in the union match");
704 }
705
706 static void create_concat(struct lens *lens, struct state *state) {
707     assert(lens->tag == L_CONCAT);
708     struct split *oldsplit = state->split;
709
710     struct split *split = split_concat(state, lens);
711
712     set_split(state, split);
713     for (int i=0; i < lens->nchildren; i++) {
714         if (state->split == NULL) {
715             put_error(state, lens,
716                       "Not enough components in concat");
717             list_free(split);
718             return;
719         }
720         create_lens(lens->children[i], state);
721         next_split(state);
722     }
723     list_free(split);
724     set_split(state, oldsplit);
725 }
726
727 static void create_square(struct lens *lens, struct state *state) {
728     assert(lens->tag == L_SQUARE);
729     struct lens *concat = lens->child;
730
731     struct split *oldsplit = state->split;
732     struct split *split = split_concat(state, concat);
733     struct lens *left = concat->children[0];
734
735     set_split(state, split);
736     for (int i=0; i < concat->nchildren; i++) {
737         if (state->split == NULL) {
738             put_error(state, concat, "Not enough components in square");
739             list_free(split);
740             return;
741         }
742         struct lens *curr = concat->children[i];
743         if (i == (concat->nchildren - 1) && left->tag == L_KEY)
744             state->override = state->key;
745         create_lens(curr, state);
746         state->override = NULL;
747         next_split(state);
748     }
749     list_free(split);
750     set_split(state, oldsplit);
751 }
752
753 static void create_quant_star(struct lens *lens, struct state *state) {
754     assert(lens->tag == L_STAR);
755     struct split *oldsplit = state->split;
756     struct split *last_split = NULL;
757
758     struct split *split = split_iter(state, lens);
759
760     set_split(state, split);
761     last_split = state->split;
762     while (state->split != NULL) {
763         create_lens(lens->child, state);
764         last_split = state->split;
765         next_split(state);
766     }
767     if (state->pos != oldsplit->end)
768         error_quant_star(last_split, lens, state);
769     list_free(split);
770     set_split(state, oldsplit);
771 }
772
773 static void create_quant_maybe(struct lens *lens, struct state *state) {
774     assert(lens->tag == L_MAYBE);
775
776     if (applies(lens->child, state)) {
777         create_lens(lens->child, state);
778     }
779 }
780
781 static void create_rec(struct lens *lens, struct state *state) {
782     create_lens(lens->body, state);
783 }
784
785 static void create_lens(struct lens *lens, struct state *state) {
786     if (state->error != NULL)
787         return;
788     switch(lens->tag) {
789     case L_DEL:
790         create_del(lens, state);
791         break;
792     case L_STORE:
793         put_store(lens, state);
794         break;
795     case L_KEY:
796         fprintf(state->out, "%s", state->key);
797         break;
798     case L_LABEL:
799     case L_VALUE:
800         /* Nothing to do */
801         break;
802     case L_SEQ:
803         /* Nothing to do */
804         break;
805     case L_COUNTER:
806         /* Nothing to do */
807         break;
808     case L_CONCAT:
809         create_concat(lens, state);
810         break;
811     case L_UNION:
812         create_union(lens, state);
813         break;
814     case L_SUBTREE:
815         create_subtree(lens, state);
816         break;
817     case L_STAR:
818         create_quant_star(lens, state);
819         break;
820     case L_MAYBE:
821         create_quant_maybe(lens, state);
822         break;
823     case L_REC:
824         create_rec(lens, state);
825         break;
826     case L_SQUARE:
827         create_square(lens, state);
828         break;
829     default:
830         assert(0);
831         break;
832     }
833 }
834
835 void lns_put(FILE *out, struct lens *lens, struct tree *tree,
836              const char *text, struct lns_error **err) {
837     struct state state;
838     struct lns_error *err1;
839
840     if (err != NULL)
841         *err = NULL;
842     if (tree == NULL)
843         return;
844
845     MEMZERO(&state, 1);
846     state.path = strdup("");
847     state.skel = lns_parse(lens, text, &state.dict, &err1);
848
849     if (err1 != NULL) {
850         if (err != NULL)
851             *err = err1;
852         else
853             free_lns_error(err1);
854         goto error;
855     }
856     state.out = out;
857     state.split = make_split(tree);
858     state.key = tree->label;
859     put_lens(lens, &state);
860     if (err != NULL) {
861         *err = state.error;
862     } else {
863         free_lns_error(state.error);
864     }
865
866  error:
867     free(state.path);
868     free_split(state.split);
869     free_skel(state.skel);
870     free_dict(state.dict);
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  */