#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
+#include <termcap.h>
#include <unistd.h>
#include <limits.h>
-
-#ifndef YMAX
-#ifdef UNIX
#include <stdio.h>
-#undef putchar
-#undef getchar
-#undef NULL
-#undef EOF
-extern char *CE, *VS, *SO, *SE, *CL, *AL, *CM;
-#define YMAX 49
-#else
-#define YMAX 24 /* Maximum y coordinate starting at 0 */
-/* Escape sequences. */
-extern char *enter_string; /* String printed on entering mined */
-extern char *rev_video; /* String for starting reverse video */
-extern char *normal_video; /* String for leaving reverse video */
-extern char *rev_scroll; /* String for reverse scrolling */
-extern char *pos_string; /* Absolute cursor positioning */
-#define X_PLUS ' ' /* To be added to x for cursor sequence */
-#define Y_PLUS ' ' /* To be added to y for cursor sequence */
-#endif /* UNIX */
+extern char *CE, *VS, *SO, *SE, *CL, *AL, *CM;
+#define YMAX 49 /* Maximum y coordinate starting at 0 */
#define XMAX 79 /* Maximum x coordinate starting at 0*/
#define SCREENMAX (YMAX - 1) /* Number of lines displayed */
#define XBREAK (XMAX - 0) /* Line shift at this coordinate */
/*
* Print character on terminal
*/
-#define putchar(c) (void) write_char(STD_OUT, (c))
+#define putch(c) _putch((c))
/*
* Ring bell on terminal
*/
-#define ring_bell() putchar('\07')
+#define ring_bell() putch('\07')
/*
* Print string on terminal
*/
#define get_shift(cnt) ((cnt) & DUMMY_MASK)
-#endif /* YMAX */
-
/* mined1.c */
void FS(void);
void reset(LINE *head_line, int screen_y );
void set_cursor(int nx, int ny );
void open_device(void);
-int getchar(void);
+int getch(void);
void display(int x_coord, int y_coord, LINE *line, int count );
int write_char(int fd, int c );
int writeline(int fd, char *text );
void panic(char *message );
char *alloc(int bytes );
void free_space(char *p );
-/*
-#ifdef UNIX
-void(*key_map [128]) (void);
-#else
-void(*key_map [256]) (void);
-#endif
-*/
void initialize(void);
char *basename(char *path );
void load_file(char *file );
int get_number(char *message, int *result );
int input(char *inbuf, FLAG clearfl );
int get_file(char *message, char *file );
-int _getchar(void);
+int _getch(void);
void _flush(void);
-void _putchar(int c );
+int _putch(int c );
void get_term(void);
/* mined2.c */
-void UP(void);
-void DN(void);
-void LF(void);
-void RT(void);
+void UP1(void);
+void DN1(void);
+void LF1(void);
+void RT1(void);
void HIGH(void);
void LOW(void);
void BL(void);
* 3.3 Moving around.
*
* Several commands are implemented for moving through the file.
- * Moving up (UP), down (DN) left (LF) and right (RT) are done by the
+ * Moving up (UP1), down (DN1) left (LF1) and right (RT1) are done by the
* arrow keys. Moving one line below the screen scrolls the screen one
* line up. Moving one line above the screen scrolls the screen one line
* down. The functions forward_scroll () and reverse_scroll () take care
* string). The functions status_line (string1, string2), error (string1,
* string2), clear_status () and bottom_line () all print information on
* the status line.
- * Get_string (message, buffer) reads a string and getchar () reads one
+ * Get_string (message, buffer) reads a string and getch () reads one
* character from the terminal.
* Num_out ((long) number) prints the number into a 11 digit field
* without leading zero's. It returns a pointer to the resulting string.
* Output is done by four functions: writeline(fd,string), clear_buffer()
* write_char (fd, c) and flush_buffer (fd). Three defines are provided
* to write on filedescriptor STD_OUT (terminal) which is used normally:
- * string_print (string), putchar (c) and flush (). All these functions
+ * string_print (string), putch (c) and flush (). All these functions
* use the global I/O buffer screen and the global index for this array
* called out_count. In this way I/O can be buffered, so that reads or
* writes can be done in blocks of SCREEN_SIZE size.
/* Free old linked list, initialize global variables and load new file */
initialize();
-#ifdef UNIX
- tputs(CL, 0, _putchar);
-#else
- string_print (enter_string);
-#endif /* UNIX */
+ tputs(CL, 0, _putch);
load_file(new_file[0] == '\0' ? NULL : new_file);
}
return;
case 0: /* This is the child */
set_cursor(0, ymax);
- putchar('\n');
+ putch('\n');
flush();
raw_mode(OFF);
if (rpipe) { /* Fix stdin */
clear_status ();
set_cursor(0, ymax);
if (revfl == ON) { /* Print rev. start sequence */
-#ifdef UNIX
- tputs(SO, 0, _putchar);
-#else
- string_print(rev_video);
-#endif /* UNIX */
+ tputs(SO, 0, _putch);
stat_visible = TRUE;
}
else /* Used as clear_status() */
ret = input(inbuf, statfl);
/* Print normal video */
-#ifdef UNIX
- tputs(SE, 0, _putchar);
- tputs(CE, 0, _putchar);
-#else
- string_print(normal_video);
- string_print(blank_line); /* Clear the rest of the line */
-#endif /* UNIX */
+ tputs(SE, 0, _putch);
+ tputs(CE, 0, _putch); /* Clear the rest of the line */
if (inbuf != NULL)
set_cursor(0, ymax);
else
*/
void set_cursor(int nx, int ny)
{
-#ifdef UNIX
- extern char *tgoto();
-
- tputs(tgoto(CM, nx, ny), 0, _putchar);
-#else
- char text_buffer[10];
-
- build_string(text_buffer, pos_string, ny+1, nx+1);
- string_print(text_buffer);
-#endif /* UNIX */
+ tputs(tgoto(CM, nx, ny), 0, _putch);
}
/*
* Getchar() reads one character from the terminal. The character must be
* masked with 0377 to avoid sign extension.
*/
-int getchar(void)
+int getch(void)
{
-#ifdef UNIX
- return (_getchar() & 0377);
-#else
- char c;
-
- if (read(input_fd, &c, 1) != 1 && quit == FALSE)
- panic("Can't read one char from fd #0");
-
- return c & 0377;
-#endif /* UNIX */
+ return (_getch() & 0377);
}
/*
/* Print the blank lines (if any) */
if (loading == FALSE) {
while (count-- >= 0) {
-#ifdef UNIX
- tputs(CE, 0, _putchar);
-#else
- string_print(blank_line);
-#endif /* UNIX */
- putchar('\n');
+ tputs(CE, 0, _putch);
+ putch('\n');
}
}
}
tab_count = tab(count);
while (count < XBREAK && count < tab_count) {
count++;
- putchar(' ');
+ putch(' ');
}
textp++;
}
else {
if (*textp >= '\01' && *textp <= '\037') {
-#ifdef UNIX
- tputs(SO, 0, _putchar);
-#else
- string_print (rev_video);
-#endif /* UNIX */
- putchar(*textp++ + '\100');
-#ifdef UNIX
- tputs(SE, 0, _putchar);
-#else
- string_print (normal_video);
-#endif /* UNIX */
+ tputs(SO, 0, _putch);
+ putch(*textp++ + '\100');
+ tputs(SE, 0, _putch);
}
else
- putchar(*textp++);
+ putch(*textp++);
count++;
}
}
/* If line is longer than XBREAK chars, print the shift_mark */
if (count == XBREAK && *textp != '\n')
- putchar(textp[1]=='\n' ? *textp : SHIFT_MARK);
+ putch(textp[1]=='\n' ? *textp : SHIFT_MARK);
/* Clear the rest of the line is clear_line is TRUE */
if (clear_line == TRUE) {
-#ifdef UNIX
- tputs(CE, 0, _putchar);
-#else
- string_print(blank_line);
-#endif /* UNIX */
- putchar('\n');
+ tputs(CE, 0, _putch);
+ putch('\n');
}
}
{
if (out_count <= 0) /* There is nothing to flush */
return FINE;
-#ifdef UNIX
if (fd == STD_OUT) {
printf("%.*s", out_count, screen);
_flush();
}
- else
-#endif /* UNIX */
- if (write(fd, screen, out_count) != out_count) {
+ else if (write(fd, screen, out_count) != out_count) {
bad_write(fd);
return ERRORS;
}
/* Ask for confirmation */
status_line("Really abort? ", NULL);
- if (getchar() != 'y') {
+ if (getch() != 'y') {
clear_status();
return;
}
/* Reset terminal */
raw_mode(OFF);
set_cursor(0, ymax);
- putchar('\n');
+ putch('\n');
flush();
-#ifdef UNIX
abort();
-#else
- exit(1);
-#endif /* UNIX */
}
#define UNDEF _POSIX_VDISABLE
{
extern char yank_file[];
-#ifdef UNIX
- tputs(CL, 0, _putchar);
+ tputs(CL, 0, _putch);
build_string(text_buffer, "%s\nError code %d\n", message, errno);
-#else
- build_string(text_buffer, "%s%s\nError code %d\n", enter_string, message, errno);
-#endif /* UNIX */
(void) write(STD_OUT, text_buffer, length_of(text_buffer));
if (loading == FALSE)
(void) unlink(yank_file);
raw_mode(OFF);
-#ifdef UNIX
abort();
-#else
- exit(1);
-#endif /* UNIX */
}
char *alloc(int bytes)
char text_buffer[MAX_CHARS]; /* Buffer for modifying text */
/* Escape sequences. */
-#ifdef UNIX
char *CE, *VS, *SO, *SE, *CL, *AL, *CM;
-#else
-char *enter_string = "\033[H\033[J"; /* String printed on entering mined */
-char *pos_string = "\033[%d;%dH"; /* Absolute cursor position */
-char *rev_scroll = "\033M"; /* String for reverse scrolling */
-char *rev_video = "\033[7m"; /* String for starting reverse video */
-char *normal_video = "\033[m"; /* String for leaving reverse video */
-char *blank_line = "\033[K"; /* Clear line to end */
-#endif /* UNIX */
/*
* Yank variables.
register int index; /* Index in key table */
struct winsize winsize;
-#ifdef UNIX
get_term();
- tputs(VS, 0, _putchar);
- tputs(CL, 0, _putchar);
-#else
- string_print(enter_string); /* Hello world */
-#endif /* UNIX */
+ tputs(VS, 0, _putch);
+ tputs(CL, 0, _putch);
if (ioctl(STD_OUT, TIOCGWINSZ, &winsize) == 0 && winsize.ws_row != 0) {
ymax = winsize.ws_row - 1;
screenmax = ymax - 1;
/* Main loop of the editor. */
for (;;) {
- index = getchar();
+ index = getch();
if (stat_visible == TRUE)
clear_status();
if (quit == TRUE)
void RD(void)
{
/* Clear screen */
-#ifdef UNIX
- tputs(VS, 0, _putchar);
- tputs(CL, 0, _putchar);
-#else
- string_print(enter_string);
-#endif /* UNIX */
+ tputs(VS, 0, _putch);
+ tputs(CL, 0, _putch);
/* Print first page */
display(0, 0, top_line, last_y);
/* Clear last line */
set_cursor(0, ymax);
-#ifdef UNIX
- tputs(CE, 0, _putchar);
-#else
- string_print(blank_line);
-#endif /* UNIX */
+ tputs(CE, 0, _putch);
move_to(x, y);
}
raw_mode(OFF);
set_cursor(0, ymax);
- putchar('\n');
+ putch('\n');
flush();
(void) unlink(yank_file); /* Might not be necessary */
exit(0);
{
if (c == '[') {
/* Start of ASCII escape sequence. */
- c = getchar();
+ c = getch();
switch (c) {
case 'H': return(HO);
- case 'A': return(UP);
- case 'B': return(DN);
- case 'C': return(RT);
- case 'D': return(LF);
+ case 'A': return(UP1);
+ case 'B': return(DN1);
+ case 'C': return(RT1);
+ case 'D': return(LF1);
case '@': return(MA);
case 'G': return(FS);
case 'S': return(SR);
register void (*func)();
int index;
- index = getchar();
+ index = getch();
while (index >= '0' && index <= '9' && quit == FALSE) {
count *= 10;
count += index - '0';
- index = getchar();
+ index = getch();
}
if (count == 0) {
count = 1;
} else {
func = key_map[index];
if (func == ESC)
- func = escfunc(getchar());
+ func = escfunc(getch());
}
if (func == I) { /* Function assigned? */
status_line(file_name[0] ? basename(file_name) : "[buffer]" ,
" has been modified. Save? (y/n)");
- while((c = getchar()) != 'y' && c != 'n' && quit == FALSE) {
+ while((c = getch()) != 'y' && c != 'n' && quit == FALSE) {
ring_bell();
flush();
}
status_line(message, NULL);
- index = getchar();
+ index = getch();
if (quit == FALSE && (index < '0' || index > '9')) {
error("Bad count", NULL);
return ERRORS;
while (index >= '0' && index <= '9' && quit == FALSE) {
count *= 10;
count += index - '0';
- index = getchar();
+ index = getch();
}
if (quit == TRUE) {
*ptr = '\0';
while (quit == FALSE) {
flush();
- switch (c = getchar()) {
+ switch (c = getch()) {
case '\b' : /* Erase previous char */
if (ptr > inbuf) {
ptr--;
-#ifdef UNIX
- tputs(SE, 0, _putchar);
-#else
- string_print(normal_video);
-#endif /* UNIX */
+ tputs(SE, 0, _putch);
if (is_tab(*ptr))
string_print(" \b\b\b \b\b");
else
string_print(" \b\b \b");
-#ifdef UNIX
- tputs(SO, 0, _putchar);
-#else
- string_print(rev_video);
-#endif /* UNIX */
+ tputs(SO, 0, _putch);
string_print(" \b");
*ptr = '\0';
}
if (c == '\t')
string_print("^I");
else
- putchar(c);
+ putch(c);
string_print(" \b");
}
else
* UNIX I/O Routines *
* ======================================================================== */
-#ifdef UNIX
-#undef putchar
-
-int _getchar(void)
+int _getch(void)
{
char c;
(void) fflush(stdout);
}
-void _putchar(char c)
+int _putch(int c)
{
- (void) write_char(STD_OUT, c);
+ if (write_char(STD_OUT, c) == FINE)
+ return c;
+ else
+ return EOF;
}
void get_term(void)
exit(1);
}
}
-#endif /* UNIX */
/*
* Move one line up.
*/
-void UP(void)
+void UP1(void)
{
if (y == 0) { /* Top line of screen. Scroll one line */
(void) reverse_scroll();
/*
* Move one line down.
*/
-void DN(void)
+void DN1(void)
{
if (y == last_y) { /* Last line of screen. Scroll one line */
if (bot_line->next == tail && bot_line->text[0] != '\n') {
dummy_line(); /* Create new empty line */
- DN();
+ DN1();
return;
}
else {
/*
* Move left one position.
*/
-void LF(void)
+void LF1(void)
{
if (x == 0 && get_shift(cur_line->shift_count) == 0) {/* Begin of line */
if (cur_line->prev != header) {
- UP(); /* Move one line up */
+ UP1(); /* Move one line up */
move_to(LINE_END, y);
}
}
/*
* Move right one position.
*/
-void RT(void)
+void RT1(void)
{
if (*cur_text == '\n') {
if (cur_line->next != tail) { /* Last char of file */
- DN(); /* Move one line down */
+ DN1(); /* Move one line down */
move_to(LINE_START, y);
}
}
if (reverse_scroll() == ERRORS)
break; /* Top of file reached */
set_cursor(0, ymax); /* Erase very bottom line */
-#ifdef UNIX
- tputs(CE, 0, _putchar);
-#else
- string_print(blank_line);
-#endif /* UNIX */
+ tputs(CE, 0, _putch);
if (y + i > screenmax) /* line no longer on screen */
move_to(0, screenmax >> 1);
else
(void) reverse_scroll();
set_cursor(0, ymax); /* Erase very bottom line */
-#ifdef UNIX
- tputs(CE, 0, _putchar);
-#else
- string_print(blank_line);
-#endif /* UNIX */
+ tputs(CE, 0, _putch);
move_to(x, (y == screenmax) ? screenmax : y + 1);
}
/* Perform the scroll */
set_cursor(0, 0);
-#ifdef UNIX
- tputs(AL, 0, _putchar);
-#else
- string_print(rev_scroll);
-#endif /* UNIX */
+ tputs(AL, 0, _putch);
set_cursor(0, 0);
line_print(top_line);
start_char = '\0';
}
- LF();
+ LF1();
begin_line = cur_line->text;
textp = cur_text;
/* If we're at end of line. move to the first word on the next line. */
if (*textp == '\n' && cur_line->next != tail) {
- DN();
+ DN1();
move_to(LINE_START, y);
textp = cur_text;
while (*textp != '\n' && white_space(*textp))
if (x == 0 && cur_line->prev == header)
return; /* Top of file */
- LF(); /* Move one left */
+ LF1(); /* Move one left */
DCC(); /* Delete character under cursor */
}
register char ctrl;
status_line("Enter control character.", NULL);
- if ((ctrl = getchar()) >= '\01' && ctrl <= '\037') {
+ if ((ctrl = getch()) >= '\01' && ctrl <= '\037') {
S(ctrl); /* Insert the char */
clear_status();
}
void LIB(void)
{
S('\n'); /* Insert the line */
- UP(); /* Move one line up */
+ UP1(); /* Move one line up */
move_to(LINE_END, y); /* Move to end of this line */
}