User handling improvments.
[adu.git] / error.h
diff --git a/error.h b/error.h
index 8603291cebc6768dc198f7d2a7a5d718c620bf94..87d3fe2820ff16f80fcd0e6718344d7bd107bf4e 100644 (file)
--- a/error.h
+++ b/error.h
@@ -1,10 +1,5 @@
-extern char *__errlist[];
-extern char *__error_txt;
-
-//__printf_2_3 void __log(int ll, const char* fmt,...);
-
 /**
- * This bit indicates whether a number is considered a system error number
+ * This bit indicates whether a number is considered a system error code.
  * If yes, the system errno is just the result of clearing this bit from
  * the given number.
  */
@@ -16,82 +11,28 @@ extern char *__error_txt;
 /** Set the system error bit for the given number. */
 #define ERRNO_TO_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
 
-/** Check whether a given number is a system error number.
- *
- * \param num The value to be checked.
- * \param _errno The system error number.
- *
- * \return True if \a num is the representation of the system
- * error identified by \a _errno.
- */
-static inline int is_errno(int num, int _errno)
-{
-       assert(num > 0 && _errno > 0);
-       return ERRNO_TO_ERROR(_errno) == num;
-}
-
-/**
- * version of strerror(3).
- *
- * \param num The error number.
- *
- * \return The error text of \a num.
- */
-static inline char *error_txt(int num)
-{
-       assert(num > 0);
-       if (IS_SYSTEM_ERROR(num))
-               return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
-       else
-               return __errlist[num];
-}
-
 #define ALL_ERRORS \
        _ERROR(SUCCESS, "success") \
-       _ERROR(BAD_DB_DIR, "invalid database directory") \
-       _ERROR(NO_COLUMN_DESC, "missing column description") \
-       _ERROR(BAD_NAME, "invalid name for a column/table") \
-       _ERROR(BAD_STORAGE_TYPE, "invalid storage type") \
-       _ERROR(BAD_STORAGE_FLAGS, "invalid storage flags") \
-       _ERROR(NO_COLUMN_NAME, "missing column name") \
-       _ERROR(NO_COLUMNS, "at least one column required") \
-       _ERROR(BAD_COLUMN_NAME, "invalid name for a table column") \
-       _ERROR(NO_UNIQUE_RBTREE_COLUMN, "need at least one mapped column with OSL_UNIQE and OSL_RBTREE OSL") \
-       _ERROR(NO_RBTREE_COL, "at least one column needs an rbtree") \
-       _ERROR(DUPLICATE_COL_NAME, "column name given twice") \
-       _ERROR(BAD_STORAGE_SIZE, "invalid storage size") \
-       _ERROR(NO_COMPARE_FUNC, "missing compare function") \
-       _ERROR(BAD_DATA_SIZE, "wrong data size for fixed-size column") \
-       _ERROR(NOT_MAPPED, "file not mapped") \
-       _ERROR(ALREADY_MAPPED, "file already mapped") \
-       _ERROR(BAD_SIZE, "invalid size specified") \
-       _ERROR(TRUNC, "failed to truncate file") \
-       _ERROR(BAD_TABLE, "table not open") \
-       _ERROR(BAD_TABLE_DESC, "invalid table description") \
-       _ERROR(RB_KEY_EXISTS, "key already exists in rbtree") \
-       _ERROR(RB_KEY_NOT_FOUND, "key not found in rbtree") \
-       _ERROR(BAD_ROW_NUM, "invalid row number") \
-       _ERROR(INDEX_CORRUPTION, "index corruption detected") \
-       _ERROR(INVALID_OBJECT, "reference to invalid object") \
-       _ERROR(STAT, "can not stat file") \
-       _ERROR(WRITE, "write error") \
-       _ERROR(LSEEK, "lseek error") \
-       _ERROR(BUSY, "table is busy") \
-       _ERROR(SHORT_TABLE, "table too short") \
-       _ERROR(NO_MAGIC, "missing table header magic") \
-       _ERROR(VERSION_MISMATCH, "table version not supported") \
-       _ERROR(BAD_COLUMN_NUM, "invalid column number") \
-       _ERROR(BAD_TABLE_FLAGS, "invalid flags in table description") \
-       _ERROR(BAD_ROW, "invalid row") \
+       _ERROR(SYNTAX, "syntax error") \
+       _ERROR(LOOP_COMPLETE, "loop complete") \
+       _ERROR(HASH_TABLE_OVERFLOW, "hash table too small") \
+       _ERROR(BAD_UID, "uid not found in hash table") \
        _ERROR(ATOI_OVERFLOW, "value too large") \
        _ERROR(STRTOLL, "unknown strtoll error") \
        _ERROR(ATOI_NO_DIGITS, "no digits found in string") \
        _ERROR(ATOI_JUNK_AT_END, "further characters after number") \
-       _ERROR(FGETS, "fgets error") \
        _ERROR(EMPTY, "file empty") \
        _ERROR(MMAP, "mmap error") \
-       _ERROR(SYNTAX, "syntax error") \
-       _ERROR(LOOP_COMPLETE, "loop complete")
+       _ERROR(OSL, "osl error") \
+       _ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR") \
+       _ERROR(OUTPUT, "error writing output") \
+       _ERROR(MALFORMED_FORMAT, "malformed format string") \
+       _ERROR(BAD_ALIGN_SPEC, "bad alignment specifier") \
+       _ERROR(TRAILING_GARBAGE, "trailing garbage after specifier") \
+       _ERROR(UNIT, "no unit allowed here") \
+       _ERROR(BAD_UNIT, "invalid unit specifier") \
+       _ERROR(BAD_ATOM, "invalid atom") \
+       _ERROR(BAD_OUTPUT_ARG, "invalid name for output") \
 
 
 /**
@@ -105,4 +46,44 @@ enum error_codes {
 };
 #undef _ERROR
 #define _ERROR(err, msg) msg,
-#define DEFINE_ERRLIST char *__errlist[] = {ALL_ERRORS}
+#define DEFINE_ERRLIST char *adu_errlist[] = {ALL_ERRORS}
+
+extern int osl_errno;
+extern char *adu_errlist[];
+
+
+/**
+ * adu's version of strerror(3).
+ *
+ * \param num The error number.
+ *
+ * \return The error text of \a num.
+ */
+static inline const char *adu_strerror(int num)
+{
+       assert(num > 0);
+       if (num == E_OSL) {
+               assert(osl_errno > 0);
+               return osl_strerror((osl_errno));
+       }
+       if (IS_SYSTEM_ERROR(num))
+               return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
+       return adu_errlist[num];
+}
+
+/**
+ * Wrapper for osl library calls.
+ *
+ * This should be used for all calls to osl functions that return an osl error
+ * code. It changes the return value to \p -E_OSL appropriately so that it can
+ * be used for printing the correct error message.
+ *
+ * \return \a ret if \a ret >= 0, \p -E_OSL otherwise.
+ */
+static inline int osl(int ret)
+{
+       if (ret >= 0)
+               return ret;
+       osl_errno = -ret;
+       return -E_OSL;
+}