[comp.sys.handhelds] HP48 Download macro for BRIEF

williams@qualcomm.com (Paul Williamson) (04/06/91)

/*************************************************************************\
*                                                                         *
* HP-48SX Kermit Interface for BRIEF                                      *
*                                                                         *
* Copyright 1991 Paul Williamson, KB5MU; All Rights Reserved              *
* You may use this program for non-commercial purposes.                   *
* You may distribute copies, as long as no fee is charged and any         *
* modifications you make are clearly marked.                              *
*                                                                         *
* This file is written in CBRIEF, the C-like macro language used          *
* to customize BRIEF, a program editor.  I used BRIEF version 3.10        *
* to write and debug it.                                                  *
*                                                                         *
* It defines a single macro, kermit_download.  If you invoke the macro    *
* from inside a file containing an HP-48SX downloadable object (of any    *
* flavor), the file will be written out to disk (if necessary) and        *
* downloaded to your calculator immediately.  You don't have to leave     *
* the editor!  If you have a region of text within the file selected      *
* when you invoke kermit_download, the macro assumes that you want to     *
* use just the marked region as an HP-48SX object.  It will ask you for   *
* two names: one for the HP-48SX, and one for the PC.  You will probably  *
* want to accept the default PC name, which is just the HP-48SX name.     *
* The macro will then write the region to the PC file you specify, and    *
* download it to the HP-48SX file you specify.                            *
*                                                                         *
* The macro calls the regular Kermit program.  I tested with the version  *
* that calls itself "IBM-PC MS-DOS Kermit: 3.10 13 March 1991", from      *
* Columbia University.  This program must be on your DOS PATH somewhere.  *
* The macro also assumes that a special Kermit initialization file,       *
* named KERMHP48.INI, is somewhere on your path.  This file contains      *
* Kermit commands to configure Kermit's parameters to match the HP-48SX.  *
* My  KERMHP48.INI looks like this:                                       *
*       ;; configuration for talking to HP48SX                            *
*       set remote on                                                     *
*       set port 4                                                        *
*       set speed 9600                                                    *
*       pause 0         X  ; why do I need this?                                *
*                                                                         *
* When I download a file containing HP-48SX code, I always name it with   *
* an extension of ".48".  BRIEF can key on the file extension and load    *
* this macro automatically, so you'll always have one-keystroke Kermit    *
* transfers available from inside the editor.  Just add this to your      *
* initials macro:                                                         *
*     (macro .48                                                          *
*        (                                                                *
*           (autoload "hp48" "kermit_download")                           *
*           (keyboard_push)                                               *
*           (assign_to_key "<Ctrl-d>" "kermit_download")                  *
*           (use_local_keyboard (inq_keyboard))                           *
*           (keyboard_pop 1)                                              *
*        )                                                                *
*     )                                                                   *
* Assuming that you name this file HP48.CB, and compile it using CB, and  *
* place the resulting HP48.CM file in your MACROS directory, this will    *
* automatically set you up.  The assign_to_key command above assigns      *
* this macro to Ctrl-D (for download).  You can, of course, assign it to  *
* whatever key suits your fancy.                                          *
*                                                                         *
* I'd appreciate any comments you may have.  Bug reports are always       *
* welcome, but of course I can't promise to do anything about them.       *
*                                                                         *
* Paul Williamson, KB5MU                                                  *
* Internet: pwilliamson@drzeus.qualcomm.com                               *
* Compu$erve: 75265,367                                                   *
*                                                                         *
\*************************************************************************/

int kermit_download()
{
   string   command;             // command to send to kermit
   string   pc_fn;               // name of the current file on the PC
   string   hp_fn;               // name of the object to be sent to HP48
   int      ret_val;             // return value from Kermit
   int      old_msg_level = inq_msg_level();

   if (inq_marked())             // check to see if a region is marked
      {                          // If it is, we need to make a file of it.
      int      buf_id;
      int      old_buf = inq_buffer();       // save current buffer ID

      if (!get_parm(NULL, hp_fn, "HP48SX Name: "))
         return (-1);            // don't do anything if he won't answer
      if (!strlen(hp_fn))
         return(-1);             // or if he didn't give us a name.
      if (!get_parm(NULL, pc_fn, "PC filename: ", NULL, hp_fn))
         return(-1);             // same here.
      if (!strlen(pc_fn))
         return(-1);             // and here.

      buf_id = create_buffer("temp file", pc_fn, 1); // make a system buffer
      if (!buf_id)               // and make sure that worked.
         {
         error("%s is not a valid filename.");
         beep();
         return(-2);
         }

      /* I'd like to use transfer() here, so as not to stomp the scrap,
         but transfer doesn't grok column-marks.  I decided that copying
         by columns was more important than preserving the scrap. */

      copy();                    // copy marked region to scrap
      set_buffer(buf_id);
      paste();                   // and stick it in our system buffer

      set_msg_level(0);          // (let write error messages show thru)
      if (write_buffer() <= 0)   // write the good stuff out to a file
         {                       // making sure it worked.
         error("Couldn't write file %s.", pc_fn);
         beep();
         set_msg_level(old_msg_level);
         return (-2);
         }

      set_msg_level(old_msg_level);
      set_buffer(old_buf);             // and clean up the garbage
      delete_buffer(buf_id);     
      }
   else  // there was no region marked, so we can deal with the whole file
      {
      inq_names(pc_fn, NULL, hp_fn);   // get the name of the current file
                                       // pc_fn gets full path
      if (inq_modified())              // hp_fn gets just the filename
         {
         message("Writing %s...", pc_fn); // write it out if it's dirty
         set_msg_level(0);
         write_buffer();
         set_msg_level(old_msg_level);
         }
      }

   sprintf(command, "kermit -f kermhp48.ini send %s %s >&NUL",
               pc_fn, hp_fn);       // the command to download the file
                                    // KERMHP48.INI contains the parameters
   message("Running Kermit...");    // Keep the user informed.
   ret_val = dos(command, 0);       // Finally, run Kermit.
   switch (ret_val)                 // and let the user know what happened.
      {
      case 0:
         message("File transmitted OK.");
      case 1:
         error("File send failed!");
         beep();
      default:                      // No other error code should happen,
         error("Error code %d", ret_val); // but handle it anyway.
         beep();
      }
   return (ret_val);                // pass status back up, for other macros
}
/* end of kermit_download */