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