1 /*
2 * Copyright (c) 2002-2003, Darren Hiebert
3 *
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License version 2 or (at your option) any later version.
6 *
7 * This module contains a lose assortment of shared functions.
8 */
9
10 #include "general.h" /* must always come first */
11 #include <string.h>
12 #include <errno.h>
13
14 #include "error_p.h"
15 #include "options_p.h"
16 #include "routines_p.h"
17
18 #ifdef HAVE_JANSSON
19 #include <jansson.h>
20 #endif
21
22 #define selected(var,feature) (((int)(var) & (int)(feature)) == (int)feature)
23
24 static errorPrintFunc errorPrinter;
25 static void *errorPrinterData;
26
setErrorPrinter(errorPrintFunc printer,void * data)27 extern void setErrorPrinter (errorPrintFunc printer, void *data)
28 {
29 errorPrinter = printer;
30 errorPrinterData = data;
31 }
32
stderrDefaultErrorPrinter(const errorSelection selection,const char * const format,va_list ap,void * data CTAGS_ATTR_UNUSED)33 extern bool stderrDefaultErrorPrinter (const errorSelection selection,
34 const char *const format,
35 va_list ap, void *data CTAGS_ATTR_UNUSED)
36 {
37 fprintf (stderr, "%s: %s", getExecutableName (),
38 selected (selection, WARNING) ? "Warning: " :
39 selected (selection, NOTICE) ? "Notice: " : "");
40 vfprintf (stderr, format, ap);
41 if (selected (selection, PERROR))
42 {
43 #ifdef HAVE_STRERROR
44 fprintf (stderr, " : %s", strerror (errno));
45 #else
46 perror (" ");
47 #endif
48 }
49 fputs ("\n", stderr);
50
51 return (selected (selection, FATAL) || Option.fatalWarnings)? true: false;
52 }
53
error(const errorSelection selection,const char * const format,...)54 extern void error (const errorSelection selection,
55 const char *const format, ...)
56 {
57 va_list ap;
58 bool shouldExit;
59
60 if (Option.quiet && selected (selection, NOTICE))
61 return;
62
63 va_start (ap, format);
64 shouldExit = (* errorPrinter) (selection, format, ap, errorPrinterData);
65 va_end (ap);
66
67 if (shouldExit)
68 exit (1);
69 }
70
71 #ifdef HAVE_JANSSON
jsonErrorPrinter(const errorSelection selection,const char * const format,va_list ap,void * data CTAGS_ATTR_UNUSED)72 bool jsonErrorPrinter (const errorSelection selection, const char *const format, va_list ap,
73 void *data CTAGS_ATTR_UNUSED)
74 {
75 #define ERR_BUFFER_SIZE 4096
76 static char reason[ERR_BUFFER_SIZE];
77
78 vsnprintf (reason, ERR_BUFFER_SIZE, format, ap);
79 reason [ERR_BUFFER_SIZE - 1] = '\0'; /* Do we need this? */
80
81 json_t *response = json_object ();
82 json_object_set_new (response, "_type", json_string ("error"));
83 json_object_set_new (response, "message", json_string (reason));
84 if (selected (selection, NOTICE))
85 json_object_set_new (response, "notice", json_true ());
86 if (selected (selection, WARNING))
87 json_object_set_new (response, "warning", json_true ());
88 if (selected (selection, FATAL))
89 json_object_set_new (response, "fatal", json_true ());
90 if (selected (selection, PERROR))
91 {
92 json_object_set_new (response, "errno", json_integer (errno));
93 json_object_set_new (response, "perror", json_string (strerror (errno)));
94 }
95 json_dumpf (response, stdout, JSON_PRESERVE_ORDER);
96 fprintf (stdout, "\n");
97
98 json_decref (response);
99
100 return false;
101 }
102 #endif
103