summaryrefslogtreecommitdiff
path: root/docs/autodocifier.pl
blob: 2ce1edd7530fee02aabb064cd7df6dca87ed6348 (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
#!/usr/bin/perl -w
#
# autodocufier.pl - extracts usage messages from busybox usage.c and
# pretty-prints them to stdout.

use strict;

my $line;
my $applet;
my $count;
my $full_usage;

open(USAGE, 'usage.h') or die "usage.h: $!";

while (defined($line = <USAGE>)) {
	$count=0;
	if ($line =~ /^#define (\w+)_trivial_usage/) {
		# grab the applet name
		$applet = $1;
		print "\n$applet:\n";

		while (defined($line = <USAGE>)) {
			if ( $count==0 ) {
			    $count++;
			    print "\t$applet ";
			} else { print "\t"; }
			$full_usage = $applet . "_full_usage";
			last if ( $line =~ /$full_usage/ );
			# Skip preprocessor stuff
			next if $line =~ /^\s*#/;
			# Strip the continuation char
			$line =~ s/\\$//;
			# strip quotes off
			$line =~ s/^\s*"//;
			$line =~ s/"\s*$//;
			# substitute escape sequences
			# (there's probably a better way to do this...)
			$line =~ s/\\t/	/g;
			$line =~ s/\\n//g;
			# fix up preprocessor macros
			$line =~ s/USAGE_\w+\([\s]*?(".*?").*?\)/$1/sg;
			# Strip any empty quotes out
			$line =~ s/"[\s]*"//sg;
			# strip line end quotes, again
			$line =~ s/^\s*"//;
			$line =~ s/"\s*$//;

			# Finally, print it
			print "$line\n";
		}
		printf("\n");
		while (defined($line = <USAGE>)) {
			if ( $count==0 ) {
			    $count++;
			    print "\t$applet ";
			} else { print "\t"; }
			# we're done if we hit a line lacking a '\' at the end
			#last if ! $line !~ /\\$/;
			if ( $line !~ /\\$/ ) {
			    #print "Got one at $line\n";
			    last;
			}
			# Skip preprocessor stuff
			next if $line =~ /^\s*#/;
			# Strip the continuation char
			$line =~ s/\\$//;
			# strip quotes off
			$line =~ s/^\s*"//;
			$line =~ s/"\s*$//;
			# substitute escape sequences
			# (there's probably a better way to do this...)
			$line =~ s/\\t/	/g;
			$line =~ s/\\n//g;
			# Automagically #define all preprocessor lines
			#$line =~ s/USAGE_\w+\([\s]*?(".*?")\s,\s".*"\s\)/$1/sg;
			$line =~ s/USAGE_\w+\(\s*?(".*").*\)/$1/sg;
			# Strip any empty quotes out
			$line =~ s/"[\s]*"//sg;
			# strip line end quotes, again
			$line =~ s/^\s*"//;
			$line =~ s/"\s*$//;

			# Finally, print it
			print "$line\n";
		}
		printf("\n\n");
	}
}