This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.0 alpha 4
[perl5.git] / ext / curses / curses.mus
CommitLineData
79072805 1/* $RCSfile: curses.mus,v $$Revision: 4.1 $$Date: 92/08/07 18:28:53 $
ff8e2863
LW
2 *
3 * $Log: curses.mus,v $
79072805
LW
4 * Revision 4.1 92/08/07 18:28:53 lwall
5 *
9f971974
LW
6 * Revision 4.0.1.2 92/06/08 16:06:12 lwall
7 * patch20: function key support added to curses.mus
8 *
db4e6270
LW
9 * Revision 4.0.1.1 91/11/05 19:06:19 lwall
10 * patch11: usub/curses.mus now supports SysV curses
11 *
fe14fcc3
LW
12 * Revision 4.0 91/03/20 01:56:13 lwall
13 * 4.0 baseline.
14 *
ff8e2863
LW
15 * Revision 3.0.1.1 90/08/09 04:05:21 lwall
16 * patch19: Initial revision
17 *
18 */
19
20#include "EXTERN.h"
21#include "perl.h"
ff8e2863
LW
22
23char *savestr();
24
db4e6270 25#undef bool
ff8e2863
LW
26#include <curses.h>
27
db4e6270
LW
28#ifndef A_UNDERLINE
29#define NOSETATTR
30#define A_STANDOUT 0x0200
31#define A_UNDERLINE 0x0100
32#define A_REVERSE 0x0200
33#define A_BLINK 0x0400
34#define A_BOLD 0x0800
35#define A_ALTCHARSET 0x1000
36#define A_NORMAL 0
37#endif
38
39#ifdef USG
40static char *tcbuf = NULL;
41#endif
42
43#ifdef NOSETATTR
44static unsigned curattr = NORMAL;
45#endif
46
ff8e2863
LW
47static enum uservars {
48 UV_curscr,
49 UV_stdscr,
ff8e2863
LW
50 UV_ttytype,
51 UV_LINES,
52 UV_COLS,
53 UV_ERR,
54 UV_OK,
db4e6270
LW
55#ifdef BSD
56 UV_Def_term,
57 UV_My_term,
58#endif
59 UV_A_STANDOUT,
60 UV_A_UNDERLINE,
61 UV_A_REVERSE,
62 UV_A_BLINK,
63 UV_A_DIM,
64 UV_A_BOLD,
65 UV_A_NORMAL,
ff8e2863
LW
66};
67
68static enum usersubs {
69 US_addch,
70 US_waddch,
71 US_addstr,
72 US_waddstr,
73 US_box,
74 US_clear,
75 US_wclear,
76 US_clearok,
77 US_clrtobot,
78 US_wclrtobot,
79 US_clrtoeol,
80 US_wclrtoeol,
81 US_delch,
82 US_wdelch,
83 US_deleteln,
84 US_wdeleteln,
85 US_erase,
86 US_werase,
ff8e2863
LW
87 US_idlok,
88 US_insch,
89 US_winsch,
90 US_insertln,
91 US_winsertln,
92 US_move,
93 US_wmove,
94 US_overlay,
95 US_overwrite,
ff8e2863
LW
96 US_refresh,
97 US_wrefresh,
98 US_standout,
99 US_wstandout,
100 US_standend,
101 US_wstandend,
102 US_cbreak,
103 US_nocbreak,
104 US_echo,
105 US_noecho,
106 US_getch,
107 US_wgetch,
108 US_getstr,
109 US_wgetstr,
110 US_raw,
111 US_noraw,
ff8e2863
LW
112 US_baudrate,
113 US_delwin,
114 US_endwin,
115 US_erasechar,
ff8e2863
LW
116 US_getyx,
117 US_inch,
118 US_winch,
119 US_initscr,
120 US_killchar,
121 US_leaveok,
122 US_longname,
ff8e2863
LW
123 US_mvwin,
124 US_newwin,
125 US_nl,
126 US_nonl,
127 US_scrollok,
128 US_subwin,
129 US_touchline,
ff8e2863
LW
130 US_touchwin,
131 US_unctrl,
132 US_gettmode,
133 US_mvcur,
134 US_scroll,
135 US_savetty,
136 US_resetty,
137 US_setterm,
db4e6270
LW
138 US_attroff,
139 US_wattroff,
140 US_attron,
141 US_wattron,
142 US_attrset,
143 US_wattrset,
144#ifdef CURSEFMT
145 US_printw, /* remove */
146 US_wprintw, /* remove */
147 US_scanw, /* delete */
148 US_wscanw, /* delete */
149#endif
150 US_getcap,
151#ifdef BSD
152 US_flushok,
153 US_fullname,
154 US_touchoverlap,
ff8e2863
LW
155 US_tstp,
156 US__putchar,
db4e6270
LW
157#endif
158 US_mysub,
159 US_testcallback,
ff8e2863
LW
160};
161
162static int usersub();
163static int userset();
164static int userval();
165
166int
167init_curses()
168{
169 struct ufuncs uf;
170 char *filename = "curses.c";
171
172 uf.uf_set = userset;
173 uf.uf_val = userval;
174
175#define MAGICVAR(name, ix) uf.uf_index = ix, magicname(name, &uf, sizeof uf)
176
177 MAGICVAR("curscr", UV_curscr);
178 MAGICVAR("stdscr", UV_stdscr);
ff8e2863
LW
179 MAGICVAR("ttytype", UV_ttytype);
180 MAGICVAR("LINES", UV_LINES);
181 MAGICVAR("COLS", UV_COLS);
182 MAGICVAR("ERR", UV_ERR);
183 MAGICVAR("OK", UV_OK);
db4e6270
LW
184#ifdef BSD
185 MAGICVAR("Def_term",UV_Def_term);
186 MAGICVAR("My_term", UV_My_term);
187#endif
188 MAGICVAR("A_STANDOUT", UV_A_STANDOUT);
189 MAGICVAR("A_UNDERLINE", UV_A_UNDERLINE);
190 MAGICVAR("A_REVERSE", UV_A_REVERSE);
191 MAGICVAR("A_BLINK", UV_A_BLINK);
192 MAGICVAR("A_DIM", UV_A_DIM);
193 MAGICVAR("A_BOLD", UV_A_BOLD);
194 MAGICVAR("A_NORMAL", UV_A_NORMAL);
ff8e2863
LW
195
196 make_usub("addch", US_addch, usersub, filename);
197 make_usub("waddch", US_waddch, usersub, filename);
198 make_usub("addstr", US_addstr, usersub, filename);
199 make_usub("waddstr", US_waddstr, usersub, filename);
200 make_usub("box", US_box, usersub, filename);
201 make_usub("clear", US_clear, usersub, filename);
202 make_usub("wclear", US_wclear, usersub, filename);
203 make_usub("clearok", US_clearok, usersub, filename);
204 make_usub("clrtobot", US_clrtobot, usersub, filename);
205 make_usub("wclrtobot", US_wclrtobot, usersub, filename);
206 make_usub("clrtoeol", US_clrtoeol, usersub, filename);
207 make_usub("wclrtoeol", US_wclrtoeol, usersub, filename);
208 make_usub("delch", US_delch, usersub, filename);
209 make_usub("wdelch", US_wdelch, usersub, filename);
210 make_usub("deleteln", US_deleteln, usersub, filename);
211 make_usub("wdeleteln", US_wdeleteln, usersub, filename);
212 make_usub("erase", US_erase, usersub, filename);
213 make_usub("werase", US_werase, usersub, filename);
ff8e2863
LW
214 make_usub("idlok", US_idlok, usersub, filename);
215 make_usub("insch", US_insch, usersub, filename);
216 make_usub("winsch", US_winsch, usersub, filename);
217 make_usub("insertln", US_insertln, usersub, filename);
218 make_usub("winsertln", US_winsertln, usersub, filename);
219 make_usub("move", US_move, usersub, filename);
220 make_usub("wmove", US_wmove, usersub, filename);
221 make_usub("overlay", US_overlay, usersub, filename);
222 make_usub("overwrite", US_overwrite, usersub, filename);
ff8e2863
LW
223 make_usub("refresh", US_refresh, usersub, filename);
224 make_usub("wrefresh", US_wrefresh, usersub, filename);
225 make_usub("standout", US_standout, usersub, filename);
226 make_usub("wstandout", US_wstandout, usersub, filename);
227 make_usub("standend", US_standend, usersub, filename);
228 make_usub("wstandend", US_wstandend, usersub, filename);
229 make_usub("cbreak", US_cbreak, usersub, filename);
230 make_usub("nocbreak", US_nocbreak, usersub, filename);
231 make_usub("echo", US_echo, usersub, filename);
232 make_usub("noecho", US_noecho, usersub, filename);
233 make_usub("getch", US_getch, usersub, filename);
234 make_usub("wgetch", US_wgetch, usersub, filename);
235 make_usub("getstr", US_getstr, usersub, filename);
236 make_usub("wgetstr", US_wgetstr, usersub, filename);
237 make_usub("raw", US_raw, usersub, filename);
238 make_usub("noraw", US_noraw, usersub, filename);
ff8e2863
LW
239 make_usub("baudrate", US_baudrate, usersub, filename);
240 make_usub("delwin", US_delwin, usersub, filename);
241 make_usub("endwin", US_endwin, usersub, filename);
242 make_usub("erasechar", US_erasechar, usersub, filename);
ff8e2863
LW
243 make_usub("getyx", US_getyx, usersub, filename);
244 make_usub("inch", US_inch, usersub, filename);
245 make_usub("winch", US_winch, usersub, filename);
246 make_usub("initscr", US_initscr, usersub, filename);
247 make_usub("killchar", US_killchar, usersub, filename);
248 make_usub("leaveok", US_leaveok, usersub, filename);
249 make_usub("longname", US_longname, usersub, filename);
ff8e2863
LW
250 make_usub("mvwin", US_mvwin, usersub, filename);
251 make_usub("newwin", US_newwin, usersub, filename);
252 make_usub("nl", US_nl, usersub, filename);
253 make_usub("nonl", US_nonl, usersub, filename);
254 make_usub("scrollok", US_scrollok, usersub, filename);
255 make_usub("subwin", US_subwin, usersub, filename);
256 make_usub("touchline", US_touchline, usersub, filename);
ff8e2863
LW
257 make_usub("touchwin", US_touchwin, usersub, filename);
258 make_usub("unctrl", US_unctrl, usersub, filename);
259 make_usub("gettmode", US_gettmode, usersub, filename);
260 make_usub("mvcur", US_mvcur, usersub, filename);
261 make_usub("scroll", US_scroll, usersub, filename);
262 make_usub("savetty", US_savetty, usersub, filename);
263 make_usub("resetty", US_resetty, usersub, filename);
264 make_usub("setterm", US_setterm, usersub, filename);
db4e6270
LW
265 make_usub("getcap", US_getcap, usersub, filename);
266 make_usub("attroff", US_attroff, usersub, filename);
267 make_usub("wattroff", US_wattroff, usersub, filename);
268 make_usub("attron", US_attron, usersub, filename);
269 make_usub("wattron", US_wattron, usersub, filename);
270 make_usub("attrset", US_attrset, usersub, filename);
271 make_usub("wattrset", US_wattrset, usersub, filename);
272#ifdef CURSEFMT
273 make_usub("printw", US_printw, usersub, filename);
274 make_usub("wprintw", US_wprintw, usersub, filename);
275 make_usub("scanw", US_scanw, usersub, filename);
276 make_usub("wscanw", US_wscanw, usersub, filename);
277#endif
278#ifdef BSD
279 make_usub("flushok", US_flushok, usersub, filename);
280 make_usub("fullname", US_fullname, usersub, filename);
281 make_usub("touchoverlap", US_touchoverlap,usersub, filename);
ff8e2863
LW
282 make_usub("tstp", US_tstp, usersub, filename);
283 make_usub("_putchar", US__putchar, usersub, filename);
db4e6270
LW
284#endif
285 make_usub("testcallback", US_testcallback,usersub, filename);
286 };
287
288#ifdef USG
289static char
290*getcap(cap)
291register char *cap;
292{
293 static char nocaperr[] = "Cannot read termcap entry.";
294
295 extern char *tgetstr();
296
297 if (tcbuf == NULL) {
298 if ((tcbuf = malloc(1024)) == NULL) {
299 fatal(nocaperr);
300 }
301 if (tgetent(tcbuf, ttytype) == -1) {
302 fatal(nocaperr);
303 }
304 }
305
306 return (tgetstr(cap, NULL));
307}
308#endif
309
310#ifdef NOSETATTR
311#define attron(attr) wattron(stdscr, attr)
312#define attroff(attr) wattroff(stdscr, attr)
313#define attset(attr) wattset(stdscr, attr)
314
315int
316wattron(win, attr)
317WINDOW *win;
318chtype attr;
319{
320 curattr |= attr;
321 if (curattr & A_STANDOUT) {
322 return(wstandout(win));
323 } else {
324 return(wstandend(win));
325 }
326}
327
328int
329wattroff(win, attr)
330WINDOW *win;
331chtype attr;
332{
333 curattr &= (~attr);
334 if (curattr & A_STANDOUT) {
335 return(wstandout(win));
336 } else {
337 return(wstandend(win));
338 }
339}
ff8e2863 340
db4e6270
LW
341int
342wattrset(win, attr)
343WINDOW *win;
344chtype attr;
345{
346 curattr = attr;
347 if (curattr & A_STANDOUT) {
348 return(wstandout(win));
349 } else {
350 return(wstandend(win));
351 }
352}
353
354#endif
355
ff8e2863
LW
356static int
357usersub(ix, sp, items)
358int ix;
359register int sp;
360register int items;
361{
362 STR **st = stack->ary_array + sp;
363 register int i;
364 register char *tmps;
365 register STR *Str; /* used in str_get and str_gnum macros */
366
367 switch (ix) {
368CASE int addch
369I char ch
370END
371
372CASE int waddch
373I WINDOW* win
374I char ch
375END
376
377CASE int addstr
378I char* str
379END
380
381CASE int waddstr
382I WINDOW* win
383I char* str
384END
385
386CASE int box
387I WINDOW* win
388I char vert
389I char hor
390END
391
392CASE int clear
393END
394
395CASE int wclear
396I WINDOW* win
397END
398
399CASE int clearok
400I WINDOW* win
401I bool boolf
402END
403
404CASE int clrtobot
405END
406
407CASE int wclrtobot
408I WINDOW* win
409END
410
411CASE int clrtoeol
412END
413
414CASE int wclrtoeol
415I WINDOW* win
416END
417
418CASE int delch
419END
420
421CASE int wdelch
422I WINDOW* win
423END
424
425CASE int deleteln
426END
427
428CASE int wdeleteln
429I WINDOW* win
430END
431
432CASE int erase
433END
434
435CASE int werase
436I WINDOW* win
437END
438
ff8e2863
LW
439CASE int idlok
440I WINDOW* win
441I bool boolf
442END
443
444CASE int insch
445I char c
446END
447
448CASE int winsch
449I WINDOW* win
450I char c
451END
452
453CASE int insertln
454END
455
456CASE int winsertln
457I WINDOW* win
458END
459
460CASE int move
461I int y
462I int x
463END
464
465CASE int wmove
466I WINDOW* win
467I int y
468I int x
469END
470
471CASE int overlay
472I WINDOW* win1
473I WINDOW* win2
474END
475
476CASE int overwrite
477I WINDOW* win1
478I WINDOW* win2
479END
480
ff8e2863
LW
481CASE int refresh
482END
483
484CASE int wrefresh
485I WINDOW* win
486END
487
488CASE int standout
489END
490
491CASE int wstandout
492I WINDOW* win
493END
494
495CASE int standend
496END
497
498CASE int wstandend
499I WINDOW* win
500END
501
502CASE int cbreak
503END
504
505CASE int nocbreak
506END
507
508CASE int echo
509END
510
511CASE int noecho
512END
513
514 case US_getch:
db4e6270
LW
515 if (items != 0)
516 fatal("Usage: &getch()");
517 else {
518 int retval;
ff8e2863
LW
519 char retch;
520
db4e6270 521 retval = getch();
ff8e2863
LW
522 if (retval == EOF)
523 st[0] = &str_undef;
524 else {
525 retch = retval;
9f971974
LW
526 if (retval > 0377)
527 str_numset(st[0], (double) retval);
528 else
529 str_nset(st[0], &retch, 1);
ff8e2863 530 }
db4e6270
LW
531 }
532 return sp;
ff8e2863
LW
533
534 case US_wgetch:
db4e6270
LW
535 if (items != 1)
536 fatal("Usage: &wgetch($win)");
537 else {
538 int retval;
ff8e2863 539 char retch;
db4e6270 540 WINDOW* win = *(WINDOW**) str_get(st[1]);
ff8e2863 541
db4e6270 542 retval = wgetch(win);
ff8e2863
LW
543 if (retval == EOF)
544 st[0] = &str_undef;
545 else {
546 retch = retval;
9f971974
LW
547 if (retval > 0377)
548 str_numset(st[0], (double) retval);
549 else
550 str_nset(st[0], &retch, 1);
ff8e2863 551 }
db4e6270
LW
552 }
553 return sp;
ff8e2863
LW
554
555CASE int getstr
db4e6270 556O char* str
ff8e2863
LW
557END
558
559CASE int wgetstr
560I WINDOW* win
db4e6270 561O char* str
ff8e2863
LW
562END
563
564CASE int raw
565END
566
567CASE int noraw
568END
569
570CASE int baudrate
571END
572
573CASE int delwin
574I WINDOW* win
575END
576
577CASE int endwin
578END
579
580CASE int erasechar
581END
582
ff8e2863
LW
583 case US_getyx:
584 if (items != 3)
585 fatal("Usage: &getyx($win, $y, $x)");
586 else {
587 int retval;
588 STR* str = str_new(0);
589 WINDOW* win = *(WINDOW**) str_get(st[1]);
590 int y;
591 int x;
592
593 do_sprintf(str, items - 1, st + 1);
594 retval = getyx(win, y, x);
595 str_numset(st[2], (double)y);
596 str_numset(st[3], (double)x);
597 str_numset(st[0], (double) retval);
598 str_free(str);
599 }
600 return sp;
ff8e2863
LW
601
602CASE int inch
603END
604
605CASE int winch
606I WINDOW* win
607END
608
609CASE WINDOW* initscr
610END
611
612CASE int killchar
613END
614
615CASE int leaveok
616I WINDOW* win
617I bool boolf
618END
619
db4e6270 620#ifdef BSD
ff8e2863
LW
621CASE char* longname
622I char* termbuf
623IO char* name
624END
db4e6270
LW
625#else
626CASE char* longname
627I char* termbug
628I char* name
ff8e2863 629END
db4e6270 630#endif
ff8e2863
LW
631
632CASE int mvwin
633I WINDOW* win
634I int y
635I int x
636END
637
638CASE WINDOW* newwin
639I int lines
640I int cols
641I int begin_y
642I int begin_x
643END
644
645CASE int nl
646END
647
648CASE int nonl
649END
650
651CASE int scrollok
652I WINDOW* win
653I bool boolf
654END
655
656CASE WINDOW* subwin
657I WINDOW* win
658I int lines
659I int cols
660I int begin_y
661I int begin_x
662END
663
664CASE int touchline
665I WINDOW* win
666I int y
667I int startx
668I int endx
669END
670
ff8e2863
LW
671CASE int touchwin
672I WINDOW* win
673END
674
675CASE char* unctrl
676I char ch
677END
678
679CASE int gettmode
680END
681
682CASE int mvcur
683I int lasty
684I int lastx
685I int newy
686I int newx
687END
688
689CASE int scroll
690I WINDOW* win
691END
692
693CASE int savetty
694END
695
696CASE void resetty
697END
698
699CASE int setterm
700I char* name
701END
702
db4e6270
LW
703CASE int attroff
704I chtype str
705END
706
707CASE int wattroff
708I chtype str
709END
710
711CASE int wattron
712I chtype str
713END
714
715CASE int attron
716I chtype str
717END
718
719CASE int attrset
720I chtype str
721END
722
723CASE int wattrset
724I chtype str
725END
726
727#ifdef CURSEFMT
728 case US_printw:
729 if (items < 1)
730 fatal("Usage: &printw($fmt, $arg1, $arg2, ... )");
731 else {
732 int retval;
733 STR* str = str_new(0);
734
79072805 735 do_sprintf(str, items, st + 1);
db4e6270
LW
736 retval = addstr(str->str_ptr);
737 str_numset(st[0], (double) retval);
738 str_free(str);
739 }
740 return sp;
741
742 case US_wprintw:
743 if (items < 2)
744 fatal("Usage: &wprintw($win, $fmt, $arg1, $arg2, ... )");
745 else {
746 int retval;
747 STR* str = str_new(0);
748 WINDOW* win = *(WINDOW**) str_get(st[1]);
749
750 do_sprintf(str, items - 1, st + 1);
751 retval = waddstr(win, str->str_ptr);
752 str_numset(st[0], (double) retval);
753 str_free(str);
754 }
755 return sp;
756
757#endif
758
759CASE char* getcap
760I char* str
761END
762
763#ifdef BSD
764CASE int flushok
765I WINDOW* win
766I bool boolf
767END
768
769CASE int fullname
770I char* termbuf
771IO char* name
772END
773
774CASE int touchoverlap
775I WINDOW* win1
776I WINDOW* win2
777END
778
ff8e2863
LW
779CASE int tstp
780END
781
782CASE int _putchar
783I char ch
784END
785
db4e6270
LW
786 case US_testcallback:
787 sp = callback("callback", sp + items, curcsv->wantarray, 1, items);
788 break;
789
790#endif
791
ff8e2863
LW
792 default:
793 fatal("Unimplemented user-defined subroutine");
794 }
795 return sp;
796}
797
798static int
799userval(ix, str)
800int ix;
801STR *str;
802{
803 switch (ix) {
804 case UV_COLS:
805 str_numset(str, (double)COLS);
806 break;
ff8e2863
LW
807 case UV_ERR:
808 str_numset(str, (double)ERR);
809 break;
810 case UV_LINES:
811 str_numset(str, (double)LINES);
812 break;
ff8e2863
LW
813 case UV_OK:
814 str_numset(str, (double)OK);
815 break;
816 case UV_curscr:
817 str_nset(str, &curscr, sizeof(WINDOW*));
818 break;
819 case UV_stdscr:
820 str_nset(str, &stdscr, sizeof(WINDOW*));
821 break;
822 case UV_ttytype:
823 str_set(str, ttytype);
824 break;
db4e6270
LW
825#ifdef BSD
826 case UV_Def_term:
827 str_set(str, Def_term);
828 break;
829 case UV_My_term:
830 str_numset(str, (double)My_term);
831 break;
832#endif
833 case UV_A_STANDOUT:
834 str_numset(str, (double)A_STANDOUT);
835 break;
836 case UV_A_UNDERLINE:
837 str_numset(str, (double)A_UNDERLINE);
838 break;
839 case UV_A_REVERSE:
840 str_numset(str, (double)A_REVERSE);
841 break;
842 case UV_A_BLINK:
843 str_numset(str, (double)A_BLINK);
844 break;
845 case UV_A_DIM:
846 str_numset(str, (double)A_DIM);
847 break;
848 case UV_A_BOLD:
849 str_numset(str, (double)A_BOLD);
850 break;
851 case UV_A_NORMAL:
852 str_numset(str, (double)A_NORMAL);
853 break;
ff8e2863
LW
854 }
855 return 0;
856}
857
858static int
859userset(ix, str)
860int ix;
861STR *str;
862{
863 switch (ix) {
864 case UV_COLS:
865 COLS = (int)str_gnum(str);
866 break;
ff8e2863
LW
867 case UV_LINES:
868 LINES = (int)str_gnum(str);
869 break;
ff8e2863
LW
870 case UV_ttytype:
871 strcpy(ttytype, str_get(str)); /* hope it fits */
db4e6270
LW
872#ifdef USG
873 if (tcbuf != NULL) {
874 free(tcbuf);
875 tcbuf = NULL;
876 }
877#endif
878 break;
879#ifdef BSD
880 case UV_Def_term:
881 Def_term = savestr(str_get(str)); /* never freed */
882 break;
883 case UV_My_term:
884 My_term = (bool)str_gnum(str);
ff8e2863 885 break;
db4e6270 886#endif
ff8e2863
LW
887 }
888 return 0;
889}