tweaked include directives so gcc -MM generates better dependency files
[comedilib.git] / lib / error.c
1 /*
2     lib/error.c
3     error functions and data
4
5     COMEDILIB - Linux Control and Measurement Device Interface Library
6     Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation, version 2.1
11     of the License.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Lesser General Public License for more details.
17
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21     USA.
22 */
23
24 #include "libinternal.h"
25
26 #include <stdio.h>
27 #include <string.h>
28
29 char *__comedilib_error_strings[]={
30         _s("No error"),
31         _s("Unknown error"),
32         _s("Bad comedi_t structure"),
33         _s("Invalid subdevice"),
34         _s("Invalid channel"),
35         _s("Buffer overflow"),
36         _s("Buffer underflow"),
37         _s("Command not supported"),
38         _s("Not supported"),
39 };
40 #define n_errors (sizeof(__comedilib_error_strings)/sizeof(void *))
41
42 int __comedi_loglevel=1;
43 int __comedi_errno=0;
44
45 EXPORT_SYMBOL(comedi_loglevel,0.7.18);
46 int comedi_loglevel(int loglevel)
47 {
48         int old_loglevel=__comedi_loglevel;
49         
50         __comedi_loglevel=loglevel;
51         
52         return old_loglevel;
53 }
54
55 EXPORT_SYMBOL(comedi_errno,0.7.18);
56 int comedi_errno(void)
57 {
58         return __comedi_errno;
59 }
60
61 EXPORT_SYMBOL(comedi_strerror,0.7.18);
62 char *comedi_strerror(int errnum)
63 {
64         if(errnum<COMEDI_NOERROR || errnum>=COMEDI_NOERROR+n_errors)
65                 return strerror(errnum);
66
67         return GETTEXT(__comedilib_error_strings[errnum-COMEDI_NOERROR]);
68 }
69
70 EXPORT_SYMBOL(comedi_perror,0.7.18);
71 void comedi_perror(const char *s)
72 {
73         if(__comedi_loglevel>=3){
74                 fprintf(stderr,"comedi_perror(): __comedi_errno=%d\n",__comedi_errno);
75         }
76         if(!s)s="comedilib";
77         fprintf(stderr,"%s: %s\n",s,comedi_strerror(__comedi_errno));
78 }
79
80 void libc_error(void)
81 {
82         __comedi_errno=errno;
83         if(__comedi_loglevel>=2){
84                 comedi_perror("libc error");
85         }
86 }
87
88 void internal_error(int err)
89 {
90         __comedi_errno=err;
91         if(__comedi_loglevel>=2){
92                 comedi_perror("internal error");
93         }
94 }
95
96
97
98 int valid_dev(comedi_t *it)
99 {
100         if(!it || it->magic!=COMEDILIB_MAGIC){
101                 internal_error(COMEDILIB_BADDEV);
102                 return 0;
103         }
104         
105         return 1;
106 }
107
108 int valid_subd(comedi_t *it,unsigned int subd)
109 {
110         if(!valid_dev(it))return 0;
111         if(subd>=it->n_subdevices){
112                 internal_error(COMEDILIB_BADSUBD);
113                 return 0;
114         }
115         
116         return 1;
117 }
118
119 int valid_chan(comedi_t *it,unsigned int subd,unsigned int chan)
120 {
121         if(!valid_subd(it,subd))return 0;
122         if(chan>=it->subdevices[subd].n_chan){
123                 internal_error(COMEDILIB_BADCHAN);
124                 return 0;
125         }
126         
127         return 1;
128 }
129
130