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 #ifndef NPOS |
3575
|
31 #define NPOS std::string::npos |
3295
|
32 #endif |
|
33 |
|
34 static bool |
3575
|
35 looks_like_octave_copyright (const std::string& s) |
3295
|
36 { |
6528
|
37 // Perhaps someday we will want to do more here, so leave this as a |
|
38 // separate function. |
3295
|
39 |
6528
|
40 return (s.substr (0, 9) == "Copyright"); |
3295
|
41 } |
|
42 |
|
43 // Eat whitespace and comments from FFILE, returning the text of the |
|
44 // first block of comments that doesn't look like a copyright notice, |
|
45 |
3575
|
46 static std::string |
3295
|
47 extract_help_text (void) |
|
48 { |
3575
|
49 std::string help_txt; |
3295
|
50 |
|
51 bool first_comments_seen = false; |
|
52 bool begin_comment = false; |
|
53 bool have_help_text = false; |
|
54 bool in_comment = false; |
3427
|
55 bool discard_space = true; |
3295
|
56 int c; |
|
57 |
3575
|
58 while ((c = std::cin.get ()) != EOF) |
3295
|
59 { |
|
60 if (begin_comment) |
|
61 { |
|
62 if (c == '%' || c == '#') |
|
63 continue; |
3427
|
64 else if (discard_space && c == ' ') |
|
65 { |
|
66 discard_space = false; |
|
67 continue; |
|
68 } |
3295
|
69 else |
|
70 begin_comment = false; |
|
71 } |
|
72 |
|
73 if (in_comment) |
|
74 { |
|
75 if (! have_help_text) |
|
76 { |
|
77 first_comments_seen = true; |
|
78 help_txt += (char) c; |
|
79 } |
|
80 |
|
81 if (c == '\n') |
|
82 { |
|
83 in_comment = false; |
3427
|
84 discard_space = true; |
3295
|
85 |
3575
|
86 if ((c = std::cin.get ()) != EOF) |
3295
|
87 { |
|
88 if (c == '\n') |
|
89 break; |
|
90 } |
|
91 else |
|
92 break; |
|
93 } |
|
94 } |
|
95 else |
|
96 { |
|
97 switch (c) |
|
98 { |
|
99 case ' ': |
|
100 case '\t': |
|
101 if (first_comments_seen) |
|
102 have_help_text = true; |
|
103 break; |
|
104 |
|
105 case '\n': |
|
106 if (first_comments_seen) |
|
107 have_help_text = true; |
|
108 continue; |
|
109 |
|
110 case '%': |
|
111 case '#': |
|
112 begin_comment = true; |
|
113 in_comment = true; |
|
114 break; |
|
115 |
|
116 default: |
|
117 goto done; |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 done: |
|
123 |
|
124 if (! help_txt.empty ()) |
|
125 { |
|
126 if (looks_like_octave_copyright (help_txt)) |
|
127 help_txt.resize (0); |
|
128 |
|
129 if (help_txt.empty ()) |
|
130 help_txt = extract_help_text (); |
|
131 } |
|
132 |
|
133 return help_txt; |
|
134 } |
|
135 |
|
136 int |
|
137 main (int argc, char **argv) |
|
138 { |
3575
|
139 std::string name; |
3295
|
140 |
|
141 if (argc != 2) |
|
142 { |
3575
|
143 std::cerr << "usage: gethelp name\n"; |
3295
|
144 return 1; |
|
145 } |
|
146 else |
|
147 name = argv[1]; |
|
148 |
3575
|
149 std::string help_text = extract_help_text (); |
3295
|
150 |
|
151 if (! help_text.empty ()) |
|
152 { |
3576
|
153 std::cout << "" << name << "\n" << help_text; |
3295
|
154 |
|
155 if (help_text[help_text.length () - 1] != '\n') |
3576
|
156 std::cout << "\n"; |
3295
|
157 } |
|
158 |
|
159 return 0; |
|
160 } |