dio fixes
[comedilib.git] / lib / error.c
1 /*
2     lib/error.c
3     error handling routines
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1997-8 David A. Schleef <ds@stm.lbl.gov>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program 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
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23
24 #include <libinternal.h>
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29
30 char *__comedilib_error_strings[]={
31         "No error",
32         "Unknown error",
33         "Bad comedi_t structure",
34         "Invalid subdevice",
35         "Invalid channel",
36 };
37
38 int __comedi_loglevel=1;
39 int __comedi_errno=0;
40
41 int comedi_loglevel(int loglevel)
42 {
43         int old_loglevel=__comedi_loglevel;
44         
45         __comedi_loglevel=loglevel;
46         
47         return old_loglevel;
48 }
49
50 int comedi_errno(void)
51 {
52         return __comedi_errno;
53 }
54
55 char *comedi_strerror(int errnum)
56 {
57         if(errnum<COMEDILIB_NOERROR ||
58            errnum>=COMEDILIB_NOERROR+sizeof(__comedilib_error_strings)
59            /sizeof(__comedilib_error_strings[0]))
60                 return strerror(errnum);
61
62         return __comedilib_error_strings[errnum-COMEDILIB_NOERROR];
63 }
64
65 void comedi_perror(const char *s)
66 {
67         if(__comedi_loglevel>=3){
68                 fprintf(stderr,"comedi_perror(): __comedi_errno=%d\n",__comedi_errno);
69         }
70         if(!s)s="comedilib";
71         fprintf(stderr,"%s: %s\n",s,comedi_strerror(__comedi_errno));
72 }
73
74 void libc_error(void)
75 {
76         __comedi_errno=errno;
77         if(__comedi_loglevel>=2){
78                 comedi_perror("libc error");
79         }
80 }
81
82 void internal_error(int err)
83 {
84         __comedi_errno=err;
85         if(__comedi_loglevel>=2){
86                 comedi_perror("internal error");
87         }
88 }
89
90
91
92 int valid_dev(comedi_t *it)
93 {
94         if(!it || it->magic!=COMEDILIB_MAGIC){
95                 internal_error(COMEDILIB_BADDEV);
96                 return 0;
97         }
98         
99         return 1;
100 }
101
102 int valid_subd(comedi_t *it,unsigned int subd)
103 {
104         if(!valid_dev(it))return 0;
105         if(subd>=it->n_subdevices){
106                 internal_error(COMEDILIB_BADSUBD);
107                 return 0;
108         }
109         
110         return 1;
111 }
112
113 int valid_chan(comedi_t *it,unsigned int subd,unsigned int chan)
114 {
115         if(!valid_subd(it,subd))return 0;
116         if(chan>=it->subdevices[subd].n_chan){
117                 internal_error(COMEDILIB_BADCHAN);
118                 return 0;
119         }
120         
121         return 1;
122 }
123
124