home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / PROGRAMM / SERIAL.ZIP / SIMPLE.C < prev    next >
C/C++ Source or Header  |  1993-02-07  |  16KB  |  628 lines

  1. /********************************************************************
  2. * simple.c - Simple Comm 1.0 - A Simple Communications Program      *
  3. *            Copyright (c) 1992 By Mark D. Goodwin                  *
  4. ********************************************************************/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <conio.h>
  9. #include <dos.h>
  10. #include <ctype.h>
  11. #ifndef __TURBOC__
  12. #include <graph.h>
  13. #include <direct.h>
  14. #else
  15. #include <dir.h>
  16. #endif
  17. #include "serial.h"
  18.  
  19. /* data format constants */
  20. #define P8N1 1
  21. #define P7E1 0
  22.  
  23. /* configuration file record structure */
  24. typedef struct {
  25.     int port;
  26.     long baud_rate;
  27.     int parity;
  28.     int lock_port;
  29.     char initialization_string[81];
  30.     char hangup_string[41];
  31.     char dial_prefix[41];
  32. } CONFIG;
  33.  
  34. /* function prototypes */
  35. void config_program(void);
  36. void dial(void);
  37. void download(void);
  38. void exit_program(void);
  39. char *get_modem_string(char *s, char *d);
  40. void help(void);
  41. void hangup(void);
  42. void modem_control_string(char *s);
  43. void read_config(void);
  44. void save_config(void);
  45. void upload(void);
  46.  
  47. char *files[10];
  48. CONFIG config;
  49.  
  50. void main(void)
  51. {
  52.     int c;
  53.  
  54.     read_config();                      /* read in the config file */
  55.     ansi_dsr = put_serial;              /* set ANSI routine */
  56.     ansi_dsr_flag = TRUE;
  57.     ansiout(12);
  58.     ansiprintf("Simple Comm v1.0\n");
  59.     ansiprintf("Copyright (c) 1992 By Mark D. Goodwin\n\n");
  60.     ansiprintf("Initializing Modem.....\n\n");
  61.     open_port(config.port, 4096);        /* open the comm port */
  62.     /* configure the port if no carrier */
  63.     if (carrier())
  64.         ansiprintf("Already connected at %ld baud!\n\n", get_baud());
  65.     else {
  66.         set_port(config.baud_rate, config.parity ? 8 : 7,
  67.             config.parity ? NO_PARITY : EVEN_PARITY, 1);
  68.     }
  69.     set_tx_rts(TRUE);                    /* set XON/XOFF and RTS/CTS */
  70.     set_tx_xon(TRUE);                    /* flow control */
  71.     set_rx_rts(TRUE);
  72.     set_rx_xon(TRUE);
  73.     /* reset the modem and send initialization string if no carrier */
  74.     if (!carrier()) {
  75.         modem_control_string("ATZ^M");
  76.         delay(1500);
  77.         modem_control_string(config.initialization_string);
  78.         delay(500);
  79.     }
  80.     /* main program loop */
  81.     while (TRUE) {
  82.         if (kbhit()) {
  83.             c = getch();                /* get the key */
  84.             if (!c) {
  85.                 switch(getch()) {
  86.                     case 32:            /* Alt-D - Dials */
  87.                         dial();
  88.                         break;
  89.                     case 35:            /* Alt-H - Hangs Up */
  90.                         hangup();
  91.                         break;
  92.                     case 45:               /* Alt-X - Exits the Program */
  93.                         exit_program();
  94.                         break;
  95.                     case 46:            /* Alt-C - Configure Program */
  96.                         config_program();
  97.                         break;
  98.                     case 59:            /* F1 - Displays Help Screen */
  99.                         help();
  100.                         break;
  101.                     case 73:             /* Pg Up - Upload a File */
  102.                         upload();
  103.                         break;
  104.                     case 81:            /* Pg Dn - Download a File */
  105.                         download();
  106.                         break;
  107.                 }
  108.                 continue;
  109.             }
  110.             put_serial((unsigned char)c); /* send the key out the port */
  111.         }
  112.         if (in_ready()) {                /* get and display incoming */
  113.             c = get_serial();            /*  characters */
  114.             ansiout(c);
  115.         }
  116.     }
  117. }
  118.  
  119. /* file transfer error handler */
  120. int e_handler(int c, long p, char *s)
  121. {
  122.     int k;
  123.  
  124.     switch (c) {
  125.         case SENDING:                    /* file is being sent */
  126.             ansiprintf("Sending: %s\n", strupr(s));
  127.             ansiprintf("Length : %ld\n", p);
  128.             break;
  129.         case RECEIVING:                 /* file is being received */
  130.             ansiprintf("Receiving: %s\n", strupr(s));
  131.             ansiprintf("Length : %ld\n", p);
  132.             break;
  133.         case SENT:                        /* block was sent */
  134.             ansiprintf("%ld bytes sent.\r", p);
  135.             break;
  136.         case RECEIVED:                    /* block was received */
  137.             ansiprintf("%ld bytes received.\r", p);
  138.             break;
  139.         case ERROR:                        /* an error occurred */
  140.             ansiprintf("\nError: %s in block %ld\n", s, p);
  141.             break;
  142.         case COMPLETE:                    /* file transfer complete */
  143.             ansiprintf("\n%s\n", s);
  144.             break;
  145.     }
  146.     if (kbhit()) {                        /* check for key pressed */
  147.         if (!(k = getch())) {            /*  abort if ESC pressed */
  148.             getch();
  149.             return TRUE;
  150.         }
  151.         if (k == 27)
  152.             return FALSE;
  153.     }
  154.     return TRUE;
  155. }
  156.  
  157. /* download file routine */
  158. void download(void)
  159. {
  160.     int c;
  161.     char cdir[81], line[81];
  162.  
  163.     getcwd(cdir, 81);                      /* get the current directory */
  164.     ansiprintf("\n");                        /* display the menu */
  165.     ansiprintf("<X> - Xmodem\n");
  166.     ansiprintf("<K> - Xmodem-1K\n");
  167.     ansiprintf("<Y> - Ymodem\n");
  168.     ansiprintf("<G> - Ymodem-G\n");
  169.     ansiprintf("<Q> - Quit\n");
  170.     ansiprintf("\nPlease make a selection: ");
  171.     while (TRUE) {
  172.         while (!kbhit()) ;
  173.         switch (toupper(getch())) {
  174.             case 0:                        /* handle extended keys */
  175.                 getch();
  176.                 break;
  177.             case 'X':                    /* Xmodem or Xmodem-1K */
  178.             case 'K':
  179.                 /* prompt the user for the filename */
  180.                 ansiprintf("X\n\nPlease enter the filename: ");
  181.                 gets(line);
  182.                 ansiprintf("\n");
  183.                 if (!*line)
  184.                     return;
  185.                 recv_file(XMODEM, e_handler, line);    /* get the file */
  186.                 return;
  187.             case 'Y':
  188.                 ansiprintf("Y\n\n");
  189.                 recv_file(YMODEM, e_handler, cdir);    /* get the file */
  190.                 return;
  191.             case 'G':
  192.                 ansiprintf("G\n\n");
  193.                 recv_file(YMODEMG, e_handler, cdir); /* get the file */
  194.                 return;
  195.             case 'Q':                    /* quit */
  196.                 ansiprintf("Q\n\n");
  197.                 return;
  198.         }
  199.     }
  200. }
  201.  
  202. /* upload a file */
  203. void upload(void)
  204. {
  205.     int c;
  206.     char line[81];
  207.  
  208.     ansiprintf("\n");                        /* display the menu */
  209.     ansiprintf("<X> - Xmodem\n");
  210.     ansiprintf("<K> - Xmodem-1K\n");
  211.     ansiprintf("<Y> - Ymodem\n");
  212.     ansiprintf("<G> - Ymodem-G\n");
  213.     ansiprintf("<Q> - Quit\n");
  214.     ansiprintf("\nPlease make a selection: ");
  215.     while (TRUE) {
  216.         while (!kbhit()) ;
  217.         switch (toupper(getch())) {
  218.             case 0:                        /* handle extended keys */
  219.                 getch();
  220.                 break;
  221.             case 'X':
  222.                 /* prompt the user for the filename */
  223.                 ansiprintf("X\n\nPlease enter the filename: ");
  224.                 gets(line);
  225.                 ansiprintf("\n");
  226.                 if (!*line)
  227.                     return;
  228.                 files[0] = line;
  229.                 files[1] = NULL;
  230.                 xmit_file(XMODEM, e_handler, files); /* send the file */
  231.                 return;
  232.             case 'K':
  233.                 /* prompt the user for the filename */
  234.                 ansiprintf("K\n\nPlease enter the filename: ");
  235.                 gets(line);
  236.                 ansiprintf("\n");
  237.                 if (!*line)
  238.                     return;
  239.                 files[0] = line;
  240.                 files[1] = NULL;
  241.                 xmit_file(XMODEM1K, e_handler, files); /* send the file */
  242.                 return;
  243.             case 'Y':
  244.                 /* prompt the user for the filename */
  245.                 ansiprintf("Y\n\nPlease enter the filename: ");
  246.                 gets(line);
  247.                 ansiprintf("\n");
  248.                 if (!*line)
  249.                     return;
  250.                 files[0] = line;
  251.                 files[1] = NULL;
  252.                 xmit_file(YMODEM, e_handler, files); /* send the file */
  253.                 return;
  254.             case 'G':
  255.                 /* prompt the user for the filename */
  256.                 ansiprintf("G\n\nPlease enter the filename: ");
  257.                 gets(line);
  258.                 ansiprintf("\n");
  259.                 if (!*line)
  260.                     return;
  261.                 files[0] = line;
  262.                 files[1] = NULL;
  263.                 xmit_file(YMODEMG, e_handler, files); /* send the file */
  264.                 return;
  265.             case 'Q':
  266.                 ansiprintf("Q\n\n");           /* quit */
  267.                 return;
  268.         }
  269.     }
  270. }
  271.  
  272. /* Help Routine */
  273. void help(void)
  274. {
  275.     ansiprintf("\n");                        /* display the special keys */
  276.     ansiprintf("Alt-C - Configure Program\n");
  277.     ansiprintf("Alt-D - Dial Phone\n");
  278.     ansiprintf("Alt-H - Hang Up Modem\n");
  279.     ansiprintf("Pg Up - Upload a File\n");
  280.     ansiprintf("Pg Dn - Download a File\n");
  281.     ansiprintf("Alt-X - Exit Program\n");
  282.     ansiprintf("\n");
  283.     ansiprintf("Press any key to continue.....\n");
  284.     ansiprintf("\n");
  285.     while (!kbhit()) {                       /* wait for a key */
  286.         if (!getch())
  287.             getch();
  288.         return;
  289.     }
  290. }
  291.  
  292. /* Exit Program Routine */
  293. void exit_program(void)
  294. {
  295.     /* if carrier, ask whether to D/C or not */
  296.     if (carrier()) {
  297.         ansiprintf("\nStill connected.....Hang Up (Y/n)? ");
  298.         while (TRUE) {
  299.             if (kbhit()) {
  300.                 switch (toupper(getch())) {
  301.                     case 0:                 /* handle extended keys */
  302.                         getch();
  303.                         continue;
  304.                     case 13:
  305.                     case 'Y':
  306.                         ansiprintf("Y\n\n"); /* hangup */
  307.                         hangup();
  308.                         break;
  309.                     case 'N':
  310.                         ansiprintf("N\n\n"); /* don't hangup */
  311.                         break;
  312.                     default:
  313.                         continue;
  314.                 }
  315.                 break;
  316.             }
  317.         }
  318.     }
  319.     close_port();                        /* close the serial port */
  320.     exit(0);                            /* exit the program */
  321. }
  322.  
  323. /* dial routine */
  324. void dial(void)
  325. {
  326.     int br;
  327.     char line[81], dstring[81], number[81];
  328.  
  329.     /* prompt user for the number to be dialed */
  330.     ansiprintf("\nEnter the number you wish to dial: ");
  331.     gets(number);
  332.     if (!*number)
  333.         return;
  334.     sprintf(dstring, "%s%s", config.dial_prefix, number);
  335.     while (TRUE) {
  336.         ansiprintf("\nDailing %s\n", number);
  337.         delay(2000);
  338.         ansiprintf("Seconds Remaining: ");
  339.         sprintf(line, "%s^M", dstring);
  340.         modem_control_string(line);        /* dial the number */
  341.         get_modem_string(line, dstring); /* get a response */
  342.         /* dial interrupted by user */
  343.         if (!strcmp(line, "INTERRUPTED")) {
  344.             ansiprintf("\n");
  345.             return;
  346.         }
  347.         /* user wants to repeat the dial immediately */
  348.         if (!strcmp(line, "RECYCLE")) {
  349.             ansiprintf("\n");
  350.             continue;
  351.         }
  352.         /* connection was made */
  353.         if (strstr(line, "CONNECT") != NULL) {
  354.             ansiprintf("\n%s\n", line);
  355.             /* set DTE rate to match the connection rate if port */
  356.             /*  isn't locked */
  357.             if (!config.lock_port) {
  358.                 br = atoi(line + 7);
  359.                 if (br)
  360.                     set_baud(br);
  361.                 else
  362.                     set_baud(300);
  363.             }
  364.             return;
  365.         }
  366.         ansiprintf("\n%s\n", line);            /* display the result string */
  367.     }
  368. }
  369.  
  370. /* get response string */
  371. char *get_modem_string(char *s, char *d)
  372. {
  373.     int i, c;
  374.     unsigned last;
  375.  
  376.     last = mpeek(0, 0x46c);
  377.     i = 1092;
  378.     *s = 0;
  379.     ansiprintf("%-2d", (i * 10) / 182);        /* display time remaining */
  380.     while (TRUE) {
  381.         /* check for user intervention */
  382.         if (kbhit()) {
  383.             switch (c = getch()) {
  384.                 case 0:                    /* handle extended keys */
  385.                     getch();
  386.                     break;
  387.                 case 27:                /* ESC - abort dial */
  388.                     put_serial(13);
  389.                     sprintf(s, "INTERRUPTED");
  390.                     return s;
  391.                 case ' ':                /* SPACE - redial */
  392.                     put_serial(13);
  393.                     sprintf(s, "RECYCLE");
  394.                     return s;
  395.             }
  396.             continue;
  397.         }
  398.         /* get a character from the port if one's there */
  399.         if (in_ready()) {
  400.             switch (c = get_serial()) {
  401.                 case 13:                /* CR - return the result string */
  402.                     if (*s)
  403.                         return s;
  404.                     continue;
  405.                 default:
  406.                     if (c != 10) {        /* add char to end of string */
  407.                         s[strlen(s) + 1] = 0;
  408.                         s[strlen(s)] = (char)c;
  409.                         /* ignore RINGING and the dial string */
  410.                         if (!strcmp(s, "RINGING") || !strcmp(s, d))
  411.                             *s = 0;
  412.                     }
  413.             }
  414.         }
  415.         /* check for timeout and display time remaining */
  416.         if (last != mpeek(0, 0x46c)) {
  417.             last = mpeek(0, 0x46c);
  418.             i--;
  419.             ansiprintf("\b \b\b \b%-2d", (i * 10) / 182);
  420.             if (!i) {
  421.                 *s = 0;
  422.                 put_serial(13);
  423.                 return s;
  424.             }
  425.         }
  426.     }
  427. }
  428.  
  429. /* Hang Up Modem Routine */
  430. void hangup(void)
  431. {
  432.     int i;
  433.     unsigned last;
  434.  
  435.     ansiprintf("\nHanging Up");
  436.     last = mpeek(0, 0x46c);             /* get current system clock */
  437.     i = 180;                            /* try for about 10 seconds */
  438.     set_dtr(FALSE);                        /* drop DTR */
  439.     while (carrier() && i) {            /* loop till loss of carrier */
  440.         if (last != mpeek(0, 0x46c)) {  /*  or time out */
  441.             i--;
  442.             last = mpeek(0, 0x46c);
  443.             ansiprintf(".");
  444.         }
  445.     }
  446.     set_dtr(TRUE);                        /* raise DTR */
  447.     if (!carrier()) {                    /* return if disconnect */
  448.         ansiprintf("\n");
  449.         purge_in();
  450.         return;
  451.     }
  452.     modem_control_string(config.hangup_string); /* send software command */
  453.     i = 180;                            /* try for about 10 seconds */
  454.     while (carrier() && i) {            /* loop till loss of carrier */
  455.         if (last != mpeek(0, 0x46c)) {   /*  or time out */
  456.             i--;
  457.             last = mpeek(0, 0x46c);
  458.             ansiprintf(".");
  459.         }
  460.     }
  461.     purge_in();
  462.     ansiprintf("\n");
  463. }
  464.  
  465. /* Send Control String to Modem */
  466. void modem_control_string(char *s)
  467. {
  468.     while (*s) {                        /* loop for the entire string */
  469.         switch (*s) {
  470.             case '~':                    /* if ~, wait a half second */
  471.                 delay(500);
  472.                 break;
  473.             default:
  474.                 switch (*s) {
  475.                     case '^':            /* if ^, it's a control code */
  476.                         if (s[1]) {        /* send the control code */
  477.                             s++;
  478.                             put_serial((unsigned char)(*s - 64));
  479.                         }
  480.                         break;
  481.                     default:
  482.                         put_serial(*s);    /* send the character */
  483.                 }
  484.                 delay(50);                /* wait 50 ms. for slow modems */
  485.         }
  486.         s++;                               /* bump the string pointer */
  487.     }
  488. }
  489.  
  490. /* read program configuration file */
  491. void read_config(void)
  492. {
  493.     FILE *f;
  494.  
  495.     /* open the file, create default if it doesn't exist */
  496.     if ((f = fopen("SIMPLE.CFG", "rb")) == NULL) {
  497.         if ((f = fopen("SIMPLE.CFG", "wb")) != NULL) {
  498.             config.port = 1;
  499.             config.baud_rate = 57600;
  500.             config.parity = P8N1;
  501.             config.lock_port = FALSE;
  502.             sprintf(config.initialization_string, "ATS0=0V1X4^M");
  503.             sprintf(config.hangup_string, "~~~+++~~~ATH0^M");
  504.             sprintf(config.dial_prefix, "ATDT");
  505.             fwrite(&config, sizeof(CONFIG), 1, f);
  506.             return;
  507.         }
  508.         return;
  509.     }
  510.     fread(&config, sizeof(CONFIG), 1, f); /* read in config info */
  511. }
  512.  
  513. /* write program configuration file */
  514. void write_config(void)
  515. {
  516.     FILE *f;
  517.  
  518.     /* write the config info */
  519.     if ((f = fopen("SIMPLE.CFG", "w+b")) != NULL) {
  520.         fwrite(&config, sizeof(CONFIG), 1, f);
  521.         fclose(f);
  522.     }
  523. }
  524.  
  525. /* program configuration routine */
  526. void config_program(void)
  527. {
  528.     int t;
  529.     long tl;
  530.     char line[81];
  531.  
  532.     while (TRUE) {
  533.         /* display the menu */
  534.         ansiprintf("\n");
  535.         ansiprintf("<1> Serial Port     : %d\n", config.port);
  536.         ansiprintf("<2> Baud Rate       : %ld\n", config.baud_rate);
  537.         ansiprintf("<3> Parity          : %s\n",
  538.             config.parity == P8N1 ? "8N1" : "7E1");
  539.         ansiprintf("<4> Lock Serial Port: %s\n",
  540.             config.lock_port ? "Yes" : "No");
  541.         ansiprintf("<5> Init. String    : %s\n", config.initialization_string);
  542.         ansiprintf("<6> Hangup String   : %s\n", config.hangup_string);
  543.         ansiprintf("<7> Dial Prefix     : %s\n", config.dial_prefix);
  544.         ansiprintf("<S> Save and Quit\n");
  545.         ansiprintf("<Q> Quit\n");
  546.         ansiprintf("\nPlease make a selection: ");
  547.         while (TRUE) {
  548.             while (!kbhit()) ;
  549.             switch (toupper(getch())) {
  550.                 case 0:                   /* handle extended keys */
  551.                     getch();
  552.                     continue;
  553.                 case '1':                /* get new port */
  554.                     ansiprintf("1\n\n");
  555.                     ansiprintf("Please select the new serial port: ");
  556.                     gets(line);
  557.                     ansiprintf("\n");
  558.                     if (!(t = atoi(line)))
  559.                         break;
  560.                     /* make sure it's a valid port */
  561.                     if (t > 4 || !port_exist(t))
  562.                         ansiprintf("That port doesn't exist!\n");
  563.                     else {
  564.                         close_port();
  565.                         config.port = t;
  566.                         open_port(config.port, 4096);
  567.                     }
  568.                     break;
  569.                 case '2':                /* get new baud rate */
  570.                     ansiprintf("2\n\n");
  571.                     ansiprintf("Please enter the new baud rate: ");
  572.                     gets(line);
  573.                     ansiprintf("\n");
  574.                     if ((tl = atol(line))) {
  575.                         config.baud_rate = tl;
  576.                         set_baud(config.baud_rate);
  577.                     }
  578.                     break;
  579.                 case '3':                /* toggle data format */
  580.                     ansiprintf("3\n\n");
  581.                     config.parity = (config.parity == P8N1 ? P7E1 : P8N1);
  582.                     set_data_format(config.parity ? 8 : 7,
  583.                         config.parity ? NO_PARITY : EVEN_PARITY, 1);
  584.                     break;
  585.                 case '4':                /* toggle locked port flag */
  586.                     ansiprintf("4\n\n");
  587.                     config.lock_port = !config.lock_port;
  588.                     break;
  589.                 case '5':                /* get new init string */
  590.                     ansiprintf("5\n\n");
  591.                     ansiprintf("Please enter the new initialization string: ");
  592.                     gets(line);
  593.                     ansiprintf("\n");
  594.                     if (*line)
  595.                         sprintf(config.initialization_string, strupr(line));
  596.                     break;
  597.                 case '6':                /* get new hangup string */
  598.                     ansiprintf("6\n\n");
  599.                     ansiprintf("Please enter the new hangup string: ");
  600.                     gets(line);
  601.                     ansiprintf("\n");
  602.                     if (*line)
  603.                         sprintf(config.hangup_string, strupr(line));
  604.                     break;
  605.                 case '7':                /* get new dial prefix */
  606.                     ansiprintf("7\n\n");
  607.                     ansiprintf("Please enter the new dial prefix: ");
  608.                     gets(line);
  609.                     ansiprintf("\n");
  610.                     if (*line)
  611.                         sprintf(config.dial_prefix, strupr(line));
  612.                     break;
  613.                 case 'S':                /* save config and return */
  614.                     ansiprintf("S\n\n");
  615.                     write_config();
  616.                     return;
  617.                 case 'Q':               /* quit */
  618.                     ansiprintf("Q\n\n");
  619.                     return;
  620.                 default:
  621.                     continue;
  622.             }
  623.             break;
  624.         }
  625.     }
  626. }
  627.  
  628.