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