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