Jack Jansen | fbfacf6 | 1995-01-18 13:44:20 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* |
| 26 | ** Written by Jack Jansen, October 1994, initially only to allow him to |
| 27 | ** test the ctb module:-) |
| 28 | */ |
| 29 | |
| 30 | #include "allobjects.h" |
| 31 | #include "modsupport.h" /* For getargs() etc. */ |
| 32 | #include "structmember.h" |
| 33 | |
| 34 | #include <console.h> |
| 35 | |
| 36 | static object *ErrorObject; |
| 37 | |
| 38 | #define OFF(x) offsetof(struct __copt, x) |
| 39 | |
| 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)}, |
| 51 | {NULL} |
| 52 | }; |
| 53 | |
| 54 | static unsigned char mytitle[256]; |
| 55 | typedef struct { |
| 56 | OB_HEAD |
| 57 | } coptobject; |
| 58 | |
| 59 | staticforward typeobject Xxotype; |
| 60 | |
| 61 | #define is_coptobject(v) ((v)->ob_type == &Xxotype) |
| 62 | |
| 63 | static coptobject * |
| 64 | newcoptobject() |
| 65 | { |
| 66 | coptobject *self; |
| 67 | self = NEWOBJ(coptobject, &Xxotype); |
| 68 | return self; |
| 69 | } |
| 70 | |
| 71 | /* Xxo methods */ |
| 72 | |
| 73 | static void |
| 74 | copt_dealloc(self) |
| 75 | coptobject *self; |
| 76 | { |
| 77 | DEL(self); |
| 78 | } |
| 79 | |
| 80 | static object * |
| 81 | copt_getattr(self, name) |
| 82 | coptobject *self; |
| 83 | char *name; |
| 84 | { |
| 85 | return getmember((char *)&console_options, copt_memberlist, name); |
| 86 | } |
| 87 | |
| 88 | static int |
| 89 | copt_setattr(self, name, v) |
| 90 | coptobject *self; |
| 91 | char *name; |
| 92 | object *v; |
| 93 | { |
| 94 | char *str; |
| 95 | int len; |
| 96 | |
| 97 | if ( strcmp(name, "title") == 0 ) { |
| 98 | if ( !v || !is_stringobject(v)) { |
| 99 | err_setstr(ErrorObject, "title must be a string"); |
| 100 | return -1; |
| 101 | } |
| 102 | str = getstringvalue(v); |
| 103 | len = strlen(str); |
| 104 | mytitle[0] = (unsigned char)len; |
| 105 | strncpy((char *)mytitle+1, str, mytitle[0]); |
| 106 | console_options.title = mytitle; |
| 107 | return 0; |
| 108 | } |
| 109 | return setmember((char *)&console_options, copt_memberlist, name, v); |
| 110 | } |
| 111 | |
| 112 | static typeobject Xxotype = { |
| 113 | OB_HEAD_INIT(&Typetype) |
| 114 | 0, /*ob_size*/ |
| 115 | "console options", /*tp_name*/ |
| 116 | sizeof(coptobject), /*tp_basicsize*/ |
| 117 | 0, /*tp_itemsize*/ |
| 118 | /* methods */ |
| 119 | (destructor)copt_dealloc, /*tp_dealloc*/ |
| 120 | 0, /*tp_print*/ |
| 121 | (getattrfunc)copt_getattr, /*tp_getattr*/ |
| 122 | (setattrfunc)copt_setattr, /*tp_setattr*/ |
| 123 | 0, /*tp_compare*/ |
| 124 | 0, /*tp_repr*/ |
| 125 | 0, /*tp_as_number*/ |
| 126 | 0, /*tp_as_sequence*/ |
| 127 | 0, /*tp_as_mapping*/ |
| 128 | 0, /*tp_hash*/ |
| 129 | }; |
| 130 | |
| 131 | /* ------------------------------------------- */ |
| 132 | |
| 133 | typedef struct { |
| 134 | OB_HEAD |
| 135 | FILE *fp; |
| 136 | object *file; |
| 137 | } cons_object; |
| 138 | |
| 139 | staticforward typeobject constype; |
| 140 | |
| 141 | #define is_cons_object(v) ((v)->ob_type == &constype) |
| 142 | |
| 143 | static cons_object * |
| 144 | newcons_object(fp, file) |
| 145 | FILE *fp; |
| 146 | object *file; |
| 147 | { |
| 148 | cons_object *self; |
| 149 | self = NEWOBJ(cons_object, &constype); |
| 150 | if (self == NULL) |
| 151 | return NULL; |
| 152 | self->fp = fp; |
| 153 | self->file = file; |
| 154 | return self; |
| 155 | } |
| 156 | |
| 157 | /* cons methods */ |
| 158 | |
| 159 | static void |
| 160 | cons_dealloc(self) |
| 161 | cons_object *self; |
| 162 | { |
| 163 | DECREF(self->file); |
| 164 | DEL(self); |
| 165 | } |
| 166 | |
| 167 | static object * |
| 168 | cons_setmode(self, args) |
| 169 | cons_object *self; |
| 170 | object *args; |
| 171 | { |
| 172 | int mode; |
| 173 | |
| 174 | if (!getargs(args, "i", &mode)) |
| 175 | return NULL; |
| 176 | csetmode(mode, self->fp); |
| 177 | INCREF(None); |
| 178 | return None; |
| 179 | } |
| 180 | |
| 181 | static object * |
| 182 | cons_cleos(self, args) |
| 183 | cons_object *self; |
| 184 | object *args; |
| 185 | { |
| 186 | if (!getnoarg(args)) |
| 187 | return NULL; |
| 188 | ccleos(self->fp); |
| 189 | INCREF(None); |
| 190 | return None; |
| 191 | } |
| 192 | |
| 193 | static object * |
| 194 | cons_cleol(self, args) |
| 195 | cons_object *self; |
| 196 | object *args; |
| 197 | { |
| 198 | if (!getnoarg(args)) |
| 199 | return NULL; |
| 200 | ccleol(self->fp); |
| 201 | INCREF(None); |
| 202 | return None; |
| 203 | } |
| 204 | |
| 205 | static object * |
| 206 | cons_show(self, args) |
| 207 | cons_object *self; |
| 208 | object *args; |
| 209 | { |
| 210 | if (!getnoarg(args)) |
| 211 | return NULL; |
| 212 | cshow(self->fp); |
| 213 | INCREF(None); |
| 214 | return None; |
| 215 | } |
| 216 | |
| 217 | static object * |
| 218 | cons_hide(self, args) |
| 219 | cons_object *self; |
| 220 | object *args; |
| 221 | { |
| 222 | if (!getnoarg(args)) |
| 223 | return NULL; |
| 224 | chide(self->fp); |
| 225 | INCREF(None); |
| 226 | return None; |
| 227 | } |
| 228 | |
| 229 | static object * |
| 230 | cons_echo2printer(self, args) |
| 231 | cons_object *self; |
| 232 | object *args; |
| 233 | { |
| 234 | if (!getnoarg(args)) |
| 235 | return NULL; |
| 236 | cecho2printer(self->fp); |
| 237 | INCREF(None); |
| 238 | return None; |
| 239 | } |
| 240 | |
| 241 | static object * |
| 242 | cons_gotoxy(self, args) |
| 243 | cons_object *self; |
| 244 | object *args; |
| 245 | { |
| 246 | int x, y; |
| 247 | |
| 248 | if (!getargs(args, "(ii)", &x, &y)) |
| 249 | return NULL; |
| 250 | cgotoxy(x, y, self->fp); |
| 251 | INCREF(None); |
| 252 | return None; |
| 253 | } |
| 254 | |
| 255 | static object * |
| 256 | cons_getxy(self, args) |
| 257 | cons_object *self; |
| 258 | object *args; |
| 259 | { |
| 260 | int x, y; |
| 261 | |
| 262 | if (!getnoarg(args)) |
| 263 | return NULL; |
| 264 | cgetxy(&x, &y, self->fp); |
| 265 | return mkvalue("(ii)", x, y); |
| 266 | } |
| 267 | |
| 268 | static object * |
| 269 | cons_settabs(self, args) |
| 270 | cons_object *self; |
| 271 | object *args; |
| 272 | { |
| 273 | int arg; |
| 274 | |
| 275 | if (!getargs(args, "i", &arg)) |
| 276 | return NULL; |
| 277 | csettabs(arg, self->fp); |
| 278 | INCREF(None); |
| 279 | return None; |
| 280 | } |
| 281 | |
| 282 | static object * |
| 283 | cons_inverse(self, args) |
| 284 | cons_object *self; |
| 285 | object *args; |
| 286 | { |
| 287 | int arg; |
| 288 | |
| 289 | if (!getargs(args, "i", &arg)) |
| 290 | return NULL; |
| 291 | cinverse(arg, self->fp); |
| 292 | INCREF(None); |
| 293 | return None; |
| 294 | } |
| 295 | |
| 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 */ |
| 308 | }; |
| 309 | |
| 310 | static object * |
| 311 | cons_getattr(self, name) |
| 312 | cons_object *self; |
| 313 | char *name; |
| 314 | { |
| 315 | if ( strcmp(name, "file") == 0 ) { |
| 316 | INCREF(self->file); |
| 317 | return self->file; |
| 318 | } |
| 319 | return findmethod(cons_methods, (object *)self, name); |
| 320 | } |
| 321 | |
| 322 | static typeobject constype = { |
| 323 | OB_HEAD_INIT(&Typetype) |
| 324 | 0, /*ob_size*/ |
| 325 | "cons", /*tp_name*/ |
| 326 | sizeof(cons_object), /*tp_basicsize*/ |
| 327 | 0, /*tp_itemsize*/ |
| 328 | /* methods */ |
| 329 | (destructor)cons_dealloc, /*tp_dealloc*/ |
| 330 | 0, /*tp_print*/ |
| 331 | (getattrfunc)cons_getattr, /*tp_getattr*/ |
| 332 | 0, /*tp_setattr*/ |
| 333 | 0, /*tp_compare*/ |
| 334 | 0, /*tp_repr*/ |
| 335 | 0, /*tp_as_number*/ |
| 336 | 0, /*tp_as_sequence*/ |
| 337 | 0, /*tp_as_mapping*/ |
| 338 | 0, /*tp_hash*/ |
| 339 | }; |
| 340 | /* --------------------------------------------------------------------- */ |
| 341 | |
| 342 | /* Return a new console */ |
| 343 | |
| 344 | static object * |
| 345 | maccons_copen(self, args) |
| 346 | object *self; /* Not used */ |
| 347 | object *args; |
| 348 | { |
| 349 | FILE *fp; |
| 350 | object *file; |
| 351 | cons_object *rv; |
| 352 | char *name; |
| 353 | unsigned char nbuf[256]; |
| 354 | int len; |
| 355 | |
| 356 | name = NULL; |
| 357 | if (!getnoarg(args)) |
| 358 | return NULL; |
| 359 | if ( (fp=fopenc()) == NULL ) { |
| 360 | err_errno(ErrorObject); |
| 361 | return NULL; |
| 362 | } |
| 363 | if ( (file=newopenfileobject(fp, "<a console>", "r+", fclose)) == NULL) |
| 364 | return NULL; |
| 365 | rv = newcons_object(fp, file); |
| 366 | if ( rv == NULL ) { |
| 367 | fclose(fp); |
| 368 | return NULL; |
| 369 | } |
| 370 | return (object *)rv; |
| 371 | } |
| 372 | |
| 373 | /* Return an open file as a console */ |
| 374 | |
| 375 | static object * |
| 376 | maccons_fopen(self, args) |
| 377 | object *self; /* Not used */ |
| 378 | object *args; |
| 379 | { |
| 380 | cons_object *rv; |
| 381 | object *file; |
| 382 | FILE *fp; |
| 383 | |
| 384 | if (!newgetargs(args, "O", &file)) |
| 385 | return NULL; |
| 386 | if ( !is_fileobject(file) ) { |
| 387 | err_badarg(); |
| 388 | return NULL; |
| 389 | } |
| 390 | fp = getfilefile(file); |
| 391 | if ( !isatty(fileno(fp)) ) { |
| 392 | err_setstr(ErrorObject, "File is not a console"); |
| 393 | return NULL; |
| 394 | } |
| 395 | rv = newcons_object(fp, file); |
| 396 | if ( rv == NULL ) { |
| 397 | return NULL; |
| 398 | } |
| 399 | INCREF(file); |
| 400 | return (object *)rv; |
| 401 | } |
| 402 | |
| 403 | /* List of functions defined in the module */ |
| 404 | |
| 405 | static struct methodlist maccons_methods[] = { |
| 406 | {"fopen", (method)maccons_fopen, 1}, |
| 407 | {"copen", (method)maccons_copen, 0}, |
| 408 | {NULL, NULL} /* sentinel */ |
| 409 | }; |
| 410 | |
| 411 | |
| 412 | /* Initialization function for the module (*must* be called initmacconsole) */ |
| 413 | |
| 414 | void |
| 415 | initmacconsole() |
| 416 | { |
| 417 | object *m, *d, *o; |
| 418 | |
| 419 | /* Create the module and add the functions */ |
| 420 | m = initmodule("macconsole", maccons_methods); |
| 421 | |
| 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); |
| 433 | |
| 434 | /* Check for errors */ |
| 435 | if (err_occurred()) |
| 436 | fatal("can't initialize module macconsole"); |
| 437 | } |