home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / jed098-4.zip / JED / DOC / SCRIPT.TXT < prev    next >
Text File  |  1997-02-01  |  4KB  |  127 lines

  1. This document describes how to use JED in its non-interactive mode.  It
  2. assumes that the reader is reasonably familiar with S-Lang programming for
  3. JED. 
  4.  
  5. ---------------------------------------------------------------------------
  6. In a very real sense, JED is a text processing utility that operates in two
  7. modes: interactive mode and non-interactive or BATCH mode.  One example of
  8. using JED in batch mode is:
  9.  
  10.    jed -batch -l preparse.sl
  11.    
  12. JED will run non-interactively only if the command line parameter is
  13. `-batch'.   For example, 
  14.  
  15.     % jed -batch -f quit_jed
  16.     loading /usr/local/jed/lib/site.slc
  17.     loading /usr/local/jed/lib/os.sl
  18.     loading /usr1/users/davis/.jedrc
  19.  
  20. Notice that this is very noisy since it displays all the files that it is
  21. loading. There is a quieter non-interactive form that is specified by using
  22. `-script' instead of `-batch'.  The syntax for this parameter is:
  23.  
  24.    jed -script script-file [arg] ....
  25.  
  26. The `-script' parameter functions exactly like the `-l' (load file)
  27. parameter except that 
  28.  
  29.    1.  It runs in non-interactive mode.
  30.  
  31.    2.  The rest of the command line arguments (arg) are not parsed.  It is
  32.        up to the code in the script file to parse the remaining arguments if
  33.        they are present.
  34.        
  35.    3.  The user's .jedrc (jed.rc) file is NOT loaded.  To load it, add the
  36.        appropriate `evalfile' to the script.
  37.  
  38. It is best to provide an example, although silly.  This example script
  39. writes simply counts the number of lines in the files specified on the
  40. command line and writes the value on stdout.
  41.  
  42. ---------------------------------------------------------------------
  43. variable i = 3;
  44. variable count = 0;
  45. if (i == MAIN_ARGC)
  46.   {
  47.      message ("Usage: jed -script count-lines.sl file ...");
  48.      quit_jed ();
  49.   }
  50.   
  51. while (i < MAIN_ARGC)
  52. {
  53.    read_file (command_line_arg (i)); pop ();
  54.    eob ();
  55.    count += whatline ();
  56.    if (bolp ()) count--;
  57.    delbuf (whatbuf());
  58.    
  59.    i = i + 1;
  60. }
  61.  
  62. tt_send(string (count));
  63. quit_jed ();
  64. ----------------------------------------------------------------------
  65.  
  66. Note the following points:
  67.  
  68.    1. When the script file is loaded, the NEXT command line argument to 
  69.       be evaluated is 3.
  70.       
  71.    2. The global variable MAIN_ARGC contains the number of command line
  72.       arguments.  Since they are numbered from 0, the last one is number
  73.       MAIN_ARGC - 1.
  74.  
  75.    3. The function `command_line_arg' returns the specified argument.
  76.  
  77.    4. In the non-interactive mode, The function `tt_send' must be used to
  78.       write to stdout.  JED also uses `message' but this function writes to
  79.       stderr in batch mode. 
  80.  
  81.    5. `quit_jed' is called to exit the editor.  `exit_jed' could also be
  82.       used when it is necessary to run the exit hooks.
  83.       
  84. To exectute this script, type:
  85.  
  86.    % jed -script count-lines.sl 
  87.    
  88. It should display the usage.
  89.  
  90. Unix also provides a mechanism for making any script file an executable
  91. file.  The trick is to add the appropriate line to the top of the script and
  92. change the permissions on the script to make it executable.  In this case,
  93. do the following:
  94.  
  95.     1. Add:
  96.     
  97.          #!/usr/local/bin/jed -script
  98.       
  99.        to the top of the file.  Some operating systems put a limit on the
  100.        number of characters for this line.  It might be necessary to create
  101.        a symbolic link from, say /bin/jed to the place where jed is kept.
  102.        
  103.     2. At the unix prompt, enter:  chmod +x count-lines.sl
  104.  
  105. As a final example, consider a script file that may be used in DOS batch
  106. files to replace strings in files, e.g., to replace all occurances of `x'
  107. with `y' in `C:\windows\win.ini'.  If such a script file is called
  108. `change.sl', it could be used as:
  109.  
  110.     jed -script change.sl C:\windows\win.ini x y
  111.     
  112. A script file that performs this task is:
  113.  
  114.    % change.sl
  115.    
  116.    if (MAIN_ARGC != 6)
  117.      {
  118.         message ("Usage: jed -script change.sl file old-string new-string");
  119.         quit_jed ();
  120.      }
  121.   
  122.    () = read_file (command_line_arg(3));
  123.    replace (command_line_arg (4), command_line_arg (5));
  124.    save_buffer ();
  125.    quit_jed ();
  126.  
  127.