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