home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / COMM / MISC / PCCP019.ZIP / HOST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-07  |  11.3 KB  |  569 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include<process.h>
  13.  
  14. #define DLLSBREG 0
  15. #define DLMSBREG 1
  16. #define INTCTLREG 1
  17. #define INTIDREG 2
  18. #define LCTLREG 3
  19. #define MCTLREG 4
  20. #define STATREG 5
  21. #define MSTATREG 6
  22.  
  23. #define DCDMASK 0x80
  24. #define CTSMASK 0x10
  25. #define TXMTMASK 0x20
  26. #define RXRDYMASK 0x01
  27.  
  28. #define INTACK 0x20
  29.  
  30. #define DB7 0x02
  31. #define DB8 0x03
  32. #define STOP2 0x04
  33. #define PARITYEN 0x08
  34. #define PARITYEVEN 0x10
  35. #define DLAB 0x80
  36.  
  37. #define INTBASE1 0x20
  38. #define INTMASK1 0x21
  39. #define INTBASE2 0xa0
  40. #define INTMASK2 0xa1
  41.  
  42. #define TBUFSIZ 256
  43.  
  44. #define NAK 21
  45. #define ACK 6
  46. #define SOH 1
  47. #define EOT 4
  48.  
  49. int index, basereg;
  50. unsigned char buf[TBUFSIZ];
  51. unsigned char diffintmask, irqnum;
  52. void (interrupt far *oldvect)();
  53.  
  54. void interrupt far inthndl(_es, _ds, _di, _si, _bp, _sp,
  55.               _bx, _dx, _cx, _ax, _ip, _cs, _flags)
  56.     unsigned _es, _ds, _di, _si, _bp, _sp;
  57.     unsigned _bx, _dx, _cx, _ax, _ip, _cs, _flags;
  58.     {
  59.     if(inp(basereg+STATREG)&RXRDYMASK)
  60.         {
  61.         buf[index++]=inp(basereg)&0xff;
  62.         index=index%TBUFSIZ;
  63.         }
  64.     outp(INTBASE1, INTACK);
  65.     outp(INTBASE2, INTACK);
  66.     }
  67.  
  68. unsigned intnum;
  69. unsigned char oldintmask;
  70. unsigned char oldlctl, olddllsb, olddlmsb, oldintctl, oldmctl;
  71.  
  72. cleanup()
  73.     {
  74.     if(intnum==10)
  75.         outp(INTMASK2, oldintmask);
  76.     else
  77.         outp(INTMASK1, oldintmask);
  78.     outp(basereg+LCTLREG, DLAB);
  79.     outp(basereg+DLLSBREG, olddllsb);
  80.     outp(basereg+DLMSBREG, olddlmsb);
  81.     outp(basereg+LCTLREG, oldlctl);
  82.     outp(basereg+INTCTLREG, oldintctl);
  83.     outp(basereg+MCTLREG, oldmctl);
  84.     _dos_setvect(intnum, oldvect);
  85.     }
  86.  
  87. sendchar(c)
  88.     unsigned char c;
  89.     {
  90.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)))
  91.         if(kbhit())
  92.             getch();
  93.     outp(basereg, c);
  94.     }
  95.  
  96. int follow;
  97.  
  98. quit()
  99.     {
  100.     cleanup();
  101.     exit(99);
  102.     }
  103.  
  104. sendstr(str)
  105.     char *str;
  106.     {
  107.     int i;
  108.     for(i=0;str[i]!='\0';++i)
  109.         if(str[i]=='\n')
  110.             {
  111.             sendchar('\r');
  112.             sendchar('\n');
  113.             }
  114.         else
  115.             sendchar(str[i]);
  116.     }
  117.  
  118. portgets(str)
  119.     char *str;
  120.     {
  121.     int i;
  122.     i=0;
  123.     while(i<256)
  124.         {
  125.         while(follow==index)
  126.             {
  127.             if(!(inp(basereg+MSTATREG)&DCDMASK))
  128.                 return(-1);
  129.             if(kbhit())
  130.                 getch();
  131.             }
  132.         str[i]=buf[follow++];
  133.         if(str[i]=='\b')
  134.             if(i>0)
  135.                 {
  136.                 i-=2;
  137.                 sendchar('\b');
  138.                 sendchar(' ');
  139.                 sendchar('\b');
  140.                 }
  141.             else
  142.                 {
  143.                 i--;
  144.                 sendchar(0x07);
  145.                 }
  146.         else
  147.             sendchar(str[i]);
  148.         follow%=TBUFSIZ;
  149.         if((str[i]=='\r')||(str[i]=='\n'))
  150.             {
  151.             sendchar('\r');
  152.             sendchar('\n');
  153.             str[i]='\0';
  154.             break;
  155.             }
  156.         i++;
  157.         }
  158.     str[256]='\0';
  159.     return(0);
  160.     }
  161.         
  162. pwportgets(str)
  163.     char *str;
  164.     {
  165.     int i;
  166.     i=0;
  167.     while(i<256)
  168.         {
  169.         while(follow==index)
  170.             {
  171.             if(!(inp(basereg+MSTATREG)&DCDMASK))
  172.                 return(-1);
  173.             if(kbhit())
  174.                 getch();
  175.             }
  176.         str[i]=buf[follow++];
  177.         if(str[i]=='\b')
  178.             if(i>0)
  179.                 {
  180.                 i-=2;
  181.                 sendchar('\b');
  182.                 sendchar(' ');
  183.                 sendchar('\b');
  184.                 }
  185.             else
  186.                 {
  187.                 i--;
  188.                 sendchar(0x07);
  189.                 }
  190.         else
  191.             sendchar('.');
  192.         follow%=TBUFSIZ;
  193.         if((str[i]=='\r')||(str[i]=='\n'))
  194.             {
  195.             sendchar('\r');
  196.             sendchar('\n');
  197.             str[i]='\0';
  198.             break;
  199.             }
  200.         i++;
  201.         }
  202.     str[256]='\0';
  203.     return(0);
  204.     }
  205.  
  206. unsigned char newintmask, lctl, dlmsb, dllsb;
  207.  
  208. setup()
  209.     {
  210.     outp(basereg+LCTLREG, DLAB);
  211.     olddllsb=inp(basereg+DLLSBREG);
  212.     olddlmsb=inp(basereg+DLMSBREG);
  213.     outp(basereg+DLLSBREG, dllsb);
  214.     outp(basereg+DLMSBREG, dlmsb);
  215.     oldlctl=inp(basereg+LCTLREG);
  216.     outp(basereg+LCTLREG, lctl);
  217.     _dos_setvect(intnum, inthndl);
  218.     oldintctl=inp(basereg+INTCTLREG);
  219.     outp(basereg+INTCTLREG, 0x00);
  220.     oldmctl=inp(basereg+MCTLREG);
  221.     outp(basereg+MCTLREG, 0x0b);
  222.     newintmask=diffintmask;
  223.     newintmask&=oldintmask;
  224.     if(intnum==10)
  225.         outp(INTMASK2, newintmask);
  226.     else
  227.         outp(INTMASK1, newintmask);
  228.     outp(INTBASE1, INTACK); /* Clean up leftovers */
  229.     outp(INTBASE2, INTACK);
  230.     outp(basereg+INTCTLREG, 0x01);
  231.     outp(INTBASE1, INTACK); /* What a zoo! */
  232.     outp(INTBASE2, INTACK);
  233.     }
  234.  
  235. main(argc, argv)
  236.     int argc;
  237.     char **argv;
  238.     {
  239.     long timestamp;
  240.     int i, j, outfd, ok, c, run, result;
  241.     unsigned speed;
  242.     int comnum;
  243.     char str[256];
  244.     index=follow=0;
  245.     lctl=0;
  246.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  247.     if(!strcmp(getenv("REMOTE"), "YES"))
  248.         {
  249.         printf("You appear to be already logged in remotely, judging by the environment\n");
  250.         printf("variable REMOTE, so this is probably a very bad idea.\n");
  251.         printf("Are you sure you want to run HOST? (y or n) --> ");
  252.         if(getchar()!='y') /* Note getchar() and not getch()! */
  253.             {
  254.             printf("I didn't think so!\n");
  255.             exit(99);
  256.             }
  257.         else
  258.             printf("OK, you're the boss!");
  259.         }
  260.     if(argc!=4)
  261.         {
  262.         printf("USAGE: host <comnum> <bps> <password>\n");
  263.         exit(1);
  264.         }
  265.     printf("Control-C to Exit.\n");
  266.     comnum=atoi(argv[1])-1;
  267.     newintmask=0;
  268.     switch(comnum)
  269.         {
  270.         case 0:
  271.             irqnum=4;
  272.             diffintmask=0xff&~0x10;
  273.             basereg=0x3f8;
  274.             break;
  275.         case 1:
  276.             irqnum=3;
  277.             diffintmask=0xff&~0x08;
  278.             basereg=0x2f8;
  279.             break;
  280.         case 2:
  281.             irqnum=4;
  282.             diffintmask=0xff&~0x10;
  283.             basereg=0x3e8;
  284.             break;
  285.         case 3:
  286.             irqnum=3;
  287.             diffintmask=0xff&~0x08;
  288.             basereg=0x2e8;
  289.             break;
  290.         case 4:
  291.             irqnum=2;
  292.             diffintmask=0xff&~0x02;
  293.             basereg=0x3e8;
  294.             break;
  295.         case 5:
  296.             irqnum=2;
  297.             diffintmask=0xff&~0x02;
  298.             basereg=0x2e8;
  299.             break;
  300.         case 6:
  301.             irqnum=5;
  302.             diffintmask=0xff&~0x20;
  303.             basereg=0x3e8;
  304.             break;
  305.         case 7:
  306.             irqnum=5;
  307.             diffintmask=0xff&~0x20;
  308.             basereg=0x2e8;
  309.             break;
  310.         default:
  311.             printf("Bad port choice.\n");
  312.             exit(4);
  313.         }
  314.     intnum=irqnum+8;
  315.     speed=atoi(argv[2]);
  316.     switch(speed)
  317.         {
  318.         case 300:
  319.             dlmsb=0;
  320.             dllsb=0xc0;
  321.             break;
  322.         case 1200:
  323.             dlmsb=0;
  324.             dllsb=0x60;
  325.             break;
  326.         case 2400:
  327.             dlmsb=0;
  328.             dllsb=0x30;
  329.             break;
  330.         case 9600:
  331.             dlmsb=0;
  332.             dllsb=0x0c;
  333.             break;
  334.         case 19200:
  335.             dlmsb=0;
  336.             dllsb=0x06;
  337.             break;
  338.         case 38400:
  339.             dlmsb=0;
  340.             dllsb=0x03;
  341.             break;
  342.         case 57600:
  343.             dlmsb=0;
  344.             dllsb=0x02;
  345.             break;
  346.         default:
  347.             printf("Bad speed.\n");
  348.             exit(5);
  349.         }
  350.     lctl|=DB8;
  351.     oldvect=_dos_getvect(intnum);
  352.     oldintmask=(intnum==10)?inp(INTMASK2):inp(INTMASK1);
  353.     signal(SIGINT, quit);
  354.     outp(basereg+MCTLREG, 0x03); /* Set DTR, RTS, Int. En. */
  355.     while(1)
  356.         {
  357.         printf("Awaiting carrier.\n");
  358.         while(!(inp(basereg+MSTATREG)&DCDMASK))
  359.             if(kbhit())
  360.                 getch();
  361.         printf("Carrier detected.\n");
  362.         setup();
  363.         timestamp=time(NULL);
  364.         while((time(NULL)-timestamp)<3);
  365.         follow=index;
  366.         for(i=0;i<3;++i)
  367.             {
  368.             sprintf(str, "Password? --> ");
  369.             sendstr(str);
  370.             if(pwportgets(str)==-1)
  371.                 {
  372.                 run=0;
  373.                 break;
  374.                 }
  375.             else if(!strcmp(str, argv[3]))
  376.                 {
  377.                 run=1;
  378.                 break;
  379.                 }
  380.             else
  381.                 run=0;
  382.             }
  383.         while(run)
  384.             {
  385.             follow=index; /* flush input */
  386.             sendstr("\n\n     DOWNLOAD:  (1) Xmodem   (2) Xmodem CRC   (3) Xmodem CRC 1K\n\n");
  387.             sendstr("       UPLOAD:  (4) Xmodem   (5) Xmodem CRC 1K Optional\n\n");
  388.             sendstr("          (s) Shell        (q) Quit        (^R) Reload HOST\n\n             ---> ");
  389.             while(index==follow)
  390.                 {
  391.                 if(!(inp(basereg+MSTATREG)&DCDMASK))
  392.                     {
  393.                     run=0;
  394.                     break;
  395.                     }
  396.                 if(kbhit())
  397.                     getch();
  398.                 }
  399.             if(!run)
  400.                 break;
  401.             c=buf[follow++];
  402.             follow%=TBUFSIZ;
  403.             switch(c)
  404.                 {
  405.                 case 18:
  406.                     sendstr("Reload HOST\n");
  407.                     cleanup();
  408.                     execlp("host", "host", argv[1], argv[2], argv[3], NULL);
  409.                     break;
  410.                 case 'q':
  411.                 case 'Q':
  412.                     sendstr("Quit\n");
  413.                     run=0;
  414.                     break;
  415.                 case 's':
  416.                 case 'S':
  417.                     sendstr("Shell\n");
  418.                     printf("Entering command.com remote shell.\n");
  419.                     sprintf(str, "COM%d", comnum+1);
  420.                     putenv("PROMPT=REMOTE>$p$g");
  421.                     putenv("REMOTE=YES");
  422.                     spawnlp(P_WAIT, "c:\\command.com", "command.com", str, "/e:01024", NULL);
  423.                     putenv("REMOTE=NO");
  424.                     setup();
  425.                     break;
  426.                 case '1':
  427.                     sendstr("Xmodem Download.\nSource file pathname? (Blank to cancel)\n --> ");
  428.                     if((result=portgets(str))==-1)
  429.                         {
  430.                         run=0;
  431.                         break;
  432.                         }
  433.                     if(!strlen(str))
  434.                         break;
  435.                     result=spawnlp(P_WAIT, "xmodems", "xmodems", argv[1], argv[2], "1", str, NULL);
  436.                     setup();
  437.                     sendstr("Press any key to continue: --> ");
  438.                     while(follow==index)
  439.                         {
  440.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  441.                             {
  442.                             run=0;
  443.                             break;
  444.                             }
  445.                         if(kbhit())
  446.                             getch();
  447.                         }
  448.                     follow=index;
  449.                     sprintf(str, "Exit code = %d.\n", result);
  450.                     sendstr(str);
  451.                     break;
  452.                 case '2':
  453.                     sendstr("Xmodem CRC Download.\nSource file pathname? (Blank to cancel)\n --> ");
  454.                     if((result=portgets(str))==-1)
  455.                         {
  456.                         run=0;
  457.                         break;
  458.                         }
  459.                     if(!strlen(str))
  460.                         break;
  461.                     result=spawnlp(P_WAIT, "xmcrcs", "xmcrcs", argv[1], argv[2], "1", str, NULL);
  462.                     setup();
  463.                     sendstr("Press any key to continue: --> ");
  464.                     while(follow==index)
  465.                         {
  466.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  467.                             {
  468.                             run=0;
  469.                             break;
  470.                             }
  471.                         if(kbhit())
  472.                             getch();
  473.                         }
  474.                     follow=index;
  475.                     sprintf(str, "Exit code = %d.\n", result);
  476.                     sendstr(str);
  477.                     break;
  478.                 case '3':
  479.                     sendstr("Xmodem CRC 1k Download.\nSource file pathname? (Blank to cancel)\n --> ");
  480.                     if((result=portgets(str))==-1)
  481.                         {
  482.                         run=0;
  483.                         break;
  484.                         }
  485.                     if(!strlen(str))
  486.                         break;
  487.                     result=spawnlp(P_WAIT, "xmcrc1ks", "xmcrc1ks", argv[1], argv[2], "1", str, NULL);
  488.                     setup();
  489.                     sendstr("Press any key to continue: --> ");
  490.                     while(follow==index)
  491.                         {
  492.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  493.                             {
  494.                             run=0;
  495.                             break;
  496.                             }
  497.                         if(kbhit())
  498.                             getch();
  499.                         }
  500.                     follow=index;
  501.                     sprintf(str, "Exit code = %d.\n", result);
  502.                     sendstr(str);
  503.                     break;
  504.                 case '4':
  505.                     sendstr("Xmodem Upload.\nTarget file pathname? (Blank to cancel)\n --> ");
  506.                     if((result=portgets(str))==-1)
  507.                         {
  508.                         run=0;
  509.                         break;
  510.                         }
  511.                     if(!strlen(str))
  512.                         break;
  513.                     result=spawnlp(P_WAIT, "xmodemr", "xmodemr", argv[1], argv[2], "1", str, NULL);
  514.                     setup();
  515.                     sendstr("Press any key to continue: --> ");
  516.                     while(follow==index)
  517.                         {
  518.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  519.                             {
  520.                             run=0;
  521.                             break;
  522.                             }
  523.                         if(kbhit())
  524.                             getch();
  525.                         }
  526.                     follow=index;
  527.                     sprintf(str, "Exit code = %d.\n", result);
  528.                     sendstr(str);
  529.                     break;
  530.                 case '5':
  531.                     sendstr("Xmodem CRC Upload, 1k Optional.\nTarget file pathname? (Blank to cancel)\n --> ");
  532.                     if((result=portgets(str))==-1)
  533.                         {
  534.                         run=0;
  535.                         break;
  536.                         }
  537.                     if(!strlen(str))
  538.                         break;
  539.                     result=spawnlp(P_WAIT, "xmcrc1kr", "xmcrc1kr", argv[1], argv[2], "1", str, NULL);
  540.                     setup();
  541.                     sendstr("Press any key to continue: --> ");
  542.                     while(follow==index)
  543.                         {
  544.                         if(!(inp(basereg+MSTATREG)&DCDMASK))
  545.                             {
  546.                             run=0;
  547.                             break;
  548.                             }
  549.                         if(kbhit())
  550.                             getch();
  551.                         }
  552.                     follow=index;
  553.                     sprintf(str, "Exit code = %d.\n", result);
  554.                     sendstr(str);
  555.                     break;
  556.                 }
  557.             }
  558.         printf("Dropping DTR for about 5 seconds.\n");
  559.         /* Drop DTR to hang up */
  560.         outp(basereg+MCTLREG, 0x08);
  561.         timestamp=time(NULL);
  562.         while(time(NULL)-timestamp<5)
  563.             if(kbhit())
  564.                 getch();
  565.         outp(basereg+MCTLREG, 0x0b);
  566.         cleanup();
  567.         }
  568.     }
  569.