home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / atari / tos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-23  |  6.8 KB  |  343 lines

  1. /*    SCCS Id: @(#)tos.c    3.1    90/14/08
  2. /* NetHack may be freely redistributed.  See license for details. */
  3.  
  4. /*
  5.  *  TOS system functions.
  6.  */
  7.  
  8. #define NEED_VARARGS
  9. #include "hack.h"
  10. #include "termcap.h"
  11.  
  12. #ifdef TOS
  13.  
  14. # include <osbind.h>
  15. # ifndef WORD
  16. #  define WORD short        /* 16 bits -- redefine if necessary */
  17. # endif
  18.  
  19. #include <ctype.h>
  20.  
  21. static char NDECL(DOSgetch);
  22. static char NDECL(BIOSgetch);
  23. static void NDECL(init_aline);
  24. char *_a_line;            /* for Line A variables */
  25. # ifdef TEXTCOLOR
  26. boolean colors_changed = FALSE;
  27. # endif
  28.  
  29. int
  30. tgetch()
  31. {
  32.     char ch;
  33.  
  34.     /* BIOSgetch can use the numeric key pad on IBM compatibles. */
  35.     if (flags.BIOS)
  36.         ch = BIOSgetch();
  37.     else
  38.         ch = DOSgetch();
  39.     return ((ch == '\r') ? '\n' : ch);
  40. }
  41.  
  42. /*
  43.  *  Keyboard translation tables.
  44.  */
  45. #define KEYPADLO    0x61
  46. #define KEYPADHI    0x71
  47.  
  48. #define PADKEYS     (KEYPADHI - KEYPADLO + 1)
  49. #define iskeypad(x)    (KEYPADLO <= (x) && (x) <= KEYPADHI)
  50.  
  51. /*
  52.  * Keypad keys are translated to the normal values below.
  53.  * When flags.BIOS is active, shifted keypad keys are translated to the
  54.  *    shift values below.
  55.  */
  56. static const struct pad {
  57.     char normal, shift, cntrl;
  58. } keypad[PADKEYS] = {
  59.             {C('['), 'Q', C('[')},        /* UNDO */
  60.             {'?', '/', '?'},        /* HELP */
  61.             {'(', 'a', '('},        /* ( */
  62.             {')', 'w', ')'},        /* ) */
  63.             {'/', '/', '/'},        /* / */
  64.             {C('p'), '$', C('p')},        /* * */
  65.             {'y', 'Y', C('y')},        /* 7 */
  66.             {'k', 'K', C('k')},        /* 8 */
  67.             {'u', 'U', C('u')},        /* 9 */
  68.             {'h', 'H', C('h')},        /* 4 */
  69.             {'.', '.', '.'},
  70.             {'l', 'L', C('l')},        /* 6 */
  71.             {'b', 'B', C('b')},        /* 1 */
  72.             {'j', 'J', C('j')},        /* 2 */
  73.             {'n', 'N', C('n')},        /* 3 */
  74.             {'i', 'I', C('i')},        /* Ins */
  75.             {'.', ':', ':'}            /* Del */
  76. }, numpad[PADKEYS] = {
  77.             {C('['), 'Q', C('[')}    ,    /* UNDO */
  78.             {'?', '/', '?'},        /* HELP */
  79.             {'(', 'a', '('},        /* ( */
  80.             {')', 'w', ')'},        /* ) */
  81.             {'/', '/', '/'},        /* / */
  82.             {C('p'), '$', C('p')},        /* * */
  83.             {'7', M('7'), '7'},        /* 7 */
  84.             {'8', M('8'), '8'},        /* 8 */
  85.             {'9', M('9'), '9'},        /* 9 */
  86.             {'4', M('4'), '4'},        /* 4 */
  87.             {'.', '.', '.'},        /* 5 */
  88.             {'6', M('6'), '6'},        /* 6 */
  89.             {'1', M('1'), '1'},        /* 1 */
  90.             {'2', M('2'), '2'},        /* 2 */
  91.             {'3', M('3'), '3'},        /* 3 */
  92.             {'i', 'I', C('i')},        /* Ins */
  93.             {'.', ':', ':'}            /* Del */
  94. };
  95.  
  96. /*
  97.  * Unlike Ctrl-letter, the Alt-letter keystrokes have no specific ASCII
  98.  * meaning unless assigned one by a keyboard conversion table, so the
  99.  * keyboard BIOS normally does not return a character code when Alt-letter
  100.  * is pressed.    So, to interpret unassigned Alt-letters, we must use a
  101.  * scan code table to translate the scan code into a letter, then set the
  102.  * "meta" bit for it.  -3.
  103.  */
  104. #define SCANLO        0x10
  105. #define SCANHI        0x32
  106. #define SCANKEYS    (SCANHI - SCANLO + 1)
  107. #define inmap(x)    (SCANLO <= (x) && (x) <= SCANHI)
  108.  
  109. static const char scanmap[SCANKEYS] = {     /* ... */
  110.     'q','w','e','r','t','y','u','i','o','p','[',']', '\n',
  111.     0, 'a','s','d','f','g','h','j','k','l',';','\'', '`',
  112.     0, '\\', 'z','x','c','v','b','N','m'     /* ... */
  113. };
  114.  
  115. /*
  116.  * BIOSgetch gets keys directly with a BIOS call.
  117.  */
  118. #define SHIFT        (0x1 | 0x2)
  119. #define CTRL        0x4
  120. #define ALT        0x8
  121.  
  122. static char
  123. BIOSgetch()
  124. {
  125.     unsigned char scan, shift, ch;
  126.     const struct pad *kpad;
  127.  
  128.     long  x;
  129.  
  130.     /* Get scan code.
  131.      */
  132.     x = Crawcin();
  133.     ch = x & 0x0ff;
  134.     scan = (x & 0x00ff0000L) >> 16;
  135.     /* Get shift status.
  136.      */
  137.     shift = Kbshift(-1);
  138.  
  139.     /* Translate keypad keys */
  140.     if (iskeypad(scan)) {
  141.         kpad = flags.num_pad ? numpad : keypad;
  142.         if (shift & SHIFT)
  143.             ch = kpad[scan - KEYPADLO].shift;
  144.         else if (shift & CTRL)
  145.             ch = kpad[scan - KEYPADLO].cntrl;
  146.         else
  147.             ch = kpad[scan - KEYPADLO].normal;
  148.     }
  149.     /* Translate unassigned Alt-letters */
  150.     if ((shift & ALT) && !ch) {
  151.         if (inmap(scan))
  152.             ch = scanmap[scan - SCANLO];
  153.         return (isprint(ch) ? M(ch) : ch);
  154.     }
  155.     return ch;
  156. }
  157.  
  158. static char
  159. DOSgetch()
  160. {
  161.     return (Crawcin() & 0x007f);
  162. }
  163.  
  164.  
  165. long
  166. freediskspace(path)
  167. char *path;
  168. {
  169.     int drive = 0;
  170.     struct {
  171.         long freal; /*free allocation units*/
  172.         long total; /*total number of allocation units*/
  173.         long bps;   /*bytes per sector*/
  174.         long pspal; /*physical sectors per allocation unit*/
  175.     } freespace;
  176.     if (path[0] && path[1] == ':')
  177.         drive = (toupper(path[0]) - 'A') + 1;
  178.     if (Dfree(&freespace,drive)<0) return -1;
  179.     return freespace.freal*freespace.bps*freespace.pspal;
  180. }
  181.  
  182. /*
  183.  * Functions to get filenames using wildcards
  184.  */
  185. int
  186. findfirst(path)
  187. char *path;
  188. {
  189.     return (Fsfirst(path, 0) == 0);
  190. }
  191.  
  192. int
  193. findnext()
  194. {
  195.     return (Fsnext() == 0);
  196. }
  197.  
  198. char *
  199. foundfile_buffer()
  200. {
  201.     return (char *)Fgetdta() + 30;
  202. }
  203.  
  204. long
  205. filesize(file)
  206. char *file;
  207. {
  208.     if (findfirst(file))
  209.         return  (* (long *) ((char *)Fgetdta() + 26));
  210.     else
  211.         return -1L;
  212. }
  213.  
  214. /*
  215.  * Chdrive() changes the default drive.
  216.  */
  217. void
  218. chdrive(str)
  219. char *str;
  220. {
  221.     char *ptr;
  222.     char drive;
  223.  
  224.     if ((ptr = index(str, ':')) != NULL) {
  225.         drive = toupper(*(ptr - 1));
  226.         (void)Dsetdrv(drive - 'A');
  227.     }
  228.     return;
  229. }
  230.  
  231.  
  232. void
  233. get_scr_size()
  234. {
  235.     init_aline();
  236.     LI = (*((WORD  *)(_a_line + -42L))) + 1;
  237.     CO = (*((WORD  *)(_a_line + -44L))) + 1;
  238. }
  239.  
  240. # define BIGBUF  8192
  241.  
  242. int
  243. _copyfile(from, to)
  244. char *from, *to;
  245. {
  246.     int fromfd, tofd, r;
  247.     char *buf;
  248.  
  249.     if ((fromfd = open(from, O_RDONLY|O_BINARY, 0)) < 0)
  250.         return -1;
  251.     if ((tofd = open(to, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, FCMASK)) < 0)
  252.         return -1;
  253.     buf = (char *)alloc((size_t)BIGBUF);
  254.     while ( (r = read(fromfd, buf, BIGBUF)) > 0)
  255.         write(tofd, buf, r);
  256.     close(fromfd);
  257.     close(tofd);
  258.     free(buf);
  259.     return 0;    /* successful */
  260. }
  261.  
  262. int kbhit()
  263. {
  264.     return Cconis();
  265. }
  266.  
  267. static void
  268. init_aline()
  269. {
  270. # ifdef __GNUC__
  271. /* line A calls nuke registers d0-d2,a0-a2; not all compilers regard these
  272.    as scratch registers, though, so we save them
  273.  */
  274.     asm(" moveml d0-d2/a0-a2, sp@-");
  275.     asm(" .word 0xa000; movel d0, __a_line");
  276.     asm(" moveml sp@+, d0-d2/a0-a2");
  277. # else
  278.     asm(" movem.l d0-d2/a0-a2, -(sp)");
  279.     asm(" .dc.w 0xa000");    /* tweak as necessary for your compiler */
  280.     asm(" move.l d0, __a_line");
  281.     asm(" movem.l (sp)+, d0-d2/a0-a2");
  282. # endif
  283. }
  284.  
  285. # ifdef TEXTCOLOR
  286. /* used in termcap.c to decide how to set up the hilites */
  287. unsigned long tos_numcolors = 2;
  288.  
  289. void
  290. set_colors()
  291. {
  292.     if (!flags.BIOS)
  293.         return;
  294.     init_aline();
  295.     tos_numcolors = 1 << (((unsigned char *) _a_line)[1]);
  296.     if (tos_numcolors <= 2) {            /* mono */
  297.         flags.use_color = FALSE;
  298.         return;
  299.     } else {
  300.         colors_changed = TRUE;
  301.     }
  302. }
  303.  
  304. void
  305. restore_colors()
  306. {
  307.     colors_changed = FALSE;
  308. }
  309. # endif /* TEXTCOLOR */
  310.  
  311. # ifdef SUSPEND
  312.  
  313. #include    <signal.h>
  314.  
  315. #   ifdef MINT
  316. extern int __mint;
  317. #   endif
  318.  
  319. int
  320. dosuspend() {
  321. #   ifdef MINT
  322.     extern int kill();
  323.     if (__mint == 0) {
  324. #   endif
  325.         pline("Sorry, it seems we have no SIGTSTP here.  Try ! or S.");
  326. #   ifdef MINT
  327.     }
  328.     else if(signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
  329.         suspend_nhwindows(NULL);
  330.         (void) signal(SIGTSTP, SIG_DFL);
  331.         (void) kill(0, SIGTSTP);
  332.         get_scr_size();
  333.         resume_nhwindows();
  334.     } else {
  335.         pline("I don't think your shell has job control.");
  336.     }
  337. #   endif /* MINT */
  338.     return(0);
  339. }
  340. # endif /* SUSPEND */
  341.  
  342. #endif /* TOS */
  343.