4279
|
1 #if defined (__DECCXX) |
|
2 #define __USE_STD_IOSTREAM |
|
3 #endif |
|
4 |
3295
|
5 #include <string> |
3575
|
6 #include <iostream> |
3295
|
7 |
|
8 #ifndef NPOS |
3575
|
9 #define NPOS std::string::npos |
3295
|
10 #endif |
|
11 |
|
12 static bool |
3575
|
13 looks_like_octave_copyright (const std::string& s) |
3295
|
14 { |
|
15 bool retval = false; |
|
16 |
3575
|
17 std::string t = s.substr (0, 14); |
3295
|
18 |
3427
|
19 if (t == "Copyright (C) ") |
3295
|
20 { |
|
21 size_t pos = s.find ('\n'); |
|
22 |
|
23 if (pos != NPOS) |
|
24 { |
|
25 pos = s.find ('\n', pos + 1); |
|
26 |
|
27 if (pos != NPOS) |
|
28 { |
|
29 pos++; |
|
30 |
3427
|
31 t = s.substr (pos, 28); |
3295
|
32 |
3427
|
33 if (t == "This file is part of Octave." |
|
34 || t == "This program is free softwar") |
3295
|
35 retval = true; |
|
36 } |
|
37 } |
|
38 } |
|
39 |
|
40 return retval; |
|
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 } |