edeb09bb795fb4d225824bab65dc8aecd71166a5
* Copyright (c) 1988 Mark Nudleman
* Copyright (c) 1988 Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1988 Mark Nudleman.\n\
@(#) Copyright (c) 1988 Regents of the University of California.\n\
static char sccsid
[] = "@(#)main.c 5.3 (Berkeley) %G%";
* Entry point, initialization, miscellaneous routines.
public char * every_first_cmd
;
public char *current_file
;
public char *previous_file
;
public POSITION prev_pos
;
public int force_logfile
= 0;
public char * namelogfile
= NULL
;
extern char * tagpattern
;
* Filename "-" means standard input.
* No filename means the "current" file, from the command line.
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
);
if (strcmp(filename
, "-") == 0)
error("Can view standard input only once");
} else if ((m
= bad_file(filename
, message
, sizeof(message
))) != NULL
)
} else if ((f
= open(filename
, 0)) < 0)
error(errno_message(filename
, message
, sizeof(message
)));
* 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");
if (f
== 0 && namelogfile
!= 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 (previous_file
!= NULL
)
previous_file
= current_file
;
prev_pos
= position(TOP
);
if (every_first_cmd
!= NULL
)
first_cmd
= every_first_cmd
;
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.
error("No (N-th) next file");
* Edit the previous file in the command line list.
error("No (N-th) previous file");
* Copy a file directly to standard output.
* Used if standard output is not a tty.
while ((c
= ch_forw_get()) != EOI
)
* If he asked for a log file and we have opened standard input,
* We take care not to blindly overwrite an existing file.
* {{ We could use access() here. }}
exists
= open(namelogfile
, 0);
if (exists
&& !force_logfile
)
static char w
[] = "WARNING: log file exists: ";
strtcpy(message
+sizeof(w
)-1, namelogfile
,
sizeof(message
)-sizeof(w
));
answer
= 'X'; /* Ask the user what to do */
answer
= 'O'; /* Create the log file */
logfile
= creat(namelogfile
, 0644);
logfile
= open(namelogfile
, 1);
if (lseek(logfile
, (off_t
)0, L_XTND
) < 0)
answer
= 0; /* Don't print an error message */
putstr("\n Overwrite, Append, or Don't log? ");
if (logfile
< 0 && answer
!= 0)
sprintf(message
, "Cannot write to \"%s\"",
* Process command line arguments and LESS environment arguments.
* Command line arguments override environment arguments.
scan_option(getenv("LESS"));
(argv
[0][0] == '-' || argv
[0][0] == '+') &&
editor
= getenv("EDITOR");
if (editor
== NULL
|| *editor
== '\0')
* Set up list of files to be examined.
* Just copy the input file(s) to output.
} while (++curr_ac
< ac
);
* Select the first file to examine.
* Verify that no filenames were also given.
* Edit the file selected by the "tags" search,
* and search for the proper line in the file.
error("No filenames allowed with -t option");
edit("-"); /* Standard input */
* Try all the files named as command arguments.
* We are simply looking for one which can be
} while (file
< 0 && ++curr_ac
< ac
);
* Copy a string, truncating to the specified length if necessary.
* Unlike strncpy(), the resulting string is guaranteed to be null-terminated.
* Copy a string to a "safe" place
* (that is, to a buffer allocated by calloc).
p
= calloc(strlen(s
)+1, sizeof(char));
error("cannot allocate memory");
* Put cursor at bottom left corner, clear the line,
* reset the terminal modes, and exit.