3313
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1999, 2000, 2002, 2003, 2005, 2007 John W. Eaton |
3313
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3313
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3313
|
20 |
|
21 */ |
|
22 |
4279
|
23 #if defined (__DECCXX) |
|
24 #define __USE_STD_IOSTREAM |
|
25 #endif |
|
26 |
4177
|
27 #include <cctype> |
3575
|
28 #include <iostream> |
|
29 #include <fstream> |
3294
|
30 #include <string> |
4215
|
31 #include <map> |
3294
|
32 |
7048
|
33 #include <cstdlib> |
|
34 #include <cstring> |
|
35 |
3294
|
36 static const char doc_delim = ''; |
|
37 |
4178
|
38 static std::map<std::string, std::string> help_text; |
4177
|
39 |
3294
|
40 static void |
3575
|
41 fatal (const std::string& msg) |
3294
|
42 { |
3575
|
43 std::cerr << msg << "\n"; |
3294
|
44 exit (1); |
|
45 } |
|
46 |
|
47 static void |
|
48 usage (void) |
|
49 { |
3575
|
50 std::cerr << "usage: munge-texi -d DOCSTRING-FILE file ...\n"; |
3294
|
51 exit (1); |
|
52 } |
|
53 |
3575
|
54 static std::string |
3576
|
55 extract_symbol_name (std::istream& is) |
3294
|
56 { |
3575
|
57 std::string symbol_name; |
3294
|
58 |
|
59 int c; |
|
60 while ((c = is.get ()) != EOF && c != '\n') |
|
61 symbol_name += (char) c; |
|
62 |
|
63 return symbol_name; |
|
64 } |
|
65 |
3575
|
66 static std::string |
|
67 extract_docstring (std::istream& is) |
3294
|
68 { |
3575
|
69 std::string doc; |
3294
|
70 |
|
71 int c; |
|
72 while ((c = is.get ()) != EOF && c != doc_delim) |
|
73 doc += (char) c; |
|
74 |
|
75 return doc; |
|
76 } |
|
77 |
|
78 static void |
5334
|
79 skip_comments (std::ifstream& is) |
|
80 { |
|
81 int c; |
|
82 |
|
83 bool in_comment = false; |
|
84 |
|
85 while ((c = is.get ()) != EOF) |
|
86 { |
|
87 if (c == '#') |
|
88 in_comment = true; |
|
89 else if (c == '\n') |
|
90 in_comment = false; |
|
91 else if (! (in_comment || ::isspace (c))) |
|
92 { |
|
93 is.putback (c); |
|
94 break; |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 static void |
3575
|
100 process_doc_file (const std::string& fname) |
3294
|
101 { |
3575
|
102 std::ifstream infile (fname.c_str ()); |
3294
|
103 |
|
104 if (infile) |
|
105 { |
5334
|
106 skip_comments (infile); |
|
107 |
3294
|
108 if (infile.get () != doc_delim) |
|
109 fatal ("invalid doc file format"); |
|
110 |
3575
|
111 std::string symbol_name; |
3294
|
112 |
|
113 do |
|
114 { |
|
115 symbol_name = extract_symbol_name (infile); |
|
116 |
|
117 if (! symbol_name.empty ()) |
|
118 { |
3575
|
119 std::string doc_string = extract_docstring (infile); |
3294
|
120 |
4177
|
121 if (help_text.find (symbol_name) != help_text.end ()) |
3575
|
122 std::cerr << "ignoring duplicate entry for " |
|
123 << symbol_name << "\n"; |
3294
|
124 else |
|
125 help_text[symbol_name] = doc_string; |
|
126 } |
|
127 } |
|
128 while (! symbol_name.empty ()); |
|
129 } |
|
130 else |
|
131 fatal ("unable to open docfile"); |
|
132 } |
|
133 |
|
134 static void |
3575
|
135 process_texi_input_file (std::istream& is, std::ostream& os) |
3294
|
136 { |
3404
|
137 os << "@c DO NOT EDIT! Generated automatically by munge-texi.\n\n"; |
3294
|
138 |
|
139 bool bol = true; |
|
140 |
|
141 int c; |
|
142 while ((c = is.get ()) != EOF) |
|
143 { |
|
144 if (bol) |
|
145 { |
|
146 if (c == '@') |
|
147 { |
3575
|
148 std::string symbol_name; |
3294
|
149 |
|
150 char buf[16]; |
|
151 int i = 0; |
|
152 buf[i++] = c; |
|
153 |
|
154 if ((buf[i++] = (char) is.get ()) == 'D' |
|
155 && (buf[i++] = (char) is.get ()) == 'O' |
|
156 && (buf[i++] = (char) is.get ()) == 'C' |
|
157 && (buf[i++] = (char) is.get ()) == 'S' |
|
158 && (buf[i++] = (char) is.get ()) == 'T' |
|
159 && (buf[i++] = (char) is.get ()) == 'R' |
|
160 && (buf[i++] = (char) is.get ()) == 'I' |
|
161 && (buf[i++] = (char) is.get ()) == 'N' |
|
162 && (buf[i++] = (char) is.get ()) == 'G' |
|
163 && (buf[i++] = (char) is.get ()) == '(') |
|
164 { |
|
165 while ((c = is.get ()) != EOF && c != ')') |
|
166 symbol_name += (char) c; |
|
167 |
|
168 if (is.eof ()) |
|
169 fatal ("end of file while reading @DOCSTRING command"); |
|
170 else |
3301
|
171 { |
3575
|
172 std::string doc_string = help_text[symbol_name]; |
3301
|
173 |
4623
|
174 int j = 0; |
|
175 while (doc_string[j] == ' ') |
|
176 j++; |
3301
|
177 |
4623
|
178 if (doc_string.substr (j, 15) == "-*- texinfo -*-") |
3301
|
179 { |
4623
|
180 j += 15; |
3301
|
181 |
4623
|
182 while (isspace (doc_string[j])) |
|
183 j++; |
3301
|
184 |
3401
|
185 // Make `see also' references in functions |
|
186 // possible using @anchor{TAG} (new with |
|
187 // Texinfo 4.0). |
|
188 |
|
189 os << "@anchor{doc-" << symbol_name << "}\n"; |
|
190 |
4623
|
191 os << doc_string.substr (j); |
3301
|
192 } |
|
193 else |
|
194 os << doc_string; |
|
195 } |
3294
|
196 } |
|
197 else |
|
198 { |
|
199 buf[i] = '\0'; |
|
200 os << buf; |
|
201 |
|
202 if (buf[i - 1] == '\n') |
|
203 bol = true; |
|
204 } |
|
205 } |
|
206 else |
|
207 os.put ((char) c); |
|
208 } |
|
209 else |
|
210 { |
|
211 if (c == '\n') |
|
212 bol = true; |
|
213 |
|
214 os.put ((char) (c)); |
|
215 } |
|
216 } |
|
217 } |
|
218 |
|
219 int |
|
220 main (int argc, char **argv) |
|
221 { |
|
222 while (*++argv) |
|
223 { |
|
224 if (! strcmp (*argv, "-d")) |
|
225 { |
|
226 if (*++argv) |
|
227 process_doc_file (*argv); |
|
228 else |
|
229 usage (); |
|
230 } |
|
231 else |
|
232 break; |
|
233 } |
|
234 |
3575
|
235 process_texi_input_file (std::cin, std::cout); |
3294
|
236 |
|
237 return 0; |
|
238 } |
3313
|
239 |
|
240 /* |
|
241 ;;; Local Variables: *** |
|
242 ;;; mode: C++ *** |
|
243 ;;; End: *** |
|
244 */ |