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