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