1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
26 ** Written by Jack Jansen, October 1994, initially only to allow him to
27 ** test the ctb module:-)
30 #include "allobjects.h"
31 #include "modsupport.h" /* For getargs() etc. */
32 #include "structmember.h"
36 static object
*ErrorObject
;
38 #define OFF(x) offsetof(struct __copt, x)
40 static struct memberlist copt_memberlist
[] = {
41 {"top", T_SHORT
, OFF(top
)},
42 {"left", T_SHORT
, OFF(left
)},
43 {"title", T_PSTRING
, OFF(title
)},
44 {"procID", T_SHORT
, OFF(procID
), RO
},
45 {"txFont", T_SHORT
, OFF(txFont
)},
46 {"txSize", T_SHORT
, OFF(txSize
)},
47 {"txFace", T_SHORT
, OFF(txFace
)},
48 {"nrows", T_SHORT
, OFF(nrows
)},
49 {"ncols", T_SHORT
, OFF(ncols
)},
50 {"pause_atexit", T_SHORT
, OFF(pause_atexit
)},
54 static unsigned char mytitle
[256];
59 staticforward typeobject Xxotype
;
61 #define is_coptobject(v) ((v)->ob_type == &Xxotype)
67 self
= NEWOBJ(coptobject
, &Xxotype
);
81 copt_getattr(self
, name
)
85 return getmember((char *)&console_options
, copt_memberlist
, name
);
89 copt_setattr(self
, name
, v
)
97 if ( strcmp(name
, "title") == 0 ) {
98 if ( !v
|| !is_stringobject(v
)) {
99 err_setstr(ErrorObject
, "title must be a string");
102 str
= getstringvalue(v
);
104 mytitle
[0] = (unsigned char)len
;
105 strncpy((char *)mytitle
+1, str
, mytitle
[0]);
106 console_options
.title
= mytitle
;
109 return setmember((char *)&console_options
, copt_memberlist
, name
, v
);
112 static typeobject Xxotype
= {
113 OB_HEAD_INIT(&Typetype
)
115 "console options", /*tp_name*/
116 sizeof(coptobject
), /*tp_basicsize*/
119 (destructor
)copt_dealloc
, /*tp_dealloc*/
121 (getattrfunc
)copt_getattr
, /*tp_getattr*/
122 (setattrfunc
)copt_setattr
, /*tp_setattr*/
126 0, /*tp_as_sequence*/
131 /* ------------------------------------------- */
139 staticforward typeobject constype
;
141 #define is_cons_object(v) ((v)->ob_type == &constype)
144 newcons_object(fp
, file
)
149 self
= NEWOBJ(cons_object
, &constype
);
168 cons_setmode(self
, args
)
174 if (!getargs(args
, "i", &mode
))
176 csetmode(mode
, self
->fp
);
182 cons_cleos(self
, args
)
194 cons_cleol(self
, args
)
206 cons_show(self
, args
)
218 cons_hide(self
, args
)
230 cons_echo2printer(self
, args
)
236 cecho2printer(self
->fp
);
242 cons_gotoxy(self
, args
)
248 if (!getargs(args
, "(ii)", &x
, &y
))
250 cgotoxy(x
, y
, self
->fp
);
256 cons_getxy(self
, args
)
264 cgetxy(&x
, &y
, self
->fp
);
265 return mkvalue("(ii)", x
, y
);
269 cons_settabs(self
, args
)
275 if (!getargs(args
, "i", &arg
))
277 csettabs(arg
, self
->fp
);
283 cons_inverse(self
, args
)
289 if (!getargs(args
, "i", &arg
))
291 cinverse(arg
, self
->fp
);
296 static struct methodlist cons_methods
[] = {
297 {"setmode", (method
)cons_setmode
},
298 {"gotoxy", (method
)cons_gotoxy
},
299 {"getxy", (method
)cons_getxy
},
300 {"cleos", (method
)cons_cleos
},
301 {"cleol", (method
)cons_cleol
},
302 {"settabs", (method
)cons_settabs
},
303 {"inverse", (method
)cons_inverse
},
304 {"show", (method
)cons_show
},
305 {"hide", (method
)cons_hide
},
306 {"echo2printer", (method
)cons_echo2printer
},
307 {NULL
, NULL
} /* sentinel */
311 cons_getattr(self
, name
)
315 if ( strcmp(name
, "file") == 0 ) {
319 return findmethod(cons_methods
, (object
*)self
, name
);
322 static typeobject constype
= {
323 OB_HEAD_INIT(&Typetype
)
326 sizeof(cons_object
), /*tp_basicsize*/
329 (destructor
)cons_dealloc
, /*tp_dealloc*/
331 (getattrfunc
)cons_getattr
, /*tp_getattr*/
336 0, /*tp_as_sequence*/
340 /* --------------------------------------------------------------------- */
342 /* Return a new console */
345 maccons_copen(self
, args
)
346 object
*self
; /* Not used */
353 unsigned char nbuf
[256];
359 if ( (fp
=fopenc()) == NULL
) {
360 err_errno(ErrorObject
);
363 if ( (file
=newopenfileobject(fp
, "<a console>", "r+", fclose
)) == NULL
)
365 rv
= newcons_object(fp
, file
);
373 /* Return an open file as a console */
376 maccons_fopen(self
, args
)
377 object
*self
; /* Not used */
384 if (!newgetargs(args
, "O", &file
))
386 if ( !is_fileobject(file
) ) {
390 fp
= getfilefile(file
);
391 if ( !isatty(fileno(fp
)) ) {
392 err_setstr(ErrorObject
, "File is not a console");
395 rv
= newcons_object(fp
, file
);
403 /* List of functions defined in the module */
405 static struct methodlist maccons_methods
[] = {
406 {"fopen", (method
)maccons_fopen
, 1},
407 {"copen", (method
)maccons_copen
, 0},
408 {NULL
, NULL
} /* sentinel */
412 /* Initialization function for the module (*must* be called initmacconsole) */
419 /* Create the module and add the functions */
420 m
= initmodule("macconsole", maccons_methods
);
422 /* Add some symbolic constants to the module */
423 #define INTATTR(name, value) o = newintobject(value); dictinsert(d, name, o);
424 d
= getmoduledict(m
);
425 ErrorObject
= newstringobject("macconsole.error");
426 dictinsert(d
, "error", ErrorObject
);
427 o
= (object
*)newcoptobject();
428 dictinsert(d
, "options", o
);
429 INTATTR("C_RAW", C_RAW
);
430 INTATTR("C_CBREAK", C_CBREAK
);
431 INTATTR("C_ECHO", C_ECHO
);
432 INTATTR("C_NOECHO", C_NOECHO
);
434 /* Check for errors */
436 fatal("can't initialize module macconsole");