Added silc_errno_set_location and silc_errno_location
[runtime.git] / lib / silcutil / silcerrno.c
index fae62b500f2a2786137443cba72a70f8a9a0af88..ae7af839add398c7b3e0f03e36e10af30110aa0c 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2007 Pekka Riikonen
+  Copyright (C) 2007 - 2008 Pekka Riikonen
 
   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
@@ -17,7 +17,7 @@
 
 */
 
-#include "silc.h"
+#include "silcruntime.h"
 
 /* Get last error */
 
@@ -44,9 +44,8 @@ void silc_set_errno(SilcResult error)
       return;
   }
 
-  SILC_LOG_DEBUG(("Error: %s (%d)", silc_errno_string(error), error));
-
   tls->error_reason[0] = '\0';
+  tls->filename[0] = '\0';
   tls->error = error;
 }
 
@@ -59,9 +58,8 @@ void silc_set_errno_nofail(SilcResult error)
   if (!tls)
     return;
 
-  SILC_LOG_DEBUG(("Error: %s (%d)", silc_errno_string(error), error));
-
   tls->error_reason[0] = '\0';
+  tls->filename[0] = '\0';
   tls->error = error;
 }
 
@@ -79,12 +77,11 @@ void silc_set_errno_reason(SilcResult error, const char *format, ...)
       return;
   }
 
-  va_start(va, format);
-  silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
-  va_end(va);
-
-  SILC_LOG_DEBUG(("Error: %s (%d): %s", silc_errno_string(error), error,
-                 tls->error_reason));
+  if (format) {
+    va_start(va, format);
+    silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
+    va_end(va);
+  }
 
   tls->error = error;
 }
@@ -99,12 +96,11 @@ void silc_set_errno_reason_nofail(SilcResult error, const char *format, ...)
   if (!tls)
     return;
 
-  va_start(va, format);
-  silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
-  va_end(va);
-
-  SILC_LOG_DEBUG(("Error: %s (%d): %s", silc_errno_string(error), error,
-                 tls->error_reason));
+  if (format) {
+    va_start(va, format);
+    silc_vsnprintf(tls->error_reason, sizeof(tls->error_reason), format, va);
+    va_end(va);
+  }
 
   tls->error = error;
 }
@@ -350,7 +346,7 @@ const char *silc_errno_strings[] =
   "Interrupted",
   "Not valid",
   "Limit reached",
-  "",
+  "Syntax error",
   "",
   "",
   "",
@@ -432,6 +428,8 @@ const char *silc_errno_strings[] =
   "Bad match register number",
   "Badly placed special character",
   "Regular expression too complex",
+  "Bad regular expression opcode",
+  "Bad repeat value",
 };
 
 /* Map error to string */
@@ -443,3 +441,43 @@ const char *silc_errno_string(SilcResult error)
 
   return silc_errno_strings[error];
 }
+
+/* Set error location */
+
+void silc_set_errno_location(const char *filename,
+                            SilcUInt32 current_line_number,
+                            SilcUInt32 current_column_number)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls)
+    return;
+
+  if (filename)
+    silc_snprintf(tls->filename, sizeof(tls->filename), filename);
+  tls->cur_line = current_line_number;
+  tls->cur_column = current_column_number;
+}
+
+/* Get error location */
+
+SilcBool silc_errno_location(const char **filename,
+                            SilcUInt32 *current_line,
+                            SilcUInt32 *current_column)
+{
+  SilcTls tls = silc_thread_get_tls();
+
+  if (!tls)
+    return FALSE;
+  if (tls->filename[0] == '\0' && !tls->cur_line && !tls->cur_column)
+    return FALSE;
+
+  if (filename)
+    *filename = tls->filename;
+  if (current_line)
+    *current_line = tls->cur_line;
+  if (current_column)
+    *current_column = tls->cur_column;
+
+  return TRUE;
+}