home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1987 / 06 / exmode.c < prev    next >
C/C++ Source or Header  |  1987-08-05  |  5KB  |  219 lines

  1. #include    <stdio.h>
  2. #include    <dos.h>
  3.  
  4. /*
  5.     by    Tom Zimniewicz
  6.  
  7.     this file is one of three, the others are excom.asm and excom.txt
  8. */
  9.  
  10. /*
  11.     (C) 1987, Crystal Computer Consulting Inc.
  12.     This software may be used freely, at your own risk,
  13.     as long as this notice is not removed.
  14. */
  15.  
  16. #define    EXINIT    4        /* ah value for extended init */
  17. #define    EXREM    5        /* ah value to remove excom */
  18. #define    EXCOM    0x5A5A        /* magic value to identify excom */
  19. #define    NOCTS    0x00000010    /* CTS not required for transmit */
  20. #define    NODSR    0x00000020    /* DSR not required for transmit */
  21. #define    DTR    0x00000100    /* DTR input flow control */
  22. #define    RTS    0x00000200    /* RTS input flow control */
  23. #define    XNXFIN    0x00010000    /* XON/XOFF input flow control */
  24. #define    XNXFOUT    0x00020000    /* XON/XOFF output flow control */
  25. #define    ANYXOUT    0x00040000    /* any char restarts output after XOFF */
  26. #define    B19200    0x00100000    /* set baud rate to 19200 */
  27. #define    B38400    0x00200000    /* set baud rate to 38400 */
  28. #define    COMINIT    0x80000000    /* port was selected */
  29.  
  30. typedef struct {
  31.     char
  32.         *str;        /* command string */
  33.     void
  34.         (*fnct)();    /* command to execute */
  35.     long
  36.         arg;        /* argument to pass */
  37. } CMDS;                /* exmode command table */
  38.  
  39. char
  40.     *insmsg = "excom installed",
  41.     *remmsg = "excom not installed",
  42.     *helpmsg;
  43. int
  44.     portnum = -1;        /* port to initialize */
  45. long
  46.     init1 = 0,        /* extended init for com1 */
  47.     init2 = 0;        /* extended init for com2 */
  48. void
  49.     exit(), install(), remcom(), exinit(), setport();
  50. CMDS
  51.     cmds[] = {
  52.         "install",    install,    0,
  53.         "remove",    remcom,        0,
  54.         "com1",        setport,    0,
  55.         "com2",        setport,    1,
  56.         "nocts",    exinit,        NOCTS,
  57.         "nodsr",    exinit,        NODSR,
  58.         "dtr",        exinit,        DTR,
  59.         "rts",        exinit,        RTS,
  60.         "xnxfin",    exinit,        XNXFIN,
  61.         "xnxfout",    exinit,        XNXFOUT,
  62.         "anyxout",    exinit,        ANYXOUT,
  63.         "19200",    exinit,        B19200,
  64.         "38400",    exinit,        B38400,
  65.         NULL
  66.     };
  67.  
  68.  
  69. main(argc, argv)
  70. int
  71.     argc;
  72. register char
  73.     **argv;
  74. {
  75.     register CMDS
  76.         *cmdp;
  77.  
  78.     /* is excom installed */
  79.     if (int14(EXINIT, 0L, 0) == EXCOM && int14(EXINIT, 0L, 1) == EXCOM)
  80.         helpmsg = insmsg;
  81.     else
  82.         helpmsg = remmsg;
  83.  
  84.     if (argc == 1)
  85.         help(helpmsg);
  86.  
  87.     while (*++argv != NULL) {
  88.         /* look up the argument in the command table */
  89.         for (cmdp = cmds;  cmdp->str != NULL;  ++cmdp)
  90.             if (strcmp(*argv, cmdp->str) == 0)
  91.                 break;
  92.  
  93.         if (cmdp->str == NULL)
  94.             help("bad command");
  95.  
  96.         else {
  97.             /* excom must be installed to set options */
  98.             if (helpmsg == remmsg && cmdp->fnct != install)
  99.                 help(helpmsg);
  100.  
  101.             /* execute the command */
  102.             (*cmdp->fnct)(cmdp->arg);
  103.         }
  104.     }
  105.  
  106.     if ((init1 & (B19200 | B38400)) == (B19200 | B38400)
  107.         || (init2 & (B19200 | B38400)) == (B19200 | B38400))
  108.         help("ambiguous baud rate setting");
  109.  
  110.     /* actually send the init bits to excom */
  111.     if ((init1 & COMINIT) != 0)
  112.         int14(EXINIT, init1, 0);
  113.  
  114.     if ((init2 & COMINIT) != 0)
  115.         int14(EXINIT, init2, 1);
  116.  
  117.     exit(0);
  118. }
  119.  
  120.  
  121. /* install excom */
  122. void
  123. install()
  124. {
  125.     system("excom");
  126.     helpmsg = insmsg;
  127. }
  128.  
  129.  
  130. /* remove excom */
  131. void
  132. remcom()
  133. {
  134.     int14(5, 0L, 0);
  135.     helpmsg = remmsg;
  136. }
  137.  
  138.  
  139. /* collect up the init bits for each port */
  140. void
  141. exinit(thebit)
  142. long
  143.     thebit;
  144. {
  145.     if (portnum == -1)
  146.         help("no port selected");
  147.  
  148.     else if (portnum == 0)
  149.         init1 |= thebit;
  150.  
  151.     else
  152.         init2 |= thebit;
  153. }
  154.  
  155.  
  156. /* set port number */
  157. void
  158. setport(newport)
  159. long
  160.     newport;
  161. {
  162.     portnum = newport;
  163.  
  164.     if (portnum == 0)
  165.         init1 |= COMINIT;
  166.  
  167.     else
  168.         init2 |= COMINIT;
  169. }
  170.  
  171.  
  172. /* perform int 14h with ah, al, bx & dx as passed, return ax value */
  173. int14(cmd, val, port)
  174. int
  175.     cmd;
  176. long
  177.     val;
  178. int
  179.     port;
  180. {
  181.     union REGS
  182.         ir,        /* registers send to bios */
  183.         or;        /* registers returned from bios */
  184.  
  185.     ir.h.ah = cmd;
  186.     ir.h.al = val >> 16;
  187.     ir.x.cx = val;
  188.     ir.x.dx = port;
  189.  
  190.     int86(0x14, &ir, &or);
  191.  
  192.     return or.x.ax;
  193. }
  194.  
  195.  
  196. /* provide a bit of assistance */
  197. help(msg)
  198. char
  199.     *msg;
  200. {
  201.     printf("\n%s\n\n", msg);
  202.  
  203.     fputs("install\t\tinstall excom\n", stdout);
  204.     fputs("remove\t\tremove excom\n", stdout);
  205.     fputs("com1\t\tsubsequent commands for com1\n", stdout);
  206.     fputs("com2\t\tsubsequent commands for com2\n", stdout);
  207.     fputs("nocts\t\tdon't require CTS to transmit\n", stdout);
  208.     fputs("nodsr\t\tdon't require DSR to transmit\n", stdout);
  209.     fputs("dtr\t\tuse DTR for input flow control\n", stdout);
  210.     fputs("rts\t\tuse RTS for input flow control\n", stdout);
  211.     fputs("xnxfin\t\tuse XON/XOFF (^S ^Q) input flow control\n", stdout);
  212.     fputs("xnxfout\t\tuse XON/XOFF (^S ^Q) output flow control\n", stdout);
  213.     fputs("anyxout\t\tany char will restart after XOFF\n", stdout);
  214.     fputs("19200\t\tset baud rate\n", stdout);
  215.     fputs("38400\t\tset baud rate\n", stdout);
  216.  
  217.     exit(1);
  218. }
  219.