File: error.c

package info (click to toggle)
libticables 1.3.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,352 kB
  • ctags: 2,309
  • sloc: sh: 11,957; ansic: 11,866; makefile: 153; sed: 16
file content (366 lines) | stat: -rw-r--r-- 10,257 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Hey EMACS -*- linux-c -*- */
/* $Id$ */

/*  libticables2 - link cable library, a part of the TiLP project
 *  Copyright (C) 1999-2005  Romain Lievin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __WIN32__
#include <windows.h>
#endif
#include <glib.h>
#include <errno.h>

#include "config.h"

#include "gettext.h"
#include "export1.h"
#include "error.h"
#include "logging.h"

/**
 * ticables_error_get:
 * @number: error number (see error.h for list).
 * @message: a newly GLib allocated string which contains corresponding error *message.
 *
 * Attempt to match the message corresponding to the error number. The returned
 * string must be freed when no longer needed.
 * The error message has the following format:
 * - 1: the error message
 * - 2: the cause(s), explanations on how to fix it
 * - 3: the error returned by the system
 *
 * Return value: 0 if error has been caught, the error number otherwise (propagation).
 **/
TIEXPORT1 int TICALL ticables_error_get(CableError number, char **message)
{
	char *tmp;

	if (message == NULL)
	{
		ticables_critical("ticables_error_get(NULL)\n");
		return number;
	}

	switch(number)
	{
		case ERR_BUSY:
			*message = g_strconcat(
			_("Msg: link cable is busy."),
			"\n",
			_("Cause: a transfer is in progress."),
			NULL);
		break;

		case ERR_ROOT:
			*message = g_strconcat(
			_("Msg: unable to use parallel/serial port: access refused."),
			"\n",
			_("Cause: you are not running program as root or setuid root."),
			NULL);
		break;

		case ERR_TTDEV:
			*message = g_strconcat(
			_("Msg: unable to use serial (/dev/ttySx) device."),
			"\n",
			_("Cause: the /dev/ttySx node doesn't exist or you don't have required permissions. " \
				"How to grant permission to devices to the user depends on the " \
				"distribution, it may involve adding the user to a group such " \
				"as 'dialout' or 'uucp'. This can be done by editing /etc/group " \
				"and add the username after the ':'. Example: dialout:x:20:romain " \
				""
				),
			NULL);
		break;

		case ERR_PPDEV:
			*message = g_strconcat(
			_("Msg: unable to use parport (/dev/parportX) device."),
			"\n",
			_("Cause: the /dev/parportX node doesn't exist or you don't have required permissions. "
				"How to grant permission to devices to the user depends on the " \
				"distribution, it may involve adding the user to a group such " \
				"as 'lp'. This can be done by editing /etc/group " \
				"and add the username after the ':'. Example: lp:x:7:romain " \
				""
				),
			NULL);
		break;

		case ERR_USBFS:
			*message = g_strconcat(
			_("Msg: unable to use USB pseudo file system."),
			"\n",
			_("Cause: the USB filesystem is not mounted or " \
				"cannot access USB device because current user doesn't have the necessary " \
				"permissions. How to grant permission to devices to the user depends on the " \
				"distribution; it may involve adding udev and/or pam_console configuration " \
				"files, and you may in addition have to add your user to a group such " \
				"as 'usb' or set-up libusb mounting parameters." \
				"If packages of TiLP and its libraries for your distribution exist, " \
				"it is recommended to use those, as everything which can be configured " \
				"automatically will be set up for you. Otherwise, manual setup steps, if any, are " \
				"explained in the CONFIG file shipped with the ticables2 library. " \
				""
				),
			NULL);
		break;

		case ERR_ILLEGAL_ARG:
			*message = g_strconcat(
			_("Msg: illegal operation or argument."),
			"\n",
			_("Cause: the program which uses this library is buggy. Fire-up the developer!"),
			NULL);
		break;

		case ERR_DHA_NOT_FOUND:
			*message = g_strconcat(
			_("Msg: DhaHelper driver and/or library not found."),
			"\n",
			_("Cause: the driver may have been not started yet; it may have been uninstalled; the library has been installed without administrator privileges."),
			NULL);
		break;

		case ERR_RWP_NOT_FOUND:
			*message = g_strconcat(
			_("Msg: RwPorts driver and/or library not found."),
			"\n",
			_("Cause: the driver may have been not started yet; it may have been uninstalled; the library has been installed without administrator privileges."),
			NULL);
		break;

		case ERR_WRITE_ERROR:
			*message = g_strconcat(
			_("Msg: error occured while writing to the device."),
			NULL);
		break;

		case ERR_WRITE_TIMEOUT:
			*message = g_strconcat(
			_("Msg: timeout occured while writing to the device."),
			"\n",
			_("Cause: check that link cable is plugged and/or the calculator is ready."),
			NULL);
		break;

		case ERR_READ_ERROR:
			*message = g_strconcat(
			_("Msg: error occured while reading from the device."),
			NULL);
		break;

		case ERR_READ_TIMEOUT:
			*message = g_strconcat(
			_("Msg: timeout occured while reading from the device."),
			"\n",
			_("Cause: check that link cable is plugged and/or the calculator is ready."),
			NULL);
		break;

		case ERR_PROBE_FAILED:
			*message = g_strconcat(
			_("Msg: the probing of device failed."),
			"\n",
			_("Cause: internal error. If you get it, this is a bug!"),
			NULL);
		break;

		case ERR_GRY_CREATEFILE:
		case ERR_GRY_SETUPCOMM:
		case ERR_GRY_GETCOMMSTATE:
		case ERR_GRY_SETCOMMSTATE:
		case ERR_GRY_GETCOMMTIMEOUT:
		case ERR_GRY_SETCOMMTIMEOUT:
		case ERR_GRY_PURGECOMM:
		case ERR_GRY_SETCOMMMASK:
			*message = g_strconcat(
			_("Msg: unable to open serial port."),
			"\n",
			_("Cause: check that device is not used/locked by another application."),
			NULL);
		break;

		case ERR_VTI_FINDWINDOW:
		case ERR_VTI_OPENFILEMAPPING:
		case ERR_VTI_MAPVIEWOFFILE:
			*message = g_strconcat(
			_("Msg: failed to get VTI handle."),
			"\n",
			_("Cause: VTI may not be running. VTI must be launched before."),
			NULL);
		break;

		case ERR_VTI_IPCKEY:
		case ERR_VTI_SHMGET:
		case ERR_VTI_SHMAT:
		case ERR_VTI_SHMDT:
		case ERR_VTI_SHMCTL:
			*message = g_strconcat(
			_("Msg: unable to get a unique IPC (Inter Process Communication) key."),
			"\n",
			_("Cause: check that you have enough resources for allocating a shared memory segment."),
			NULL);
		break;

		case ERR_TIE_OPEN:
		case ERR_TIE_CLOSE:
		*message = g_strconcat(
			_("Msg: unable to open/close pipes for virtual linking."),
			"\n",
			_("Cause: check that you have permissions to create a pipe in the /tmp directory."),
			NULL);
		break;

		case ERR_TTY_OPEN:
		case ERR_GRY_OPEN:
			*message = g_strconcat(
			_("Msg: unable to open serial device."),
			"\n",
			_("Cause: check that you have permissions on /dev/ttySx device. Check that device is not locked."),
			NULL);
		break;

		case ERR_TTY_IOCTL:
		case ERR_GRY_IOCTL:
			*message = g_strconcat(
			_("Msg: unable to issue a specific command on serial device."),
			"\n",
			_("Cause: check that you have permissions on /dev/ttySx device. Check that device is not locked."),
			NULL);
		break;

		case ERR_PPT_OPEN:
			*message = g_strconcat(
			_("Msg: unable to open parallel device."),
			"\n",
			_("Cause: check that you have permissions on /dev/parportX device. Check that device is not locked."),
			NULL);
		break;

		case ERR_PPT_IOCTL:
			*message = g_strconcat(
			_("Msg: unable to issue a specific command on parallel device."),
			"\n",
			_("Cause: check that you have permissions on /dev/parportX device. Check that device is not locked."),
			NULL);
		break;

		case ERR_LIBUSBWIN32_NOT_PRESENT:
			*message = g_strconcat(
			_("Msg: unable to find the libusb-win32 driver."),
			"\n",
			_("Cause: the driver is not present and/or installed."),
			NULL);
		break;

		case ERR_LIBUSB_OPEN:
		case ERR_LIBUSB_CLAIM:
		case ERR_LIBUSB_CONFIG:
			*message = g_strconcat(
			_("Msg: failed to open the USB device."),
			"\n",
			_("Cause: Check that the USB cable is plugged in and that the calculator is turned ON! Also, check libusb and usbfs for valid permissions."),
			NULL);
		break;

		case ERR_LIBUSB_RESET:
			*message = g_strconcat(
			_("Msg: unable to reset USB device."),
			"\n",
			_("Cause: Check that cable is connected or not stalled. Try to unplug/plug it."),
			NULL);
		break;

		case ERR_NOT_OPEN:
			*message = g_strconcat(
			_("Msg: attempting to use a cable which has not been open before."),
			"\n",
			_("Cause: Internal error."),
			NULL);
		break;

		case ERR_NO_CABLE:
			*message = g_strconcat(
			_("Msg: no cable found."),
			"\n",
			_("Cause: probing failed or invalid parameters."),
			NULL);
		break;

		case ERR_NO_LIBRARY:
			*message = g_strconcat(
			_("Msg: DLL not found."),
			"\n",
			_("Cause: maybe you renamed the DLL ?"),
			NULL);
		break;

		case ERR_RAW_IO_UNSUPPORTED:
			*message = g_strconcat(
			_("Msg: this cable does not support raw I/O."),
			NULL);
		break;

		default:
		// propagate error code
		return number;

	}

#ifndef __WIN32__
	if (errno != 0) 
	{
		gchar *str;
		tmp = *message;
		
		str = g_strdup_printf(" (errno = %i)", errno);
		*message = g_strconcat(tmp, "\n", "System: ", strerror(errno), str, "\n", NULL);
		g_free(tmp);
		g_free(str);
	}
#else
	if (GetLastError()) 
	{
		LPVOID lpMsgBuf;
		gchar *str;
		tmp = *message;

	FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER |
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL, 
			GetLastError(),
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR)&lpMsgBuf, 
			0, 
			NULL);

		str = g_strdup_printf("(%li) %s", (long)GetLastError, (char *)lpMsgBuf);
		*message = g_strconcat(tmp, "\n", "System: ", str, NULL);
		g_free(tmp);
		g_free(str);
	}
#endif

	return 0;
}