File: error.c

package info (click to toggle)
universal-ctags 6.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,612 kB
  • sloc: ansic: 158,498; sh: 8,621; lisp: 7,742; vhdl: 6,518; cpp: 2,583; perl: 2,578; python: 2,324; javascript: 2,054; cs: 1,193; lex: 1,015; sql: 897; makefile: 787; ruby: 764; php: 755; cobol: 741; f90: 566; ada: 559; asm: 509; yacc: 465; fortran: 412; xml: 405; objc: 289; tcl: 280; java: 173; erlang: 65; pascal: 58; ml: 49; awk: 44; haskell: 42
file content (102 lines) | stat: -rw-r--r-- 2,751 bytes parent folder | download | duplicates (8)
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
97
98
99
100
101
102
/*
*   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_p.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: " :
		 selected (selection, NOTICE) ? "Notice: " : "");
	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;

	if (Option.quiet && selected (selection, NOTICE))
		return;

	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, NOTICE))
		json_object_set_new (response, "notice", json_true ());
	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