f55df96c552b0c2106bd5945e6f9213a292a3789
[dragonfly.git] / usr.bin / ee / ee.c
1 /*
2  |      ee (easy editor)
3  |
4  |      An easy to use, simple screen oriented editor.
5  |
6  |      written by Hugh Mahon
7  |
8  |      THIS MATERIAL IS PROVIDED "AS IS".  THERE ARE
9  |      NO WARRANTIES OF ANY KIND WITH REGARD TO THIS
10  |      MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE
11  |      IMPLIED WARRANTIES OF MERCHANTABILITY AND
12  |      FITNESS FOR A PARTICULAR PURPOSE.  Neither
13  |      Hewlett-Packard nor Hugh Mahon shall be liable
14  |      for errors contained herein, nor for
15  |      incidental or consequential damages in
16  |      connection with the furnishing, performance or
17  |      use of this material.  Neither Hewlett-Packard
18  |      nor Hugh Mahon assumes any responsibility for
19  |      the use or reliability of this software or
20  |      documentation.  This software and
21  |      documentation is totally UNSUPPORTED.  There
22  |      is no support contract available.  Hewlett-
23  |      Packard has done NO Quality Assurance on ANY
24  |      of the program or documentation.  You may find
25  |      the quality of the materials inferior to
26  |      supported materials.
27  |
28  |      This software is not a product of Hewlett-Packard, Co., or any 
29  |      other company.  No support is implied or offered with this software.
30  |      You've got the source, and you're on your own.
31  |
32  |      This software may be distributed under the terms of Larry Wall's 
33  |      Artistic license, a copy of which is included in this distribution. 
34  |
35  |      This notice must be included with this software and any derivatives.
36  |
37  |      This editor was purposely developed to be simple, both in 
38  |      interface and implementation.  This editor was developed to 
39  |      address a specific audience: the user who is new to computers 
40  |      (especially UNIX).
41  |      
42  |      ee is not aimed at technical users; for that reason more 
43  |      complex features were intentionally left out.  In addition, 
44  |      ee is intended to be compiled by people with little computer 
45  |      experience, which means that it needs to be small, relatively 
46  |      simple in implementation, and portable.
47  |
48  |      This software and documentation contains
49  |      proprietary information which is protected by
50  |      copyright.  All rights are reserved.
51  *
52  * $FreeBSD: src/usr.bin/ee/ee.c,v 1.16.2.6 2002/05/11 16:33:06 mp Exp $
53  * $DragonFly: src/usr.bin/ee/ee.c,v 1.3 2003/10/04 20:36:43 hmp Exp $
54  */
55
56 char *ee_copyright_message = 
57 "Copyright (c) 1986, 1990, 1991, 1992, 1993, 1994, 1995, 1996 Hugh Mahon ";
58
59 char *ee_long_notice[] = {
60         "This software and documentation contains", 
61         "proprietary information which is protected by", 
62         "copyright.  All rights are reserved."
63         };
64
65 char *version = "@(#) ee, version 1.4.1";
66
67 #ifdef NCURSE
68 #include "new_curse.h"
69 #elif HAS_NCURSES
70 #include <ncurses.h>
71 #else
72 #include <curses.h>
73 #endif
74
75 #ifdef HAS_CTYPE
76 #include <ctype.h>
77 #endif
78 #include <err.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <paths.h>
82 #include <pwd.h>
83 #include <signal.h>
84 #include <sys/types.h>
85 #include <sys/stat.h>
86 #ifdef HAS_SYS_WAIT
87 #include <sys/wait.h>
88 #endif
89 #ifdef HAS_STDARG
90 #include <stdarg.h>
91 #endif
92 #ifdef HAS_STDLIB
93 #include <stdlib.h>
94 #endif
95 #include <string.h>
96 #ifdef HAS_UNISTD
97 #include <unistd.h>
98 #endif
99
100 #ifndef NO_CATGETS
101 #include <locale.h>
102 #include <nl_types.h>
103
104 nl_catd catalog;
105 #else
106 #define catgetlocal(a, b) (b)
107 #endif /* NO_CATGETS */
108
109 #ifndef SIGCHLD
110 #define SIGCHLD SIGCLD
111 #endif
112
113 #define TAB 9
114 #define max(a, b)       (a > b ? a : b)
115 #define min(a, b)       (a < b ? a : b)
116
117 /*
118  |      defines for type of data to show in info window
119  */
120
121 #define CONTROL_KEYS 1
122 #define COMMANDS     2
123
124 struct text {
125         unsigned char *line;            /* line of characters           */
126         int line_number;                /* line number                  */
127         int line_length;        /* actual number of characters in the line */
128         int max_length; /* maximum number of characters the line handles */
129         struct text *next_line;         /* next line of text            */
130         struct text *prev_line;         /* previous line of text        */
131         };
132
133 struct text *first_line;        /* first line of current buffer         */
134 struct text *dlt_line;          /* structure for info on deleted line   */
135 struct text *curr_line;         /* current line cursor is on            */
136 struct text *tmp_line;          /* temporary line pointer               */
137 struct text *srch_line;         /* temporary pointer for search routine */
138
139 struct files {          /* structure to store names of files to be edited*/
140         unsigned char *name;            /* name of file                         */
141         struct files *next_name;
142         };
143
144 struct files *top_of_stack = NULL;
145
146 int d_wrd_len;                  /* length of deleted word               */
147 int position;                   /* offset in bytes from begin of line   */
148 int scr_pos;                    /* horizontal position                  */
149 int scr_vert;                   /* vertical position on screen          */
150 int scr_horz;                   /* horizontal position on screen        */
151 int tmp_vert, tmp_horz;
152 int input_file;                 /* indicate to read input file          */
153 int recv_file;                  /* indicate reading a file              */
154 int edit;                       /* continue executing while true        */
155 int gold;                       /* 'gold' function key pressed          */
156 int fildes;                     /* file descriptor                      */
157 int case_sen;                   /* case sensitive search flag           */
158 int last_line;                  /* last line for text display           */
159 int last_col;                   /* last column for text display         */
160 int horiz_offset = 0;           /* offset from left edge of text        */
161 int clear_com_win;              /* flag to indicate com_win needs clearing */
162 int text_changes = FALSE;       /* indicate changes have been made to text */
163 int get_fd;                     /* file descriptor for reading a file   */
164 int info_window = TRUE;         /* flag to indicate if help window visible */
165 int info_type = CONTROL_KEYS;   /* flag to indicate type of info to display */
166 int expand_tabs = TRUE;         /* flag for expanding tabs              */
167 int right_margin = 0;           /* the right margin                     */
168 int observ_margins = TRUE;      /* flag for whether margins are observed */
169 int shell_fork;
170 int temp_stdin;                 /* temporary storage for stdin          */
171 int temp_stdout;                /* temp storage for stdout descriptor   */
172 int temp_stderr;                /* temp storage for stderr descriptor   */
173 int pipe_out[2];                /* pipe file desc for output            */
174 int pipe_in[2];                 /* pipe file descriptors for input      */
175 int out_pipe;                   /* flag that info is piped out          */
176 int in_pipe;                    /* flag that info is piped in           */
177 int formatted = FALSE;          /* flag indicating paragraph formatted  */
178 int auto_format = FALSE;        /* flag for auto_format mode            */
179 int restricted = FALSE;         /* flag to indicate restricted mode     */
180 int nohighlight = FALSE;        /* turns off highlighting               */
181 int eightbit = TRUE;            /* eight bit character flag             */
182 int local_LINES = 0;            /* copy of LINES, to detect when win resizes */
183 int local_COLS = 0;             /* copy of COLS, to detect when win resizes  */
184 int curses_initialized = FALSE; /* flag indicating if curses has been started*/
185 int emacs_keys_mode = FALSE;    /* mode for if emacs key binings are used    */
186 int ee_chinese = FALSE;         /* allows handling of multi-byte characters  */
187                                 /* by checking for high bit in a byte the    */
188                                 /* code recognizes a two-byte character      */
189                                 /* sequence                                  */
190
191 unsigned char *point;           /* points to current position in line   */
192 unsigned char *srch_str;        /* pointer for search string            */
193 unsigned char *u_srch_str;      /* pointer to non-case sensitive search */
194 unsigned char *srch_1;          /* pointer to start of suspect string   */
195 unsigned char *srch_2;          /* pointer to next character of string  */
196 unsigned char *srch_3;
197 unsigned char *in_file_name = NULL;     /* name of input file           */
198 char *tmp_file; /* temporary file name                  */
199 unsigned char *d_char;          /* deleted character                    */
200 unsigned char *d_word;          /* deleted word                         */
201 unsigned char *d_line;          /* deleted line                         */
202 char in_string[513];    /* buffer for reading a file            */
203 unsigned char *print_command = "lpr";   /* string to use for the print command  */
204 unsigned char *start_at_line = NULL;    /* move to this line at start of session*/
205 const char count_text_default[] = "===============================================================================";
206 int count_text_len = sizeof(count_text_default);        /* length of the line above     */
207 char count_text[sizeof(count_text_default)];    /* buffer for current position display  */
208 int in;                         /* input character                      */
209
210 FILE *temp_fp;                  /* temporary file pointer               */
211 FILE *bit_bucket;               /* file pointer to /dev/null            */
212
213 char *table[] = { 
214         "^@", "^A", "^B", "^C", "^D", "^E", "^F", "^G", "^H", "\t", "^J", 
215         "^K", "^L", "^M", "^N", "^O", "^P", "^Q", "^R", "^S", "^T", "^U", 
216         "^V", "^W", "^X", "^Y", "^Z", "^[", "^\\", "^]", "^^", "^_"
217         };
218
219 WINDOW *com_win;
220 WINDOW *text_win;
221 WINDOW *help_win;
222 WINDOW *info_win;
223 WINDOW *count_win;
224
225 #if defined(__STDC__) || defined(__cplusplus)
226 #define P_(s) s
227 #else
228 #define P_(s) ()
229 #endif
230
231
232 /*
233  |      The following structure allows menu items to be flexibly declared.
234  |      The first item is the string describing the selection, the second 
235  |      is the address of the procedure to call when the item is selected,
236  |      and the third is the argument for the procedure.
237  |
238  |      For those systems with i18n, the string should be accompanied by a
239  |      catalog number.  The 'int *' should be replaced with 'void *' on 
240  |      systems with that type.
241  |
242  |      The first menu item will be the title of the menu, with NULL 
243  |      parameters for the procedure and argument, followed by the menu items.
244  |
245  |      If the procedure value is NULL, the menu item is displayed, but no 
246  |      procedure is called when the item is selected.  The number of the 
247  |      item will be returned.  If the third (argument) parameter is -1, no 
248  |      argument is given to the procedure when it is called.
249  */
250
251 struct menu_entries {
252         char *item_string;
253         int (*procedure)P_((struct menu_entries *));
254         struct menu_entries *ptr_argument;
255         int (*iprocedure)P_((int));
256         void (*nprocedure)P_((void));
257         int argument;
258         };
259
260 int main P_((int argc, char *argv[]));
261 unsigned char *resiz_line P_((int factor, struct text *rline, int rpos));
262 void insert P_((int character));
263 void delete P_((int disp));
264 void scanline P_((unsigned char *pos));
265 int tabshift P_((int temp_int));
266 int out_char P_((WINDOW *window, int character, int column));
267 int len_char P_((int character, int column));
268 void draw_line P_((int vertical, int horiz, unsigned char *ptr, int t_pos, int length));
269 void insert_line P_((int disp));
270 struct text *txtalloc P_((void));
271 struct files *name_alloc P_((void));
272 unsigned char *next_word P_((unsigned char *string));
273 void prev_word P_((void));
274 void control P_((void));
275 void emacs_control P_((void));
276 void bottom P_((void));
277 void top P_((void));
278 void nextline P_((void));
279 void prevline P_((void));
280 void left P_((int disp));
281 void right P_((int disp));
282 void find_pos P_((void));
283 void up P_((void));
284 void down P_((void));
285 void function_key P_((void));
286 void print_buffer P_((void));
287 void command_prompt P_((void));
288 void command P_((char *cmd_str1));
289 int scan P_((char *line, int offset, int column));
290 char *get_string P_((char *prompt, int advance));
291 int compare P_((char *string1, char *string2, int sensitive));
292 void goto_line P_((char *cmd_str));
293 void midscreen P_((int line, unsigned char *pnt));
294 void get_options P_((int numargs, char *arguments[]));
295 void check_fp P_((void));
296 void get_file P_((char *file_name));
297 void get_line P_((int length, unsigned char *in_string, int *append));
298 void draw_screen P_((void));
299 void finish P_((void));
300 int quit P_((int noverify));
301 void edit_abort P_((int arg));
302 void delete_text P_((void));
303 int write_file P_((char *file_name));
304 int search P_((int display_message));
305 void search_prompt P_((void));
306 void del_char P_((void));
307 void undel_char P_((void));
308 void del_word P_((void));
309 void undel_word P_((void));
310 void del_line P_((void));
311 void undel_line P_((void));
312 void adv_word P_((void));
313 void move_rel P_((char *direction, int lines));
314 void eol P_((void));
315 void bol P_((void));
316 void adv_line P_((void));
317 void sh_command P_((char *string));
318 void set_up_term P_((void));
319 void resize_check P_((void));
320 int menu_op P_((struct menu_entries *));
321 void paint_menu P_((struct menu_entries menu_list[], int max_width, int max_height, int list_size, int top_offset, WINDOW *menu_win, int off_start, int vert_size));
322 void help P_((void));
323 void paint_info_win P_((void));
324 void no_info_window P_((void));
325 void create_info_window P_((void));
326 int file_op P_((int arg));
327 void shell_op P_((void));
328 void leave_op P_((void));
329 void redraw P_((void));
330 int Blank_Line P_((struct text *test_line));
331 void Format P_((void));
332 void ee_init P_((void));
333 void dump_ee_conf P_((void));
334 void echo_string P_((char *string));
335 void spell_op P_((void));
336 void ispell_op P_((void));
337 int first_word_len P_((struct text *test_line));
338 void Auto_Format P_((void));
339 void modes_op P_((void));
340 char *is_in_string P_((char *string, char *substring));
341 char *resolve_name P_((char *name));
342 int restrict_mode P_((void));
343 int unique_test P_((char *string, char *list[]));
344 void renumber_lines P_((struct text *firstline, int startnumber));
345 void strings_init P_((void));
346
347 #undef P_
348 /*
349  |      allocate space here for the strings that will be in the menu
350  */
351
352 struct menu_entries modes_menu[] = {
353         {"", NULL, NULL, NULL, NULL, 0},        /* title                */
354         {"", NULL, NULL, NULL, NULL, -1},       /* 1. tabs to spaces    */
355         {"", NULL, NULL, NULL, NULL, -1},       /* 2. case sensitive search*/
356         {"", NULL, NULL, NULL, NULL, -1},       /* 3. margins observed  */
357         {"", NULL, NULL, NULL, NULL, -1},       /* 4. auto-paragraph    */
358         {"", NULL, NULL, NULL, NULL, -1},       /* 5. eightbit characters*/
359         {"", NULL, NULL, NULL, NULL, -1},       /* 6. info window       */
360         {"", NULL, NULL, NULL, NULL, -1},       /* 7. emacs key bindings*/
361         {"", NULL, NULL, NULL, NULL, -1},       /* 8. right margin      */
362         {"", NULL, NULL, NULL, NULL, -1},       /* 9. chinese text      */
363         {"", NULL, NULL, NULL, dump_ee_conf, -1}, /* 10. save editor config */
364         {NULL, NULL, NULL, NULL, NULL, -1}      /* terminator           */
365         };
366
367 char *mode_strings[11]; 
368
369 #define NUM_MODES_ITEMS 10
370
371 struct menu_entries config_dump_menu[] = {
372         {"", NULL, NULL, NULL, NULL, 0}, 
373         {"", NULL, NULL, NULL, NULL, -1},
374         {"", NULL, NULL, NULL, NULL, -1},
375         {NULL, NULL, NULL, NULL, NULL, -1}
376         };
377
378 struct menu_entries leave_menu[] = {
379         {"", NULL, NULL, NULL, NULL, -1}, 
380         {"", NULL, NULL, NULL, finish, -1}, 
381         {"", NULL, NULL, quit, NULL, TRUE}, 
382         {NULL, NULL, NULL, NULL, NULL, -1}
383         };
384
385 #define READ_FILE 1
386 #define WRITE_FILE 2
387 #define SAVE_FILE 3
388
389 struct menu_entries file_menu[] = {
390         {"", NULL, NULL, NULL, NULL, -1},
391         {"", NULL, NULL, file_op, NULL, READ_FILE},
392         {"", NULL, NULL, file_op, NULL, WRITE_FILE},
393         {"", NULL, NULL, file_op, NULL, SAVE_FILE},
394         {"", NULL, NULL, NULL, print_buffer, -1},
395         {NULL, NULL, NULL, NULL, NULL, -1}
396         };
397
398 struct menu_entries search_menu[] = {
399         {"", NULL, NULL, NULL, NULL, 0}, 
400         {"", NULL, NULL, NULL, search_prompt, -1},
401         {"", NULL, NULL, search, NULL, TRUE},
402         {NULL, NULL, NULL, NULL, NULL, -1}
403         };
404
405 struct menu_entries spell_menu[] = {
406         {"", NULL, NULL, NULL, NULL, -1}, 
407         {"", NULL, NULL, NULL, spell_op, -1},
408         {"", NULL, NULL, NULL, ispell_op, -1},
409         {NULL, NULL, NULL, NULL, NULL, -1}
410         };
411
412 struct menu_entries misc_menu[] = {
413         {"", NULL, NULL, NULL, NULL, -1}, 
414         {"", NULL, NULL, NULL, Format, -1},
415         {"", NULL, NULL, NULL, shell_op, -1}, 
416         {"", menu_op, spell_menu, NULL, NULL, -1}, 
417         {NULL, NULL, NULL, NULL, NULL, -1}
418         };
419
420 struct menu_entries main_menu[] = {
421         {"", NULL, NULL, NULL, NULL, -1}, 
422         {"", NULL, NULL, NULL, leave_op, -1}, 
423         {"", NULL, NULL, NULL, help, -1},
424         {"", menu_op, file_menu, NULL, NULL, -1}, 
425         {"", NULL, NULL, NULL, redraw, -1}, 
426         {"", NULL, NULL, NULL, modes_op, -1}, 
427         {"", menu_op, search_menu, NULL, NULL, -1}, 
428         {"", menu_op, misc_menu, NULL, NULL, -1}, 
429         {NULL, NULL, NULL, NULL, NULL, -1}
430         };
431
432 char *help_text[23];
433 char *control_keys[5];
434
435 char *emacs_help_text[22];
436 char *emacs_control_keys[5];
437
438 char *command_strings[5];
439 char *commands[32];
440 char *init_strings[22];
441
442 #define MENU_WARN 1
443
444 #define max_alpha_char 36
445
446 /*
447  |      Declarations for strings for localization
448  */
449
450 char *com_win_message;          /* to be shown in com_win if no info window */
451 char *no_file_string;
452 char *ascii_code_str;
453 char *printer_msg_str;
454 char *command_str;
455 char *file_write_prompt_str;
456 char *file_read_prompt_str;
457 char *char_str;
458 char *unkn_cmd_str;
459 char *non_unique_cmd_msg;
460 char *line_num_str;
461 char *line_len_str;
462 char *current_file_str;
463 char *usage0;
464 char *usage1;
465 char *usage2;
466 char *usage3;
467 char *usage4;
468 char *file_is_dir_msg;
469 char *new_file_msg;
470 char *cant_open_msg;
471 char *open_file_msg;
472 char *file_read_fin_msg;
473 char *reading_file_msg;
474 char *read_only_msg;
475 char *file_read_lines_msg;
476 char *save_file_name_prompt;
477 char *file_not_saved_msg;
478 char *changes_made_prompt;
479 char *yes_char;
480 char *file_exists_prompt;
481 char *create_file_fail_msg;
482 char *writing_file_msg;
483 char *file_written_msg;
484 char *searching_msg;
485 char *str_not_found_msg;
486 char *search_prompt_str;
487 char *exec_err_msg;
488 char *continue_msg;
489 char *menu_cancel_msg;
490 char *menu_size_err_msg;
491 char *press_any_key_msg;
492 char *shell_prompt;
493 char *formatting_msg;
494 char *shell_echo_msg;
495 char *spell_in_prog_msg;
496 char *margin_prompt;
497 char *restricted_msg;
498 char *ON;
499 char *OFF;
500 char *HELP;
501 char *WRITE;
502 char *READ;
503 char *LINE;
504 char *FILE_str;
505 char *CHARACTER;
506 char *REDRAW;
507 char *RESEQUENCE;
508 char *AUTHOR;
509 char *VERSION;
510 char *CASE;
511 char *NOCASE;
512 char *EXPAND;
513 char *NOEXPAND;
514 char *Exit_string;
515 char *QUIT_string;
516 char *INFO;
517 char *NOINFO;
518 char *MARGINS;
519 char *NOMARGINS;
520 char *AUTOFORMAT;
521 char *NOAUTOFORMAT;
522 char *Echo;
523 char *PRINTCOMMAND;
524 char *RIGHTMARGIN;
525 char *HIGHLIGHT;
526 char *NOHIGHLIGHT;
527 char *EIGHTBIT;
528 char *NOEIGHTBIT;
529 char *EMACS_string;
530 char *NOEMACS_string;
531 char *conf_dump_err_msg;
532 char *conf_dump_success_msg;
533 char *conf_not_saved_msg;
534 char *ree_no_file_msg;
535 char *cancel_string;
536 char *menu_too_lrg_msg;
537 char *more_above_str, *more_below_str;
538
539 char *chinese_cmd, *nochinese_cmd;
540
541 #ifndef __STDC__
542 #ifndef HAS_STDLIB
543 extern char *malloc();
544 extern char *realloc();
545 extern char *getenv();
546 FILE *fopen();                  /* declaration for open function        */
547 #endif /* HAS_STDLIB */
548 #endif /* __STDC__ */
549
550 int
551 main(int argc, char **argv)             /* beginning of main program            */
552 {
553         int counter;
554
555         for (counter = 1; counter < 24; counter++)
556                 signal(counter, SIG_IGN);
557
558         signal(SIGCHLD, SIG_DFL);
559         signal(SIGSEGV, SIG_DFL);
560         signal(SIGINT, edit_abort);
561         signal(SIGHUP, edit_abort);
562
563         d_char = malloc(3);     /* provide a buffer for multi-byte chars */
564         d_word = malloc(150);
565         *d_word = (char) NULL;
566         d_line = NULL;
567         dlt_line = txtalloc();
568         dlt_line->line = d_line;
569         dlt_line->line_length = 0;
570         curr_line = first_line = txtalloc();
571         curr_line->line = point = malloc(10);
572         curr_line->line_length = 1;
573         curr_line->max_length = 10;
574         curr_line->prev_line = NULL;
575         curr_line->next_line = NULL;
576         curr_line->line_number  = 1;
577         srch_str = NULL;
578         u_srch_str = NULL;
579         position = 1;
580         scr_pos =0;
581         scr_vert = 0;
582         scr_horz = 0;
583         bit_bucket = fopen(_PATH_DEVNULL, "w");
584         edit = TRUE;
585         gold = case_sen = FALSE;
586         shell_fork = TRUE;
587         strings_init();
588         ee_init();
589         if (argc > 0 )
590                 get_options(argc, argv);
591         set_up_term();
592         if (right_margin == 0)
593                 right_margin = COLS - 1;
594         if (top_of_stack == NULL)
595         {
596                 if (restrict_mode())
597                 {
598                         wmove(com_win, 0, 0);
599                         werase(com_win);
600                         wprintw(com_win, ree_no_file_msg);
601                         wrefresh(com_win);
602                         edit_abort(0);
603                 }
604                 wprintw(com_win, no_file_string);
605                 wrefresh(com_win);
606         }
607         else
608                 check_fp();
609
610         clear_com_win = TRUE;
611
612         while(edit) 
613         {
614                 if (info_window)
615                 {
616                         snprintf(count_text, count_text_len, "L: %d C: %d %s", \
617                                 curr_line->line_number, scr_horz + 1, count_text_default);
618                         wmove(count_win, 0, 0);
619                         if (!nohighlight)
620                                 wstandout(count_win);
621                         wprintw(count_win, count_text);
622                         wstandend(count_win);
623                         wnoutrefresh(count_win);
624                 }
625
626                 wnoutrefresh(text_win);
627                 doupdate();
628                 in = wgetch(text_win);
629                 if (in == -1)
630                         continue;
631
632                 resize_check();
633
634                 if (clear_com_win)
635                 {
636                         clear_com_win = FALSE;
637                         wmove(com_win, 0, 0);
638                         werase(com_win);
639                         if (!info_window)
640                         {
641                                 wprintw(com_win, "%s", com_win_message);
642                         }
643                         wrefresh(com_win);
644                 }
645
646                 if (in > 255)
647                         function_key();
648                 else if ((in == '\10') || (in == 127))
649                 {
650                         in = 8;         /* make sure key is set to backspace */
651                         delete(TRUE);
652                 }
653                 else if ((in > 31) || (in == 9))
654                         insert(in);
655                 else if ((in >= 0) && (in <= 31))
656                 {
657                         if (emacs_keys_mode)
658                                 emacs_control();
659                         else
660                                 control();
661                 }
662         }
663         return(0);
664 }
665
666 /* factor: resize factor */
667 /* rline: position in line */
668 unsigned char *
669 resiz_line(int factor, struct text *rline, int rpos)
670 /* resize the line to length + factor*/
671 {
672         unsigned char *rpoint;
673         int resiz_var;
674  
675         rline->max_length += factor;
676         rpoint = rline->line = realloc(rline->line, rline->max_length );
677         for (resiz_var = 1 ; (resiz_var < rpos) ; resiz_var++)
678                 rpoint++;
679         return(rpoint);
680 }
681
682 void 
683 insert(int character)           /* insert character into line           */
684 {
685         int counter;
686         int value;
687         unsigned char *temp;    /* temporary pointer                    */
688         unsigned char *temp2;   /* temporary pointer                    */
689
690         if ((character == '\011') && (expand_tabs))
691         {
692                 counter = len_char('\011', scr_horz);
693                 for (; counter > 0; counter--)
694                         insert(' ');
695                 if (auto_format)
696                         Auto_Format();
697                 return;
698         }
699         text_changes = TRUE;
700         if ((curr_line->max_length - curr_line->line_length) < 5)
701                 point = resiz_line(10, curr_line, position);
702         curr_line->line_length++;
703         temp = point;
704         counter = position;
705         while (counter < curr_line->line_length)        /* find end of line */
706         {
707                 counter++;
708                 temp++;
709         }
710         temp++;                 /* increase length of line by one       */
711         while (point < temp)
712         {
713                 temp2=temp - 1;
714                 *temp= *temp2;  /* shift characters over by one         */
715                 temp--;
716         }
717         *point = character;     /* insert new character                 */
718         wclrtoeol(text_win);
719         if (((character >= 0) && (character < ' ')) || (character >= 127)) /* check for TAB character*/
720         {
721                 scr_pos = scr_horz += out_char(text_win, character, scr_horz);
722                 point++;
723                 position++;
724         }
725         else
726         {
727                 waddch(text_win, character);
728                 scr_pos = ++scr_horz;
729                 point++;
730                 position ++;
731         }
732
733         if ((observ_margins) && (right_margin < scr_pos))
734         {
735                 counter = position;
736                 while (scr_pos > right_margin)
737                         prev_word();
738                 if (scr_pos == 0)
739                 {
740                         while (position < counter)
741                                 right(TRUE);
742                 }
743                 else
744                 {
745                         counter -= position;
746                         insert_line(TRUE);
747                         for (value = 0; value < counter; value++)
748                                 right(TRUE);
749                 }
750         }
751
752         if ((scr_horz - horiz_offset) > last_col)
753         {
754                 horiz_offset += 8;
755                 midscreen(scr_vert, point);
756         }
757
758         if ((auto_format) && (character == ' ') && (!formatted))
759                 Auto_Format();
760         else if ((character != ' ') && (character != '\t'))
761                 formatted = FALSE;
762
763         draw_line(scr_vert, scr_horz, point, position, curr_line->line_length);
764 }
765
766 void 
767 delete(int disp)                        /* delete character             */
768 {
769         unsigned char *tp;
770         unsigned char *temp2;
771         struct text *temp_buff;
772         int temp_vert;
773         int temp_pos;
774         int del_width = 1;
775
776         if (point != curr_line->line)   /* if not at beginning of line  */
777         {
778                 text_changes = TRUE;
779                 temp2 = tp = point;
780                 if ((ee_chinese) && (position >= 2) && (*(point - 2) > 127))
781                 {
782                         del_width = 2;
783                 }
784                 tp -= del_width;
785                 point -= del_width;
786                 position -= del_width;
787                 temp_pos = position;
788                 curr_line->line_length -= del_width;
789                 if ((*tp < ' ') || (*tp >= 127))        /* check for TAB */
790                         scanline(tp);
791                 else
792                         scr_horz -= del_width;
793                 scr_pos = scr_horz;
794                 if (in == 8)
795                 {
796                         if (del_width == 1)
797                                 *d_char = *point; /* save deleted character  */
798                         else
799                         {
800                                 d_char[0] = *point;
801                                 d_char[1] = *(point + 1);
802                         }
803                         d_char[del_width] = (unsigned char) NULL;
804                 }
805                 while (temp_pos <= curr_line->line_length)
806                 {
807                         temp_pos++;
808                         *tp = *temp2;
809                         tp++;
810                         temp2++;
811                 }
812                 if (scr_horz < horiz_offset)
813                 {
814                         horiz_offset -= 8;
815                         midscreen(scr_vert, point);
816                 }
817         }
818         else if (curr_line->prev_line != NULL)
819         {
820                 text_changes = TRUE;
821                 left(disp);                     /* go to previous line  */
822                 temp_buff = curr_line->next_line;
823                 point = resiz_line(temp_buff->line_length, curr_line, position);
824                 if (temp_buff->next_line != NULL)
825                         temp_buff->next_line->prev_line = curr_line;
826                 curr_line->next_line = temp_buff->next_line;
827                 renumber_lines(curr_line->next_line, curr_line->line_number + 1);
828                 temp2 = temp_buff->line;
829                 if (in == 8)
830                 {
831                         d_char[0] = '\n';
832                         d_char[1] = (unsigned char) NULL;
833                 }
834                 tp = point;
835                 temp_pos = 1;
836                 while (temp_pos < temp_buff->line_length)
837                 {
838                         curr_line->line_length++;
839                         temp_pos++;
840                         *tp = *temp2;
841                         tp++;
842                         temp2++;
843                 }
844                 *tp = (char) NULL;
845                 free(temp_buff->line);
846                 free(temp_buff);
847                 temp_buff = curr_line;
848                 temp_vert = scr_vert;
849                 scr_pos = scr_horz;
850                 if (scr_vert < last_line)
851                 {
852                         wmove(text_win, scr_vert + 1, 0);
853                         wdeleteln(text_win);
854                 }
855                 while ((temp_buff != NULL) && (temp_vert < last_line))
856                 {
857                         temp_buff = temp_buff->next_line;
858                         temp_vert++;
859                 }
860                 if ((temp_vert == last_line) && (temp_buff != NULL))
861                 {
862                         tp = temp_buff->line;
863                         wmove(text_win, last_line,0);
864                         wclrtobot(text_win);
865                         draw_line(last_line, 0, tp, 1, temp_buff->line_length);
866                         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
867                 }
868         }
869         draw_line(scr_vert, scr_horz, point, position, curr_line->line_length);
870         formatted = FALSE;
871 }
872
873 void 
874 scanline(unsigned char *pos)    /* find the proper horizontal position for the pointer  */
875 {
876         int temp;
877         unsigned char *ptr;
878
879         ptr = curr_line->line;
880         temp = 0;
881         while (ptr < pos)
882         {
883                 if (*ptr <= 8)
884                         temp += 2;
885                 else if (*ptr == 9)
886                         temp += tabshift(temp);
887                 else if ((*ptr >= 10) && (*ptr <= 31))
888                         temp += 2;
889                 else if ((*ptr >= 32) && (*ptr < 127))
890                         temp++;
891                 else if (*ptr == 127)
892                         temp += 2;
893                 else if (!eightbit)
894                         temp += 5;
895                 else
896                         temp++;
897                 ptr++;
898         }
899         scr_horz = temp;
900         if ((scr_horz - horiz_offset) > last_col)
901         {
902                 horiz_offset = (scr_horz - (scr_horz % 8)) - (COLS - 8);
903                 midscreen(scr_vert, point);
904         }
905         else if (scr_horz < horiz_offset)
906         {
907                 horiz_offset = max(0, (scr_horz - (scr_horz % 8)));
908                 midscreen(scr_vert, point);
909         }
910 }
911
912 int 
913 tabshift(int temp_int)          /* give the number of spaces to shift   */
914 {
915         int leftover;
916
917         leftover = ((temp_int + 1) % 8);
918         if (leftover == 0)
919                 return (1);
920         else
921                 return (9 - leftover);
922 }
923
924 int 
925 out_char(WINDOW *window, int character, int column)
926 /* output non-printing character */
927 {
928         int i1, i2;
929         unsigned char *string;
930         char string2[8];
931
932         if (character == TAB)
933         {
934                 i1 = tabshift(column);
935                 for (i2 = 0; 
936                   (i2 < i1) && (((column+i2+1)-horiz_offset) < last_col); i2++)
937                 {
938                         waddch(window, ' ');
939                 }
940                 return(i1);
941         }
942         else if ((character >= '\0') && (character < ' '))
943         {
944                 string = table[(int) character];
945         }
946         else if ((character < 0) || (character >= 127))
947         {
948                 if (character == 127)
949                         string = "^?";
950                 else if (!eightbit)
951                 {
952                         sprintf(string2, "<%d>", (character < 0) ? (character + 256) : character);
953                         string = string2;
954                 }
955                 else
956                 {
957                         waddch(window, (unsigned char)character );
958                         return(1);
959                 }
960         }
961         else
962         {
963                 waddch(window, (unsigned char)character);
964                 return(1);
965         }
966         for (i2 = 0; (string[i2] != (char) NULL) && (((column+i2+1)-horiz_offset) < last_col); i2++)
967                 waddch(window, string[i2]);
968         return(strlen(string));
969 }
970
971 /* column: the column must be known to provide spacing for tabs */
972 int
973 len_char(int character, int column)     /* return the length of the character   */
974 {
975         int length;
976
977         if (character == '\t')
978                 length = tabshift(column);
979         else if ((character >= 0) && (character < 32))
980                 length = 2;
981         else if ((character >= 32) && (character <= 126))
982                 length = 1;
983         else if (character == 127)
984                 length = 2;
985         else if (((character > 126) || (character < 0)) && (!eightbit))
986                 length = 5;
987         else
988                 length = 1;
989
990         return(length);
991 }
992
993 /*
994 vertical: current vertical position on screen
995 horiz:  current horizontal position on screen
996 ptr:    pointer to line
997 t_pos:  current position (offset in bytes) from bol
998 length: length (in bytes) of line
999 */
1000 void
1001 draw_line(int vertical, int horiz, unsigned char *ptr, int t_pos, int length)
1002 /* redraw line from current position */
1003 {
1004         int d;          /* partial length of special or tab char to display  */
1005         unsigned char *temp;    /* temporary pointer to position in line             */
1006         int abs_column; /* offset in screen units from begin of line         */
1007         int column;     /* horizontal position on screen                     */
1008         int row;        /* vertical position on screen                       */
1009         int posit;      /* temporary position indicator within line          */
1010
1011         abs_column = horiz;
1012         column = horiz - horiz_offset;
1013         row = vertical;
1014         temp = ptr;
1015         d = 0;
1016         posit = t_pos;
1017         if (column < 0)
1018         {
1019                 wmove(text_win, row, 0);
1020                 wclrtoeol(text_win);
1021         }
1022         while (column < 0)
1023         {
1024                 d = len_char(*temp, abs_column);
1025                 abs_column += d;
1026                 column += d;
1027                 posit++;
1028                 temp++;
1029         }
1030         wmove(text_win, row, column);
1031         wclrtoeol(text_win);
1032         while ((posit < length) && (column <= last_col))
1033         {
1034                 if ((*temp < 32) || (*temp >= 127))
1035                 {
1036                         column += len_char(*temp, abs_column);
1037                         abs_column += out_char(text_win, *temp, abs_column);
1038                 }
1039                 else
1040                 {
1041                         abs_column++;
1042                         column++;
1043                         waddch(text_win, *temp);
1044                 }
1045                 posit++;
1046                 temp++;
1047         }
1048         if (column < last_col)
1049                 wclrtoeol(text_win);
1050         wmove(text_win, vertical, (horiz - horiz_offset));
1051 }
1052
1053 void 
1054 insert_line(int disp)                   /* insert new line              */
1055 {
1056         int temp_pos;
1057         int temp_pos2;
1058         unsigned char *temp;
1059         unsigned char *extra;
1060         struct text *temp_nod;
1061
1062         text_changes = TRUE;
1063         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1064         wclrtoeol(text_win);
1065         temp_nod= txtalloc();
1066         temp_nod->line = extra= malloc(10);
1067         temp_nod->line_length = 1;
1068         temp_nod->max_length = 10;
1069         temp_nod->next_line = curr_line->next_line;
1070         renumber_lines(temp_nod, curr_line->line_number + 1);
1071         if (temp_nod->next_line != NULL)
1072                 temp_nod->next_line->prev_line = temp_nod;
1073         temp_nod->prev_line = curr_line;
1074         curr_line->next_line = temp_nod;
1075         temp_pos2 = position;
1076         temp = point;
1077         if (temp_pos2 < curr_line->line_length)
1078         {
1079                 temp_pos = 1;
1080                 while (temp_pos2 < curr_line->line_length)
1081                 {
1082                         if ((temp_nod->max_length - temp_nod->line_length)< 5)
1083                                 extra = resiz_line(10, temp_nod, temp_pos);
1084                         temp_nod->line_length++;
1085                         temp_pos++;
1086                         temp_pos2++;
1087                         *extra= *temp;
1088                         extra++;
1089                         temp++;
1090                 }
1091                 temp=point;
1092                 *temp = (char) NULL;
1093                 temp = resiz_line((1 - temp_nod->line_length), curr_line, position);
1094                 curr_line->line_length = 1 + temp - curr_line->line;
1095         }
1096         curr_line->line_length = position;
1097         curr_line = temp_nod;
1098         *extra = (char) NULL;
1099         position = 1;
1100         point= curr_line->line;
1101         if (disp)
1102         {
1103                 if (scr_vert < last_line)
1104                 {
1105                         scr_vert++;
1106                         wclrtoeol(text_win);
1107                         wmove(text_win, scr_vert, 0);
1108                         winsertln(text_win);
1109                 }
1110                 else
1111                 {
1112                         wmove(text_win, 0,0);
1113                         wdeleteln(text_win);
1114                         wmove(text_win, last_line,0);
1115                         wclrtobot(text_win);
1116                 }
1117                 scr_pos = scr_horz = 0;
1118                 if (horiz_offset)
1119                 {
1120                         horiz_offset = 0;
1121                         midscreen(scr_vert, point);
1122                 }
1123                 draw_line(scr_vert, scr_horz, point, position,
1124                         curr_line->line_length);
1125         }
1126 }
1127
1128 struct text *txtalloc(void)             /* allocate space for line structure    */
1129 {
1130         return((struct text *) malloc(sizeof( struct text)));
1131 }
1132
1133 struct files *name_alloc(void)  /* allocate space for file name list node */
1134 {
1135         return((struct files *) malloc(sizeof( struct files)));
1136 }
1137
1138 unsigned char *next_word(unsigned char *string)
1139 /* move to next word in string */
1140 {
1141         while ((*string != (char) NULL) && ((*string != 32) && (*string != 9)))
1142                 string++;
1143         while ((*string != (char) NULL) && ((*string == 32) || (*string == 9)))
1144                 string++;
1145         return(string);
1146 }
1147
1148 void 
1149 prev_word(void) /* move to start of previous word in text       */
1150 {
1151         if (position != 1)
1152         {
1153                 if ((position != 1) && ((point[-1] == ' ') || (point[-1] == '\t')))
1154                 {       /* if at the start of a word    */
1155                         while ((position != 1) && ((*point != ' ') && (*point != '\t')))
1156                                 left(TRUE);
1157                 }
1158                 while ((position != 1) && ((*point == ' ') || (*point == '\t')))
1159                         left(TRUE);
1160                 while ((position != 1) && ((*point != ' ') && (*point != '\t')))
1161                         left(TRUE);
1162                 if ((position != 1) && ((*point == ' ') || (*point == '\t')))
1163                         right(TRUE);
1164         }
1165         else
1166                 left(TRUE);
1167 }
1168
1169 void 
1170 control(void)                   /* use control for commands             */
1171 {
1172         char *string;
1173
1174         if (in == 1)            /* control a    */
1175         {
1176                 string = get_string(ascii_code_str, TRUE);
1177                 if (*string != (char) NULL)
1178                 {
1179                         in = atoi(string);
1180                         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1181                         insert(in);
1182                 }
1183                 free(string);
1184         }
1185         else if (in == 2)       /* control b    */
1186                 bottom();
1187         else if (in == 3)       /* control c    */
1188         {
1189                 command_prompt();
1190         }
1191         else if (in == 4)       /* control d    */
1192                 down();
1193         else if (in == 5)       /* control e    */
1194                 search_prompt();
1195         else if (in == 6)       /* control f    */
1196                 undel_char();
1197         else if (in == 7)       /* control g    */
1198                 bol();
1199         else if (in == 8)       /* control h    */
1200                 delete(TRUE);
1201         else if (in == 9)       /* control i    */
1202                 ;
1203         else if (in == 10)      /* control j    */
1204                 insert_line(TRUE);
1205         else if (in == 11)      /* control k    */
1206                 del_char();
1207         else if (in == 12)      /* control l    */
1208                 left(TRUE);
1209         else if (in == 13)      /* control m    */
1210                 insert_line(TRUE);
1211         else if (in == 14)      /* control n    */
1212                 move_rel("d", max(5, (last_line - 5)));
1213         else if (in == 15)      /* control o    */
1214                 eol();
1215         else if (in == 16)      /* control p    */
1216                 move_rel("u", max(5, (last_line - 5)));
1217         else if (in == 17)      /* control q    */
1218                 ;
1219         else if (in == 18)      /* control r    */
1220                 right(TRUE);
1221         else if (in == 19)      /* control s    */
1222                 ;
1223         else if (in == 20)      /* control t    */
1224                 top();
1225         else if (in == 21)      /* control u    */
1226                 up();
1227         else if (in == 22)      /* control v    */
1228                 undel_word();
1229         else if (in == 23)      /* control w    */
1230                 del_word();
1231         else if (in == 24)      /* control x    */
1232                 search(TRUE);
1233         else if (in == 25)      /* control y    */
1234                 del_line();
1235         else if (in == 26)      /* control z    */
1236                 undel_line();
1237         else if (in == 27)      /* control [ (escape)   */
1238         {
1239                 menu_op(main_menu);
1240         }       
1241 }
1242
1243 /*
1244  |      Emacs control-key bindings
1245  */
1246
1247 void 
1248 emacs_control(void)
1249 {
1250         char *string;
1251
1252         if (in == 1)            /* control a    */
1253                 bol();
1254         else if (in == 2)       /* control b    */
1255                 left(TRUE);
1256         else if (in == 3)       /* control c    */
1257         {
1258                 command_prompt();
1259         }
1260         else if (in == 4)       /* control d    */
1261                 del_char();
1262         else if (in == 5)       /* control e    */
1263                 eol();
1264         else if (in == 6)       /* control f    */
1265                 right(TRUE);
1266         else if (in == 7)       /* control g    */
1267                 move_rel("u", max(5, (last_line - 5)));
1268         else if (in == 8)       /* control h    */
1269                 delete(TRUE);
1270         else if (in == 9)       /* control i    */
1271                 ;
1272         else if (in == 10)      /* control j    */
1273                 undel_char();
1274         else if (in == 11)      /* control k    */
1275                 del_line();
1276         else if (in == 12)      /* control l    */
1277                 undel_line();
1278         else if (in == 13)      /* control m    */
1279                 insert_line(TRUE);
1280         else if (in == 14)      /* control n    */
1281                 down();
1282         else if (in == 15)      /* control o    */
1283         {
1284                 string = get_string(ascii_code_str, TRUE);
1285                 if (*string != (char) NULL)
1286                 {
1287                         in = atoi(string);
1288                         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1289                         insert(in);
1290                 }
1291                 free(string);
1292         }
1293         else if (in == 16)      /* control p    */
1294                 up();
1295         else if (in == 17)      /* control q    */
1296                 ;
1297         else if (in == 18)      /* control r    */
1298                 undel_word();
1299         else if (in == 19)      /* control s    */
1300                 ;
1301         else if (in == 20)      /* control t    */
1302                 top();
1303         else if (in == 21)      /* control u    */
1304                 bottom();
1305         else if (in == 22)      /* control v    */
1306                 move_rel("d", max(5, (last_line - 5)));
1307         else if (in == 23)      /* control w    */
1308                 del_word();
1309         else if (in == 24)      /* control x    */
1310                 search(TRUE);
1311         else if (in == 25)      /* control y    */
1312                 search_prompt();
1313         else if (in == 26)      /* control z    */
1314                 adv_word();
1315         else if (in == 27)      /* control [ (escape)   */
1316         {
1317                 menu_op(main_menu);
1318         }       
1319 }
1320
1321 void 
1322 bottom(void)                    /* go to bottom of file                 */
1323 {
1324         while (curr_line->next_line != NULL)
1325                 curr_line = curr_line->next_line;
1326         point = curr_line->line;
1327         if (horiz_offset)
1328                 horiz_offset = 0;
1329         position = 1;
1330         midscreen(last_line, point);
1331         scr_pos = scr_horz;
1332 }
1333
1334 void 
1335 top(void)                               /* go to top of file                    */
1336 {
1337         while (curr_line->prev_line != NULL)
1338                 curr_line = curr_line->prev_line;
1339         point = curr_line->line;
1340         if (horiz_offset)
1341                 horiz_offset = 0;
1342         position = 1;
1343         midscreen(0, point);
1344         scr_pos = scr_horz;
1345 }
1346
1347 void 
1348 nextline(void)                  /* move pointers to start of next line  */
1349 {
1350         curr_line = curr_line->next_line;
1351         point = curr_line->line;
1352         position = 1;
1353         if (scr_vert == last_line)
1354         {
1355                 wmove(text_win, 0,0);
1356                 wdeleteln(text_win);
1357                 wmove(text_win, last_line,0);
1358                 wclrtobot(text_win);
1359                 draw_line(last_line,0,point,1,curr_line->line_length);
1360         }
1361         else
1362                 scr_vert++;
1363 }
1364
1365 void 
1366 prevline(void)                  /* move pointers to start of previous line*/
1367 {
1368         curr_line = curr_line->prev_line;
1369         point = curr_line->line;
1370         position = 1;
1371         if (scr_vert == 0)
1372         {
1373                 winsertln(text_win);
1374                 draw_line(0,0,point,1,curr_line->line_length);
1375         }
1376         else
1377                 scr_vert--;
1378         while (position < curr_line->line_length)
1379         {
1380                 position++;
1381                 point++;
1382         }
1383 }
1384
1385 void 
1386 left(int disp)                          /* move left one character      */
1387 {
1388         if (point != curr_line->line)   /* if not at begin of line      */
1389         {
1390                 if ((ee_chinese) && (position >= 2) && (*(point - 2) > 127))
1391                 {
1392                         point--;
1393                         position--;
1394                 }
1395                 point--;
1396                 position--;
1397                 scanline(point);
1398                 wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1399                 scr_pos = scr_horz;
1400         }
1401         else if (curr_line->prev_line != NULL)
1402         {
1403                 if (!disp)
1404                 {
1405                         curr_line = curr_line->prev_line;
1406                         point = curr_line->line + curr_line->line_length;
1407                         position = curr_line->line_length;
1408                         return;
1409                 }
1410                 position = 1;
1411                 prevline();
1412                 scanline(point);
1413                 scr_pos = scr_horz;
1414                 wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1415         }
1416 }
1417
1418 void 
1419 right(int disp)                         /* move right one character     */
1420 {
1421         if (position < curr_line->line_length)
1422         {
1423                 if ((ee_chinese) && (*point > 127) && 
1424                     ((curr_line->line_length - position) >= 2))
1425                 {
1426                         point++;
1427                         position++;
1428                 }
1429                 point++;
1430                 position++;
1431                 scanline(point);
1432                 wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1433                 scr_pos = scr_horz;
1434         }
1435         else if (curr_line->next_line != NULL)
1436         {
1437                 if (!disp)
1438                 {
1439                         curr_line = curr_line->next_line;
1440                         point = curr_line->line;
1441                         position = 1;
1442                         return;
1443                 }
1444                 nextline();
1445                 scr_pos = scr_horz = 0;
1446                 if (horiz_offset)
1447                 {
1448                         horiz_offset = 0;
1449                         midscreen(scr_vert, point);
1450                 }
1451                 wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1452                 position = 1;   
1453         }
1454 }
1455
1456 void 
1457 find_pos(void)          /* move to the same column as on other line     */
1458 {
1459         scr_horz = 0;
1460         position = 1;
1461         while ((scr_horz < scr_pos) && (position < curr_line->line_length))
1462         {
1463                 if (*point == 9)
1464                         scr_horz += tabshift(scr_horz);
1465                 else if (*point < ' ')
1466                         scr_horz += 2;
1467                 else if ((ee_chinese) && (*point > 127) && 
1468                     ((curr_line->line_length - position) >= 2))
1469                 {
1470                         scr_horz += 2;
1471                         point++;
1472                         position++;
1473                 }
1474                 else
1475                         scr_horz++;
1476                 position++;
1477                 point++;
1478         }
1479         if ((scr_horz - horiz_offset) > last_col)
1480         {
1481                 horiz_offset = (scr_horz - (scr_horz % 8)) - (COLS - 8);
1482                 midscreen(scr_vert, point);
1483         }
1484         else if (scr_horz < horiz_offset)
1485         {
1486                 horiz_offset = max(0, (scr_horz - (scr_horz % 8)));
1487                 midscreen(scr_vert, point);
1488         }
1489         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1490 }
1491
1492 void 
1493 up(void)                                        /* move up one line             */
1494 {
1495         if (curr_line->prev_line != NULL)
1496         {
1497                 prevline();
1498                 point = curr_line->line;
1499                 find_pos();
1500         }
1501 }
1502
1503 void 
1504 down(void)                                      /* move down one line           */
1505 {
1506         if (curr_line->next_line != NULL)
1507         {
1508                 nextline();
1509                 find_pos();
1510         }
1511 }
1512
1513 void 
1514 function_key(void)                              /* process function key         */
1515 {
1516         if (in == KEY_LEFT)
1517                 left(TRUE);
1518         else if (in == KEY_RIGHT)
1519                 right(TRUE);
1520         else if (in == KEY_HOME)
1521                 bol();
1522         else if (in == KEY_END)
1523                 eol();
1524         else if ( in == KEY_UP)
1525                 up();
1526         else if (in == KEY_DOWN)
1527                 down();
1528         else if (in == KEY_NPAGE)
1529                 move_rel("d", max( 5, (last_line - 5)));
1530         else if (in == KEY_PPAGE)
1531                 move_rel("u", max(5, (last_line - 5)));
1532         else if (in == KEY_DL)
1533                 del_line();
1534         else if (in == KEY_DC)
1535                 del_char();
1536         else if (in == KEY_BACKSPACE)
1537                 delete(TRUE);
1538         else if (in == KEY_IL)
1539         {               /* insert a line before current line    */
1540                 insert_line(TRUE);
1541                 left(TRUE);
1542         }
1543         else if (in == KEY_F(1))
1544                 gold = !gold;
1545         else if (in == KEY_F(2))
1546         {
1547                 if (gold)
1548                 {
1549                         gold = FALSE;
1550                         undel_line();
1551                 }
1552                 else
1553                         undel_char();
1554         }
1555         else if (in == KEY_F(3))
1556         {
1557                 if (gold)
1558                 {
1559                         gold = FALSE;
1560                         undel_word();
1561                 }
1562                 else
1563                         del_word();
1564         }
1565         else if (in == KEY_F(4))
1566         {
1567                 if (gold)
1568                 {
1569                         gold = FALSE;
1570                         paint_info_win();
1571                         midscreen(scr_vert, point);
1572                 }
1573                 else
1574                         adv_word();
1575         }
1576         else if (in == KEY_F(5))
1577         {
1578                 if (gold)
1579                 {
1580                         gold = FALSE;
1581                         search_prompt();
1582                 }
1583                 else
1584                         search(TRUE);
1585         }
1586         else if (in == KEY_F(6))
1587         {
1588                 if (gold)
1589                 {
1590                         gold = FALSE;
1591                         bottom();
1592                 }
1593                 else
1594                         top();
1595         }
1596         else if (in == KEY_F(7))
1597         {
1598                 if (gold)
1599                 {
1600                         gold = FALSE;
1601                         eol();
1602                 }
1603                 else
1604                         bol();
1605         }
1606         else if (in == KEY_F(8))
1607         {
1608                 if (gold)
1609                 {
1610                         gold = FALSE;
1611                         command_prompt();
1612                 } 
1613                 else
1614                         adv_line();
1615         }
1616 }
1617
1618 void 
1619 print_buffer(void)
1620 {
1621         char buffer[256];
1622
1623         sprintf(buffer, ">!%s", print_command);
1624         wmove(com_win, 0, 0);
1625         wclrtoeol(com_win);
1626         wprintw(com_win, printer_msg_str, print_command);
1627         wrefresh(com_win);
1628         command(buffer);
1629 }
1630
1631 void 
1632 command_prompt(void)
1633 {
1634         char *cmd_str;
1635         int result;
1636
1637         info_type = COMMANDS;
1638         paint_info_win();
1639         cmd_str = get_string(command_str, TRUE);
1640         if ((result = unique_test(cmd_str, commands)) != 1)
1641         {
1642                 werase(com_win);
1643                 wmove(com_win, 0, 0);
1644                 if (result == 0)
1645                         wprintw(com_win, unkn_cmd_str, cmd_str);
1646                 else
1647                         wprintw(com_win, non_unique_cmd_msg);
1648
1649                 wrefresh(com_win);
1650
1651                 info_type = CONTROL_KEYS;
1652                 paint_info_win();
1653
1654                 if (cmd_str != NULL)
1655                         free(cmd_str);
1656                 return;
1657         }
1658         command(cmd_str);
1659         wrefresh(com_win);
1660         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
1661         info_type = CONTROL_KEYS;
1662         paint_info_win();
1663         if (cmd_str != NULL)
1664                 free(cmd_str);
1665 }
1666
1667 void 
1668 command(char *cmd_str1)         /* process commands from keyboard       */
1669 {
1670         char *cmd_str2 = NULL;
1671         char *cmd_str = cmd_str1;
1672
1673         clear_com_win = TRUE;
1674         if (compare(cmd_str, HELP, FALSE))
1675                 help();
1676         else if (compare(cmd_str, WRITE, FALSE))
1677         {
1678                 if (restrict_mode())
1679                 {
1680                         return;
1681                 }
1682                 cmd_str = next_word(cmd_str);
1683                 if (*cmd_str == (char) NULL)
1684                 {
1685                         cmd_str = cmd_str2 = get_string(file_write_prompt_str, TRUE);
1686                 }
1687                 tmp_file = resolve_name(cmd_str);
1688                 write_file(tmp_file);
1689                 if (tmp_file != cmd_str)
1690                         free(tmp_file);
1691         }
1692         else if (compare(cmd_str, READ, FALSE))
1693         {
1694                 if (restrict_mode())
1695                 {
1696                         return;
1697                 }
1698                 cmd_str = next_word(cmd_str);
1699                 if (*cmd_str == (char) NULL)
1700                 {
1701                         cmd_str = cmd_str2 = get_string(file_read_prompt_str, TRUE);
1702                 }
1703                 tmp_file = cmd_str;
1704                 recv_file = TRUE;
1705                 tmp_file = resolve_name(cmd_str);
1706                 check_fp();
1707                 if (tmp_file != cmd_str)
1708                         free(tmp_file);
1709         }
1710         else if (compare(cmd_str, LINE, FALSE))
1711         {
1712                 wmove(com_win, 0, 0);
1713                 wclrtoeol(com_win);
1714                 wprintw(com_win, line_num_str, curr_line->line_number);
1715                 wprintw(com_win, line_len_str, curr_line->line_length);
1716         }
1717         else if (compare(cmd_str, FILE_str, FALSE))
1718         {
1719                 wmove(com_win, 0, 0);
1720                 wclrtoeol(com_win);
1721                 if (in_file_name == NULL)
1722                         wprintw(com_win, no_file_string);
1723                 else
1724                         wprintw(com_win, current_file_str, in_file_name);
1725         }
1726         else if ((*cmd_str >= '0') && (*cmd_str <= '9'))
1727                 goto_line(cmd_str);
1728         else if (compare(cmd_str, CHARACTER, FALSE))
1729         {
1730                 wmove(com_win, 0, 0);
1731                 wclrtoeol(com_win);
1732                 wprintw(com_win, char_str, *point);
1733         }
1734         else if (compare(cmd_str, REDRAW, FALSE))
1735                 redraw();
1736         else if (compare(cmd_str, RESEQUENCE, FALSE))
1737         {
1738                 tmp_line = first_line->next_line;
1739                 while (tmp_line != NULL)
1740                 {
1741                 tmp_line->line_number = tmp_line->prev_line->line_number + 1;
1742                         tmp_line = tmp_line->next_line;
1743                 }
1744         }
1745         else if (compare(cmd_str, AUTHOR, FALSE))
1746         {
1747                 wmove(com_win, 0, 0);
1748                 wclrtoeol(com_win);
1749                 wprintw(com_win, "written by Hugh Mahon");
1750         }
1751         else if (compare(cmd_str, VERSION, FALSE))
1752         {
1753                 wmove(com_win, 0, 0);
1754                 wclrtoeol(com_win);
1755                 wprintw(com_win, "%s", version);
1756         }
1757         else if (compare(cmd_str, CASE, FALSE))
1758                 case_sen = TRUE;
1759         else if (compare(cmd_str, NOCASE, FALSE))
1760                 case_sen = FALSE;
1761         else if (compare(cmd_str, EXPAND, FALSE))
1762                 expand_tabs = TRUE;
1763         else if (compare(cmd_str, NOEXPAND, FALSE))
1764                 expand_tabs = FALSE;
1765         else if (compare(cmd_str, Exit_string, FALSE))
1766                 finish();
1767         else if (compare(cmd_str, chinese_cmd, FALSE))
1768         {
1769                 ee_chinese = TRUE;
1770 #ifdef NCURSE
1771                 nc_setattrib(A_NC_BIG5);
1772 #endif /* NCURSE */
1773         }
1774         else if (compare(cmd_str, nochinese_cmd, FALSE))
1775         {
1776                 ee_chinese = FALSE;
1777 #ifdef NCURSE
1778                 nc_clearattrib(A_NC_BIG5);
1779 #endif /* NCURSE */
1780         }
1781         else if (compare(cmd_str, QUIT_string, FALSE))
1782                 quit(0);
1783         else if (*cmd_str == '!')
1784         {
1785                 cmd_str++;
1786                 if ((*cmd_str == ' ') || (*cmd_str == 9))
1787                         cmd_str = next_word(cmd_str);
1788                 sh_command(cmd_str);
1789         }
1790         else if ((*cmd_str == '<') && (!in_pipe))
1791         {
1792                 in_pipe = TRUE;
1793                 shell_fork = FALSE;
1794                 cmd_str++;
1795                 if ((*cmd_str == ' ') || (*cmd_str == '\t'))
1796                         cmd_str = next_word(cmd_str);
1797                 command(cmd_str);
1798                 in_pipe = FALSE;
1799                 shell_fork = TRUE;
1800         }
1801         else if ((*cmd_str == '>') && (!out_pipe))
1802         {
1803                 out_pipe = TRUE;
1804                 cmd_str++;
1805                 if ((*cmd_str == ' ') || (*cmd_str == '\t'))
1806                         cmd_str = next_word(cmd_str);
1807                 command(cmd_str);
1808                 out_pipe = FALSE;
1809         }
1810         else
1811         {
1812                 wmove(com_win, 0, 0);
1813                 wclrtoeol(com_win);
1814                 wprintw(com_win, unkn_cmd_str, cmd_str);
1815         }
1816         if (cmd_str2 != NULL)
1817                 free(cmd_str2);
1818 }
1819
1820 int 
1821 scan(char *line, int offset, int column)
1822 /* determine horizontal position for get_string */
1823 {
1824         char *stemp;
1825         int i;
1826         int j;
1827
1828         stemp = line;
1829         i = 0;
1830         j = column;
1831         while (i < offset)
1832         {
1833                 i++;
1834                 j += len_char(*stemp, j);
1835                 stemp++;
1836         }
1837         return(j);
1838 }
1839
1840 /*
1841 prompt: string containing user prompt message
1842 advance: if true, skip leading spaces and tabs
1843 */
1844 char *
1845 get_string(char *prompt, int advance)
1846 /* read string from input on command line */
1847 {
1848         char *string;
1849         char *tmp_string;
1850         char *nam_str;
1851         char *g_point;
1852         int tmp_int;
1853         int g_horz, g_position, g_pos;
1854         int esc_flag;
1855
1856         g_point = tmp_string = malloc(512);
1857         wmove(com_win,0,0);
1858         wclrtoeol(com_win);
1859         waddstr(com_win, prompt);
1860         wrefresh(com_win);
1861         nam_str = tmp_string;
1862         clear_com_win = TRUE;
1863         g_horz = g_position = scan(prompt, strlen(prompt), 0);
1864         g_pos = 0;
1865         do
1866         {
1867                 esc_flag = FALSE;
1868                 in = wgetch(com_win);
1869                 if (in == -1)
1870                         continue;
1871                 if (((in == 8) || (in == 127) || (in == KEY_BACKSPACE)) && (g_pos > 0))
1872                 {
1873                         tmp_int = g_horz;
1874                         g_pos--;
1875                         g_horz = scan(g_point, g_pos, g_position);
1876                         tmp_int = tmp_int - g_horz;
1877                         for (; 0 < tmp_int; tmp_int--)
1878                         {
1879                                 if ((g_horz+tmp_int) < (last_col - 1))
1880                                 {
1881                                         waddch(com_win, '\010');
1882                                         waddch(com_win, ' ');
1883                                         waddch(com_win, '\010');
1884                                 }
1885                         }
1886                         nam_str--;
1887                 }
1888                 else if ((in != 8) && (in != 127) && (in != '\n') && (in != '\r') && (in < 256))
1889                 {
1890                         if (in == '\026')       /* control-v, accept next character verbatim    */
1891                         {                       /* allows entry of ^m, ^j, and ^h       */
1892                                 esc_flag = TRUE;
1893                                 in = wgetch(com_win);
1894                                 if (in == -1)
1895                                         continue;
1896                         }
1897                         *nam_str = in;
1898                         g_pos++;
1899                         if (((in < ' ') || (in > 126)) && (g_horz < (last_col - 1)))
1900                                 g_horz += out_char(com_win, in, g_horz);
1901                         else
1902                         {
1903                                 g_horz++;
1904                                 if (g_horz < (last_col - 1))
1905                                         waddch(com_win, in);
1906                         }
1907                         nam_str++;
1908                 }
1909                 wrefresh(com_win);
1910                 if (esc_flag)
1911                         in = (char) NULL;
1912         } while ((in != '\n') && (in != '\r'));
1913         *nam_str = (char) NULL;
1914         nam_str = tmp_string;
1915         if (((*nam_str == ' ') || (*nam_str == 9)) && (advance))
1916                 nam_str = next_word(nam_str);
1917         string = malloc(strlen(nam_str) + 1);
1918         strcpy(string, nam_str);
1919         free(tmp_string);
1920         wrefresh(com_win);
1921         return(string);
1922 }
1923
1924 int 
1925 compare(char *string1, char *string2, int sensitive) /* compare two strings */
1926 {
1927         char *strng1;
1928         char *strng2;
1929         int tmp;
1930         int equal;
1931
1932         strng1 = string1;
1933         strng2 = string2;
1934         tmp = 0;
1935         if ((strng1 == NULL) || (strng2 == NULL) || (*strng1 == (char) NULL) || (*strng2 == (char) NULL))
1936                 return(FALSE);
1937         equal = TRUE;
1938         while (equal)
1939         {
1940                 if (sensitive)
1941                 {
1942                         if (*strng1 != *strng2)
1943                                 equal = FALSE;
1944                 }
1945                 else
1946                 {
1947                         if (toupper(*strng1) != toupper(*strng2))
1948                                 equal = FALSE;
1949                 }
1950                 strng1++;
1951                 strng2++;
1952                 if ((*strng1 == (char) NULL) || (*strng2 == (char) NULL) || (*strng1 == ' ') || (*strng2 == ' '))
1953                         break;
1954                 tmp++;
1955         }
1956         return(equal);
1957 }
1958
1959 void 
1960 goto_line(char *cmd_str)
1961 {
1962         int number;
1963         int i;
1964         char *ptr;
1965         char *direction = NULL;
1966         struct text *t_line;
1967
1968         ptr = cmd_str;
1969         i= 0;
1970         while ((*ptr >='0') && (*ptr <= '9'))
1971         {
1972                 i= i * 10 + (*ptr - '0');
1973                 ptr++;
1974         }
1975         number = i;
1976         i = 0;
1977         t_line = curr_line;
1978         while ((t_line->line_number > number) && (t_line->prev_line != NULL))
1979         {
1980                 i++;
1981                 t_line = t_line->prev_line;
1982                 direction = "u";
1983         }
1984         while ((t_line->line_number < number) && (t_line->next_line != NULL))
1985         {
1986                 i++;
1987                 direction = "d";
1988                 t_line = t_line->next_line;
1989         }
1990         if ((i < 30) && (i > 0))
1991         {
1992                 move_rel(direction, i);
1993         }
1994         else
1995         {
1996                 curr_line = t_line;
1997                 point = curr_line->line;
1998                 position = 1;
1999                 midscreen((last_line / 2), point);
2000                 scr_pos = scr_horz;
2001         }
2002         wmove(com_win, 0, 0);
2003         wclrtoeol(com_win);
2004         wprintw(com_win, line_num_str, curr_line->line_number);
2005         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
2006 }
2007
2008 void 
2009 midscreen(int line, unsigned char *pnt)
2010 /* put current line in middle of screen */
2011 {
2012         struct text *mid_line;
2013         int i;
2014
2015         line = min(line, last_line);
2016         mid_line = curr_line;
2017         for (i = 0; ((i < line) && (curr_line->prev_line != NULL)); i++)
2018                 curr_line = curr_line->prev_line;
2019         scr_vert = scr_horz = 0;
2020         wmove(text_win, 0, 0);
2021         draw_screen();
2022         scr_vert = i;
2023         curr_line = mid_line;
2024         scanline(pnt);
2025         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
2026 }
2027
2028 void 
2029 get_options(int numargs, char **arguments)
2030 /* get arguments from command line */
2031 {
2032         char *buff;
2033         int count;
2034         struct files *temp_names = NULL;
2035         char *name;
2036         char *ptr;
2037
2038         /*
2039          |      see if editor was invoked as 'ree' (restricted mode)
2040          */
2041
2042         if (!(name = strrchr(arguments[0], '/')))
2043                 name = arguments[0];
2044         else
2045                 name++;
2046         if (!strcmp(name, "ree"))
2047                 restricted = TRUE;
2048
2049         top_of_stack = NULL;
2050         input_file = FALSE;
2051         recv_file = FALSE;
2052         count = 1;
2053         while (count < numargs)
2054         {
2055                 buff = arguments[count];
2056                 if (!strcmp("-i", buff))
2057                 {
2058                         info_window = FALSE;
2059                 }
2060                 else if (!strcmp("-e", buff))
2061                 {
2062                         expand_tabs = FALSE;
2063                 }
2064                 else if (!strcmp("-h", buff))
2065                 {
2066                         nohighlight = TRUE;
2067                 }
2068                 else if (!strcmp("-?", buff))
2069                 {
2070                         fprintf(stderr, usage0, arguments[0]);
2071                         fprintf(stderr, usage1);
2072                         fprintf(stderr, usage2);
2073                         fprintf(stderr, usage3);
2074                         fprintf(stderr, usage4);
2075                         exit(1);
2076                 }
2077                 else if (*buff == '+')
2078                 {
2079                         buff++;
2080                         start_at_line = buff;
2081                 }
2082
2083                 else
2084                 {
2085                         if (top_of_stack == NULL)
2086                         {
2087                                 temp_names = top_of_stack = name_alloc();
2088                         }
2089                         else
2090                         {
2091                                 temp_names->next_name = name_alloc();
2092                                 temp_names = temp_names->next_name;
2093                         }
2094                         ptr = temp_names->name = malloc(strlen(buff) + 1);
2095                         while (*buff != (char) NULL)
2096                         {
2097                                 *ptr = *buff;
2098                                 buff++;
2099                                 ptr++;
2100                         }
2101                         *ptr = (char) NULL;
2102                         temp_names->next_name = NULL;
2103                         input_file = TRUE;
2104                         recv_file = TRUE;
2105                 }
2106                 count++;
2107         }
2108 }
2109
2110 void 
2111 check_fp(void)          /* open or close files according to flags */
2112 {
2113         int line_num;
2114         int temp;
2115         struct stat buf;
2116
2117         clear_com_win = TRUE;
2118         tmp_vert = scr_vert;
2119         tmp_horz = scr_horz;
2120         tmp_line = curr_line;
2121         if (input_file)
2122         {
2123                 in_file_name = tmp_file = top_of_stack->name;
2124                 top_of_stack = top_of_stack->next_name;
2125         }
2126         temp = stat(tmp_file, &buf);
2127         buf.st_mode &= ~07777;
2128         if ((temp != -1) && (buf.st_mode != 0100000) && (buf.st_mode != 0))
2129         {
2130                 wprintw(com_win, file_is_dir_msg, tmp_file);
2131                 wrefresh(com_win);
2132                 if (input_file)
2133                 {
2134                         quit(0);
2135                         return;
2136                 }
2137                 else
2138                         return;
2139         }
2140         if ((get_fd = open(tmp_file, O_RDONLY)) == -1)
2141         {
2142                 wmove(com_win, 0, 0);
2143                 wclrtoeol(com_win);
2144                 if (input_file)
2145                         wprintw(com_win, new_file_msg, tmp_file);
2146                 else
2147                         wprintw(com_win, cant_open_msg, tmp_file);
2148                 wrefresh(com_win);
2149                 wmove(text_win, scr_vert, (scr_horz - horiz_offset));
2150                 wrefresh(text_win);
2151                 recv_file = FALSE;
2152                 input_file = FALSE;
2153                 return;
2154         }
2155         else
2156                 get_file(tmp_file);
2157
2158         recv_file = FALSE;
2159         line_num = curr_line->line_number;
2160         scr_vert = tmp_vert;
2161         scr_horz = tmp_horz;
2162         if (input_file)
2163                 curr_line= first_line;
2164         else
2165                 curr_line = tmp_line;
2166         point = curr_line->line;
2167         draw_screen();
2168         if (input_file)
2169         {
2170                 input_file = FALSE;
2171                 if (start_at_line != NULL)
2172                 {
2173                         line_num = atoi(start_at_line) - 1;
2174                         move_rel("d", line_num);
2175                         line_num = 0;
2176                         start_at_line = NULL;
2177                 }
2178         }
2179         else
2180         {
2181                 wmove(com_win, 0, 0);
2182                 wclrtoeol(com_win);
2183                 text_changes = TRUE;
2184                 if ((tmp_file != NULL) && (*tmp_file != (char) NULL))
2185                         wprintw(com_win, file_read_fin_msg, tmp_file);
2186         }
2187         wrefresh(com_win);
2188         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
2189         wrefresh(text_win);
2190 }
2191
2192 void 
2193 get_file(char *file_name) /* read specified file into current buffer */
2194 {
2195         int can_read;           /* file has at least one character      */
2196         int length;             /* length of line read by read          */
2197         int append;             /* should text be appended to current line */
2198         struct text *temp_line;
2199         char ro_flag = FALSE;
2200
2201         if (recv_file)          /* if reading a file                    */
2202         {
2203                 wmove(com_win, 0, 0);
2204                 wclrtoeol(com_win);
2205                 wprintw(com_win, reading_file_msg, file_name);
2206                 if (access(file_name, 2))       /* check permission to write */
2207                 {
2208                         if ((errno == ENOTDIR) || (errno == EACCES) || (errno == EROFS) || (errno == ETXTBSY) || (errno == EFAULT))
2209                         {
2210                                 wprintw(com_win, read_only_msg);
2211                                 ro_flag = TRUE;
2212                         }
2213                 }
2214                 wrefresh(com_win);
2215         }
2216         if (curr_line->line_length > 1) /* if current line is not blank */
2217         {
2218                 insert_line(FALSE);
2219                 left(FALSE);
2220                 append = FALSE;
2221         }
2222         else
2223                 append = TRUE;
2224         can_read = FALSE;               /* test if file has any characters  */
2225         while (((length = read(get_fd, in_string, 512)) != 0) && (length != -1))
2226         {
2227                 can_read = TRUE;  /* if set file has at least 1 character   */
2228                 get_line(length, in_string, &append);
2229         }
2230         if ((can_read) && (curr_line->line_length == 1))
2231         {
2232                 temp_line = curr_line->prev_line;
2233                 temp_line->next_line = curr_line->next_line;
2234                 if (temp_line->next_line != NULL)
2235                         temp_line->next_line->prev_line = temp_line;
2236                 if (curr_line->line != NULL)
2237                         free(curr_line->line);
2238                 free(curr_line);
2239                 curr_line = temp_line;
2240         }
2241         if (input_file) /* if this is the file to be edited display number of lines     */
2242         {
2243                 wmove(com_win, 0, 0);
2244                 wclrtoeol(com_win);
2245                 wprintw(com_win, file_read_lines_msg, in_file_name, curr_line->line_number);
2246                 if (ro_flag)
2247                         wprintw(com_win, read_only_msg);
2248                 wrefresh(com_win);
2249         }
2250         else if (can_read)      /* not input_file and file is non-zero size */
2251                 text_changes = TRUE;
2252
2253         if (recv_file)          /* if reading a file                    */
2254         {
2255                 in = EOF;
2256         }
2257 }
2258
2259 /*
2260 length: length of string read by read
2261 in_string: string read by read
2262 append: TRUE if must append more text to end of current line
2263 */
2264 void
2265 get_line(int length, unsigned char *in_string, int *append)
2266 /* read string and split into lines */
2267 {
2268         unsigned char *str1;
2269         unsigned char *str2;
2270         int num;                /* offset from start of string          */
2271         int char_count;         /* length of new line (or added portion */
2272         int temp_counter;       /* temporary counter value              */
2273         struct text *tline;     /* temporary pointer to new line        */
2274         int first_time;         /* if TRUE, the first time through the loop */
2275
2276         str2 = in_string;
2277         num = 0;
2278         first_time = TRUE;
2279         while (num < length)
2280         {
2281                 if (!first_time)
2282                 {
2283                         if (num < length)
2284                         {
2285                                 str2++;
2286                                 num++;
2287                         }
2288                 }
2289                 else
2290                         first_time = FALSE;
2291                 str1 = str2;
2292                 char_count = 1;
2293                 /* find end of line     */
2294                 while ((*str2 != '\n') && (num < length))
2295                 {
2296                         str2++;
2297                         num++;
2298                         char_count++;
2299                 }
2300                 if (!(*append)) /* if not append to current line, insert new one */
2301                 {
2302                         tline = txtalloc();     /* allocate data structure for next line */
2303                         tline->next_line = curr_line->next_line;
2304                         renumber_lines(tline, curr_line->line_number + 1);
2305                         tline->prev_line = curr_line;
2306                         curr_line->next_line = tline;
2307                         if (tline->next_line != NULL)
2308                                 tline->next_line->prev_line = tline;
2309                         curr_line = tline;
2310                         curr_line->line = point = (unsigned char *) malloc(char_count);
2311                         curr_line->line_length = char_count;
2312                         curr_line->max_length = char_count;
2313                 }
2314                 else
2315                 {
2316                         point = resiz_line(char_count, curr_line, curr_line->line_length); 
2317                         curr_line->line_length += (char_count - 1);
2318                 }
2319                 for (temp_counter = 1; temp_counter < char_count; temp_counter++)
2320                 {
2321                         *point = *str1;
2322                         point++;
2323                         str1++;
2324                 }
2325                 *point = (char) NULL;
2326                 *append = FALSE;
2327                 if ((num == length) && (*str2 != '\n'))
2328                         *append = TRUE;
2329         }
2330 }
2331
2332 void 
2333 draw_screen(void)               /* redraw the screen from current postion       */
2334 {
2335         struct text *temp_line;
2336         unsigned char *line_out;
2337         int temp_vert;
2338
2339         temp_line = curr_line;
2340         temp_vert = scr_vert;
2341         wclrtobot(text_win);
2342         while ((temp_line != NULL) && (temp_vert <= last_line))
2343         {
2344                 line_out = temp_line->line;
2345                 draw_line(temp_vert, 0, line_out, 1, temp_line->line_length);
2346                 temp_vert++;
2347                 temp_line = temp_line->next_line;
2348         }
2349         wmove(text_win, temp_vert, 0);
2350         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
2351 }
2352
2353 void 
2354 finish(void)    /* prepare to exit edit session */
2355 {
2356         char *file_name = in_file_name;
2357
2358         /*
2359          |      changes made here should be reflected in the 'save' 
2360          |      portion of file_op()
2361          */
2362
2363         if ((file_name == NULL) || (*file_name == (char) NULL))
2364                 file_name = get_string(save_file_name_prompt, TRUE);
2365
2366         if ((file_name == NULL) || (*file_name == (char) NULL))
2367         {
2368                 wmove(com_win, 0, 0);
2369                 wprintw(com_win, file_not_saved_msg);
2370                 wclrtoeol(com_win);
2371                 wrefresh(com_win);
2372                 clear_com_win = TRUE;
2373                 return;
2374         }
2375
2376         tmp_file = resolve_name(file_name);
2377         if (tmp_file != file_name)
2378         {
2379                 free(file_name);
2380                 file_name = tmp_file;
2381         }
2382
2383         if (write_file(file_name))
2384         {
2385                 text_changes = FALSE;
2386                 quit(0);
2387         }
2388 }
2389
2390 int 
2391 quit(int noverify)              /* exit editor                  */
2392 {
2393         char *ans;
2394
2395         touchwin(text_win);
2396         wrefresh(text_win);
2397         if ((text_changes) && (!noverify))
2398         {
2399                 ans = get_string(changes_made_prompt, TRUE);
2400                 if (toupper(*ans) == toupper(*yes_char))
2401                         text_changes = FALSE;
2402                 else
2403                         return(0);
2404                 free(ans);
2405         }
2406         if (top_of_stack == NULL)
2407         {
2408                 if (info_window)
2409                         wrefresh(info_win);
2410                 wrefresh(com_win);
2411                 resetty();
2412                 endwin();
2413                 putchar('\n');
2414                 exit(0);
2415         }
2416         else
2417         {
2418                 delete_text();
2419                 recv_file = TRUE;
2420                 input_file = TRUE;
2421                 check_fp();
2422                 text_changes = FALSE;
2423         }
2424         return(0);
2425 }
2426
2427 void 
2428 edit_abort(int arg)
2429 {
2430         wrefresh(com_win);
2431         resetty();
2432         endwin();
2433         putchar('\n');
2434         exit(1);
2435 }
2436
2437 void 
2438 delete_text(void)
2439 {
2440         while (curr_line->next_line != NULL)
2441                 curr_line = curr_line->next_line;
2442         while (curr_line != first_line)
2443         {
2444                 free(curr_line->line);
2445                 curr_line = curr_line->prev_line;
2446                 free(curr_line->next_line);
2447         }
2448         curr_line->next_line = NULL;
2449         *curr_line->line = (char) NULL;
2450         curr_line->line_length = 1;
2451         curr_line->line_number = 1;
2452         point = curr_line->line;
2453         scr_pos = scr_vert = scr_horz = 0;
2454         position = 1;
2455 }
2456
2457 int 
2458 write_file(char *file_name)
2459 {
2460         char cr;
2461         char *tmp_point;
2462         struct text *out_line;
2463         int lines, charac;
2464         int temp_pos;
2465         int write_flag = TRUE;
2466
2467         charac = lines = 0;
2468         if ((in_file_name == NULL) || strcmp(in_file_name, file_name))
2469         {
2470                 if ((temp_fp = fopen(file_name, "r")))
2471                 {
2472                         tmp_point = get_string(file_exists_prompt, TRUE);
2473                         if (toupper(*tmp_point) == toupper(*yes_char))
2474                                 write_flag = TRUE;
2475                         else 
2476                                 write_flag = FALSE;
2477                         fclose(temp_fp);
2478                         free(tmp_point);
2479                 }
2480         }
2481
2482         clear_com_win = TRUE;
2483
2484         if (write_flag)
2485         {
2486                 if ((temp_fp = fopen(file_name, "w")) == NULL)
2487                 {
2488                         clear_com_win = TRUE;
2489                         wmove(com_win,0,0);
2490                         wclrtoeol(com_win);
2491                         wprintw(com_win, create_file_fail_msg, file_name);
2492                         wrefresh(com_win);
2493                         return(FALSE);
2494                 }
2495                 else
2496                 {
2497                         wmove(com_win,0,0);
2498                         wclrtoeol(com_win);
2499                         wprintw(com_win, writing_file_msg, file_name);
2500                         wrefresh(com_win);
2501                         cr = '\n';
2502                         out_line = first_line;
2503                         while (out_line != NULL)
2504                         {
2505                                 temp_pos = 1;
2506                                 tmp_point= out_line->line;
2507                                 while (temp_pos < out_line->line_length)
2508                                 {
2509                                         putc(*tmp_point, temp_fp);
2510                                         tmp_point++;
2511                                         temp_pos++;
2512                                 }
2513                                 charac += out_line->line_length;
2514                                 out_line = out_line->next_line;
2515                                 putc(cr, temp_fp);
2516                                 lines++;
2517                         }
2518                         fclose(temp_fp);
2519                         wmove(com_win,0,0);
2520                         wclrtoeol(com_win);
2521                         wprintw(com_win, file_written_msg, file_name, lines, charac);
2522                         wrefresh(com_win);
2523                         return(TRUE);
2524                 }
2525         }
2526         else
2527                 return(FALSE);
2528 }
2529
2530 int 
2531 search(int display_message) /* search for string in srch_str */
2532 {
2533         int lines_moved;
2534         int iter;
2535         int found;
2536
2537         if ((srch_str == NULL) || (*srch_str == (char) NULL))
2538                 return(FALSE);
2539         if (display_message)
2540         {
2541                 wmove(com_win, 0, 0);
2542                 wclrtoeol(com_win);
2543                 wprintw(com_win, searching_msg);
2544                 wrefresh(com_win);
2545                 clear_com_win = TRUE;
2546         }
2547         lines_moved = 0;
2548         found = FALSE;
2549         srch_line = curr_line;
2550         srch_1 = point;
2551         if (position < curr_line->line_length)
2552                 srch_1++;
2553         iter = position + 1;
2554         while ((!found) && (srch_line != NULL))
2555         {
2556                 while ((iter < srch_line->line_length) && (!found))
2557                 {
2558                         srch_2 = srch_1;
2559                         if (case_sen)   /* if case sensitive            */
2560                         {
2561                                 srch_3 = srch_str;
2562                         while ((*srch_2 == *srch_3) && (*srch_3 != (char) NULL))
2563                                 {
2564                                         found = TRUE;
2565                                         srch_2++;
2566                                         srch_3++;
2567                                 }       /* end while    */
2568                         }
2569                         else            /* if not case sensitive        */
2570                         {
2571                                 srch_3 = u_srch_str;
2572                         while ((toupper(*srch_2) == *srch_3) && (*srch_3 != (char) NULL))
2573                                 {
2574                                         found = TRUE;
2575                                         srch_2++;
2576                                         srch_3++;
2577                                 }
2578                         }       /* end else     */
2579                         if (!((*srch_3 == (char) NULL) && (found)))
2580                         {
2581                                 found = FALSE;
2582                                 if (iter < srch_line->line_length)
2583                                         srch_1++;
2584                                 iter++;
2585                         }
2586                 }
2587                 if (!found)
2588                 {
2589                         srch_line = srch_line->next_line;
2590                         if (srch_line != NULL)
2591                                 srch_1 = srch_line->line;
2592                         iter = 1;
2593                         lines_moved++;
2594                 }
2595         }
2596         if (found)
2597         {
2598                 if (display_message)
2599                 {
2600                         wmove(com_win, 0, 0);
2601                         wclrtoeol(com_win);
2602                         wrefresh(com_win);
2603                 }
2604                 if (lines_moved == 0)
2605                 {
2606                         while (position < iter)
2607                                 right(TRUE);
2608                 }
2609                 else
2610                 {
2611                         if (lines_moved < 30)
2612                         {
2613                                 move_rel("d", lines_moved);
2614                                 while (position < iter)
2615                                         right(TRUE);
2616                         }
2617                         else 
2618                         {
2619                                 curr_line = srch_line;
2620                                 point = srch_1;
2621                                 position = iter;
2622                                 scanline(point);
2623                                 scr_pos = scr_horz;
2624                                 midscreen((last_line / 2), point);
2625                         }
2626                 }
2627         }
2628         else
2629         {
2630                 if (display_message)
2631                 {
2632                         wmove(com_win, 0, 0);
2633                         wclrtoeol(com_win);
2634                         wprintw(com_win, str_not_found_msg, srch_str);
2635                         wrefresh(com_win);
2636                 }
2637                 wmove(text_win, scr_vert,(scr_horz - horiz_offset));
2638         }
2639         return(found);
2640 }
2641
2642 void 
2643 search_prompt(void) /* prompt and read search string (srch_str) */
2644 {
2645         if (srch_str != NULL)
2646                 free(srch_str);
2647         if ((u_srch_str != NULL) && (*u_srch_str != (char) NULL))
2648                 free(u_srch_str);
2649         srch_str = get_string(search_prompt_str, FALSE);
2650         gold = FALSE;
2651         srch_3 = srch_str;
2652         srch_1 = u_srch_str = malloc(strlen(srch_str) + 1);
2653         while (*srch_3 != (char) NULL)
2654         {
2655                 *srch_1 = toupper(*srch_3);
2656                 srch_1++;
2657                 srch_3++;
2658         }
2659         *srch_1 = (char) NULL;
2660         search(TRUE);
2661 }
2662
2663 void 
2664 del_char(void) /* delete current character */
2665 {
2666         in = 8;  /* backspace */
2667         if (position < curr_line->line_length)  /* if not end of line   */
2668         {
2669                 if ((ee_chinese) && (*point > 127) && 
2670                     ((curr_line->line_length - position) >= 2))
2671                 {
2672                         point++;
2673                         position++;
2674                 }
2675                 position++;
2676                 point++;
2677                 scanline(point);
2678                 delete(TRUE);
2679         }
2680         else
2681         {
2682                 right(FALSE);
2683                 delete(FALSE);
2684         }
2685 }
2686
2687 void 
2688 undel_char(void) /* undelete last deleted character */
2689 {
2690         if (d_char[0] == '\n')  /* insert line if last del_char deleted eol */
2691                 insert_line(TRUE);
2692         else
2693         {
2694                 in = d_char[0];
2695                 insert(in);
2696                 if (d_char[1] != (unsigned char) NULL)
2697                 {
2698                         in = d_char[1];
2699                         insert(in);
2700                 }
2701         }
2702 }
2703
2704 void 
2705 del_word(void) /* delete word in front of cursor */
2706 {
2707         int tposit;
2708         int difference;
2709         unsigned char *d_word2;
2710         unsigned char *d_word3;
2711         unsigned char tmp_char[3];
2712
2713         if (d_word != NULL)
2714                 free(d_word);
2715         d_word = malloc(curr_line->line_length);
2716         tmp_char[0] = d_char[0];
2717         tmp_char[1] = d_char[1];
2718         tmp_char[2] = d_char[2];
2719         d_word3 = point;
2720         d_word2 = d_word;
2721         tposit = position;
2722         while ((tposit < curr_line->line_length) && 
2723                                 ((*d_word3 != ' ') && (*d_word3 != '\t')))
2724         {
2725                 tposit++;
2726                 *d_word2 = *d_word3;
2727                 d_word2++;
2728                 d_word3++;
2729         }
2730         while ((tposit < curr_line->line_length) && 
2731                                 ((*d_word3 == ' ') || (*d_word3 == '\t')))
2732         {
2733                 tposit++;
2734                 *d_word2 = *d_word3;
2735                 d_word2++;
2736                 d_word3++;
2737         }
2738         *d_word2 = (char) NULL;
2739         d_wrd_len = difference = d_word2 - d_word;
2740         d_word2 = point;
2741         while (tposit < curr_line->line_length)
2742         {
2743                 tposit++;
2744                 *d_word2 = *d_word3;
2745                 d_word2++;
2746                 d_word3++;
2747         }
2748         curr_line->line_length -= difference;
2749         *d_word2 = (char) NULL;
2750         draw_line(scr_vert, scr_horz,point,position,curr_line->line_length);
2751         d_char[0] = tmp_char[0];
2752         d_char[1] = tmp_char[1];
2753         d_char[2] = tmp_char[2];
2754         text_changes = TRUE;
2755         formatted = FALSE;
2756 }
2757
2758 void 
2759 undel_word(void) /* undelete last deleted word */
2760 {
2761         int temp;
2762         int tposit;
2763         unsigned char *tmp_old_ptr;
2764         unsigned char *tmp_space;
2765         unsigned char *tmp_ptr;
2766         unsigned char *d_word_ptr;
2767
2768         /*
2769          |      resize line to handle undeleted word
2770          */
2771         if ((curr_line->max_length - (curr_line->line_length + d_wrd_len)) < 5)
2772                 point = resiz_line(d_wrd_len, curr_line, position);
2773         tmp_ptr = tmp_space = malloc(curr_line->line_length + d_wrd_len);
2774         d_word_ptr = d_word;
2775         temp = 1;
2776         /*
2777          |      copy d_word contents into temp space
2778          */
2779         while (temp <= d_wrd_len)
2780         {
2781                 temp++;
2782                 *tmp_ptr = *d_word_ptr;
2783                 tmp_ptr++;
2784                 d_word_ptr++;
2785         }
2786         tmp_old_ptr = point;
2787         tposit = position;
2788         /*
2789          |      copy contents of line from curent position to eol into 
2790          |      temp space
2791          */
2792         while (tposit < curr_line->line_length)
2793         {
2794                 temp++;
2795                 tposit++;
2796                 *tmp_ptr = *tmp_old_ptr;
2797                 tmp_ptr++;
2798                 tmp_old_ptr++;
2799         }
2800         curr_line->line_length += d_wrd_len;
2801         tmp_old_ptr = point;
2802         *tmp_ptr = (char) NULL;
2803         tmp_ptr = tmp_space;
2804         tposit = 1;
2805         /*
2806          |      now copy contents from temp space back to original line
2807          */
2808         while (tposit < temp)
2809         {
2810                 tposit++;
2811                 *tmp_old_ptr = *tmp_ptr;
2812                 tmp_ptr++;
2813                 tmp_old_ptr++;
2814         }
2815         *tmp_old_ptr = (char) NULL;
2816         free(tmp_space);
2817         draw_line(scr_vert, scr_horz, point, position, curr_line->line_length);
2818 }
2819
2820 void 
2821 del_line(void) /* delete from cursor to end of line */
2822 {
2823         unsigned char *dl1;
2824         unsigned char *dl2;
2825         int tposit;
2826
2827         if (d_line != NULL)
2828                 free(d_line);
2829         d_line = malloc(curr_line->line_length);
2830         dl1 = d_line;
2831         dl2 = point;
2832         tposit = position;
2833         while (tposit < curr_line->line_length)
2834         {
2835                 *dl1 = *dl2;
2836                 dl1++;
2837                 dl2++;
2838                 tposit++;
2839         }
2840         dlt_line->line_length = 1 + tposit - position;
2841         *dl1 = (char) NULL;
2842         *point = (char) NULL;
2843         curr_line->line_length = position;
2844         wclrtoeol(text_win);
2845         if (curr_line->next_line != NULL)
2846         {
2847                 right(FALSE);
2848                 delete(FALSE);
2849         }
2850         text_changes = TRUE;
2851 }
2852
2853 void 
2854 undel_line(void) /* undelete last deleted line */
2855 {
2856         unsigned char *ud1;
2857         unsigned char *ud2;
2858         int tposit;
2859
2860         if (dlt_line->line_length == 0)
2861                 return;
2862
2863         insert_line(TRUE);
2864         left(TRUE);
2865         point = resiz_line(dlt_line->line_length, curr_line, position);
2866         curr_line->line_length += dlt_line->line_length - 1;
2867         ud1 = point;
2868         ud2 = d_line;
2869         tposit = 1;
2870         while (tposit < dlt_line->line_length)
2871         {
2872                 tposit++;
2873                 *ud1 = *ud2;
2874                 ud1++;
2875                 ud2++;
2876         }
2877         *ud1 = (char) NULL;
2878         draw_line(scr_vert, scr_horz,point,position,curr_line->line_length);
2879 }
2880
2881 void 
2882 adv_word(void) /* advance to next word */
2883 {
2884 while ((position < curr_line->line_length) && ((*point != 32) && (*point != 9)))
2885                 right(TRUE);
2886 while ((position < curr_line->line_length) && ((*point == 32) || (*point == 9)))
2887                 right(TRUE);
2888 }
2889
2890 void 
2891 move_rel(char *direction, int lines)    /* move relative to current line        */
2892 {
2893         int i;
2894         char *tmp;
2895
2896         if (*direction == 'u')
2897         {
2898                 scr_pos = 0;
2899                 while (position > 1)
2900                         left(TRUE);
2901                 for (i = 0; i < lines; i++)
2902                 {
2903                         up();
2904                 }
2905                 if ((last_line > 5) && ( scr_vert < 4))
2906                 {
2907                         tmp = point;
2908                         tmp_line = curr_line;
2909                         for (i= 0;(i<5)&&(curr_line->prev_line != NULL); i++)
2910                         {
2911                                 up();
2912                         }
2913                         scr_vert = scr_vert + i;
2914                         curr_line = tmp_line;
2915                         point = tmp;
2916                         scanline(point);
2917                 }
2918         }
2919         else
2920         {
2921                 if ((position != 1) && (curr_line->next_line != NULL))
2922                 {
2923                         nextline();
2924                         scr_pos = scr_horz = 0;
2925                         if (horiz_offset)
2926                         {
2927                                 horiz_offset = 0;
2928                                 midscreen(scr_vert, point);
2929                         }
2930                 }
2931                 else
2932                         adv_line();
2933                 for (i = 1; i < lines; i++)
2934                 {
2935                         down();
2936                 }
2937                 if ((last_line > 10) && (scr_vert > (last_line - 5)))
2938                 {
2939                         tmp = point;
2940                         tmp_line = curr_line;
2941                         for (i=0; (i<5) && (curr_line->next_line != NULL); i++)
2942                         {
2943                                 down();
2944                         }
2945                         scr_vert = scr_vert - i;
2946                         curr_line = tmp_line;
2947                         point = tmp;
2948                         scanline(point);
2949                 }
2950         }
2951         wmove(text_win, scr_vert, (scr_horz - horiz_offset));
2952 }
2953
2954 void 
2955 eol(void)                               /* go to end of line                    */
2956 {
2957         if (position < curr_line->line_length)
2958         {
2959                 while (position < curr_line->line_length)
2960                         right(TRUE);
2961         }
2962         else if (curr_line->next_line != NULL)
2963         {
2964                 right(TRUE);
2965                 while (position < curr_line->line_length)
2966                         right(TRUE);
2967         }
2968 }
2969
2970 void 
2971 bol(void)                               /* move to beginning of line    */
2972 {
2973         if (point != curr_line->line)
2974         {
2975                 while (point != curr_line->line)
2976                         left(TRUE);
2977         }
2978         else if (curr_line->prev_line != NULL)
2979         {
2980                 scr_pos = 0;
2981                 up();
2982         }
2983 }
2984
2985 void 
2986 adv_line(void)  /* advance to beginning of next line    */
2987 {
2988         if ((point != curr_line->line) || (scr_pos > 0))
2989         {
2990                 while (position < curr_line->line_length)
2991                         right(TRUE);
2992                 right(TRUE);
2993         }
2994         else if (curr_line->next_line != NULL)
2995         {
2996                 scr_pos = 0;
2997                 down();
2998         }
2999 }
3000
3001 /* string: string containing user command */
3002 void 
3003 sh_command(char *string)        /* execute shell command                        */
3004 {
3005         char *temp_point;
3006         char *last_slash;
3007         char *path;             /* directory path to executable         */
3008         int parent;             /* zero if child, child's pid if parent */
3009         int value;
3010         int return_val;
3011         struct text *line_holder;
3012
3013         if (restrict_mode())
3014         {
3015                 return;
3016         }
3017
3018         if (!(path = getenv("SHELL")))
3019                 path = "/bin/sh";
3020         last_slash = temp_point = path;
3021         while (*temp_point != (char) NULL)
3022         {
3023                 if (*temp_point == '/')
3024                         last_slash = ++temp_point;
3025                 else
3026                         temp_point++;
3027         }
3028
3029         /*
3030          |      if in_pipe is true, then output of the shell operation will be 
3031          |      read by the editor, and curses doesn't need to be turned off
3032          */
3033
3034         if (!in_pipe)
3035         {
3036                 keypad(com_win, FALSE);
3037                 keypad(text_win, FALSE);
3038                 echo();
3039                 nl();
3040                 noraw();
3041                 resetty();
3042
3043 #ifndef NCURSE
3044                 endwin();
3045 #endif
3046         }
3047
3048         if (in_pipe)
3049         {
3050                 pipe(pipe_in);          /* create a pipe        */
3051                 parent = fork();
3052                 if (!parent)            /* if the child         */
3053                 {
3054 /*
3055  |  child process which will fork and exec shell command (if shell output is
3056  |  to be read by editor)
3057  */
3058                         in_pipe = FALSE;
3059 /*
3060  |  redirect stdout to pipe
3061  */
3062                         temp_stdout = dup(1);
3063                         close(1);
3064                         dup(pipe_in[1]);
3065 /*
3066  |  redirect stderr to pipe
3067  */
3068                         temp_stderr = dup(2);
3069                         close(2);
3070                         dup(pipe_in[1]);
3071                         close(pipe_in[1]);
3072                         /*
3073                          |      child will now continue down 'if (!in_pipe)' 
3074                          |      path below
3075                          */
3076                 }
3077                 else  /* if the parent  */
3078                 {
3079 /*
3080  |  prepare editor to read from the pipe
3081  */
3082                         signal(SIGCHLD, SIG_IGN);
3083                         line_holder = curr_line;
3084                         tmp_vert = scr_vert;
3085                         close(pipe_in[1]);
3086                         get_fd = pipe_in[0];
3087                         get_file("");
3088                         close(pipe_in[0]);
3089                         scr_vert = tmp_vert;
3090                         scr_horz = scr_pos = 0;
3091                         position = 1;
3092                         curr_line = line_holder;
3093                         point = curr_line->line;
3094                         out_pipe = FALSE;
3095                         signal(SIGCHLD, SIG_DFL);
3096 /*
3097  |  since flag "in_pipe" is still TRUE, the path which waits for the child 
3098  |  process to die will be avoided.
3099  |  (the pipe is closed, no more output can be expected)
3100  */
3101                 }
3102         }
3103         if (!in_pipe)
3104         {
3105                 signal(SIGINT, SIG_IGN);
3106                 if (out_pipe)
3107                 {
3108                         pipe(pipe_out);
3109                 }
3110 /*
3111  |  fork process which will exec command
3112  */
3113                 parent = fork();   
3114                 if (!parent)            /* if the child */
3115                 {
3116                         if (shell_fork)
3117                                 putchar('\n');
3118                         if (out_pipe)
3119                         {
3120 /*
3121  |  prepare the child process (soon to exec a shell command) to read from the 
3122  |  pipe (which will be output from the editor's buffer)
3123  */
3124                                 close(0);
3125                                 dup(pipe_out[0]);
3126                                 close(pipe_out[0]);
3127                                 close(pipe_out[1]);
3128                         }
3129                         for (value = 1; value < 24; value++)
3130                                 signal(value, SIG_DFL);
3131                         execl(path, last_slash, "-c", string, NULL);
3132                         errx(1, exec_err_msg, path);
3133                 }
3134                 else    /* if the parent        */
3135                 {
3136                         if (out_pipe)
3137                         {
3138 /*
3139  |  output the contents of the buffer to the pipe (to be read by the 
3140  |  process forked and exec'd above as stdin)
3141  */
3142                                 close(pipe_out[0]);
3143                                 line_holder = first_line;
3144                                 while (line_holder != NULL)
3145                                 {
3146                                         write(pipe_out[1], line_holder->line, (line_holder->line_length-1));
3147                                         write(pipe_out[1], "\n", 1);
3148                                         line_holder = line_holder->next_line;
3149                                 }
3150                                 close(pipe_out[1]);
3151                                 out_pipe = FALSE;
3152                         }
3153                         do
3154                         {
3155                                 return_val = wait((int *) 0);
3156                         }
3157                         while ((return_val != parent) && (return_val != -1));
3158 /*
3159  |  if this process is actually the child of the editor, exit.  Here's how it 
3160  |  works:
3161  |  The editor forks a process.  If output must be sent to the command to be 
3162  |  exec'd another process is forked, and that process (the child's child) 
3163  |  will exec the command.  In this case, "shell_fork" will be FALSE.  If no 
3164  |  output is to be performed to the shell command, "shell_fork" will be TRUE.
3165  |  If this is the editor process, shell_fork will be true, otherwise this is 
3166  |  the child of the edit process.
3167  */
3168                         if (!shell_fork)
3169                                 exit(0);
3170                 }
3171                 signal(SIGINT, edit_abort);
3172         }
3173         if (shell_fork)
3174         {
3175                 printf("%s", continue_msg);
3176                 fflush(stdout);
3177                 while ((in = getchar()) != '\n')
3178                         ;
3179         }
3180
3181         if (!in_pipe)
3182         {
3183                 fixterm();
3184                 noecho();
3185                 nonl();
3186                 raw();
3187                 keypad(text_win, TRUE);
3188                 keypad(com_win, TRUE);
3189                 if (info_window)
3190                         clearok(info_win, TRUE);
3191         }
3192
3193         redraw();
3194 }
3195
3196 void 
3197 set_up_term(void)               /* set up the terminal for operating with ae    */
3198 {
3199         if (!curses_initialized)
3200         {
3201                 initscr();
3202                 savetty();
3203                 noecho();
3204                 raw();
3205                 nonl();
3206                 curses_initialized = TRUE;
3207         }
3208
3209         if (((LINES > 15) && (COLS >= 80)) && info_window)
3210                 last_line = LINES - 8;
3211         else
3212         {
3213                 info_window = FALSE;
3214                 last_line = LINES - 2;
3215         }
3216
3217         idlok(stdscr, TRUE);
3218         com_win = newwin(1, COLS, (LINES - 1), 0);
3219         keypad(com_win, TRUE);
3220         idlok(com_win, TRUE);
3221         wrefresh(com_win);
3222         if (!info_window)
3223                 text_win = newwin((LINES - 1), COLS, 0, 0);
3224         else
3225                 text_win = newwin((LINES - 7), COLS, 6, 0);
3226         keypad(text_win, TRUE);
3227         idlok(text_win, TRUE);
3228         wrefresh(text_win);
3229         help_win = newwin((LINES - 1), COLS, 0, 0);
3230         keypad(help_win, TRUE);
3231         idlok(help_win, TRUE);
3232         if (info_window)
3233         {
3234                 info_type = CONTROL_KEYS;
3235                 info_win = newwin(5, COLS, 0, 0);
3236                 werase(info_win);
3237                 paint_info_win();
3238                 count_win = newwin(1, COLS, 5, 0);
3239                 leaveok(count_win, TRUE);
3240                 wrefresh(count_win);
3241         }
3242
3243         last_col = COLS - 1;
3244         local_LINES = LINES;
3245         local_COLS = COLS;
3246
3247 #ifdef NCURSE
3248         if (ee_chinese)
3249                 nc_setattrib(A_NC_BIG5);
3250 #endif /* NCURSE */
3251
3252 }
3253
3254 void 
3255 resize_check(void)
3256 {
3257         if ((LINES == local_LINES) && (COLS == local_COLS))
3258                 return;
3259
3260         if (info_window)
3261                 delwin(info_win);
3262         delwin(text_win);
3263         delwin(com_win);
3264         delwin(help_win);
3265         delwin(count_win);
3266         set_up_term();
3267         redraw();
3268         wrefresh(text_win);
3269 }
3270
3271 static char item_alpha[] = "abcdefghijklmnopqrstuvwxyz0123456789 ";
3272
3273 int 
3274 menu_op(struct menu_entries *menu_list)
3275 {
3276         WINDOW *temp_win;
3277         int max_width, max_height;
3278         int x_off, y_off;
3279         int counter;
3280         int length;
3281         int input;
3282         int temp = 0;
3283         int list_size;
3284         int top_offset;         /* offset from top where menu items start */
3285         int vert_pos;           /* vertical position                      */
3286         int vert_size;          /* vertical size for menu list item display */
3287         int off_start = 1;      /* offset from start of menu items to start display */
3288
3289
3290         /*
3291          |      determine number and width of menu items
3292          */
3293
3294         list_size = 1;
3295         while (menu_list[list_size + 1].item_string != NULL)
3296                 list_size++;
3297         max_width = 0;
3298         for (counter = 0; counter <= list_size; counter++)
3299         {
3300                 if ((length = strlen(menu_list[counter].item_string)) > max_width)
3301                         max_width = length;
3302         }
3303         max_width += 3;
3304         max_width = max(max_width, strlen(menu_cancel_msg));
3305         max_width = max(max_width, max(strlen(more_above_str), strlen(more_below_str)));
3306         max_width += 6;
3307
3308         /*
3309          |      make sure that window is large enough to handle menu
3310          |      if not, print error message and return to calling function
3311          */
3312
3313         if (max_width > COLS)
3314         {
3315                 wmove(com_win, 0, 0);
3316                 werase(com_win);
3317                 wprintw(com_win, menu_too_lrg_msg);
3318                 wrefresh(com_win);
3319                 clear_com_win = TRUE;
3320                 return(0);
3321         }
3322
3323         top_offset = 0;
3324
3325         if (list_size > LINES)
3326         {
3327                 max_height = LINES;
3328                 if (max_height > 11)
3329                         vert_size = max_height - 8;
3330                 else
3331                         vert_size = max_height;
3332         }
3333         else
3334         {
3335                 vert_size = list_size;
3336                 max_height = list_size;
3337         }
3338
3339         if (LINES >= (vert_size + 8))
3340         {
3341                 if (menu_list[0].argument != MENU_WARN)
3342                         max_height = vert_size + 8;
3343                 else
3344                         max_height = vert_size + 7;
3345                 top_offset = 4;
3346         }
3347         x_off = (COLS - max_width) / 2;
3348         y_off = (LINES - max_height - 1) / 2;
3349         temp_win = newwin(max_height, max_width, y_off, x_off);
3350         keypad(temp_win, TRUE);
3351
3352         paint_menu(menu_list, max_width, max_height, list_size, top_offset, temp_win, off_start, vert_size);
3353
3354         counter = 1;
3355         vert_pos = 0;
3356         do
3357         {
3358                 if (off_start > 2)
3359                         wmove(temp_win, (1 + counter + top_offset - off_start), 3);
3360                 else
3361                         wmove(temp_win, (counter + top_offset - off_start), 3);
3362
3363                 wrefresh(temp_win);
3364                 input = wgetch(temp_win);
3365
3366                 if (((tolower(input) >= 'a') && (tolower(input) <= 'z')) || 
3367                     ((input >= '0') && (input <= '9')))
3368                 {
3369                         if ((tolower(input) >= 'a') && (tolower(input) <= 'z'))
3370                         {
3371                                 temp = 1 + tolower(input) - 'a';
3372                         }
3373                         else if ((input >= '0') && (input <= '9'))
3374                         {
3375                                 temp = (2 + 'z' - 'a') + (input - '0');
3376                         }
3377
3378                         if (temp <= list_size)
3379                         {
3380                                 input = '\n';
3381                                 counter = temp;
3382                         }
3383                 }
3384                 else
3385                 {               
3386                         switch (input)
3387                         {
3388                                 case ' ':       /* space        */
3389                                 case '\004':    /* ^d, down     */
3390                                 case KEY_RIGHT:
3391                                 case KEY_DOWN:
3392                                         counter++;
3393                                         if (counter > list_size)
3394                                                 counter = 1;
3395                                         break;
3396                                 case '\010':    /* ^h, backspace*/
3397                                 case '\025':    /* ^u, up       */
3398                                 case 127:       /* ^?, delete   */
3399                                 case KEY_BACKSPACE:
3400                                 case KEY_LEFT:
3401                                 case KEY_UP:
3402                                         counter--;
3403                                         if (counter == 0)
3404                                                 counter = list_size;
3405                                         break;
3406                                 case '\033':    /* escape key   */
3407                                         if (menu_list[0].argument != MENU_WARN)
3408                                                 counter = 0;
3409                                         break;
3410                                 case '\014':    /* ^l           */
3411                                 case '\022':    /* ^r, redraw   */
3412                                         paint_menu(menu_list, max_width, max_height, 
3413                                                 list_size, top_offset, temp_win, 
3414                                                 off_start, vert_size);
3415                                         break;
3416                                 default:
3417                                         break;
3418                         }
3419                 }
3420         
3421                 if (((list_size - off_start) >= (vert_size - 1)) && 
3422                         (counter > (off_start + vert_size - 3)) && 
3423                                 (off_start > 1))
3424                 {
3425                         if (counter == list_size)
3426                                 off_start = (list_size - vert_size) + 2;
3427                         else
3428                                 off_start++;
3429
3430                         paint_menu(menu_list, max_width, max_height, 
3431                                    list_size, top_offset, temp_win, off_start, 
3432                                    vert_size);
3433                 }
3434                 else if ((list_size != vert_size) && 
3435                                 (counter > (off_start + vert_size - 2)))
3436                 {
3437                         if (counter == list_size)
3438                                 off_start = 2 + (list_size - vert_size);
3439                         else if (off_start == 1)
3440                                 off_start = 3;
3441                         else
3442                                 off_start++;
3443
3444                         paint_menu(menu_list, max_width, max_height, 
3445                                    list_size, top_offset, temp_win, off_start, 
3446                                    vert_size);
3447                 }
3448                 else if (counter < off_start)
3449                 {
3450                         if (counter <= 2)
3451                                 off_start = 1;
3452                         else
3453                                 off_start = counter;
3454
3455                         paint_menu(menu_list, max_width, max_height, 
3456                                    list_size, top_offset, temp_win, off_start, 
3457                                    vert_size);
3458                 }
3459         }
3460         while ((input != '\r') && (input != '\n') && (counter != 0));
3461
3462         werase(temp_win);
3463         wrefresh(temp_win);
3464         delwin(temp_win);
3465
3466         if ((menu_list[counter].procedure != NULL) || 
3467             (menu_list[counter].iprocedure != NULL) || 
3468             (menu_list[counter].nprocedure != NULL))
3469         {
3470                 if (menu_list[counter].argument != -1)
3471                         (*menu_list[counter].iprocedure)(menu_list[counter].argument);
3472                 else if (menu_list[counter].ptr_argument != NULL)
3473                         (*menu_list[counter].procedure)(menu_list[counter].ptr_argument);
3474                 else
3475                         (*menu_list[counter].nprocedure)();
3476         }
3477
3478         if (info_window)
3479                 paint_info_win();
3480         redraw();
3481
3482         return(counter);
3483 }
3484
3485 void 
3486 paint_menu(struct menu_entries *menu_list, int max_width, int max_height,
3487            int list_size, int top_offset, WINDOW *menu_win, int off_start,
3488            int vert_size)
3489 {
3490         int counter, temp_int;
3491
3492         werase(menu_win);
3493
3494         /*
3495          |      output top and bottom portions of menu box only if window 
3496          |      large enough 
3497          */
3498
3499         if (max_height > vert_size)
3500         {
3501                 wmove(menu_win, 1, 1);
3502                 if (!nohighlight)
3503                         wstandout(menu_win);
3504                 waddch(menu_win, '+');
3505                 for (counter = 0; counter < (max_width - 4); counter++)
3506                         waddch(menu_win, '-');
3507                 waddch(menu_win, '+');
3508
3509                 wmove(menu_win, (max_height - 2), 1);
3510                 waddch(menu_win, '+');
3511                 for (counter = 0; counter < (max_width - 4); counter++)
3512                         waddch(menu_win, '-');
3513                 waddch(menu_win, '+');
3514                 wstandend(menu_win);
3515                 wmove(menu_win, 2, 3);
3516                 waddstr(menu_win, menu_list[0].item_string);
3517                 wmove(menu_win, (max_height - 3), 3);
3518                 if (menu_list[0].argument != MENU_WARN)
3519                         waddstr(menu_win, menu_cancel_msg);
3520         }
3521         if (!nohighlight)
3522                 wstandout(menu_win);
3523
3524         for (counter = 0; counter < (vert_size + top_offset); counter++)
3525         {
3526                 if (top_offset == 4)
3527                 {
3528                         temp_int = counter + 2;
3529                 }
3530                 else
3531                         temp_int = counter;
3532
3533                 wmove(menu_win, temp_int, 1);
3534                 waddch(menu_win, '|');
3535                 wmove(menu_win, temp_int, (max_width - 2));
3536                 waddch(menu_win, '|');
3537         }
3538         wstandend(menu_win);
3539
3540         if (list_size > vert_size)
3541         {
3542                 if (off_start >= 3)
3543                 {
3544                         temp_int = 1;
3545                         wmove(menu_win, top_offset, 3);
3546                         waddstr(menu_win, more_above_str);
3547                 }
3548                 else
3549                         temp_int = 0;
3550
3551                 for (counter = off_start; 
3552                         ((temp_int + counter - off_start) < (vert_size - 1));
3553                                 counter++)
3554                 {
3555                         wmove(menu_win, (top_offset + temp_int + 
3556                                                 (counter - off_start)), 3);
3557                         if (list_size > 1)
3558                                 wprintw(menu_win, "%c) ", item_alpha[min((counter - 1), max_alpha_char)]);
3559                         waddstr(menu_win, menu_list[counter].item_string);
3560                 }
3561
3562                 wmove(menu_win, (top_offset + (vert_size - 1)), 3);
3563
3564                 if (counter == list_size)
3565                 {
3566                         if (list_size > 1)
3567                                 wprintw(menu_win, "%c) ", item_alpha[min((counter - 1), max_alpha_char)]);
3568                         wprintw(menu_win, menu_list[counter].item_string);
3569                 }
3570                 else
3571                         wprintw(menu_win, more_below_str);
3572         }
3573         else
3574         {
3575                 for (counter = 1; counter <= list_size; counter++)
3576                 {
3577                         wmove(menu_win, (top_offset + counter - 1), 3);
3578                         if (list_size > 1)
3579                                 wprintw(menu_win, "%c) ", item_alpha[min((counter - 1), max_alpha_char)]);
3580                         waddstr(menu_win, menu_list[counter].item_string);
3581                 }
3582         }
3583 }
3584
3585 void 
3586 help(void)
3587 {
3588         int counter;
3589
3590         werase(help_win);
3591         clearok(help_win, TRUE);
3592         for (counter = 0; counter < 22; counter++)
3593         {
3594                 wmove(help_win, counter, 0);
3595                 waddstr(help_win, (emacs_keys_mode) ? 
3596                         emacs_help_text[counter] : help_text[counter]);
3597         }
3598         wrefresh(help_win);
3599         werase(com_win);
3600         wmove(com_win, 0, 0);
3601         wprintw(com_win, press_any_key_msg);
3602         wrefresh(com_win);
3603         counter = wgetch(com_win);
3604         werase(com_win);
3605         wmove(com_win, 0, 0);
3606         werase(help_win);
3607         wrefresh(help_win);
3608         wrefresh(com_win);
3609         redraw();
3610 }
3611
3612 void 
3613 paint_info_win(void)
3614 {
3615         int counter;
3616
3617         if (!info_window)
3618                 return;
3619
3620         werase(info_win);
3621         for (counter = 0; counter < 5; counter++)
3622         {
3623                 wmove(info_win, counter, 0);
3624                 wclrtoeol(info_win);
3625                 if (info_type == CONTROL_KEYS)
3626                         waddstr(info_win, (emacs_keys_mode) ? 
3627                           emacs_control_keys[counter] : control_keys[counter]);
3628                 else if (info_type == COMMANDS)
3629                         waddstr(info_win, command_strings[counter]);
3630         }
3631         wrefresh(info_win);
3632 }
3633
3634 void 
3635 no_info_window(void)
3636 {
3637         if (!info_window)
3638                 return;
3639         delwin(info_win);
3640         delwin(text_win);
3641         info_window = FALSE;
3642         last_line = LINES - 2;
3643         text_win = newwin((LINES - 1), COLS, 0, 0);
3644         keypad(text_win, TRUE);
3645         idlok(text_win, TRUE);
3646         clearok(text_win, TRUE);
3647         midscreen(scr_vert, point);
3648         wrefresh(text_win);
3649         clear_com_win = TRUE;
3650 }
3651
3652 void 
3653 create_info_window(void)
3654 {
3655         if (info_window)
3656                 return;
3657         last_line = LINES - 8;
3658         delwin(text_win);
3659         text_win = newwin((LINES - 7), COLS, 6, 0);
3660         keypad(text_win, TRUE);
3661         idlok(text_win, TRUE);
3662         werase(text_win);
3663         info_window = TRUE;
3664         info_win = newwin(5, COLS, 0, 0);
3665         werase(info_win);
3666         info_type = CONTROL_KEYS;
3667         midscreen(min(scr_vert, last_line), point);
3668         clearok(info_win, TRUE);
3669         paint_info_win();
3670         count_win = newwin(1, COLS, 5, 0);
3671         leaveok(count_win, TRUE);
3672         wrefresh(count_win);
3673         wrefresh(text_win);
3674         clear_com_win = TRUE;
3675 }
3676
3677 int 
3678 file_op(int arg)
3679 {
3680         char *string;
3681         int flag;
3682
3683         if (restrict_mode())
3684         {
3685                 return(0);
3686         }
3687
3688         if (arg == READ_FILE)
3689         {
3690                 string = get_string(file_read_prompt_str, TRUE);
3691                 recv_file = TRUE;
3692                 tmp_file = resolve_name(string);
3693                 check_fp();
3694                 if (tmp_file != string)
3695                         free(tmp_file);
3696                 free(string);
3697         }
3698         else if (arg == WRITE_FILE)
3699         {
3700                 string = get_string(file_write_prompt_str, TRUE);
3701                 tmp_file = resolve_name(string);
3702                 write_file(tmp_file);
3703                 if (tmp_file != string)
3704                         free(tmp_file);
3705                 free(string);
3706         }
3707         else if (arg == SAVE_FILE)
3708         {
3709         /*
3710          |      changes made here should be reflected in finish()
3711          */
3712
3713                 if (in_file_name)
3714                         flag = TRUE;
3715                 else
3716                         flag = FALSE;
3717
3718                 string = in_file_name;
3719                 if ((string == NULL) || (*string == (char) NULL))
3720                         string = get_string(save_file_name_prompt, TRUE);
3721                 if ((string == NULL) || (*string == (char) NULL))
3722                 {
3723                         wmove(com_win, 0, 0);
3724                         wprintw(com_win, file_not_saved_msg);
3725                         wclrtoeol(com_win);
3726                         wrefresh(com_win);
3727                         clear_com_win = TRUE;
3728                         return(0);
3729                 }
3730                 if (!flag)
3731                 {
3732                         tmp_file = resolve_name(string);
3733                         if (tmp_file != string)
3734                         {
3735                                 free(string);
3736                                 string = tmp_file;
3737                         }
3738                 }
3739                 if (write_file(string))
3740                 {
3741                         in_file_name = string;
3742                         text_changes = FALSE;
3743                 }
3744                 else if (!flag)
3745                         free(string);
3746         }
3747         return(0);
3748 }
3749
3750 void 
3751 shell_op(void)
3752 {
3753         char *string;
3754
3755         if (((string = get_string(shell_prompt, TRUE)) != NULL) && 
3756                         (*string != (char) NULL))
3757         {
3758                 sh_command(string);
3759                 free(string);
3760         }
3761 }
3762
3763 void 
3764 leave_op(void)
3765 {
3766         if (text_changes)
3767         {
3768                 menu_op(leave_menu);
3769         }
3770         else
3771                 quit(TRUE);
3772 }
3773
3774 void 
3775 redraw(void)
3776 {
3777         if (info_window)
3778         {
3779                 clearok(info_win, TRUE);
3780                 paint_info_win();
3781         }
3782         else
3783                 clearok(text_win, TRUE);
3784         midscreen(scr_vert, point);
3785 }
3786
3787 /*
3788  |      The following routines will "format" a paragraph (as defined by a 
3789  |      block of text with blank lines before and after the block).
3790  */
3791
3792 int 
3793 Blank_Line(struct text *test_line)
3794 /* test if line has any non-space characters */
3795 {
3796         unsigned char *line;
3797         int length;
3798         
3799         if (test_line == NULL)
3800                 return(TRUE);
3801
3802         length = 1;
3803         line = test_line->line;
3804
3805         /*
3806          |      To handle troff/nroff documents, consider a line with a 
3807          |      period ('.') in the first column to be blank.  To handle mail 
3808          |      messages with included text, consider a line with a '>' blank.
3809          */
3810
3811         if ((*line == '.') || (*line == '>'))
3812                 return(TRUE);
3813
3814         while (((*line == ' ') || (*line == '\t')) && (length < test_line->line_length))
3815         {
3816                 length++;
3817                 line++;
3818         }
3819         if (length != test_line->line_length)
3820                 return(FALSE);
3821         else
3822                 return(TRUE);
3823 }
3824
3825 void 
3826 Format(void)    /* format the paragraph according to set margins        */
3827 {
3828         int string_count;
3829         int offset;
3830         int temp_case;
3831         int status;
3832         int tmp_af;
3833         int counter;
3834         unsigned char *line;
3835         unsigned char *tmp_srchstr;
3836         unsigned char *temp1, *temp2;
3837         unsigned char *temp_dword;
3838         unsigned char temp_d_char[3];
3839
3840         temp_d_char[0] = d_char[0];
3841         temp_d_char[1] = d_char[1];
3842         temp_d_char[2] = d_char[2];
3843
3844 /*
3845  |      if observ_margins is not set, or the current line is blank, 
3846  |      do not format the current paragraph
3847  */
3848
3849         if ((!observ_margins) || (Blank_Line(curr_line)))
3850                 return;
3851
3852 /*
3853  |      save the currently set flags, and clear them
3854  */
3855
3856         wmove(com_win, 0, 0);
3857         wclrtoeol(com_win);
3858         wprintw(com_win, formatting_msg);
3859         wrefresh(com_win);
3860
3861 /*
3862  |      get current position in paragraph, so after formatting, the cursor 
3863  |      will be in the same relative position
3864  */
3865
3866         tmp_af = auto_format;
3867         auto_format = FALSE;
3868         offset = position;
3869         if (position != 1)
3870                 prev_word();
3871         temp_dword = d_word;
3872         d_word = NULL;
3873         temp_case = case_sen;
3874         case_sen = TRUE;
3875         tmp_srchstr = srch_str;
3876         temp2 = srch_str = (unsigned char *) malloc(1 + curr_line->line_length - position);
3877         if ((*point == ' ') || (*point == '\t'))
3878                 adv_word();
3879         offset -= position;
3880         counter = position;
3881         line = temp1 = point;
3882         while ((*temp1 != (char) NULL) && (*temp1 != ' ') && (*temp1 != '\t') && (counter < curr_line->line_length))
3883         {
3884                 *temp2 = *temp1;
3885                 temp2++;
3886                 temp1++;
3887                 counter++;
3888         }
3889         *temp2 = (char) NULL;
3890         if (position != 1)
3891                 bol();
3892         while (!Blank_Line(curr_line->prev_line))
3893                 bol();
3894         string_count = 0;
3895         status = TRUE;
3896         while ((line != point) && (status))
3897         {
3898                 status = search(FALSE);
3899                 string_count++;
3900         }
3901
3902         wmove(com_win, 0, 0);
3903         wclrtoeol(com_win);
3904         wprintw(com_win, formatting_msg);
3905         wrefresh(com_win);
3906
3907 /*
3908  |      now get back to the start of the paragraph to start formatting
3909  */
3910
3911         if (position != 1)
3912                 bol();
3913         while (!Blank_Line(curr_line->prev_line))
3914                 bol();
3915
3916         observ_margins = FALSE;
3917
3918 /*
3919  |      Start going through lines, putting spaces at end of lines if they do 
3920  |      not already exist.  Append lines together to get one long line, and 
3921  |      eliminate spacing at begin of lines.
3922  */
3923
3924         while (!Blank_Line(curr_line->next_line))
3925         {
3926                 eol();
3927                 left(TRUE);
3928                 if (*point != ' ')
3929                 {
3930                         right(TRUE);
3931                         insert(' ');
3932                 }
3933                 else
3934                         right(TRUE);
3935                 del_char();
3936                 if ((*point == ' ') || (*point == '\t'))
3937                         del_word();
3938         }
3939
3940 /*
3941  |      Now there is one long line.  Eliminate extra spaces within the line
3942  |      after the first word (so as not to blow away any indenting the user 
3943  |      may have put in).
3944  */
3945
3946         bol();
3947         adv_word();
3948         while (position < curr_line->line_length)
3949         {
3950                 if ((*point == ' ') && (*(point + 1) == ' '))
3951                         del_char();
3952                 else
3953                         right(TRUE);
3954         }
3955
3956 /*
3957  |      Now make sure there are two spaces after a '.'.
3958  */
3959
3960         bol();
3961         while (position < curr_line->line_length)
3962         {
3963                 if ((*point == '.') && (*(point + 1) == ' '))
3964                 {
3965                         right(TRUE);
3966                         insert(' ');
3967                         insert(' ');
3968                         while (*point == ' ')
3969                                 del_char();
3970                 }
3971                 right(TRUE);
3972         }
3973
3974         observ_margins = TRUE;
3975         bol();
3976
3977         wmove(com_win, 0, 0);
3978         wclrtoeol(com_win);
3979         wprintw(com_win, formatting_msg);
3980         wrefresh(com_win);
3981
3982 /*
3983  |      create lines between margins
3984  */
3985
3986         while (position < curr_line->line_length)
3987         {
3988                 while ((scr_pos < right_margin) && (position < curr_line->line_length))
3989                         right(TRUE);
3990                 if (position < curr_line->line_length)
3991                 {
3992                         prev_word();
3993                         if (position == 1)
3994                                 adv_word();
3995                         insert_line(TRUE);
3996                 }
3997         }
3998
3999 /*
4000  |      go back to begin of paragraph, put cursor back to original position
4001  */
4002
4003         bol();
4004         while (!Blank_Line(curr_line->prev_line))
4005                 bol();
4006
4007 /*
4008  |      find word cursor was in
4009  */
4010
4011         while ((status) && (string_count > 0))
4012         {
4013                 search(FALSE);
4014                 string_count--;
4015         }
4016
4017 /*
4018  |      offset the cursor to where it was before from the start of the word
4019  */
4020
4021         while (offset > 0)
4022         {
4023                 offset--;
4024                 right(TRUE);
4025         }
4026
4027 /*
4028  |      reset flags and strings to what they were before formatting
4029  */
4030
4031         if (d_word != NULL)
4032                 free(d_word);
4033         d_word = temp_dword;
4034         case_sen = temp_case;
4035         free(srch_str);
4036         srch_str = tmp_srchstr;
4037         d_char[0] = temp_d_char[0];
4038         d_char[1] = temp_d_char[1];
4039         d_char[2] = temp_d_char[2];
4040         auto_format = tmp_af;
4041
4042         midscreen(scr_vert, point);
4043         werase(com_win);
4044         wrefresh(com_win);
4045 }
4046
4047 unsigned char *init_name[3] = {
4048         "/usr/share/misc/init.ee", 
4049         NULL, 
4050         ".init.ee"
4051         };
4052
4053 void 
4054 ee_init(void)   /* check for init file and read it if it exists */
4055 {
4056         FILE *init_file;
4057         unsigned char *string;
4058         unsigned char *str1;
4059         unsigned char *str2;
4060         char *home;
4061         int counter;
4062         int temp_int;
4063
4064         string = getenv("HOME");
4065         if (!string)
4066             string = "/root"; /* Set to reasonable default so we don't crash */
4067         str1 = home = malloc(strlen(string)+10);
4068         strcpy(home, string);
4069         strcat(home, "/.init.ee");
4070         init_name[1] = home;
4071         string = malloc(512);
4072
4073         for (counter = 0; counter < 3; counter++)
4074         {
4075                 if (!(access(init_name[counter], 4)))
4076                 {
4077                         init_file = fopen(init_name[counter], "r");
4078                         while ((str2 = fgets(string, 512, init_file)) != NULL)
4079                         {
4080                                 str1 = str2 = string;
4081                                 while (*str2 != '\n')
4082                                         str2++;
4083                                 *str2 = (char) NULL;
4084
4085                                 if (unique_test(string, init_strings) != 1)
4086                                         continue;
4087
4088                                 if (compare(str1, CASE, FALSE))
4089                                         case_sen = TRUE;
4090                                 else if (compare(str1, NOCASE, FALSE))
4091                                         case_sen = FALSE;
4092                                 else if (compare(str1, EXPAND, FALSE))
4093                                         expand_tabs = TRUE;