00001
00002
00003
00004
00005 #ifndef OS2_PORT
00006
00007 #ifndef lint
00008 static char vcid[] = "$Id: raw.c,v 1.3 1995/07/27 19:21:19 duchier Exp $";
00009 #endif
00010
00011
00012
00013
00014
00015 #ifndef NORAW
00016
00017 #include <stdio.h>
00018 #include <ctype.h>
00019 #include <sgtty.h>
00020 #include <termio.h>
00021
00022 #ifndef TANDEM
00023 #define TANDEM 0
00024 #endif
00025
00026 #include <sys/types.h>
00027 #include <sys/time.h>
00028 #include <sys/ioctl.h>
00029
00030 #endif
00031
00032 #include "extern.h"
00033 #include "print.h"
00034 #ifndef OS2_PORT
00035 #include "built_ins.h"
00036 #else
00037 #include "built_in.h"
00038 #endif
00039
00040 #include "types.h"
00041 #include "trees.h"
00042 #include "lefun.h"
00043 #include "login.h"
00044 #include "error.h"
00045 #include "templates.h"
00046 #include "modules.h"
00047
00048 #ifdef X11
00049 #include "xpred.h"
00050 #endif
00051
00052 #ifndef NORAW
00053
00054
00055 #define stdin_fileno fileno (stdin)
00056
00057 extern long level;
00058 static struct sgttyb param_input;
00059 static long mode_raw = FALSE;
00060 static char bufbuf[BUFSIZ+1] = {0};
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 long c_begin_raw ()
00075
00076 {
00077 struct sgttyb param;
00078 struct termio argio;
00079
00080 if (mode_raw)
00081 {
00082 Errorline ("in begin_raw: already in mode raw\n");
00083 return FALSE;
00084 }
00085
00086 if (ioctl (stdin_fileno, TIOCGETP, ¶m_input) == -1)
00087 Errorline ("in begin_raw: cannot get the input parameters\n");
00088
00089 bcopy ((char*)¶m_input, (char*)¶m, sizeof (param));
00090
00091 #if 0
00092
00093 param.sg_flags |= CBREAK | TANDEM | RAW;
00094 #else
00095 param.sg_flags |= CBREAK | TANDEM;
00096 #endif
00097
00098 param.sg_flags &= ~ECHO;
00099
00100 if (ioctl (stdin_fileno, TIOCSETN, ¶m) == -1)
00101 Errorline ("in begin_raw: cannot set the input parameters\n");
00102
00103 if (ioctl (stdin_fileno, TCGETA, &argio) == -1)
00104 Errorline ("in begin_raw: cannot get the terminal\n");
00105
00106
00107 #if 1
00108
00109 argio.c_lflag &= ~(ICANON|ISIG);
00110 argio.c_cc[VMIN] = 1;
00111 argio.c_cc[VTIME] = 0;
00112
00113
00114 argio.c_iflag &= ~(ISTRIP|IXON);
00115
00116
00117 argio.c_oflag |= OPOST|ONLCR;
00118 #else
00119 argio.c_iflag &= ~(ISTRIP);
00120 #endif
00121
00122 if (ioctl (stdin_fileno, TCSETA, &argio) == -1)
00123 Errorline ("in begin_raw: cannot set the terminal\n");
00124
00125 setvbuf (stdin, bufbuf, _IOFBF, BUFSIZ);
00126
00127 bzero (bufbuf, BUFSIZ+1);
00128
00129 mode_raw = TRUE;
00130 return TRUE;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 long c_get_raw ()
00146 {
00147 include_var_builtin (2);
00148 ptr_definition types[2];
00149 long nfds;
00150 fd_set readfd, writefd, exceptfd;
00151 struct timeval timeout;
00152 long char_flag = FALSE, event_flag = FALSE;
00153 long c = 0;
00154 ptr_psi_term key_code;
00155 long level;
00156
00157 types[0] = real;
00158 types[1] = boolean;
00159
00160 begin_builtin (c_get_raw, 2, 0, types);
00161
00162 if ((int)strlen (bufbuf) == 0)
00163 {
00164 level = (unsigned long) aim->c;
00165
00166
00167 do
00168 {
00169 FD_ZERO(&readfd);
00170 FD_SET(stdin_fileno, &readfd);
00171 FD_ZERO(&writefd);
00172 FD_ZERO(&exceptfd);
00173 timeout.tv_sec = 0;
00174 timeout.tv_usec = 100000;
00175
00176 nfds = select (32, &readfd, &writefd, &exceptfd, &timeout);
00177 if (nfds == -1)
00178 {
00179 if (errno != EINTR)
00180 {
00181 Errorline ("it is not possible to read characters or X events\n");
00182 exit_life(TRUE);
00183 }
00184 else
00185 interrupt ();
00186 }
00187 else
00188 if (nfds == 0)
00189 {
00190 #ifdef X11
00191 if (x_exist_event ())
00192 {
00193 event_flag = TRUE;
00194 release_resid (xevent_existing);
00195 }
00196 #endif
00197 }
00198 else
00199 {
00200 if (FD_ISSET(stdin_fileno, &readfd) != 0)
00201 {
00202
00203
00204
00205
00206 if ((c = fgetc (stdin)) != 0)
00207 {
00208 unify_real_result (args[0], (REAL) c);
00209 char_flag = TRUE;
00210
00211 }
00212 }
00213 else
00214 Errorline ("in select: unknown descriptor\n");
00215 }
00216 } while (!(char_flag || event_flag));
00217 }
00218 else
00219 {
00220 unify_real_result (args[0], (REAL) bufbuf[0]);
00221 char_flag = TRUE;
00222 }
00223
00224
00225 if (char_flag)
00226 bcopy (&bufbuf[1], bufbuf, BUFSIZ-1);
00227
00228
00229 unify_bool_result (args[1], event_flag);
00230
00231 success = TRUE;
00232 end_builtin ();
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 long c_put_raw ()
00248 {
00249 include_var_builtin (1);
00250 ptr_definition types[1];
00251
00252 types[0] = real;
00253
00254 begin_builtin (c_put_raw, 1, 0, types);
00255
00256 putchar ((char) val[0]);
00257 fflush (stdout);
00258 success = TRUE;
00259 end_builtin ();
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 long c_end_raw ()
00275
00276 {
00277 if (!mode_raw)
00278 {
00279 Errorline ("in c_end_raw: not in mode raw\n");
00280 return FALSE;
00281 }
00282
00283 if (ioctl (stdin_fileno, TIOCSETN, ¶m_input) == -1)
00284 Errorline ("in end_raw: cannot reset mode raw\n");
00285
00286 setvbuf (stdin, bufbuf, _IONBF, BUFSIZ);
00287 bzero (bufbuf, BUFSIZ);
00288
00289 mode_raw = FALSE;
00290 return TRUE;
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 long c_in_raw ()
00306
00307 {
00308 deref_ptr (aim->a);
00309 unify_bool_result (aim->b, mode_raw);
00310
00311 return TRUE;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 long c_window_flag ()
00327
00328 {
00329 deref_ptr (aim->a);
00330 #ifdef X11
00331 unify_bool_result (aim->b, x_window_creation);
00332 #else
00333 unify_bool_result (aim->b, FALSE);
00334 #endif
00335
00336 return TRUE;
00337 }
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 long c_reset_window_flag ()
00352
00353 {
00354 deref_ptr (aim->a);
00355 #ifdef X11
00356 x_window_creation = FALSE;
00357 #endif
00358
00359 return TRUE;
00360 }
00361
00362
00363 #endif
00364
00365
00366
00367
00368
00369 void raw_setup_builtins ()
00370
00371 {
00372 #ifndef NORAW
00373 new_built_in(bi_module,"begin_raw", predicate, c_begin_raw);
00374 new_built_in(bi_module,"get_raw", predicate, c_get_raw);
00375 new_built_in(bi_module,"put_raw", predicate, c_put_raw);
00376 new_built_in(bi_module,"end_raw", predicate, c_end_raw);
00377 new_built_in(bi_module,"in_raw", function, c_in_raw);
00378 new_built_in(bi_module,"window_flag", function, c_window_flag);
00379 new_built_in(bi_module,"reset_window_flag", predicate, c_reset_window_flag);
00380 #endif
00381 }
00382 #else
00383
00384
00385
00386
00387
00388
00389
00390
00391 #ifndef NORAW
00392
00393 #include <stdio.h>
00394 #include <ctype.h>
00395 #include <stdlib.h>
00396 #include <conio.h>
00397 #define LBSIZE 200
00398 static char lbuf[LBSIZE]={0};
00399 static int lpos[LBSIZE];
00400 static int lindex;
00401 static int lcount;
00402 static int lposition;
00403 static long rseed = 1L;
00404 #ifndef TANDEM
00405 #define TANDEM 0
00406 #endif
00407
00408 #include <sys/types.h>
00409 #endif
00410
00411 #include "extern.h"
00412 #include "print.h"
00413 #include "built_in.h"
00414 #include "types.h"
00415 #include "trees.h"
00416 #include "lefun.h"
00417 #include "login.h"
00418 #include "error.h"
00419 #include "template.h"
00420 #include "modules.h"
00421
00422 #ifndef NORAW
00423
00424
00425 #define stdin_fileno fileno (stdin)
00426
00427 extern long level;
00428 static long mode_raw = FALSE;
00429 static void xputc(int ch)
00430 {
00431 _putch(ch);
00432 }
00433 static int xgetc()
00434 {
00435 return (_getch());
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 long c_begin_raw ()
00451
00452 {
00453 if (mode_raw)
00454 {
00455 Errorline ("in begin_raw: already in mode raw\n");
00456 return FALSE;
00457 }
00458 lposition = 0;
00459 lindex = 0;
00460 lcount = 0;
00461 mode_raw = TRUE;
00462 return TRUE;
00463 }
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 long c_get_raw ()
00478 {
00479 int ch=0;
00480 ptr_definition types[2];
00481 long nfds;
00482 long char_flag = FALSE, event_flag = FALSE;
00483 long c = 0;
00484 ptr_psi_term key_code;
00485 long level;
00486
00487 include_var_builtin (2);
00488 types[0] = real;
00489 types[1] = boolean;
00490
00491 begin_builtin (c_get_raw, 2, 0, types);
00492
00493
00494
00495 event_flag = FALSE;
00496 if (lcount--)
00497 {
00498 unify_real_result (
00499 args[0], (REAL) lbuf[lindex++]);
00500 char_flag = TRUE;
00501 success=TRUE;
00502 }
00503 else
00504 {
00505
00506 level = (unsigned long) aim->c;
00507 for (lcount = 0; ; )
00508 switch (ch = xgetc()) {
00509 case '\r':
00510 case '\n':
00511 case '\020':
00512 lbuf[lcount++] = '\n';
00513 xputc('\r'); xputc('\n'); lposition = 0;\
00514 fflush(stdout);
00515 lindex = 0; lcount--;
00516 unify_real_result (
00517 args[0], (REAL) lbuf[lindex++]);
00518 char_flag = TRUE;
00519 success=TRUE;
00520 break;
00521 case '\010':
00522 case '\177':
00523 if (lcount) {
00524 lcount--;
00525 while (lposition > lpos[lcount]) {
00526 xputc('\010'); xputc(' '); xputc('\010');
00527 fflush(stdout);
00528 lposition--;
00529 }
00530 }
00531 break;
00532 case '\032':
00533 unify_real_result (
00534 args[0], (REAL) ch);
00535 char_flag = FALSE;
00536 success = FALSE;
00537 break;
00538 default:
00539 if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
00540 lbuf[lcount] = ch;
00541 lpos[lcount] = lposition;
00542 if (ch == '\t')
00543 do {
00544 xputc(' ');
00545 fflush(stdout);
00546 } while (++lposition & 7);
00547 else {
00548 xputc(ch); lposition++;
00549 fflush(stdout);
00550 }
00551 lcount++;
00552 }
00553 else {
00554 switch (ch) {
00555 case '\003': interrupt();
00556 case '\032':
00557 event_flag = TRUE;
00558 success = FALSE;
00559 break;
00560 default:
00561 {
00562 fflush(stdout);
00563 unify_real_result (
00564 args[0], (REAL) ch);
00565 char_flag = TRUE;
00566 success = TRUE;
00567 break;
00568 }
00569 }
00570 }
00571 }
00572 }
00573
00574
00575
00576 unify_bool_result (args[1], event_flag);
00577 end_builtin ();
00578 }
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592 long c_put_raw ()
00593 {
00594 int ch;
00595 include_var_builtin (1);
00596
00597
00598 ptr_definition types[1];
00599
00600 types[0] = real;
00601
00602 begin_builtin (c_put_raw, 1, 0, types);
00603 ch = val[0];
00604
00605 if (ch == '\n') {
00606 xputc('\r'); xputc('\n');
00607
00608 }
00609 else {
00610 xputc(ch);
00611
00612 }
00613
00614 fflush (stdout);
00615 success = TRUE;
00616 end_builtin ();
00617 }
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631 long c_end_raw ()
00632
00633 {
00634 if (!mode_raw)
00635 {
00636 Errorline ("in c_end_raw: not in mode raw\n");
00637 return FALSE;
00638 }
00639 mode_raw = FALSE;
00640 return TRUE;
00641 }
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 long c_in_raw ()
00656
00657 {
00658 deref_ptr (aim->a);
00659 unify_bool_result (aim->b, mode_raw);
00660
00661 return TRUE;
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676 long c_window_flag ()
00677
00678 {
00679 deref_ptr (aim->a);
00680 unify_bool_result (aim->b, FALSE);
00681
00682 return TRUE;
00683 }
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697 long c_reset_window_flag ()
00698
00699 {
00700 deref_ptr (aim->a);
00701
00702 return TRUE;
00703 }
00704
00705
00706 #endif
00707
00708
00709
00710
00711
00712 void raw_setup_builtins ()
00713
00714 {
00715 #ifndef NORAW
00716 new_built_in(bi_module,"begin_raw", predicate, &c_begin_raw);
00717 new_built_in(bi_module,"get_raw", predicate, &c_get_raw);
00718 new_built_in(bi_module,"put_raw", predicate, &c_put_raw);
00719 new_built_in(bi_module,"end_raw", predicate, &c_end_raw);
00720 new_built_in(bi_module,"in_raw", function, &c_in_raw);
00721 new_built_in(bi_module,"window_flag", function, &c_window_flag);
00722 new_built_in(bi_module,"reset_window_flag", predicate, &c_reset_window_flag);
00723 #endif
00724 }
00725 #endif