summaryrefslogtreecommitdiffstats
path: root/src/backend/utils/generate-errcodes.pl
blob: 34d0f25c2335d95d4962cc15b065ce3775135764 (plain)
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
#!/usr/bin/perl
#
# Generate the errcodes.h header from errcodes.txt
# Copyright (c) 2000-2023, PostgreSQL Global Development Group

use strict;
use warnings;
use Getopt::Long;

my $outfile = '';

GetOptions('outfile=s' => \$outfile) or die "$0: wrong arguments";

open my $errcodes, '<', $ARGV[0]
  or die "$0: could not open input file '$ARGV[0]': $!\n";

my $outfh;
if ($outfile)
{
	open $outfh, '>', $outfile
	  or die "$0: could not open output file '$outfile': $!\n";
}
else
{
	$outfh = *STDOUT;
}

print $outfh
  "/* autogenerated from src/backend/utils/errcodes.txt, do not edit */\n";
print $outfh "/* there is deliberately not an #ifndef ERRCODES_H here */\n";


while (<$errcodes>)
{
	chomp;

	# Skip comments
	next if /^#/;
	next if /^\s*$/;

	# Emit a comment for each section header
	if (/^Section:(.*)/)
	{
		my $header = $1;
		$header =~ s/^\s+//;
		print $outfh "\n/* $header */\n";
		next;
	}

	die "unable to parse errcodes.txt"
	  unless /^([^\s]{5})\s+[EWS]\s+([^\s]+)/;

	(my $sqlstate, my $errcode_macro) = ($1, $2);

	# Split the sqlstate letters
	$sqlstate = join ",", split "", $sqlstate;

	# And quote them
	$sqlstate =~ s/([^,])/'$1'/g;

	print $outfh "#define $errcode_macro MAKE_SQLSTATE($sqlstate)\n";
}

close $errcodes;
close $outfh if ($outfile);