home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: DFÜ und Kommunikation / SOS-DFUE.ISO / programm / dos / utility / pccp076 / ramrec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-20  |  5.6 KB  |  301 lines

  1. /* Copyright (C) 1993 Peter Edward Cann */
  2.  
  3. #include<stdio.h>
  4. #include<fcntl.h>
  5. #include<sys\types.h>
  6. #include<sys\stat.h>
  7. #include<stdlib.h>
  8. #include<malloc.h>
  9. #include<signal.h>
  10. #include<bios.h>
  11. #include<dos.h>
  12. #include"port.h"
  13. #include"tperday.h"
  14.  
  15.  
  16. unsigned long tick;
  17.  
  18. void (interrupt far *oldtick)();
  19.  
  20. void interrupt far tickhndl()
  21.     {
  22.     tick++;
  23.     }
  24.  
  25. sendchar(c)
  26.     unsigned char c;
  27.     {
  28.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  29.     outp(basereg, c);
  30.     }
  31.  
  32. #define VBUFSIZ 65408 /* leave random amount of room for alloc overhead */
  33. #define MAXVBUFS 8
  34.  
  35. unsigned char *(bufbuf[MAXVBUFS]);
  36. unsigned vbufi;
  37. int bufbufi, nvbufs; /* n may be < MAX 'cause we alloc dynamically */
  38.  
  39. int putbyte(c)
  40.     unsigned char c;
  41.     {
  42.     if(vbufi>=VBUFSIZ)
  43.         {
  44.         vbufi=0;
  45.         if((++bufbufi)>=nvbufs)
  46.             return(-1);
  47.         }
  48.     bufbuf[bufbufi][vbufi++]=c;
  49.     return(0);
  50.     }
  51.  
  52. int follow;
  53.  
  54. quit()
  55.     {
  56.     sendchar('X'&31);
  57.     while(!(inp(basereg+STATREG)&TXSHMTMASK));
  58.     cleanup(0);
  59.     exit(129);
  60.     }
  61.  
  62. sendstr(str)
  63.     char *str;
  64.     {
  65.     int i;
  66.     for(i=0;str[i]!='\0';++i)
  67.         sendchar(str[i]);
  68.     }
  69.  
  70. int follow;
  71.  
  72. #define NSCANS 3
  73. unsigned char scans[NSCANS][16]={"CONNECT\r\n", "ERROR\r\n", "VCON\r\n"};
  74. #define SCANTIMEOUT 16
  75.  
  76. int scan()
  77.     {
  78.     time_t timestamp;
  79.     int i, j, scani[NSCANS];
  80.     timestamp=time(NULL);
  81.     for(i=0;i<NSCANS;++i)
  82.         scani[i]=0;
  83.     while(1)
  84.         {
  85.         while(follow==index)
  86.             {
  87.             if(kbhit())
  88.                 getch();
  89.             if((time(NULL)-timestamp)>SCANTIMEOUT)
  90.                 {
  91.                 for(j=0;j<NSCANS;j++)
  92.                     scani[j]=0;
  93.                 return(-1);
  94.                 }
  95.             }
  96.         putch(buf[follow]);
  97.         for(i=0;i<NSCANS;i++)
  98.             if(scans[i][scani[i]]==buf[follow])
  99.                 {
  100.                 scani[i]++;
  101.                 if(scans[i][scani[i]]=='\0')
  102.                     {
  103.                     for(j=0;j<NSCANS;j++)
  104.                         scani[j]=0;
  105.                     follow++;
  106.                     follow%=TBUFSIZ;
  107.                     return(i);
  108.                     }
  109.                 }
  110.             else
  111.                 scani[i]=0;
  112.         follow++;
  113.         follow%=TBUFSIZ;
  114.         }
  115.     }
  116.  
  117. main(argc, argv)
  118.     int argc;
  119.     char **argv;
  120.     {
  121.     int dleflag, i, fd, tflag;
  122.     unsigned char c;
  123.     long start, stop;
  124.     if((argc!=4)&&(argc!=6))
  125.         {
  126.         printf("USAGE: ramrec <comnum> <speed> <file> [<start> <stop>]\n");
  127.         printf("\n<start> is number of ticks to wait after a DTMF report before starting.\n");
  128.         printf("<stop> is number of ticks after the tone report to end.\n");
  129.         printf("\nNormal exit code is 128.\n");
  130.         exit(140);
  131.         }
  132.     if((fd=open(argv[3], O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE))==-1)
  133.         {
  134.         printf("Unable to open voice file %s for write.\n", argv[3]);
  135.         exit(150);
  136.         }
  137.     for(nvbufs=0;nvbufs<MAXVBUFS;nvbufs++)
  138.         if((bufbuf[nvbufs]=malloc(VBUFSIZ))==NULL)
  139.             break;
  140.     printf("Allocated %d sub-buffers.\n", nvbufs);
  141.     if(!nvbufs)
  142.         exit(246);
  143.     if(argc==6)
  144.         {
  145.         tflag=1;
  146.         start=atol(argv[4]);
  147.         stop=atol(argv[5]);
  148.         }
  149.     else
  150.         tflag=0;
  151.     vbufi=bufbufi=0;
  152.     comnum=atoi(argv[1])-1;
  153.     speed=atoi(argv[2]);
  154.     databits='8';
  155.     parity='n';
  156.     stopbits='1';
  157.     setport();
  158.     readset();
  159.     follow=index=0;
  160.     printf("Recording voice. Any char to end; Control-C to nuke.\n");
  161.     oldtick=_dos_getvect(0x1c);
  162.     signal(SIGINT, quit);
  163.     _dos_setvect(0x1c, tickhndl);
  164.     setup();
  165.     if(tflag)
  166.         {
  167.         while(1)
  168.             {
  169.             while(index==follow)
  170.                 if(_bios_keybrd(_KEYBRD_READY))
  171.                     {
  172.                     _bios_keybrd(_KEYBRD_READ);
  173.                     cleanup(0);
  174.                     _dos_setvect(0x1c, oldtick);
  175.                     exit(0);
  176.                     }
  177.             c=buf[follow++];
  178.             if(follow>=TBUFSIZ)
  179.                 follow=0;
  180.             if(((c>='0')&&(c<='9'))||(c=='*')||(c=='#'))
  181.                 break;
  182.             }
  183.         tick=0L;
  184.         while(1)
  185.             {
  186.             if(_bios_keybrd(_KEYBRD_READY))
  187.                 {
  188.                 _bios_keybrd(_KEYBRD_READ);
  189.                 cleanup(0);
  190.                 _dos_setvect(0x1c, oldtick);
  191.                 exit(0);
  192.                 }
  193.             if(tick>=start)
  194.                 break;
  195.             }
  196.         }
  197.     sendstr("AT#VRX\r");
  198.     switch(scan())
  199.         {
  200.         case -1:
  201.             cleanup(0);
  202.             printf("Timeout waiting for CONNECT.\n");
  203.             _dos_setvect(0x1c, oldtick);
  204.             exit(130);
  205.             break;
  206.         case 0:
  207.             break;
  208.         case 1:
  209.             cleanup(0);
  210.             printf("Modem rejected play command.\n");
  211.             _dos_setvect(0x1c, oldtick);
  212.             exit(131);
  213.             break;
  214.         default:
  215.             cleanup(0);
  216.             printf("Impossible error; unrecognized scan() return.\n");
  217.             _dos_setvect(0x1c, oldtick);
  218.             exit(132);
  219.             break;
  220.         }
  221.     dleflag=0;
  222.     while(1)
  223.         {
  224.         while(follow==index)
  225.             {
  226.             if(_bios_keybrd(_KEYBRD_READY))
  227.                 {
  228.                 sendchar('\r'); /*kick modem & cross fingers*/
  229.                 if((_bios_keybrd(_KEYBRD_READ)&0xff)==0x03)
  230.                     quit();
  231.                 }
  232.             if(tflag)
  233.                 {
  234.                 if(tick>stop)
  235.                     {
  236.                     sendchar('\r');
  237.                     /* Modem doesn't like huge mess */
  238.                     /* of key abort characters.     */
  239.                     if(tick>=36)
  240.                         tick-=36;
  241.                     else
  242.                         tick=0;
  243.                     }
  244.                 }
  245.             }
  246.         c=buf[follow++];
  247.         if(follow>=TBUFSIZ)
  248.             follow=0;
  249.         if(c==0x10)
  250.             {
  251.             if(dleflag)
  252.                 {
  253.                 putbyte(0x10);
  254.                 putbyte(0x10);
  255.                 dleflag=0;
  256.                 }
  257.             else
  258.                 dleflag=1;
  259.             }
  260.         else if(!dleflag)
  261.             {
  262.             /* Open-coded putbute() */
  263.             if(vbufi>=VBUFSIZ)
  264.                 {
  265.                 vbufi=0;
  266.                 if((++bufbufi)>=nvbufs)
  267.                     return(-1);
  268.                 }
  269.             bufbuf[bufbufi][vbufi++]=c;
  270.             }
  271.         else if(c==0x03)
  272.             {
  273.             putbyte(0x10);
  274.             putbyte(0x03);
  275.             scan(); /* Wait for VCON or whatever */
  276.             cleanup(0);
  277.             for(i=0;i<bufbufi;i++)
  278.                 if(((unsigned)write(fd, bufbuf[i], VBUFSIZ))!=((unsigned)VBUFSIZ))
  279.                     {
  280.                     printf("Disk buffer flush error.\n");
  281.                     _dos_setvect(0x1c, oldtick);
  282.                     exit(160);
  283.                     }
  284.             if(write(fd, bufbuf[bufbufi], vbufi)!=vbufi)
  285.                 {
  286.                 printf("Disk buffer flush error.\n");
  287.                 _dos_setvect(0x1c, oldtick);
  288.                 exit(160);
  289.                 }
  290.             _dos_setvect(0x1c, oldtick);
  291.             exit(128);
  292.             }
  293.         else
  294.             dleflag=0; /* ignore shielded codes */
  295.         }
  296.     printf("Programming error; fell through endless while loop!\n");
  297.     cleanup(0);
  298.     _dos_setvect(0x1c, oldtick);
  299.     exit(250);
  300.     }
  301.