#define ISPIPE(fd) ((fd)==0)
extern char *every_first_cmd
;
extern struct scrpos initial_scrpos
;
extern int force_logfile
;
extern char *namelogfile
;
* Filename == "-" means standard input.
* Filename == NULL means just close the current file.
edit(filename
, just_looking
)
* Close the current file, but don't open a new one.
} else if (strcmp(filename
, "-") == 0)
} else if ((parg
.p_string
= bad_file(filename
)) != NULL
)
} else if ((f
= open(filename
, O_RDONLY
|O_BINARY
)) < 0)
} else if ((f
= open(filename
, 0)) < 0)
parg
.p_string
= errno_message(filename
);
} else if (!force_open
&& !just_looking
&& bin_file(f
))
parg
.p_string
= filename
;
answer
= query("\"%s\" may be a binary file. Continue? ",
if (answer
!= 'y' && answer
!= 'Y')
* Not really necessary to call this an error,
* but if the control terminal (for commands)
* and the input file (for data) are the same,
* we get weird results at best.
parg
.p_string
= "less -?";
parg
.p_string
= "less -\\?";
error("Cannot take input from a terminal (\"%s\" for help)",
if (f
>= 0 && ISPIPE(f
) && s
!= NULL
&& is_tty
)
* We are now committed to using the new file.
* Close the current input file and set up to use the new one.
if (curr_ifile
!= NULL_IFILE
)
* Save the current position so that we can return to
* the same position if we edit this file again.
if (scrpos
.pos
!= NULL_POSITION
)
store_pos(curr_ifile
, &scrpos
);
* Close the current file, unless it is a pipe.
* Get the saved position for that file.
curr_ifile
= get_ifile(filename
, curr_ifile
);
get_pos(curr_ifile
, &initial_scrpos
);
if (every_first_cmd
!= NULL
)
ungetsc(every_first_cmd
);
no_display
= !any_display
;
* Output is to a real tty.
* Indicate there is nothing displayed yet.
if (no_display
&& errmsgs
> 0)
* We displayed some messages on error output
* (file descriptor 2; see error() function).
* Before erasing the screen contents,
* display the file name and wait for a keystroke.
parg
.p_string
= filename
;
* Edit a space-separated list of files.
* For each filename in the list, enter it into the ifile list.
* Then edit the first one.
* good_filename keeps track of the first valid filename.
save_curr_ifile
= curr_ifile
;
while ((s
= skipsp(s
)) < es
)
* Get the next filename and null terminate it.
while (*s
!= ' ' && *s
!= '\0')
* This enters it into the command line list (if it is good).
* If it is the first good file we've seen, remember it.
* {{ A little weirdness here: if any of the filenames
* are already in the list, subsequent ones get
* entered after the position where that one already
* was, instead of at the end. }}
if (edit(filename
, 1) == 0 && good_filename
== NULL
)
good_filename
= filename
;
* Edit the first valid filename in the list.
if (good_filename
!= NULL
)
curr_ifile
= save_curr_ifile
;
(void) edit(good_filename
, 0);
* Edit the first file in the command line (ifile) list.
* Edit the last file in the command line (ifile) list.
* Edit the next file in the command line (ifile) list.
while (--n
>= 0 || edit(get_filename(h
), 0))
if ((h
= next_ifile(h
)) == NULL_IFILE
)
* Reached end of the ifile list.
* Found a file that we can edit.
* Edit the previous file in the command line list.
while (--n
>= 0 || edit(get_filename(h
), 0))
if ((h
= prev_ifile(h
)) == NULL_IFILE
)
* Reached beginning of the ifile list.
* Found a file that we can edit.
* Edit a specific file in the command line (ifile) list.
if ((h
= next_ifile(h
)) == NULL_IFILE
)
* Reached end of the list without finding it.
} while (get_index(h
) != n
);
return (edit(get_filename(h
), 0));
* Copy a file directly to standard output.
* Used if standard output is not a tty.
while ((c
= ch_forw_get()) != EOI
)
* If the user asked for a log file and our input file
* is standard input, create the log file.
* We take care not to blindly overwrite an existing file.
* {{ We could use access() here. }}
exists
= open(filename
, 0);
* Decide whether to overwrite the log file or append to it.
* (If it doesn't exist we "overwrite" it.
if (!exists
|| force_logfile
)
* Overwrite (or create) the log file.
parg
.p_string
= filename
;
answer
= query("Warning: \"%s\" exists; Overwrite, Append or Don't log? ", &parg
);
* Overwrite: create the file.
logfile
= creat(filename
, 0644);
* Append: open the file and seek to the end.
logfile
= open(filename
, O_APPEND
|O_WRONLY
);
logfile
= open(filename
, 1);
if (lseek(logfile
, (offset_t
)0, 2) == BAD_LSEEK
)
answer
= query("Overwrite, Append, or Don't log? ", NULL_PARG
);
* Error in opening logfile.
parg
.p_string
= filename
;
error("Cannot write to \"%s\"", &parg
);