home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume32 / xbbs / part07 / bbscport.c < prev    next >
C/C++ Source or Header  |  1992-09-08  |  8KB  |  393 lines

  1. /*
  2.     bbscport.c
  3.  
  4. */
  5.  
  6. #include "bbscdef.h"
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <sys/types.h>
  10. #include <sys/locking.h>
  11. extern int no_cntrl_k;
  12. extern int hold_off;
  13. extern int toggle_hold;
  14. extern unsigned int Zsec;
  15.  
  16. #define LASTDATE  " 05/05/89 "
  17.  
  18. #define PGMNAME "BBSCPORT "
  19.  
  20. char portin()        /* get one byte from the port */
  21. {
  22.     char byte;
  23.     int byte0;
  24.     unsigned int ssec, tsec, usec, vsec;
  25.     int this_timer, wsec;
  26.     if(toggle_hold) (void) sys_toggle();
  27.     hold_off = TRUE;
  28.     this_timer = which_timer;
  29.     which_timer = 3;
  30.     ssec = alarm(0);
  31.     tsec = waittime;
  32.     if(tsec > ssec)
  33.         {
  34.         tsec = ssec;
  35.         which_timer = this_timer;
  36.         }
  37.     Zsec = ssec - tsec;
  38.     alarm(tsec);
  39.     byte0=cget();
  40.     byte = (char)byte0;
  41.     switch ( byte0 ) {
  42.     
  43.     case -1:
  44.         fprintf(stderr,"cget() returns -1 -- ABORTING");
  45.         exit(1);
  46.     default:
  47.         usec = alarm(0);
  48.         which_timer = this_timer;
  49.         wsec = ssec - (tsec - usec);
  50.         if(wsec < 2 ) wsec = 2;
  51.         vsec = wsec;
  52.         alarm( vsec );
  53.         hold_off = FALSE;
  54.         if(toggle_hold) (void)sys_toggle();
  55.         return(byte);
  56.     }
  57. }
  58.  
  59. char portin_chat()        /* get one byte from the port */
  60. {
  61.     char byte;
  62.     int byte0;
  63.     byte0=cget_chat();
  64.     byte = (char)byte0;
  65.     switch ( byte0 ) {
  66.     
  67.     case -1:
  68.         fprintf(stderr,"cget() returns -1 -- ABORTING");
  69.         exit(1);
  70.     default:
  71.         return(byte);
  72.     }
  73. }
  74. portsin(buf,max)    /* get a line of input max. chars long */
  75. int max ; char *buf ;
  76.     {
  77.     int cnt, byte ; char bytex ;
  78.     cnt = 0;
  79.     byte = FALSE;
  80.     while (++cnt <= max && byte != '\r')
  81.         {
  82.         while((byte = (int)portin()) < ' ' || byte > '}')
  83.             {
  84.             if( byte == 127) byte = '\b';
  85.             if (byte == '\r') { break ; } /* carriage return */
  86.             if (byte == '\b' && cnt > 1)    /* backspace */
  87.                 {
  88.                 portout(byte);
  89.                 portout(' ');
  90.                 portout(byte);
  91.                 *buf--;    /* backout last char */
  92.                 cnt--;    /* decrement count too */
  93.                 }
  94.             }
  95.         if (byte != '\r')
  96.             {
  97.             *buf++ = byte;
  98.             }
  99.         portout(byte);    /* echo good chars only */
  100.         }
  101.     *buf++    = '\0';            /* tag \0 on end */
  102.     }
  103. portsinz(buf,max)    /* get a line of input max. chars long */
  104. int max ; char *buf ;
  105.     {
  106.     int cnt, byte ; char bytex ;
  107.     cnt = 0;
  108.     byte = FALSE;
  109.     while (++cnt <= max && byte != '\r')
  110.         {
  111.         while((byte = (int)portin()) < ' ' || byte > '}')
  112.             {
  113.             if (byte == 127) byte = '\b';
  114.             if (byte == '\r') { break ; } /* carriage return */
  115.             if (byte == '\b' && cnt > 1)    /* backspace */
  116.                 {
  117.                 portout(byte);
  118.                 portout(' ');
  119.                 portout(byte);
  120.                 *buf--;    /* backout last char */
  121.                 cnt--;    /* decrement count too */
  122.                 }
  123.             }
  124.         if (byte != '\r')
  125.             {
  126.             *buf++ = byte;
  127.             }
  128.         portout('_');    /* echo an underscore  */
  129.         }
  130.     *buf++    = '\0';            /* tag \0 on end */
  131.     }
  132. portsinm(buf,max,buf1)    /* get a line of input max. chars long */
  133. int max;
  134. char *buf, *buf1;
  135.     {
  136.     int cnt, byte ; char bytex ;
  137.     int  new_max;
  138.     cnt = 0;
  139.     new_max = max;
  140.     byte = FALSE;
  141.     while (++cnt <= new_max && byte != '\r')
  142.         {
  143.         while((byte = (int)portin()) < ' ' || byte > '}')
  144.             {
  145.             if (byte == 127) byte = '\b';
  146.             if (byte == '\r') { break ; } /* carriage return */
  147.             if (byte == '\b' && cnt > 1)    /* backspace */
  148.                 {
  149.                 portout(byte);
  150.                 portout(' ');
  151.                 portout(byte);
  152.                 *buf--;    /* backout last char */
  153.                 cnt--;    /* decrement count too */
  154.                 }
  155.             if (byte == '\b' && cnt == 1 && in_the_buffer > 0)
  156.                 {
  157.                 portout(byte);
  158.                 portout(' ');
  159.                 portout(byte);
  160.                 in_the_buffer--;
  161.                 new_max++;
  162.                 buf1[in_the_buffer] = '\0';
  163.                 }
  164.  
  165.             }
  166.         if (byte != '\r')
  167.             {
  168.             *buf++ = byte;
  169.             }
  170.         portout(byte);    /* echo good chars only */
  171.         }
  172.     *buf++    = '\0';            /* tag \0 on end */
  173.     }
  174.  
  175.  
  176. portsin_cmp(buf,max,cmp_str)    /* get a line of input max. chars long */
  177. int max ; char *buf , *cmp_str;
  178.     {
  179.     int cnt, byte ; char bytex ;
  180.     char *result;
  181.     cnt = 0;
  182.     byte = FALSE;
  183.     while (++cnt <= max && byte != '\r')
  184.         {
  185.         while((byte = (int)portin()) < ' ' || byte > '}')
  186.             {
  187.             if (byte == 127) byte = '\b';
  188.             if (byte == '\r') { break ; } /* carriage return */
  189.             if (byte == '\b' && cnt > 1)    /* backspace */
  190.                 {
  191.                 portout(byte);
  192.                 portout(' ');
  193.                 portout(byte);
  194.                 *buf--;    /* backout last char */
  195.                 cnt--;    /* decrement count too */
  196.                 }
  197.             }
  198.         if (byte != '\r')
  199.             {
  200.             if(cnt == 1)
  201.                 {
  202.                 result = strchr(cmp_str,byte);
  203.                 if(result != NULL)        
  204.                     {
  205.                     *buf++ = byte;
  206.                     portout(byte);
  207.                     *buf++ = '\0';
  208.                     return;
  209.                     }
  210.                 }
  211.             *buf++ = byte;
  212.             }
  213.         portout(byte);    /* echo good chars only */
  214.         }
  215.     *buf++    = '\0';            /* tag \0 on end */
  216.     }
  217.  
  218. portout(byte)        /* send one byte to the port */
  219. char byte;        /* return CTL_K for those times want to check */
  220.     {        /* if the person wants to stop sending        */
  221.     char byte0 ;
  222.  
  223.     byte0 = byte ; write(STDOUT,&byte0,1) ; /* send the byte */
  224.     if(if_monitor)
  225.         {
  226.         write(mon_handle,&byte0,1);
  227.         }
  228.     return(OK) ;
  229.     }
  230.   
  231. portout_chat(byte)        /* send one byte to the port */
  232. char byte;        /* return CTL_K for those times want to check */
  233.     {        /* if the person wants to stop sending        */
  234.     char byte0 ;
  235.  
  236.     byte0 = byte ; write(STDOUT,&byte0,1) ; /* send the byte */
  237.     if(if_monitor)
  238.         {
  239.         write(mon_handle,&byte0,1);
  240.         }
  241.     return(OK) ;
  242.     }
  243.   
  244. portsout(string)    /* send a string to the port */
  245. char *string ;
  246.     {
  247.     char byte ;
  248.  
  249.     while (byte = (*string++))
  250.         {
  251.           portout(byte) ;        /* send one byte at a time */
  252.         }
  253.     }
  254.  
  255. portsout_chat(string)    /* send a string to the port */
  256. char *string ;
  257.     {
  258.     char byte ;
  259.  
  260.     while (byte = (*string++))
  261.         {
  262.           portout_chat(byte) ;        /* send one byte at a time */
  263.         }
  264.     }
  265. portlsout(string,len)  /* send a string to the port, pad to length */
  266. char *string ; int len ;
  267.     {
  268.     char byte ;
  269.  
  270.     while (byte = (*string++))
  271.         {
  272.           portout(byte) ;        /* send one byte at a time */
  273.         len-- ;
  274.         }
  275.     while (len > 0) { portout(' ') ; len-- ; } /* pad with spaces */
  276.     }
  277.  
  278. porttype(tbuf)        /* type a file to the port */
  279. FILE    *tbuf ;
  280.     {
  281.     int byte ;
  282.     int xp;
  283.     if(xpert)xp = 10; else xp = 5;
  284.     stop_that = FALSE;    /* reset switch */
  285.     if(!no_cntrl_k) portsout("\r\nType CTL-K to skip this\r\n\n");
  286.     lnctx=1;
  287.     byte = 0;
  288.     while (((byte = getc (tbuf)) != EOF) && (byte != CPMEOF))
  289.         {
  290.         if(byte == '\n')
  291.             {
  292.             portout('\r');
  293.             if(toggle)
  294.             {
  295.                 lnctx++;
  296.                 if ( lnctx == 23 )
  297.                     {
  298.                     portsout(CRLF);
  299.                     if(!no_cntrl_k)
  300.                         portsout("*** Depress a key to continue ( control-k to quit ) ........ ");
  301.                     else
  302.                         portsout("*** Depress a key to continue  ........ ");
  303.                     jnk[0] = portin();
  304.                     if (jnk[0] == CTL_K )
  305.                     {              
  306.                         stop_that = TRUE;
  307.                     }
  308.                     portsout(CRLF);
  309.                     lnctx=1;
  310.                     }
  311.             }
  312.             }
  313.         if ( isprint(byte) == 0  && isspace(byte) == 0 )
  314.         {
  315.             portsout(CRLF);
  316.             portsout(CRLF);
  317.             portsout(CRLF);
  318.             portsout("A non-printable character has been detected!");
  319.             portsout(CRLF);
  320.             portsout("This is probably NOT an ASCII file!");
  321.             portsout(CRLF);
  322.             portsout(CRLF);
  323.             portsout(CRLF);
  324.             stop_that = FALSE;
  325.             return;
  326.         }
  327.         portout(byte);
  328.         if (stop_that)         /* received ctl-K or K */
  329.             {
  330.             portsout(CRLF);
  331.             stop_that = FALSE;    /* reset switch */
  332.             return;            /* nuf's enough */
  333.             }
  334.         }
  335.     if(toggle && !no_cntrl_k && ( lnctx > xp ))
  336.         {
  337.         portsout(CRLF);
  338.         portsout("*** Depress a key to continue ........ ");
  339.         jnk[0] = portin();
  340.         portsout(CRLF);
  341.         }
  342.     }
  343.  
  344. portinit()
  345.     {
  346.         setraw();
  347.     }
  348.     /* set raw mode for this terminal 
  349.     struct sgttyb arg;
  350.     ioctl (STDIN, TIOCGETP, &arg);
  351.     arg.sg_flags |= RAW ;
  352.     arg.sg_flags &= ~ECHO;
  353.     ioctl (STDIN, TIOCSETP, &arg);
  354.     }
  355. */
  356.  
  357. portrst()
  358.     {
  359.         restore();
  360.     }
  361.     /* set raw mode for this terminal 
  362.     struct sgttyb arg;
  363.     ioctl (STDIN, TIOCGETP, &arg);
  364.     arg.sg_flags &= ~RAW;
  365.     arg.sg_flags |= ECHO ;
  366.     ioctl (STDIN, TIOCSETP, &arg);
  367.     }
  368. */
  369.  
  370. char gobble()                /* gobble up any answer */
  371.     {
  372.     int cnt = 0 ;
  373.     while (cnt++ < 20) (void)portin() ;
  374.     }
  375. rewritx()
  376. {
  377.     FILE *scope;
  378.     int fds, result;
  379.     if((scope=fopen(USERS,"r+"))==NULL)
  380.         {
  381.         portsout("\n\rError opening USERS file!\n\r");
  382.         exit(1);
  383.         }
  384.     fds = fileno(scope);
  385.     rewind(scope);
  386.     locking(fds, LK_LOCK, 0L);
  387.     result=fseek(scope, save_d_pos, 0);
  388.     rewrtuser(scope);
  389.     rewind(scope);
  390.     locking(fds, LK_UNLCK, 0L);
  391.     fclose(scope);
  392. }
  393.