Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 470 lines (399 sloc) 9.893 kb
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
1 /* guiopen.c */
2 /* Copyright 1995 by Steve Kirkendall */
3
4
5 #include "elvis.h"
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
6 #ifdef FEATURE_RCSID
7 char id_guiopen[] = "$Id: guiopen.c,v 2.30 2003/10/17 17:41:23 steve Exp $";
8 #endif
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
9 #ifdef GUI_OPEN
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
10 # if ANY_UNIX
11 # include <unistd.h>
12 # else
13 # include <io.h>
14 # endif
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
15
16 /* This file contains a user interface which internally resembles guitcap.c,
17 * but which doesn't depend on termcap and can therefore be used with terminals
18 * of an unknown type.
19 */
20
21
22 #if USE_PROTOTYPES
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
23 static ELVBOOL creategw(char *name, char *firstcmd);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
24 static int init(int argc, char **argv);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
25 static int scriptinit(int argc, char **argv);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
26 static int test(void);
27 static void beep(GUIWIN *gw);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
28 static void destroygw(GUIWIN *gw, ELVBOOL force);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
29 static void endtty(void);
30 static void loop(void);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
31 static void scriptloop(void);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
32 static void quitloop(void);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
33 static ELVBOOL scriptmsg(GUIWIN *gw, MSGIMP imp, CHAR *text, int len);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
34 static void starttty(void);
35 static void term(void);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
36 static void scriptterm(void);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
37 static void textline(GUIWIN *gw, CHAR *text, int len);
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
38 static ELVBOOL oprgopen(char *command, ELVBOOL willread, ELVBOOL willwrite);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
39 static int oprgclose(void);
40 #endif
41
42
43 static char ttyerasekey; /* taken from the ioctl structure */
44
45 static void starttty()
46 {
47 /* change the terminal mode to cbreak/noecho */
48 ttyinit();
49 ttyraw(&ttyerasekey);
50 }
51
52 static void endtty()
53 {
54 /* change the terminal mode back the way it was */
55 ttynormal();
56
57 /* leave the cursor on a fresh line */
58 ttywrite("\n", 1);
59 }
60
61 /*----------------------------------------------------------------------------*/
62 /* start of GUI functions */
63
64 GUIWIN *current;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
65 ELVBOOL anycmd;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
66
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
67 /* return ElvTrue if available. */
68 static int test()
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
69 {
70 return 1;
71 }
72
73
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
74 /* initialize the "open" interface. */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
75 static int init(argc, argv)
76 int argc; /* number of command-line arguments */
77 char **argv; /* values of command-line arguments */
78 {
79 int i;
80
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
81 /* initialize the tty */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
82 starttty();
83
84 /* set anycmd if there is a "-c cmd" or "+cmd" argument. */
85 for (i = 1; i < argc && !anycmd; i++)
86 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
87 anycmd = (ELVBOOL)(argv[i][0] == '+' ||
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
88 (argv[i][0] == '-' && argv[i][1] == 'c'));
89 }
90 return argc;
91 }
92
93
94 /* Repeatedly get events (keystrokes), and call elvis' event functions */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
95 static void loop()
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
96 {
97 char buf[10];
98 int len;
99 MAPSTATE mst = MAP_CLEAR;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
100
101 /* peform the -c command or -t tag */
102 if (mainfirstcmd(windefault))
103 return;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
104
105 while (current)
106 {
107 /* redraw the window(s) */
108 (void)eventdraw(current);
109
110 /* read events */
111 do
112 {
113 len = ttyread(buf, sizeof buf, (mst==MAP_CLEAR ? 0 : 2));
114 } while (len < 1);
115
116 /* process keystroke data */
117 mst = eventkeys(current, toCHAR(buf), len);
118 }
119 }
120
121
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
122 /* shut down the "open" interface */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
123 static void term()
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
124 {
125 if (gui == &guiopen)
126 endtty();
127 }
128
129
130 /* initialize the "script" or "quit" interface. */
131 static int scriptinit(argc, argv)
132 int argc; /* number of command-line arguments */
133 char **argv; /* values of command-line arguments */
134 {
135 int i;
136
137 /* set anycmd if there is a "-c cmd" or "+cmd" argument. */
138 for (i = 1; i < argc && !anycmd; i++)
139 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
140 anycmd = (ELVBOOL)(argv[i][0] == '+' ||
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
141 (argv[i][0] == '-' && argv[i][1] == 'c'));
142 }
143 return argc;
144 }
145
146
147 /* This is the loop function for the "script" user interface. It reads text
148 * from stdin, and then executes it. Finishes by deleting the only window.
149 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
150 static void scriptloop()
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
151 {
152 BUFFER script;
153 MARKBUF start, end;
154 RESULT result;
155 char inbuf[1024];
156 long n;
157
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
158 /* peform the -c command or -t tag */
159 if (mainfirstcmd(windefault))
160 return;
161
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
162 /* create a buffer to hold the script */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
163 script = bufalloc(NULL, 0, ElvTrue);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
164
165 /* read from stdin, into a buffer */
166 while ((n = read(0, inbuf, sizeof(inbuf))) > 0)
167 {
168 (void)marktmp(end, script, o_bufchars(script));
169 bufreplace(&end, &end, toCHAR(inbuf), n);
170 }
171
172 /* execute the script */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
173 eventfocus(current, ElvTrue);
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
174 result = experform(windefault, marktmp(start, script, 0L),
175 marktmp(end, script, o_bufchars(script)));
176
177 /* destroy the window (if the script didn't do that already) */
178 if (current)
179 (void)eventdestroy(current);
180 }
181
182
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
183 /* This is the loop function for the "quit" user interface. It immediately
184 * deletes the only window, and then exits.
185 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
186 static void quitloop()
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
187 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
188 /* peform the -c command or -t tag */
189 if (mainfirstcmd(windefault))
190 return;
191
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
192 if (!anycmd)
193 msg(MSG_INFO, "session=(session)");
194 if (current)
195 (void)eventdestroy(current);
196 }
197
198
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
199 /* shut down the "script" or "quit" interface */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
200 static void scriptterm()
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
201 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
202 /* nothing actions needed */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
203 }
204
205
206 /* This function creates a window */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
207 static ELVBOOL creategw(name, firstcmd)
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
208 char *name; /* name of new window's buffer */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
209 char *firstcmd; /* other parameters, if any */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
210 {
211 /* We can only have one window. If we already made it, fail */
212 if (current)
213 {
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
214 return ElvFalse;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
215 }
216
217 /* make elvis do its own initialization */
218 current = (GUIWIN *)1;
219 if (!eventcreate(current, NULL, name, 24, 80))
220 {
221 /* elvis can't make it -- fail */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
222 return ElvFalse;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
223 }
224
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
225 /* if there is a firstcmd, then execute it */
226 if (firstcmd)
227 {
228 winoptions(winofgw(current));
3a9bb55 @mbert Import Elvis 2.1_3 (written by Steve Kirkendall)
authored
229 exstring(windefault, toCHAR(firstcmd), "+cmd");
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
230 }
231
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
232 return ElvTrue;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
233 }
234
235
236 /* This function deletes a window */
237 static void destroygw(gw, force)
238 GUIWIN *gw; /* window to be destroyed */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
239 ELVBOOL force; /* if ElvTrue, try harder */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
240 {
241 assert(gw == current);
242
243 /* simulate a "destroy" event */
244 eventdestroy(gw);
245 current = NULL;
246 }
247
248 /* This function rings the bell */
249 static void beep(gw)
250 GUIWIN *gw; /* window that generated a beep request */
251 {
252 ttywrite("\007", 1);
253 }
254
255
256 /* This function outputs a mixture of text and control characters. The only
257 * possible control characters will be '\b', '\n', and '\r'.
258 */
259 static void textline(gw, text, len)
260 GUIWIN *gw; /* where the text is to be output */
261 CHAR *text; /* the text to be output */
262 int len; /* length of text */
263 {
264 ttywrite(tochar8(text), len);
265 }
266
267
268 /* This function is used by the "quit" gui to hide all messages except errors */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
269 static ELVBOOL scriptmsg(gw, imp, text, len)
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
270 GUIWIN *gw; /* window which generated the message */
271 MSGIMP imp; /* message importance */
272 CHAR *text; /* the message itself */
273 int len; /* length of the message */
274 {
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
275 if (imp == MSG_ERROR || imp == MSG_FATAL)
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
276 return ElvFalse; /* so error is displayed normally */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
277
278 /* other message types are ignored */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
279 return ElvTrue;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
280 }
281
282
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
283 static ELVBOOL oprgopen(command, willwrite, willread)
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
284 char *command;
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
285 ELVBOOL willwrite;
286 ELVBOOL willread;
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
287 {
288 /* switch the tty to normal mode */
289 ttynormal();
290
291 /* use the OS prgopen() function */
292 return prgopen(command, willwrite, willread);
293 }
294
295
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
296 static int oprgclose()
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
297 {
298 int ret;
299
300 /* use the OS perclose() function */
301 ret = prgclose();
302
303 /* switch the tty back to raw mode */
304 ttyraw(&ttyerasekey);
305
306 return ret;
307 }
308
309
310 /* structs of this type are used to describe each available GUI */
311 GUI guiopen =
312 {
313 "open", /* name */
314 "Generic interface with limited capabilities",
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
315 ElvFalse, /* exonly */
316 ElvTrue, /* newblank */
317 ElvFalse, /* minimizeclr */
318 ElvTrue, /* scrolllast */
319 ElvFalse, /* shiftrows */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
320 3, /* movecost */
321 0, /* opts */
322 NULL, /* optdescs */
323 test,
324 init,
325 NULL, /* usage */
326 loop,
327 NULL, /* poll */
328 term,
329 creategw,
330 destroygw,
331 NULL, /* focusgw */
332 NULL, /* retitle */
333 NULL, /* reset */
334 NULL, /* flush */
335 NULL, /* moveto */
336 NULL, /* draw */
337 NULL, /* shift */
338 NULL, /* scroll */
339 NULL, /* clrtoeol */
340 textline,
341 beep,
342 NULL, /* msg */
343 NULL, /* scrollbar */
344 NULL, /* status */
345 NULL, /* keylabel */
346 NULL, /* clipopen */
347 NULL, /* clipwrite */
348 NULL, /* clipread */
349 NULL, /* clipclose */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
350 coloransi, /* color */
351 NULL, /* colorfree */
352 NULL, /* setbg */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
353 NULL, /* guicmd */
354 NULL, /* tabcmd */
355 NULL, /* save */
356 NULL, /* wildcard */
357 oprgopen, /* prgopen */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
358 oprgclose, /* prgclose */
359 NULL /* stop */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
360 };
361
362
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
363
364 /* structs of this type are used to describe each available GUI */
365 GUI guiscript =
366 {
367 "script", /* name */
368 "Reads a script from stdin",
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
369 ElvTrue, /* exonly */
370 ElvTrue, /* newblank */
371 ElvFalse, /* minimizeclr */
372 ElvTrue, /* scrolllast */
373 ElvFalse, /* shiftrows */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
374 0, /* movecost */
375 0, /* opts */
376 NULL, /* optdescs */
377 test,
378 scriptinit,
379 NULL, /* usage */
380 scriptloop,
381 NULL, /* poll */
382 scriptterm,
383 creategw,
384 destroygw,
385 NULL, /* focusgw */
386 NULL, /* retitle */
387 NULL, /* reset */
388 NULL, /* flush */
389 NULL, /* moveto */
390 NULL, /* draw */
391 NULL, /* shift */
392 NULL, /* scroll */
393 NULL, /* clrtoeol */
394 textline,
395 beep,
396 scriptmsg,
397 NULL, /* scrollbar */
398 NULL, /* status */
399 NULL, /* keylabel */
400 NULL, /* clipopen */
401 NULL, /* clipwrite */
402 NULL, /* clipread */
403 NULL, /* clipclose */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
404 coloransi, /* color */
405 NULL, /* colorfree */
406 NULL, /* setbg */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
407 NULL, /* guicmd */
408 NULL, /* tabcmd */
409 NULL, /* save */
410 NULL, /* wildcard */
411 NULL, /* prgopen */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
412 NULL, /* prgclose */
413 NULL /* stop */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
414 };
415
416
417
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
418 /* structs of this type are used to describe each available GUI */
419 GUI guiquit =
420 {
421 "quit", /* name */
422 "Quits immediately after processing \"-c cmd\"",
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
423 ElvTrue, /* exonly */
424 ElvTrue, /* newblank */
425 ElvFalse, /* minimizeclr */
426 ElvTrue, /* scrolllast */
427 ElvFalse, /* shiftrows */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
428 3, /* movecost */
429 0, /* opts */
430 NULL, /* optdescs */
431 test,
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
432 scriptinit,
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
433 NULL, /* usage */
434 quitloop,
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
435 NULL, /* poll */
436 scriptterm,
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
437 creategw,
438 destroygw,
439 NULL, /* focusgw */
440 NULL, /* retitle */
441 NULL, /* reset */
442 NULL, /* flush */
443 NULL, /* moveto */
444 NULL, /* draw */
445 NULL, /* shift */
446 NULL, /* scroll */
447 NULL, /* clrtoeol */
448 textline,
449 beep,
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
450 scriptmsg,
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
451 NULL, /* scrollbar */
452 NULL, /* status */
453 NULL, /* keylabel */
454 NULL, /* clipopen */
455 NULL, /* clipwrite */
456 NULL, /* clipread */
457 NULL, /* clipclose */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
458 coloransi, /* color */
459 NULL, /* colorfree */
460 NULL, /* setbg */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
461 NULL, /* guicmd */
462 NULL, /* tabcmd */
463 NULL, /* save */
464 NULL, /* wildcard */
8d1ac0c @mbert Import Elvis 2.1 (written by Steve Kirkendall)
authored
465 NULL, /* prgopen */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
466 NULL, /* prgclose */
467 NULL /* stop */
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
468 };
469 #endif
Something went wrong with that request. Please try again.