forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmined1.c
More file actions
1878 lines (1692 loc) · 55.9 KB
/
Copy pathmined1.c
File metadata and controls
1878 lines (1692 loc) · 55.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Part one of the mined editor.
*/
/*
* Author: Michiel Huisjes.
*
* 1. General remarks.
*
* Mined is a screen editor designed for the MINIX operating system.
* It is meant to be used on files not larger than 50K and to be fast.
* When mined starts up, it reads the file into its memory to minimize
* disk access. The only time that disk access is needed is when certain
* save, write or copy commands are given.
*
* Mined has the style of Emacs or Jove, that means that there are no modes.
* Each character has its own entry in an 256 pointer to function array,
* which is called when that character is typed. Only ASCII characters are
* connected with a function that inserts that character at the current
* location in the file. Two execptions are <linefeed> and <tab> which are
* inserted as well. Note that the mapping between commands and functions
* called is implicit in the table. Changing the mapping just implies
* changing the pointers in this table.
*
* The display consists of SCREENMAX + 1 lines and XMAX + 1 characters. When
* a line is larger (or gets larger during editing) than XBREAK characters,
* the line is either shifted SHIFT_SIZE characters to the left (which means
* that the first SHIFT_SIZE characters are not printed) or the end of the
* line is marked with the SHIFT_MARK character and the rest of the line is
* not printed. A line can never exceed MAX_CHARS characters. Mined will
* always try to keep the cursor on the same line and same (relative)
* x-coordinate if nothing changed. So if you scroll one line up, the cursor
* stays on the same line, or when you move one line down, the cursor will
* move to the same place on the line as it was on the previous.
* Every character on the line is available for editing including the
* linefeed at the the of the line. When the linefeed is deleted, the current
* line and the next line are joined. The last character of the file (which
* is always a linefeed) can never be deleted.
* The bottomline (as indicated by YMAX + 1) is used as a status line during
* editing. This line is usually blank or contains information mined needs
* during editing. This information (or rather questions) is displayed in
* reverse video.
*
* The terminal modes are changed completely. All signals like start/stop,
* interrupt etc. are unset. The only signal that remains is the quit signal.
* The quit signal (^\) is the general abort signal for mined. Typing a ^\
* during searching or when mined is asking for filenames, etc. will abort
* the function and mined will return to the main loop. Sending a quit
* signal during the main loop will abort the session (after confirmation)
* and the file is not (!) saved.
* The session will also be aborted when an unrecoverable error occurs. E.g
* when there is no more memory available. If the file has been modified,
* mined will ask if the file has to be saved or not.
* If there is no more space left on the disk, mined will just give an error
* message and continue.
*
* The number of system calls are minized. This is done to keep the editor
* as fast as possible. I/O is done in SCREEN_SIZE reads/writes. Accumulated
* output is also flushed at the end of each character typed.
*
* 2. Regular expressions
*
* Mined has a build in regular expression matcher, which is used for
* searching and replace routines. A regular expression consists of a
* sequence of:
*
* 1. A normal character matching that character.
* 2. A . matching any character.
* 3. A ^ matching the begin of a line.
* 4. A $ (as last character of the pattern) mathing the end of a line.
* 5. A \<character> matching <character>.
* 6. A number of characters enclosed in [] pairs matching any of these
* characters. A list of characters can be indicated by a '-'. So
* [a-z] matches any letter of the alphabet. If the first character
* after the '[' is a '^' then the set is negated (matching none of
* the characters).
* A ']', '^' or '-' can be escaped by putting a '\' in front of it.
* Of course this means that a \ must be represented by \\.
* 7. If one of the expressions as described in 1-6 is followed by a
* '*' than that expressions matches a sequence of 0 or more of
* that expression.
*
* Parsing of regular expression is done in two phases. In the first phase
* the expression is compiled into a more comprehensible form. In the second
* phase the actual matching is done. For more details see 3.6.
*
*
* 3. Implementation of mined.
*
* 3.1 Data structures.
*
* The main data structures are as follows. The whole file is kept in a
* double linked list of lines. The LINE structure looks like this:
*
* typedef struct Line {
* struct Line *next;
* struct Line *prev;
* char *text;
* unsigned char shift_count;
* } LINE;
*
* Each line entry contains a pointer to the next line, a pointer to the
* previous line and a pointer to the text of that line. A special field
* shift_count contains the number of shifts (in units of SHIFT_SIZE)
* that is performed on that line. The total size of the structure is 7
* bytes so a file consisting of 1000 empty lines will waste a lot of
* memory. A LINE structure is allocated for each line in the file. After
* that the number of characters of the line is counted and sufficient
* space is allocated to store them (including a linefeed and a '\0').
* The resulting address is assigned to the text field in the structure.
*
* A special structure is allocated and its address is assigned to the
* variable header as well as the variable tail. The text field of this
* structure is set to NULL. The tail->prev of this structure points
* to the last LINE of the file and the header->next to the first LINE.
* Other LINE *variables are top_line and bot_line which point to the
* first line resp. the last line on the screen.
* Two other variables are important as well. First the LINE *cur_line,
* which points to the LINE currently in use and the char *cur_text,
* which points to the character at which the cursor stands.
* Whenever an ASCII character is typed, a new line is build with this
* character inserted. Then the old data space (pointed to by
* cur_line->text) is freed, data space for the new line is allocated and
* assigned to cur_line->text.
*
* Two global variables called x and y represent the x and y coordinates
* from the cursor. The global variable nlines contains the number of
* lines in the file. Last_y indicates the maximum y coordinate of the
* screen (which is usually SCREENMAX).
*
* A few strings must be initialized by hand before compiling mined.
* These string are enter_string, which is printed upon entering mined,
* rev_video (turn on reverse video), normal_video, rev_scroll (perform a
* reverse scroll) and pos_string. The last string should hold the
* absolute position string to be printed for cursor motion. The #define
* X_PLUS and Y_PLUS should contain the characters to be added to the
* coordinates x and y (both starting at 0) to finish cursor positioning.
*
* 3.2 Starting up.
*
* Mined can be called with or without argument and the function
* load_file () is called with these arguments. load_file () checks
* if the file exists if it can be read and if it is writable and
* sets the writable flag accordingly. If the file can be read,
* load_file () reads a line from the file and stores this line into
* a structure by calling install_line () and line_insert () which
* installs the line into the double linked list, until the end of the
* file is reached.
* Lines are read by the function get_line (), which buffers the
* reading in blocks of SCREEN_SIZE. Load_file () also initializes the
* LINE *variables described above.
*
* 3.3 Moving around.
*
* Several commands are implemented for moving through the file.
* Moving up (UP), down (DN) left (LF) and right (RT) are done by the
* arrow keys. Moving one line below the screen scrolls the screen one
* line up. Moving one line above the screen scrolls the screen one line
* down. The functions forward_scroll () and reverse_scroll () take care
* of that.
* Several other move functions exist: move to begin of line (BL), end of
* line (EL) top of screen (HIGH), bottom of screen (LOW), top of file
* (HO), end of file (EF), scroll one page down (PD), scroll one page up
* (PU), scroll one line down (SD), scroll one line up (SU) and move to a
* certain line number (GOTO).
* Two functions called MN () and MP () each move one word further or
* backwards. A word is a number of non-blanks seperated by a space, a
* tab or a linefeed.
*
* 3.4 Modifying text.
*
* The modifying commands can be separated into two modes. The first
* being inserting text, and the other deleting text. Two functions are
* created for these purposes: insert () and delete (). Both are capable
* of deleting or inserting large amounts of text as well as one
* character. Insert () must be given the line and location at which
* the text must be inserted. Is doesn't make any difference whether this
* text contains linefeeds or not. Delete () must be given a pointer to
* the start line, a pointer from where deleting should start on that
* line and the same information about the end position. The last
* character of the file will never be deleted. Delete () will make the
* necessary changes to the screen after deleting, but insert () won't.
* The functions for modifying text are: insert one char (S), insert a
* file (file_insert (fd)), insert a linefeed and put cursor back to
* end of line (LIB), delete character under the cursor (DCC), delete
* before cursor (even linefeed) (DPC), delete next word (DNW), delete
* previous word (DPC) and delete to end of line (if the cursor is at
* a linefeed delete line) (DLN).
*
* 3.5 Yanking.
*
* A few utilities are provided for yanking pieces of text. The function
* MA () marks the current position in the file. This is done by setting
* LINE *mark_line and char *mark_text to the current position. Yanking
* of text can be done in two modes. The first mode just copies the text
* from the mark to the current position (or visa versa) into a buffer
* (YA) and the second also deletes the text (DT). Both functions call
* the function set_up () with the delete flag on or off. Set_up ()
* checks if the marked position is still a valid one (by using
* check_mark () and legal ()), and then calls the function yank () with
* a start and end position in the file. This function copies the text
* into a scratch_file as indicated by the variable yank_file. This
* scratch_file is made uniq by the function scratch_file (). At the end
* of copying yank will (if necessary) delete the text. A global flag
* called yank_status keeps track of the buffer (or file) status. It is
* initialized on NOT_VALID and set to EMPTY (by set_up ()) or VALID (by
* yank ()). Several things can be done with the buffer. It can be
* inserted somewhere else in the file (PT) or it can be copied into
* another file (WB), which will be prompted for.
*
* 3.6 Search and replace routines.
*
* Searching for strings and replacing strings are done by regular
* expressions. For any expression the function compile () is called
* with as argument the expression to compile. Compile () returns a
* pointer to a structure which looks like this:
*
* typedef struct regex {
* union {
* char *err_mess;
* int *expression;
* } result;
* char status;
* char *start_ptr;
* char *end_ptr;
* } REGEX;
*
* If something went wrong during compiling (e.g. an illegal expression
* was given), the function reg_error () is called, which sets the status
* field to REG_ERROR and the err_mess field to the error message. If the
* match must be anchored at the beginning of the line (end of line), the
* status field is set to BEGIN_LINE (END_LINE). If none of these special
* cases are true, the field is set to 0 and the function finished () is
* called. Finished () allocates space to hold the compiled expression
* and copies this expression into the expression field of the union
* (bcopy ()). Matching is done by the routines match() and line_check().
* Match () takes as argument the REGEX *program, a pointer to the
* startposition on the current line, and a flag indicating FORWARD or
* REVERSE search. Match () checks out the whole file until a match is
* found. If match is found it returns a pointer to the line in which the
* match was found else it returns a NULL. Line_check () takes the
* same arguments, but return either MATCH or NO_MATCH.
* During checking, the start_ptr and end_ptr fields of the REGEX
* structure are assigned to the start and end of the match.
* Both functions try to find a match by walking through the line
* character by character. For each possibility, the function
* check_string () is called with as arguments the REGEX *program and the
* string to search in. It starts walking through the expression until
* the end of the expression or the end of the string is reached.
* Whenever a * is encountered, this position of the string is marked,
* the maximum number of matches are performed and the function star ()
* is called in order to try to find the longest match possible. Star ()
* takes as arguments the REGEX program, the current position of the
* string, the marked position and the current position of the expression
* Star () walks from the current position of the string back to the
* marked position, and calls string_check () in order to find a match.
* It returns MATCH or NO_MATCH, just as string_check () does.
* Searching is now easy. Both search routines (forward (SF) and
* backwards search (SR)) call search () with an apropiate message and a
* flag indicating FORWARD or REVERSE search. Search () will get an
* expression from the user by calling get_expression(). Get_expression()
* returns a pointer to a REGEX structure or NULL upon errors and
* prompts for the expression. If no expression if given, the previous is
* used instead. After that search will call match (), and if a match is
* found, we can move to that place in the file by the functions find_x()
* and find_y () which will find display the match on the screen.
* Replacing can be done in two ways. A global replace (GR) or a line
* replace (LR). Both functions call change () with a message an a flag
* indicating global or line replacement. Change () will prompt for the
* expression and for the replacement. Every & in the replacement pattern
* means substitute the match instead. An & can be escaped by a \. When
* a match is found, the function substitute () will perform the
* substitution.
*
* 3.6 Miscellaneous commands.
*
* A few commands haven't be discussed yet. These are redraw the screen
* (RD) fork a shell (SH), print file status (FS), write file to disc
* (WT), insert a file at current position (IF), leave editor (XT) and
* visit another file (VI). The last two functions will check if the file
* has been modified. If it has, they will ask if you want to save the
* file by calling ask_save ().
* The function ESC () will repeat a command n times. It will prompt for
* the number. Aborting the loop can be done by sending the ^\ signal.
*
* 3.7 Utility functions.
*
* Several functions exists for internal use. First allocation routines:
* alloc (bytes) and newline () will return a pointer to free data space
* if the given size. If there is no more memory available, the function
* panic () is called.
* Signal handling: The only signal that can be send to mined is the
* SIGQUIT signal. This signal, functions as a general abort command.
* Mined will abort if the signal is given during the main loop. The
* function abort_mined () takes care of that.
* Panic () is a function with as argument a error message. It will print
* the message and the error number set by the kernel (errno) and will
* ask if the file must be saved or not. It resets the terminal
* (raw_mode ()) and exits.
* String handling routines like copy_string(to, from), length_of(string)
* and build_string (buffer, format, arg1, arg2, ...). The latter takes
* a description of the string out out the format field and puts the
* result in the buffer. (It works like printf (3), but then into a
* string). The functions status_line (string1, string2), error (string1,
* string2), clear_status () and bottom_line () all print information on
* the status line.
* Get_string (message, buffer) reads a string and getchar () reads one
* character from the terminal.
* Num_out ((long) number) prints the number into a 11 digit field
* without leading zero's. It returns a pointer to the resulting string.
* File_status () prints all file information on the status line.
* Set_cursor (x, y) prints the string to put the cursor at coordinates
* x and y.
* Output is done by four functions: writeline(fd,string), clear_buffer()
* write_char (fd, c) and flush_buffer (fd). Three defines are provided
* to write on filedescriptor STD_OUT (terminal) which is used normally:
* string_print (string), putchar (c) and flush (). All these functions
* use the global I/O buffer screen and the global index for this array
* called out_count. In this way I/O can be buffered, so that reads or
* writes can be done in blocks of SCREEN_SIZE size.
* The following functions all handle internal line maintenance. The
* function proceed (start_line, count) returns the count'th line after
* start_line. If count is negative, the count'th line before the
* start_line is returned. If header or tail is encountered then that
* will be returned. Display (x, y, start_line, count) displays count
* lines starting at coordinates [x, y] and beginning at start_line. If
* the header or tail is encountered, empty lines are displayed instead.
* The function reset (head_line, ny) reset top_line, last_y, bot_line,
* cur_line and y-coordinate. This is not a neat way to do the
* maintenance, but it sure saves a lot of code. It is usually used in
* combination with display ().
* Put_line(line, offset, clear_line), prints a line (skipping characters
* according to the line->shift_size field) until XBREAK - offset
* characters are printed or a '\n' is encountered. If clear_line is
* TRUE, spaces are printed until XBREAK - offset characters.
* Line_print (line) is a #define from put_line (line, 0, TRUE).
* Moving is done by the functions move_to (x, y), move_addres (address)
* and move (x, adress, y). This function is the most important one in
* mined. New_y must be between 0 and last_y, new_x can be about
* anything, address must be a pointer to an character on the current
* line (or y). Move_to () first adjust the y coordinate together with
* cur_line. If an address is given, it finds the corresponding
* x-coordinate. If an new x-coordinate was given, it will try to locate
* the corresponding character. After that it sets the shift_count field
* of cur_line to an apropiate number according to new_x. The only thing
* left to do now is to assign the new values to cur_line, cur_text, x
* and y.
*
* 4. Summary of commands.
*
* CURSOR MOTION
* up-arrow Move cursor 1 line up. At top of screen, reverse scroll
* down-arrow Move cursor 1 line down. At bottom, scroll forward.
* left-arrow Move cursor 1 character left or to end of previous line
* right-arrow Move cursor 1 character right or to start of next line
* CTRL-A Move cursor to start of current line
* CTRL-Z Move cursor to end of current line
* CTRL-^ Move cursor to top of screen
* CTRL-_ Move cursor to bottom of screen
* CTRL-F Forward to start of next word (even to next line)
* CTRL-B Backward to first character of previous word
*
* SCREEN MOTION
* Home key Move cursor to first character of file
* End key Move cursor to last character of file
* PgUp Scroll backward 1 page. Bottom line becomes top line
* PgD Scroll backward 1 page. Top line becomes bottom line
* CTRL-D Scroll screen down one line (reverse scroll)
* CTRL-U Scroll screen up one line (forward scroll)
*
* MODIFYING TEXT
* ASCII char Self insert character at cursor
* tab Insert tab at cursor
* backspace Delete the previous char (left of cursor), even line feed
* Del Delete the character under the cursor
* CTRL-N Delete next word
* CTRL-P Delete previous word
* CTRL-O Insert line feed at cursor and back up 1 character
* CTRL-T Delete tail of line (cursor to end); if empty, delete line
* CTRL-@ Set the mark (remember the current location)
* CTRL-K Delete text from the mark to current position save on file
* CTRL-C Save the text from the mark to the current position
* CTRL-Y Insert the contents of the save file at current position
* CTRL-Q Insert the contents of the save file into a new file
* CTRL-G Insert a file at the current position
*
* MISCELLANEOUS
* CTRL-E Erase and redraw the screen
* CTRL-V Visit file (read a new file); complain if old one changed
* CTRL-W Write the current file back to the disk
* numeric + Search forward (prompt for regular expression)
* numeric - Search backward (prompt for regular expression)
* numeric 5 Print the current status of the file
* CTRL-R (Global) Replace str1 by str2 (prompts for each string)
* CTRL-L (Line) Replace string1 by string2
* CTRL-S Fork off a shell and wait for it to finish
* CTRL-X EXIT (prompt if file modified)
* CTRL-] Go to a line. Prompts for linenumber
* CTRL-\ Abort whatever editor was doing and start again
* escape key Repeat a command count times; (prompts for count)
*/
/* ======================================================================== *
* Utilities *
* ======================================================================== */
#include "mined.h"
#include <signal.h>
#include <termios.h>
#include <limits.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
extern int errno;
int ymax = YMAX;
int screenmax = SCREENMAX;
/*
* Print file status.
*/
void FS(void)
{
fstatus(file_name[0] ? "" : "[buffer]", -1L);
}
/*
* Visit (edit) another file. If the file has been modified, ask the user if
* he wants to save it.
*/
void VI(void)
{
char new_file[LINE_LEN]; /* Buffer to hold new file name */
if (modified == TRUE && ask_save() == ERRORS)
return;
/* Get new file name */
if (get_file("Visit file:", new_file) == ERRORS)
return;
/* Free old linked list, initialize global variables and load new file */
initialize();
#ifdef UNIX
tputs(CL, 0, _putchar);
#else
string_print (enter_string);
#endif /* UNIX */
load_file(new_file[0] == '\0' ? NULL : new_file);
}
/*
* Write file in core to disc.
*/
int WT(void)
{
register LINE *line;
register long count = 0L; /* Nr of chars written */
char file[LINE_LEN]; /* Buffer for new file name */
int fd; /* Filedescriptor of file */
if (modified == FALSE) {
error ("Write not necessary.", NULL);
return FINE;
}
/* Check if file_name is valid and if file can be written */
if (file_name[0] == '\0' || writable == FALSE) {
if (get_file("Enter file name:", file) != FINE)
return ERRORS;
copy_string(file_name, file); /* Save file name */
}
if ((fd = creat(file_name, 0644)) < 0) { /* Empty file */
error("Cannot create ", file_name);
writable = FALSE;
return ERRORS;
}
else
writable = TRUE;
clear_buffer();
status_line("Writing ", file_name);
for (line = header->next; line != tail; line = line->next) {
if (line->shift_count & DUMMY) {
if (line->next == tail && line->text[0] == '\n')
continue;
}
if (writeline(fd, line->text) == ERRORS) {
count = -1L;
break;
}
count += (long) length_of(line->text);
}
if (count > 0L && flush_buffer(fd) == ERRORS)
count = -1L;
(void) close(fd);
if (count == -1L)
return ERRORS;
modified = FALSE;
rpipe = FALSE; /* File name is now assigned */
/* Display how many chars (and lines) were written */
fstatus("Wrote", count);
return FINE;
}
/* Call WT and discard value returned. */
void XWT(void)
{
(void) WT();
}
/*
* Call an interactive shell.
*/
void SH(void)
{
register int w;
int pid, status;
char *shell;
if ((shell = getenv("SHELL")) == NULL) shell = "/bin/sh";
switch (pid = fork()) {
case -1: /* Error */
error("Cannot fork.", NULL);
return;
case 0: /* This is the child */
set_cursor(0, ymax);
putchar('\n');
flush();
raw_mode(OFF);
if (rpipe) { /* Fix stdin */
close (0);
if (open("/dev/tty", 0) < 0)
exit (126);
}
execl(shell, shell, (char *) 0);
exit(127); /* Exit with 127 */
default : /* This is the parent */
signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
do {
w = wait(&status);
} while (w != -1 && w != pid);
}
raw_mode(ON);
RD();
if ((status >> 8) == 127) /* Child died with 127 */
error("Cannot exec ", shell);
else if ((status >> 8) == 126)
error("Cannot open /dev/tty as fd #0", NULL);
}
/*
* Proceed returns the count'th line after `line'. When count is negative
* it returns the count'th line before `line'. When the next (previous)
* line is the tail (header) indicating EOF (tof) it stops.
*/
LINE *proceed(register LINE *line, register int count)
{
if (count < 0)
while (count++ < 0 && line != header)
line = line->prev;
else
while (count-- > 0 && line != tail)
line = line->next;
return line;
}
/*
* Show concatenation of s1 and s2 on the status line (bottom of screen)
* If revfl is TRUE, turn on reverse video on both strings. Set stat_visible
* only if bottom_line is visible.
*/
int bottom_line(FLAG revfl, char *s1, char *s2, char *inbuf, FLAG statfl)
{
int ret = FINE;
char buf[LINE_LEN];
register char *p = buf;
*p++ = ' ';
if (s1 != NULL)
while ((*p = *s1++))
p++;
if (s2 != NULL)
while ((*p = *s2++))
p++;
*p++ = ' ';
*p++ = 0;
if (revfl == ON && stat_visible == TRUE)
clear_status ();
set_cursor(0, ymax);
if (revfl == ON) { /* Print rev. start sequence */
#ifdef UNIX
tputs(SO, 0, _putchar);
#else
string_print(rev_video);
#endif /* UNIX */
stat_visible = TRUE;
}
else /* Used as clear_status() */
stat_visible = FALSE;
string_print(buf);
if (inbuf != NULL)
ret = input(inbuf, statfl);
/* Print normal video */
#ifdef UNIX
tputs(SE, 0, _putchar);
tputs(CE, 0, _putchar);
#else
string_print(normal_video);
string_print(blank_line); /* Clear the rest of the line */
#endif /* UNIX */
if (inbuf != NULL)
set_cursor(0, ymax);
else
set_cursor(x, y); /* Set cursor back to old position */
flush(); /* Perform the actual write */
if (ret != FINE)
clear_status();
return ret;
}
/*
* Count_chars() count the number of chars that the line would occupy on the
* screen. Counting starts at the real x-coordinate of the line.
*/
int count_chars(LINE *line)
{
register int cnt = get_shift(line->shift_count) * -SHIFT_SIZE;
register char *textp = line->text;
/* Find begin of line on screen */
while (cnt < 0) {
if (is_tab(*textp++))
cnt = tab(cnt);
else
cnt++;
}
/* Count number of chars left */
cnt = 0;
while (*textp != '\n') {
if (is_tab(*textp++))
cnt = tab(cnt);
else
cnt++;
}
return cnt;
}
/*
* Move to coordinates nx, ny at screen. The caller must check that scrolling
* is not needed.
* If new_x is lower than 0 or higher than XBREAK, move_to() will check if
* the line can be shifted. If it can it sets(or resets) the shift_count field
* of the current line accordingly.
* Move also sets cur_text to the right char.
* If we're moving to the same x coordinate, try to move the the x-coordinate
* used on the other previous call.
*/
void move(register int new_x, char *new_address, int new_y)
{
register LINE *line = cur_line; /* For building new cur_line */
int shift = 0; /* How many shifts to make */
static int rel_x = 0; /* Remember relative x position */
int tx = x;
/* Check for illegal values */
if (new_y < 0 || new_y > last_y)
return;
/* Adjust y-coordinate and cur_line */
if (new_y < y)
while (y != new_y) {
y--;
line = line->prev;
}
else
while (y != new_y) {
y++;
line = line->next;
}
/* Set or unset relative x-coordinate */
if (new_address == NULL) {
new_address = find_address(line, (new_x == x) ? rel_x : new_x , &tx);
if (new_x != x)
rel_x = tx;
new_x = tx;
}
else
rel_x = new_x = find_x(line, new_address);
/* Adjust shift_count if new_x lower than 0 or higher than XBREAK */
if (new_x < 0 || new_x >= XBREAK) {
if (new_x > XBREAK || (new_x == XBREAK && *new_address != '\n'))
shift = (new_x - XBREAK) / SHIFT_SIZE + 1;
else {
shift = new_x / SHIFT_SIZE;
if (new_x % SHIFT_SIZE)
shift--;
}
if (shift != 0) {
line->shift_count += shift;
new_x = find_x(line, new_address);
set_cursor(0, y);
line_print(line);
rel_x = new_x;
}
}
/* Assign and position cursor */
x = new_x;
cur_text = new_address;
cur_line = line;
set_cursor(x, y);
}
/*
* Find_x() returns the x coordinate belonging to address.
* (Tabs are expanded).
*/
int find_x(LINE *line, char *address)
{
register char *textp = line->text;
register int nx = get_shift(line->shift_count) * -SHIFT_SIZE;
while (textp != address && *textp != '\0') {
if (is_tab(*textp++)) /* Expand tabs */
nx = tab(nx);
else
nx++;
}
return nx;
}
/*
* Find_address() returns the pointer in the line with offset x_coord.
* (Tabs are expanded).
*/
char *find_address(LINE *line, int x_coord, int *old_x)
{
register char *textp = line->text;
register int tx = get_shift(line->shift_count) * -SHIFT_SIZE;
while (tx < x_coord && *textp != '\n') {
if (is_tab(*textp)) {
if (*old_x - x_coord == 1 && tab(tx) > x_coord)
break; /* Moving left over tab */
else
tx = tab(tx);
}
else
tx++;
textp++;
}
*old_x = tx;
return textp;
}
/*
* Length_of() returns the number of characters int the string `string'
* excluding the '\0'.
*/
int length_of(register char *string)
{
register int count = 0;
if (string != NULL) {
while (*string++ != '\0')
count++;
}
return count;
}
/*
* Copy_string() copies the string `from' into the string `to'. `To' must be
* long enough to hold `from'.
*/
void copy_string(register char *to, register char *from)
{
while ((*to++ = *from++))
;
}
/*
* Reset assigns bot_line, top_line and cur_line according to `head_line'
* which must be the first line of the screen, and an y-coordinate,
* which will be the current y-coordinate (if it isn't larger than last_y)
*/
void reset(LINE *head_line, int screen_y)
{
register LINE *line;
top_line = line = head_line;
/* Search for bot_line (might be last line in file) */
for (last_y = 0; last_y < nlines - 1 && last_y < screenmax
&& line->next != tail; last_y++)
line = line->next;
bot_line = line;
y = (screen_y > last_y) ? last_y : screen_y;
/* Set cur_line according to the new y value */
cur_line = proceed(top_line, y);
}
/*
* Set cursor at coordinates x, y.
*/
void set_cursor(int nx, int ny)
{
#ifdef UNIX
extern char *tgoto();
tputs(tgoto(CM, nx, ny), 0, _putchar);
#else
char text_buffer[10];
build_string(text_buffer, pos_string, ny+1, nx+1);
string_print(text_buffer);
#endif /* UNIX */
}
/*
* Routine to open terminal when mined is used in a pipeline.
*/
void open_device(void)
{
if ((input_fd = open("/dev/tty", 0)) < 0)
panic("Cannot open /dev/tty for read");
}
/*
* Getchar() reads one character from the terminal. The character must be
* masked with 0377 to avoid sign extension.
*/
int getchar(void)
{
#ifdef UNIX
return (_getchar() & 0377);
#else
char c;
if (read(input_fd, &c, 1) != 1 && quit == FALSE)
panic("Can't read one char from fd #0");
return c & 0377;
#endif /* UNIX */
}
/*
* Display() shows count lines on the terminal starting at the given
* coordinates. When the tail of the list is encountered it will fill the
* rest of the screen with blank_line's.
* When count is negative, a backwards print from `line' will be done.
*/
void display(int x_coord, int y_coord, register LINE *line, register int count)
{
set_cursor(x_coord, y_coord);
/* Find new startline if count is negative */
if (count < 0) {
line = proceed(line, count);
count = -count;
}
/* Print the lines */
while (line != tail && count-- >= 0) {
line_print(line);
line = line->next;
}
/* Print the blank lines (if any) */
if (loading == FALSE) {
while (count-- >= 0) {
#ifdef UNIX
tputs(CE, 0, _putchar);
#else
string_print(blank_line);
#endif /* UNIX */
putchar('\n');
}
}
}
/*
* Write_char does a buffered output.
*/
int write_char(int fd, int c)
{
screen [out_count++] = c;
if (out_count == SCREEN_SIZE) /* Flush on SCREEN_SIZE chars */
return flush_buffer(fd);
return FINE;
}
/*
* Writeline writes the given string on the given filedescriptor.
*/
int writeline(register int fd, register char *text)
{
while(*text)
if (write_char(fd, *text++) == ERRORS)
return ERRORS;
return FINE;
}
/*
* Put_line print the given line on the standard output. If offset is not zero
* printing will start at that x-coordinate. If the FLAG clear_line is TRUE,
* then (screen) line will be cleared when the end of the line has been
* reached.
*/
void put_line(LINE *line, int offset, FLAG clear_line)
{
register char *textp = line->text;
register int count = get_shift(line->shift_count) * -SHIFT_SIZE;
int tab_count; /* Used in tab expansion */
/* Skip all chars as indicated by the offset and the shift_count field */
while (count < offset) {
if (is_tab(*textp++))
count = tab(count);
else
count++;
}
while (*textp != '\n' && count < XBREAK) {
if (is_tab(*textp)) { /* Expand tabs to spaces */
tab_count = tab(count);
while (count < XBREAK && count < tab_count) {
count++;
putchar(' ');
}
textp++;
}
else {
if (*textp >= '\01' && *textp <= '\037') {
#ifdef UNIX
tputs(SO, 0, _putchar);
#else
string_print (rev_video);
#endif /* UNIX */
putchar(*textp++ + '\100');
#ifdef UNIX
tputs(SE, 0, _putchar);
#else
string_print (normal_video);
#endif /* UNIX */
}
else
putchar(*textp++);
count++;
}
}
/* If line is longer than XBREAK chars, print the shift_mark */
if (count == XBREAK && *textp != '\n')
putchar(textp[1]=='\n' ? *textp : SHIFT_MARK);
/* Clear the rest of the line is clear_line is TRUE */
if (clear_line == TRUE) {
#ifdef UNIX
tputs(CE, 0, _putchar);
#else
string_print(blank_line);
#endif /* UNIX */
putchar('\n');
}
}
/*
* Flush the I/O buffer on filedescriptor fd.
*/
int flush_buffer(int fd)