new six version M.Z.

This commit is contained in:
cvs
2003-10-24 15:02:29 +00:00
parent 501d336adb
commit 9aa438f2b8
6 changed files with 501 additions and 88 deletions

View File

@@ -4,8 +4,9 @@
#include <sys/time.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "term.h"
#include "myc_mem.h"
#include "myc_str.h"
#include "sys_select.h"
#define ESC_TMO 1000
@@ -138,7 +139,110 @@ static char *history[HISTORY_LINES]={NULL};
static int hist_pos=0; /* position when scrolling through the history */
static int hist_end=0; /* end of history. Always history[hist_end]==NULL */
static int dirty=0; /* line is to be cleared through a call of term_clear_line */
static char filehead[256]="";
FILE *term_open_pref(char *head, char *mode) {
char buf[PATH_MAX], hom[PATH_MAX];
char *cret, *home, usr[256];
int i;
cret=getenv("USER");
if (cret == NULL || *cret == '\0') return NULL;
str_copy(usr, cret);
if (strcmp(usr, "lnsg") == 0) { /* special case lnsg */
cret=getcwd(buf, sizeof(buf));
if (cret == NULL) return NULL;
home=getenv("HOME");
realpath(home, hom);
cret=strstr(buf,hom);
if (cret == buf) { /* cwd starts with HOME, take subdirectory as usr */
cret+=strlen(hom)+1;
str_copy(usr, "lnsg_");
str_append(usr, cret);
cret=strchr(usr, '/');
if (cret != NULL) *cret='\0';
}
}
/* usr is now the username, or lnsg_<subdirectory> */
str_copy(buf, head);
str_append(buf, usr);
return fopen(buf, mode);
}
char *term_fgets(char *buf, int size, FILE *fil) {
char *p, *ret;
char skipbuf[256];
buf[0]='\0';
ret=fgets(buf, size, fil);
if (ret==NULL) {
return NULL;
}
p=strchr(buf,'\n');
if (p==NULL) {
while (p==NULL) { /* skip rest of line */
ret=fgets(skipbuf, sizeof(skipbuf), fil);
if (ret==NULL) {
return NULL;
}
p=strchr(skipbuf,'\n');
}
} else {
*p='\0';
}
return ret;
}
void term_save_hist(int
trimlast) {
FILE *fil;
int i,n;
if (filehead[0]=='\0') return;
fil=term_open_pref(filehead, "w");
if (fil==NULL) return;
n=HISTORY_LINES-1;
if (trimlast) {
n--;
}
i=hist_end;
while (n>0) {
n--;
i++; if (i>=HISTORY_LINES) i=0;
if (history[i]!=NULL) {
fputs(history[i], fil); fputs("\n", fil);
}
}
fclose(fil);
}
void term_read_hist(char *id) {
FILE *fil;
int i;
char buf[1024], *lin;
str_copy(filehead, "/tmp/");
str_append(filehead, id);
str_append(filehead, "_hist.");
fil=term_open_pref(filehead, "r");
if (fil==NULL) return;
hist_end=0;
while (hist_end<HISTORY_LINES-1 && !feof(fil)) {
term_fgets(buf, sizeof(buf), fil);
i=strlen(buf);
if (i>0) {
lin=MALLOC(i+1);
strncpy(lin, buf, i+1);
history[hist_end]=lin;
hist_end++;
}
}
fclose(fil);
history[hist_end]=NULL;
hist_pos=hist_end;
}
static int dirty=0; /* line is to be cleared through a call of term_clear */
void term_clear(void) {
if (dirty) {
@@ -147,10 +251,17 @@ void term_clear(void) {
}
}
void term_off(void) {
dirty=1;
term_clear();
sys_keys_off();
}
int term_get_line(char *buf, int size, int *pos, char *prompt, fd_set *mask) {
char key, *lin;
int i,j,l,iret,buflen;
char tmp[512];
static int init=1;
buf[size-1]='\0'; /* make sure buf is null terminated */
l=strlen(buf);
@@ -200,6 +311,7 @@ int term_get_line(char *buf, int size, int *pos, char *prompt, fd_set *mask) {
history[hist_end]==NULL;
}
hist_pos=hist_end;
term_save_hist(0);
return(STDIN_FILENO);
/* normal EXIT */
@@ -232,8 +344,14 @@ int term_get_line(char *buf, int size, int *pos, char *prompt, fd_set *mask) {
buf[size-1]='\0';
hist_pos=i;
l=strlen(buf);
*pos=l;
} else {
buf[0]='\0';
l=0;
if (history[hist_pos]!=NULL) {
hist_pos=i;
}
}
*pos=l;
break;
default:
if (l<size-1) {