home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / MACROS.PEL < prev    next >
Text File  |  1995-03-31  |  12KB  |  485 lines

  1. # $Header:   P:\source\wmacros\macros.pev   1.32   31 Mar 1995 15:41:32   NOBLE  $
  2.  
  3. ##############################################################################
  4. #
  5. #       Compuware Corporation
  6. #         31440 Northwestern Highway
  7. #           Farmington Hills, Michigan 48334-2564
  8. #
  9. #   This source code listing contains information that is
  10. #   proprietary to Compuware Corporation and may not be copied
  11. #   duplicated, translated, transmitted, stored, retrieved
  12. #   or in any manner or by any method conveyed or disclosed
  13. #   to a third party or parties without express written
  14. #   permission from Compuware Corporation.
  15. #
  16. #  
  17. ##############################################################################
  18.  
  19. #### $Workfile:   macros.pel  $: Support for Playback/Record and Macros
  20.  
  21. # reserved global variables
  22.  
  23. global  recording    = -1
  24. global  recordString = ""
  25. global  playingback  = 0
  26.  
  27. ## record and playback
  28.  
  29. global  currentRecordString = ""
  30. local    pausedRecordString  = ""
  31. local    recordingPaused     = 0
  32.  
  33. function pause_recording()
  34. {
  35.    if ( !recording && recordingPaused )
  36.    {
  37.       record()
  38.       recordingPaused = 0
  39.       recording       = 1
  40.    }
  41.    else if ( recording == 1 )
  42.    {
  43.       pausedRecordString = pausedRecordString record()
  44.       recordingPaused    = 1
  45.       recording          = 0
  46.    }
  47. }
  48.  
  49. function record_macro()
  50. {
  51.    local   str
  52.  
  53.    if ( playingback )
  54.       return
  55.  
  56.    if ( recordingPaused )
  57.    {
  58.       message( "Keystroke macro defined." )
  59.       recordingPaused = 0
  60.       str = ""
  61.    }
  62.    else
  63.       str = record_key()
  64.  
  65.    currentRecordString = pausedRecordString str
  66.    pausedRecordString  = ""
  67. }
  68.  
  69. function playback_macro()
  70. {
  71.    playingback = 1
  72.    if ( recordingPaused )
  73.       message( "Playback while recording." )
  74.    else
  75.    {
  76.       if ( currentRecordString )
  77.          playback( currentRecordString )
  78.       else
  79.          playback()
  80.    }
  81.    playingback = 0
  82. }
  83.  
  84. function load_key_file(append, fn)
  85. {
  86.    local   fid
  87.    local   ch
  88.  
  89.    if (!fn)
  90.         fn = prompt_history( "RECORD", "Keystroke macro file: ", "", 1, 1, "load_key_file_prompt")
  91.  
  92.    if ( fn )
  93.    {
  94.       if ( (fid = fopen( fn, 0 )) > 0 )
  95.       {
  96.          if (!append)
  97.             currentRecordString = ""
  98.  
  99.          while ( (ch = fgetc(fid)) >= 0 )
  100.          {
  101.             currentRecordString = currentRecordString chr( ch )
  102.          }
  103.          fclose( fid )
  104.       }
  105.       else
  106.          notify( "Unable to open: " fn )
  107.    }
  108. }
  109.  
  110. function write_key_file(fn)
  111. {
  112.    local   fid
  113.  
  114.    if ( currentRecordString )
  115.    {
  116.       if (!fn)
  117.          fn = prompt_history( "RECORD", "Save keystrokes as: ", "", 1, 1, "write_key_file_prompt")
  118.  
  119.       if ( fn )
  120.       {
  121.          if ( (fid = fopen( fn, 1 )) > 0 )
  122.          {
  123.             fprintf( fid, currentRecordString )
  124.             fclose( fid )
  125.          }
  126.          else
  127.             notify( "Unable to open: " fn )
  128.       }
  129.    }
  130.    else
  131.       notify( "No keystroke macro to save." )
  132. }
  133.  
  134. global function record_key()
  135. {
  136.    if (playingback)
  137.            return
  138.  
  139.    if( recording > 0 )
  140.       message("Keystroke macro defined.")
  141.    else
  142.    {
  143.       if (recording != -1)
  144.       {
  145.          if (tolower(confirm("Overwrite existing keystroke macro [yn]? ",
  146.                              "YyNn")) != "y")
  147.          {
  148.             return
  149.          }
  150.       }
  151.       message("Defining keystroke macro.")
  152.       recording = 0
  153.    }
  154.  
  155.    recording = !recording
  156.  
  157.    return record()
  158. }
  159.  
  160. ## functions supporting invoke_function:
  161.  
  162. local   instr
  163. local   inpos
  164. global  extended_quotes = FALSE # when true, Allows args that have characters
  165.                                 # before a quote (such as C'string').
  166.  
  167. # Get a backslash escape sequence from instr at inpos.
  168. # Return the single character string.
  169. #
  170. local function getescape()
  171. {
  172.    local acc, i, ch
  173.  
  174.    ch = substr(instr,inpos,1)
  175.    inpos++
  176.    if (ch >= "0" && ch <= "7")
  177.    {
  178.       # Octal escape sequence.
  179.       acc = ch + 0
  180.       for (i=0; i<3; i++)
  181.       {
  182.          ch = substr(instr,inpos,1)
  183.          if (ch >= "0" && ch <= "7")
  184.          {
  185.             inpos++
  186.             acc = acc * 8 + ch
  187.          }
  188.       }
  189.       return( chr(acc) )
  190.    }
  191.    else if (ch == "x")
  192.    {
  193.       # Hex escape sequence.
  194.       for (i=0; i<2; i++)
  195.       {
  196.          ch = toupper( substr(instr,inpos+i,1) )
  197.          if ( !((ch >= "0" && ch <= "9") || (ch >= "A" && ch <= "F")))
  198.             break
  199.       }
  200.       ch = chr( 0 + ("0x" substr(instr,inpos,i)) )
  201.       inpos += i
  202.       return(ch)
  203.    }
  204.    else if ((i = index("btfnr\\",ch)))
  205.       return( substr("\b\t\f\n\r\\",i,1) )
  206.    else
  207.       return(ch)
  208. }
  209.  
  210. # Break the string into arguments and return an array of the arguments,
  211. # where arg[1] is the first argument, arg[2] the second, etc.
  212. # Allow spaces in quoted arguments.
  213. #
  214. # 11/09/93 - MLW did it : Extened quote_args to allow either double (") or
  215. #                         single (') quotes to be used.  The other quote
  216. #                         char can be embedded in the string.
  217. #                         (I.E. 'This "is" ok!')
  218. #
  219. local function quote_args(lp)
  220. {
  221.    local i, j, ch, outbuf, quote
  222.    local fun_args
  223.    local fun_argc = 0
  224.  
  225.    instr = lp
  226.    inpos = 1
  227.    while (1)
  228.    {
  229.       ch = substr(instr,inpos,1)
  230.       if ( !ch )
  231.          break
  232.       else if ( index(" \t",ch) )
  233.       {
  234.          inpos++
  235.          continue
  236.       }
  237.       else if ( ch == "\"" || ch == "'")
  238.       {
  239.          inpos++
  240.          outbuf = ""
  241.          quote  = ch
  242.          while (1)
  243.          {
  244.             ch = substr(instr,inpos++,1)
  245.  
  246.             if ( ch == "" )
  247.                error("improperly quoted string: %s", instr)
  248.             else if ( ch == quote )
  249.                break
  250.             else if (ch == "\\")
  251.                outbuf = outbuf getescape()
  252.             else
  253.                outbuf = outbuf ch
  254.          }
  255.          fun_args[ ++fun_argc ] = outbuf
  256.       }
  257.       else
  258.       {
  259.          if ((i = cindex(substr(instr,inpos)," \t")))
  260.          {
  261.             if (extended_quotes)
  262.             {
  263.                outbuf = substr(instr,inpos,i-1)
  264.  
  265.                if ((j = cindex(outbuf, "\"'")))
  266.                {
  267.                   outbuf = substr(outbuf, 1, j)
  268.                   inpos += j
  269.                   quote  = substr(instr,inpos-1,1)
  270.  
  271.                   while (1)
  272.                   {
  273.                      ch = substr(instr,inpos++,1)
  274.  
  275.                      if ( ch == "" )
  276.                         error("improperly quoted string: %s", instr)
  277.                      else
  278.                      {
  279.                         outbuf = outbuf ch
  280.  
  281.                         if ( ch == quote )
  282.                            break
  283.                      }
  284.                   }
  285.                }
  286.                else
  287.                   inpos += i
  288.  
  289.                fun_args[++fun_argc] = outbuf
  290.             }
  291.             else
  292.             {
  293.                fun_args[++fun_argc] = substr(instr,inpos,i-1)
  294.                inpos += i
  295.             }
  296.          }
  297.          else
  298.          {
  299.             fun_args[++fun_argc] = substr(instr,inpos)
  300.             break
  301.          }
  302.          #outbuf = ""
  303.          #do {
  304.          #   outbuf = outbuf ch
  305.          #   ch     = substr(instr,inpos++,1)
  306.          #} while (ch && ch != " " && ch != "\t")
  307.       }
  308.    }
  309.    return( fun_args )
  310. }
  311.  
  312. # This is like function_id but allows quoted arguments in both
  313. # function calls and variable assignments.
  314. # Backslash sequences and spaces are processed in the quoted strings.
  315. #
  316. # 11/09/93 - MLW did it : See quote_args()
  317. #
  318. global function quoted_function_id(mac)
  319. {
  320.    local   args
  321.    if ( match( mac, /^[^\"=]+[ \t]*=[ \t]*\"/ ))
  322.    {
  323.       # It is an assignment of a quoted string to a variable,
  324.       # for example:  var = "this and that"
  325.       args = quote_args( substr(mac,RLENGTH) )
  326.       # Concatenate the "var =" part with the first quoted argument.
  327.       # Trim spaces between the "=" and the quoted string
  328.       # so that the argument does not begin with extraneous spaces.
  329.       return function_id( trim(substr(mac,1,RLENGTH-1)) args[1] )
  330.    }
  331.    else if (cindex(mac,"\"'") && match(mac, /[ \t]/))
  332.    {
  333.       # It is a function call with quoted arguments.
  334.       args = quote_args( substr(mac,RSTART+1) )
  335.       return function_id( substr(mac,1,RSTART-1), args )
  336.    }
  337.    else
  338.    {
  339.       # No arguments.
  340.       return function_id( mac )
  341.    }
  342. }
  343.  
  344. global function display_array( val, name )
  345. {
  346.    local   typo
  347.    local   i, v
  348.    local   dhArray
  349.    local   width = 30
  350.  
  351.    # check argument type
  352.    if ( typeof( val ) != "array" )
  353.       return
  354.  
  355.    if ( name )
  356.       name = name " - " 
  357.  
  358.    # create a dialog box for the list
  359.    dhArray = create_selection_dialog( name "Array Contents", width )
  360.    set_dialog_item( dhArray, DI_OK, DAC_HIDE)
  361.  
  362.    # insert the array contents
  363.    for ( i in val )
  364.    {
  365.       v    = val[i]
  366.       typo = typeof( v )
  367.  
  368.       if ( 0 + v )
  369.          v = sprintf( "%-12s  (hex 0x%08X)", v, v )
  370.  
  371.       if( typo == "string" )
  372.          v = sprintf( "%-12s: \"%s\"", i, v )
  373.       else if ( typo == "int" )
  374.          v = sprintf( "%-12s: %s", i, v )
  375.       else
  376.          v = sprintf( "%-12s: %s, type: %s", i, v, typo )
  377.  
  378.       set_dialog_item( dhArray, IDL_LIST, DAC_ADD_ITEM, v )
  379.    }
  380.    begin_dialog(dhArray)
  381.    delete_dialog(dhArray)
  382.  
  383.    # clear the message line
  384.    message( "" )
  385. }
  386.  
  387. global function invoke_function( mac )
  388. {
  389.    local   typo, val, fid
  390.  
  391.    if( !mac )
  392.         mac = ltrim( prompt_history("XMACRO","Command: ","", 1, 1, "command" ) );
  393.  
  394.    if (mac)
  395.    {
  396.       add_prompt_history("XMACRO", mac)
  397.       message( "" )
  398.       if ( tolower( mac ) ~ /^help[ \t]/)
  399.       {
  400.          mac = ltrim( trim( tolower( substr( mac, 5 ) )))
  401.          if (mac)
  402.             display_help_item( mac )
  403.          else
  404.             gui_help_index()
  405.       }
  406.       else if ( (mac ~ /^dos[ \t]/) ||  (mac ~ /^system[ \t]/) )
  407.       {
  408.          # special handling for "dos" and related commands:
  409.  
  410.          sub( /^dos[ \t]+/, "", mac )
  411.          sub( /^system[ \t]+/, "", mac )
  412.          sub( /[ \t]+$/, "", mac )
  413.  
  414.          if( match( mac, /^".*"$/ ))
  415.          {
  416.             # remove outer-most quotes, if any:
  417.             mac = substr( mac, 2, length( mac ) - 2 )
  418.          }
  419.          else if( match( mac, /^'.*'$/ ))
  420.          {
  421.             # remove outer-most quotes, if any:
  422.             mac = substr( mac, 2, length( mac ) - 2 )
  423.          }
  424.  
  425.          if(( val = system( mac )))
  426.             warning( "Command exited with %d", val )
  427.       }
  428.       else if (mac ~ /^filter[ \t]/ )
  429.       {
  430.          # special handling for "filter":
  431.          sub( /^filter[ \t]+/, "", mac )
  432.  
  433.          if( match( mac, /^".*"$/ ))
  434.          {
  435.             # remove outer-most quotes, if any:
  436.             mac = substr( mac, 2, length( mac ) - 2 )
  437.          }
  438.          else if( match( mac, /^'.*'$/ ))
  439.          {
  440.             # remove outer-most quotes, if any:
  441.             mac = substr( mac, 2, length( mac ) - 2 )
  442.          }
  443.  
  444.          filter( mac )
  445.       }
  446.       else if ( substr(mac,1,1) == "?" )
  447.       {
  448.          # special handling for variable inquiry:
  449.          mac = ltrim(substr(mac,2))
  450.          if( mac )
  451.          {
  452.             fid = quoted_function_id(mac)
  453.             if ( fid )
  454.             {
  455.                val = execute_function( fid )
  456.                display_update()
  457.                typo = typeof( val )
  458.                if ( typo == "array" )
  459.                {
  460.                   display_array( val )
  461.                   return
  462.                }
  463.                else if( 0 + val )
  464.                   val = sprintf( "%s  (hex 0x%08X)", val, val )
  465.  
  466.                else if( typo == "string" )
  467.                   val = "\"" val "\""
  468.  
  469.                notify( "%s == %s, type: %s", mac, val, typo )
  470.             }
  471.             else
  472.                warning( "\"" mac "\" is invalid" )
  473.          }
  474.       }
  475.       else
  476.       {
  477.          # normal F10 commands:
  478.          if ((fid = quoted_function_id(mac)))
  479.             execute_function( fid )
  480.          else
  481.             warning( "\"%s\" is invalid", mac )
  482.       }
  483.    }
  484. }
  485.