1 /* grecs - Gray's Extensible Configuration System
2    Copyright (C) 2007-2016 Sergey Poznyakoff
3 
4    Grecs is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 3 of the License, or (at your
7    option) any later version.
8 
9    Grecs is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with Grecs. If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <grecs.h>
21 #include <wordsplit.h>
22 
23 static struct grecs_txtacc *line_acc;
24 
25 void
grecs_line_acc_create()26 grecs_line_acc_create()
27 {
28 	line_acc = grecs_txtacc_create();
29 }
30 
31 void
grecs_line_acc_free()32 grecs_line_acc_free()
33 {
34 	grecs_txtacc_free(line_acc);
35 	line_acc = NULL;
36 }
37 
38 void
grecs_line_acc_grow_char(int c)39 grecs_line_acc_grow_char(int c)
40 {
41 	char t = c;
42 	grecs_txtacc_grow(line_acc, &t, 1);
43 }
44 
45 int
grecs_line_acc_grow_char_unescape(int c)46 grecs_line_acc_grow_char_unescape(int c)
47 {
48 	if (c != '\n') {
49 		int uc = wordsplit_c_unquote_char(c);
50 		grecs_line_acc_grow_char(uc ? uc : c);
51 		return !uc;
52 	}
53 	return 0;
54 }
55 
56 void
grecs_line_acc_grow(const char * text,size_t len)57 grecs_line_acc_grow(const char *text, size_t len)
58 {
59 	grecs_txtacc_grow(line_acc, text, len);
60 }
61 
62 /* Same, but unescapes the last character from text */
63 void
grecs_line_acc_grow_unescape_last(char * text,size_t len,grecs_locus_t const * loc)64 grecs_line_acc_grow_unescape_last(char *text, size_t len,
65 				  grecs_locus_t const *loc)
66 {
67 	grecs_txtacc_grow(line_acc, text, len - 2);
68 	if (grecs_line_acc_grow_char_unescape(text[len - 1]) && loc)
69 		grecs_warning(loc, 0,
70 			      _("unknown escape sequence: '\\%c'"),
71 			      text[len - 1]);
72 }
73 
74 void
grecs_line_begin()75 grecs_line_begin()
76 {
77 	if (!line_acc)
78 		grecs_line_acc_create();
79 }
80 
81 char *
grecs_line_finish()82 grecs_line_finish()
83 {
84 	grecs_line_acc_grow_char(0);
85 	return grecs_txtacc_finish(line_acc, 1);
86 }
87 
88 
89 
90 
91