home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
useful
/
util
/
edit
/
vim
/
src
/
archie.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-09
|
13KB
|
763 lines
/* vi:ts=4:sw=4
*
*
* VIM - Vi IMproved
*
* Code Contributions By: Bram Moolenaar mool@oce.nl
* Tim Thompson twitch!tjt
* Tony Andrews onecom!wldrdg!tony
* G. R. (Fred) Walter watmath!watcgl!grwalter
*/
/*
* archie.c -- RISC OS + UnixLib specific code.
*
* A lot of this file was written by Juergen Weigert.
*
* It was then hacked to pieces by Alun Jones to work on the Acorn
* Archimedes!
*/
#include "vim.h"
#include "globals.h"
#include "param.h"
#include "proto.h"
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/os.h>
#include <signal.h>
#include <termio.h>
static int Read __ARGS((char *, long));
static int WaitForChar __ARGS((int));
static int RealWaitForChar __ARGS((int));
static void fill_inbuf __ARGS((void));
static int do_resize = FALSE;
/* I'm sure this should be defined in UnixLib, but it ain't!
*/
short ospeed;
void
mch_write(s, len)
char *s;
int len;
{
int i;
for (i=0; i<len; i++)
{
os_vdu(s[i]);
}
}
/*
* GetChars(): low level input funcion.
* Get a characters from the keyboard.
* If time == 0 do not wait for characters.
* If time == n wait a short time for characters.
* If time == -1 wait forever for characters.
*/
int
GetChars(buf, maxlen, time)
char *buf;
int maxlen;
int time;
{
if (time >= 0)
{
if (WaitForChar(time) == 0) /* no character available */
return 0;
}
else /* time == -1 */
{
/*
* If there is no character available within 2 seconds (default)
* write the autoscript file to disk
*/
if (WaitForChar((int)p_ut) == 0)
updatescript(0);
}
WaitForChar(-1);
return Read(buf, (long)maxlen);
}
void
vim_delay()
{
clock_t now;
now = clock();
while (clock() < now + CLOCKS_PER_SEC/2);
}
/*
* No job control. Fake it by starting a new shell.
*/
void
mch_suspend()
{
outstr("new shell started\n");
call_shell(NULL, 0, 1);
}
void
mch_windinit()
{
Columns = 80;
Rows = 24;
flushbuf();
mch_get_winsize();
}
/*
* Check_win checks whether we have an interactive window.
*/
void
check_win(argc, argv)
int argc;
char **argv;
{
if (!isatty(0) || !isatty(1))
{
fprintf(stderr, "VIM: no controlling terminal\n");
exit(2);
}
}
/*
* fname_case(): Set the case of the filename, if it already exists.
* This will cause the filename to remain exactly the same.
*/
void
fname_case(name)
char *name;
{
}
void
settitle(str)
char *str;
{
}
void
resettitle()
{
}
/*
* Get name of current directory into buffer 'buf' of length 'len' bytes.
* Return non-zero for success.
*/
int
dirname(buf, len)
char *buf;
int len;
{
extern int errno;
extern char *sys_errlist[];
if (getcwd(buf,len) == NULL)
{
strcpy(buf, sys_errlist[errno]);
return 0;
}
return 1;
}
/*
* get absolute filename into buffer 'buf' of length 'len' bytes
*/
int
FullName(fname, buf, len)
char *fname, *buf;
int len;
{
int l;
char olddir[MAXPATHL];
char *p;
int c;
int retval = 1;
if (fname == NULL) /* always fail */
return 0;
*buf = 0;
if (*fname != '/')
{
/*
* If the file name has a path, change to that directory for a moment,
* and then do the getwd() (and get back to where we were).
* This will get the correct path name with "../" things.
*/
if ((p = strrchr(fname, '/')) != NULL)
{
if (getcwd(olddir, MAXPATHL) == NULL)
{
p = NULL; /* can't get current dir: don't chdir */
retval = 0;
}
else
{
c = *p;
*p = NUL;
chdir("\\"); /* Try to maintain PSD */
if (chdir(fname))
retval = 0;
else
fname = p + 1;
*p = c;
}
}
if (getcwd(buf, len) == NULL)
{
retval = 0;
*buf = NUL;
}
l = strlen(buf);
if (l && buf[l - 1] != '/')
strcat(buf, "/");
if (p)
{
chdir("\\"); /* Maintain PSD */
chdir(olddir);
}
}
strcat(buf, fname);
return retval;
}
/*
* get file permissions for 'name'
*/
long
getperm(name)
char *name;
{
struct stat statb;
if (stat(name, &statb))
return -1;
return statb.st_mode;
}
/*
* set file permission for 'name' to 'perm'
*/
int
setperm(name, perm)
char *name;
int perm;
{
return chmod(name, perm);
}
/*
* check if "name" is a directory
*/
int
isdir(name)
char *name;
{
struct stat statb;
if (stat(name, &statb))
return -1;
return (statb.st_mode & S_IFMT) == S_IFDIR;
}
void
mch_windexit(r)
int r;
{
settmode(0);
stoptermcap();
flushbuf();
stopscript(); /* remove autoscript file */
exit(r);
}
void
mch_settmode(raw)
int raw;
{
static int old225, old226, old4;
int retvals[3];
static struct termio told;
struct termio tnew;
if (raw)
{
/* Make arrow keys act as function keys.
*/
os_byte(4, 2, 0, retvals);
old4 = retvals[1];
/* Now make function keys return NULL followed by a character.
* Remember the old value for resetting.
*/
os_byte(225, 0xC0, 0, retvals);
old225 = retvals[1];
os_byte(226, 0xD0, 0, retvals);
old226 = retvals[1];
ioctl(0, TCGETA, &told);
tnew = told;
tnew.c_iflag &= ~(ICRNL | IXON); /* ICRNL enables typing ^V^M */
/* IXON enables typing ^S/^Q */
tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE);
tnew.c_cc[VMIN] = 1; /* return after 1 char */
tnew.c_cc[VTIME] = 0; /* don't wait */
ioctl(0, TCSETA, &tnew);
}
else
{
os_byte(4, old4, 0, retvals);
os_byte(225, old225, 0, retvals);
os_byte(226, old226, 0, retvals);
ioctl(0, TCSETA, &told);
}
}
/*
* Try to get the current window size:
* 1. with an ioctl(), most accurate method
* 2. from the environment variables LINES and COLUMNS
* 3. from the termcap
* 4. keep using the old values
*/
int
mch_get_winsize()
{
int old_Rows = Rows;
int old_Columns = Columns;
char *p;
Columns = 0;
Rows = 0;
/*
* 1. try using an ioctl. It is the most accurate method.
*/
{
struct winsize ws;
if (ioctl(0, TIOCGWINSZ, &ws) == 0)
{
Columns = ws.ws_col;
Rows = ws.ws_row;
}
}
/*
* 2. get size from environment
*/
if (Columns == 0 || Rows == 0)
{
if ((p = (char *)getenv("LINES")))
Rows = atoi(p);
if ((p = (char *)getenv("COLUMNS")))
Columns = atoi(p);
}
/*
* 3. try reading the termcap
*/
if (Columns == 0 || Rows == 0)
{
extern void getlinecol();
getlinecol(); /* get "co" and "li" entries from termcap */
}
/*
* 4. If everything fails, use the old values
*/
if (Columns <= 0 || Rows <= 0)
{
Columns = old_Columns;
Rows = old_Rows;
return 1;
}
debug2("mch_get_winsize: %dx%d\n", (int)Columns, (int)Rows);
Rows_max = Rows; /* remember physical max height */
check_winsize();
script_winsize();
/* if size changed: screenalloc will allocate new screen buffers */
return (0);
}
void
mch_set_winsize()
{
/* should try to set the window size to Rows and Columns */
}
int
call_shell(cmd, dummy, cooked)
char *cmd;
int dummy;
int cooked;
{
int x;
char newcmd[1024];
flushbuf();
if (cooked)
settmode(0); /* set to cooked mode */
if (cmd == NULL)
x = system(p_sh);
else
{
sprintf(newcmd, "*%s", cmd);
x = system(newcmd);
}
if (x == 127)
{
emsg("Cannot execute shell sh");
outchar('\n');
}
else if (x)
{
smsg("%d returned", x);
outchar('\n');
}
if (cooked)
settmode(1); /* set to raw mode */
return x;
}
/*
* The input characters are buffered to be able to check for a CTRL-C.
* This should be done with signals, but I don't know how to do that in
* a portable way for a tty in RAW mode.
*/
#define INBUFLEN 50
static unsigned char inbuf[INBUFLEN]; /* internal typeahead buffer */
static int inbufcount = 0; /* number of chars in inbuf[] */
static int
Read(buf, maxlen)
char *buf;
long maxlen;
{
if (inbufcount == 0) /* if the buffer is empty, fill it */
fill_inbuf();
if (maxlen > inbufcount)
maxlen = inbufcount;
memmove(buf, inbuf, maxlen);
inbufcount -= maxlen;
if (inbufcount)
memmove(inbuf, inbuf + maxlen, inbufcount);
return (int)maxlen;
}
void
breakcheck()
{
/*
* check for CTRL-C typed by reading all available characters
*/
if (RealWaitForChar(0)) /* if characters available */
fill_inbuf();
}
static void
fill_inbuf()
{
int len;
if (i