File: error.c

package info (click to toggle)
universal-ctags 0%2Bgit20181215-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,444 kB
  • sloc: ansic: 84,242; vhdl: 5,924; sh: 5,830; perl: 1,743; cpp: 1,599; cs: 1,193; python: 812; sql: 572; f90: 534; php: 479; yacc: 459; fortran: 341; makefile: 325; asm: 311; objc: 284; ruby: 261; xml: 245; java: 157; tcl: 133; cobol: 122; lisp: 113; erlang: 61; ada: 55; ml: 49; awk: 43
file content (96 lines) | stat: -rw-r--r-- 2,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
*   Copyright (c) 2002-2003, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License version 2 or (at your option) any later version.
*
*   This module contains a lose assortment of shared functions.
*/

#include "general.h"  /* must always come first */
#include <string.h>
#include <errno.h>

#include "error.h"
#include "options_p.h"
#include "routines_p.h"

#ifdef HAVE_JANSSON
#include <jansson.h>
#endif

#define selected(var,feature)	(((int)(var) & (int)(feature)) == (int)feature)

static errorPrintFunc errorPrinter;
static void *errorPrinterData;

extern void setErrorPrinter (errorPrintFunc printer, void *data)
{
	errorPrinter = printer;
	errorPrinterData = data;
}

extern bool stderrDefaultErrorPrinter (const errorSelection selection,
					  const char *const format,
					  va_list ap, void *data CTAGS_ATTR_UNUSED)
{
	fprintf (stderr, "%s: %s", getExecutableName (),
		 selected (selection, WARNING) ? "Warning: " : "");
	vfprintf (stderr, format, ap);
	if (selected (selection, PERROR))
	{
#ifdef HAVE_STRERROR
		fprintf (stderr, " : %s", strerror (errno));
#else
		perror (" ");
#endif
	}
	fputs ("\n", stderr);

	return (selected (selection, FATAL) || Option.fatalWarnings)? true: false;
}

extern void error (const errorSelection selection,
		   const char *const format, ...)
{
	va_list ap;
	bool shouldExit;

	va_start (ap, format);
	shouldExit = (* errorPrinter) (selection, format, ap, errorPrinterData);
	va_end (ap);

	if (shouldExit)
		exit (1);
}

#ifdef HAVE_JANSSON
bool jsonErrorPrinter (const errorSelection selection, const char *const format, va_list ap,
					   void *data CTAGS_ATTR_UNUSED)
{
#define ERR_BUFFER_SIZE 4096
	static char reason[ERR_BUFFER_SIZE];

	vsnprintf (reason, ERR_BUFFER_SIZE, format, ap);
	reason [ERR_BUFFER_SIZE - 1] = '\0'; /* Do we need this? */

	json_t *response = json_object ();
	json_object_set_new (response, "_type", json_string ("error"));
	json_object_set_new (response, "message", json_string (reason));
	if (selected (selection, WARNING))
		json_object_set_new (response, "warning", json_true ());
	if (selected (selection, FATAL))
		json_object_set_new (response, "fatal", json_true ());
	if (selected (selection, PERROR))
	{
		json_object_set_new (response, "errno", json_integer (errno));
		json_object_set_new (response, "perror", json_string (strerror (errno)));
	}
	json_dumpf (response, stdout, JSON_PRESERVE_ORDER);
	fprintf (stdout, "\n");

	json_decref (response);

	return false;
}
#endif