added user setable buffer size.
[unix-history] / usr / src / usr.bin / window / main.c
... / ...
CommitLineData
1#ifndef lint
2static char *sccsid = "@(#)main.c 3.6 83/08/25";
3#endif
4
5#include "defs.h"
6
7char escapec = CTRL(p);
8int nbufline = 48; /* compatible */
9
10#define next(a) (*++*(a) ? *(a) : (*++(a) ? *(a) : (char *)usage()))
11
12/*ARGSUSED*/
13main(argc, argv)
14char **argv;
15{
16 register n;
17 register char *p;
18 char fflag = 0;
19 char dflag = 0;
20 int imask;
21 struct timezone timezone;
22
23 if (p = rindex(*argv, '/'))
24 p++;
25 else
26 p = *argv;
27 debug = strcmp(p, "a.out") == 0;
28 while (*++argv) {
29 if (**argv == '-') {
30 switch (*++*argv) {
31 case 'f':
32 fflag++;
33 break;
34 case 'e':
35 setescape(next(argv));
36 break;
37 case 't':
38 terse++;
39 break;
40 case 'd':
41 dflag++;
42 break;
43 case 'D':
44 debug++;
45 break;
46 default:
47 (void) usage();
48 }
49 } else
50 (void) usage();
51 }
52 if ((shell = getenv("SHELL")) == 0)
53 shell = "/bin/csh";
54 if (shellname = rindex(shell, '/'))
55 shellname++;
56 else
57 shellname = shell;
58 (void) gettimeofday(&starttime, &timezone);
59 if (wwinit() < 0) {
60 (void) fflush(stdout);
61 (void) fprintf(stderr, "Can't do windows on this terminal.\n");
62 exit(1);
63 }
64 if (debug) {
65 wwnewtty.ww_tchars.t_quitc = wwoldtty.ww_tchars.t_quitc;
66 (void) wwsettty(0, &wwnewtty);
67 }
68
69 if ((cmdwin = wwopen(WWO_REVERSE, 1, wwncol, 0, 0, 0)) == 0) {
70 (void) wwflush();
71 (void) fprintf(stderr, "Can't open command window.\r\n");
72 goto bad;
73 }
74 if ((framewin = wwopen(WWO_GLASS|WWO_FRAME, wwnrow, wwncol, 0, 0, 0))
75 == 0) {
76 (void) wwflush();
77 (void) fprintf(stderr, "Can't open frame window.\r\n");
78 goto bad;
79 }
80 wwadd(framewin, &wwhead);
81 if ((boxwin = wwopen(WWO_GLASS, wwnrow, wwncol, 0, 0, 0)) == 0) {
82 (void) wwflush();
83 (void) fprintf(stderr, "Can't open box window.\r\n");
84 goto bad;
85 }
86
87 curwin = cmdwin;
88 wwupdate();
89 wwflush();
90 (void) signal(SIGCHLD, wwchild);
91 if (!fflag) {
92 if (!terse)
93 wwadd(cmdwin, &wwhead);
94 if (dflag || doconfig() < 0)
95 dodefault();
96 if (selwin != 0) {
97 curwin = selwin;
98 wwcursor(selwin, 0);
99 }
100 if (!terse) {
101 wwdelete(cmdwin);
102 reframe();
103 }
104 }
105 while (!quit) {
106 if (curwin == cmdwin) {
107 docmd();
108 continue;
109 }
110 /*
111 * Loop until we get some keyboard input.
112 */
113 while (ibufc == 0) {
114 wwcurtowin(curwin);
115 wwupdate();
116 wwflush();
117 imask = 1 << 0;
118 while (wwforce(&imask) < 0)
119 ;
120 if ((imask & 1 << 0) == 0)
121 continue;
122 /* NOTE: ibufc == 0 */
123 ibufp = ibuf;
124 if ((ibufc = read(0, ibuf, sizeof ibuf)) < 0) {
125 ibufc = 0;
126 nreade++;
127 } else if (ibufc == 0)
128 nreadz++;
129 else
130 nreadc += ibufc;
131 nread++;
132 }
133 /*
134 * Weird loop. Copy the buffer to the pty
135 * and stopping on the escape character
136 * in a hopefully efficient way.
137 * Probably a good thing to make ibufc == 1 a special
138 * case.
139 */
140 for (p = ibufp, n = ibufc;;) {
141 if (--n < 0) {
142 (void) write(curwin->ww_pty, ibufp, ibufc);
143 ibufp = ibuf;
144 ibufc = 0;
145 break;
146 } else if (*p++ == escapec) {
147 if ((n = p - ibufp) > 1)
148 (void) write(curwin->ww_pty,
149 ibufp, n - 1);
150 ibufp = p;
151 ibufc -= n;
152 curwin = cmdwin;
153 break;
154 }
155 }
156 }
157 wwupdate();
158 wwflush();
159bad:
160 wwend();
161 return 0;
162}
163
164usage()
165{
166 (void) fprintf(stderr, "window: [-e escape-char] [-t] [-f] [-d]\n");
167 exit(1);
168 return 0; /* for lint */
169}