catch.h

00001 /*
00002  * Copyright (C) 2014- The University of Notre Dame
00003  * This software is distributed under the GNU General Public License.
00004  * See the file COPYING for details.
00005  */
00006 
00007 #ifndef CATCH_H
00008 #define CATCH_H
00009 
00010 #include "debug.h"
00011 
00012 #include <errno.h>
00013 #include <string.h>
00014 
00015 #define PROTECT(e) \
00016         do {\
00017                 int s = errno;\
00018                 (e);\
00019                 errno = s;\
00020         } while (0)
00021 
00022 #define CLOSE_FD(fd) \
00023         do {\
00024                 if (fd >= 0) {\
00025                         PROTECT(close(fd));\
00026                         fd = -1;\
00027                 }\
00028         } while (0)
00029 
00030 #define CLOSE_DIR(dir) \
00031         do {\
00032                 if (dir) {\
00033                         PROTECT(closedir(dir));\
00034                         dir = NULL;\
00035                 }\
00036         } while (0)
00037 
00038 #define THROW_QUIET(e) \
00039         do {\
00040                 rc = (e);\
00041                 goto out;\
00042         } while (0)
00043 
00044 #define CATCH(expr) \
00045         do {\
00046                 rc = (expr);\
00047                 if (rc) {\
00048                         debug(D_DEBUG, "%s: %s:%d[%s] error: %d `%s'", __func__, __FILE__, __LINE__, CCTOOLS_SOURCE, (int)rc, strerror((int)rc));\
00049                         goto out;\
00050                 }\
00051         } while (0)
00052 
00053 #define RCUNIX(rc) (rc == 0 ? 0 : (errno = (int)rc, -1))
00054 
00055 #define UNIXRC(ux) ((ux) == -1 ? errno : 0)
00056 
00057 #define CATCHUNIX(expr) \
00058         do {\
00059                 rc = (expr);\
00060                 if (rc == -1) {\
00061                         rc = errno;\
00062                         debug(D_DEBUG, "%s: %s:%d[%s] unix error: -1 (errno = %d) `%s'", __func__, __FILE__, __LINE__, CCTOOLS_SOURCE, (int)rc, strerror((int)rc));\
00063                         goto out;\
00064                 }\
00065         } while (0)
00066 
00067 #define CATCHUNIXIGNORE(expr,err) \
00068         do {\
00069                 rc = (expr);\
00070                 if (rc == -1) {\
00071                         rc = errno;\
00072                         if (errno != err) {\
00073                                 debug(D_DEBUG, "%s: %s:%d[%s] unix error: -1 (errno = %d) `%s'", __func__, __FILE__, __LINE__, CCTOOLS_SOURCE, (int)rc, strerror((int)rc));\
00074                                 goto out;\
00075                         }\
00076                 }\
00077         } while (0)
00078 
00079 #endif
00080 
00081 /* vim: set noexpandtab tabstop=4: */

Generated on 3 Nov 2016 for cctools by  doxygen 1.6.1