home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / MOUSE / SWPRT.ZIP / SWPRT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-24  |  3.3 KB  |  128 lines

  1. /*
  2. +--------------------------------------------------------+
  3. |  SWPRT.C  -  sets printer port assignments for IBM PC  |
  4. |  R. Trevithick                 Last modified: 12/24/87 |
  5. +--------------------------------------------------------+
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <dos.h>
  10.  
  11. #define PSEG 64         /* Printer table segment  */
  12. #define PRN1  8         /* Printer 1 offset       */
  13. #define PRN2 10         /* Printer 2 offset       */
  14. #define PRN3 12         /* Printer 3 offset       */
  15.  
  16. FILE    *msg;           /* messages to user       */
  17.  
  18. int     p1, p2, p3, new_port = 0;
  19.  
  20.  
  21. /* Function prototypes */
  22. void    read_values(void), swap(int a, int b), normalize(void), set(void);
  23. void    end_prog(int code), help_screen(void);
  24.  
  25.  
  26. main(int argc, char *argv[])
  27. {
  28.         int     i;
  29.  
  30.         msg = stderr;                   /* default to showing messages */
  31.  
  32.         for (i = 1; i < argc; i++) {
  33.                 if (argv[i][0] == '/') {
  34.                         if (tolower(argv[i][1]) == 'q') 
  35.                                 msg = NULL;
  36.                         else
  37.                         if (argv[i][1] > 0x30 && argv[i][1] < 0x34) 
  38.                                 new_port = argv[i][1];
  39.                 }
  40.         }
  41.  
  42.         if (new_port == 0) help_screen();
  43.  
  44.         normalize();
  45.         set();
  46.         end_prog(0);
  47. }
  48.  
  49.  
  50.  
  51. /********** set the requested printer by switching it with LPT1 ******/
  52. void set(void)
  53. {
  54.         if (new_port == '1') return;            /* already set by normalize */
  55.         if (new_port == '2') swap(PRN1, PRN2);
  56.         if (new_port == '3') swap(PRN1, PRN3);
  57. }
  58.  
  59.  
  60.  
  61.  
  62. /********** normalize all printer assignments ******************/
  63. void normalize(void)
  64. {
  65.         read_values();
  66.         if (p2 > p1) swap(PRN1, PRN2);
  67.         read_values();
  68.         if (p3 > p2) swap(PRN2, PRN3);
  69.         read_values();
  70.         if (p2 > p1) swap(PRN1, PRN2);
  71. }
  72.  
  73.  
  74.  
  75.  
  76. /********** swap the two addresses received ********************/
  77. void swap(int offset_1, int offset_2)
  78. {
  79.         int     temp1, temp2;
  80.  
  81.         temp1 = peek(PSEG, offset_1);
  82.         temp2 = peek(PSEG, offset_2);
  83.  
  84.         poke(PSEG, offset_1, temp2);
  85.         poke(PSEG, offset_2, temp1);
  86. }
  87.  
  88.  
  89.  
  90.  
  91. /********** read the actual port assignments *******************/
  92. void read_values(void)
  93. {
  94.         p1 = peek(PSEG, PRN1);
  95.         p2 = peek(PSEG, PRN2);
  96.         p3 = peek(PSEG, PRN3);
  97. }
  98.  
  99.  
  100.  
  101. /********** report the current printer assignment **************/
  102. void end_prog(code)
  103. int     code;
  104. {
  105.         read_values();
  106.         fputs("\n---LPT1 output routed to parallel port #", msg);
  107.         if (p1 > p2 && p2 >= p3) fputs("1 -normal", msg);
  108.         if (p2 > p1 && p2 >= p3) fputs("2", msg);
  109.         if (p3 > p1 && p3 >= p2) fputs("3", msg);
  110.         fputs("\n", msg);
  111.         _exit(code);
  112. }
  113.  
  114.  
  115.  
  116. /********** display a help screen and call exit routine *********/
  117. void help_screen(void)
  118. {
  119.         fputs("\nSWPRT - switch printer ports   12/24/87   R. Trevithick"
  120.               "\r\n\n\tSWPRT [/1] [/2] [/3] [/q]\n\n"
  121.               "\t/1    route LPT1 output normally\n"
  122.               "\t/2    route LPT1 output to parallel port #2\n"
  123.               "\t/3    route LPT1 output to parallel port #3\n"
  124.               "\t/q    quiet; suppress all messages\n", msg);
  125.         end_prog(1);
  126. }
  127.  
  128.