Imported Upstream version 1.5.0
[platform/upstream/augeas.git] / src / info.c
1 /*
2  * info.c: filename/linenumber information for parser/interpreter
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 <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     int fl = info->first_line, ll = info->last_line;
67     int fc = info->first_column, lc = info->last_column;
68     fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
69
70     if (fl > 0) {
71         if (fl == ll) {
72             if (fc == lc) {
73                 r = xasprintf(&result, "%s:%d.%d:", fname, fl, fc);
74             } else {
75                 r = xasprintf(&result, "%s:%d.%d-.%d:", fname, fl, fc, lc);
76             }
77         } else {
78             r = xasprintf(&result, "%s:%d.%d-%d.%d:", fname, fl, fc, ll, lc);
79         }
80     } else {
81         r = xasprintf(&result, "%s:", fname);
82     }
83     return (r == -1) ? NULL : result;
84 }
85
86 void print_info(FILE *out, struct info *info) {
87     if (info == NULL) {
88         fprintf(out, "(no file info):");
89         return;
90     }
91     fprintf(out, "%s:",
92             info->filename != NULL ? info->filename->str : "(unknown file)");
93     if (info->first_line > 0) {
94         if (info->first_line == info->last_line) {
95             if (info->first_column == info->last_column) {
96                 fprintf(out, "%d.%d:", info->first_line, info->first_column);
97             } else {
98                 fprintf(out, "%d.%d-.%d:", info->first_line,
99                         info->first_column, info->last_column);
100             }
101         } else {
102             fprintf(out, "%d.%d-%d.%d:",
103                     info->first_line, info->first_column,
104                     info->last_line, info->last_column);
105         }
106     }
107 }
108
109 void free_info(struct info *info) {
110     if (info == NULL)
111         return;
112     assert(info->ref == 0);
113     unref(info->filename, string);
114     free(info);
115 }
116
117 struct span *make_span(struct info *info) {
118     struct span *span = NULL;
119     if (ALLOC(span) < 0) {
120         return NULL;
121     }
122     /* UINT_MAX means span is not initialized yet */
123     span->span_start = UINT_MAX;
124     span->filename = ref(info->filename);
125     return span;
126 }
127
128 void free_span(struct span *span) {
129     if (span == NULL)
130         return;
131     unref(span->filename, string);
132     free(span);
133 }
134
135 void print_span(struct span *span) {
136     if (span == NULL)
137         return;
138     printf("%s label=(%i:%i) value=(%i:%i) span=(%i,%i)\n",
139             span->filename->str,
140             span->label_start, span->label_end,
141             span->value_start, span->value_end,
142             span->span_start, span->span_end);
143 }
144
145 void update_span(struct span *node_info, int x, int y) {
146     if (node_info == NULL)
147         return;
148     if (node_info->span_start == UINT_MAX) {
149         node_info->span_start = x;
150         node_info->span_end = y;
151     } else {
152         if (node_info->span_start > x)
153             node_info->span_start = x;
154         if (node_info->span_end < y)
155             node_info->span_end = y;
156     }
157 }
158
159 /*
160  * Local variables:
161  *  indent-tabs-mode: nil
162  *  c-indent-level: 4
163  *  c-basic-offset: 4
164  *  tab-width: 4
165  * End:
166  */