2 * Copyright (C) 1984-2011 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
22 extern char *every_first_cmd;
23 extern int any_display;
24 extern int force_open;
27 extern IFILE curr_ifile;
28 extern IFILE old_ifile;
29 extern struct scrpos initial_scrpos;
30 extern void constant *ml_examine;
31 #if SPACES_IN_FILENAMES
32 extern char openquote;
33 extern char closequote;
38 extern int force_logfile;
39 extern char *namelogfile;
43 public dev_t curr_dev;
44 public ino_t curr_ino;
47 char *curr_altfilename = NULL;
48 static void *curr_altpipe;
52 * Textlist functions deal with a list of words separated by spaces.
53 * init_textlist sets up a textlist structure.
54 * forw_textlist uses that structure to iterate thru the list of
55 * words, returning each one as a standard null-terminated string.
56 * back_textlist does the same, but runs thru the list backwards.
59 init_textlist(tlist, str)
60 struct textlist *tlist;
64 #if SPACES_IN_FILENAMES
67 char *esc = get_meta_escape();
68 int esclen = strlen(esc);
71 tlist->string = skipsp(str);
72 tlist->endstring = tlist->string + strlen(tlist->string);
73 for (s = str; s < tlist->endstring; s++)
75 #if SPACES_IN_FILENAMES
79 } else if (esclen > 0 && s + esclen < tlist->endstring &&
80 strncmp(s, esc, esclen) == 0)
84 } else if (delim_quoted)
88 } else /* (!delim_quoted) */
103 forw_textlist(tlist, prev)
104 struct textlist *tlist;
110 * prev == NULL means return the first word in the list.
111 * Otherwise, return the word after "prev".
116 s = prev + strlen(prev);
117 if (s >= tlist->endstring)
121 if (s >= tlist->endstring)
127 back_textlist(tlist, prev)
128 struct textlist *tlist;
134 * prev == NULL means return the last word in the list.
135 * Otherwise, return the word before "prev".
138 s = tlist->endstring;
139 else if (prev <= tlist->string)
145 if (s <= tlist->string)
147 while (s[-1] != '\0' && s > tlist->string)
153 * Close the current input file.
158 struct scrpos scrpos;
160 if (curr_ifile == NULL_IFILE)
164 * Save the current position so that we can return to
165 * the same position if we edit this file again.
168 if (scrpos.pos != NULL_POSITION)
170 store_pos(curr_ifile, &scrpos);
174 * Close the file descriptor, unless it is a pipe.
178 * If we opened a file using an alternate name,
179 * do special stuff to close it.
181 if (curr_altfilename != NULL)
183 close_altfile(curr_altfilename, get_filename(curr_ifile),
185 free(curr_altfilename);
186 curr_altfilename = NULL;
188 curr_ifile = NULL_IFILE;
190 curr_ino = curr_dev = 0;
195 * Edit a new file (given its name).
196 * Filename == "-" means standard input.
197 * Filename == NULL means just close the current file.
203 if (filename == NULL)
204 return (edit_ifile(NULL_IFILE));
205 return (edit_ifile(get_ifile(filename, curr_ifile)));
209 * Edit a new file (given its IFILE).
210 * ifile == NULL means just close the current file.
222 char *qopen_filename;
225 IFILE was_curr_ifile;
228 if (ifile == curr_ifile)
231 * Already have the correct file open.
237 * We must close the currently open file now.
238 * This is necessary to make the open_altfile/close_altfile pairs
239 * nest properly (or rather to avoid nesting at all).
240 * {{ Some stupid implementations of popen() mess up if you do:
241 * fA = popen("A"); fB = popen("B"); pclose(fA); pclose(fB); }}
246 was_curr_ifile = save_curr_ifile();
247 if (curr_ifile != NULL_IFILE)
249 chflags = ch_getflags();
251 if ((chflags & CH_HELPFILE) && held_ifile(was_curr_ifile) <= 1)
254 * Don't keep the help file in the ifile list.
256 del_ifile(was_curr_ifile);
257 was_curr_ifile = old_ifile;
261 if (ifile == NULL_IFILE)
264 * No new file to open.
265 * (Don't set old_ifile, because if you call edit_ifile(NULL),
266 * you're supposed to have saved curr_ifile yourself,
267 * and you'll restore it if necessary.)
269 unsave_ifile(was_curr_ifile);
273 filename = save(get_filename(ifile));
275 * See if LESSOPEN specifies an "alternate" file to open.
278 alt_filename = open_altfile(filename, &f, &alt_pipe);
279 open_filename = (alt_filename != NULL) ? alt_filename : filename;
280 qopen_filename = shell_unquote(open_filename);
283 if (alt_pipe != NULL)
286 * The alternate "file" is actually a pipe.
287 * f has already been set to the file descriptor of the pipe
288 * in the call to open_altfile above.
289 * Keep the file descriptor open because it was opened
290 * via popen(), and pclose() wants to close it.
292 chflags |= CH_POPENED;
293 } else if (strcmp(open_filename, "-") == 0)
296 * Use standard input.
297 * Keep the file descriptor open because we can't reopen it.
300 chflags |= CH_KEEPOPEN;
302 * Must switch stdin to BINARY mode.
305 #if MSDOS_COMPILER==DJGPPC
307 * Setting stdin to binary by default causes
308 * Ctrl-C to not raise SIGINT. We must undo
311 __djgpp_set_ctrl_c(1);
313 } else if (strcmp(open_filename, FAKE_HELPFILE) == 0)
316 chflags |= CH_HELPFILE;
317 } else if ((parg.p_string = bad_file(open_filename)) != NULL)
320 * It looks like a bad file. Don't try to open it.
325 if (alt_filename != NULL)
327 close_altfile(alt_filename, filename, alt_pipe);
331 free(qopen_filename);
334 * Re-open the current file.
336 if (was_curr_ifile == ifile)
339 * Whoops. The "current" ifile is the one we just deleted.
344 reedit_ifile(was_curr_ifile);
346 } else if ((f = open(qopen_filename, OPEN_READ)) < 0)
349 * Got an error trying to open it.
351 parg.p_string = errno_message(filename);
357 chflags |= CH_CANSEEK;
358 if (!force_open && !opened(ifile) && bin_file(f))
361 * Looks like a binary file.
362 * Ask user if we should proceed.
364 parg.p_string = filename;
365 answer = query("\"%s\" may be a binary file. See it anyway? ",
367 if (answer != 'y' && answer != 'Y')
377 * Get the saved position for the file.
379 if (was_curr_ifile != NULL_IFILE)
381 old_ifile = was_curr_ifile;
382 unsave_ifile(was_curr_ifile);
385 curr_altfilename = alt_filename;
386 curr_altpipe = alt_pipe;
387 set_open(curr_ifile); /* File has been opened */
388 get_pos(curr_ifile, &initial_scrpos);
392 if (!(chflags & CH_HELPFILE))
395 if (namelogfile != NULL && is_tty)
396 use_logfile(namelogfile);
399 /* Remember the i-number and device of the opened file. */
402 int r = stat(qopen_filename, &statbuf);
405 curr_ino = statbuf.st_ino;
406 curr_dev = statbuf.st_dev;
410 if (every_first_cmd != NULL)
411 ungetsc(every_first_cmd);
414 free(qopen_filename);
415 no_display = !any_display;
422 * Output is to a real tty.
426 * Indicate there is nothing displayed yet.
433 cmd_addhist(ml_examine, filename);
434 if (no_display && errmsgs > 0)
437 * We displayed some messages on error output
438 * (file descriptor 2; see error() function).
439 * Before erasing the screen contents,
440 * display the file name and wait for a keystroke.
442 parg.p_string = filename;
451 * Edit a space-separated list of files.
452 * For each filename in the list, enter it into the ifile list.
453 * Then edit the first one.
464 struct textlist tl_files;
465 struct textlist tl_gfiles;
467 save_ifile = save_curr_ifile();
468 good_filename = NULL;
471 * Run thru each filename in the list.
472 * Try to glob the filename.
473 * If it doesn't expand, just try to open the filename.
474 * If it does expand, try to open each name in that list.
476 init_textlist(&tl_files, filelist);
478 while ((filename = forw_textlist(&tl_files, filename)) != NULL)
480 gfilelist = lglob(filename);
481 init_textlist(&tl_gfiles, gfilelist);
483 while ((gfilename = forw_textlist(&tl_gfiles, gfilename)) != NULL)
485 if (edit(gfilename) == 0 && good_filename == NULL)
486 good_filename = get_filename(curr_ifile);
491 * Edit the first valid filename in the list.
493 if (good_filename == NULL)
495 unsave_ifile(save_ifile);
498 if (get_ifile(good_filename, curr_ifile) == curr_ifile)
501 * Trying to edit the current file; don't reopen it.
503 unsave_ifile(save_ifile);
506 reedit_ifile(save_ifile);
507 return (edit(good_filename));
511 * Edit the first file in the command line (ifile) list.
516 curr_ifile = NULL_IFILE;
517 return (edit_next(1));
521 * Edit the last file in the command line (ifile) list.
526 curr_ifile = NULL_IFILE;
527 return (edit_prev(1));
532 * Edit the n-th next or previous file in the command line (ifile) list.
535 edit_istep(h, n, dir)
543 * Skip n filenames, then try to edit each filename.
547 next = (dir > 0) ? next_ifile(h) : prev_ifile(h);
550 if (edit_ifile(h) == 0)
553 if (next == NULL_IFILE)
556 * Reached end of the ifile list.
563 * Interrupt breaks out, if we're in a long
564 * list of files that can't be opened.
571 * Found a file that we can edit.
581 return (edit_istep(h, n, +1));
588 return edit_istep(curr_ifile, n, +1);
596 return (edit_istep(h, n, -1));
603 return edit_istep(curr_ifile, n, -1);
607 * Edit a specific file in the command line (ifile) list.
618 if ((h = next_ifile(h)) == NULL_IFILE)
621 * Reached end of the list without finding it.
625 } while (get_index(h) != n);
627 return (edit_ifile(h));
633 if (curr_ifile != NULL_IFILE)
634 hold_ifile(curr_ifile, 1);
639 unsave_ifile(save_ifile)
642 if (save_ifile != NULL_IFILE)
643 hold_ifile(save_ifile, -1);
647 * Reedit the ifile which was previously open.
650 reedit_ifile(save_ifile)
657 * Try to reopen the ifile.
658 * Note that opening it may fail (maybe the file was removed),
659 * in which case the ifile will be deleted from the list.
660 * So save the next and prev ifiles first.
662 unsave_ifile(save_ifile);
663 next = next_ifile(save_ifile);
664 prev = prev_ifile(save_ifile);
665 if (edit_ifile(save_ifile) == 0)
668 * If can't reopen it, open the next input file in the list.
670 if (next != NULL_IFILE && edit_inext(next, 0) == 0)
673 * If can't open THAT one, open the previous input file in the list.
675 if (prev != NULL_IFILE && edit_iprev(prev, 0) == 0)
678 * If can't even open that, we're stuck. Just quit.
686 IFILE save_ifile = save_curr_ifile();
688 reedit_ifile(save_ifile);
692 * Edit standard input.
699 error("Missing filename (\"less --help\" for help)", NULL_PARG);
706 * Copy a file directly to standard output.
707 * Used if standard output is not a tty.
714 while ((c = ch_forw_get()) != EOI)
722 * If the user asked for a log file and our input file
723 * is standard input, create the log file.
724 * We take care not to blindly overwrite an existing file.
727 use_logfile(filename)
734 if (ch_getflags() & CH_CANSEEK)
736 * Can't currently use a log file on a file that can seek.
741 * {{ We could use access() here. }}
743 filename = shell_unquote(filename);
744 exists = open(filename, OPEN_READ);
746 exists = (exists >= 0);
749 * Decide whether to overwrite the log file or append to it.
750 * If it doesn't exist we "overwrite" it.
752 if (!exists || force_logfile)
755 * Overwrite (or create) the log file.
761 * Ask user what to do.
763 parg.p_string = filename;
764 answer = query("Warning: \"%s\" exists; Overwrite, Append or Don't log? ", &parg);
772 * Overwrite: create the file.
774 logfile = creat(filename, 0644);
778 * Append: open the file and seek to the end.
780 logfile = open(filename, OPEN_APPEND);
781 if (lseek(logfile, (off_t)0, SEEK_END) == BAD_LSEEK)
800 answer = query("Overwrite, Append, or Don't log? (Type \"O\", \"A\", \"D\" or \"q\") ", NULL_PARG);
807 * Error in opening logfile.
809 parg.p_string = filename;
810 error("Cannot write to \"%s\"", &parg);