* Copyright (c) 1988 Mark Nudleman
* Copyright (c) 1988, 1993
* Regents of the University of California. All rights reserved.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1988 Mark Nudleman.\n\
@(#) Copyright (c) 1988, 1993
Regents of the University of California. All rights reserved.\n";
static char sccsid
[] = "@(#)main.c 5.14 (Berkeley) %G%";
* Entry point, initialization, miscellaneous routines.
char *current_file
, *previous_file
, *current_name
, *next_name
;
* Filename "-" means standard input.
* No filename means the "current" file, from the command line.
off_t initial_pos
, position();
char *rindex(), *strerror(), *save(), *bad_file();
initial_pos
= NULL_POSITION
;
if (filename
== NULL
|| *filename
== '\0') {
error("No current file");
filename
= save(av
[curr_ac
]);
else if (strcmp(filename
, "#") == 0) {
if (*previous_file
== '\0') {
error("no previous file");
filename
= save(previous_file
);
filename
= save(filename
);
/* use standard input. */
if (!strcmp(filename
, "-")) {
error("Can view standard input only once");
else if ((m
= bad_file(filename
, message
, sizeof(message
))) != NULL
) {
else if ((f
= open(filename
, O_RDONLY
, 0)) < 0) {
(void)sprintf(message
, "%s: %s", filename
, strerror(errno
));
* 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.
error("Can't take input from a terminal");
* We are now committed to using the new file.
* Close the current input file and set up to use the new one.
if (previous_file
!= NULL
)
previous_file
= current_file
;
prev_pos
= position(TOP
);
current_name
= (p
= rindex(filename
, '/')) ? p
+ 1 : filename
;
next_name
= av
[curr_ac
+ 1];
int no_display
= !any_display
;
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.
* Indicate there is nothing displayed yet.
if (initial_pos
!= NULL_POSITION
)
* Edit the next file in the command line list.
if (quit_at_eof
|| position(TOP
) == NULL_POSITION
)
error("No (N-th) next file");
(void)edit(av
[curr_ac
+= n
]);
* Edit the previous file in the command line list.
error("No (N-th) previous file");
(void)edit(av
[curr_ac
-= n
]);
* copy a file directly to standard output; used if stdout is not a tty.
* the only processing is to squeeze multiple blank input lines.
while ((c
= ch_forw_get()) != EOI
)
else while ((c
= ch_forw_get()) != EOI
)
char *envargv
[2], *getenv();
* Process command line arguments and MORE environment arguments.
* Command line arguments override environment arguments.
if (envargv
[1] = getenv("MORE")) {
(void)option(envargc
, envargv
);
argcnt
= option(argc
, argv
);
* Set up list of files to be examined.
* Just copy the input file(s) to output.
(void)edit((char *)NULL
);
} while (++curr_ac
< ac
);
/* select the first file to examine. */
* A -t option was given; edit the file selected by the
* "tags" search, and search for the proper line in the file.
if (!tagfile
|| !edit(tagfile
) || tagsearch())
(void)edit("-"); /* Standard input */
* Try all the files named as command arguments.
* We are simply looking for one which can be
(void)edit((char *)NULL
);
} while (file
< 0 && ++curr_ac
< ac
);
* Copy a string to a "safe" place
* (that is, to a buffer allocated by malloc).
char *p
, *strcpy(), *malloc();
p
= malloc((u_int
)strlen(s
)+1);
error("cannot allocate memory");
* Put cursor at bottom left corner, clear the line,
* reset the terminal modes, and exit.