Bump to 1.14.1
[platform/upstream/augeas.git] / src / info.c
1 /*
2  * info.c: filename/linenumber information for parser/interpreter
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 <lutter@redhat.com>
21  */
22
23 #include <config.h>
24 #include <stdbool.h>
25
26 #include "info.h"
27 #include "internal.h"
28 #include "memory.h"
29 #include "ref.h"
30 #include "errcode.h"
31
32 /*
33  * struct string
34  */
35 struct string *make_string(char *str) {
36     struct string *string;
37     make_ref(string);
38     string->str = str;
39     return string;
40 }
41
42 struct string *dup_string(const char *str) {
43     struct string *string;
44     make_ref(string);
45     if (str == NULL)
46         string->str = strdup("");
47     else
48         string->str = strdup(str);
49     if (string->str == NULL)
50         unref(string, string);
51     return string;
52 }
53
54 void free_string(struct string *string) {
55     if (string == NULL)
56         return;
57     assert(string->ref == 0);
58     free(string->str);
59     free(string);
60 }
61
62 /*
63  * struct info
64  */
65 char *format_info(struct info *info) {
66     const char *fname;
67     char *result = NULL;
68     int r = 0;
69
70     if (info == NULL) {
71         return strdup("(no file info)");
72     }
73
74     int fl = info->first_line, ll = info->last_line;
75     int fc = info->first_column, lc = info->last_column;
76     fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
77
78     if (fl > 0) {
79         if (fl == ll) {
80             if (fc == lc) {
81                 r = xasprintf(&result, "%s:%d.%d:", fname, fl, fc);
82             } else {
83                 r = xasprintf(&result, "%s:%d.%d-.%d:", fname, fl, fc, lc);
84             }
85         } else {
86             r = xasprintf(&result, "%s:%d.%d-%d.%d:", fname, fl, fc, ll, lc);
87         }
88     } else {
89         r = xasprintf(&result, "%s:", fname);
90     }
91     return (r == -1) ? NULL : result;
92 }
93
94 void print_info(FILE *out, struct info *info) {
95     if (info == NULL) {
96         fprintf(out, "(no file info):");
97         return;
98     }
99     fprintf(out, "%s:",
100             info->filename != NULL ? info->filename->str : "(unknown file)");
101     if (info->first_line > 0) {
102         if (info->first_line == info->last_line) {
103             if (info->first_column == info->last_column) {
104                 fprintf(out, "%d.%d:", info->first_line, info->first_column);
105             } else {
106                 fprintf(out, "%d.%d-.%d:", info->first_line,
107                         info->first_column, info->last_column);
108             }
109         } else {
110             fprintf(out, "%d.%d-%d.%d:",
111                     info->first_line, info->first_column,
112                     info->last_line, info->last_column);
113         }
114     }
115 }
116
117 bool typecheck_p(const struct info *info) {
118     return (info->error->aug->flags & AUG_TYPE_CHECK) != 0;
119 }
120
121 void free_info(struct info *info) {
122     if (info == NULL)
123         return;
124     assert(info->ref == 0);
125     unref(info->filename, string);
126     free(info);
127 }
128
129 struct span *make_span(struct info *info) {
130     struct span *span = NULL;
131     if (ALLOC(span) < 0) {
132         return NULL;
133     }
134     /* UINT_MAX means span is not initialized yet */
135     span->span_start = UINT_MAX;
136     span->filename = ref(info->filename);
137     return span;
138 }
139
140 void free_span(struct span *span) {
141     if (span == NULL)
142         return;
143     unref(span->filename, string);
144     free(span);
145 }
146
147 void print_span(struct span *span) {
148     if (span == NULL)
149         return;
150     printf("%s label=(%i:%i) value=(%i:%i) span=(%i,%i)\n",
151             span->filename->str,
152             span->label_start, span->label_end,
153             span->value_start, span->value_end,
154             span->span_start, span->span_end);
155 }
156
157 void update_span(struct span *node_info, int x, int y) {
158     if (node_info == NULL)
159         return;
160     if (node_info->span_start == UINT_MAX) {
161         node_info->span_start = x;
162         node_info->span_end = y;
163     } else {
164         if (node_info->span_start > x)
165             node_info->span_start = x;
166         if (node_info->span_end < y)
167             node_info->span_end = y;
168     }
169 }
170
171 /*
172  * Local variables:
173  *  indent-tabs-mode: nil
174  *  c-indent-level: 4
175  *  c-basic-offset: 4
176  *  tab-width: 4
177  * End:
178  */