-
Notifications
You must be signed in to change notification settings - Fork 752
/
more.c
1833 lines (1707 loc) · 48.5 KB
/
more.c
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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
/*
* @(#) more.c 1.1 88/03/29 more:more.c
*/
/*
** more.c - General purpose tty output filter and file perusal program
**
** by Eric Shienbrood, UC Berkeley
**
** modified by Geoff Peck, UCB to add underlining, single spacing
** modified by John Foderaro, UCB to add -c and MORE environment variable
** modified by Hans Spiller, Microsoft to handle \r better July 23, 82
** added ? help command, and -w
**
** vwh 11 Jan 83 M001
** modified to handle x.out magic number and magic number
** byte ordering OTHER than the vax and pdp11.
** JJD 19 Jan 83 M002
** - fix distributed on USENET
** From decvax!ucbvax!dist2 Sun Dec 6 02:58:31 1981
** Subject: FIXED: bug in src/more/more.c
** - fixed bug on terminal with "magic cookie" standout
** sequences.
** JJD 14 Feb 83 M003
** - fix exit status of more
** - Made first letter of "no more" message uppercase
** andyp 03 Aug 83 M004 3.0 upgrade
** - moved <local/uparm.h> to cmd/include.
** - use UCB, rather than XENIX, stty(2).
** andyp 30 Nov 83 M005
** - (thanks to reubenb). Changed frame variable to static, it is
** used as a global buffer. We never saw the bug before because
** of the depth of the stack.
** barrys 03 Jul 84 M006
** - Updated the usage message to include the 's' and 'w' options
** and to make the 'n' option a separate entry (uncommented).
** ericc 26 Dec 84 M007
** - Replaced the constant 0x7fffffffffffffffL with MAXLONG.
** ericc 25 Jul 85 M008
** - made "-r" option display control characters as '^x', as documented.
** - fixed processing of '\b' so that more doesn't terminate when
** the sequence "\b\n" is encountered.
** - changed "Hit Rubout ..." to "Hit Del ...", for ibm keyboards.
** davidby 9 March 1988 Unmarked
** - replaced all locally defined functions with library equivalents,
** - changed from termcap to terminfo
** - included <values.h> for MAXLONG value
** - removed most ifdef code for V6, V7, and BSD
** - added /etc/magic support for file type checking
*/
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <curses.h>
#include <term.h>
#include <sys/ioctl.h>
#include <setjmp.h>
#include <sys/stat.h>
#include <values.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <euc.h>
#include <getwidth.h>
#include <locale.h>
#include <widec.h>
#include <wctype.h>
#include <limits.h>
eucwidth_t wp;
int cw[4];
int scw[4];
#include <locale.h>
/* Help file will eventually go in libpath(more.help) on all systems */
#ifdef INGRES
#define VI "/usr/bin/vi"
#define HELPFILE "/mntp/doucette/more/more.help"
#define LOCAL_HELP "/usr/lib/locale/%s/LC_MESSAGES/more.help"
#endif
#ifndef INGRES
#ifndef HELPFILE
#define HELPFILE "/usr/lib/more.help"
#define LOCAL_HELP "/usr/lib/locale/%s/LC_MESSAGES/more.help"
#endif
#define VI "vi"
#endif
#define Fopen(s,m) (Currline = 0,file_pos=0,fopen(s,m))
#define Ftell(f) file_pos
#define Fseek(f,off) (file_pos=off,fseeko(f,off,0))
#define Getc(f) (++file_pos, getc(f))
#define Ungetc(c,f) (--file_pos, ungetc(c,f))
#define pr(s1) fputs(s1, stdout)
#define clreos() putp(clr_eos)
#define cleareol() putp(clr_eol)
#define home() putp(cursor_home)
#define LINSIZ 512
#define ctrl(letter) ((letter) & 077)
#define RUBOUT '\177'
#define ESC '\033'
#define QUIT '\034'
struct termio otty; /* old tty modes */
struct termio ntty; /* new tty modes */
off_t file_pos, file_size;
int fnum, no_intty, no_tty;
int dum_opt;
off_t dlines;
void end_it(int sig) __NORETURN;
void onquit(int sig);
void chgwinsz(int sig);
#ifdef SIGTSTP
void onsusp(int sig);
#endif
int nscroll = 11; /* Number of lines scrolled by 'd' */
int fold_opt = 1; /* Fold long lines */
int stop_opt = 1; /* Stop after form feeds */
int ssp_opt = 0; /* Suppress white space */
int ul_opt = 1; /* Underline as best we can */
int cr_opt = 0; /* show ctrl characters as '^c' */
int wait_opt = 0; /* prompt for exit at eof */
int promptlen;
off_t Currline; /* Line we are currently at */
int startup = 1;
int firstf = 1;
int notell = 1;
int inwait, Pause, errors;
int within; /* true if we are within a file,
false if we are between files */
int hard, dumb, noscroll, hardtabs, clreol;
int catch_susp; /* We should catch the SIGTSTP signal */
char **fnames; /* The list of file names */
int nfiles; /* Number of files left to process */
char *shell; /* The name of the shell to use */
int shellp; /* A previous shell command exists */
char ch;
jmp_buf restore;
char obuf[BUFSIZ]; /* stdout buffer */
char Line[LINSIZ]; /* Line buffer */
int Lpp = 24; /* lines per page */
char *ULenter, *ULexit; /* enter and exit underline mode */
int Mcol = 80; /* number of columns */
int Wrap = 1; /* set if automargins */
int fseeko();
struct {
off_t chrctr, line;
} context, screen_start;
int exitstat = 0; /* status to use when exiting more */ /*M003*/
static void execute(char *filename, char *cmd, ...);
static void error(char *mess);
static void wait_eof(void);
static void prompt(char *filename);
static void argscan(char *s);
static void copy_file(register FILE *f);
static void initterm(void);
static void do_shell(char *filename);
static FILE *checkf(register char *fs, int *clearfirst);
static void screen(register FILE *f, register off_t num_lines);
static void skiplns(register off_t n, register FILE *f);
static void skipf(register int nskip);
static int readch(void);
static void prmpt_erase(register int col);
static void kill_line(void);
static void prbuf(register char *s, register int n);
static void search(char buf[], FILE *file, register off_t n);
static void doclear(void);
static void ttyin(char buf[], register int nmax, char pchar);
static int expand(char *outbuf, char *inbuf);
static void show(register char ch);
static void set_tty(void);
static void reset_tty(void);
static void rdline(register FILE *f);
static off_t command(char *filename, register FILE *f);
static int getaline(register FILE *f, int *length);
static int number(char *cmd);
static int colon(char *filename, int cmd, off_t nlines);
int
main(int argc, char *argv[])
{
register FILE *f;
register char *s;
register char *p;
register int ch;
register off_t left;
int prnames = 0;
int initopt = 0;
int srchopt = 0;
int clearit = 0;
off_t initline;
char initbuf[80];
setlocale( LC_ALL, "" );
getwidth(&wp);
cw[0] = 1;
cw[1] = wp._eucw1;
cw[2] = wp._eucw2+1;
cw[3] = wp._eucw3+1;
scw[0] = 1;
scw[1] = wp._scrw1;
scw[2] = wp._scrw2;
scw[3] = wp._scrw3;
nfiles = argc;
fnames = argv;
(void) setlocale(LC_ALL,"");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
initterm ();
if(s = getenv("MORE")) argscan(s);
while (--nfiles > 0) {
if ((ch = (*++fnames)[0]) == '-') {
argscan(*fnames+1);
}
else if (ch == '+') {
s = *fnames;
if (*++s == '/') {
srchopt++;
for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
*p++ = *s++;
*p = '\0';
}
else {
initopt++;
for (initline = 0; *s != '\0'; s++)
if (isdigit (*s))
initline = initline*10 + *s -'0';
--initline;
}
}
else break;
}
/* allow clreol only if cursor_home and clr_eol and clr_eos strings are
* defined, and in that case, make sure we are in noscroll mode
*/
if(clreol)
{
if (!cursor_home || !clr_eol || !clr_eos) {
clreol = 0;
}
else noscroll = 1;
}
if (dlines == 0)
dlines =(off_t) (Lpp - (noscroll ? 1 : 2));
left = dlines;
if (nfiles > 1)
prnames++;
if (!no_intty && nfiles == 0) {
fprintf(stderr, gettext("Usage: %s\
[-cdflrsuw] [-lines] [+linenumber] [+/pattern] [filename ...].\n")
, argv[0]);
exit(1);
}
else
f = stdin;
if (!no_tty) {
signal(SIGQUIT, onquit);
signal(SIGINT, end_it);
signal(SIGWINCH, chgwinsz);
#ifdef SIGTSTP
if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
signal(SIGTSTP, onsusp);
catch_susp++;
}
#endif
set_tty();
}
if (no_intty) {
if (no_tty)
copy_file (stdin);
else {
if ((ch = Getc (f)) == '\f')
doclear();
else {
Ungetc (ch, f);
if (noscroll && (ch != EOF)) {
if (clreol)
home ();
else
doclear ();
}
}
if (!setjmp(restore)) {
if (srchopt) {
search (initbuf, stdin,(off_t) 1);
if (noscroll)
left--;
}
else if (initopt)
skiplns (initline, stdin);
}
else
left = command(NULL, f);
screen (stdin, left);
}
no_intty = 0;
prnames++;
firstf = 0;
}
while (fnum < nfiles) {
if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
context.line = context.chrctr = 0;
Currline = 0;
if (firstf) setjmp (restore);
if (firstf) {
firstf = 0;
if (srchopt)
{
search (initbuf, f,(off_t) 1);
if (noscroll)
left--;
}
else if (initopt)
skiplns (initline, f);
}
else if (fnum < nfiles && !no_tty) {
setjmp (restore);
left = command (fnames[fnum], f);
}
if (left != 0) {
if ((noscroll || clearit) && (file_size != LLONG_MAX))
if (clreol)
home ();
else
doclear ();
if (prnames) {
if (ceol_standout_glitch)
prmpt_erase (0);
if (clreol)
cleareol ();
pr("::::::::::::::");
if (promptlen > 14)
prmpt_erase (14);
printf ("\n");
if(clreol) cleareol();
printf("%s\n", fnames[fnum]);
if(clreol) cleareol();
pr("::::::::::::::\n");
if (left > (off_t)(Lpp - 4))
left =(off_t)(Lpp - 4);
}
if (no_tty)
copy_file (f);
else {
within++;
screen(f, left);
within = 0;
}
}
setjmp (restore);
fflush(stdout);
fclose(f);
screen_start.line = screen_start.chrctr = 0LL;
context.line = context.chrctr = 0LL;
} else
exitstat |= 1; /*M003*/
fnum++;
firstf = 0;
}
if (wait_opt) wait_eof();
reset_tty ();
return (exitstat); /*M003*/
}
static void
argscan(char *s)
{
for (dlines = 0; *s != '\0'; s++)
if (isdigit(*s))
dlines = dlines*10 + *s - '0';
else if (*s == 'd')
dum_opt = 1;
else if (*s == 'l')
stop_opt = 0;
else if (*s == 'f')
fold_opt = 0;
else if (*s == 'p')
noscroll++;
else if (*s == 'c')
clreol++;
else if (*s == 's')
ssp_opt = 1;
else if (*s == 'u')
ul_opt = 0;
else if (*s == 'r')
cr_opt = 1;
else if (*s == 'w')
wait_opt = 1;
}
/*
** Check whether the file named by fs is a file which the user may
** access. If it is, return the opened file. Otherwise return NULL.
*/
static FILE *
checkf(register char *fs, int *clearfirst)
{
struct stat stbuf;
register FILE *f;
int c;
if (stat (fs, &stbuf) == -1) {
fflush(stdout);
if (clreol)
cleareol ();
perror(fs);
return (NULL);
}
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
printf(gettext("\n*** %s: directory ***\n\n"), fs);
return (NULL);
}
if ((f=Fopen(fs, "r")) == NULL) {
fflush(stdout);
perror(fs);
return (NULL);
}
if ((c = Getc(f)) == '\f') /* end M001 */
*clearfirst = 1;
else {
*clearfirst = 0;
Ungetc (c, f);
}
if ((file_size = (off_t)stbuf.st_size) == 0)
file_size = LLONG_MAX;
return (f);
}
/*
** Print out the contents of the file f, one screenful at a time.
*/
#define STOP -10
static void
screen(register FILE *f, register off_t num_lines)
{
register int c;
register int nchars;
int length; /* length of current line */
static int prev_len = 1; /* length of previous line */
for (;;) {
while (num_lines > 0 && !Pause) {
if ((nchars = getaline (f, &length)) == EOF)
{
if (clreol) clreos();
return;
}
if (ssp_opt && length == 0 && prev_len == 0)
continue;
prev_len = length;
if (ceol_standout_glitch ||
(enter_standout_mode && *enter_standout_mode == ' ')
&& promptlen > 0)
prmpt_erase (0);
/* must clear before drawing line since tabs on some terminals
* do not erase what they tab over.
*/
if (clreol)
cleareol ();
prbuf (Line, length);
if (nchars < promptlen)
prmpt_erase (nchars); /* prmpt_erase () sets promptlen to 0 */
else promptlen = 0;
/* is this needed?
* if (clreol)
* cleareol(); */ /* must clear again in case we wrapped */
if (nchars < Mcol || !fold_opt)
putchar('\n');
if (nchars == STOP)
break;
num_lines--;
}
fflush(stdout);
if ((c = Getc(f)) == EOF)
{
if (clreol) clreos ();
return;
}
if (Pause && clreol)
clreos ();
Ungetc (c, f);
setjmp (restore);
Pause = 0; startup = 0;
if ((num_lines = command (NULL, f)) == 0)
return;
if (hard && promptlen > 0)
prmpt_erase (0);
if (noscroll && num_lines == dlines) {
if (clreol)
home();
else
doclear ();
}
screen_start.line = Currline;
screen_start.chrctr = Ftell (f);
}
}
/*
** Come here if a quit signal is received
*/
/*
* sig is put in as a dummy arg to have the compiler not to complain
*/
/* ARGSUSED */
void
onquit(int sig)
{
signal(SIGQUIT, SIG_IGN);
if (!inwait) {
putchar ('\n');
if (!startup) {
signal(SIGQUIT, onquit);
longjmp (restore, 1);
}
else
Pause++;
}
else if (!dum_opt && notell) {
write (2, gettext("[Use q or Q to quit]"), 20);
promptlen += 20;
notell = 0;
}
signal(SIGQUIT, onquit);
}
/*
** Come here if a signal for a window size change is received
*/
/*ARGSUSED*/
void
chgwinsz(int sig)
{
struct winsize win;
(void) signal(SIGWINCH, SIG_IGN);
if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
if (win.ws_row != 0) {
Lpp = win.ws_row;
nscroll = Lpp/2 - 1;
if (nscroll <= 0)
nscroll = 1;
dlines = (off_t)(Lpp - (noscroll ? 1 : 2));
}
if (win.ws_col != 0)
Mcol = win.ws_col;
}
(void) signal(SIGWINCH, chgwinsz);
}
/*
** Clean up terminal state and exit. Also come here if interrupt signal received
*/
/*
* sig is put in as a dummy arg to have the compiler not to complain
*/
/* ARGSUSED */
void
end_it(int sig)
{
reset_tty ();
if (clreol) {
putchar ('\r');
clreos ();
fflush (stdout);
}
else if (!clreol && (promptlen > 0)) {
kill_line ();
fflush (stdout);
}
else
write (2, "\n", 1);
_exit(exitstat); /*M003*/
}
static void
copy_file(register FILE *f)
{
register int c;
while ((c = getc(f)) != EOF)
putchar(c);
}
static char Bell = ctrl('G');
/* See whether the last component of the path name "path" is equal to the
** string "string"
*/
int
tailequ(char *path, char *string)
{
return (!strcmp(basename(path), string));
}
static void
prompt(char *filename)
{
if (clreol)
cleareol ();
else if (promptlen > 0)
kill_line ();
if (!hard) {
promptlen = 8;
if (enter_standout_mode && exit_standout_mode)
putp (enter_standout_mode);
if (clreol)
cleareol ();
pr(gettext("--More--"));
if (filename != NULL) {
promptlen += printf (gettext("(Next file: %s)"), filename);
}
else if (!no_intty) {
promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size));
}
if (dum_opt) {
promptlen += pr(gettext("[Hit space to continue, Del to abort]"));
}
if (enter_standout_mode && exit_standout_mode)
putp (exit_standout_mode);
if (clreol) clreos ();
fflush(stdout);
}
else
write (2, &Bell, 1);
inwait++;
}
/*
* when run from another program or a shell script, it is
* sometimes useful to prevent the next program from scrolling
* us off the screen before we get a chance to read this page.
* -Hans, July 24, 1982
*/
static void
wait_eof(void)
{
if (enter_standout_mode && exit_standout_mode)
putp (enter_standout_mode);
promptlen = pr(gettext("--No more--")); /*M003*/
if (dum_opt)
promptlen += pr(gettext("[Hit any key to continue]"));
if (enter_standout_mode && exit_standout_mode)
putp(exit_standout_mode);
if (clreol) clreos();
fflush(stdout);
readch();
prmpt_erase (0);
fflush(stdout);
}
/*
** Get a logical line
*/
static int
getaline(register FILE *f, int *length)
{
register int c;
register char *p;
register int column;
static int colflg;
register int oldcolumn;
int csno;
p = Line;
column = 0;
oldcolumn = 0;
c = Getc (f);
if (colflg && c == '\n') {
Currline++;
c = Getc (f);
}
while (p < &Line[LINSIZ - 1]) {
csno = csetno(c);
if (c == EOF) {
if (p > Line) {
*p = '\0';
*length = p - Line;
return (column);
}
*length = p - Line;
return (EOF);
}
if (!csno) {
if (c == '\n') {
/* detect \r\n. -Hans */
if (p>Line && p[-1] == '\r') {
column = oldcolumn;
p--;
}
Currline++;
break;
}
*p++ = c;
if (c == '\t')
if (hardtabs && column < promptlen && !hard) {
if (clr_eol && !dumb) {
column = 1 + (column | 7);
putp (clr_eol);
promptlen = 0;
}
else {
for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) {
*p++ = ' ';
}
if (column >= promptlen) promptlen = 0;
}
}
else
column = 1 + (column | 7);
else if ((c == '\b') && (ul_opt || !cr_opt) && (column > 0)) /* M008 */
column--;
/* this is sort of strange. what was here before was that
\r always set column to zero, and the hack above to
detect \r\n didnt exist. the net effect is to make
the current line be overwritten by the prompt if it
had a \r at the end, and the line start after the \r
otherwise. I suppose this is useful for overstriking
on hard copy terminals, but not on anything glass
-Hans */
else if ((c == '\r') && !cr_opt) {
oldcolumn = column;
column = 0;
}
else if (c == '\f' && stop_opt) {
p[-1] = '^';
*p++ = 'L';
column += 2;
Pause++;
}
else if (c == EOF) {
*length = p - Line;
return (column);
}
else if (c < ' ' && cr_opt){ /* M008 begin */
p[-1] = '^';
*p++ = c | ('A' - 1);
column += 2;
} /* M008 end */
else if (c >= ' ' && c != RUBOUT)
column++;
} /* end of code set 0 */
else {
column += scw[csno];
if ( column > Mcol && fold_opt ) {
column -= scw[csno];
while ( column < Mcol ) {
column++;
*p++ = ' ';
}
column = Mcol;
Ungetc(c,f);
} else {
int i;
*p++ = c;
for(i=1; i<cw[csno];i++)
*p++ = Getc(f);
}
} /* end of codeset 1 ~ 3 */
if (column >= Mcol && fold_opt) break;
c = Getc (f);
}
if (column >= Mcol && Mcol > 0) {
if (!Wrap) {
*p++ = '\n';
}
}
colflg = column == Mcol && fold_opt;
if (colflg && eat_newline_glitch && Wrap) {
*p++ = '\n'; /* simulate normal wrap */
}
*length = p - Line;
*p = 0;
return (column);
}
/*
** Erase the rest of the prompt, assuming we are starting at column col.
*/
static void
prmpt_erase(register int col)
{
if (promptlen == 0)
return;
if (hard) {
putchar ('\n');
}
else {
if (col == 0)
putchar ('\r');
if (!dumb && clr_eol)
putp (clr_eol);
else
for (col = promptlen - col; col > 0; col--)
putchar (' ');
}
promptlen = 0;
}
/*
** Erase the current line entirely
*/
static void
kill_line(void)
{
prmpt_erase (0);
if (!clr_eol || dumb) putchar ('\r');
}
/* Print a buffer of n characters */
static void
prbuf(register char *s, register int n)
{
char c; /* next ouput character */
register int state = 0; /* next output char's UL state */
static int pstate = 0; /* current terminal UL state (off) */
while (--n >= 0)
if (!ul_opt)
putchar (*s++);
else {
if (n >= 2 && s[0] == '_' && s[1] == '\b') {
n -= 2;
s += 2;
c = *s++;
state = 1;
} else if (n >= 2 && s[1] == '\b' && s[2] == '_') {
n -= 2;
c = *s++;
s += 2;
state = 1;
} else {
c = *s++;
state = 0;
}
if (state != pstate)
putp(state ? ULenter : ULexit);
pstate = state;
putchar(c);
if (state && underline_char) {
putp(cursor_left);
putp(underline_char);
}
}
/*
* M002
* You don't want to stay in standout mode at the end of the line;
* on some terminals, this will leave all of the remaining blank
* space on the line in standout mode.
*/
if (state && !underline_char) { /*M002*/
putp(ULexit); /*M002*/
pstate = 0; /*M002*/
} /*M002*/
}
/*
** Clear the screen
*/
static void
doclear(void)
{
if (clear_screen && !hard) {
putp(clear_screen);
/* Put out carriage return so that system doesn't
** get confused by escape sequences when expanding tabs
*/
putchar ('\r');
promptlen = 0;
}
}
static int lastcmd, lastp;
static off_t lastarg;
static int lastcolon;
char shell_line[PATH_MAX];
/*
** Read a command and do it. A command consists of an optional integer
** argument followed by the command character. Return the number of lines
** to display in the next screenful. If there is nothing more to display
** in the current file, zero is returned.
*/
static off_t
command(char *filename, register FILE *f)
{
register off_t nlines;
register off_t retval;
register int c;
char colonch;
FILE *helpf;
int done;
char comchar, cmdbuf[80];
char filebuf[128];
char *loc;
#define ret(val) retval=val;done++;break
done = 0;
if (!errors)
prompt (filename);
else
errors = 0;
for (;;) {
nlines = number (&comchar);
lastp = colonch = 0;
if (comchar == '.') { /* Repeat last command */
lastp++;
comchar = lastcmd;
nlines = lastarg;
if (lastcmd == ':')
colonch = lastcolon;
}
lastcmd = comchar;
lastarg = nlines;
if((comchar != RUBOUT) && !dum_opt) {
if (comchar == otty.c_cc[VERASE]) {
kill_line ();
prompt (filename);