Debian Codegraph @

src/backend/utils/generate-errcodes.pl

raw
1 #!/usr/bin/perl
2 #
3 # Generate the errcodes.h header from errcodes.txt
4 # Copyright (c) 2000-2025, PostgreSQL Global Development Group
5
6 use strict;
7 use warnings FATAL => 'all';
8 use Getopt::Long;
9
10 my $outfile = '';
11
12 GetOptions('outfile=s' => \$outfile) or die "$0: wrong arguments";
13
14 open my $errcodes, '<', $ARGV[0]
15 or die "$0: could not open input file '$ARGV[0]': $!\n";
16
17 my $outfh;
18 if ($outfile)
19 {
20 open $outfh, '>', $outfile
21 or die "$0: could not open output file '$outfile': $!\n";
22 }
23 else
24 {
25 $outfh = *STDOUT;
26 }
27
28 print $outfh
29 "/* autogenerated from src/backend/utils/errcodes.txt, do not edit */\n";
30 print $outfh "/* there is deliberately not an #ifndef ERRCODES_H here */\n";
31
32
33 while (<$errcodes>)
34 {
35 chomp;
36
37 # Skip comments
38 next if /^#/;
39 next if /^\s*$/;
40
41 # Emit a comment for each section header
42 if (/^Section:(.*)/)
43 {
44 my $header = $1;
45 $header =~ s/^\s+//;
46 print $outfh "\n/* $header */\n";
47 next;
48 }
49
50 die "unable to parse errcodes.txt"
51 unless /^([^\s]{5})\s+[EWS]\s+([^\s]+)/;
52
53 (my $sqlstate, my $errcode_macro) = ($1, $2);
54
55 # Split the sqlstate letters
56 $sqlstate = join ",", split "", $sqlstate;
57
58 # And quote them
59 $sqlstate =~ s/([^,])/'$1'/g;
60
61 print $outfh "#define $errcode_macro MAKE_SQLSTATE($sqlstate)\n";
62 }
63
64 close $errcodes;
65 close $outfh if ($outfile);