xref: /netbsd-src/external/bsd/less/dist/mkhelp.pl (revision e4a6e799a67c2028562d75b4e61407b22434aa36)
1#! /usr/bin/env perl
2use strict;
3
4# Silly little program to generate the help.c source file
5# from the less.hlp text file.
6# The output of this script is a C program defining a char array
7# whose content is the input to this script.
8
9{
10    my ($sec,$min,$hour,$mday,$mon,$year) = gmtime();
11    printf "/* This file was generated by mkhelp.pl from less.hlp at %d:%02d on %d/%d/%d */\n",
12        $hour, $min, $year+1900, $mon+1, $mday;
13    print "#include \"less.h\"\n";
14    print "constant char helpdata[] = {\n";
15    my $ch = 0;
16    my $prevch;
17    for (;;) {
18        $prevch = $ch;
19        $ch = getc();
20        last if not defined $ch;
21        if ($ch eq "'") {
22            print "'\\'',";
23        } elsif ($ch eq "\\") {
24            print "'\\\\',";
25        } elsif ($ch eq "\b") {
26            print "'\\b',";
27        } elsif ($ch eq "\t") {
28            print "'\\t',";
29        } elsif ($ch eq "\n") {
30            print "'\\n',\n" if $prevch ne "\r";
31        } elsif ($ch eq "\r") {
32            print "'\\n',\n" if $prevch ne "\n";
33        } else {
34            if (ord($ch) >= ord(' ') && ord($ch) < 0x7f) {
35                print "'$ch',";
36            } else {
37                printf "0x%02x,", ord($ch);
38            }
39        }
40    }
41    # Add an extra null char to avoid having a trailing comma.
42    print " 0 };\n";
43    print "constant int size_helpdata = sizeof(helpdata) - 1;\n";
44}
45