summaryrefslogtreecommitdiff
path: root/src/roff/grog/grog.pl
blob: a61106c1cd8f92f06f057a99184ea12346912dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/perl
# grog -- guess options for groff command
# Inspired by doctype script in Kernighan & Pike, Unix Programming
# Environment, pp 306-8.

$prog = $0;
$prog =~ s@.*/@@;

$sp = "[\\s\\n]";

push(@command, "groff");

while ($ARGV[0] =~ /^-./) {
    $arg = shift(@ARGV);
    $sp = "" if $arg eq "-C";
    &usage(0) if $arg eq "-v" || $arg eq "--version";
    &help() if $arg eq "--help";
    last if $arg eq "--";
    push(@command, $arg);
}

if (@ARGV) {
    foreach $arg (@ARGV) {
	&process($arg, 0);
    }
}
else {
    &process("-", 0);
}

sub process {
    local($filename, $level) = @_;
    local(*FILE);

    if (!open(FILE, $filename eq "-" ? $filename : "< $filename")) {
	print STDERR "$prog: can't open \`$filename': $!\n";
	exit 1 unless $level;
	return;
    }
    while (<FILE>) {
	if (/^\.TS$sp/) {
	    $_ = <FILE>;
	    if (!/^\./) {
		$tbl++;
		$soelim++ if $level;
	    }
	}
	elsif (/^\.EQ$sp/) {
	    $_ = <FILE>;
	    if (!/^\./ || /^\.[0-9]/) {
		$eqn++;
		$soelim++ if $level;
	    }
	}
	elsif (/^\.GS$sp/) {
	    $_ = <FILE>;
	    if (!/^\./) {
		$grn++;
		$soelim++ if $level;
	    }
	}
	elsif (/^\.G1$sp/) {
	    $_ = <FILE>;
	    if (!/^\./) {
		$grap++;
		$pic++;
		$soelim++ if $level;
	    }
	}
	elsif (/^\.PS$sp([ 0-9.<].*)?$/) {
	    if (/^\.PS\s*<\s*(\S+)/) {
		$pic++;
		$soelim++ if $level;
		&process($1, $level);
	    }
	    else {
		$_ = <FILE>;
		if (!/^\./ || /^\.ps/) {
		    $pic++;
		    $soelim++ if $level;
		}
	    }
	}
	elsif (/^\.R1$sp/ || /^\.\[$sp/) {
	    $refer++;
	    $soelim++ if $level;
	}
	elsif (/^\.[PLI]P$sp/) {
	    $PP++;
	}
	elsif (/^\.P$/) {
	    $P++;
	}
	elsif (/^\.(PH|SA)$sp/) {
	    $mm++;
	}
	elsif (/^\.TH$sp/) {
	    $TH++;
	}
	elsif (/^\.SH$sp/) {
	    $SH++;
	}
	elsif (/^\.([pnil]p|sh)$sp/) {
	    $me++;
	}
	elsif (/^\.Dd$sp/) {
	    $mdoc++;
	}
	elsif (/^\.(Tp|Dp|De|Cx|Cl)$sp/) {
	    $mdoc_old = 1;
	}
	# In the old version of -mdoc `Oo' is a toggle, in the new it's
	# closed by `Oc'.
	elsif (/^\.Oo$sp/) {
	    $Oo++;
	    s/^\.Oo/\. /;
	    redo;
	}
	# The test for `Oo' and `Oc' not starting a line (as allowed by the
	# new implementation of -mdoc) is not complete; it assumes that
	# macro arguments are well behaved, i.e., "" is used within "..." to
	# indicate a doublequote as a string element, and weird features
	# like `.foo a"b' are not used.
	elsif (/^\..* Oo( |$)/) {
	    s/\\\".*//;
	    s/\"[^\"]*\"//g;
	    s/\".*//;
	    if (s/ Oo( |$)/ /) {
		$Oo++;
	    }
	    redo;
	}
	elsif (/^\.Oc$sp/) {
	    $Oo--;
	    s/^\.Oc/\. /;
	    redo;
	}
	elsif (/^\..* Oc( |$)/) {
	    s/\\\".*//;
	    s/\"[^\"]*\"//g;
	    s/\".*//;
	    if (s/ Oc( |$)/ /) {
		$Oo--;
	    }
	    redo;
	}
	if (/^\.so$sp/) {
	    chop;
	    s/^.so *//;
	    s/\\\".*//;
	    s/ .*$//;
	    &process($_, $level + 1) unless /\\/ || $_ eq "";
	}
    }
    close(FILE);
}

sub usage {
    local($exit_status) = $_;
    print "GNU grog (groff) version @VERSION@\n";
    exit $exit_status;
}

sub help {
    print "usage: grog [ option ...] [files...]\n";
    exit 0;
}

if ($pic || $tbl || $eqn || $grn || $grap || $refer) {
    $s = "-";
    $s .= "s" if $soelim;
    $s .= "R" if $refer;
    # grap must be run before pic
    $s .= "G" if $grap;
    $s .= "p" if $pic;
    $s .= "g" if $grn;
    $s .= "t" if $tbl;
    $s .= "e" if $eqn;
    push(@command, $s);
}

if ($me > 0) {
    push(@command, "-me");
}
elsif ($SH > 0 && $TH > 0) {
    push(@command, "-man");
}
elsif ($PP > 0) {
    push(@command, "-ms");
}
elsif ($P > 0 || $mm > 0) {
    push(@command, "-mm");
}
elsif ($mdoc > 0) {
    push(@command, ($mdoc_old || $Oo > 0) ? "-mdoc-old" : "-mdoc");
}

push(@command, "--") if @ARGV && $ARGV[0] =~ /^-./;

push(@command, @ARGV);

# We could implement an option to execute the command here.

foreach (@command) {
    next unless /[\$\\\"\';&()|<> \t\n]/;
    s/\'/\'\\\'\'/;
    $_ = "'" . $_ . "'";
}

print join(' ', @command), "\n";