#include "vim.h"
const char *__dynamic_da_name = "Vim heap";
int ro_line_mode = TRUE;
int windowed;
int WinLeft, WinTop;
int ScrollTop;
int old_escape_state = -1;
int old_cursor_state = -1;
#define rgb(r,g,b) ((b<<24) + (g<<16) + (r<<8))
#define NORMAL_FG 0x00000000
#define NORMAL_BG 0xffffffff
static int
map_colour(dos)
int dos;
{
switch (dos)
{
case 0: return 0;
case 1: return rgb(0,0,139);
case 2: return rgb(0,100,0);
case 3: return rgb(0,139,139);
case 4: return rgb(139,0,0);
case 5: return rgb(139,0,139);
case 6: return rgb(165,42,42);
case 7: return rgb(211,211,211);
case 8: return rgb(169,169,169);
case 9: return rgb(173,216,230);
case 10: return rgb(144,238,144);
case 11: return rgb(224,255,255);
case 12: return rgb(255,0,0);
case 13: return rgb(255,0,255);
case 14: return rgb(255,255,0);
case 15: return rgb(255,255,255);
}
return rgb(100,100,100);
}
static void
text_fg(fg)
int fg;
{
xswi(ColourTrans_SetTextColour, fg, 0, 0, 0);
}
static void
text_bg(bg)
int bg;
{
xswi(ColourTrans_SetTextColour, bg, 0, 0, 1 << 7);
}
#define OUT_NORMAL 0
#define OUT_NUMBER 1
void
mch_write(s, len)
char_u *s;
int len;
{
static int mode = OUT_NORMAL;
static int x, y;
if (!term_console)
{
while (len--)
{
char_u c = *s++;
swi(OS_WriteC, c);
if (c == 10)
swi(OS_WriteI + 13);
}
return;
}
while (len--)
{
char_u c = *s++;
switch (mode)
{
case OUT_NUMBER:
if (c < '0' || c > '9')
{
mode = OUT_NORMAL;
}
else
{
x = (x * 10) + c - '0';
continue;
}
case OUT_NORMAL:
switch (c)
{
case 1:
mode = OUT_NUMBER;
y = x;
x = 0;
break;
case 2:
swi(OS_WriteI + 31);
swi(OS_WriteC, x);
swi(OS_WriteC, y - ScrollTop);
break;
case 3:
if (x == Rows -1 && y == 0 && !windowed)
{
swi(OS_WriteI + 26);
}
else
{
swi(OS_WriteI + 28);
swi(OS_WriteC, WinLeft);
swi(OS_WriteC, WinTop + x);
swi(OS_WriteC, WinLeft + Columns - 1);
swi(OS_WriteC, WinTop + y);
}
ScrollTop = y;
break;
case 4:
text_fg(NORMAL_FG);
text_bg(NORMAL_BG);
break;
case 5:
text_fg(NORMAL_BG);
text_bg(NORMAL_FG);
break;
case 10:
swi(OS_NewLine);
break;
case 14:
swi(OS_WriteN,
"\027\001\000\000\000\000\000\000\000\000",
10);
break;
case 15:
swi(OS_WriteN,
"\027\001\002\000\000\000\000\000\000\000",
10);
break;
case 16:
swi(OS_WriteN,
"\027\001\003\000\000\000\000\000\000\000",
10);
case 17:
text_fg(map_colour(x));
break;
case 18:
text_bg(map_colour(x));
break;
case 19:
swi(OS_WriteN,
"\027\007\000\002\000\000\000\000\000\000",
10);
break;
default:
swi(OS_WriteC, c);
}
continue;
default:
printf("[output error]");
mode = OUT_NORMAL;
}
}
}
int
mch_inchar(buf, maxlen, wtime, tb_change_cnt)
char_u *buf;
int maxlen;
long wtime;
int tb_change_cnt;
{
int got=0;
unsigned int start_time = clock();
if (ro_line_mode)
{
static char_u line_buffer[256];
static int remaining_chars = 0;
static int buf_pos = 0;
if (remaining_chars == 0)
{
int old_esc_state;
swi(OS_Byte, 200, 1, 0xfe);
old_esc_state = r1;
buf_pos = 0;
if (xswi(OS_ReadLine, line_buffer, 255, 0, 255) & (c_flag | v_flag))
{
got_int = TRUE;
r1 = 0;
}
line_buffer[r1] = 13;
remaining_chars = r1 + 1;
swi(OS_Byte, 200, old_esc_state, 0);
}
if (remaining_chars <= maxlen)
{
int got = remaining_chars;
memcpy(buf, line_buffer + buf_pos, got);
remaining_chars = 0;
return got;
}
memcpy(buf, line_buffer + buf_pos, maxlen);
buf_pos += maxlen;
remaining_chars -= maxlen;
return maxlen;
}
if (!term_console)
{
if (wtime == 0)
return 0;
if (xswi(OS_ReadC) & c_flag)
{
got_int = TRUE;
swi(OS_Byte, 124);
r0 = 0x1b;
}
buf[0] = r0;
return 1;
}
while (xswi(OS_Byte,145,0) & c_flag)
{
if ( (wtime != -1) && (clock() - start_time) >= wtime )
return 0;
}
while (got < maxlen)
{
buf[got++] = r2;
if (xswi(OS_Byte,145,0) & c_flag)
return got;
}
return got;
}
int
mch_char_avail()
{
if (!term_console)
return 0;
if (xswi(OS_Byte, 152, 0) & c_flag)
return 0;
return 1;
}
long_u
mch_avail_mem(special)
int special;
{
if (xswi(Wimp_SlotSize, -1, -1) & v_flag)
return 0x7fffffff;
return r2;
}
void
mch_delay(msec, ignoreinput)
long msec;
int ignoreinput;
{
int start_time, time_now;
int csec = msec / 10;
swi(OS_ReadMonotonicTime);
start_time = r0;
for (;;)
{
swi(OS_ReadMonotonicTime);
time_now = r0;
if (time_now - start_time > csec)
return;
#ifdef FEAT_GUI
if (gui.in_use)
gui_mch_wait_for_chars(start_time + csec);
#endif
}
}
void
mch_suspend()
{
suspend_shell();
}
void
mch_init()
{
int buf[7] = {132, 135, 256, 257, 1, 2, -1};
#ifndef FEAT_GUI
swi(OS_WriteI);
#endif
swi(OS_ReadVduVariables, buf, buf);
WinLeft = buf[0];
WinTop = buf[1];
Columns = buf[2];
Rows = buf[3] + 1;
ScrollTop = 0;
if (Rows == buf[5] + 1 && Columns == buf[4] + 1)
windowed = 0;
else
windowed = 1;
text_fg(NORMAL_FG);
text_bg(NORMAL_BG);
}
int
mch_check_win(argc, argv)
int argc;
char **argv;
{
return OK;
}
int
mch_input_isatty()
{
if (xswi(OS_ChangeRedirection, -1, -1) & v_flag)
return TRUE;
if (r0 == 0)
return TRUE;
return FALSE;
}
#ifdef FEAT_TITLE
int
mch_can_restore_title()
{
return FALSE;
}
int
mch_can_restore_icon()
{
return FALSE;
}
void
mch_settitle(title, icon)
char_u *title;
char_u *icon;
{
if (title == NULL)
title = (char_u *) "<untitled>";
#ifdef FEAT_GUI
if (gui.in_use && strcmp(title, gui.window_title))
{
int length;
length = strlen(title);
if (length >= gui.window_title_size)
length = gui.window_title_size - 1;
strncpy(gui.window_title, title, length);
gui.window_title[length] = 0;
ro_redraw_title(gui.window_handle);
}
#endif
return;
}
void
mch_restore_title(which)
int which;
{
return;
}
#endif
int
mch_get_user_name(s, len)
char_u *s;
int len;
{
*s = NUL;
return FAIL;
}
void
mch_get_host_name(s, len)
char_u *s;
int len;
{
if (xswi(OS_ReadVarVal, "Machine$Name", s, len, 0, 3) & v_flag)
{
vim_strncpy(s, "(unknown)", len - 1);
}
}
long
mch_get_pid()
{
if (xswi(Wimp_ReadSysInfo, 5) & v_flag)
return 0;
return r0;
}
int
mch_dirname(buf, len)
char_u *buf;
int len;
{
if (xswi(OS_FSControl, 37, "@", buf, 0, 0, len) & v_flag)
return FAIL;
return OK;
}
int
mch_FullName(fname, buf, len, force)
char_u *fname, *buf;
int len;
int force;
{
if (xswi(OS_FSControl, 37, fname, buf, 0, 0, len) & v_flag)
return FAIL;
return OK;
}
int
mch_isFullName(fname)
char_u *fname;
{
if (strstr(fname, "::") && strstr(fname,".$."))
return TRUE;
return FALSE;
}
long
mch_getperm(name)
char_u *name;
{
struct stat statb;
if (stat((char *)name, &statb))
return -1;
return statb.st_mode;
}
int
mch_setperm(name, perm)
char_u *name;
long perm;
{
return (chmod((char *)name, (mode_t)perm) == 0 ? OK : FAIL);
}
void
mch_hide(name)
char_u *name;
{
}
int
mch_isdir(name)
char_u *name;
{
if (xswi(OS_File, 17, name) & v_flag)
return FALSE;
if (r0 == 2 || r0 == 3)
return TRUE;
return FALSE;
}
int
mch_can_exe(name)
char_u *name;
{
char_u *buf;
char_u *p;
int retval;
buf = alloc((unsigned)STRLEN(name) + 7);
if (buf == NULL)
return -1;
sprintf((char *)buf, "which %s", name);
p = get_cmd_output(buf, NULL, SHELL_SILENT);
vim_free(buf);
if (p == NULL)
return -1;
retval = (*p != NUL && strstr((char *)p, "not found") == NULL);
vim_free(p);
return retval;
}
int
mch_nodetype(name)
char_u *name;
{
return NODE_NORMAL;
}
void
mch_early_init()
{
int __riscosify_control = __RISCOSIFY_NO_PROCESS;
}
void
mch_exit(r)
int r;
{
settmode(TMODE_COOK);
exiting = TRUE;
out_flush();
ml_close_all(TRUE);
#ifdef FEAT_GUI
if (gui.in_use)
gui_exit(r);
#endif
swi(OS_NewLine);
if (old_escape_state != -1)
swi(OS_Byte, 229, old_escape_state, 0);
if (old_cursor_state != -1)
swi(OS_Byte, 4, old_cursor_state);
exit(r);
}
void
mch_settmode(tmode)
int tmode;
{
if (tmode == TMODE_COOK)
{
ro_line_mode = TRUE;
return;
}
ro_line_mode = FALSE;
if (term_console)
{
swi(OS_WriteN,
"\027\000\012\000\000\000\000\000\000\000",
10);
swi(OS_Byte, 4, 1);
if (old_cursor_state == -1)
old_cursor_state = r1;
}
swi(OS_Byte, 229, 1, 0);
if (old_escape_state == -1)
old_escape_state = r1;
}
void
mch_setmouse(on)
int on;
{
}
int
mch_screenmode(arg)
char_u *arg;
{
EMSG(_(e_screenmode));
return FAIL;
}
int
mch_get_shellsize()
{
return term_console ? OK : FAIL;
}
void
mch_set_shellsize()
{
}
void
mch_new_shellsize()
{
}
int
mch_call_shell(cmd, options)
char_u *cmd;
int options;
{
int retval;
int tmode = cur_tmode;
if (cmd == NULL)
cmd = (char_u *) "GOS";
#ifdef FEAT_GUI
if (gui.in_use)
return gui_mch_call_shell(cmd, options);
#endif
if (options & SHELL_COOKED)
settmode(TMODE_COOK);
MSG_PUTS("\n");
retval = system(cmd);
if (retval && !(options & SHELL_SILENT))
EMSG(strerror(EOPSYS));
swi(OS_Byte, 229, 1, 0);
if (tmode == TMODE_RAW)
settmode(TMODE_RAW);
return retval ? FAIL : OK;
}
void
mch_breakcheck()
{
if (xswi(OS_Byte, 121, 0xf0) & v_flag)
return;
if (r1 == 0xff)
{
got_int = TRUE;
swi(OS_Byte, 15, 1);
}
}
int
mch_expandpath(gap, path, flags)
garray_T *gap;
char_u *path;
int flags;
{
int got;
char_u *pattern;
pattern = vim_strsave(path);
if (pattern == NULL)
return 0;
got = expand_section(gap, (char_u *)"", pattern, flags);
vim_free(pattern);
return got;
}
int
expand_section(gap, root, rest, flags)
garray_T *gap;
char_u *root;
char_u *rest;
int flags;
{
static char_u buf[MAXPATHL];
char_u dir[MAXPATHL];
int start_element = -1;
char_u c;
int i;
int got, dir_pos;
int buflen;
int colon = 0;
buflen = strlen(root);
STRNCPY(buf, root, buflen);
for (i = 0; c = rest[i]; i++)
{
if (c == PATHSEP)
{
start_element = i;
colon = 0;
}
if (c == ':')
{
start_element = i + 1;
colon = 1;
}
if (c == '#' || c == '*')
break;
}
if (c == 0)
start_element = i;
if (buflen + start_element + 4 >= MAXPATHL)
return 0;
if (start_element >= 0)
{
if (*root && !colon)
buf[buflen++] = PATHSEP;
strncpy(buf + buflen, rest, start_element);
buflen += start_element;
}
buf[buflen] = 0;
if (c == 0)
{
addfile(gap, buf, flags);
return 1;
}
if (start_element < 0 || !colon)
start_element++;
rest += start_element;
start_element = -1;
for (i = 0; rest[i]; i++)
{
if (rest[i] == '.')
{
start_element = i;
rest[i] = 0;
break;
}
}
r3 = 0;
dir_pos = 0;
got = 0;
while (dir_pos != -1)
{
buf[buflen] = 0;
if (xswi(OS_GBPB, 9,
buf,
buf + buflen + (1 - colon),
1,
dir_pos,
MAXPATHL - 2 - buflen,
rest)
& v_flag)
{
EMSG(r0 + 4);
r4 = -1;
}
dir_pos = r4;
if (r3 > 0)
{
char_u *path = buf;
if (buflen == 0)
path++;
else if (!colon)
buf[buflen] = '.';
if (start_element == -1)
{
addfile(gap, path, flags);
got++;
}
else
{
swi(OS_File, 17, path);
if (r0 == 2 || r0 == 3)
got += expand_section(gap,
path,
rest + start_element + 1,
flags);
}
}
}
if (start_element >= 0)
rest[start_element] = '.';
return got;
}
int
mch_expand_wildcards(num_pat, pat, num_file, file, flags)
int num_pat;
char_u **pat;
int *num_file;
char_u ***file;
int flags;
{
return FAIL;
}
int
mch_has_exp_wildcard(p)
char_u *p;
{
if (vim_strpbrk((char_u *)"*#", p))
return TRUE;
return FALSE;
}
int
mch_has_wildcard(p)
char_u *p;
{
if (vim_strpbrk((char_u *)"*#`", p))
return TRUE;
return FALSE;
}
int
mch_remove(file)
char_u *file;
{
if (xswi(OS_FSControl, 27, file, 0, 0) & v_flag)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
char_u *
mch_munge_fname(fname)
char_u *fname;
{
char_u c;
int len;
char_u *retval;
retval = fname = vim_strsave(fname);
if (fname == NULL)
return NULL;
if (strncmp(fname, "$VIM/", 5) == 0)
{
strncpy(fname, "Vim:", 4);
for (fname += 5; c = *fname; fname++)
{
if (c == '.')
break;
if (c == '/')
fname[-1] = '.';
else
fname[-1] = c;
}
fname[-1] = '\0';
}
else
{
if (xswi(OS_File, 17, fname) & v_flag)
r0 == 0;
if (r0)
return retval;
len = strlen(fname);
if (strcmp(fname + len - 4, ".vim") == 0)
{
fname[len - 4] = '\0';
for (; c = *fname; fname++)
{
if (c == '/')
*fname = '.';
}
}
}
return retval;
}
int
ro_buflist_add(old_name)
char_u *old_name;
{
char_u *fname;
char_u *leaf;
char_u *ptr;
char_u c;
int retval;
if (old_name == NULL)
return buflist_add(NULL, 0);
fname = vim_strsave(old_name);
if (fname == NULL)
return buflist_add(old_name, 0);
leaf = fname;
for (ptr = fname; c = *ptr; ptr++)
{
if (c == '/')
{
leaf = ptr + 1;
*ptr = '.';
}
else if (c == '.')
break;
}
if (c == '.')
{
ptr += old_name - fname;
*ptr = '\0';
sprintf(leaf,
"%s.%s",
ptr + 1,
leaf - fname + old_name);
}
retval = buflist_add(fname, 0);
free(fname);
return retval;
}
int
mch_chdir(dir)
char_u *dir;
{
int length;
int retval;
char_u *new_dir;
length = strlen(dir);
if (dir[length - 1] != '.')
return chdir(dir);
new_dir = vim_strsave(dir);
if (new_dir == NULL)
return chdir(dir);
while (new_dir[--length] == '.')
new_dir[length] = '\0';
retval = chdir(new_dir);
vim_free(new_dir);
return retval;
}
void
mch_read_filetype(file)
char_u *file;
{
int type;
char_u type_string[9];
int i;
if (xswi(OS_File, 23, file) & v_flag)
type = 0xfff;
else
type = r6;
swi(OS_FSControl, 18, 0, type);
((int *) type_string)[0] = r2;
((int *) type_string)[1] = r3;
type_string[8] = 0;
for (i = 0; type_string[i] > ' '; i++)
;
type_string[i] = 0;
set_string_option_direct("osfiletype", -1, type_string, OPT_FREE, 0);
return;
}
void
mch_set_filetype(file, type)
char_u *file;
char_u *type;
{
if (xswi(OS_FSControl, 31, type) & v_flag)
{
EMSG(_("E366: Invalid 'osfiletype' option - using Text"));
r2 = 0xfff;
}
swi(OS_File, 18, file, r2);
}
int
mch_check_filetype(fname, type)
char_u *fname;
char_u *type;
{
int value;
char *end;
if (*type != '&')
return FALSE;
value = strtol(type + 1, &end, 16);
if (*end)
return FALSE;
if (xswi(OS_File, 23, fname) & v_flag)
return FALSE;
return (r0 && r6 == value);
}