Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Psh::Builtins::Help; |
2 | ||
3 | require Psh::Support::Builtins; | |
4 | ||
5 | sub get_pod_from_file { | |
6 | my $tmpfile= shift; | |
7 | my $arg= shift; | |
8 | my $tmp=''; | |
9 | if( -r $tmpfile) { | |
10 | open(FILE, "< $tmpfile"); | |
11 | my $add=0; | |
12 | while(<FILE>) { | |
13 | if( !$add && /\=item \* C\<$arg[ >]/) { | |
14 | $tmp="\n".$_; | |
15 | $add=1; | |
16 | } elsif( $add) { | |
17 | $tmp.=$_; | |
18 | } | |
19 | if( $add && $_ =~ /\=cut/) { | |
20 | $add=0; | |
21 | last; | |
22 | } | |
23 | } | |
24 | close(FILE); | |
25 | } | |
26 | return $tmp; | |
27 | } | |
28 | ||
29 | =item * C<help [COMMAND]> | |
30 | ||
31 | If COMMAND is specified, print out help on it; otherwise print out a list of | |
32 | B<psh> builtins. | |
33 | ||
34 | =cut | |
35 | ||
36 | sub bi_help | |
37 | { | |
38 | my $arg= shift; | |
39 | if( $arg) { | |
40 | my $tmpfile; | |
41 | foreach my $line (@INC) { | |
42 | $tmpfile= Psh::OS::catfile( | |
43 | Psh::OS::catdir($line,'Psh','Builtins'), | |
44 | ucfirst($arg).'.pm'); | |
45 | $tmp= get_pod_from_file($tmpfile,$arg); | |
46 | last if $tmp; | |
47 | $tmpfile= Psh::OS::catfile( | |
48 | Psh::OS::catdir($line,'Psh','Builtins','Fallback'), | |
49 | ucfirst($arg).'.pm'); | |
50 | $tmp= get_pod_from_file($tmpfile,$arg); | |
51 | last if $tmp; | |
52 | } | |
53 | if( $tmp ) { | |
54 | Psh::OS::display_pod("=over 4\n".$tmp."\n=back\n"); | |
55 | } else { | |
56 | Psh::Util::print_error_i18n('no_help',$arg); | |
57 | } | |
58 | } else { | |
59 | Psh::Util::print_out_i18n('help_header'); | |
60 | Psh::Util::print_list(Psh::Support::Builtins::get_builtin_commands()); | |
61 | } | |
62 | return (1,undef); | |
63 | } | |
64 | ||
65 | sub any_help { | |
66 | my $text= shift; | |
67 | my ($com)= $text=~/^\s*(\S+)/; | |
68 | $com||=$text; | |
69 | print "\n"; | |
70 | if (Psh::Support::Builtins::is_builtin($com)) { | |
71 | bi_help($com); | |
72 | } else { | |
73 | system("man $com"); | |
74 | } | |
75 | eval { | |
76 | $Psh::term->on_new_line(); | |
77 | }; | |
78 | } | |
79 | ||
80 | sub cmpl_help { | |
81 | my $text= shift; | |
82 | return (1,grep { Psh::Util::starts_with($_,$text) } Psh::Support::Builtins::get_builtin_commands()); | |
83 | } | |
84 | ||
85 | 1; |