home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / UTILS / DIRUTL / SETDRU13.LBR / SETDRU.CQ / SETDRU.C
Text File  |  2000-06-30  |  7KB  |  261 lines

  1. /* setdru -- install setdru1 or setdru2                                     */
  2. /* copyright 1983-84  Michael M Rubenstein                                  */
  3.  
  4. /* version 1.3   9 Feb 84  -- eliminated Z80 code                           */
  5. /*                            change in run time library only, but version  */
  6. /*                            changed to avoid confusion.                   */
  7.  
  8. /* version 1.2   2 Feb 84  -- code modified for distribution                */
  9.  
  10. /* version 1.1  26 Nov 83  -- allowed user numbers up thru 31 for           */
  11. /*                            compatibility with zcpr                       */
  12.  
  13. #define NULL    0
  14. #define TRUE    1
  15. #define FALSE   0
  16.  
  17. /* declaration of structure for fcb            */
  18. struct fcb_ {
  19.             struct {
  20.                    char drive;        /* drive number */
  21.                    char fname[8];     /* file name */
  22.                    char ftype[3];     /* file type */
  23.                    char fext;         /* file extent */
  24.                    char filler[3];
  25.                    } name1, name2;
  26.             char crec;                /* current record */
  27.             int rrec;                 /* random record */
  28.             char rovf;                /* random record overflow */
  29.             };
  30.  
  31. #define FCB struct fcb_
  32.  
  33. #define CTLC            '\003'
  34.  
  35. #define BUFSIZE         (16*1024)
  36.  
  37. struct sdr {            char        s_fil1[3];      /* jp instruction */
  38.                         int         s_len;          /* length of module */
  39.                         unsigned    s_plen;         /* length of pgm */
  40.                         struct {    char    s_drive, s_user;
  41.                                     char    s_fname[8];
  42.                                     char    s_ftype[3];
  43.                                }    s_file[9];
  44.            };
  45.  
  46. extern FCB              dfcb_;
  47. extern char             dbuff_[];
  48. extern struct sdr       sdru1, sdru2;
  49.  
  50. struct {                char    maxin;
  51.                         char    insize;
  52.                         char    inline[33];
  53.        }                inbuf = { 32 };
  54.  
  55.  
  56. main()
  57. {
  58.   static struct sdr     *sdru;
  59.   static int            integr;
  60.   static int            i, j;
  61.   static char           *s, *t;
  62.   FCB                   infcb, outfcb, holdfcb;
  63.   char                  buffer[BUFSIZE];
  64.  
  65.   signon();
  66.   integr = ask("\nDo you want SETDRU integrated with the program (y or n)? ");
  67.   sdru = integr ? &sdru2 : &sdru1;
  68.   for (;;)
  69.   {
  70.     if (!askfn("\nTo which program do you want to apply SETDRU? ",
  71.                &infcb))
  72.       return;
  73.     strncpy(sdru->s_file[0].s_fname, infcb.name1.fname, 11);
  74.     sdru->s_file[0].s_drive = dfcb_.name1.drive
  75.                             ? dfcb_.name1.drive : (curdsk() + 1);
  76.     sdru->s_file[0].s_user = '\0';
  77.     if (integr)
  78.     {
  79.       if (open(&infcb) != 0xff)
  80.       {
  81.         filsiz(&infcb);
  82.         sdru->s_plen = 128 * infcb.rrec;
  83.         break;
  84.       }
  85.       ps("Cannot open\n");
  86.     }
  87.     else
  88.     {
  89.       sdru->s_file[0].s_user = askuser();
  90.       break;
  91.     }
  92.   }
  93.  
  94.   if (!askfn("What is the name of the program to be created? ",
  95.              &outfcb))
  96.       return;
  97.   strncpy(&holdfcb, &outfcb, sizeof(FCB));
  98.   strncpy(outfcb.name1.ftype, "$$$", 3);
  99.   delete(&outfcb);
  100.   if (make(&outfcb) == 0xff)
  101.     error("Cannot create file");
  102.  
  103.   for (i = 1; i < 9; ++i)
  104.   { 
  105.     ps("\nFile ");
  106.     pn(i);
  107.     if (!askfn("? ", &dfcb_))
  108.       if (i > 1)
  109.         break;
  110.       else
  111.       {
  112.         i = 0;
  113.         continue;
  114.       }
  115.     strncpy(sdru->s_file[i].s_fname, dfcb_.name1.fname, 11);
  116.     sdru->s_file[i].s_drive = dfcb_.name1.drive
  117.                             ? dfcb_.name1.drive : (curdsk() + 1);
  118.     sdru->s_file[i].s_user = askuser();
  119.   }
  120.  
  121.   for (s = (char *) sdru, i = sdru->s_len; i > 0; s += 128, i -=128)
  122.   {
  123.     setdma(s);
  124.     if (wrseq(&outfcb))
  125.       error("Error writing file.");
  126.   }
  127.   if (integr)
  128.   {
  129.     do
  130.     {
  131.       for (s = buffer, i = BUFSIZE; i > 0; s += 128, i -= 128)
  132.       {
  133.         setdma(s);
  134.         if (j = rdseq(&infcb))
  135.           break;
  136.       }
  137.       for (t = buffer; t < s; t += 128)
  138.       {
  139.         setdma(t);
  140.         if (wrseq(t, &outfcb))
  141.           error("Error writing file.");
  142.       }
  143.     } while(!j);
  144.   }
  145.  
  146.   close(&outfcb);
  147.   delete(&holdfcb);
  148.   strncpy(&outfcb.name2, &holdfcb.name1, sizeof(outfcb.name1));
  149.   outfcb.name2.drive = '\0';
  150.   if (rename(&outfcb) == 0xff)
  151.     error("Cannot rename output file.");
  152.   ps("\nInstallation successful.\n");
  153. }
  154.  
  155. signon()
  156. {
  157.   static char           *msg[] = {
  158.   "Copyright 1983-4  Michael M Rubenstein\n",
  159.   "SETDRU 1.3 -- set drive and user for specified files in a program\n\n",
  160.   "Up to 8 (ambiguous) file names may be specified.\n\n",
  161.   "An input file will match the first matching file name.  If it is\n",
  162.   "ambiguous, the redirection (drive and user) will be used only if the\n",
  163.   "file cannot be found on the drive specified by the program.  If\n",
  164.   "unambiguous, the file will always be redirected.\n\n",
  165.   "An output file will match the first unambiguous (only) matching file\n",
  166.   "name.  It will always be redirected.\n\n",
  167.   "Nonmatching files will not be affected\n\n",
  168.   "May be installed as a separate program which loads the desired program\n",
  169.   "or as a modification to the desired program.\n\n",
  170.   "See SETDRU.DOC for more information.\n\n",
  171.   "Specify drive letters in the normal manner (B:FILE.TYP).  You will be\n",
  172.   "prompted for user number.\n",
  173.   NULL
  174.                                  };
  175.   char                  **s;
  176.  
  177.   for (s = msg; *s != NULL; ++s)
  178.     ps(*s);
  179. }
  180.  
  181. /* ask yes or no                                                            */
  182. ask(s)
  183.   char                  *s;
  184. {
  185.   for (;;)
  186.   {
  187.     ps(s);
  188.     rdbuf(&inbuf);
  189.     wrcon('\n');
  190.     switch (toupper(inbuf.inline[0]))
  191.     {
  192.       case '\r':
  193.       case '\n':
  194.       case 'Y':     return TRUE;
  195.  
  196.       case 'N':     return FALSE;
  197.  
  198.       case CTLC:    exit();
  199.  
  200.     }
  201.   }
  202. }
  203.  
  204. askfn(s, f)
  205.   char                  *s;
  206.   FCB                   *f;
  207. {
  208.   for (;;)
  209.   {
  210.     ps(s);
  211.     rdbuf(&inbuf);
  212.     wrcon('\n');
  213.     if (inbuf.insize == '\0')
  214.       return FALSE;
  215.     inbuf.inline[inbuf.insize] = '\0';
  216.     setfcb(inbuf.inline, f);
  217.     if (f->name1.drive == '\0')
  218.       f->name1.drive = curdsk() + 1;
  219.     if (f->name1.fname[0] != ' ')
  220.       return(TRUE);
  221.   }
  222. }
  223.  
  224. askuser()
  225. {
  226.   static int            n;
  227.  
  228.   for (;;)
  229.   {
  230.     ps("User number? ");
  231.     rdbuf(&inbuf);
  232.     wrcon('\n');
  233.     if (inbuf.insize == 0)
  234.       return getusr();
  235.     inbuf.inline[inbuf.insize] = '\0';
  236.     if ((n = atoi(inbuf.inline)) >=0 && n <= 31)
  237.       return n;
  238.   }
  239. }
  240.  
  241. pn(n)
  242.   int                   n;
  243. {
  244.   if (n > 9)
  245.     pn(n / 10);
  246.   wrcon(n % 10 + '0');
  247. }
  248.  
  249. ps(s)
  250.   char                  *s;
  251. {
  252.   while (*s)
  253.   {
  254.     if (*s == '\n')
  255.       wrcon('\r');
  256.     wrcon(*(s++));
  257.   }
  258. }
  259.    return TRUE;
  260.  
  261.       case '