Debian Codegraph @

src/error.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef __WIN32__
26 #include <windows.h>
27 #endif
28 #include <glib.h>
29 #include <errno.h>
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "ticables.h"
36 #include "gettext.h"
37 #include "export1.h"
38 #include "error.h"
39 #include "logging.h"
40
41 /**
42 * ticables_error_get:
43 * @number: error number (see error.h for list).
44 * @message: a newly GLib allocated string which contains corresponding error *message.
45 *
46 * Attempt to match the message corresponding to the error number. The returned
47 * string must be freed when no longer needed.
48 * The error message has the following format:
49 * - 1: the error message
50 * - 2: the cause(s), explanations on how to fix it
51 * - 3: the error returned by the system
52 *
53 * Return value: 0 if error has been caught, the error number otherwise (propagation).
54 **/
55 int TICALL ticables_error_get(int number, char **message)
56 {
57 if (message == NULL)
58 {
59 ticables_critical("ticables_error_get(NULL)\n");
60 return number;
61 }
62
63 switch(number)
64 {
65 case ERR_BUSY:
66 *message = g_strconcat(
67 _("Msg: link cable is busy."),
68 "\n",
69 _("Cause: a transfer is in progress."),
70 NULL);
71 break;
72
73 case ERR_ROOT:
74 *message = g_strconcat(
75 _("Msg: unable to use parallel/serial port: access refused."),
76 "\n",
77 _("Cause: you are not running program as root or setuid root."),
78 NULL);
79 break;
80
81 case ERR_TTDEV:
82 *message = g_strconcat(
83 _("Msg: unable to use serial (/dev/ttySx) device."),
84 "\n",
85 _("Cause: the /dev/ttySx node doesn't exist or you don't have required permissions. " \
86 "How to grant permission to devices to the user depends on the " \
87 "distribution, it may involve adding the user to a group such " \
88 "as 'dialout' or 'uucp'. This can be done by editing /etc/group " \
89 "and add the username after the ':'. Example: dialout:x:20:romain " \
90 ""
91 ),
92 NULL);
93 break;
94
95 case ERR_PPDEV:
96 *message = g_strconcat(
97 _("Msg: unable to use parport (/dev/parportX) device."),
98 "\n",
99 _("Cause: the /dev/parportX node doesn't exist or you don't have required permissions. "
100 "How to grant permission to devices to the user depends on the " \
101 "distribution, it may involve adding the user to a group such " \
102 "as 'lp'. This can be done by editing /etc/group " \
103 "and add the username after the ':'. Example: lp:x:7:romain " \
104 ""
105 ),
106 NULL);
107 break;
108
109 case ERR_USBFS:
110 *message = g_strconcat(
111 _("Msg: unable to use USB pseudo file system."),
112 "\n",
113 _("Cause: the USB filesystem is not mounted or " \
114 "cannot access USB device because current user doesn't have the necessary " \
115 "permissions. How to grant permission to devices to the user depends on the " \
116 "distribution; it may involve adding udev and/or pam_console configuration " \
117 "files, and you may in addition have to add your user to a group such " \
118 "as 'usb' or set-up libusb mounting parameters." \
119 "If packages of TiLP and its libraries for your distribution exist, " \
120 "it is recommended to use those, as everything which can be configured " \
121 "automatically will be set up for you. Otherwise, manual setup steps, if any, are " \
122 "explained in the CONFIG file shipped with the ticables2 library. " \
123 ""
124 ),
125 NULL);
126 break;
127
128 case ERR_ILLEGAL_ARG:
129 *message = g_strconcat(
130 _("Msg: illegal operation or argument."),
131 "\n",
132 _("Cause: the program which uses this library is buggy. Fire-up the developer!"),
133 NULL);
134 break;
135
136 case ERR_DHA_NOT_FOUND:
137 *message = g_strconcat(
138 _("Msg: DhaHelper driver and/or library not found."),
139 "\n",
140 _("Cause: the driver may have been not started yet; it may have been uninstalled; the library has been installed without administrator privileges."),
141 NULL);
142 break;
143
144 case ERR_RWP_NOT_FOUND:
145 *message = g_strconcat(
146 _("Msg: RwPorts driver and/or library not found."),
147 "\n",
148 _("Cause: the driver may have been not started yet; it may have been uninstalled; the library has been installed without administrator privileges."),
149 NULL);
150 break;
151
152 case ERR_WRITE_ERROR:
153 *message = g_strdup(
154 _("Msg: error occurred while writing to the device."));
155 break;
156
157 case ERR_WRITE_TIMEOUT:
158 *message = g_strconcat(
159 _("Msg: timeout occurred while writing to the device."),
160 "\n",
161 _("Cause: check that link cable is plugged and/or the calculator is ready."),
162 NULL);
163 break;
164
165 case ERR_READ_ERROR:
166 *message = g_strdup(
167 _("Msg: error occurred while reading from the device."));
168 break;
169
170 case ERR_READ_TIMEOUT:
171 *message = g_strconcat(
172 _("Msg: timeout occurred while reading from the device."),
173 "\n",
174 _("Cause: check that link cable is plugged and/or the calculator is ready."),
175 NULL);
176 break;
177
178 case ERR_PROBE_FAILED:
179 *message = g_strconcat(
180 _("Msg: the probing of device failed."),
181 "\n",
182 _("Cause: internal error. If you get it, this is a bug!"),
183 NULL);
184 break;
185
186 case ERR_GRY_CREATEFILE:
187 case ERR_GRY_SETUPCOMM:
188 case ERR_GRY_GETCOMMSTATE:
189 case ERR_GRY_SETCOMMSTATE:
190 case ERR_GRY_GETCOMMTIMEOUT:
191 case ERR_GRY_SETCOMMTIMEOUT:
192 case ERR_GRY_PURGECOMM:
193 case ERR_GRY_SETCOMMMASK:
194 *message = g_strconcat(
195 _("Msg: unable to open serial port."),
196 "\n",
197 _("Cause: check that device is not used/locked by another application."),
198 NULL);
199 break;
200
201 case ERR_VTI_FINDWINDOW:
202 case ERR_VTI_OPENFILEMAPPING:
203 case ERR_VTI_MAPVIEWOFFILE:
204 *message = g_strconcat(
205 _("Msg: failed to get VTI handle."),
206 "\n",
207 _("Cause: VTI may not be running. VTI must be launched before."),
208 NULL);
209 break;
210
211 case ERR_VTI_IPCKEY:
212 case ERR_VTI_SHMGET:
213 case ERR_VTI_SHMAT:
214 case ERR_VTI_SHMDT:
215 case ERR_VTI_SHMCTL:
216 *message = g_strconcat(
217 _("Msg: unable to get a unique IPC (Inter Process Communication) key."),
218 "\n",
219 _("Cause: check that you have enough resources for allocating a shared memory segment."),
220 NULL);
221 break;
222
223 case ERR_TIE_OPEN:
224 case ERR_TIE_CLOSE:
225 *message = g_strconcat(
226 _("Msg: unable to open/close pipes for virtual linking."),
227 "\n",
228 _("Cause: check that you have permissions to create a pipe in the /tmp directory."),
229 NULL);
230 break;
231
232 case ERR_TTY_OPEN:
233 case ERR_GRY_OPEN:
234 *message = g_strconcat(
235 _("Msg: unable to open serial device."),
236 "\n",
237 _("Cause: check that you have permissions on /dev/ttySx device. Check that device is not locked."),
238 NULL);
239 break;
240
241 case ERR_TTY_IOCTL:
242 case ERR_GRY_IOCTL:
243 *message = g_strconcat(
244 _("Msg: unable to issue a specific command on serial device."),
245 "\n",
246 _("Cause: check that you have permissions on /dev/ttySx device. Check that device is not locked."),
247 NULL);
248 break;
249
250 case ERR_PPT_OPEN:
251 *message = g_strconcat(
252 _("Msg: unable to open parallel device."),
253 "\n",
254 _("Cause: check that you have permissions on /dev/parportX device. Check that device is not locked."),
255 NULL);
256 break;
257
258 case ERR_PPT_IOCTL:
259 *message = g_strconcat(
260 _("Msg: unable to issue a specific command on parallel device."),
261 "\n",
262 _("Cause: check that you have permissions on /dev/parportX device. Check that device is not locked."),
263 NULL);
264 break;
265
266 case ERR_LIBUSBWIN32_NOT_PRESENT:
267 *message = g_strconcat(
268 _("Msg: unable to find the libusb-win32 driver."),
269 "\n",
270 _("Cause: the driver is not present and/or installed."),
271 NULL);
272 break;
273
274 case ERR_LIBUSB_OPEN:
275 case ERR_LIBUSB_CLAIM:
276 case ERR_LIBUSB_CONFIG:
277 *message = g_strconcat(
278 _("Msg: failed to open the USB device."),
279 "\n",
280 _("Cause: Check that the USB cable is plugged in and that the calculator is turned ON! Also, check libusb and usbfs for valid permissions."),
281 NULL);
282 break;
283
284 case ERR_LIBUSB_RESET:
285 *message = g_strconcat(
286 _("Msg: unable to reset USB device."),
287 "\n",
288 _("Cause: Check that cable is connected or not stalled. Try to unplug/plug it."),
289 NULL);
290 break;
291
292 case ERR_NOT_OPEN:
293 *message = g_strconcat(
294 _("Msg: attempting to use a cable which has not been open before."),
295 "\n",
296 _("Cause: Internal error."),
297 NULL);
298 break;
299
300 case ERR_NO_CABLE:
301 *message = g_strconcat(
302 _("Msg: no cable found."),
303 "\n",
304 _("Cause: probing failed or invalid parameters."),
305 NULL);
306 break;
307
308 case ERR_NO_LIBRARY:
309 *message = g_strconcat(
310 _("Msg: DLL not found."),
311 "\n",
312 _("Cause: maybe you renamed the DLL ?"),
313 NULL);
314 break;
315
316 case ERR_RAW_IO_UNSUPPORTED:
317 *message = g_strconcat(
318 _("Msg: this cable does not support raw I/O."),
319 "\n",
320 _("Cause: unsupported operation type on cable."),
321 NULL);
322 break;
323
324 case ERR_TCPC_OPEN:
325 case ERR_TCPC_CLOSE:
326 case ERR_TCPS_OPEN:
327 case ERR_TCPS_CLOSE:
328 *message = g_strconcat(
329 _("Msg: unable to open/close sockets for virtual linking."),
330 "\n",
331 _("Cause: check that you have permissions to open sockets."),
332 NULL);
333 break;
334
335 default:
336 // propagate error code
337 return number;
338
339 }
340
341 #ifndef __WIN32__
342 if (errno != 0)
343 {
344 gchar *str;
345 char * tmp = *message;
346
347 str = g_strdup_printf(" (errno = %i)", errno);
348 *message = g_strconcat(tmp, "\n", "System: ", strerror(errno), str, "\n", NULL);
349 g_free(tmp);
350 g_free(str);
351 }
352 #else
353 if (GetLastError())
354 {
355 LPVOID lpMsgBuf;
356 gchar *str;
357 char * tmp = *message;
358
359 FormatMessage(
360 FORMAT_MESSAGE_ALLOCATE_BUFFER |
361 FORMAT_MESSAGE_FROM_SYSTEM |
362 FORMAT_MESSAGE_IGNORE_INSERTS,
363 NULL,
364 GetLastError(),
365 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
366 (LPTSTR)&lpMsgBuf,
367 0,
368 NULL);
369
370 str = g_strdup_printf("(%li) %s", (long)GetLastError, (char *)lpMsgBuf);
371 *message = g_strconcat(tmp, "\n", "System: ", str, NULL);
372 g_free(tmp);
373 g_free(str);
374 }
375 #endif
376
377 return 0;
378 }
379
380 /**
381 * ticables_error_free:
382 * @message: a message previously allocated by ticables_error_get()
383 *
384 * Free the given message string allocated by ticables_error_get();
385 *
386 * Return value: 0 if the argument was valid and the message was freed, nonzero otherwise.
387 **/
388 int TICALL ticables_error_free(char *message)
389 {
390 if (message == NULL)
391 {
392 ticables_critical("ticables_error_free(NULL)\n");
393 return ERR_ILLEGAL_ARG;
394 }
395 g_free(message);
396 return 0;
397 }