POLYCOMM.CPP
上传用户:xr_qian
上传日期:2007-01-05
资源大小:443k
文件大小:17k
源码类别:

通讯/手机编程

开发平台:

DOS

  1. // ******************************************************************** //
  2. //                                                                      //
  3. //      POLYCOMM.CPP                                                    //
  4. //      Copyright (c) 1993, Michael Holmes and Bob Flanders             //
  5. //      C++ Communication Utilities                                     //
  6. //                                                                      //
  7. //      Chapter 7: Receiving a FAX                                      //
  8. //      Last changed in chapter 7                                       //
  9. //                                                                      //
  10. //      This file contains the main function for the PolyComm           //
  11. //      program.  This code is specific for the Microsoft C/C++         //
  12. //      version 7 compiler.                                             //
  13. //                                                                      //
  14. //          Compile with:  CL /AC polycomm.cpp /link graphics           //
  15. //                                                                      //
  16. // ******************************************************************** //
  17. #include <stdio.h>                          // standard i/o library
  18. #include <stdarg.h>                         // variable argument list
  19. #include <string.h>                         // string handling routines
  20. #include <stdlib.h>                         // std conversion routines
  21. #include <dos.h>                            // dos functions
  22. #include <ctype.h>                          // character routines
  23. #include <conio.h>                          // console functions
  24. #include <bios.h>                           // bios functions
  25. #include <io.h>                             // i/o functions
  26. #include <direct.h>                         // directory routines
  27. #include <systypes.h>                      // system types definition
  28. #include <sysstat.h>                       // ..attribute bits
  29. #include <fcntl.h>                          // ..and file control
  30. #include <graph.h>                          // text mode routines
  31. #include "keys.h"                           // keyboard definitions
  32. #define CURSOR()    _settextcursor(0x0707)  // normal text cursor
  33. #define BIGCURSOR() _settextcursor(0x0000)  // insert mode cursor
  34. #define NOCURSOR()  _settextcursor(0x2000)  // turn off cursor
  35. #define COUNT(x)    (sizeof(x) / sizeof(x[0]))      // item count
  36. #define NOT         !                       // shorthand logical
  37. #define BYTE        char                    // single byte
  38. #define UINT        unsigned int            // unsigned integer
  39. #define UCHAR       unsigned char           // ..and unsigned character
  40. #define ULONG       unsigned long           // ..and unsigned long
  41. #define MAX_PATH    79                      // maximum path length
  42. #define MIX(x,y)    ((x << 4) + (y))        // mix colors for fg and bg
  43. #define FG(x)       (unsigned char) x >> 4  // extract foreground color
  44. #define BG(x)       x & 0x07                // ..and background color
  45. #define IN(x)       _inp(base + x)          // read a UART register
  46. #define OUT(x,y)    _outp(base + x, y)      // ..and write a register
  47. #define NULLPTR(x)  &x ? x : ""             // make null ptr point to null
  48. #define LAST(s)     s[strlen(s) - 1]        // last character in string
  49. #define SECS(x)     (long) (x * 182L) / 10L // seconds to ticks conversion
  50. #define TRUE        1                       // true value
  51. #define FALSE       0                       // false value
  52. #define SOH         1                       // start of header
  53. #define STX         2                       // start of text
  54. #define ETX         3                       // end of text
  55. #define EOT         4                       // end of transmission
  56. #define ACK         6                       // positive acknowledgment
  57. #define XOFF        19                      // flow control X-OFF
  58. #define DLE         16                      // data link escape
  59. #define NAK         21                      // negative acknowledgment
  60. #define CAN         24                      // cancel process
  61. #define MK_FP(s,o)  (((long) s << 16) | (long) o)  // make a long pointer
  62. #define INT_PARMS UINT es, UINT ds,         /* interrupt calling conv  */
  63.                   UINT di, UINT si,                                      
  64.                   UINT bp, UINT sp,                                      
  65.                   UINT bx, UINT dx,                                      
  66.                   UINT cx, UINT ax,                                      
  67.                   UINT ip, UINT cs,                                      
  68.                   UINT flags
  69. /* ******************************************************************** *
  70.  *
  71.  *  UART Register Definitions
  72.  *
  73.  * ******************************************************************** */
  74.                                             // UART regs (base address +)
  75. #define RBR         0                       // receive buffer register
  76. #define THR         0                       // transmit holding register
  77. #define DLL         0                       // divisor latch LSB
  78. #define DLM         1                       // divisor latch MSB
  79. #define IER         1                       // interrupt enable register
  80. #define IIR         2                       // interrupt id register
  81. #define FCR         2                       // FIFO control register
  82. #define AFR         2                       // alternate function register
  83. #define LCR         3                       // line control register
  84. #define MCR         4                       // modem control register
  85. #define LSR         5                       // line status register
  86. #define MSR         6                       // modem status register
  87. #define SCR         7                       // scratch register
  88.                                             // interrupt enable register
  89. #define IER_RBF     0x01                    //  receive buffer full
  90. #define IER_TBE     0x02                    //  transmit buffer empty
  91. #define IER_LSI     0x04                    //  line status interrupt
  92. #define IER_MSI     0x08                    //  modem status interrupt
  93. #define IER_ALL     0x0f                    //  enable all interrupts
  94.                                             // interrupt id register
  95. #define IIR_PEND    0x01                    //  interrupt pending = 0
  96. #define IIR_II      0x06                    //  interrupt id bits
  97.                                             //   000 = modem status change
  98.                                             //   001 = trans holding empty
  99.                                             //   010 = receive buffer full
  100.                                             //   110 = receive fifo full
  101.                                             //   011 = line status change
  102. #define IIR_MSI     0x00                    //  modem status interrupt
  103. #define IIR_TBE     0x02                    //  transmit buffer empty
  104. #define IIR_RBF     0x04                    //  receive buffer full
  105. #define IIR_LSI     0x06                    //  line status interrupt
  106. #define IIR_RFF     0x0c                    //  receive fifo threshold
  107.                                             // fifo control register
  108. #define FCR_FIFO    0x01                    //  fifo enable
  109. #define FCR_RCVR    0x02                    //  receiver fifo reset
  110. #define FCR_XMIT    0x04                    //  transmit fifo reset
  111. #define FCR_DMA     0x08                    //  DMA mode select
  112. #define FCR_TRIGGER 0xc0                    //  receiver trigger select
  113.                                             //   00 = 1 byte
  114.                                             //   01 = 4 bytes
  115.                                             //   10 = 8 bytes
  116.                                             //   11 = 14 bytes
  117. #define FCR_16550   0xc7                    //  16550 fifo enable/reset
  118.                                             // line control register
  119. #define LCR_WLEN    0x03                    //  word length
  120.                                             //   10 = 7 bits
  121.                                             //   11 = 8 bits
  122. #define LCR_STOP    0x04                    //  stop bits
  123.                                             //   0 = 1 stop bit
  124.                                             //   1 = 2 stop bits
  125. #define LCR_PARITY  0x08                    //  parity enable
  126.                                             //   0 = no parity
  127.                                             //   1 = send/check parity
  128. #define LCR_EVEN    0x10                    //  even/odd parity
  129.                                             //   0 = odd parity
  130.                                             //   1 = even parity
  131. #define LCR_BREAK   0x40                    //  break, set to xmit break
  132. #define LCR_DLAB    0x80                    //  divisor latch access bit
  133.                                             // modem control register
  134. #define MCR_DTR     0x01                    //  DTR control
  135. #define MCR_RTS     0x02                    //  RTS control
  136. #define MCR_OUT2    0x08                    //  OUT2 control
  137. #define MCR_DO      0x0b                    //  dtr, rts & out2 enabled
  138.                                             // line status register
  139. #define LSR_DR      0x01                    //  data ready
  140. #define LSR_ORUN    0x02                    //  overrun error
  141. #define LSR_PRTY    0x04                    //  parity error
  142. #define LSR_FRM     0x08                    //  framing error
  143. #define LSR_BRK     0x10                    //  break interrupt
  144. #define LSR_THRE    0x20                    //  transmit holding reg empty
  145. #define LSR_TSRE    0x40                    //  transmit shift reg emtpy
  146. #define LSR_ERROR   0x1e                    //  error conditions
  147.                                             // modem status register
  148. #define MSR_DCTS    0x01                    //  delta clear to send
  149. #define MSR_DDSR    0x02                    //  delta data set ready
  150. #define MSR_TERI    0x04                    //  trailing edge ring indicator
  151. #define MSR_DCD     0x08                    //  delta carrier detect
  152. #define MSR_CTS     0x10                    //  clear to send
  153. #define MSR_DSR     0x20                    //  data set ready (modem ready)
  154. #define MSR_RI      0x40                    //  ring indicated
  155. #define MSR_CD      0x80                    //  carrier detected
  156. /* ******************************************************************** *
  157.  *
  158.  *    8259 Programmable Interrupt Controller Definitions
  159.  *
  160.  * ******************************************************************** */
  161. #define I8259       0x20                    // control register address
  162. #define EOI         0x20                    // end of interrupt command
  163. #define I8259M      0x21                    // mask register
  164. /* ******************************************************************** *
  165.  *
  166.  *  Routine definitions
  167.  *
  168.  * ******************************************************************** */
  169. void    initialization(int, char *[]),      // initialization
  170.         status_line(void),                  // update status line
  171.         wait_ms(long),                      // wait in milliseconds
  172.         wait(long);                         // ..and wait in seconds
  173. int     about(int, int),                    // about box routine
  174.         pc_exit(int, int),                  // menu exit routine
  175.         ports(int, int),                    // port selection menu routine
  176.         comm_parms(int, int),               // comm parms menu routine
  177.         hangup(int, int),                   // hangup menu routine
  178.         phone_list(int, int),               // phonebook menu routine
  179.         dl_xmodem(int, int),                // xmodem download menu rtn
  180.         ul_xmodem(int, int),                // ..and upload menu routine
  181.         dl_ymodem(int, int),                // ymodem download menu rtn
  182.         ul_ymodem(int, int),                // ..and upload menu routine
  183.         rcv_fax(int, int),                  // receive a fax
  184.         send_fax(int, int),                 // .. and send a fax
  185.         get_key(int);                       // get any type of key
  186. /* ******************************************************************** *
  187.  *
  188.  *  Set the stack size to 8k
  189.  *
  190.  * ******************************************************************** */
  191. extern
  192. unsigned _stklen = 8192;
  193. /* ******************************************************************** *
  194.  *
  195.  *  PolyComm includes
  196.  *
  197.  * ******************************************************************** */
  198. #include "screen.cpp"                       // screen handling routines
  199. #include "window.cpp"                       // window class
  200. #include "menu.cpp"                         // menu class
  201. #include "list.cpp"                         // list class
  202. #include "comm.cpp"                         // basic comm support
  203. #include "global.cpp"                       // strings and global data
  204. #include "utility.cpp"                      // utility functions
  205. #include "protocol.cpp"                     // protocol class
  206. #include "xmodem.cpp"                       // XMODEM and XMODEM/CRC class
  207. #include "ymodem.cpp"                       // YMODEM class
  208. #include "command.cpp"                      // command menu routine
  209. #include "dial.cpp"                         // dial menu routines
  210. #include "config.cpp"                       // configuration menu rtns
  211. #include "fax.cpp"                          // facsimile support
  212. #include "transfer.cpp"                     // transfer file menu rtns
  213. /* ******************************************************************** *
  214.  *
  215.  *  main() -- PolyComm mainline
  216.  *
  217.  * ******************************************************************** */
  218. void    main(int argc,                      // command line token count
  219.              char *argv[])                  // ..and command line tokens
  220. {
  221. printf(copyright);                          // display copyright msg
  222. initialization(argc, argv);                 // init and parse cmd line
  223. while(NOT quit_flag)                        // loop 'til user requests out
  224.     {
  225.     terminal_handler();                     // try to get a keyboard char
  226.     if (NOT comm->IEmpty())                 // q. any incoming com chars?
  227.         terminal_display();                 // a. yes .. show input stream
  228.     }
  229. rc = 0;                                     // clear DOS errorlevel
  230. quit_with(done);                            // ..and give completion msg
  231. }
  232. /* ******************************************************************** *
  233.  *
  234.  *  initialization() -- perform framework initializations
  235.  *
  236.  * ******************************************************************** */
  237. void    initialization(int  ac,             // command line token count
  238.                        char *av[])          // ..and command line tokens
  239. {
  240. struct  videoconfig vc;                     // screen info structure
  241. old_break = _dos_getvect(0x1b);             // get old ^break handler addr
  242. if (ac > 2 ||                               // q. need help..
  243.             NOT strcmp(av[1], "/?"))        // ..or want help?
  244.     quit_with(help);                        // a. yes .. give help/quit
  245. _dos_setvect(0x1b, control_break);          // set up control break
  246. _dos_setvect(0x24, critical_routine);       // ..and DOS critical handler
  247. _getvideoconfig(&vc);                       // get current screen info
  248. max_lines = vc.numtextrows;                 // save maximum nbr of lines
  249. if (vc.numtextcols < 80)                    // q. less than 80 columns?
  250.     quit_with(bad_width);                   // a. yes .. give error/quit
  251. if (vc.mode == _TEXTBW80 ||                 // q. black and white mode..
  252.             vc.mode == _TEXTMONO)           // ..or monochrome mode?
  253.     {
  254.     main_menu.SetColors(mono_1, mono_2);    // a. yes .. set up for
  255.     term_cn = mono_2;                       // ..monochrome display
  256.     term_cr = mono_1;                       // ..for all windows
  257.     stat_cn = mono_1;
  258.     }
  259. if (vc.mode == _TEXTMONO)                   // q. mono adapter?
  260.     vid_seg = (char huge *)MK_FP(0xb000, 0);// a. yes .. use mono memory
  261. load_config(av[1]);                         // load modem config file
  262. load_pb();                                  // ..and phonebook file
  263. check_ports();                              // check for available ports
  264. build_comm();                               // set up Comm object
  265. wait_ms(500L);                              // wait a little bit
  266. full_screen = 1;                            // show init complete
  267. status_line();                              // ..and display status line
  268. _wscroll = 1;                               // set scrolling mode
  269. term = new Window(1, 1, 80, 24,             // define terminal window
  270.               term_cn, term_cr);            // ..and its colors
  271. term->Open(none);                           // ..then open w/o borders
  272. comm->IClear();                             // ..clear input buffer
  273. }