171
|
1 /* info.c -- Display nodes of Info files in multiple windows. */ |
|
2 |
|
3 /* This file is part of GNU Info, a program for reading online documentation |
|
4 stored in Info format. |
|
5 |
|
6 Copyright (C) 1993 Free Software Foundation, Inc. |
|
7 |
|
8 This program is free software; you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation; either version 2, or (at your option) |
|
11 any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program; if not, write to the Free Software |
|
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 Written by Brian Fox (bfox@ai.mit.edu). */ |
|
23 |
1242
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
171
|
28 #include "info.h" |
|
29 #include "dribble.h" |
173
|
30 #include "terminal.h" |
171
|
31 #include "getopt.h" |
|
32 |
|
33 /* Non-zero means search all indices for APROPOS_SEARCH_STRING. */ |
|
34 static int apropos_p = 0; |
|
35 |
|
36 /* Variable containing the string to search for when apropos_p is non-zero. */ |
|
37 static char *apropos_search_string = (char *)NULL; |
|
38 |
173
|
39 /* Non-zero means search all indices for INDEX_SEARCH_STRING. Unlike |
|
40 apropos, this puts the user at the node, running info. */ |
|
41 static int index_search_p = 0; |
|
42 |
|
43 /* Variable containing the string to search for when index_search_p is |
|
44 non-zero. */ |
|
45 static char *index_search_string = (char *)NULL; |
|
46 |
171
|
47 /* Non-zero means print version info only. */ |
|
48 static int print_version_p = 0; |
|
49 |
|
50 /* Non-zero means print a short description of the options. */ |
|
51 static int print_help_p = 0; |
|
52 |
|
53 /* Array of the names of nodes that the user specified with "--node" on the |
|
54 command line. */ |
|
55 static char **user_nodenames = (char **)NULL; |
|
56 static int user_nodenames_index = 0; |
|
57 static int user_nodenames_slots = 0; |
|
58 |
|
59 /* String specifying the first file to load. This string can only be set |
|
60 by the user specifying "--file" on the command line. */ |
|
61 static char *user_filename = (char *)NULL; |
|
62 |
|
63 /* String specifying the name of the file to dump nodes to. This value is |
|
64 filled if the user speficies "--output" on the command line. */ |
|
65 static char *user_output_filename = (char *)NULL; |
|
66 |
|
67 /* Non-zero indicates that when "--output" is specified, all of the menu |
|
68 items of the specified nodes (and their subnodes as well) should be |
|
69 dumped in the order encountered. This basically can print a book. */ |
|
70 int dump_subnodes = 0; |
|
71 |
|
72 /* Structure describing the options that Info accepts. We pass this structure |
|
73 to getopt_long (). If you add or otherwise change this structure, you must |
|
74 also change the string which follows it. */ |
|
75 #define APROPOS_OPTION 1 |
|
76 #define DRIBBLE_OPTION 2 |
|
77 #define RESTORE_OPTION 3 |
173
|
78 #define IDXSRCH_OPTION 4 |
171
|
79 static struct option long_options[] = { |
|
80 { "apropos", 1, 0, APROPOS_OPTION }, |
|
81 { "directory", 1, 0, 'd' }, |
|
82 { "node", 1, 0, 'n' }, |
|
83 { "file", 1, 0, 'f' }, |
|
84 { "subnodes", 0, &dump_subnodes, 1 }, |
|
85 { "output", 1, 0, 'o' }, |
|
86 { "help", 0, &print_help_p, 1 }, |
|
87 { "version", 0, &print_version_p, 1 }, |
|
88 { "dribble", 1, 0, DRIBBLE_OPTION }, |
|
89 { "restore", 1, 0, RESTORE_OPTION }, |
173
|
90 { "index-search", 1, 0, IDXSRCH_OPTION }, |
171
|
91 {NULL, 0, NULL, 0} |
|
92 }; |
|
93 |
|
94 /* String describing the shorthand versions of the long options found above. */ |
|
95 static char *short_options = "d:n:f:o:s"; |
|
96 |
|
97 /* Some "forward" declarations. */ |
|
98 static void usage (), info_short_help (), remember_info_program_name (); |
|
99 |
|
100 |
|
101 /* **************************************************************** */ |
|
102 /* */ |
|
103 /* Main Entry Point to the Info Program */ |
|
104 /* */ |
|
105 /* **************************************************************** */ |
|
106 |
|
107 int |
|
108 main (argc, argv) |
|
109 int argc; |
|
110 char **argv; |
|
111 { |
|
112 int getopt_long_index; /* Index returned by getopt_long (). */ |
|
113 NODE *initial_node; /* First node loaded by Info. */ |
|
114 |
|
115 #if defined (NeXT) && defined (NOTDEF) |
|
116 malloc_debug (0x0ffffffff); |
|
117 #endif /* NeXT && NOTDEF */ |
|
118 |
|
119 remember_info_program_name (argv[0]); |
|
120 |
|
121 while (1) |
|
122 { |
|
123 int option_character; |
|
124 |
|
125 option_character = getopt_long |
|
126 (argc, argv, short_options, long_options, &getopt_long_index); |
|
127 |
|
128 /* getopt_long () returns EOF when there are no more long options. */ |
|
129 if (option_character == EOF) |
|
130 break; |
|
131 |
|
132 /* If this is a long option, then get the short version of it. */ |
|
133 if (option_character == 0 && long_options[getopt_long_index].flag == 0) |
|
134 option_character = long_options[getopt_long_index].val; |
|
135 |
|
136 /* Case on the option that we have received. */ |
|
137 switch (option_character) |
|
138 { |
|
139 case 0: |
|
140 break; |
|
141 |
|
142 /* User wants to add a directory. */ |
|
143 case 'd': |
|
144 info_add_path (optarg, INFOPATH_PREPEND); |
|
145 break; |
|
146 |
|
147 /* User is specifying a particular node. */ |
|
148 case 'n': |
|
149 add_pointer_to_array (optarg, user_nodenames_index, user_nodenames, |
|
150 user_nodenames_slots, 10, char *); |
|
151 break; |
|
152 |
|
153 /* User is specifying a particular Info file. */ |
|
154 case 'f': |
|
155 if (user_filename) |
|
156 free (user_filename); |
|
157 |
|
158 user_filename = savestring (optarg); |
|
159 break; |
|
160 |
|
161 /* User is specifying the name of a file to output to. */ |
|
162 case 'o': |
|
163 if (user_output_filename) |
|
164 free (user_output_filename); |
|
165 user_output_filename = savestring (optarg); |
|
166 break; |
|
167 |
|
168 /* User is specifying that she wishes to dump the subnodes of |
|
169 the node that she is dumping. */ |
|
170 case 's': |
|
171 dump_subnodes = 1; |
|
172 break; |
|
173 |
|
174 /* User has specified a string to search all indices for. */ |
|
175 case APROPOS_OPTION: |
|
176 apropos_p = 1; |
|
177 maybe_free (apropos_search_string); |
|
178 apropos_search_string = savestring (optarg); |
|
179 break; |
|
180 |
|
181 /* User has specified a dribble file to receive keystrokes. */ |
|
182 case DRIBBLE_OPTION: |
|
183 close_dribble_file (); |
|
184 open_dribble_file (optarg); |
|
185 break; |
|
186 |
|
187 /* User has specified an alternate input stream. */ |
|
188 case RESTORE_OPTION: |
|
189 info_set_input_from_file (optarg); |
|
190 break; |
|
191 |
173
|
192 /* User has specified a string to search all indices for. */ |
|
193 case IDXSRCH_OPTION: |
|
194 index_search_p = 1; |
|
195 maybe_free (index_search_string); |
|
196 index_search_string = savestring (optarg); |
|
197 break; |
|
198 |
171
|
199 default: |
|
200 usage (); |
|
201 } |
|
202 } |
|
203 |
|
204 /* If the user specified --version, then show the version and exit. */ |
|
205 if (print_version_p) |
|
206 { |
|
207 printf ("GNU Info, Version %s.\n", version_string ()); |
|
208 exit (0); |
|
209 } |
|
210 |
|
211 /* If the `--help' option was present, show the help and exit. */ |
|
212 if (print_help_p) |
|
213 { |
|
214 info_short_help (); |
|
215 exit (0); |
|
216 } |
|
217 |
|
218 /* If the user hasn't specified a path for Info files, default that path |
|
219 now. */ |
|
220 if (!infopath) |
|
221 { |
|
222 char *path_from_env, *getenv (); |
|
223 |
|
224 path_from_env = getenv ("INFOPATH"); |
|
225 |
|
226 if (path_from_env) |
|
227 info_add_path (path_from_env); |
|
228 else |
|
229 info_add_path (DEFAULT_INFOPATH); |
|
230 } |
|
231 |
|
232 /* If the user specified a particular filename, add the path of that |
|
233 file to the contents of INFOPATH. */ |
|
234 if (user_filename) |
|
235 { |
|
236 char *directory_name, *temp; |
|
237 |
|
238 directory_name = savestring (user_filename); |
|
239 temp = filename_non_directory (directory_name); |
|
240 |
|
241 if (temp != directory_name) |
|
242 { |
|
243 *temp = 0; |
|
244 info_add_path (directory_name, INFOPATH_PREPEND); |
|
245 } |
|
246 |
|
247 free (directory_name); |
|
248 } |
|
249 |
|
250 /* If the user wants to search every known index for a given string, |
|
251 do that now, and report the results. */ |
|
252 if (apropos_p) |
|
253 { |
|
254 info_apropos (apropos_search_string); |
|
255 exit (0); |
|
256 } |
|
257 |
|
258 /* Get the initial Info node. It is either "(dir)Top", or what the user |
|
259 specifed with values in user_filename and user_nodenames. */ |
|
260 if (user_nodenames) |
|
261 initial_node = info_get_node (user_filename, user_nodenames[0]); |
|
262 else |
|
263 initial_node = info_get_node (user_filename, (char *)NULL); |
|
264 |
|
265 /* If we couldn't get the initial node, this user is in trouble. */ |
|
266 if (!initial_node) |
|
267 { |
|
268 if (info_recent_file_error) |
|
269 info_error (info_recent_file_error); |
|
270 else |
|
271 info_error |
|
272 (CANT_FIND_NODE, user_nodenames ? user_nodenames[0] : "Top"); |
|
273 exit (1); |
|
274 } |
|
275 |
|
276 /* Special cases for when the user specifies multiple nodes. If we are |
|
277 dumping to an output file, dump all of the nodes specified. Otherwise, |
|
278 attempt to create enough windows to handle the nodes that this user wants |
|
279 displayed. */ |
|
280 if (user_nodenames_index > 1) |
|
281 { |
|
282 free (initial_node); |
|
283 |
|
284 if (user_output_filename) |
|
285 dump_nodes_to_file |
|
286 (user_filename, user_nodenames, user_output_filename, dump_subnodes); |
|
287 else |
|
288 begin_multiple_window_info_session (user_filename, user_nodenames); |
|
289 |
|
290 exit (0); |
|
291 } |
|
292 |
173
|
293 /* If the user specified `--index-search string', start the info |
|
294 session in the node corresponding to the first match. */ |
|
295 if (index_search_p) |
|
296 { |
|
297 int status = 0; |
|
298 |
|
299 initialize_info_session (initial_node, 0); |
|
300 |
|
301 if (index_entry_exists (windows, index_search_string)) |
|
302 { |
|
303 terminal_clear_screen (); |
|
304 terminal_prep_terminal (); |
|
305 display_update_display (windows); |
|
306 info_last_executed_command = (VFunction *)NULL; |
|
307 |
|
308 do_info_index_search (windows, 0, index_search_string); |
|
309 |
|
310 info_read_and_dispatch (); |
|
311 |
|
312 terminal_unprep_terminal (); |
|
313 } |
|
314 else |
|
315 { |
|
316 fprintf (stderr, "no entries found\n"); |
|
317 status = 13; |
|
318 } |
|
319 |
|
320 close_dribble_file (); |
|
321 exit (status); |
|
322 } |
|
323 |
171
|
324 /* If there are arguments remaining, they are the names of menu items |
|
325 in sequential info files starting from the first one loaded. That |
|
326 file name is either "dir", or the contents of user_filename if one |
|
327 was specified. */ |
|
328 while (optind != argc) |
|
329 { |
|
330 REFERENCE **menu; |
|
331 REFERENCE *entry; |
|
332 NODE *node; |
|
333 char *arg; |
|
334 |
|
335 /* Remember the name of the menu entry we want. */ |
|
336 arg = argv[optind++]; |
|
337 |
|
338 /* Build and return a list of the menu items in this node. */ |
|
339 menu = info_menu_of_node (initial_node); |
|
340 |
|
341 /* If there wasn't a menu item in this node, stop here, but let |
|
342 the user continue to use Info. Perhaps they wanted this node |
|
343 and didn't realize it. */ |
|
344 if (!menu) |
|
345 { |
|
346 begin_info_session_with_error |
|
347 (initial_node, "There is no menu in this node."); |
|
348 exit (0); |
|
349 } |
|
350 |
|
351 /* Find the specified menu item. */ |
|
352 entry = info_get_labeled_reference (arg, menu); |
|
353 |
|
354 /* If the item wasn't found, search the list sloppily. Perhaps this |
|
355 user typed "buffer" when they really meant "Buffers". */ |
|
356 if (!entry) |
|
357 { |
|
358 register int i; |
|
359 |
|
360 for (i = 0; entry = menu[i]; i++) |
|
361 if (strnicmp (entry->label, arg, strlen (arg)) == 0) |
|
362 break; |
|
363 } |
|
364 |
|
365 /* If we failed to find the reference, start Info with the current |
|
366 node anyway. It is probably a misspelling. */ |
|
367 if (!entry) |
|
368 { |
|
369 char *error_message = "There is no menu item \"%s\" in this node."; |
|
370 |
|
371 info_free_references (menu); |
|
372 |
|
373 /* If we were supposed to dump this node, complain. */ |
|
374 if (user_output_filename) |
|
375 info_error (error_message, arg); |
|
376 else |
|
377 begin_info_session_with_error (initial_node, error_message, arg); |
|
378 |
|
379 exit (0); |
|
380 } |
|
381 |
|
382 /* We have found the reference that the user specified. Clean it |
|
383 up a little bit. */ |
|
384 if (!entry->filename) |
|
385 { |
|
386 if (initial_node->parent) |
|
387 entry->filename = savestring (initial_node->parent); |
|
388 else |
|
389 entry->filename = savestring (initial_node->filename); |
|
390 } |
|
391 |
|
392 /* Find this node. If we can find it, then turn the initial_node |
|
393 into this one. If we cannot find it, try using the label of the |
|
394 entry as a file (i.e., "(LABEL)Top"). Otherwise the Info file is |
|
395 malformed in some way, and we will just use the current value of |
|
396 initial node. */ |
|
397 node = info_get_node (entry->filename, entry->nodename); |
|
398 |
|
399 if (!node && entry->nodename && |
|
400 (strcmp (entry->label, entry->nodename) == 0)) |
|
401 node = info_get_node (entry->label, "Top"); |
|
402 |
|
403 if (node) |
|
404 { |
|
405 free (initial_node); |
|
406 initial_node = node; |
|
407 info_free_references (menu); |
|
408 } |
|
409 else |
|
410 { |
|
411 char *temp = savestring (entry->label); |
|
412 char *error_message; |
|
413 |
|
414 error_message = "Unable to find the node referenced by \"%s\"."; |
|
415 |
|
416 info_free_references (menu); |
|
417 |
|
418 /* If we were trying to dump the node, then give up. Otherwise, |
|
419 start the session with an error message. */ |
|
420 if (user_output_filename) |
|
421 info_error (error_message, temp); |
|
422 else |
|
423 begin_info_session_with_error (initial_node, error_message, temp); |
|
424 |
|
425 exit (0); |
|
426 } |
|
427 } |
|
428 |
|
429 /* If the user specified that this node should be output, then do that |
|
430 now. Otherwise, start the Info session with this node. */ |
|
431 if (user_output_filename) |
|
432 dump_node_to_file (initial_node, user_output_filename, dump_subnodes); |
|
433 else |
|
434 begin_info_session (initial_node); |
|
435 |
|
436 exit (0); |
|
437 } |
|
438 |
250
|
439 static char *program_name = "info"; |
171
|
440 |
|
441 static void |
|
442 remember_info_program_name (fullpath) |
|
443 char *fullpath; |
|
444 { |
|
445 char *filename; |
|
446 |
|
447 filename = filename_non_directory (fullpath); |
|
448 program_name = savestring (filename); |
|
449 } |
|
450 |
|
451 /* Produce a very brief descripton of the available options and exit with |
|
452 an error. */ |
|
453 static void |
|
454 usage () |
|
455 { |
|
456 fprintf (stderr,"%s\n%s\n%s\n%s\n%s\n", |
|
457 "Usage: info [-d dir-path] [-f info-file] [-o output-file] [-n node-name]...", |
|
458 " [--directory dir-path] [--file info-file] [--node node-name]...", |
|
459 " [--help] [--output output-file] [--subnodes] [--version]", |
|
460 " [--dribble dribble-file] [--restore from-file]", |
|
461 " [menu-selection ...]"); |
|
462 exit (1); |
|
463 } |
|
464 |
|
465 /* Produce a scaled down description of the available options to Info. */ |
|
466 static void |
|
467 info_short_help () |
|
468 { |
|
469 printf ("%s", "\ |
|
470 Here is a quick description of Info's options. For a more complete\n\ |
|
471 description of how to use Info, type `info info options'.\n\ |
|
472 \n\ |
|
473 --directory DIR Add DIR to INFOPATH.\n\ |
|
474 --file FILENAME Specify Info file to visit.\n\ |
|
475 --node NODENAME Specify nodes in first visited Info file.\n\ |
|
476 --output FILENAME Output selected nodes to FILENAME.\n\ |
|
477 --dribble FILENAME Remember user keystrokes in FILENAME.\n\ |
|
478 --restore FILENAME Read initial keystrokes from FILENAME.\n\ |
|
479 --subnodes Recursively output menu items.\n\ |
|
480 --help Get this help message.\n\ |
|
481 --version Display Info's version information.\n\ |
|
482 \n\ |
|
483 Remaining arguments to Info are treated as the names of menu\n\ |
|
484 items in the initial node visited. You can easily move to the\n\ |
|
485 node of your choice by specifying the menu names which describe\n\ |
|
486 the path to that node. For example, `info emacs buffers'.\n"); |
|
487 |
|
488 exit (0); |
|
489 } |