1#!/usr/bin/perl
2#
3# Generate the spiexceptions.h header from errcodes.txt
4# Copyright (c) 2000-2018, PostgreSQL Global Development Group
5
6use warnings;
7use strict;
8
9print
10  "/* autogenerated from src/backend/utils/errcodes.txt, do not edit */\n";
11print "/* there is deliberately not an #ifndef SPIEXCEPTIONS_H here */\n";
12
13open my $errcodes, '<', $ARGV[0] or die;
14
15while (<$errcodes>)
16{
17	chomp;
18
19	# Skip comments
20	next if /^#/;
21	next if /^\s*$/;
22
23	# Skip section headers
24	next if /^Section:/;
25
26	die unless /^([^\s]{5})\s+([EWS])\s+([^\s]+)(?:\s+)?([^\s]+)?/;
27
28	(my $sqlstate, my $type, my $errcode_macro, my $condition_name) =
29	  ($1, $2, $3, $4);
30
31	# Skip non-errors
32	next unless $type eq 'E';
33
34	# Skip lines without PL/pgSQL condition names
35	next unless defined($condition_name);
36
37	# Change some_error_condition to SomeErrorCondition
38	$condition_name =~ s/([a-z])([^_]*)(?:_|$)/\u$1$2/g;
39
40	print "{ \"spiexceptions.$condition_name\", "
41	  . "\"$condition_name\", $errcode_macro },\n";
42}
43
44close $errcodes;
45