1 /*========================================================================* 2 * Mined.h * 3 *========================================================================*/ 4 5 6 #include <sys/types.h> 7 #include <fcntl.h> 8 #include <stdlib.h> 9 #include <termcap.h> 10 #include <unistd.h> 11 #include <limits.h> 12 #include <stdio.h> 13 14 extern char *CE, *VS, *SO, *SE, *CL, *AL, *CM; 15 #define YMAX 49 /* Maximum y coordinate starting at 0 */ 16 #define XMAX 79 /* Maximum x coordinate starting at 0*/ 17 #define SCREENMAX (YMAX - 1) /* Number of lines displayed */ 18 #define XBREAK (XMAX - 0) /* Line shift at this coordinate */ 19 #define SHIFT_SIZE 25 /* Number of chars to shift */ 20 #define SHIFT_MARK '!' /* Char indicating line continues */ 21 #define MAX_CHARS 1024 /* Maximum chars on one line */ 22 23 /* LINE_START must be rounded up to the lowest SHIFT_SIZE */ 24 #define LINE_START (((-MAX_CHARS - 1) / SHIFT_SIZE) * SHIFT_SIZE \ 25 - SHIFT_SIZE) 26 #define LINE_END (MAX_CHARS + 1) /* Highest x-coordinate for line */ 27 28 #define LINE_LEN (XMAX + 1) /* Number of characters on line */ 29 #define SCREEN_SIZE (XMAX * YMAX) /* Size of I/O buffering */ 30 #define BLOCK_SIZE 1024 31 32 /* Return values of functions */ 33 #define ERRORS -1 34 #define NO_LINE (ERRORS - 1) /* Must be < 0 */ 35 #define FINE (ERRORS + 1) 36 #define NO_INPUT (ERRORS + 2) 37 38 #define STD_OUT 1 /* File descriptor for terminal */ 39 40 #define REPORT 2 /* Report change of lines on # lines */ 41 42 typedef int FLAG; 43 44 /* General flags */ 45 #define FALSE 0 46 #define TRUE 1 47 #define NOT_VALID 2 48 #define VALID 3 49 #define OFF 4 50 #define ON 5 51 52 /* Expression flags */ 53 #define FORWARD 6 54 #define REVERSE 7 55 56 /* Yank flags */ 57 #define SMALLER 8 58 #define BIGGER 9 59 #define SAME 10 60 #define EMPTY 11 61 #define NO_DELETE 12 62 #define DELETE 13 63 #define READ 14 64 #define WRITE 15 65 66 /* 67 * The Line structure. Each line entry contains a pointer to the next line, 68 * a pointer to the previous line, a pointer to the text and an unsigned char 69 * telling at which offset of the line printing should start (usually 0). 70 */ 71 struct Line { 72 struct Line *next; 73 struct Line *prev; 74 char *text; 75 unsigned char shift_count; 76 }; 77 78 typedef struct Line LINE; 79 80 /* Dummy line indicator */ 81 #define DUMMY 0x80 82 #define DUMMY_MASK 0x7F 83 84 /* Expression definitions */ 85 #define NO_MATCH 0 86 #define MATCH 1 87 #define REG_ERROR 2 88 89 #define BEGIN_LINE (2 * REG_ERROR) 90 #define END_LINE (2 * BEGIN_LINE) 91 92 /* 93 * The regex structure. Status can be any of 0, BEGIN_LINE or REG_ERROR. In 94 * the last case, the result.err_mess field is assigned. Start_ptr and end_ptr 95 * point to the match found. For more details see the documentation file. 96 */ 97 struct regex { 98 union { 99 char *err_mess; 100 int *expression; 101 } result; 102 char status; 103 char *start_ptr; 104 char *end_ptr; 105 }; 106 107 typedef struct regex REGEX; 108 109 /* 110 * Forward declarations 111 */ 112 extern int nlines; /* Number of lines in file */ 113 extern LINE *header; /* Head of line list */ 114 extern LINE *tail; /* Last line in line list */ 115 extern LINE *top_line; /* First line of screen */ 116 extern LINE *bot_line; /* Last line of screen */ 117 extern LINE *cur_line; /* Current line in use */ 118 extern char *cur_text; /* Pointer to char on current line in use */ 119 extern int last_y; /* Last y of screen. Usually SCREENMAX */ 120 extern int ymax; 121 extern int screenmax; 122 extern char screen[SCREEN_SIZE];/* Output buffer for "writes" and "reads" */ 123 124 extern int x, y; /* x, y coordinates on screen */ 125 extern FLAG modified; /* Set when file is modified */ 126 extern FLAG stat_visible; /* Set if status_line is visible */ 127 extern FLAG writable; /* Set if file cannot be written */ 128 extern FLAG quit; /* Set when quit character is typed */ 129 extern FLAG rpipe; /* Set if file should be read from stdin */ 130 extern int input_fd; /* Fd for command input */ 131 extern FLAG loading; /* Set if we're loading a file */ 132 extern int out_count; /* Index in output buffer */ 133 extern char file_name[LINE_LEN]; /* Name of file in use */ 134 extern char text_buffer[MAX_CHARS]; /* Buffer for modifying text */ 135 extern char *blank_line; /* Clear line to end */ 136 137 extern char yank_file[]; /* Temp file for buffer */ 138 extern FLAG yank_status; /* Status of yank_file */ 139 extern long chars_saved; /* Nr of chars saved in buffer */ 140 141 /* 142 * Empty output buffer 143 */ 144 #define clear_buffer() (out_count = 0) 145 146 /* 147 * Print character on terminal 148 */ 149 #define putch(c) _putch((c)) 150 151 /* 152 * Ring bell on terminal 153 */ 154 #define ring_bell() putch('\a') 155 156 /* 157 * Print string on terminal 158 */ 159 #define string_print(str) (void) writeline(STD_OUT, (str)) 160 161 /* 162 * Flush output buffer 163 */ 164 #define flush() (void) flush_buffer(STD_OUT) 165 166 /* 167 * Convert cnt to nearest tab position 168 */ 169 #define tab(cnt) (((cnt) + 8) & ~07) 170 #define is_tab(c) ((c) == '\t') 171 172 /* 173 * Word defenitions 174 */ 175 #define white_space(c) ((c) == ' ' || (c) == '\t') 176 #define alpha(c) ((c) != ' ' && (c) != '\t' && (c) != '\n') 177 178 /* 179 * Print line on terminal at offset 0 and clear tail of line 180 */ 181 #define line_print(line) put_line(line, 0, TRUE) 182 183 /* 184 * Move to coordinates and set textp. (Don't use address) 185 */ 186 #define move_to(nx, ny) move((nx), NULL, (ny)) 187 188 /* 189 * Move to coordinates on screen as indicated by textp. 190 */ 191 #define move_address(address) move(0, (address), y) 192 193 /* 194 * Functions handling status_line. ON means in reverse video. 195 */ 196 #define status_line(str1, str2) (void) bottom_line(ON, (str1), \ 197 (str2), NULL, FALSE) 198 #define error(str1, str2) (void) bottom_line(ON, (str1), \ 199 (str2), NULL, FALSE) 200 #define get_string(str1,str2, fl) bottom_line(ON, (str1), NULL, (str2), fl) 201 #define clear_status() (void) bottom_line(OFF, NULL, NULL, \ 202 NULL, FALSE) 203 204 /* 205 * Print info about current file and buffer. 206 */ 207 #define fstatus(mess, cnt) file_status((mess), (cnt), file_name, \ 208 nlines, writable, modified) 209 210 /* 211 * Get real shift value. 212 */ 213 #define get_shift(cnt) ((cnt) & DUMMY_MASK) 214 215 /* mined1.c */ 216 217 void FS(void); 218 void VI(void); 219 int WT(void); 220 void XWT(void); 221 void SH(void); 222 LINE *proceed(LINE *line, int count ); 223 int bottom_line(FLAG revfl, char *s1, char *s2, char *inbuf, FLAG statfl 224 ); 225 int count_chars(LINE *line ); 226 void move(int new_x, char *new_address, int new_y ); 227 int find_x(LINE *line, char *address ); 228 char *find_address(LINE *line, int x_coord, int *old_x ); 229 int length_of(char *string ); 230 void copy_string(char *to, char *from ); 231 void reset(LINE *head_line, int screen_y ); 232 void set_cursor(int nx, int ny ); 233 void open_device(void); 234 int getch(void); 235 void display(int x_coord, int y_coord, LINE *line, int count ); 236 int write_char(int fd, int c ); 237 int writeline(int fd, char *text ); 238 void put_line(LINE *line, int offset, FLAG clear_line ); 239 int flush_buffer(int fd ); 240 void bad_write(int fd ); 241 void catch(int sig ); 242 void abort_mined(void); 243 void raw_mode(FLAG state ); 244 void panic(char *message ); 245 char *alloc(int bytes ); 246 void free_space(char *p ); 247 void initialize(void); 248 char *basename(char *path ); 249 void load_file(char *file ); 250 int get_line(int fd, char *buffer ); 251 LINE *install_line(char *buffer, int length ); 252 int main(int argc, char *argv []); 253 void RD(void); 254 void I(void); 255 void XT(void); 256 void ESC(void); 257 int ask_save(void); 258 int line_number(void); 259 void file_status(char *message, long count, char *file, int lines, FLAG 260 writefl, FLAG changed ); 261 void build_string(char *buf, char *fmt, ...); 262 char *num_out(long number ); 263 int get_number(char *message, int *result ); 264 int input(char *inbuf, FLAG clearfl ); 265 int get_file(char *message, char *file ); 266 int _getch(void); 267 void _flush(void); 268 int _putch(int c ); 269 void get_term(void); 270 271 /* mined2.c */ 272 273 void UP1(void); 274 void DN1(void); 275 void LF1(void); 276 void RT1(void); 277 void HIGH(void); 278 void LOW(void); 279 void BL(void); 280 void EL(void); 281 void GOTO(void); 282 void PD(void); 283 void PU(void); 284 void HO(void); 285 void EF(void); 286 void SU(void); 287 void SD(void); 288 int forward_scroll(void); 289 int reverse_scroll(void); 290 void MP(void); 291 void move_previous_word(FLAG remove ); 292 void MN(void); 293 void move_next_word(FLAG remove ); 294 void DCC(void); 295 void DPC(void); 296 void DLN(void); 297 void DNW(void); 298 void DPW(void); 299 void S(int character ); 300 void CTL(void); 301 void LIB(void); 302 LINE *line_insert(LINE *line, char *string, int len ); 303 int insert(LINE *line, char *location, char *string ); 304 LINE *line_delete(LINE *line ); 305 void delete(LINE *start_line, char *start_textp, LINE *end_line, char 306 *end_textp ); 307 void PT(void); 308 void IF(void); 309 void file_insert(int fd, FLAG old_pos ); 310 void WB(void); 311 void MA(void); 312 void YA(void); 313 void DT(void); 314 void set_up(FLAG remove ); 315 FLAG checkmark(void); 316 int legal(void); 317 void yank(LINE *start_line, char *start_textp, LINE *end_line, char 318 *end_textp, FLAG remove ); 319 int scratch_file(FLAG mode ); 320 void SF(void); 321 void SR(void); 322 REGEX *get_expression(char *message ); 323 void GR(void); 324 void LR(void); 325 void change(char *message, FLAG file ); 326 char *substitute(LINE *line, REGEX *program, char *replacement ); 327 void search(char *message, FLAG method ); 328 int find_y(LINE *match_line ); 329 void finished(REGEX *program, int *last_exp ); 330 void compile(char *pattern, REGEX *program ); 331 LINE *match(REGEX *program, char *string, FLAG method ); 332 int line_check(REGEX *program, char *string, FLAG method ); 333 int check_string(REGEX *program, char *string, int *expression ); 334 int star(REGEX *program, char *end_position, char *string, int 335 *expression ); 336 int in_list(int *list, int c, int list_length, int opcode ); 337 void dummy_line(void); 338