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