home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Emulation / Atari800 / devices.c < prev    next >
C/C++ Source or Header  |  1998-02-17  |  9KB  |  673 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <string.h>
  4. #include    <ctype.h>
  5.  
  6. #ifdef VMS
  7. #include    <unixio.h>
  8. #include    <file.h>
  9. #else
  10. #include    <fcntl.h>
  11. #ifndef    AMIGA
  12. #include    <unistd.h>
  13. #endif
  14. #endif
  15.  
  16. #define    DO_DIR
  17.  
  18. #ifdef AMIGA
  19. #undef    DO_DIR
  20. #endif
  21.  
  22. #ifdef VMS
  23. #undef    DO_DIR
  24. #endif
  25.  
  26. #ifdef DO_DIR
  27. #include    <dirent.h>
  28. #endif
  29.  
  30. static char *rcsid = "$Id: devices.c,v 1.20 1998/02/17 thor, david Exp $";
  31.  
  32. #define FALSE   0
  33. #define TRUE    1
  34.  
  35. #include "atari.h"
  36. #include "cpu.h"
  37. #include "devices.h"
  38. #include "rt-config.h"
  39. #include "mem.h"
  40. #include "patch.h"
  41.  
  42. #define    ICHIDZ    0x0020
  43. #define    ICDNOZ    0x0021
  44. #define    ICCOMZ    0x0022
  45. #define    ICSTAZ    0x0023
  46. #define    ICBALZ    0x0024
  47. #define    ICBAHZ    0x0025
  48. #define    ICPTLZ    0x0026
  49. #define    ICPTHZ    0x0027
  50. #define    ICBLLZ    0x0028
  51. #define    ICBLHZ    0x0029
  52. #define    ICAX1Z    0x002a
  53. #define    ICAX2Z    0x002b
  54.  
  55. static char *H[5] =
  56. {
  57.   ".",
  58.   atari_h1_dir,
  59.   atari_h2_dir,
  60.   atari_h3_dir,
  61.   atari_h4_dir
  62. };
  63.  
  64. static int devbug = FALSE;
  65.  
  66. static FILE    *fp[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  67. static int    flag[8];
  68.  
  69. static int    fid;
  70. static char    filename[64];
  71.  
  72. #ifdef DO_DIR
  73. static DIR    *dp = NULL;
  74. #endif
  75.  
  76. void Device_Initialise (int *argc, char *argv[])
  77. {
  78.   int i;
  79.   int j;
  80.  
  81.   for (i=j=1;i<*argc;i++)
  82.     {
  83.       if (strncmp(argv[i],"-H1",2) == 0)
  84.     H[1] = argv[i]+2;
  85.       else if (strncmp(argv[i],"-H2",2) == 0)
  86.     H[2] = argv[i]+2;
  87.       else if (strncmp(argv[i],"-H3",2) == 0)
  88.     H[3] = argv[i]+2;
  89.       else if (strncmp(argv[i],"-H4",2) == 0)
  90.     H[4] = argv[i]+2;
  91.       else if (strcmp(argv[i],"-devbug") == 0)
  92.     devbug = TRUE;
  93.       else
  94.     argv[j++] = argv[i];
  95.     }
  96.  
  97.   if (devbug)
  98.     for (i=0;i<5;i++)
  99.       printf ("H%d: %s\n", i, H[i]);
  100.  
  101.   *argc = j;
  102. }
  103.  
  104. int Device_isvalid (char ch)
  105. {
  106.   int valid;
  107.  
  108.   if (isalnum(ch))
  109.     valid = TRUE;
  110.   else
  111.     switch (ch)
  112.       {
  113.       case ':' :
  114.       case '.' :
  115.       case '_' :
  116.       case '*' :
  117.       case '?' :
  118.     valid = TRUE;
  119.     break;
  120.       default :
  121.     valid = FALSE;
  122.     break;
  123.       }
  124.  
  125.   return valid;
  126. }
  127.  
  128. void Device_GetFilename ()
  129. {
  130.   int bufadr;
  131.   int offset = 0;
  132.   int devnam = TRUE;
  133.  
  134.   bufadr = DPeek(ICBALZ);
  135.  
  136.   while (Device_isvalid(Peek(bufadr)))
  137.     {
  138.       int byte = Peek(bufadr);
  139.  
  140.       if (!devnam)
  141.     {
  142.       if (isupper(byte))
  143.         byte = tolower(byte);
  144.  
  145.       filename[offset++] = byte;
  146.     }
  147.       else if (byte == ':')
  148.     devnam = FALSE;
  149.  
  150.       bufadr++;
  151.     }
  152.  
  153.   filename[offset++] = '\0';
  154. }
  155.  
  156. int match (char *pattern, char *filename)
  157. {
  158.   int status = TRUE;
  159.  
  160.   while (status && *filename && *pattern)
  161.     {
  162.       switch (*pattern)
  163.     {
  164.     case '?' :
  165.       pattern++;
  166.       filename++;
  167.       break;
  168.     case '*' :
  169.       if (*filename == pattern[1])
  170.         {
  171.           pattern++;
  172.         }
  173.       else
  174.         {
  175.           filename++;
  176.         }
  177.       break;
  178.     default :
  179.       status = (*pattern++ == *filename++);
  180.       break;
  181.     }
  182.     }
  183.   if ((*filename)
  184.       || ((*pattern)
  185.       && (((*pattern != '*') && (*pattern != '?'))
  186.           || pattern[1])))
  187.   {
  188.     status = 0;
  189.   }
  190.   return status;
  191. }
  192.  
  193. void Device_HHOPEN (void)
  194. {
  195.   char fname[128];
  196.   int devnum;
  197.   int temp;
  198.  
  199.   if (devbug)
  200.     printf ("HHOPEN\n");
  201.  
  202.   fid = Peek(0x2e) >> 4;
  203.  
  204.   if (fp[fid])
  205.     {
  206.       fclose (fp[fid]);
  207.       fp[fid] = NULL;
  208.     }
  209. /*
  210.   flag[fid] = (Peek(ICDNOZ) == 2) ? TRUE : FALSE;
  211. */
  212.   Device_GetFilename ();
  213. /*
  214.   if (Peek(ICDNOZ) == 0)
  215.     sprintf (fname, "./%s", filename);
  216.   else
  217.     sprintf (fname, "%s/%s", h_prefix, filename);
  218. */
  219.   devnum = Peek(ICDNOZ);
  220.   if (devnum > 9)
  221.     {
  222.       printf ("Attempt to access H%d: device\n", devnum);
  223.       exit (1);
  224.     }
  225.  
  226.   if (devnum >= 5)
  227.     {
  228.       flag[fid] = TRUE;
  229.       devnum -= 5;
  230.     }
  231.   else
  232.     {
  233.       flag[fid] = FALSE;
  234.     }
  235.  
  236. #ifdef VMS
  237. /* Assumes H[devnum] is a directory _logical_, not an explicit directory
  238.    specification! */
  239.   sprintf (fname, "%s:%s", H[devnum], filename);
  240. #else
  241.   sprintf (fname, "%s/%s", H[devnum], filename);
  242. #endif
  243.  
  244.   temp = Peek(ICAX1Z);
  245.  
  246.   switch (temp)
  247.     {
  248.     case 4 :
  249.       fp[fid] = fopen (fname, "r");
  250.       if (fp[fid])
  251.     {
  252.       regY = 1;
  253.       ClrN;
  254.     }
  255.       else
  256.     {
  257.       regY = 170;
  258.       SetN;
  259.     }
  260.       break;
  261.     case 6 :
  262.     case 7 :
  263. #ifdef DO_DIR
  264.       fp[fid] = tmpfile ();
  265.       if (fp[fid])
  266.     {
  267.       dp = opendir (H[devnum]);
  268.       if (dp)
  269.         {
  270.           struct dirent    *entry;
  271.           
  272.           while ((entry = readdir (dp)))
  273.         {
  274.           if (match(filename, entry->d_name))
  275.             fprintf (fp[fid],"%s\n", entry->d_name);
  276.         }
  277.           
  278.           closedir (dp);
  279.  
  280.           regY = 1;
  281.           ClrN;
  282.  
  283.           rewind (fp[fid]);
  284.  
  285.           flag[fid] = TRUE;
  286.         }
  287.       else
  288.         {
  289.           regY = 163;
  290.           SetN;
  291.           fclose (fp[fid]);
  292.           fp[fid] = NULL;
  293.         }
  294.     }
  295.       else
  296. #endif
  297.     {
  298.       regY = 163;
  299.       SetN;
  300.     }
  301.       break;
  302.     case 8 :
  303.       fp[fid] = fopen (fname, "w");
  304.       if (fp[fid])
  305.     {
  306.       regY = 1;
  307.       ClrN;
  308.     }
  309.       else
  310.     {
  311.       regY = 170;
  312.       SetN;
  313.     }
  314.       break;
  315.     default :
  316.       regY = 163;
  317.       SetN;
  318.       break;
  319.     }
  320. }
  321.  
  322. void Device_HHCLOS (void)
  323. {
  324.   if (devbug)
  325.     printf ("HHCLOS\n");
  326.  
  327.   fid = Peek(0x2e) >> 4;
  328.  
  329.   if (fp[fid])
  330.     {
  331.       fclose (fp[fid]);
  332.       fp[fid] = NULL;
  333.     }
  334.  
  335.   regY = 1;
  336.   ClrN;
  337. }
  338.  
  339. void Device_HHREAD (void)
  340. {
  341.   if (devbug)
  342.     printf ("HHREAD\n");
  343.  
  344.   fid = Peek(0x2e) >> 4;
  345.  
  346.   if (fp[fid])
  347.     {
  348.       int ch;
  349.  
  350.       ch = fgetc (fp[fid]);
  351.       if (ch != EOF)
  352.     {
  353.       if (flag[fid])
  354.         {
  355.           switch (ch)
  356.         {
  357.         case '\n' :
  358.           ch = 0x9b;
  359.           break;
  360.         default :
  361.           break;
  362.         }
  363.         }
  364.  
  365.       regA = ch;
  366.       regY = 1;
  367.       ClrN;
  368.     }
  369.       else
  370.     {
  371.       regY = 136;
  372.       SetN;
  373.     }
  374.     }
  375.   else
  376.     {
  377.       regY = 163;
  378.       SetN;
  379.     }
  380. }
  381.  
  382. void Device_HHWRIT (void)
  383. {
  384.   if (devbug)
  385.     printf ("HHWRIT\n");
  386.  
  387.   fid = Peek(0x2e) >> 4;
  388.  
  389.   if (fp[fid])
  390.     {
  391.       int ch;
  392.  
  393.       ch = regA;
  394.       if (flag[fid])
  395.     {
  396.       switch (ch)
  397.         {
  398.         case 0x9b :
  399.           ch = 0x0a;
  400.           break;
  401.         default :
  402.           break;
  403.         }
  404.     }
  405.       fputc (ch, fp[fid]);
  406.       regY = 1;
  407.       ClrN;
  408.     }
  409.   else
  410.     {
  411.       regY = 163;
  412.       SetN;
  413.     }
  414. }
  415.  
  416. void Device_HHSTAT (void)
  417. {
  418.   if (devbug)
  419.     printf ("HHSTAT\n");
  420.  
  421.   fid = Peek(0x2e) >> 4;
  422.  
  423.   regY = 146;
  424.   SetN;
  425. }
  426.  
  427. void Device_HHSPEC (void)
  428. {
  429.   if (devbug)
  430.     printf ("HHSPEC\n");
  431.  
  432.   fid = Peek(0x2e) >> 4;
  433.  
  434.   switch (Peek(ICCOMZ))
  435.     {
  436.     case 0x20 :
  437.       printf ("RENAME Command\n");
  438.       break;
  439.     case 0x21 :
  440.       printf ("DELETE Command\n");
  441.       break;
  442.     case 0x23 :
  443.       printf ("LOCK Command\n");
  444.       break;
  445.     case 0x24 :
  446.       printf ("UNLOCK Command\n");
  447.       break;
  448.     case 0x25 :
  449.       printf ("NOTE Command\n");
  450.       break;
  451.     case 0x26 :
  452.       printf ("POINT Command\n");
  453.       break;
  454.     case 0xFE :
  455.       printf ("FORMAT Command\n");
  456.       break;
  457.     default :
  458.       printf ("UNKNOWN Command\n");
  459.       break;
  460.     }
  461.  
  462.   regY = 146;
  463.   SetN;
  464. }
  465.  
  466. void Device_HHINIT (void)
  467. {
  468.   if (devbug)
  469.     printf ("HHINIT\n");
  470. }
  471.  
  472. static int phfd = -1;
  473. void Device_PHCLOS (void);
  474. static char *spool_file = NULL;
  475.  
  476. void Device_PHOPEN (void)
  477. {
  478.   if (devbug)
  479.     printf ("PHOPEN\n");
  480.  
  481.   if (phfd != -1)
  482.     Device_PHCLOS ();
  483.  
  484.   spool_file = tmpnam (NULL);
  485.   phfd = open (spool_file, O_CREAT | O_TRUNC | O_WRONLY, 0777);
  486.   if (phfd != -1)
  487.     {
  488.     regY = 1;
  489.     ClrN;
  490.     }
  491.   else
  492.     {
  493.     regY = 130;
  494.     SetN;
  495.     }
  496. }
  497.  
  498. void Device_PHCLOS (void)
  499. {
  500.   if (devbug)
  501.     printf ("PHCLOS\n");
  502.  
  503.   if (phfd != -1)
  504.     {
  505.       char command[256];
  506.       int status;
  507.  
  508.       close (phfd);
  509.  
  510.       sprintf (command, print_command, spool_file);
  511.       system (command);
  512.  
  513. #ifndef VMS
  514.       status = unlink (spool_file);
  515.       if (status == -1)
  516.     {
  517.       perror (spool_file);
  518.       exit (1);
  519.     }
  520. #endif
  521.  
  522.       phfd = -1;
  523.     }
  524.  
  525.   regY = 1;
  526.   ClrN;
  527. }
  528.  
  529. void Device_PHREAD (void)
  530. {
  531.   if (devbug)
  532.     printf ("PHREAD\n");
  533.  
  534.   regY = 146;
  535.   SetN;
  536. }
  537.  
  538. void Device_PHWRIT (void)
  539. {
  540.   unsigned char byte;
  541.   int status;
  542.  
  543.   if (devbug)
  544.     printf ("PHWRIT\n");
  545.  
  546.   byte = regA;
  547.   if (byte == 0x9b)
  548.     byte = '\n';
  549.  
  550.   status = write (phfd, &byte, 1);
  551.   if (status == 1)
  552.     {
  553.       regY = 1;
  554.       ClrN;
  555.     }
  556.   else
  557.     {
  558.       regY = 144;
  559.       SetN;
  560.     }
  561. }
  562.  
  563. void Device_PHSTAT (void)
  564. {
  565.   if (devbug)
  566.     printf ("PHSTAT\n");
  567. }
  568.  
  569. void Device_PHSPEC (void)
  570. {
  571.   if (devbug)
  572.     printf ("PHSPEC\n");
  573.  
  574.   regY = 1;
  575.   ClrN;
  576. }
  577.  
  578. void Device_PHINIT (void)
  579. {
  580.   if (devbug)
  581.     printf ("PHINIT\n");
  582.  
  583.   phfd = -1;
  584.   regY = 1;
  585.   ClrN;
  586. }
  587. /*
  588.    ================================
  589.    N = 0 : I/O Successful and Y = 1
  590.    N = 1 : I/O Error and Y = error#
  591.    ================================
  592. */
  593.  
  594. void K_Device (UBYTE esc_code)
  595. {
  596. UBYTE ch;
  597.  
  598.   switch (esc_code)
  599.     {
  600.     case ESC_K_OPEN :
  601.     case ESC_K_CLOSE :
  602.       regY = 1;
  603.       ClrN;
  604.       break;
  605.     case ESC_K_WRITE :
  606.     case ESC_K_STATUS :
  607.     case ESC_K_SPECIAL :
  608.       regY = 146;
  609.       SetN;
  610.       break;
  611.     case ESC_K_READ :
  612.       ch = getchar();
  613.       switch (ch)
  614.     {
  615.     case '\n' :
  616.       ch = 0x9b;
  617.       break;
  618.     default :
  619.       break;
  620.     }
  621.       regA = ch;
  622.       regY = 1;
  623.       ClrN;
  624.       break;
  625.     }
  626. }
  627.  
  628. void E_Device (UBYTE esc_code)
  629. {
  630. UBYTE ch;
  631.  
  632.   switch (esc_code)
  633.     {
  634.     case ESC_E_OPEN :
  635.       printf ("Editor Open\n");
  636.       regY = 1;
  637.       ClrN;
  638.       break;
  639.     case ESC_E_READ :
  640.       ch = getchar();
  641.       switch (ch)
  642.     {
  643.     case '\n' :
  644.       ch = 0x9b;
  645.       break;
  646.     default :
  647.       break;
  648.     }
  649.       regA = ch;
  650.       regY = 1;
  651.       ClrN;
  652.       break;
  653.     case ESC_E_WRITE :
  654.       ch = regA;
  655.       switch (ch)
  656.     {
  657.     case 0x7d :
  658.       putchar ('*');
  659.       break;
  660.     case 0x9b :
  661.       putchar ('\n');
  662.       break;
  663.     default :
  664.       if ((ch >= 0x20) && (ch <= 0x7e)) /* for DJGPP */
  665.         putchar (ch & 0x7f);
  666.       break;
  667.     }
  668.       regY = 1;
  669.       ClrN;
  670.       break;
  671.     }
  672. }
  673.