7016
|
1 /* |
|
2 |
|
3 Copyright (C) 1999, 2000, 2003, 2007 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
|
20 |
|
21 */ |
|
22 |
4279
|
23 #if defined (__DECCXX) |
|
24 #define __USE_STD_IOSTREAM |
|
25 #endif |
|
26 |
3295
|
27 #include <string> |
3575
|
28 #include <iostream> |
3295
|
29 |
|
30 static bool |
3575
|
31 looks_like_octave_copyright (const std::string& s) |
3295
|
32 { |
6528
|
33 // Perhaps someday we will want to do more here, so leave this as a |
|
34 // separate function. |
3295
|
35 |
6528
|
36 return (s.substr (0, 9) == "Copyright"); |
3295
|
37 } |
|
38 |
|
39 // Eat whitespace and comments from FFILE, returning the text of the |
|
40 // first block of comments that doesn't look like a copyright notice, |
|
41 |
3575
|
42 static std::string |
3295
|
43 extract_help_text (void) |
|
44 { |
3575
|
45 std::string help_txt; |
3295
|
46 |
|
47 bool first_comments_seen = false; |
|
48 bool begin_comment = false; |
|
49 bool have_help_text = false; |
|
50 bool in_comment = false; |
3427
|
51 bool discard_space = true; |
3295
|
52 int c; |
|
53 |
3575
|
54 while ((c = std::cin.get ()) != EOF) |
3295
|
55 { |
|
56 if (begin_comment) |
|
57 { |
|
58 if (c == '%' || c == '#') |
|
59 continue; |
3427
|
60 else if (discard_space && c == ' ') |
|
61 { |
|
62 discard_space = false; |
|
63 continue; |
|
64 } |
3295
|
65 else |
|
66 begin_comment = false; |
|
67 } |
|
68 |
|
69 if (in_comment) |
|
70 { |
|
71 if (! have_help_text) |
|
72 { |
|
73 first_comments_seen = true; |
|
74 help_txt += (char) c; |
|
75 } |
|
76 |
|
77 if (c == '\n') |
|
78 { |
|
79 in_comment = false; |
3427
|
80 discard_space = true; |
3295
|
81 |
3575
|
82 if ((c = std::cin.get ()) != EOF) |
3295
|
83 { |
|
84 if (c == '\n') |
|
85 break; |
|
86 } |
|
87 else |
|
88 break; |
|
89 } |
|
90 } |
|
91 else |
|
92 { |
|
93 switch (c) |
|
94 { |
|
95 case ' ': |
|
96 case '\t': |
|
97 if (first_comments_seen) |
|
98 have_help_text = true; |
|
99 break; |
|
100 |
|
101 case '\n': |
|
102 if (first_comments_seen) |
|
103 have_help_text = true; |
|
104 continue; |
|
105 |
|
106 case '%': |
|
107 case '#': |
|
108 begin_comment = true; |
|
109 in_comment = true; |
|
110 break; |
|
111 |
|
112 default: |
|
113 goto done; |
|
114 } |
|
115 } |
|
116 } |
|
117 |
|
118 done: |
|
119 |
|
120 if (! help_txt.empty ()) |
|
121 { |
|
122 if (looks_like_octave_copyright (help_txt)) |
|
123 help_txt.resize (0); |
|
124 |
|
125 if (help_txt.empty ()) |
|
126 help_txt = extract_help_text (); |
|
127 } |
|
128 |
|
129 return help_txt; |
|
130 } |
|
131 |
|
132 int |
|
133 main (int argc, char **argv) |
|
134 { |
3575
|
135 std::string name; |
3295
|
136 |
|
137 if (argc != 2) |
|
138 { |
3575
|
139 std::cerr << "usage: gethelp name\n"; |
3295
|
140 return 1; |
|
141 } |
|
142 else |
|
143 name = argv[1]; |
|
144 |
3575
|
145 std::string help_text = extract_help_text (); |
3295
|
146 |
|
147 if (! help_text.empty ()) |
|
148 { |
3576
|
149 std::cout << "" << name << "\n" << help_text; |
3295
|
150 |
|
151 if (help_text[help_text.length () - 1] != '\n') |
3576
|
152 std::cout << "\n"; |
3295
|
153 } |
|
154 |
|
155 return 0; |
|
156 } |