message: Break message location out into a separate struct.
[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 "libpspp/compiler.h"
23
24 struct string;
25
26 /* What kind of message is this? */
27 enum msg_category
28   {
29     MSG_C_GENERAL,              /* General info. */
30     MSG_C_SYNTAX,               /* Messages that relate to syntax files. */
31     MSG_C_DATA,                 /* Messages that relate to data files. */
32     MSG_N_CATEGORIES
33   };
34
35 /* How important a condition is it? */
36 enum msg_severity
37   {
38     MSG_S_ERROR,
39     MSG_S_WARNING,
40     MSG_S_NOTE,
41     MSG_N_SEVERITIES
42   };
43
44 const char *msg_severity_to_string (enum msg_severity);
45
46 /* Combination of a category and a severity for convenience. */
47 enum msg_class
48   {
49     ME, MW, MN,                 /* General error/warning/note. */
50     SE, SW, SN,                 /* Script error/warning/note. */
51     DE, DW, DN,                 /* Data-file error/note. */
52     MSG_CLASS_CNT,
53   };
54
55 static inline enum msg_category
56 msg_class_to_category (enum msg_class class)
57 {
58   return class / 3;
59 }
60
61 static inline enum msg_severity
62 msg_class_to_severity (enum msg_class class)
63 {
64   return class % 3;
65 }
66
67 static inline enum msg_class
68 msg_class_from_category_and_severity (enum msg_category category,
69                                       enum msg_severity severity)
70 {
71   return category * 3 + severity;
72 }
73
74 struct msg_location
75   {
76     char *file_name;            /* Name of file containing error, or NULL. */
77     int first_line;             /* 1-based line number, or 0 if none. */
78     int last_line;              /* 1-based exclusive last line (0=none). */
79     int first_column;           /* 1-based first column, or 0 if none. */
80     int last_column;            /* 1-based exclusive last column (0=none). */
81   };
82
83 void msg_location_destroy (struct msg_location *);
84 struct msg_location *msg_location_dup (const struct msg_location *);
85
86 bool msg_location_is_empty (const struct msg_location *);
87 void msg_location_format (const struct msg_location *, struct string *);
88
89 /* A message. */
90 struct msg
91   {
92     enum msg_category category; /* Message category. */
93     enum msg_severity severity; /* Message severity. */
94     struct msg_location *location; /* Code location. */
95     char *command_name;         /* Name of erroneous command, or NULL.  */
96     char *text;                 /* Error text. */
97   };
98
99 /* Initialization. */
100 void msg_set_handler (void (*handler) (const struct msg *, void *lexer),
101                       void *aux);
102
103 /* Working with messages. */
104 struct msg *msg_dup (const struct msg *);
105 void msg_destroy(struct msg *);
106 char *msg_to_string (const struct msg *);
107
108 /* Emitting messages. */
109 void vmsg (enum msg_class class, const char *format, va_list args)
110      PRINTF_FORMAT (2, 0);
111 void msg (enum msg_class, const char *format, ...)
112      PRINTF_FORMAT (2, 3);
113 void msg_emit (struct msg *);
114
115 void msg_error (int errnum, const char *format, ...)
116   PRINTF_FORMAT (2, 3);
117
118
119 /* Enable and disable messages. */
120 void msg_enable (void);
121 void msg_disable (void);
122
123 /* Error context. */
124 bool msg_ui_too_many_errors (void);
125 void msg_ui_reset_counts (void);
126 bool msg_ui_any_errors (void);
127 void msg_ui_disable_warnings (bool);
128
129
130 /* Used in panic situations only. */
131 const char * prepare_diagnostic_information (void);
132 const char * prepare_fatal_error_message (void);
133 void request_bug_report (const char *msg);
134
135
136 #endif /* message.h */