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