home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 16 / 16.iso / w / w055 / 4.ddi / SOURCES.LIF / SYSTEM.PEL < prev    next >
Encoding:
Text File  |  1990-09-27  |  9.2 KB  |  352 lines

  1. # $Header:   P:/source/ppee/macros/system.pev   1.34   24 Sep 1990 11:58:48   ericj  $
  2.  
  3. ##############################################################################
  4. #
  5. #           Sage Software - POLYTRON Division
  6. #             1700 NW 167th Place
  7. #               Beaverton, OR 97006
  8. #
  9. #   Copyright 1990, Sage Software, Inc.
  10. #
  11. #   Permission is hereby granted for licensed users of Sage Professional
  12. #   Editor and PolyAwk to copy and modify this source code for their own
  13. #   personal use.  These derivative works may be distributed only to other
  14. #   licensed Sage Professional Editor and PolyAwk users.  All other usage
  15. #   is prohibited without express written permission from Sage Software.
  16. #
  17. ##############################################################################
  18.  
  19. #### $Workfile:   system.pel  $: support for dos_buffer, etc.
  20.  
  21.  
  22. local    DOS_BUFFER_NAME = "DOS WINDOW"        # dos buffer buffer_name
  23. local    DOS_FILE_NAME    = "_doswin.$$$";    # temporary dos buffer filename
  24. global    dosScreenInitialized = 0;        # signifies presence of dos screen
  25.  
  26. ## create a buffer and capture interactive output from dos_window
  27. #    No buffer created if running under OS/2
  28. #
  29. global function dos_buffer( command ) {
  30.     local    width    = display_width - 8;
  31.     local    height  = display_height - 8;
  32.     local    dosBuffer;
  33.     local    saveBuffer = current_buffer;
  34.     local    saveWindow = current_window;
  35.     local    dosFileName;
  36.     local    iconified = 0;
  37.     local    one_window;
  38.     local    isdiag = 0+dialog_window;
  39.  
  40.     # regular dos_window if OS/2
  41.     if (os_name == "OS/2")
  42.         return system( command );
  43.  
  44.     # make dialog window visible
  45.     toggle_dialog( 1 );
  46.  
  47.     if (dosScreenInitialized) {
  48.         # already initialized?
  49.         if (!find_dos_window()) {
  50.             # no, create the new window.
  51.             current_window = create_window( 0, 0,    \
  52.                 display_width,            \
  53.                 display_height - 1,        \
  54.                 WINDOW_SYSTEM + WINDOW_NOBORDER + DOS_WINDOW_FLAG );
  55.             color_text = 14;
  56.         }
  57.     } else {
  58.         # is there only one window on the screen?
  59.         # if yes, split it, otherwise, use next_window
  60.         #
  61.         one_window = (next_window() == saveWindow);
  62.         assign_current_window( saveWindow );
  63.  
  64.         if (one_window) {
  65.             create_tiled_window(45,1);
  66.         }
  67.     }
  68.  
  69.     # search through the buffer list for the temporary dos_window buffer
  70.     if (find_dos_buffer()) {
  71.         # found it, make it current
  72.         dosBuffer = current_buffer;
  73.         current_buffer = saveBuffer;
  74.     } else {
  75.         #
  76.         # create the dos window buffer with 
  77.         #    NO_UNDO_HISTORY | SYSTEM_BUFFER
  78.         dosBuffer = create_buffer( DOS_BUFFER_NAME, "", 9 + DOS_BUFFER_FLAG );
  79.     }
  80.  
  81.     # try to find the temporary dos_window window.
  82.     if (!find_dos_window()) {
  83.         window_flags = or(window_flags, DOS_WINDOW_FLAG);
  84.     }
  85.  
  86.     current_buffer = dosBuffer;
  87.  
  88.     attach_window_buffer( current_window, current_buffer );
  89.  
  90.     # if the window was iconified, remember this and restore later.
  91.     if ( !and( window_flags, WINDOW_ZOOM )){
  92.         iconified = 1;
  93.         restore_window();
  94.     }
  95.  
  96.     goto_buffer_bottom();    # reposition cursor at end of buffer
  97.     goto_eol();
  98.     saveVisibles( 1 )
  99.  
  100.     display_update()
  101.  
  102.     if ((dosFileName = buildpath( DOS_FILE_NAME ))) {
  103.         write_buffer( dosFileName );
  104.         if (!command)
  105.             message( "To return to Sage Professional Editor, type \"EXIT\"." )
  106.         dos_window( command ">>& " dosFileName );
  107.  
  108.         delete_buffer();
  109.  
  110.         #
  111.         # create the dos window buffer with 
  112.         #    NO_UNDO_HISTORY | SYSTEM_BUFFER
  113.         #
  114.         current_buffer = create_buffer( DOS_BUFFER_NAME, dosFileName, 9 + DOS_BUFFER_FLAG );
  115.         goto_buffer_bottom();
  116.         goto_eol();
  117.         saveVisibles( 0 )
  118.         display_update();
  119.         unlink( dosFileName );
  120.         #
  121.         # restore previous window setting and buffer
  122.         if (iconified || command) {
  123.             if (iconified)
  124.                 collapse_window();
  125.             assign_current_window( saveWindow );
  126.             current_buffer = saveBuffer;
  127.         }
  128.         display_redraw();
  129.         message( "" );
  130.     } else {
  131.         saveVisibles( 0 )    # restore visible attributes.
  132.         message( "Illegal output filename." );
  133.     }
  134.  
  135.     if (dosScreenInitialized) {
  136.         # bury window if it's the background window
  137.         bury_window( );
  138.     }
  139.     toggle_dialog( isdiag );
  140. }
  141.  
  142. ## Set a flag to create a background window which is the entire 
  143. #    screen to hold any dos output.
  144. #
  145. global function init_dos_screen() {
  146.     dosScreenInitialized = 1
  147. }
  148.  
  149.  
  150. local old_visible_end_buffer;
  151. local old_visible_newlines;
  152. local old_visible_spaces;
  153. local old_visible_tabs;
  154. local old_window_flags;
  155.  
  156. ## save/restore current visible settings and reset to blanks
  157. #
  158. local function saveVisibles( saving ) {
  159.     if (saving) {
  160.         old_visible_end_buffer    = visible_end_buffer;
  161.         old_visible_newlines    = visible_newlines;
  162.         old_visible_spaces    = visible_spaces;
  163.         old_visible_tabs    = visible_tabs;
  164.         old_window_flags    = window_flags;
  165.         visible_end_buffer    = "";
  166.         visible_newlines    = " ";
  167.         visible_spaces        = " ";
  168.         visible_tabs        = " ";
  169.     } else {
  170.         visible_end_buffer    = old_visible_end_buffer;
  171.         visible_newlines    = old_visible_newlines;
  172.         visible_spaces        = old_visible_spaces;
  173.         visible_tabs        = old_visible_tabs;
  174.         window_flags        = old_window_flags;
  175.     }
  176. }
  177.  
  178. ## search through the list of windows until one is found with the 
  179. #    DOS_WINDOW_FLAG bit set in window_flags
  180. #
  181. #    Returns: TRUE if found
  182. #         FALSE if not found
  183. global function find_dos_window() {
  184.     local    saveWindow = current_window
  185.  
  186.     do {
  187.         if( and( window_flags, DOS_WINDOW_FLAG ))
  188.             return TRUE;
  189.     } while (next_window("",1) != saveWindow);
  190.     return FALSE;
  191. }
  192.  
  193. ## search through the list of buffer until one is found with the 
  194. #    DOS_BUFFER_FLAG bit set in buffer_flags
  195. #
  196. #    Returns: TRUE if found
  197. #         FALSE if not found
  198. global function find_dos_buffer()
  199. {
  200.     local    saveBuffer = current_buffer
  201.  
  202.     do {
  203.         if (and(buffer_flags, DOS_BUFFER_FLAG))
  204.             return TRUE;
  205.     } while (next_buffer( "", 1 ) != saveBuffer);
  206.     return FALSE;
  207. }
  208.  
  209.  
  210. ## system_window_command
  211. #
  212. # Invoke a DOS command and display the output in a window centered
  213. # on the screen.
  214. #
  215. # Direct the output to the filename specified in dosFileName.
  216. # If dosFileName is not given, create a temp name and use that.
  217. #
  218. local    arg_array
  219.  
  220. global function system_window_command( command, dosFileName ) {
  221.     local    prevWindow = current_window
  222.     local    removeTempFile
  223.     local    status
  224.  
  225.     if ( !command ) {
  226.         return -1
  227.     }
  228.  
  229.     dosFileName = buildpath( dosFileName )
  230.  
  231.     # under OS/2, just run the command
  232.     if ( os_name == "OS/2" ) {
  233.         if ( dosFileName ){
  234.             command = command " >>&" dosFileName
  235.         }
  236.         return system( command )
  237.     }
  238.  
  239.     # get file name to use for DOS buffer
  240.     if ( !dosFileName ) {
  241.         if ( !(dosFileName = create_temp_name()) ) {
  242.             return -1
  243.         }
  244.         removeTempFile = TRUE
  245.     }
  246.  
  247.     # if not enough memory for even a reduced window, quit now
  248.     if ( !init_system_factory_window()) {
  249.         if ( removeTempFile ) {
  250.             unlink( dosFileName )
  251.         }
  252.         return -1
  253.     }
  254.     display_update()
  255.  
  256.     # It is possible for the following invocation of dos_window()
  257.     # not to return.  This happens whenever the current 
  258.     # sageedit.ae file is removed or changed by the system 
  259.     # command.  To avoid leaving the "factory window" on the 
  260.     # screen, we attach an event handler to remove it for us.
  261.     #
  262.     # It is still conceivable that the clean-up would not occur, 
  263.     # i.e. if both this function and the event handler function 
  264.     # are changed by the system command.  To reduce the 
  265.     # likelihood of that occurence, we keep the two functions in 
  266.     # different files.
  267.  
  268.     arg_array[ 1 ] = current_window
  269.     arg_array[ 2 ] = prevWindow
  270.     arg_array[ 3 ] = dosFileName
  271.     arg_array[ 4 ] = removeTempFile
  272.     attach_cleanup_handler( "display_dos_buffer" )
  273.  
  274.     # do the system command in a window
  275.     status = dos_window( command " >& " dosFileName )
  276.  
  277.     # If the event handler installed above did not get invoked, it
  278.     # must have been changed by the system command.  Do here what
  279.     # the event handler would have done had it been invoked.
  280.  
  281.     if ( arg_array ) {
  282.         display_dos_buffer()
  283.     }
  284.  
  285.     return status
  286. }
  287.  
  288. ## display dos_buffer output - second half of system_window_command()
  289. #
  290. global function display_dos_buffer() {
  291.  
  292.     local    isdiag        = 0+dialog_window
  293.     local    current_window    = arg_array[ 1 ]
  294.     local    prevWindow    = arg_array[ 2 ]
  295.     local    dosFileName    = arg_array[ 3 ]
  296.     local    removeTempFile    = arg_array[ 4 ]
  297.  
  298.     delete arg_array
  299.  
  300.     # create the dos window buffer
  301.     current_buffer =                     \
  302.         create_buffer( "", dosFileName,         \
  303.             BUFFER_SYSTEM + BUFFER_NO_UNDO )
  304.  
  305.     attach_window_buffer( current_window, current_buffer )
  306.  
  307.     goto_buffer_bottom()
  308.  
  309.     toggle_dialog( 1 )
  310.      confirm( "Press <Enter> to continue.", "" )
  311.      toggle_dialog( isdiag )
  312.      message( "" )
  313.  
  314.     delete_buffer()
  315.     delete_window()
  316.     if ( prevWindow ) {
  317.         current_window = prevWindow
  318.     }
  319.  
  320.     if ( removeTempFile ) {
  321.         unlink( dosFileName )
  322.     }
  323. }
  324.  
  325. # build the window used by system_window_command()
  326. local function init_system_factory_window() {
  327.     local    prevWindow = current_window
  328.     local    x,y, x1,y1
  329.  
  330.     x = 3
  331.     y = 8
  332.     x1 = display_width - (x*2)
  333.     y1 = display_height - (y*2)
  334.  
  335.     current_window = create_factory_window( x, y, x1, y1, WINDOW_MENU0 )
  336.  
  337.     # if not enough memory - try once more with a smaller window
  338.     if ( current_window == prevWindow ) {
  339.         y1 = 6
  340.         current_window = create_factory_window( x, y, x1, y1, WINDOW_MENU0 )
  341.         if ( current_window == prevWindow ) {
  342.             warning( "unable to create window" )
  343.             return 0
  344.         }
  345.     }
  346.  
  347.     window_cursor_x = 0
  348.     window_cursor_y = 0
  349.  
  350.     return TRUE
  351. }
  352.