1 /* 2 * Copyright (c) 1987,1997, Prentice Hall 3 * All rights reserved. 4 * 5 * Redistribution and use of the MINIX operating system in source and 6 * binary forms, with or without modification, are permitted provided 7 * that the following conditions are met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials provided 15 * with the distribution. 16 * 17 * * Neither the name of Prentice Hall nor the names of the software 18 * authors or contributors may be used to endorse or promote 19 * products derived from this software without specific prior 20 * written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND 23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 33 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * [original code from minix codebase] 36 * $DragonFly: src/bin/mined/mined.h,v 1.5 2005/10/29 12:05:27 swildner Exp $* 37 */ 38 /*========================================================================* 39 * Mined.h * 40 *========================================================================*/ 41 42 #define INTEL 1 43 #define CHIP INTEL 44 #define ASSUME_CONS25 45 #define ASSUME_XTERM 46 47 #include <sys/types.h> 48 #include <fcntl.h> 49 #include <stdlib.h> 50 #include <unistd.h> 51 #include <limits.h> 52 53 #ifndef YMAX 54 #ifdef UNIX 55 #include <stdio.h> 56 #undef putchar 57 #undef getchar 58 #undef NULL 59 #undef EOF 60 extern char *CE, *VS, *SO, *SE, *CL, *AL, *CM; 61 #define YMAX 49 62 #else 63 #define YMAX 24 /* Maximum y coordinate starting at 0 */ 64 /* Escape sequences. */ 65 extern const char *enter_string; /* String printed on entering mined */ 66 extern const char *rev_video; /* String for starting reverse video */ 67 extern const char *normal_video; /* String for leaving reverse video */ 68 extern const char *rev_scroll; /* String for reverse scrolling */ 69 extern const char *pos_string; /* Absolute cursor positioning */ 70 #define X_PLUS ' ' /* To be added to x for cursor sequence */ 71 #define Y_PLUS ' ' /* To be added to y for cursor sequence */ 72 #endif /* UNIX */ 73 74 #define XMAX 79 /* Maximum x coordinate starting at 0*/ 75 #define SCREENMAX (YMAX - 1) /* Number of lines displayed */ 76 #define XBREAK (XMAX - 1) /* Line shift at this coordinate */ 77 #define SHIFT_SIZE 25 /* Number of chars to shift */ 78 #define SHIFT_MARK '!' /* Char indicating line continues */ 79 #define MAX_CHARS 1024 /* Maximum chars on one line */ 80 81 /* LINE_START must be rounded up to the lowest SHIFT_SIZE */ 82 #define LINE_START (((-MAX_CHARS - 1) / SHIFT_SIZE) * SHIFT_SIZE \ 83 - SHIFT_SIZE) 84 #define LINE_END (MAX_CHARS + 1) /* Highest x-coordinate for line */ 85 86 #define LINE_LEN (XMAX + 1) /* Number of characters on line */ 87 #define SCREEN_SIZE (XMAX * YMAX) /* Size of I/O buffering */ 88 #define BLOCK_SIZE 1024 89 90 /* Return values of functions */ 91 #define ERRORS -1 92 #define NO_LINE (ERRORS - 1) /* Must be < 0 */ 93 #define FINE (ERRORS + 1) 94 #define NO_INPUT (ERRORS + 2) 95 96 #define STD_OUT 1 /* File descriptor for terminal */ 97 98 #if (CHIP == INTEL) 99 #define MEMORY_SIZE (50 * 1024) /* Size of data space to malloc */ 100 #endif 101 102 #define REPORT 2 /* Report change of lines on # lines */ 103 104 typedef int FLAG; 105 106 /* General flags */ 107 #define FALSE 0 108 #define TRUE 1 109 #define NOT_VALID 2 110 #define VALID 3 111 #define OFF 4 112 #define ON 5 113 114 /* Expression flags */ 115 #define FORWARD 6 116 #define REVERSE 7 117 118 /* Yank flags */ 119 #define SMALLER 8 120 #define BIGGER 9 121 #define SAME 10 122 #define EMPTY 11 123 #define NO_DELETE 12 124 #define DELETE 13 125 #define READ 14 126 #define WRITE 15 127 128 /* 129 * The Line structure. Each line entry contains a pointer to the next line, 130 * a pointer to the previous line, a pointer to the text and an unsigned char 131 * telling at which offset of the line printing should start (usually 0). 132 */ 133 struct Line { 134 struct Line *next; 135 struct Line *prev; 136 char *text; 137 unsigned char shift_count; 138 }; 139 140 typedef struct Line LINE; 141 142 /* Dummy line indicator */ 143 #define DUMMY 0x80 144 #define DUMMY_MASK 0x7F 145 146 /* Expression definitions */ 147 #define NO_MATCH 0 148 #define MATCH 1 149 #define REG_ERROR 2 150 151 #define BEGIN_LINE (2 * REG_ERROR) 152 #define END_LINE (2 * BEGIN_LINE) 153 154 /* 155 * The regex structure. Status can be any of 0, BEGIN_LINE or REG_ERROR. In 156 * the last case, the result.err_mess field is assigned. Start_ptr and end_ptr 157 * point to the match found. For more details see the documentation file. 158 */ 159 struct regex { 160 union { 161 const char *err_mess; 162 int *expression; 163 } result; 164 char status; 165 char *start_ptr; 166 char *end_ptr; 167 }; 168 169 typedef struct regex REGEX; 170 171 /* NULL definitions */ 172 #define NIL_PTR ((char *) 0) 173 #define NIL_LINE ((LINE *) 0) 174 #define NIL_REG ((REGEX *) 0) 175 #define NIL_INT ((int *) 0) 176 177 /* 178 * Forward declarations 179 */ 180 extern int nlines; /* Number of lines in file */ 181 extern LINE *header; /* Head of line list */ 182 extern LINE *tail; /* Last line in line list */ 183 extern LINE *top_line; /* First line of screen */ 184 extern LINE *bot_line; /* Last line of screen */ 185 extern LINE *cur_line; /* Current line in use */ 186 extern char *cur_text; /* Pointer to char on current line in use */ 187 extern int last_y; /* Last y of screen. Usually SCREENMAX */ 188 extern int ymax; 189 extern int screenmax; 190 extern char screen[SCREEN_SIZE];/* Output buffer for "writes" and "reads" */ 191 192 extern int x, y; /* x, y coordinates on screen */ 193 extern FLAG modified; /* Set when file is modified */ 194 extern FLAG stat_visible; /* Set if status_line is visible */ 195 extern FLAG writable; /* Set if file cannot be written */ 196 extern FLAG quit; /* Set when quit character is typed */ 197 extern FLAG rpipe; /* Set if file should be read from stdin */ 198 extern int input_fd; /* Fd for command input */ 199 extern FLAG loading; /* Set if we're loading a file */ 200 extern int out_count; /* Index in output buffer */ 201 extern char file_name[LINE_LEN]; /* Name of file in use */ 202 extern char text_buffer[MAX_CHARS]; /* Buffer for modifying text */ 203 extern const char *blank_line; /* Clear line to end */ 204 205 extern char yank_file[]; /* Temp file for buffer */ 206 extern FLAG yank_status; /* Status of yank_file */ 207 extern long chars_saved; /* Nr of chars saved in buffer */ 208 209 /* 210 * Empty output buffer 211 */ 212 #define clear_buffer() (out_count = 0) 213 214 /* 215 * Print character on terminal 216 */ 217 #define putchar(c) write_char(STD_OUT, (c)) 218 219 /* 220 * Ring bell on terminal 221 */ 222 #define ring_bell() putchar('\07') 223 224 /* 225 * Print string on terminal 226 */ 227 #define string_print(str) writeline(STD_OUT, (str)) 228 229 /* 230 * Flush output buffer 231 */ 232 #define flush() flush_buffer(STD_OUT) 233 234 /* 235 * Convert cnt to nearest tab position 236 */ 237 #define tab(cnt) (((cnt) + 8) & ~07) 238 #define is_tab(c) ((c) == '\t') 239 240 /* 241 * Word defenitions 242 */ 243 #define white_space(c) ((c) == ' ' || (c) == '\t') 244 #define alpha(c) ((c) != ' ' && (c) != '\t' && (c) != '\n') 245 246 /* 247 * Print line on terminal at offset 0 and clear tail of line 248 */ 249 #define line_print(line) put_line(line, 0, TRUE) 250 251 /* 252 * Move to coordinates and set textp. (Don't use address) 253 */ 254 #define move_to(nx, ny) move((nx), NIL_PTR, (ny)) 255 256 /* 257 * Move to coordinates on screen as indicated by textp. 258 */ 259 #define move_address(address) move(0, (address), y) 260 261 /* 262 * Functions handling status_line. ON means in reverse video. 263 */ 264 #define status_line(str1, str2) bottom_line(ON, (str1), (str2), NIL_PTR, FALSE) 265 #define error(str1, str2) bottom_line(ON, (str1), (str2), NIL_PTR, FALSE) 266 #define get_string(str1,str2, fl) bottom_line(ON, (str1), NIL_PTR, (str2), fl) 267 #define clear_status() bottom_line(OFF, NIL_PTR, NIL_PTR, \ 268 NIL_PTR, FALSE) 269 270 /* 271 * Print info about current file and buffer. 272 */ 273 #define fstatus(mess, cnt) file_status((mess), (cnt), file_name, \ 274 nlines, writable, modified) 275 276 /* 277 * Get real shift value. 278 */ 279 #define get_shift(cnt) ((cnt) & DUMMY_MASK) 280 281 #endif /* YMAX */ 282 283 /* mined1.c */ 284 285 void FS(int); 286 void VI(int); 287 int WT(void); 288 void XWT(int); 289 void SH(int); 290 LINE *proceed(LINE *line, int count); 291 int bottom_line(FLAG revfl, const char *s1, const char *s2, char *inbuf, FLAG statfl); 292 int count_chars(LINE *line); 293 void move(int new_x, char *new_address, int new_y); 294 int find_x(LINE *line, char *address); 295 char *find_address(LINE *line, int x_coord, int *old_x); 296 int length_of(char *string); 297 void copy_string(char *to, const char *from); 298 void reset(LINE *head_line, int screen_y); 299 void set_cursor(int nx, int ny); 300 void open_device(void); 301 int getchar(void); 302 void display(int x_coord, int y_coord, LINE *line, int count); 303 int write_char(int fd, char c); 304 int writeline(int fd, const char *text); 305 void put_line(LINE *line, int offset, FLAG clear_line); 306 int flush_buffer(int fd); 307 void bad_write(int fd); 308 void catch(int sig); 309 void abort_mined(void); 310 void raw_mode(FLAG state); 311 void panic(const char *message); 312 char *alloc(int bytes); 313 void free_space(char *p); 314 void initialize(void); 315 char *basename(char *path); 316 void load_file(const char *file); 317 int get_line(int fd, char *buffer); 318 LINE *install_line(const char *buffer, int length); 319 void RD(int); 320 void I(int); 321 void XT(int); 322 void ESC(int); 323 int ask_save(void); 324 int line_number(void); 325 void file_status(const char *message, long count, char *file, int lines, 326 FLAG writefl, FLAG changed); 327 void build_string(char *buf, const char *fmt, ...); 328 char *num_out(long number); 329 int get_number(const char *message, int *result); 330 int input(char *inbuf, FLAG clearfl); 331 int get_file(const char *message, char *file); 332 int _getchar(void); 333 void _flush(void); 334 void _putchar(int c); 335 void get_term(void); 336 337 /* mined2.c */ 338 339 void UP(int); 340 void DN(int); 341 void LF(int); 342 void RT(int); 343 void HIGH(int); 344 void LOW(int); 345 void BL(int); 346 void EL(int); 347 void GOTO(int); 348 void HLP(int); 349 void ST(int); 350 void PD(int); 351 void PU(int); 352 void HO(int); 353 void EF(int); 354 void SU(int); 355 void SD(int); 356 int forward_scroll(void); 357 int reverse_scroll(void); 358 void MP(int); 359 void move_previous_word(FLAG remove); 360 void MN(int); 361 void move_next_word(FLAG remove); 362 void DCC(int); 363 void DPC(int); 364 void DLN(int); 365 void DNW(int); 366 void DPW(int); 367 void S(int character); 368 void CTL(int); 369 void LIB(int); 370 LINE *line_insert(LINE *line, const char *string, int len); 371 int insert(LINE *line, char *location, char *string); 372 LINE *line_delete(LINE *line); 373 void delete(LINE *start_line, char *start_textp, 374 LINE *end_line, char *end_textp); 375 void PT(int); 376 void IF(int); 377 void file_insert(int fd, FLAG old_pos); 378 void WB(int); 379 void MA(int); 380 void YA(int); 381 void DT(int); 382 void set_up(FLAG remove); 383 FLAG checkmark(void); 384 int legal(void); 385 void yank(LINE *start_line, char *start_textp, 386 LINE *end_line, char *end_textp, FLAG remove); 387 int scratch_file(FLAG mode); 388 void SF(int); 389 void SR(int); 390 REGEX *get_expression(const char *message); 391 void GR(int); 392 void LR(int); 393 void change(const char *message, FLAG file); 394 char *substitute(LINE *line, REGEX *program, char *replacement); 395 void search(const char *message, FLAG method); 396 int find_y(LINE *match_line); 397 void finished(REGEX *program, int *last_exp); 398 void compile(char *pattern, REGEX *program); 399 LINE *match(REGEX *program, char *string, FLAG method); 400 int line_check(REGEX *program, char *string, FLAG method); 401 int check_string(REGEX *program, char *string, int *expression); 402 int star(REGEX *program, char *end_position, char *string, 403 int *expression); 404 int in_list(int *list, char c, int list_length, int opcode); 405 void dummy_line(void); 406