File: error.c

package info (click to toggle)
comedilib 0.7.22-2.2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,376 kB
  • ctags: 2,466
  • sloc: ansic: 16,316; sh: 8,492; ruby: 1,360; makefile: 326; yacc: 262; perl: 257; lex: 79; python: 59
file content (130 lines) | stat: -rw-r--r-- 2,939 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
    lib/error.c
    error functions and data

    COMEDILIB - Linux Control and Measurement Device Interface Library
    Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation, version 2.1
    of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
    USA.
*/

#include "libinternal.h"

#include <stdio.h>
#include <string.h>

char *__comedilib_error_strings[]={
	_s("No error"),
	_s("Unknown error"),
	_s("Bad comedi_t structure"),
	_s("Invalid subdevice"),
	_s("Invalid channel"),
	_s("Buffer overflow"),
	_s("Buffer underflow"),
	_s("Command not supported"),
	_s("Not supported"),
};
#define n_errors (sizeof(__comedilib_error_strings)/sizeof(void *))

int __comedi_loglevel=1;
int __comedi_errno=0;

EXPORT_ALIAS_DEFAULT(_comedi_loglevel,comedi_loglevel,0.7.18);
int _comedi_loglevel(int loglevel)
{
	int old_loglevel=__comedi_loglevel;
	
	__comedi_loglevel=loglevel;
	
	return old_loglevel;
}

EXPORT_ALIAS_DEFAULT(_comedi_errno,comedi_errno,0.7.18);
int _comedi_errno(void)
{
	return __comedi_errno;
}

EXPORT_ALIAS_DEFAULT(_comedi_strerror,comedi_strerror,0.7.18);
char* _comedi_strerror(int errnum)
{
	if(errnum<COMEDI_NOERROR || errnum>=COMEDI_NOERROR+n_errors)
		return strerror(errnum);

	return GETTEXT(__comedilib_error_strings[errnum-COMEDI_NOERROR]);
}

EXPORT_ALIAS_DEFAULT(_comedi_perror,comedi_perror,0.7.18);
void _comedi_perror(const char *s)
{
	if(__comedi_loglevel>=3){
		fprintf(stderr,"comedi_perror(): __comedi_errno=%d\n",__comedi_errno);
	}
	if(!s)s="comedilib";
	fprintf(stderr,"%s: %s\n",s,comedi_strerror(__comedi_errno));
}

void libc_error(void)
{
	__comedi_errno=errno;
	if(__comedi_loglevel>=2){
		comedi_perror("libc error");
	}
}

void internal_error(int err)
{
	__comedi_errno=err;
	if(__comedi_loglevel>=2){
		comedi_perror("internal error");
	}
}



int valid_dev(comedi_t *it)
{
	if(!it || it->magic!=COMEDILIB_MAGIC){
		internal_error(COMEDILIB_BADDEV);
		return 0;
	}
	
	return 1;
}

int valid_subd(comedi_t *it,unsigned int subd)
{
	if(!valid_dev(it))return 0;
	if(subd>=it->n_subdevices){
		internal_error(COMEDILIB_BADSUBD);
		return 0;
	}
	
	return 1;
}

int valid_chan(comedi_t *it,unsigned int subd,unsigned int chan)
{
	if(!valid_subd(it,subd))return 0;
	if(chan>=it->subdevices[subd].n_chan){
		internal_error(COMEDILIB_BADCHAN);
		return 0;
	}
	
	return 1;
}