2 * Copyright (c) 1987,1997, Prentice Hall
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:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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
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.
35 * [original code from minix codebase]
37 /*========================================================================*
39 *========================================================================*/
46 #include <sys/types.h>
59 extern char *CE
, *VS
, *SO
, *SE
, *CL
, *AL
, *CM
;
62 #define YMAX 24 /* Maximum y coordinate starting at 0 */
63 /* Escape sequences. */
64 extern const char *enter_string
; /* String printed on entering mined */
65 extern const char *rev_video
; /* String for starting reverse video */
66 extern const char *normal_video
; /* String for leaving reverse video */
67 extern const char *rev_scroll
; /* String for reverse scrolling */
68 extern const char *pos_string
; /* Absolute cursor positioning */
69 #define X_PLUS ' ' /* To be added to x for cursor sequence */
70 #define Y_PLUS ' ' /* To be added to y for cursor sequence */
73 #define XMAX 79 /* Maximum x coordinate starting at 0*/
74 #define SCREENMAX (YMAX - 1) /* Number of lines displayed */
75 #define XBREAK (XMAX - 1) /* Line shift at this coordinate */
76 #define SHIFT_SIZE 25 /* Number of chars to shift */
77 #define SHIFT_MARK '!' /* Char indicating line continues */
78 #define MAX_CHARS 1024 /* Maximum chars on one line */
80 /* LINE_START must be rounded up to the lowest SHIFT_SIZE */
81 #define LINE_START (((-MAX_CHARS - 1) / SHIFT_SIZE) * SHIFT_SIZE \
83 #define LINE_END (MAX_CHARS + 1) /* Highest x-coordinate for line */
85 #define LINE_LEN (XMAX + 1) /* Number of characters on line */
86 #define SCREEN_SIZE (XMAX * YMAX) /* Size of I/O buffering */
87 #define BLOCK_SIZE 1024
89 /* Return values of functions */
91 #define NO_LINE (ERRORS - 1) /* Must be < 0 */
92 #define FINE (ERRORS + 1)
93 #define NO_INPUT (ERRORS + 2)
95 #define STD_OUT 1 /* File descriptor for terminal */
98 #define MEMORY_SIZE (50 * 1024) /* Size of data space to malloc */
101 #define REPORT 2 /* Report change of lines on # lines */
113 /* Expression flags */
128 * The Line structure. Each line entry contains a pointer to the next line,
129 * a pointer to the previous line, a pointer to the text and an unsigned char
130 * telling at which offset of the line printing should start (usually 0).
136 unsigned char shift_count
;
139 typedef struct Line LINE
;
141 /* Dummy line indicator */
143 #define DUMMY_MASK 0x7F
145 /* Expression definitions */
150 #define BEGIN_LINE (2 * REG_ERROR)
151 #define END_LINE (2 * BEGIN_LINE)
154 * The regex structure. Status can be any of 0, BEGIN_LINE or REG_ERROR. In
155 * the last case, the result.err_mess field is assigned. Start_ptr and end_ptr
156 * point to the match found. For more details see the documentation file.
160 const char *err_mess
;
168 typedef struct regex REGEX
;
170 /* NULL definitions */
171 #define NIL_PTR ((char *) 0)
172 #define NIL_LINE ((LINE *) 0)
173 #define NIL_REG ((REGEX *) 0)
174 #define NIL_INT ((int *) 0)
177 * Forward declarations
179 extern int nlines
; /* Number of lines in file */
180 extern LINE
*header
; /* Head of line list */
181 extern LINE
*tail
; /* Last line in line list */
182 extern LINE
*top_line
; /* First line of screen */
183 extern LINE
*bot_line
; /* Last line of screen */
184 extern LINE
*cur_line
; /* Current line in use */
185 extern char *cur_text
; /* Pointer to char on current line in use */
186 extern int last_y
; /* Last y of screen. Usually SCREENMAX */
188 extern int screenmax
;
189 extern char screen
[SCREEN_SIZE
];/* Output buffer for "writes" and "reads" */
191 extern int x
, y
; /* x, y coordinates on screen */
192 extern FLAG modified
; /* Set when file is modified */
193 extern FLAG stat_visible
; /* Set if status_line is visible */
194 extern FLAG writable
; /* Set if file cannot be written */
195 extern FLAG quit
; /* Set when quit character is typed */
196 extern FLAG rpipe
; /* Set if file should be read from stdin */
197 extern int input_fd
; /* Fd for command input */
198 extern FLAG loading
; /* Set if we're loading a file */
199 extern int out_count
; /* Index in output buffer */
200 extern char file_name
[LINE_LEN
]; /* Name of file in use */
201 extern char text_buffer
[MAX_CHARS
]; /* Buffer for modifying text */
202 extern const char *blank_line
; /* Clear line to end */
204 extern char yank_file
[]; /* Temp file for buffer */
205 extern FLAG yank_status
; /* Status of yank_file */
206 extern long chars_saved
; /* Nr of chars saved in buffer */
209 * Empty output buffer
211 #define clear_buffer() (out_count = 0)
214 * Print character on terminal
216 #define putchar(c) write_char(STD_OUT, (c))
219 * Ring bell on terminal
221 #define ring_bell() putchar('\07')
224 * Print string on terminal
226 #define string_print(str) writeline(STD_OUT, (str))
229 * Flush output buffer
231 #define flush() flush_buffer(STD_OUT)
234 * Convert cnt to nearest tab position
236 #define tab(cnt) (((cnt) + 8) & ~07)
237 #define is_tab(c) ((c) == '\t')
242 #define white_space(c) ((c) == ' ' || (c) == '\t')
243 #define alpha(c) ((c) != ' ' && (c) != '\t' && (c) != '\n')
246 * Print line on terminal at offset 0 and clear tail of line
248 #define line_print(line) put_line(line, 0, TRUE)
251 * Move to coordinates and set textp. (Don't use address)
253 #define move_to(nx, ny) move((nx), NIL_PTR, (ny))
256 * Move to coordinates on screen as indicated by textp.
258 #define move_address(address) move(0, (address), y)
261 * Functions handling status_line. ON means in reverse video.
263 #define status_line(str1, str2) bottom_line(ON, (str1), (str2), NIL_PTR, FALSE)
264 #define error(str1, str2) bottom_line(ON, (str1), (str2), NIL_PTR, FALSE)
265 #define get_string(str1,str2, fl) bottom_line(ON, (str1), NIL_PTR, (str2), fl)
266 #define clear_status() bottom_line(OFF, NIL_PTR, NIL_PTR, \
270 * Print info about current file and buffer.
272 #define fstatus(mess, cnt) file_status((mess), (cnt), file_name, \
273 nlines, writable, modified)
276 * Get real shift value.
278 #define get_shift(cnt) ((cnt) & DUMMY_MASK)
289 LINE
*proceed(LINE
*line
, int count
);
290 int bottom_line(FLAG revfl
, const char *s1
, const char *s2
, char *inbuf
, FLAG statfl
);
291 int count_chars(LINE
*line
);
292 void move(int new_x
, char *new_address
, int new_y
);
293 int find_x(LINE
*line
, char *address
);
294 char *find_address(LINE
*line
, int x_coord
, int *old_x
);
295 int length_of(char *string
);
296 void copy_string(char *to
, const char *from
);
297 void reset(LINE
*head_line
, int screen_y
);
298 void set_cursor(int nx
, int ny
);
299 void open_device(void);
301 void display(int x_coord
, int y_coord
, LINE
*line
, int count
);
302 int write_char(int fd
, char c
);
303 int writeline(int fd
, const char *text
);
304 void put_line(LINE
*line
, int offset
, FLAG clear_line
);
305 int flush_buffer(int fd
);
306 void bad_write(int fd
);
308 void abort_mined(void);
309 void raw_mode(FLAG state
);
310 void panic(const char *message
) __dead2
;
311 void *alloc(int bytes
);
312 void free_space(char *p
);
313 void initialize(void);
314 char *basename(char *path
);
315 void load_file(const char *file
);
316 int get_line(int fd
, char *buffer
);
317 LINE
*install_line(const char *buffer
, int length
);
323 int line_number(void);
324 void file_status(const char *message
, long count
, char *file
, int lines
,
325 FLAG writefl
, FLAG changed
);
326 void build_string(char *buf
, const char *fmt
, ...);
327 char *num_out(long number
);
328 int get_number(const char *message
, int *result
);
329 int input(char *inbuf
, FLAG clearfl
);
330 int get_file(const char *message
, char *file
);
333 void _putchar(int c
);
355 int forward_scroll(void);
356 int reverse_scroll(void);
358 void move_previous_word(FLAG remove
);
360 void move_next_word(FLAG remove
);
366 void S(int character
);
369 LINE
*line_insert(LINE
*line
, const char *string
, int len
);
370 int insert(LINE
*line
, char *location
, char *string
);
371 LINE
*line_delete(LINE
*line
);
372 void delete(LINE
*start_line
, char *start_textp
,
373 LINE
*end_line
, char *end_textp
);
376 void file_insert(int fd
, FLAG old_pos
);
381 void set_up(FLAG remove
);
382 FLAG
checkmark(void);
384 void yank(LINE
*start_line
, char *start_textp
,
385 LINE
*end_line
, char *end_textp
, FLAG remove
);
386 int scratch_file(FLAG mode
);
389 REGEX
*get_expression(const char *message
);
392 void change(const char *message
, FLAG file
);
393 char *substitute(LINE
*line
, REGEX
*program
, char *replacement
);
394 void search(const char *message
, FLAG method
);
395 int find_y(LINE
*match_line
);
396 void finished(REGEX
*program
, int *last_exp
);
397 void compile(char *pattern
, REGEX
*program
);
398 LINE
*match(REGEX
*program
, char *string
, FLAG method
);
399 int line_check(REGEX
*program
, char *string
, FLAG method
);
400 int check_string(REGEX
*program
, char *string
, int *expression
);
401 int star(REGEX
*program
, char *end_position
, char *string
,
403 int in_list(int *list
, char c
, int list_length
, int opcode
);
404 void dummy_line(void);