summaryrefslogtreecommitdiff
path: root/lib/cli++/cli++topod.pl
blob: 5d2ef1350b2a2587dd52a819dc189b60696b5869 (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
211
212
213
#!/usr/bin/perl

sub cstr2pod {
    local($_) = shift;
    s/\\"/"/go;
    s/"([^\"]*)"/"C<$1>"/go;
    $_;
}

$section = 1;

@section_order = (
		  'NAME',
		  'SYNOPSIS',
		  'DESCRIPTION',
		  'OPTIONS',
		  'RETURN VALUE',
		  'ERRORS',
		  'EXAMPLES',
		  'ENVIRONMENT',
		  'FILES',
		  'SEE ALSO',
		  'NOTES',
		  'CAVEATS',
		  'WARNINGS',
		  'DIAGNOSTICS',
		  'BUGS',
		  'RESTRICTIONS',
		  'AUTHOR',
		  'AUTHORS',
		  'HISTORY'
		  );

sub type2word {
    my($type) = shift;
    return 'INT' if $type eq 'integer';
    return 'UINT' if $type eq 'ulong';
    return 'STR' if $type eq 'string' || $type eq 'stringlist';
    return '' if $type eq 'flag' || $type eq 'counter';
    die "Invalid cli option type '$type'";
}

sub add_option {
    my($short, $long, $type, $desc) = @_;

    my $s = '[B<';
    my $o = '=item B<';
    if($short) {
	$s .= "-$short";
	$o .= "-$short";
	if($type) {
	    $s .= " $type";
	    $o .= " $type";
	}
    }
    if($short && $long) {
	$s .= ">]\n[B<";
	$o .= ">, B<";
    }
    if($long) {
	$s .= "--$long";
	$o .= "--$long";
	if($type) {
	    $s .= "=$type";
	    $o .= "=$type";
	}
    }
    $s .= ">]\n";
    $o .= ">\n\n$desc\n\n";

    $synopsis .= $s;
    $options = "=over 8\n\n" unless $options;
    $options .= $o;
}

sub parse_option {
    local($_) = shift;
    s/^\s*\{\s*//o;
    s/\s*\},?\s*/ /o;

    my $short = $1 if s/^'([^\'])',\s*//o;
    die "Invalid cli option" unless $short || s/^0,\s*//o;

    my $long = $1 if s/^"([^\"]+)",\s*//o;
    die "Invalid cli_option" unless $long || s/^0,\s*//o;

    my $type = $1 if s/^cli_option::(\S+),\s*//o;
    die "Invalid cli_option" unless $type;
    $type = &type2word($type);

    my $val = $1 if s/^([^,]+),\s*//o;
    my $var = $1 if s/^&([^,]+),\s*//o;

    my $desc = cstr2pod($1) if s/^"([^,]+)",\s*//o;
    die "Invalid cli_option" unless $desc;
    $desc =~ s/\.?$/./o if $desc;

    my $default = $1 if s/^"([^\"]+)"\s+//o;
    die "Invalid cli_option" unless $default || s/^0\s+//o;
    $desc .= " Defaults to $default." if $default;

    s/\s*\/\/\s*/ /go;
    s/^\s*//o;

    add_option($short, $long, $type, $_ || $desc);
}

sub parse_options {
    $synopsis = "B<$program>\n";

    my $line;
    while(<>) {
	s/^\s+//o;
	s/\s+$//o;
	if($line && /^\{/o) {
	    &parse_option($line);
	    $line = "";
	}
	next if /^\{\s*0\s*\},?/o;
	next if /^\{\s*0\s*,\s*\},?/o;
	last if /^\s*\};/o;
	$line =~ s/$/ $_/;
    }
    &parse_option($line) if $line;

    $synopsis .= "I<$usage>" if $usage;
    $options .= "=back" if $options;
    $sections{'SYNOPSIS'} = $synopsis;
    $sections{'OPTIONS'} = $options;
}

sub parse_notes {
    my $section;
    my $title;
    while(<>) {
	chomp;
	last unless /^$/o || s/^\/\/\s*//o;
	if(/^[\sA-Z]+$/o) {
	    $sections{$title} = $section if $title && $section;
	    undef $section;
	    $title = $_;
	} else {
	    $section .= "$_\n";
	}
    }
    $sections{$title} = $section if $title && $section;
}

sub parse_header_line {
    local($_, $comment) = @_;
    if(s/^\s*const\s+char\s*\*\s*cli_(\S+)\s*=\s*//o) {
	my $name = $1;
	s/;\s*$//o;
	s/^\"//;
	s/\"$//o;
	s/\\n$//o;
	s/\\n""/\n/go;
	$program = $_ if $name eq 'program';
	$prefix = $_ if $name eq 'help_prefix';
	$usage = $_ if $name eq 'args_usage';
	$suffix = $_ if $name eq 'help_suffix';
    }
}

sub parse_header {
    my $comment = '';
    my $line = '';
    while(<>) {
	s/^\s+//o;
	s/\s+$//o;
	if(s/^.*Copyright\s*\(C\)\s*[\d,]+\s*//o) {
	    $author = $_;
	} else {
	    last if ($program && $prefix && /^$/o);
	    next if /^#/o;
	    $comment .= "$1\n" if s|\s*//\s*(.*)$||o;
	    $line =~ s/$/\n$_/;
	    if(/;$/o) {
		&parse_header_line($line, $comment);
		undef $line;
		undef $comment;
	    }
	}
    }
}

sub parse_description {
    while(<>) {
	s/^\s+//o;
	s/\s+$//o;
	last if / cli_options\[\]\s*=\s*\{/o;
	next unless s/^\/\/\s*//o;
	$description .= "$_\n";
    }
}

&parse_header;
&parse_description;
&parse_options;
&parse_notes;

$description .= "\n\n$suffix\n" if $suffix;

$sections{'NAME'} = "$program - $prefix";
$sections{'DESCRIPTION'} = $description;
$sections{'AUTHORS'} = $author if $author;

foreach $section (@section_order) {
    print "=head1 $section\n\n$sections{$section}\n\n"
	if $sections{$section};
}

1;