cctools
catch.h
1/*
2 * Copyright (C) 2022 The University of Notre Dame
3 * This software is distributed under the GNU General Public License.
4 * See the file COPYING for details.
5 */
6
7#ifndef CATCH_H
8#define CATCH_H
9
10#include "debug.h"
11
12#include <errno.h>
13#include <string.h>
14
15#define PROTECT(e) \
16 do {\
17 int s = errno;\
18 (e);\
19 errno = s;\
20 } while (0)
21
22#define CLOSE_FD(fd) \
23 do {\
24 if (fd >= 0) {\
25 PROTECT(close(fd));\
26 fd = -1;\
27 }\
28 } while (0)
29
30#define CLOSE_DIR(dir) \
31 do {\
32 if (dir) {\
33 PROTECT(closedir(dir));\
34 dir = NULL;\
35 }\
36 } while (0)
37
38#define THROW_QUIET(e) \
39 do {\
40 rc = (e);\
41 goto out;\
42 } while (0)
43
44#define CATCH(expr) \
45 do {\
46 rc = (expr);\
47 if (rc) {\
48 debug(D_DEBUG, "%s: %s:%d[%s] error: %d `%s'", __func__, __FILE__, __LINE__, CCTOOLS_SOURCE, (int)rc, strerror((int)rc));\
49 goto out;\
50 }\
51 } while (0)
52
53#define RCUNIX(rc) (rc == 0 ? 0 : (errno = (int)rc, -1))
54
55#define UNIXRC(ux) ((ux) == -1 ? errno : 0)
56
57#define CATCHUNIX(expr) \
58 do {\
59 rc = (expr);\
60 if (rc == -1) {\
61 rc = errno;\
62 debug(D_DEBUG, "%s: %s:%d[%s] unix error: -1 (errno = %d) `%s'", __func__, __FILE__, __LINE__, CCTOOLS_SOURCE, (int)rc, strerror((int)rc));\
63 goto out;\
64 }\
65 } while (0)
66
67#define CATCHUNIXIGNORE(expr,err) \
68 do {\
69 rc = (expr);\
70 if (rc == -1) {\
71 rc = errno;\
72 if (errno != err) {\
73 debug(D_DEBUG, "%s: %s:%d[%s] unix error: -1 (errno = %d) `%s'", __func__, __FILE__, __LINE__, CCTOOLS_SOURCE, (int)rc, strerror((int)rc));\
74 goto out;\
75 }\
76 }\
77 } while (0)
78
79#endif
80
81/* vim: set noexpandtab tabstop=8: */
General purpose debugging routines.