home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / JMOD309.ZIP / JMODEM_F.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-05  |  22.9 KB  |  494 lines

  1. /****************************************************************************/
  2. /*   FILE JMODEM_F.C                                                        */
  3. /*   Created 11-JAN-1990                    Richard B. Johnson              */
  4. /*                                          405 Broughton Drive             */
  5. /*                                          Beverly, Massachusetts 01925    */
  6. /*                                          BBS (508) 922-3166              */
  7. /*                                                                          */
  8. /*   screen();         All screen output procedures. Uses INT 10H under     */
  9. /*                     MS-DOS.                                              */
  10. /*                                                                          */
  11. /*   disp();           Displays a "USAGE" message                           */
  12. /*                                                                          */
  13. /*   These routines are absolutely not necessary and could be replaced      */
  14. /*   with _printf() statements. They are used to make the pretty screens    */
  15. /*   and overlapping windows that the PC community has grown to expect.     */
  16. /*                                                                          */
  17. /****************************************************************************/
  18. #define  SCREEN                                  /* For var len param list  */
  19. #include <stdio.h>
  20. #include <dos.h> 
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "screen.h"
  24. #include "jmodem.h"
  25.  
  26. short sav_par[boxes * param];                     /* Save row/columns       */
  27. short *buffer[boxes];                             /* Pointers for  boxes    */
  28. short last_box = 0;                               /* Last box on the screen */
  29. short start_txt;                                  /* Text starts in box     */
  30. short start_row;                                  /* Starting row of box    */
  31. short start_col;                                  /* Starting column of box */
  32. short end_row;                                    /* Ending row of box      */
  33. short end_col;                                    /* Ending column of box   */
  34.  
  35. byte abrt[]  = "Aborted!";                        /* Five status messages   */
  36. byte okay[]  = "Okay ";
  37. byte retry[] = "Retry";
  38. byte done[]  = "Done!";
  39. byte flow[]  = "Flow ";
  40.  
  41. char info[] = "Usage:\r\n"\
  42.               "JMODEM R1 filename\r\n"\
  43.               "       ||___ COM port (1..4)\r\n"\
  44.               "       |____ [R]eceive, [S]end\r\n"
  45.               " - or -\r\n"
  46.               "JMODEM R(3F8:4) filename\r\n"
  47.               "       |   | |___ IRQ to use (1..7)\r\n"
  48.               "       |   |_____ absolute port address (hex) \r\n"
  49.               "       |_________ [R]eceive, [S]end\r\n";
  50.  
  51. byte malfail[] = "Memory allocation failed!";
  52.  
  53. char *signon[] = {
  54.                  "     J M O D E M",
  55.                  "File transfer protocol",
  56.                  "     "VERS
  57.                  };
  58.  
  59. char *sta_blk[] = {
  60.                   "   Block :" ,
  61.                   "  Length :" ,
  62.                   "   Bytes :" ,
  63.                   "Rate CPS :" ,
  64.                   "  Status :" ,
  65.                   "Synchronizing ...", 
  66.                   "  Receiving file ",
  67.                   "Transmitting file"
  68.                   };
  69.  
  70. char *fil_blk[] = {
  71.                   "Opening file ",
  72.                   "Can't open the file!",
  73.                   "Open okay",
  74.                   "File exists, renaming to ",
  75.                   "Can't create the file!",
  76.                   };
  77. /****************************************************************************/
  78. /*                                C O D E                                   */
  79. /****************************************************************************/
  80. void screen (short function, SYS *sys)
  81. {
  82.     union REGS bios;                              /* For int 10H            */
  83.     register const ATTRIB *attr;                  /* For attribute table    */
  84.     register const short *index;                  /* Array pointer          */
  85.     short page;                                   /* Box number             */
  86.     short i;
  87.  
  88.     bios.x.ax =                                   /* Initialize             */
  89.     bios.x.bx =
  90.     bios.x.cx =
  91.     bios.x.dx = 0;
  92.  
  93.     attr = attribute + last_box;                  /* Default attribute      */
  94.     switch (function)
  95.     {
  96.         case SCR_SGN:                             /* Sign-on message        */
  97.         {
  98.             page=0;                               /* Box number             */
  99.             kill_curs(&bios);
  100.             attr      = attribute + page;
  101.             index     = box_loc + (page * boxes);
  102.             start_row = *index++;
  103.             start_col = *index++;
  104.             end_row   = *index++;
  105.             end_col   = *index;
  106.             start_txt = start_row + 1;
  107.             set_box (page     ,                   /* Page                   */
  108.                      attr->win,                   /* Screen attribute       */
  109.                      start_row,                   /* Start row              */
  110.                      start_col,                   /* Start column           */
  111.                      end_row  ,                   /* End row                */
  112.                      end_col  ,                   /* End column             */
  113.                      sav_par  ,
  114.                      buffer   ,
  115.                      &bios  );
  116.             for (i = 0; i<3; i++)
  117.             {
  118.                 set_curs (start_txt++,start_col + 4,&bios);
  119.                 write_str(signon[i],attr->txt,&bios);
  120.             }
  121.             last_box = page;
  122.             break;
  123.         }
  124.     case SCR_FIL:                                 /* File screen            */
  125.         {
  126.             page=1;
  127.             attr      = attribute + page;
  128.             index     = box_loc + (page * boxes);
  129.             start_row = *index++;
  130.             start_col = *index++;
  131.             end_row   = *index++;
  132.             end_col   = *index;
  133.             start_txt = start_row + 1;
  134.             set_box (page     ,                   /* Page                   */
  135.                      attr->win,                   /* Screen attribute       */
  136.                      start_row,                   /* Start row              */
  137.                      start_col,                   /* Start column           */
  138.                      end_row  ,                   /* End row                */
  139.                      end_col  ,                   /* End column             */
  140.                      sav_par  ,
  141.                      buffer   ,
  142.                      &bios  );
  143.             set_curs (start_txt++,start_col + 3,&bios);
  144.             write_str(fil_blk[0],attr->txt,&bios);
  145.             write_str(sys->s_txt,attr->txt,&bios);
  146.             last_box=page;
  147.             break;
  148.         }
  149.     case SCR_FNF:                                 /* File not found         */
  150.         {
  151.             set_curs (start_txt++ ,start_col + 3, &bios);
  152.             write_str(fil_blk[1],attr->txt,&bios);
  153.             break;
  154.         }
  155.     case SCR_FOK:                                 /* File found okay        */
  156.         {
  157.             set_curs (start_txt++, start_col + 3, &bios);
  158.             write_str(fil_blk[2],attr->txt,&bios);
  159.             if (sys)
  160.             {
  161.                 strcpy(info,"File size = ");
  162.                 ltoa (sys->s_byt,&info[12],10);
  163.                 set_curs (start_txt-1, start_col + 38, &bios);
  164.                 write_str(info,attr->txt,&bios);
  165.             }
  166.             break;
  167.         }
  168.     case SCR_STA:                                 /* Status block           */
  169.         {
  170.             page      = 2;
  171.             attr      = attribute + page;
  172.             index     = box_loc + (page * boxes);
  173.             start_row = *index++;
  174.             start_col = *index++;
  175.             end_row   = *index++;
  176.             end_col   = *index;
  177.             start_txt = start_row + 1;
  178.             set_box (page     ,                   /* Page                   */
  179.                      attr->win,                   /* Screen attribute       */
  180.                      start_row,                   /* Start row              */
  181.                      start_col,                   /* Start column           */
  182.                      end_row  ,                   /* End row                */
  183.                      end_col  ,                   /* End column             */
  184.                      sav_par  ,
  185.                      buffer   ,
  186.                      &bios  );
  187.  
  188.             for (i=0; i<6; i++)
  189.             {
  190.                 set_curs (start_txt+i,start_col + 4,&bios);
  191.                 write_str(sta_blk[i],attr->txt,&bios);
  192.             }
  193.             last_box = page;
  194.             break;
  195.         }
  196.    case SCR_FRN:                                  /* File renamed           */
  197.         {
  198.             set_curs (start_txt++, start_col + 3, &bios);
  199.             write_str(fil_blk[3],attr->txt,&bios);
  200.             write_str(sys->s_txt,attr->txt,&bios);
  201.             break;
  202.         }
  203.    case SCR_FCR:                                  /* File created           */
  204.         {
  205.             set_curs (start_txt++, start_col + 3, &bios);
  206.             write_str(fil_blk[4],attr->txt,&bios);
  207.             break;
  208.         }
  209.    case SCR_SYR:                                  /* Sync receive           */
  210.         {
  211.             start_txt = start_row + 1;
  212.             set_curs (start_txt + 5, start_col + 4, &bios);
  213.             write_str(sta_blk[6],attr->txt | 0x8000 ,&bios);
  214.             break;
  215.         }
  216.    case SCR_SYT:                                  /* Sync transmit          */
  217.         {
  218.             start_txt = start_row + 1;
  219.             set_curs (start_txt + 5, start_col + 4, &bios);
  220.             write_str(sta_blk[7],attr->txt | 0x8000,& bios);
  221.             break;
  222.         }
  223.     case SCR_SYS:
  224.         {
  225.             itoa(sys->s_blk,info,10);             /* Block number           */
  226.             set_curs (start_txt, start_col + 15, &bios);
  227.             write_str(info,attr->txt,& bios);
  228.             memset(info,0x20,0x08);               /* Fixed length string    */
  229.             info[0x07] = 0x00;                    /* Set a null             */
  230.             itoa(sys->s_len,info,10);             /* Block length           */
  231.             *(strchr(info,0x00)) = 0x20;          /* Fill in the NULL       */
  232.             set_curs (start_txt + 1, start_col + 15, &bios);
  233.             write_str(info,attr->txt, &bios);
  234.             ltoa(sys->s_byt,info,10);             /* Total bytes            */
  235.             set_curs (start_txt + 2, start_col + 15, &bios);
  236.             write_str(info,attr->txt, &bios);
  237.             memset(info,0x20,0x08);               /* Fixed length string    */
  238.             info[0x07] = 0x00;                    /* Set a null             */
  239.             itoa(sys->s_cps,info,10);             /* Speed, cps             */
  240.             *(strchr(info,0x00)) = 0x20;          /* Fill in the NULL       */
  241.             set_curs (start_txt + 3, start_col + 15, &bios);
  242.             write_str(info,attr->txt, &bios);
  243.         }                                         /* Fall through, no break */
  244.     case SCR_FLG:
  245.         {
  246.             set_curs (start_txt + 4, start_col + 15, &bios);
  247.             write_str(sys->s_sta,attr->txt, &bios);
  248.             break;
  249.         }
  250.     case SCR_END:
  251.         {
  252.             do
  253.             {
  254.                 end_box (last_box, sav_par, buffer, &bios);
  255.             }   while (last_box--);
  256.             restore_curs(&bios);
  257.             break;
  258.         }
  259.     }
  260.     return;
  261. }
  262. /****************************************************************************/
  263. /*  Save screen contents in a buffer obtained from _malloc. Write a border  */
  264. /*  and screen attributes to saved screen location.  Record the address of  */
  265. /*  the buffer so the screen contents may be restored. Global *buffer[] is  */
  266. /*  used to save the pointers.                                              */
  267. /****************************************************************************/
  268. void set_box (short page ,                        /* Box number             */
  269.              word screen,                         /* Screen attribute       */
  270.              short start_row,                     /* Start row of border    */
  271.              short start_col,                     /* Start column of border */
  272.              short end_row,                       /* End row of border      */
  273.              short end_col,                       /* End column of border   */
  274.              register short *sav_par,
  275.              register short *buffer[],
  276.              union REGS *bios)
  277. {
  278.     word putscr;
  279.     short sav_col;
  280.     short sav_row;
  281.     short row;
  282.     short col;
  283.     buffer[page] = (short *)
  284.                    allocate_memory (              /* Get pointer to memory  */
  285.                    ((end_row - start_row)
  286.                   * (end_col - start_col))
  287.                   *  sizeof (short) );
  288.     if (!buffer[page])
  289.         return;
  290.  
  291.     get_curs(bios);                               /* Get cursor position    */
  292.     sav_row    = (short) bios->h.dh;              /* Save cursor row        */
  293.     sav_col    = (short) bios->h.dl;              /* Save cursor column     */
  294.     sav_par   += (page * param);                  /* Calculate index once   */
  295.     *sav_par++ = screen;
  296.     *sav_par++ = start_row;
  297.     *sav_par++ = start_col;
  298.     *sav_par++ = end_row;
  299.     *sav_par++ = end_col;
  300.     *sav_par++ = sav_row;
  301.     *sav_par   = sav_col;
  302.  
  303.     for (row = start_row; row < end_row; row++)
  304.     {
  305.         for (col = start_col; col < end_col; col++)
  306.         {
  307.             set_curs (row,col,bios);                   /* Set cursor pos    */
  308.             *buffer[page]++ = get_char_atr(bios);      /* Save char/attr    */
  309.             putscr = screen | 0x20;                    /* default (else)    */
  310.             if (row == start_row || row == end_row-1)  /* Top and bottom    */
  311.                 putscr = screen | 205;
  312.             if (col == start_col || col == end_col-1)  /* Right and left    */
  313.                 putscr = screen | 186;
  314.             if (row == start_row && col == start_col)  /* NW corner         */
  315.                 putscr = screen | 201;
  316.             if (row == start_row && col == end_col-1)  /* NE corner         */
  317.                 putscr = screen | 187;
  318.             if (row == end_row-1 && col == start_col)  /* SW corner         */
  319.                 putscr = screen | 200;
  320.             if (row == end_row -1 && col == end_col-1) /* SE corner         */
  321.                 putscr = screen | 188;
  322.             set_char_atr (putscr,bios);                /* Write to screen   */
  323.         }
  324.     }
  325.     return;
  326. }
  327. /****************************************************************************/
  328. /*  Restore the screen contents saved in memory pointed to by *buffer[].    */
  329. /****************************************************************************/
  330. void end_box (short page,
  331.               register short *sav_par,
  332.               register short *buffer[],
  333.               union REGS  *bios)
  334. {
  335.     short start_row;
  336.     short start_col;
  337.     short end_row;
  338.     short end_col;
  339.     short row;
  340.     short col;
  341.     short sav_row;
  342.     short sav_col;
  343.  
  344.     sav_par  += (page * param);                   /* Calculate index once   */
  345.     sav_par++;                                    /* Bypass screen entry    */
  346.     start_row = *sav_par++;
  347.     start_col = *sav_par++;
  348.     end_row   = *sav_par++;
  349.     end_col   = *sav_par++;
  350.     sav_row   = *sav_par++;
  351.     sav_col   = *sav_par;
  352.  
  353.     buffer[page] -= ( (end_row - start_row)       /* Get buffer start       */
  354.                * (end_col - start_col) );
  355.  
  356.     for (row = start_row; row < end_row; row++)
  357.     {
  358.         for (col = start_col; col < end_col; col++)
  359.         {
  360.             set_curs (row,col,bios);              /* Set cursor pos         */
  361.             set_char_atr(*buffer[page]++,bios);   /* Restore screen         */
  362.         }
  363.     }
  364.     free(buffer[page]);                           /* Free allocated memory  */
  365.     set_curs (sav_row,sav_col,bios);              /* Restore cursor         */
  366.     return;
  367. }
  368. /****************************************************************************/
  369. /*                      Set Cursor to row, column                           */
  370. /****************************************************************************/
  371. void set_curs (short row, short col, register union REGS *bios)
  372. {
  373.     bios->h.ah=(byte) 0x02;                       /* Set cursor function    */
  374.     bios->h.dh=(byte) row;                        /* Row                    */
  375.     bios->h.dl=(byte) col;                        /* Column                 */
  376.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  377.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  378.     return;
  379. }
  380. /****************************************************************************/
  381. /*                          Get the cursor position                         */
  382. /****************************************************************************/
  383. void get_curs(register union REGS *bios)
  384. {
  385.     bios->h.ah=(byte) 0x03;                       /* Get cursor function    */
  386.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  387.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  388.     return;
  389. }
  390. /****************************************************************************/
  391. /*                           Turn off the cursor                            */
  392. /****************************************************************************/
  393. void kill_curs(register union REGS *bios)
  394. {
  395.     bios->h.ah=(byte) 0x01;                       /* Set cursor function    */
  396.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  397.     bios->x.cx=(word) 0xFFFF;                     /* Kill the cursor        */
  398.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  399.     return;
  400. }
  401. /****************************************************************************/
  402. /*                         Restore the cursor type                          */
  403. /****************************************************************************/
  404. void restore_curs(register union REGS *bios)
  405. {
  406.     bios->h.ah=(byte) 0x01;                       /* Get cursor function    */
  407.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  408.     bios->x.cx=(word) 0x0607;                     /* Restore the cursor     */
  409.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  410.     return;
  411. }
  412. /****************************************************************************/
  413. /*                    Write char /attr at cursor position                   */
  414. /****************************************************************************/
  415. void set_char_atr(word atr_chr, register union REGS *bios)
  416. {
  417.     bios->h.ah=(byte) 0x09;                       /* Write char function    */
  418.     bios->h.al=(byte) atr_chr;                    /* Character              */
  419.     bios->h.bl=(byte) (atr_chr >> 8);             /* Attribute              */
  420.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  421.     bios->x.cx=1;                                 /* One character          */
  422.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  423.     return;
  424. }
  425. /****************************************************************************/
  426. /*                   Get char /attr at cursor position                      */
  427. /****************************************************************************/
  428. word get_char_atr(register union REGS *bios)
  429. {
  430.     bios->h.ah=(byte) 0x08;                       /* Read char function     */
  431.     bios->h.bh=(byte) 0x00;                       /* Page 0                 */
  432.     int86(VIDEO, bios, bios);                     /* Use for in and out     */
  433.     return (bios->x.ax);                          /* Its in the AX regis    */
  434.  }
  435. /****************************************************************************/
  436. /*         Write a string with attributes at current position               */
  437. /****************************************************************************/
  438. void write_str(register char *string, word atr, register union REGS *bios)
  439. {
  440.     short row;
  441.     short col;
  442.     while (*string)                               /* Until the NULL         */
  443.     {
  444.         set_char_atr(atr|(short)*string++, bios); /* Write char and attr    */
  445.         get_curs(bios);                           /* Get cursor position    */
  446.         row = (short) bios->h.dh;                 /* Get row                */
  447.         col = (short) bios->h.dl;                 /* Get column             */
  448.         if (col > 69)                             /* Protect window         */
  449.             break;
  450.         set_curs (row,++col,bios);                /* Set column             */
  451.     }
  452.     return;
  453. }
  454. /****************************************************************************/
  455. /*    Substitutes for the enormous _puts (char *) runtime module            */
  456. /*                                                                          */
  457. /****************************************************************************/
  458. int puts(register const char *string)
  459. {
  460.     union REGS bios;
  461.     while (*string)
  462.     {
  463.         bios.h.al=(byte) *string++;               /* Character to print     */
  464.         bios.h.ah=(byte) 0x0E;                    /* Dumb terminal function */
  465.         bios.h.bh=(byte) 0x00;                    /* Page 0                 */
  466.         int86(VIDEO, &bios, &bios);               /* Use for in and out     */
  467.     }
  468.     bios.h.al=(byte) 0x0D;                        /* Character to print     */
  469.     bios.h.ah=(byte) 0x0E;                        /* Dumb terminal function */
  470.     bios.h.bh=(byte) 0x00;                        /* Page 0                 */
  471.     int86(VIDEO, &bios, &bios);                   /* Use for in and out     */
  472.  
  473.     bios.h.al=(byte) 0x0A;                        /* Character to print     */
  474.     bios.h.ah=(byte) 0x0E;                        /* Dumb terminal function */
  475.     bios.h.bh=(byte) 0x00;                        /* Page 0                 */
  476.     int86(VIDEO, &bios, &bios);                   /* Use for in and out     */
  477.     return(0);
  478. }
  479. /****************************************************************************/
  480. /*                          Print the 'Usage' prompt.                       */
  481. /*    Made complicated so we don't have to use the ENORMOUS _printf()       */
  482. /*    library file.  This saves a lot of space!                             */
  483. /*                                                                          */
  484. /****************************************************************************/
  485. void disp()
  486. {
  487.     puts(signon[2]);
  488.     puts(info);
  489.     return;
  490. }
  491. /****************************************************************************/
  492. /*                       E N D  O F  M O D U L E                            */
  493. /****************************************************************************/
  494.