2000-08-06 21:13:45 +00:00
|
|
|
/* $Id$ */
|
2000-06-19 04:22:15 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* files.c *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 1999 Chris Allegretta *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 1, or (at your option) *
|
|
|
|
* any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2000-11-05 17:54:41 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2000-06-19 04:22:15 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "nano.h"
|
2000-06-21 03:00:43 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
#ifndef NANO_SMALL
|
|
|
|
#include <libintl.h>
|
|
|
|
#define _(string) gettext(string)
|
|
|
|
#else
|
|
|
|
#define _(string) (string)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Load file into edit buffer - takes data from file struct */
|
|
|
|
void load_file(void)
|
|
|
|
{
|
|
|
|
current = fileage;
|
|
|
|
wmove(edit, current_y, current_x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* What happens when there is no file to open? aiee! */
|
|
|
|
void new_file(void)
|
|
|
|
{
|
|
|
|
fileage = nmalloc(sizeof(filestruct));
|
|
|
|
fileage->data = nmalloc(1);
|
|
|
|
strcpy(fileage->data, "");
|
|
|
|
fileage->prev = NULL;
|
|
|
|
fileage->next = NULL;
|
|
|
|
fileage->lineno = 1;
|
|
|
|
filebot = fileage;
|
|
|
|
edittop = fileage;
|
|
|
|
editbot = fileage;
|
|
|
|
current = fileage;
|
|
|
|
totlines = 1;
|
|
|
|
UNSET(VIEW_MODE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int read_byte(int fd, char *filename, char *input)
|
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
|
|
|
static int index = 0;
|
|
|
|
static int size = 0;
|
|
|
|
|
|
|
|
if (index == size) {
|
|
|
|
index = 0;
|
|
|
|
size = read(fd, buf, BUFSIZ);
|
|
|
|
if (size == -1) {
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
resetty();
|
|
|
|
endwin();
|
|
|
|
perror(filename);
|
|
|
|
}
|
|
|
|
if (!size)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*input = buf[index++];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
filestruct *read_line(char *buf, filestruct * prev, int *line1ins)
|
|
|
|
{
|
|
|
|
filestruct *fileptr;
|
|
|
|
|
|
|
|
fileptr = nmalloc(sizeof(filestruct));
|
|
|
|
fileptr->data = nmalloc(strlen(buf) + 2);
|
|
|
|
strcpy(fileptr->data, buf);
|
|
|
|
|
|
|
|
if (*line1ins) {
|
|
|
|
/* Special case, insert with cursor on 1st line. */
|
|
|
|
fileptr->prev = NULL;
|
|
|
|
fileptr->next = fileage;
|
|
|
|
fileptr->lineno = 1;
|
|
|
|
*line1ins = 0;
|
|
|
|
/* If we're inserting into the first line of the file, then
|
|
|
|
we want to make sure that our edit buffer stays on the
|
|
|
|
first line (and that fileage stays up to date!) */
|
|
|
|
fileage = fileptr;
|
|
|
|
edittop = fileptr;
|
|
|
|
} else if (fileage == NULL) {
|
|
|
|
fileage = fileptr;
|
|
|
|
fileage->lineno = 1;
|
|
|
|
fileage->next = fileage->prev = NULL;
|
|
|
|
fileptr = filebot = fileage;
|
|
|
|
} else if (prev) {
|
|
|
|
fileptr->prev = prev;
|
|
|
|
fileptr->next = NULL;
|
|
|
|
fileptr->lineno = prev->lineno + 1;
|
|
|
|
prev->next = fileptr;
|
|
|
|
} else {
|
|
|
|
die(_("read_line: not on first line and prev is NULL"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int read_file(int fd, char *filename)
|
|
|
|
{
|
2000-07-08 03:57:16 +00:00
|
|
|
long size, num_lines = 0, linetemp = 0;
|
2000-06-19 04:22:15 +00:00
|
|
|
char input[2]; /* buffer */
|
|
|
|
char *buf;
|
|
|
|
long i = 0, bufx = 128;
|
|
|
|
filestruct *fileptr = current, *tmp = NULL;
|
|
|
|
int line1ins = 0;
|
|
|
|
|
|
|
|
buf = nmalloc(bufx);
|
2000-07-19 01:16:18 +00:00
|
|
|
buf[0] = '\0';
|
2000-06-19 04:22:15 +00:00
|
|
|
|
|
|
|
if (fileptr != NULL && fileptr->prev != NULL) {
|
|
|
|
fileptr = fileptr->prev;
|
|
|
|
tmp = fileptr;
|
|
|
|
} else if (fileptr != NULL && fileptr->prev == NULL) {
|
|
|
|
tmp = fileage;
|
|
|
|
current = fileage;
|
|
|
|
line1ins = 1;
|
|
|
|
}
|
|
|
|
input[1] = 0;
|
|
|
|
/* Read the entire file into file struct */
|
|
|
|
while ((size = read_byte(fd, filename, input)) > 0) {
|
|
|
|
linetemp = 0;
|
|
|
|
if (input[0] == '\n') {
|
|
|
|
fileptr = read_line(buf, fileptr, &line1ins);
|
2000-07-08 03:57:16 +00:00
|
|
|
num_lines++;
|
2000-06-19 04:22:15 +00:00
|
|
|
buf[0] = 0;
|
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
/* Now we allocate a bigger buffer 128 characters at a time.
|
|
|
|
If we allocate a lot of space for one line, we may indeed
|
|
|
|
have to use a buffer this big later on, so we don't
|
|
|
|
decrease it at all. We do free it at the end though. */
|
|
|
|
|
|
|
|
if (i >= bufx - 1) {
|
|
|
|
buf = nrealloc(buf, bufx + 128);
|
|
|
|
bufx += 128;
|
|
|
|
}
|
|
|
|
buf[i] = input[0];
|
|
|
|
buf[i + 1] = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
totsize += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we not get a newline but still have stuff to do? */
|
|
|
|
if (buf[0]) {
|
|
|
|
fileptr = read_line(buf, fileptr, &line1ins);
|
2000-07-08 03:57:16 +00:00
|
|
|
num_lines++;
|
2000-06-19 04:22:15 +00:00
|
|
|
buf[0] = 0;
|
|
|
|
}
|
|
|
|
/* Did we even GET a file? */
|
|
|
|
if (totsize == 0) {
|
|
|
|
new_file();
|
2000-07-08 03:57:16 +00:00
|
|
|
statusbar(_("Read %d lines"), num_lines);
|
2000-06-19 04:22:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2000-07-04 22:15:39 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
if (current != NULL) {
|
|
|
|
fileptr->next = current;
|
|
|
|
current->prev = fileptr;
|
|
|
|
renumber(current);
|
|
|
|
current_x = 0;
|
|
|
|
placewewant = 0;
|
|
|
|
} else if (fileptr->next == NULL) {
|
|
|
|
filebot = fileptr;
|
2000-07-04 22:15:39 +00:00
|
|
|
new_magicline();
|
|
|
|
|
|
|
|
/* Update the edit buffer */
|
2000-06-19 04:22:15 +00:00
|
|
|
load_file();
|
|
|
|
}
|
2000-07-08 03:57:16 +00:00
|
|
|
statusbar(_("Read %d lines"), num_lines);
|
|
|
|
totlines += num_lines;
|
2000-06-19 04:22:15 +00:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file (and decide if it exists) */
|
|
|
|
int open_file(char *filename, int insert, int quiet)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat fileinfo;
|
|
|
|
|
|
|
|
if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) {
|
|
|
|
if (insert) {
|
|
|
|
if (!quiet)
|
|
|
|
statusbar(_("\"%s\" not found"), filename);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
/* We have a new file */
|
|
|
|
statusbar(_("New File"));
|
|
|
|
new_file();
|
|
|
|
}
|
|
|
|
} else if ((fd = open(filename, O_RDONLY)) == -1) {
|
|
|
|
if (!quiet)
|
|
|
|
statusbar("%s: %s", strerror(errno), filename);
|
|
|
|
return -1;
|
|
|
|
} else { /* File is A-OK */
|
|
|
|
if (S_ISDIR(fileinfo.st_mode)) {
|
|
|
|
statusbar(_("File \"%s\" is a directory"), filename);
|
2000-09-16 05:25:06 +00:00
|
|
|
if (!insert)
|
|
|
|
new_file();
|
2000-06-19 04:22:15 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!quiet)
|
|
|
|
statusbar(_("Reading File"));
|
|
|
|
read_file(fd, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_insertfile(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
wrap_reset();
|
2000-11-06 02:57:22 +00:00
|
|
|
i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, "",
|
2000-06-19 04:22:15 +00:00
|
|
|
_("File to insert [from ./] "));
|
|
|
|
if (i != -1) {
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "filename is %s", answer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
i = open_file(answer, 1, 0);
|
|
|
|
|
|
|
|
dump_buffer(fileage);
|
|
|
|
set_modified();
|
|
|
|
|
|
|
|
/* Here we want to rebuild the edit window */
|
2000-07-04 02:35:19 +00:00
|
|
|
fix_editbot();
|
2000-06-19 04:22:15 +00:00
|
|
|
|
|
|
|
/* If we've gone off the bottom, recenter, otherwise just redraw */
|
2000-06-21 03:00:43 +00:00
|
|
|
if (current->lineno > editbot->lineno)
|
2000-07-29 04:33:38 +00:00
|
|
|
edit_update(current, CENTER);
|
2000-06-19 04:22:15 +00:00
|
|
|
else
|
|
|
|
edit_refresh();
|
|
|
|
|
|
|
|
UNSET(KEEP_CUTBUFFER);
|
|
|
|
display_main_list();
|
|
|
|
return i;
|
|
|
|
} else {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
UNSET(KEEP_CUTBUFFER);
|
|
|
|
display_main_list();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a file out. If tmp is nonzero, we set the umask to 0600,
|
|
|
|
* we don't set the global variable filename to it's name, and don't
|
|
|
|
* print out how many lines we wrote on the statusbar.
|
|
|
|
*
|
|
|
|
* Note that tmp is only set to 1 for storing temporary files internal
|
2000-07-14 01:20:12 +00:00
|
|
|
* to the editor, and is completely different from TEMP_OPT.
|
2000-06-19 04:22:15 +00:00
|
|
|
*/
|
|
|
|
int write_file(char *name, int tmp)
|
|
|
|
{
|
|
|
|
long size, lineswritten = 0;
|
|
|
|
char buf[PATH_MAX + 1];
|
|
|
|
filestruct *fileptr;
|
|
|
|
int fd, mask = 0;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (!strcmp(name, "")) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
titlebar();
|
|
|
|
fileptr = fileage;
|
|
|
|
|
|
|
|
|
2000-07-21 22:42:46 +00:00
|
|
|
/* Check to see if the file is a regular file and FOLLOW_SYMLINKS is
|
|
|
|
set. If so then don't do the delete and recreate code which would
|
|
|
|
cause unexpected behavior */
|
|
|
|
lstat(name, &st);
|
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
/* Open the file and truncate it. Trust the symlink. */
|
2000-07-21 22:42:46 +00:00
|
|
|
if ((ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(st.st_mode)) && !tmp) {
|
2000-07-24 21:52:17 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
|
|
|
|
S_IWOTH)) == -1) {
|
2000-07-14 02:44:02 +00:00
|
|
|
if (ISSET(TEMP_OPT)) {
|
|
|
|
UNSET(TEMP_OPT);
|
|
|
|
return do_writeout(1);
|
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Don't follow symlink. Create new file. */
|
|
|
|
else {
|
|
|
|
if (strlen(name) > (PATH_MAX - 7)) {
|
|
|
|
statusbar(_("Could not open file: Path length exceeded."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0x00, PATH_MAX + 1);
|
|
|
|
strcat(buf, name);
|
|
|
|
strcat(buf, ".XXXXXX");
|
|
|
|
if ((fd = mkstemp(buf)) == -1) {
|
2000-07-14 02:44:02 +00:00
|
|
|
if (ISSET(TEMP_OPT)) {
|
|
|
|
UNSET(TEMP_OPT);
|
|
|
|
return do_writeout(1);
|
2000-10-26 01:44:42 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dump_buffer(fileage);
|
|
|
|
while (fileptr != NULL && fileptr->next != NULL) {
|
2000-07-04 22:15:39 +00:00
|
|
|
/* Next line is so we discount the "magic line" */
|
2000-10-26 01:44:42 +00:00
|
|
|
if (filebot == fileptr && fileptr->data[0] == '\0')
|
|
|
|
break;
|
2000-07-04 22:15:39 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
size = write(fd, fileptr->data, strlen(fileptr->data));
|
|
|
|
if (size == -1) {
|
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, _("Wrote >%s\n"), fileptr->data);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
write(fd, "\n", 1);
|
|
|
|
|
|
|
|
fileptr = fileptr->next;
|
|
|
|
lineswritten++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileptr != NULL) {
|
|
|
|
size = write(fd, fileptr->data, strlen(fileptr->data));
|
|
|
|
if (size == -1) {
|
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
} else if (size > 0) {
|
|
|
|
size = write(fd, "\n", 1);
|
|
|
|
if (size == -1) {
|
|
|
|
statusbar(_("Could not open file for writing: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (close(fd) == -1) {
|
|
|
|
statusbar(_("Could not close %s: %s"), name, strerror(errno));
|
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ISSET(FOLLOW_SYMLINKS) || tmp) {
|
|
|
|
if (stat(name, &st) == -1) {
|
|
|
|
/* Use default umask as file permisions if file is a new file. */
|
|
|
|
mask = umask(0);
|
|
|
|
umask(mask);
|
|
|
|
|
|
|
|
if (tmp) /* We don't want anyone reading our temporary file! */
|
|
|
|
mask = 0600;
|
|
|
|
else
|
|
|
|
mask = 0666 & ~mask;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Use permissions from file we are overwriting. */
|
|
|
|
mask = st.st_mode;
|
|
|
|
if (unlink(name) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
statusbar(_("Could not open %s for writing: %s"),
|
|
|
|
name, strerror(errno));
|
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link(buf, name) != -1)
|
|
|
|
unlink(buf);
|
|
|
|
else if (errno != EPERM) {
|
|
|
|
statusbar(_("Could not open %s for writing: %s"),
|
|
|
|
name, strerror(errno));
|
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
} else if (rename(buf, name) == -1) { /* Try a rename?? */
|
|
|
|
statusbar(_("Could not open %s for writing: %s"),
|
|
|
|
name, strerror(errno));
|
|
|
|
unlink(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (chmod(name, mask) == -1) {
|
|
|
|
statusbar(_("Could not set permissions %o on %s: %s"),
|
|
|
|
mask, name, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (!tmp) {
|
|
|
|
strncpy(filename, name, 132);
|
|
|
|
statusbar(_("Wrote %d lines"), lineswritten);
|
|
|
|
}
|
|
|
|
UNSET(MODIFIED);
|
|
|
|
titlebar();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_writeout(int exiting)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
2000-11-02 14:53:46 +00:00
|
|
|
answer = mallocstrcpy(answer, filename);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2000-07-24 21:52:17 +00:00
|
|
|
if ((exiting) && (ISSET(TEMP_OPT))) {
|
2000-10-26 01:44:42 +00:00
|
|
|
if (filename[0]) {
|
2000-07-24 21:52:17 +00:00
|
|
|
i = write_file(answer, 0);
|
|
|
|
display_main_list();
|
|
|
|
return i;
|
2000-10-26 01:44:42 +00:00
|
|
|
} else {
|
2000-07-24 22:05:18 +00:00
|
|
|
UNSET(TEMP_OPT);
|
|
|
|
do_exit();
|
|
|
|
|
|
|
|
/* They cancelled, abort quit */
|
|
|
|
return -1;
|
2000-07-24 21:52:17 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2000-11-06 02:57:22 +00:00
|
|
|
i = statusq(1, writefile_list, WRITEFILE_LIST_LEN, answer,
|
2000-06-21 03:00:43 +00:00
|
|
|
_("File Name to write"));
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2000-06-21 03:00:43 +00:00
|
|
|
if (i != -1) {
|
2000-06-19 04:22:15 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, _("filename is %s"), answer);
|
|
|
|
#endif
|
2000-11-02 14:53:46 +00:00
|
|
|
if (strcmp(answer, filename)) {
|
2000-06-19 04:22:15 +00:00
|
|
|
struct stat st;
|
|
|
|
if (!stat(answer, &st)) {
|
|
|
|
i = do_yesno(0, 0, _("File exists, OVERWRITE ?"));
|
|
|
|
|
|
|
|
if (!i || (i == -1))
|
|
|
|
continue;
|
2000-06-21 03:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
i = write_file(answer, 0);
|
|
|
|
|
|
|
|
display_main_list();
|
|
|
|
return i;
|
2000-06-21 03:00:43 +00:00
|
|
|
} else {
|
2000-06-19 04:22:15 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
display_main_list();
|
|
|
|
return 0;
|
2000-06-21 03:00:43 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_writeout_void(void)
|
|
|
|
{
|
|
|
|
return do_writeout(0);
|
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These functions (username_tab_completion, cwd_tab_completion, and
|
|
|
|
* input_tab were taken from busybox 0.46 (cmdedit.c). Here is the notice
|
|
|
|
* from that file:
|
|
|
|
*
|
|
|
|
* Termios command line History and Editting, originally
|
|
|
|
* intended for NetBSD sh (ash)
|
|
|
|
* Copyright (c) 1999
|
|
|
|
* Main code: Adam Rogoyski <rogoyski@cs.utexas.edu>
|
|
|
|
* Etc: Dave Cinege <dcinege@psychosis.com>
|
|
|
|
* Majorly adjusted/re-written for busybox:
|
|
|
|
* Erik Andersen <andersee@debian.org>
|
|
|
|
*
|
|
|
|
* You may use this code as you wish, so long as the original author(s)
|
|
|
|
* are attributed in any redistributions of the source code.
|
|
|
|
* This code is 'as is' with no warranty.
|
|
|
|
* This code may safely be consumed by a BSD or GPL license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char **username_tab_completion(char *buf, int *num_matches)
|
|
|
|
{
|
|
|
|
char **matches = (char **) NULL;
|
|
|
|
*num_matches = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\nin username_tab_completion\n");
|
|
|
|
#endif
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This was originally called exe_n_cwd_tab_completion, but we're not
|
|
|
|
worried about executables, only filenames :> */
|
|
|
|
|
|
|
|
char **cwd_tab_completion(char *buf, int *num_matches)
|
|
|
|
{
|
2000-11-06 04:04:15 +00:00
|
|
|
char *dirName, *tmp = NULL, *tmp2 = NULL;
|
2000-11-05 17:54:41 +00:00
|
|
|
char **matches = (char **) NULL;
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *next;
|
|
|
|
|
2000-11-06 04:04:15 +00:00
|
|
|
matches = nmalloc(1024);
|
2000-11-05 17:54:41 +00:00
|
|
|
|
|
|
|
/* Stick a wildcard onto the buf, for later use */
|
|
|
|
strcat(buf, "*");
|
|
|
|
|
|
|
|
/* Now wall the current directory */
|
|
|
|
/* if (!strcmp(filename, ""))
|
|
|
|
dirName = get_current_dir_name(); */
|
|
|
|
|
|
|
|
if (strcmp(buf, "") && strstr(buf, "/")) {
|
|
|
|
dirName = malloc(strlen(buf) + 1);
|
|
|
|
tmp = buf + strlen(buf);
|
|
|
|
while (*tmp != '/' && tmp != buf)
|
|
|
|
tmp--;
|
|
|
|
strncpy(dirName, buf, tmp - buf);
|
|
|
|
dirName[tmp - buf] = 0;
|
|
|
|
tmp++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if ((dirName = getcwd(NULL, 0)) == NULL)
|
|
|
|
return matches;
|
|
|
|
else
|
|
|
|
tmp = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\nDir = %s\n", dirName);
|
|
|
|
fprintf(stderr, "\nbuf = %s\n", buf);
|
|
|
|
fprintf(stderr, "\ntmp = %s\n", tmp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
dir = opendir(dirName);
|
|
|
|
if (!dir) {
|
|
|
|
/* Don't print an error, just shut up and return */
|
|
|
|
*num_matches = 0;
|
|
|
|
beep();
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
while ((next = readdir(dir)) != NULL) {
|
|
|
|
|
|
|
|
/* Some quick sanity checks */
|
|
|
|
if ((strcmp(next->d_name, "..") == 0)
|
|
|
|
|| (strcmp(next->d_name, ".") == 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\nComparing \'%s\'\n", next->d_name);
|
|
|
|
#endif
|
|
|
|
/* See if this matches */
|
|
|
|
if (check_wildcard_match(next->d_name, tmp) == TRUE) {
|
2000-11-06 04:04:15 +00:00
|
|
|
|
|
|
|
/* Cool, found a match. Add it to the list
|
|
|
|
* This makes a lot more sense to me (Chris) this way...
|
|
|
|
*/
|
|
|
|
tmp2 = NULL;
|
|
|
|
tmp2 = nmalloc(strlen(next->d_name) + 1);
|
|
|
|
strcpy(tmp2, next->d_name);
|
|
|
|
matches[*num_matches] = tmp2;
|
2000-11-05 17:54:41 +00:00
|
|
|
++*num_matches;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (matches);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function now return an int which refers to how much the
|
|
|
|
* statusbar (place) should be advanced, i.e. the new cursor pos.
|
|
|
|
*/
|
|
|
|
int input_tab(char *buf, int place, int lastWasTab)
|
|
|
|
{
|
|
|
|
/* Do TAB completion */
|
2000-11-05 21:54:23 +00:00
|
|
|
static int num_matches = 0, match_matches = 0;
|
2000-11-05 17:54:41 +00:00
|
|
|
static char **matches = (char **) NULL;
|
2000-11-05 21:54:23 +00:00
|
|
|
int pos = place, newplace = 0, i = 0, col = 0, editline = 0;
|
|
|
|
int longestname = 0;
|
2000-11-05 21:56:54 +00:00
|
|
|
char *foo;
|
2000-11-05 17:54:41 +00:00
|
|
|
|
|
|
|
if (lastWasTab == FALSE) {
|
|
|
|
char *tmp, *matchBuf;
|
|
|
|
|
|
|
|
/* For now, we will not bother with trying to distinguish
|
|
|
|
* whether the cursor is in/at a command extression -- we
|
|
|
|
* will always try all possible matches. If you don't like
|
|
|
|
* that then feel free to fix it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Make a local copy of the string -- up to the position of the
|
|
|
|
cursor */
|
2000-11-06 05:40:03 +00:00
|
|
|
matchBuf = (char *) calloc(strlen(buf) + 2, sizeof(char));
|
|
|
|
|
2000-11-05 17:54:41 +00:00
|
|
|
strncpy(matchBuf, buf, place);
|
|
|
|
tmp = matchBuf;
|
|
|
|
|
|
|
|
/* skip any leading white space */
|
|
|
|
while (*tmp && isspace(*tmp))
|
|
|
|
++tmp;
|
|
|
|
|
|
|
|
/* Free up any memory already allocated */
|
|
|
|
if (matches != NULL) {
|
|
|
|
free(matches);
|
|
|
|
matches = (char **) NULL;
|
2000-11-06 04:04:15 +00:00
|
|
|
num_matches = 0;
|
2000-11-05 17:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the word starts with `~' and there is no slash in the word,
|
|
|
|
* then try completing this word as a username. */
|
|
|
|
|
|
|
|
/* FIXME -- this check is broken!
|
|
|
|
if (*tmp == '~' && !strchr(tmp, '/'))
|
|
|
|
matches = username_tab_completion(tmp, &num_matches); */
|
|
|
|
|
|
|
|
/* Try to match everything in the current working directory that
|
|
|
|
* matches. */
|
|
|
|
if (!matches)
|
|
|
|
matches = cwd_tab_completion(tmp, &num_matches);
|
|
|
|
|
|
|
|
/* Don't leak memory */
|
|
|
|
free(matchBuf);
|
|
|
|
|
|
|
|
/* Did we find exactly one match? */
|
2000-11-06 05:40:03 +00:00
|
|
|
switch(num_matches) {
|
|
|
|
case 0:
|
2000-11-06 05:41:20 +00:00
|
|
|
blank_edit();
|
2000-11-06 05:40:03 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2000-11-05 17:54:41 +00:00
|
|
|
buf = nrealloc(buf, strlen(buf) + strlen(matches[0]) - pos + 1);
|
|
|
|
/* write out the matched command */
|
|
|
|
strncpy(buf + pos, matches[0] + pos,
|
|
|
|
strlen(matches[0]) - pos);
|
|
|
|
newplace += strlen(matches[0]) - pos;
|
2000-11-06 05:40:03 +00:00
|
|
|
break;
|
|
|
|
default:
|
2000-11-05 21:54:23 +00:00
|
|
|
/* Check to see if all matches share a beginning, and if so
|
|
|
|
tack it onto buf and then beep */
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
match_matches = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < num_matches; i++) {
|
|
|
|
if (matches[i][pos] == 0)
|
|
|
|
break;
|
|
|
|
else if (matches[i][pos] == matches[0][pos])
|
|
|
|
match_matches++;
|
|
|
|
}
|
|
|
|
if (match_matches == num_matches &&
|
2000-11-05 22:48:35 +00:00
|
|
|
(i == num_matches || matches[i] != 0)) {
|
2000-11-05 21:54:23 +00:00
|
|
|
/* All the matches have the same character at pos+1,
|
|
|
|
so paste it into buf... */
|
2000-11-05 22:48:35 +00:00
|
|
|
buf = nrealloc(buf, strlen(buf) + 2);
|
2000-11-05 21:54:23 +00:00
|
|
|
strncpy(buf + pos, matches[0] + pos, 1);
|
|
|
|
newplace++;
|
|
|
|
pos++;
|
2000-11-05 22:48:35 +00:00
|
|
|
} else {
|
2000-11-05 21:54:23 +00:00
|
|
|
beep();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-11-06 05:40:03 +00:00
|
|
|
break;
|
2000-11-05 21:54:23 +00:00
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
} else {
|
|
|
|
/* Ok -- the last char was a TAB. Since they
|
|
|
|
* just hit TAB again, print a list of all the
|
|
|
|
* available choices... */
|
|
|
|
if (matches && num_matches > 0) {
|
|
|
|
|
|
|
|
/* Blank the edit window, and print the matches out there */
|
|
|
|
blank_edit();
|
|
|
|
wmove(edit, 0, 0);
|
|
|
|
|
2000-11-05 21:54:23 +00:00
|
|
|
editline = 0;
|
|
|
|
/* Figure out the length of the longest filename */
|
|
|
|
for (i = 0; i < num_matches; i++)
|
|
|
|
if (strlen(matches[i]) > longestname)
|
|
|
|
longestname = strlen(matches[i]);
|
|
|
|
|
|
|
|
if (longestname > COLS - 1)
|
|
|
|
longestname = COLS - 1;
|
|
|
|
|
2000-11-05 21:56:54 +00:00
|
|
|
foo = nmalloc(longestname + 5);
|
|
|
|
|
2000-11-05 17:54:41 +00:00
|
|
|
/* Print the list of matches */
|
|
|
|
for (i = 0, col = 0; i < num_matches; i++) {
|
2000-11-05 21:54:23 +00:00
|
|
|
|
|
|
|
/* make each filename shown be the same length as the longest
|
|
|
|
filename, with two spaces at the end */
|
|
|
|
snprintf(foo, longestname + 1, matches[i]);
|
|
|
|
while (strlen(foo) < longestname)
|
|
|
|
strcat(foo, " ");
|
|
|
|
|
|
|
|
strcat(foo, " ");
|
|
|
|
|
2000-11-05 22:48:35 +00:00
|
|
|
/* now, put the match on the screen */
|
|
|
|
waddnstr(edit, foo, strlen(foo));
|
|
|
|
col += strlen(foo);
|
|
|
|
|
|
|
|
/* And if the next match isn't going to fit on the
|
|
|
|
line, move to the next one */
|
|
|
|
if (col > (COLS - longestname) && matches[i + 1] != NULL) {
|
2000-11-05 21:54:23 +00:00
|
|
|
editline++;
|
|
|
|
wmove(edit, editline, 0);
|
2000-11-06 02:59:11 +00:00
|
|
|
if (editline == editwinrows - 1) {
|
|
|
|
waddstr(edit, _("(more)"));
|
|
|
|
break;
|
|
|
|
}
|
2000-11-05 17:54:41 +00:00
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
2000-11-05 21:56:54 +00:00
|
|
|
free(foo);
|
2000-11-05 17:54:41 +00:00
|
|
|
wrefresh(edit);
|
2000-11-06 05:45:48 +00:00
|
|
|
} else
|
|
|
|
beep();
|
2000-11-05 17:54:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_refresh();
|
|
|
|
return newplace;
|
|
|
|
}
|