CTABLES
[pspp] / src / libpspp / message.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef MESSAGE_H
18 #define MESSAGE_H 1
19
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include "libpspp/compiler.h"
24
25 struct string;
26
27 /* What kind of message is this? */
28 enum msg_category
29   {
30     MSG_C_GENERAL,              /* General info. */
31     MSG_C_SYNTAX,               /* Messages that relate to syntax files. */
32     MSG_C_DATA,                 /* Messages that relate to data files. */
33     MSG_N_CATEGORIES
34   };
35
36 /* How important a condition is it? */
37 enum msg_severity
38   {
39     MSG_S_ERROR,
40     MSG_S_WARNING,
41     MSG_S_NOTE,
42     MSG_N_SEVERITIES
43   };
44
45 const char *msg_severity_to_string (enum msg_severity);
46
47 /* Combination of a category and a severity for convenience. */
48 enum msg_class
49   {
50     ME, MW, MN,                 /* General error/warning/note. */
51     SE, SW, SN,                 /* Script error/warning/note. */
52     DE, DW, DN,                 /* Data-file error/note. */
53     MSG_CLASS_CNT,
54   };
55
56 static inline enum msg_category
57 msg_class_to_category (enum msg_class class)
58 {
59   return class / 3;
60 }
61
62 static inline enum msg_severity
63 msg_class_to_severity (enum msg_class class)
64 {
65   return class % 3;
66 }
67
68 static inline enum msg_class
69 msg_class_from_category_and_severity (enum msg_category category,
70                                       enum msg_severity severity)
71 {
72   return category * 3 + severity;
73 }
74
75 /* A line number and column number within a source file.  Both are 1-based.  If
76    only a line number is available, 'column' is zero.  If neither is available,
77    'line' and 'column' are zero.
78
79    Column numbers are measured according to the width of characters as shown in
80    a typical fixed-width font, in which CJK characters have width 2 and
81    combining characters have width 0.  */
82 struct msg_point
83   {
84     int line;
85     int column;
86   };
87
88 /* Location of the cause of an error. */
89 struct msg_location
90   {
91     /* Interned file name, or NULL. */
92     const char *file_name;
93
94     /* Nonnull if this came from a source file. */
95     struct lex_source *src;
96
97     /* The starting and ending point of the cause.  One of:
98
99        - Both empty, with all their members zero.
100
101        - A range of lines, with 0 < start.line <= end.line and start.column =
102          end.column = 0.
103
104        - A range of columns spanning one or more lines.  If it's on a single
105          line, then start.line = end.line and 0 < start.column <= end.column.
106          If it's across multiple lines, then 0 < start.line < end.line and the
107          column members are both positive.
108
109        Both 'start' and 'end' are inclusive, line-wise and column-wise.
110     */
111     struct msg_point start, end;
112   };
113
114 void msg_location_uninit (struct msg_location *);
115 void msg_location_destroy (struct msg_location *);
116 struct msg_location *msg_location_dup (const struct msg_location *);
117
118 void msg_location_remove_columns (struct msg_location *);
119
120 void msg_location_merge (struct msg_location **, const struct msg_location *);
121 struct msg_location *msg_location_merged (const struct msg_location *,
122                                           const struct msg_location *);
123
124 bool msg_location_is_empty (const struct msg_location *);
125 void msg_location_format (const struct msg_location *, struct string *);
126
127 struct msg_stack
128   {
129     struct msg_location *location;
130     char *description;
131   };
132
133 void msg_stack_destroy (struct msg_stack *);
134 struct msg_stack *msg_stack_dup (const struct msg_stack *);
135
136 /* A message. */
137 struct msg
138   {
139     enum msg_category category; /* Message category. */
140     enum msg_severity severity; /* Message severity. */
141     struct msg_location *location; /* Code location. */
142     struct msg_stack **stack;
143     size_t n_stack;
144     char *command_name;         /* Name of erroneous command, or NULL.  */
145     char *text;                 /* Error text. */
146   };
147
148 /* Initialization. */
149 struct msg_handler
150   {
151     void (*output_msg) (const struct msg *, void *aux);
152     void *aux;
153
154     void (*lex_source_ref) (const struct lex_source *);
155     void (*lex_source_unref) (struct lex_source *);
156     struct substring (*lex_source_get_line) (const struct lex_source *,
157                                              int line);
158   };
159 void msg_set_handler (const struct msg_handler *);
160
161 /* Working with messages. */
162 struct msg *msg_dup (const struct msg *);
163 void msg_destroy(struct msg *);
164 char *msg_to_string (const struct msg *);
165
166 /* Emitting messages. */
167 void vmsg (enum msg_class, const struct msg_location *,
168            const char *format, va_list args)
169      PRINTF_FORMAT (3, 0);
170 void msg (enum msg_class, const char *format, ...)
171      PRINTF_FORMAT (2, 3);
172 void msg_at (enum msg_class, const struct msg_location *,
173              const char *format, ...)
174      PRINTF_FORMAT (3, 4);
175 void msg_emit (struct msg *);
176
177 void msg_error (int errnum, const char *format, ...)
178   PRINTF_FORMAT (2, 3);
179
180
181 /* Enable and disable messages. */
182 void msg_enable (void);
183 void msg_disable (void);
184
185 /* Error context. */
186 bool msg_ui_too_many_errors (void);
187 void msg_ui_reset_counts (void);
188 bool msg_ui_any_errors (void);
189 void msg_ui_disable_warnings (bool);
190
191
192 /* Used in panic situations only. */
193 const char * prepare_diagnostic_information (void);
194 const char * prepare_fatal_error_message (void);
195 void request_bug_report (const char *msg);
196
197
198 #endif /* message.h */