3295
|
1 #include <string> |
|
2 #include <iostream.h> |
|
3 |
|
4 #ifndef NPOS |
|
5 #define NPOS string::npos |
|
6 #endif |
|
7 |
|
8 static bool |
|
9 looks_like_octave_copyright (const string& s) |
|
10 { |
|
11 bool retval = false; |
|
12 |
|
13 string t = s.substr (0, 15); |
|
14 |
|
15 if (t == " Copyright (C) ") |
|
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 |
|
27 t = s.substr (pos, 29); |
|
28 |
|
29 if (t == " This file is part of Octave." |
|
30 || t == " This program is free softwar") |
|
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 |
|
42 static string |
|
43 extract_help_text (void) |
|
44 { |
|
45 string help_txt; |
|
46 |
|
47 bool first_comments_seen = false; |
|
48 bool begin_comment = false; |
|
49 bool have_help_text = false; |
|
50 bool in_comment = false; |
|
51 int c; |
|
52 |
|
53 while ((c = cin.get ()) != EOF) |
|
54 { |
|
55 if (begin_comment) |
|
56 { |
|
57 if (c == '%' || c == '#') |
|
58 continue; |
|
59 else |
|
60 begin_comment = false; |
|
61 } |
|
62 |
|
63 if (in_comment) |
|
64 { |
|
65 if (! have_help_text) |
|
66 { |
|
67 first_comments_seen = true; |
|
68 help_txt += (char) c; |
|
69 } |
|
70 |
|
71 if (c == '\n') |
|
72 { |
|
73 in_comment = false; |
|
74 |
|
75 if ((c = cin.get ()) != EOF) |
|
76 { |
|
77 if (c == '\n') |
|
78 break; |
|
79 } |
|
80 else |
|
81 break; |
|
82 } |
|
83 } |
|
84 else |
|
85 { |
|
86 switch (c) |
|
87 { |
|
88 case ' ': |
|
89 case '\t': |
|
90 if (first_comments_seen) |
|
91 have_help_text = true; |
|
92 break; |
|
93 |
|
94 case '\n': |
|
95 if (first_comments_seen) |
|
96 have_help_text = true; |
|
97 continue; |
|
98 |
|
99 case '%': |
|
100 case '#': |
|
101 begin_comment = true; |
|
102 in_comment = true; |
|
103 break; |
|
104 |
|
105 default: |
|
106 goto done; |
|
107 } |
|
108 } |
|
109 } |
|
110 |
|
111 done: |
|
112 |
|
113 if (! help_txt.empty ()) |
|
114 { |
|
115 if (looks_like_octave_copyright (help_txt)) |
|
116 help_txt.resize (0); |
|
117 |
|
118 if (help_txt.empty ()) |
|
119 help_txt = extract_help_text (); |
|
120 } |
|
121 |
|
122 return help_txt; |
|
123 } |
|
124 |
|
125 int |
|
126 main (int argc, char **argv) |
|
127 { |
|
128 string name; |
|
129 |
|
130 if (argc != 2) |
|
131 { |
|
132 cerr << "usage: gethelp name\n"; |
|
133 return 1; |
|
134 } |
|
135 else |
|
136 name = argv[1]; |
|
137 |
|
138 string help_text = extract_help_text (); |
|
139 |
|
140 if (! help_text.empty ()) |
|
141 { |
|
142 cout << "" << name << "\n" << help_text; |
|
143 |
|
144 if (help_text[help_text.length () - 1] != '\n') |
|
145 cout << "\n"; |
|
146 } |
|
147 |
|
148 return 0; |
|
149 } |