Changeset View
Changeset View
Standalone View
Standalone View
contrib/less/edit.c
Show All 20 Lines | |||||
extern char *every_first_cmd; | extern char *every_first_cmd; | ||||
extern int any_display; | extern int any_display; | ||||
extern int force_open; | extern int force_open; | ||||
extern int is_tty; | extern int is_tty; | ||||
extern int sigs; | extern int sigs; | ||||
extern IFILE curr_ifile; | extern IFILE curr_ifile; | ||||
extern IFILE old_ifile; | extern IFILE old_ifile; | ||||
extern struct scrpos initial_scrpos; | extern struct scrpos initial_scrpos; | ||||
extern void constant *ml_examine; | extern void * constant ml_examine; | ||||
#if SPACES_IN_FILENAMES | #if SPACES_IN_FILENAMES | ||||
extern char openquote; | extern char openquote; | ||||
extern char closequote; | extern char closequote; | ||||
#endif | #endif | ||||
#if LOGFILE | #if LOGFILE | ||||
extern int logfile; | extern int logfile; | ||||
extern int force_logfile; | extern int force_logfile; | ||||
Show All 12 Lines | |||||
/* | /* | ||||
* Textlist functions deal with a list of words separated by spaces. | * Textlist functions deal with a list of words separated by spaces. | ||||
* init_textlist sets up a textlist structure. | * init_textlist sets up a textlist structure. | ||||
* forw_textlist uses that structure to iterate thru the list of | * forw_textlist uses that structure to iterate thru the list of | ||||
* words, returning each one as a standard null-terminated string. | * words, returning each one as a standard null-terminated string. | ||||
* back_textlist does the same, but runs thru the list backwards. | * back_textlist does the same, but runs thru the list backwards. | ||||
*/ | */ | ||||
public void | public void | ||||
init_textlist(tlist, str) | init_textlist(struct textlist *tlist, char *str) | ||||
struct textlist *tlist; | |||||
char *str; | |||||
{ | { | ||||
char *s; | char *s; | ||||
#if SPACES_IN_FILENAMES | #if SPACES_IN_FILENAMES | ||||
int meta_quoted = 0; | int meta_quoted = 0; | ||||
int delim_quoted = 0; | int delim_quoted = 0; | ||||
char *esc = get_meta_escape(); | char *esc = get_meta_escape(); | ||||
int esclen = (int) strlen(esc); | int esclen = (int) strlen(esc); | ||||
#endif | #endif | ||||
Show All 25 Lines | |||||
#else | #else | ||||
if (*s == ' ') | if (*s == ' ') | ||||
*s = '\0'; | *s = '\0'; | ||||
#endif | #endif | ||||
} | } | ||||
} | } | ||||
public char * | public char * | ||||
forw_textlist(tlist, prev) | forw_textlist(struct textlist *tlist, char *prev) | ||||
struct textlist *tlist; | |||||
char *prev; | |||||
{ | { | ||||
char *s; | char *s; | ||||
/* | /* | ||||
* prev == NULL means return the first word in the list. | * prev == NULL means return the first word in the list. | ||||
* Otherwise, return the word after "prev". | * Otherwise, return the word after "prev". | ||||
*/ | */ | ||||
if (prev == NULL) | if (prev == NULL) | ||||
s = tlist->string; | s = tlist->string; | ||||
else | else | ||||
s = prev + strlen(prev); | s = prev + strlen(prev); | ||||
if (s >= tlist->endstring) | if (s >= tlist->endstring) | ||||
return (NULL); | return (NULL); | ||||
while (*s == '\0') | while (*s == '\0') | ||||
s++; | s++; | ||||
if (s >= tlist->endstring) | if (s >= tlist->endstring) | ||||
return (NULL); | return (NULL); | ||||
return (s); | return (s); | ||||
} | } | ||||
public char * | public char * | ||||
back_textlist(tlist, prev) | back_textlist(struct textlist *tlist, char *prev) | ||||
struct textlist *tlist; | |||||
char *prev; | |||||
{ | { | ||||
char *s; | char *s; | ||||
/* | /* | ||||
* prev == NULL means return the last word in the list. | * prev == NULL means return the last word in the list. | ||||
* Otherwise, return the word before "prev". | * Otherwise, return the word before "prev". | ||||
*/ | */ | ||||
if (prev == NULL) | if (prev == NULL) | ||||
Show All 10 Lines | while (s[-1] != '\0' && s > tlist->string) | ||||
s--; | s--; | ||||
return (s); | return (s); | ||||
} | } | ||||
/* | /* | ||||
* Close the current input file. | * Close the current input file. | ||||
*/ | */ | ||||
static void | static void | ||||
close_file() | close_file(void) | ||||
{ | { | ||||
struct scrpos scrpos; | struct scrpos scrpos; | ||||
if (curr_ifile == NULL_IFILE) | if (curr_ifile == NULL_IFILE) | ||||
return; | return; | ||||
/* | /* | ||||
* Save the current position so that we can return to | * Save the current position so that we can return to | ||||
Show All 27 Lines | |||||
} | } | ||||
/* | /* | ||||
* Edit a new file (given its name). | * Edit a new file (given its name). | ||||
* Filename == "-" means standard input. | * Filename == "-" means standard input. | ||||
* Filename == NULL means just close the current file. | * Filename == NULL means just close the current file. | ||||
*/ | */ | ||||
public int | public int | ||||
edit(filename) | edit(char *filename) | ||||
char *filename; | |||||
{ | { | ||||
if (filename == NULL) | if (filename == NULL) | ||||
return (edit_ifile(NULL_IFILE)); | return (edit_ifile(NULL_IFILE)); | ||||
return (edit_ifile(get_ifile(filename, curr_ifile))); | return (edit_ifile(get_ifile(filename, curr_ifile))); | ||||
} | } | ||||
/* | /* | ||||
* Edit a new file (given its IFILE). | * Edit a new file (given its IFILE). | ||||
* ifile == NULL means just close the current file. | * ifile == NULL means just close the current file. | ||||
*/ | */ | ||||
public int | public int | ||||
edit_ifile(ifile) | edit_ifile(IFILE ifile) | ||||
IFILE ifile; | |||||
{ | { | ||||
int f; | int f; | ||||
int answer; | int answer; | ||||
int no_display; | int no_display; | ||||
int chflags; | int chflags; | ||||
char *filename; | char *filename; | ||||
char *open_filename; | char *open_filename; | ||||
char *qopen_filename; | char *qopen_filename; | ||||
▲ Show 20 Lines • Show All 233 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
/* | /* | ||||
* Edit a space-separated list of files. | * Edit a space-separated list of files. | ||||
* For each filename in the list, enter it into the ifile list. | * For each filename in the list, enter it into the ifile list. | ||||
* Then edit the first one. | * Then edit the first one. | ||||
*/ | */ | ||||
public int | public int | ||||
edit_list(filelist) | edit_list(char *filelist) | ||||
char *filelist; | |||||
{ | { | ||||
IFILE save_ifile; | IFILE save_ifile; | ||||
char *good_filename; | char *good_filename; | ||||
char *filename; | char *filename; | ||||
char *gfilelist; | char *gfilelist; | ||||
char *gfilename; | char *gfilename; | ||||
struct textlist tl_files; | struct textlist tl_files; | ||||
struct textlist tl_gfiles; | struct textlist tl_gfiles; | ||||
Show All 40 Lines | edit_list(char *filelist) | ||||
reedit_ifile(save_ifile); | reedit_ifile(save_ifile); | ||||
return (edit(good_filename)); | return (edit(good_filename)); | ||||
} | } | ||||
/* | /* | ||||
* Edit the first file in the command line (ifile) list. | * Edit the first file in the command line (ifile) list. | ||||
*/ | */ | ||||
public int | public int | ||||
edit_first() | edit_first(void) | ||||
{ | { | ||||
curr_ifile = NULL_IFILE; | curr_ifile = NULL_IFILE; | ||||
return (edit_next(1)); | return (edit_next(1)); | ||||
} | } | ||||
/* | /* | ||||
* Edit the last file in the command line (ifile) list. | * Edit the last file in the command line (ifile) list. | ||||
*/ | */ | ||||
public int | public int | ||||
edit_last() | edit_last(void) | ||||
{ | { | ||||
curr_ifile = NULL_IFILE; | curr_ifile = NULL_IFILE; | ||||
return (edit_prev(1)); | return (edit_prev(1)); | ||||
} | } | ||||
/* | /* | ||||
* Edit the n-th next or previous file in the command line (ifile) list. | * Edit the n-th next or previous file in the command line (ifile) list. | ||||
*/ | */ | ||||
static int | static int | ||||
edit_istep(h, n, dir) | edit_istep(IFILE h, int n, int dir) | ||||
IFILE h; | |||||
int n; | |||||
int dir; | |||||
{ | { | ||||
IFILE next; | IFILE next; | ||||
/* | /* | ||||
* Skip n filenames, then try to edit each filename. | * Skip n filenames, then try to edit each filename. | ||||
*/ | */ | ||||
for (;;) | for (;;) | ||||
{ | { | ||||
Show All 22 Lines | edit_istep(IFILE h, int n, int dir) | ||||
} | } | ||||
/* | /* | ||||
* Found a file that we can edit. | * Found a file that we can edit. | ||||
*/ | */ | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
edit_inext(h, n) | edit_inext(IFILE h, int n) | ||||
IFILE h; | |||||
int n; | |||||
{ | { | ||||
return (edit_istep(h, n, +1)); | return (edit_istep(h, n, +1)); | ||||
} | } | ||||
public int | public int | ||||
edit_next(n) | edit_next(int n) | ||||
int n; | |||||
{ | { | ||||
return edit_istep(curr_ifile, n, +1); | return edit_istep(curr_ifile, n, +1); | ||||
} | } | ||||
static int | static int | ||||
edit_iprev(h, n) | edit_iprev(IFILE h, int n) | ||||
IFILE h; | |||||
int n; | |||||
{ | { | ||||
return (edit_istep(h, n, -1)); | return (edit_istep(h, n, -1)); | ||||
} | } | ||||
public int | public int | ||||
edit_prev(n) | edit_prev(int n) | ||||
int n; | |||||
{ | { | ||||
return edit_istep(curr_ifile, n, -1); | return edit_istep(curr_ifile, n, -1); | ||||
} | } | ||||
/* | /* | ||||
* Edit a specific file in the command line (ifile) list. | * Edit a specific file in the command line (ifile) list. | ||||
*/ | */ | ||||
public int | public int | ||||
edit_index(n) | edit_index(int n) | ||||
int n; | |||||
{ | { | ||||
IFILE h; | IFILE h; | ||||
h = NULL_IFILE; | h = NULL_IFILE; | ||||
do | do | ||||
{ | { | ||||
if ((h = next_ifile(h)) == NULL_IFILE) | if ((h = next_ifile(h)) == NULL_IFILE) | ||||
{ | { | ||||
/* | /* | ||||
* Reached end of the list without finding it. | * Reached end of the list without finding it. | ||||
*/ | */ | ||||
return (1); | return (1); | ||||
} | } | ||||
} while (get_index(h) != n); | } while (get_index(h) != n); | ||||
return (edit_ifile(h)); | return (edit_ifile(h)); | ||||
} | } | ||||
public IFILE | public IFILE | ||||
save_curr_ifile() | save_curr_ifile(void) | ||||
{ | { | ||||
if (curr_ifile != NULL_IFILE) | if (curr_ifile != NULL_IFILE) | ||||
hold_ifile(curr_ifile, 1); | hold_ifile(curr_ifile, 1); | ||||
return (curr_ifile); | return (curr_ifile); | ||||
} | } | ||||
public void | public void | ||||
unsave_ifile(save_ifile) | unsave_ifile(IFILE save_ifile) | ||||
IFILE save_ifile; | |||||
{ | { | ||||
if (save_ifile != NULL_IFILE) | if (save_ifile != NULL_IFILE) | ||||
hold_ifile(save_ifile, -1); | hold_ifile(save_ifile, -1); | ||||
} | } | ||||
/* | /* | ||||
* Reedit the ifile which was previously open. | * Reedit the ifile which was previously open. | ||||
*/ | */ | ||||
public void | public void | ||||
reedit_ifile(save_ifile) | reedit_ifile(IFILE save_ifile) | ||||
IFILE save_ifile; | |||||
{ | { | ||||
IFILE next; | IFILE next; | ||||
IFILE prev; | IFILE prev; | ||||
/* | /* | ||||
* Try to reopen the ifile. | * Try to reopen the ifile. | ||||
* Note that opening it may fail (maybe the file was removed), | * Note that opening it may fail (maybe the file was removed), | ||||
* in which case the ifile will be deleted from the list. | * in which case the ifile will be deleted from the list. | ||||
Show All 16 Lines | if (prev != NULL_IFILE && edit_iprev(prev, 0) == 0) | ||||
return; | return; | ||||
/* | /* | ||||
* If can't even open that, we're stuck. Just quit. | * If can't even open that, we're stuck. Just quit. | ||||
*/ | */ | ||||
quit(QUIT_ERROR); | quit(QUIT_ERROR); | ||||
} | } | ||||
public void | public void | ||||
reopen_curr_ifile() | reopen_curr_ifile(void) | ||||
{ | { | ||||
IFILE save_ifile = save_curr_ifile(); | IFILE save_ifile = save_curr_ifile(); | ||||
close_file(); | close_file(); | ||||
reedit_ifile(save_ifile); | reedit_ifile(save_ifile); | ||||
} | } | ||||
/* | /* | ||||
* Edit standard input. | * Edit standard input. | ||||
*/ | */ | ||||
public int | public int | ||||
edit_stdin() | edit_stdin(void) | ||||
{ | { | ||||
if (isatty(fd0)) | if (isatty(fd0)) | ||||
{ | { | ||||
error("Missing filename (\"less --help\" for help)", NULL_PARG); | error("Missing filename (\"less --help\" for help)", NULL_PARG); | ||||
quit(QUIT_OK); | quit(QUIT_OK); | ||||
} | } | ||||
return (edit("-")); | return (edit("-")); | ||||
} | } | ||||
/* | /* | ||||
* Copy a file directly to standard output. | * Copy a file directly to standard output. | ||||
* Used if standard output is not a tty. | * Used if standard output is not a tty. | ||||
*/ | */ | ||||
public void | public void | ||||
cat_file() | cat_file(void) | ||||
{ | { | ||||
register int c; | register int c; | ||||
while ((c = ch_forw_get()) != EOI) | while ((c = ch_forw_get()) != EOI) | ||||
putchr(c); | putchr(c); | ||||
flush(); | flush(); | ||||
} | } | ||||
#if LOGFILE | #if LOGFILE | ||||
/* | /* | ||||
* If the user asked for a log file and our input file | * If the user asked for a log file and our input file | ||||
* is standard input, create the log file. | * is standard input, create the log file. | ||||
* We take care not to blindly overwrite an existing file. | * We take care not to blindly overwrite an existing file. | ||||
*/ | */ | ||||
public void | public void | ||||
use_logfile(filename) | use_logfile(char *filename) | ||||
char *filename; | |||||
{ | { | ||||
register int exists; | register int exists; | ||||
register int answer; | register int answer; | ||||
PARG parg; | PARG parg; | ||||
if (ch_getflags() & CH_CANSEEK) | if (ch_getflags() & CH_CANSEEK) | ||||
/* | /* | ||||
* Can't currently use a log file on a file that can seek. | * Can't currently use a log file on a file that can seek. | ||||
▲ Show 20 Lines • Show All 83 Lines • Show Last 20 Lines |