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