bdf882dafd21ae44dfca28df5ecd03ea16ab70d4
[alexxy/gromacs.git] / src / gromacs / gmxlib / warninp.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #include "gmxpre.h"
38
39 #include "config.h"
40
41 #include <string.h>
42
43 #include "gromacs/utility/smalloc.h"
44 #include "gromacs/legacyheaders/copyrite.h"
45 #include "gromacs/utility/cstringutil.h"
46 #include "gromacs/utility/fatalerror.h"
47 #include "gromacs/legacyheaders/warninp.h"
48
49 typedef struct warninp {
50     gmx_bool bAllowWarnings;
51     int      nwarn_note;
52     int      nwarn_warn;
53     int      nwarn_error;
54     int      maxwarn;
55     int      lineno;
56     char     filenm[256];
57 } t_warninp;
58
59 warninp_t init_warning(gmx_bool bAllowWarnings, int maxwarning)
60 {
61     warninp_t wi;
62
63     snew(wi, 1);
64
65     wi->bAllowWarnings = bAllowWarnings;
66     wi->maxwarn        = maxwarning;
67     wi->nwarn_note     = 0;
68     wi->nwarn_warn     = 0;
69     wi->nwarn_error    = 0;
70     strcpy(wi->filenm, "unknown");
71     wi->lineno         = 0;
72
73     return wi;
74 }
75
76 void set_warning_line(warninp_t wi, const char *s, int line)
77 {
78     if (s == NULL)
79     {
80         gmx_incons("Calling set_warning_line with NULL pointer");
81     }
82
83     strcpy(wi->filenm, s);
84     wi->lineno = line;
85 }
86
87 int get_warning_line(warninp_t wi)
88 {
89     return wi->lineno;
90 }
91
92 const char *get_warning_file(warninp_t wi)
93 {
94     return wi->filenm;
95 }
96
97 static void low_warning(warninp_t wi, const char *wtype, int n, const char *s)
98 {
99 #define indent 2
100     char *temp, *temp2;
101     int   i;
102
103     if (s == NULL)
104     {
105         s = "Empty error message.";
106     }
107     snew(temp, strlen(s)+indent+1);
108     for (i = 0; i < indent; i++)
109     {
110         temp[i] = ' ';
111     }
112     temp[indent] = '\0';
113     strcat(temp, s);
114     temp2 = wrap_lines(temp, 78-indent, indent, FALSE);
115     if (strlen(wi->filenm) > 0)
116     {
117         if (wi->lineno != -1)
118         {
119             fprintf(stderr, "\n%s %d [file %s, line %d]:\n%s\n\n",
120                     wtype, n, wi->filenm, wi->lineno, temp2);
121         }
122         else
123         {
124             fprintf(stderr, "\n%s %d [file %s]:\n%s\n\n",
125                     wtype, n, wi->filenm, temp2);
126         }
127     }
128     else
129     {
130         fprintf(stderr, "\n%s %d:\n%s\n\n", wtype, n, temp2);
131     }
132     sfree(temp);
133     sfree(temp2);
134 }
135
136 void warning(warninp_t wi, const char *s)
137 {
138     if (wi->bAllowWarnings)
139     {
140         wi->nwarn_warn++;
141         low_warning(wi, "WARNING", wi->nwarn_warn, s);
142     }
143     else
144     {
145         warning_error(wi, s);
146     }
147 }
148
149 void warning_note(warninp_t wi, const char *s)
150 {
151     wi->nwarn_note++;
152     low_warning(wi, "NOTE", wi->nwarn_note, s);
153 }
154
155 void warning_error(warninp_t wi, const char *s)
156 {
157     wi->nwarn_error++;
158     low_warning(wi, "ERROR", wi->nwarn_error, s);
159 }
160
161 static void print_warn_count(const char *type, int n)
162 {
163     if (n > 0)
164     {
165         fprintf(stderr, "\nThere %s %d %s%s\n",
166                 (n == 1) ? "was" : "were", n, type, (n == 1) ? "" : "s");
167     }
168 }
169
170 void check_warning_error(warninp_t wi, int f_errno, const char *file, int line)
171 {
172     if (wi->nwarn_error > 0)
173     {
174         print_warn_count("note", wi->nwarn_note);
175         print_warn_count("warning", wi->nwarn_warn);
176
177         gmx_fatal(f_errno, file, line, "There %s %d error%s in input file(s)",
178                   (wi->nwarn_error == 1) ? "was" : "were", wi->nwarn_error,
179                   (wi->nwarn_error == 1) ? ""    : "s");
180     }
181 }
182
183 void done_warning(warninp_t wi, int f_errno, const char *file, int line)
184 {
185     print_warn_count("note", wi->nwarn_note);
186     print_warn_count("warning", wi->nwarn_warn);
187
188     check_warning_error(wi, f_errno, file, line);
189
190     if (wi->maxwarn >= 0 && wi->nwarn_warn > wi->maxwarn)
191     {
192         gmx_fatal(f_errno, file, line,
193                   "Too many warnings (%d), %s terminated.\n"
194                   "If you are sure all warnings are harmless, use the -maxwarn option.",
195                   wi->nwarn_warn, ShortProgram());
196     }
197
198     sfree(wi);
199 }
200
201 void _too_few(warninp_t wi, const char *fn, int line)
202 {
203     char buf[STRLEN];
204
205     sprintf(buf,
206             "Too few parameters on line (source file %s, line %d)",
207             fn, line);
208     warning(wi, buf);
209 }
210
211 void _incorrect_n_param(warninp_t wi, const char *fn, int line)
212 {
213     char buf[STRLEN];
214
215     sprintf(buf,
216             "Incorrect number of parameters on line (source file %s, line %d)",
217             fn, line);
218     warning(wi, buf);
219 }