xref: /curl/.github/scripts/verify-examples.pl (revision eefcc1bd)
1#!/usr/bin/env perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at https://curl.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22# SPDX-License-Identifier: curl
23#
24###########################################################################
25
26my @files = @ARGV;
27my $cfile = "test.c";
28my $check = "./scripts/checksrc.pl";
29my $error;
30
31if($files[0] eq "-h") {
32    print "Usage: verify-synopsis [man pages]\n";
33    exit;
34}
35
36sub testcompile {
37    my $rc = system("gcc -c test.c -DCURL_DISABLE_TYPECHECK -DCURL_ALLOW_OLD_MULTI_SOCKET -DCURL_DISABLE_DEPRECATION -Wunused -Werror -Wno-unused-but-set-variable -I include") >> 8;
38    return $rc;
39}
40
41sub checksrc {
42    my $rc = system("$check test.c") >> 8;
43    return $rc;
44}
45
46sub extract {
47    my($f) = @_;
48    my $syn = 0;
49    my $l = 0;
50    my $iline = 0;
51    my $fail = 0;
52    open(F, "<$f") or die "failed opening input file $f : $!";
53    open(O, ">$cfile") or die "failed opening output file $cfile : $!";
54    print O "#include <curl/curl.h>\n";
55    while(<F>) {
56        $iline++;
57        if(/^.SH EXAMPLE/) {
58            $syn = 1
59        }
60        elsif($syn == 1) {
61            if(/^.nf/) {
62                $syn++;
63                print O "/* !checksrc! disable UNUSEDIGNORE all */\n";
64                print O "/* !checksrc! disable COPYRIGHT all */\n";
65                print O "/* !checksrc! disable FOPENMODE all */\n";
66                printf O "#line %d \"$f\"\n", $iline+1;
67            }
68        }
69        elsif($syn == 2) {
70            if(/^.fi/) {
71                last;
72            }
73            if(/(?<!\\)(?:\\{2})*\\(?!\\)/) {
74                print STDERR
75                  "Error while processing file $f line $iline:\n$_" .
76                  "Error: Single backslashes \\ are not properly shown in " .
77                  "manpage EXAMPLE output unless they are escaped \\\\.\n";
78                $fail = 1;
79                $error = 1;
80                last;
81            }
82            # two backslashes become one
83            $_ =~ s/\\\\/\\/g;
84            print O $_;
85            $l++;
86        }
87    }
88    close(F);
89    close(O);
90
91    return ($fail ? 0 : $l);
92}
93
94my $count;
95for my $m (@files) {
96    #print "Verify $m\n";
97    my $out = extract($m);
98    if($out) {
99      $error |= testcompile($m);
100      $error |= checksrc($m);
101    }
102    $count++;
103}
104if(!$error) {
105    print "Verified $count man pages ok\n";
106}
107else {
108    print "Detected problems\n";
109}
110exit $error;
111