home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 September / Simtel20_Sept92.cdr / msdos / sysutl / async_ce.arc / TERM.C < prev   
C/C++ Source or Header  |  1987-08-31  |  8KB  |  243 lines

  1. /*    FILE:    TERM
  2.  
  3.     Program to set all the settable features of the async deivce
  4.     driver on an MSDOS system.
  5. ;   (c) Copyright 1984 by The Computer Entomologist
  6. ;
  7. ;    Permission is hearby granted to use or distribute this software
  8. ;without any restrictions.  You may make copies for yourself or your
  9. ;friends. You may include it in any hardware or software product that you
  10. ;sell for profit.
  11. ;
  12. ;    This software is distributed as is, and is not guaranteed to work
  13. ;on any particular hardware/software configuration.  Furthermore, no 
  14. ;liability is granted with this software: the user takes responcibility for
  15. ;any damage this software may do to his system.
  16. ;
  17. ;    Nasy notices aside, if you have any questions about this software, you
  18. ;can reach me at the address below.  If you impliment any new features or
  19. ;find (and fix!) any bugs, I would be happy to hear from you.
  20. ;
  21. ;    Mike Higgins
  22. ;    The Computer Entomologist
  23. ;    P.O. Box 197
  24. ;    Duncans Mills, CA 95430
  25.  
  26.     **WARNING****
  27.     Not all of the features below are implimented yet in the driver
  28.     itself.  Do not expect them to work unless you see the code in
  29.     the driver first.  In particular, input data throttling and
  30.     output tab conversions are not implimented yet.
  31. */
  32. char    *innam[]={"dtr is input data throttle signal\n",
  33.     "rts is input data throttle signal\n",
  34.     "inxon -- xon/xoff is used to throttle input data\n",
  35.     "bell is echoed on input buffer full\n",
  36.     "half duplex -- input chars are echoed when read\n",
  37.     "flush sequence is recognized (control-x)\n",
  38.     "redraw sequence is recognized (control-r)\n",
  39.     "delete line sequence is recognized (control-u)\n",
  40.     "skip output sequence is recognized (contril-o)\n",
  41.     "errstat -- line errors produce MSDOS error status returns\n",
  42.     "errcodes -- line errors return codes with parity bit on\n",
  43.     "strip parity on input characters\n",
  44.     "upper case all input characters\n",
  45.     "control-c passes through input buffer on non-destructive read\n",
  46.     "reboot sequence is recognized (control-c control-r control-b\n",0};
  47. int    inval[]={2,4,8,0x10,0x20,0x40,0x80,
  48.         0x100,0x200,0x400,0x800,0x1000,0x2000,
  49.         0x4000,0x8000,0};
  50. char    *outnam[]={"dsr is output data throttle signal\n",
  51.     "cts is output data throttle signal\n",
  52.     "xonxoff throttles output data\n",
  53.     "offcts -- cts is ofline signal\n",
  54.     "offcd -- carrier detect is offline signal\n",
  55.     "offdsr -- dsr is offline signal\n",
  56.     "outstrip -- strip parity on all output characters\n",
  57.     "outupper -- convert output chars to upper case\n",
  58.     "tabs are converted to spaces on output\n",0};
  59. int    outval[]={2,4,8,0x10,0x20,0x40,0x1000,0x2000,0x4000,0};
  60. char    defname[]="ASYNC1";
  61. struct    asy_tab        /*structure of the return from the ioctl to async*/
  62. {
  63.     char    lctrl;        /*line control byte*/
  64.     char    mctrl;        /*modem control*/
  65.     char    lstat;        /*line status*/
  66.     char    mstat;        /*modem status*/
  67.     int    inspec;        /*input special processing bits*/
  68.     int    outspec;    /*output special processing bits*/
  69.     int    baud;        /*baud rate*/
  70. }    buf;
  71. int    i,j,l;        /*pointers and loop counters*/
  72. int    change=0;    /*flag set to 1 if any bits are changed*/
  73. int    flag=0;        /*flag set to 1 if option is recognized*/
  74. int    nope=0;        /*flag set to 1 if option is negated*/
  75.  
  76. char    *dev=defname;    /*pointer to device name*/
  77. char    *ptr;        /*general pointer to strings*/
  78.  
  79. main(argc,argv)
  80. int    argc;
  81. char    *argv[];
  82. {
  83.     int    baud=0;
  84.     int    setin=0;    /*bits to set in input mask*/
  85.     int    clrin=0;    /*bits to clear in input mask*/
  86.     int    setout=0;    /*bits to set in output mask*/
  87.     int    clrout=0;    /*bits to clear in output mask*/
  88.     j=0;
  89.     i=0;
  90.  
  91.     while(++i < argc)        /*for all input arguments,*/
  92.     {
  93.     nope=0;
  94.     flag=0;        /*initialize flags*/
  95.  
  96.     dolower(argv[i]);    /*convert them to upper case*/
  97.     l=strlen(argv[i]);
  98.  
  99.     if (argv[i][l-1] == ':')    /*is this a device name?*/
  100.     {
  101.         dev=argv[i];    /*then remember it's address*/
  102.         dev[l-1]=0;        /*and get rid of that colon!*/
  103.         flag=1;        /*mark this as a legal command*/
  104.     }
  105.  
  106.     if (strncmp(argv[i],"spe",3) == 0)    /*is this a baud change?*/
  107.     {
  108.         ptr=argv[i];
  109.         while (*ptr != 0)        /*scan forward looking for =*/
  110.         {
  111.             if (*ptr++ == '=')
  112.             {
  113.             if (sscanf(ptr,"%d",&baud) == 1)
  114.             {
  115.             change=1;    /*set flag forcing ioctlwrite*/
  116.             flag=1;        /*flag this as legal command*/
  117.             buf.baud=baud;    /*store the new baud rate*/
  118.             }
  119.             break;
  120.         }
  121.         }
  122.     }
  123.  
  124.     if (strncmp(argv[i],"help",l) == 0)    /*asking for help?*/
  125.     {
  126.         printf("This command accepts many options to control the\n");
  127.         printf("behavior of the ASYNC drivers.\n");
  128.         printf("Any option terminated with : is assumed to be the\n");
  129.         printf("name of the ASYNC drive to operate on. ASYNC1: is default.\n");
  130.         printf("The option SPEED=nnn will set the baud rate of the driver\n");
  131.         printf("to nnn.  Illegal baud rates are ignored.\n");
  132.         printf("All the following ASYNC driver options can be set by\n");
  133.         printf("typing the first few letters of the option.  To turn\n");
  134.         printf("an option off, prefix it with NO.\n");
  135.         j=0;        /*loop for all the output names*/
  136.         while (outnam[j] != 0)
  137.         printf(outnam[j++]);
  138.         j=0;        /*loop for all the input names*/
  139.         while (innam[j] != 0)
  140.         printf(innam[j++]);
  141.         flag=1;    /*mark this a legal flag*/
  142.     }
  143.     if (strncmp(argv[i],"no",2) == 0)    /*is this a negation?*/
  144.     {
  145.         argv[i] += 2;            /*skip over the "no"*/
  146.         l -= 2;                /*take "no" off the length*/
  147.         nope=1;                /*set flag*/
  148.     }
  149.  
  150.     if (flag == 0)
  151.     {
  152.         j=0;        /*loop for all the input names*/
  153.         while (innam[j] != 0)
  154.         {
  155.         if (strncmp(argv[i],innam[j],l) == 0)
  156.         {
  157.             flag=1;    /*recognise this command*/
  158.             if (nope == 0)
  159.             setin |= inval[j];    /*set this bit*/
  160.             else
  161.             clrin |= inval[j];    /*or clear it*/
  162.             change=1;            /*set the changed bit*/
  163.             break;
  164.         }
  165.         j += 1;
  166.         }
  167.     }
  168.  
  169.     if (flag == 0)
  170.     {
  171.         j=0;        /*loop for all the output names*/
  172.         while (outnam[j] != 0)
  173.         {
  174.         if (strncmp(argv[i],outnam[j],l) == 0)
  175.         {
  176.             flag=1;    /*i recognise this command,*/
  177.             if (nope == 0)    /*so set the bit that this option*/
  178.             setout |= outval[j];    /*corresponds to*/
  179.             else
  180.             clrout |= outval[j];    /*or clear it*/
  181.             change=1;
  182.             break;
  183.         }
  184.         j += 1;
  185.         }
  186.     }
  187.     if (flag == 0)        /*did anybody clame this command?*/
  188.         printf("Illegal option: %s\n",argv[i]);
  189.  
  190.     }
  191.  
  192.     ioctl(dev,&buf,10,2);    /*call the assembly ioctl for read*/
  193.  
  194.     if (change != 0)        /*were you changing any of the options?*/
  195.     {
  196.     if (baud != 0)
  197.         buf.baud=baud;
  198.     buf.inspec |= setin;    /*set any requested bits*/
  199.     buf.outspec |= setout;
  200.     buf.inspec &= ~clrin;    /*clear any requested bits*/
  201.     buf.outspec &= ~clrout;
  202.     ioctl(dev,&buf,10,3);    /*request the ioctl write*/
  203.     ioctl(dev,&buf,10,2);    /*and read it back to doublecheck*/
  204.     }
  205.     printf("status of device %s:    Baud rate: %d\n",dev,buf.baud);
  206. /*    printf("Line control: %x    Modem control: %x\n",buf.lctrl,buf.mctrl);
  207.     printf("Line status:  %x    Modem status:  %x\n",buf.lstat,buf.mstat);
  208. */
  209.     printf("    >>>>Special treatment on input:\n");
  210.     outstr(buf.inspec,inval,innam);    /*print all the input bits*/
  211.     printf("    >>>>Special treatment on output:\n");
  212.     outstr(buf.outspec,outval,outnam);    /*dito for all the output bits*/
  213.     exit(1);
  214. }
  215. /*subroutine to print all the strings who's bits are set*/
  216. outstr(bits,vals,nams)
  217. int    bits;        /*mask with bits set in it*/
  218. int    vals[];        /*array of bit values*/
  219. char    *nams[];    /*array of pointers to bit name strings*/
  220. {
  221.     
  222.     i=0;        /*initialize index to array*/
  223.     while (vals[i] != 0)
  224.     {
  225.     if ((vals[i] & bits) != 0)    /*is this bit set?*/
  226.         printf(nams[i]);        /*yes, print the string*/
  227.     i++;
  228.     }
  229.     return;
  230. }
  231. /*subroutine to lowercase a string*/
  232. dolower(str)
  233. char    *str;
  234. {
  235.     while(*str != 0)
  236.     {
  237.     if ((*str >= 'A') && (*str <= 'Z'))
  238.         *str |= 040;        /*and in the lower case bit*/
  239.     str++;
  240.     }
  241.     return;
  242. }
  243.