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