Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 7b725f4

Browse files
lassikomasanori
authored andcommitted
Harmonize Perl scripts
mkloader.pl and mkboot.pl encode a C string the same way. This job is now done in the constant() subroutine, identical in both scripts.
1 parent 1d60ccc commit 7b725f4

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

lib/ext/boot.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,10 @@ static const char boot_rom[][80] = {
236236
" ,@(map (lambda (x)\n `(,(the 'define-syntax) ,(car x) ,(",
237237
"cadr x)))\n formal)\n ,@body))))\n\n(define-macro let-syntax\n",
238238
" (lambda (form env)\n `(,(the 'letrec-syntax) ,@(cdr form))))\n",
239+
"",
239240
};
240241

242+
241243
#if PIC_USE_LIBRARY
242244
static const char boot_library_rom[][80] = {
243245
";;; There are two ways to name a library: (foo bar) or foo.bar\n;;; The former is",
@@ -367,7 +369,9 @@ static const char boot_library_rom[][80] = {
367369
" ((e eval))\n (lambda (expr . lib)\n (let ((lib (if (null? lib",
368370
") (current-library) (car lib))))\n (e expr (library-environment lib)",
369371
")))))\n (make-library '(picrin user))\n (current-library '(picrin user)))\n\n",
372+
"",
370373
};
374+
371375
#endif
372376

373377
void

tools/mkboot.pl

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,39 @@
22

33
use strict;
44

5-
# The maximum length of a string literal is 509 characters in C89.
6-
# That is why the boot_rom is split into short strings.
7-
my $chunk = 80;
8-
9-
sub print_escape_char($) {
10-
my $c = shift;
11-
if ($c eq "\n") {
12-
print "\\", "n";
13-
} elsif (($c eq "\\") || ($c eq '"')) {
14-
print "\\", $c;
15-
} else {
16-
print $c;
5+
sub constant($$) {
6+
# The maximum length of a string literal is 509 characters in C89.
7+
# That is why src is split into short strings.
8+
my ($var, $src) = @_;
9+
print "static const char ${var}[][80] = {\n";
10+
my @lines = $src =~ /.{0,80}/gs;
11+
foreach (@lines) {
12+
s/\\/\\\\/g;
13+
s/"/\\"/g;
14+
s/\n/\\n/g;
15+
print "\"$_\",\n";
1716
}
17+
print "};\n\n";
1818
}
1919

20+
local $/ = undef;
21+
2022
print <<EOL;
2123
#include "picrin.h"
2224
#include "picrin/extra.h"
2325
24-
static const char boot_rom[][$chunk] = {
2526
EOL
26-
print "\"";
27-
my $len = 0;
2827
open(IN, "piclib/boot.scm");
29-
while (read(IN, my $c, 1)) {
30-
if ($len && ($len % $chunk == 0)) { print "\",\n\""; }
31-
print_escape_char($c);
32-
$len++;
33-
}
34-
if ($!) { die "read error"; }
28+
constant("boot_rom", <IN>);
3529
close(IN);
3630
print <<EOL;
37-
",
38-
};
3931
4032
#if PIC_USE_LIBRARY
41-
static const char boot_library_rom[][$chunk] = {
4233
EOL
43-
print "\"";
44-
my $len = 0;
4534
open(IN, "piclib/library.scm");
46-
while (read(IN, my $c, 1)) {
47-
if ($len && ($len % $chunk == 0)) { print "\",\n\""; }
48-
print_escape_char($c);
49-
$len++;
50-
}
51-
if ($!) { die "read error"; }
35+
constant("boot_library_rom", <IN>);
5236
close(IN);
5337
print <<EOL;
54-
",
55-
};
5638
#endif
5739
5840
void

tools/mkloader.pl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
use strict;
44
use File::Basename qw/basename dirname/;
55

6+
sub constant($$) {
7+
# The maximum length of a string literal is 509 characters in C89.
8+
# That is why src is split into short strings.
9+
my ($var, $src) = @_;
10+
print "static const char ${var}[][80] = {\n";
11+
my @lines = $src =~ /.{0,80}/gs;
12+
foreach (@lines) {
13+
s/\\/\\\\/g;
14+
s/"/\\"/g;
15+
s/\n/\\n/g;
16+
print "\"$_\",\n";
17+
}
18+
print "};\n\n";
19+
}
20+
621
print <<EOL;
722
/**
823
* !!NOTICE!!
@@ -18,21 +33,13 @@
1833

1934
foreach my $file (@ARGV) {
2035
my $var = &escape_v($file);
21-
print "static const char ${var}[][80] = {\n";
2236

2337
open IN, $file;
2438
local $/ = undef;
2539
my $src = <IN>;
2640
close IN;
2741

28-
my @lines = $src =~ /.{0,80}/gs;
29-
foreach (@lines) {
30-
s/\\/\\\\/g;
31-
s/"/\\"/g;
32-
s/\n/\\n/g;
33-
print "\"$_\",\n";
34-
}
35-
print "};\n\n";
42+
constant($var, $src);
3643
}
3744

3845
print <<EOL;

0 commit comments

Comments
 (0)