home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / mtseek.zip / MTSEEK.CMD next >
OS/2 REXX Batch file  |  1995-10-02  |  5KB  |  158 lines

  1. /*******************************************************************/
  2. /* MTSEEK                                                          */
  3. /* rexx multi-threaded program using multiple processes and queues */
  4. /* to look through files for the occurance of some text            */
  5. /*                                                                 */ 
  6. /* program does following:                                         */
  7. /*    1. starts thread to find text in files                       */
  8. /*    2. as text if found, presents list to user                   */
  9. /*    3. user selects file to edit                                 */
  10. /*                                                                 */
  11. /* Command is:  mtseek <filespec> <text>                           */
  12. /* Author: S.A.C. Gould  1/95                                      */
  13. /*******************************************************************/
  14.  
  15.  
  16. /************************ MAIN **************************************/
  17.  
  18. main:
  19.   /* constants: */
  20.   QueueName         = "MTHREADQUEUE"
  21.   UserThread        = "userthrd.cmd"
  22.   ProcessThread       = "procthrd.cmd"
  23.   ProcOvrSem        = "\MTHREAD.SEM" /* name of file which is semaphore */
  24.   EndQueue          = "ENDQUEUE"
  25.  
  26.   /* Get Arguments */
  27.   parse arg filenames searchtext
  28.   if searchtext = "" then call Help
  29.  
  30.   /* Initialize functions */
  31.   call rxfuncadd "sysloadfuncs", "rexxutil", "sysloadfuncs"
  32.   call sysloadfuncs
  33.  
  34.   /* If queue is already created, then delete it */
  35.   rc = rxqueue('delete', QueueName)
  36.  
  37.   /* Create the queue. Should check to see if name is same */
  38.   /* then set input/output to the queue */
  39.   name = rxqueue('create', QueueName)
  40.   rc = rxqueue("set", QueueName)
  41.  
  42.   /* Use a semephore to note that queue is active */
  43.   call CreateSemaphore ProcOvrSem
  44.  
  45.   /* Now start thread to get user input */
  46.   '@start "User Input" /c /f' UserThread QueueName
  47.  
  48.   /* start thread to perform search */
  49.   '@detach' ProcessThread QueueName ProcOvrSem filenames searchtext 
  50.  
  51.   filenames.0 = 0 /* no files yet */
  52.  
  53.   /* say greeting */
  54.   'cls'
  55.   say "************** Text Search by Multitasking ***********"
  56.   say 
  57.   say "              ***** Display Window *******"
  58.   say 
  59.  
  60.   /* Process queue until completed */
  61.   do while message <> EndQueue
  62.  
  63.     do while queued() = 0   /* is there anything in the queue? */
  64.       call syssleep 1     /* don't keep constantly checking */
  65.       end /* do */
  66.  
  67.     /* get messages */
  68.     do while queued() <> 0
  69.       pull message  
  70.       if message = EndQueue then
  71.         signal cleanup
  72.       call ParseMessage message
  73.       end
  74.     end /* do while not end of queue*/
  75.  
  76. Cleanup:
  77.     /* now destroy the queue and delete semaphore */
  78.   call DeleteSemaphore ProcOvrSem
  79.   rc = rxqueue('delete', QueueName)
  80.  
  81.   exit
  82.  
  83.  
  84.  
  85. /********************** PROCEDURES ****************************************/
  86.  
  87. /* Creates zero length file to represent semaphore */
  88. CreateSemaphore: Procedure
  89.   semname = arg(1)
  90.   ok = stream(semname, 'c', 'open write')
  91.   ok = stream(semname, 'c', 'close')  
  92.   return
  93.  
  94. /* Destroys semaphore by deleting file */
  95. DeleteSemaphore: Procedure
  96.   semname = arg(1)
  97. /*  '@del' semname '> NUL'   */
  98.   ok = SysFileDelete(semname)
  99.   return
  100.  
  101.  
  102. /* processes the message based on the sender */
  103. ParseMessage: Procedure,
  104.   expose filenames.
  105.   message = arg(1)
  106.  
  107.   /* need to parse message into sender handle and information */
  108.   parse var message handle information
  109.  
  110.   select /* check to see who sent data */
  111.     when handle = "USER" then do 
  112.       call ProcessUser information
  113.       end
  114.  
  115.     when handle = "FILE" then do   /* add filesnames to list */
  116.       call ProcessFile information
  117.       end
  118.  
  119.      otherwise
  120.      say message
  121.    end /* select */
  122.    
  123.   return /* ParseMessage */
  124.  
  125. /* adds a filename to the list of files containing the text */
  126. ProcessFile: Procedure,
  127.   expose filenames.
  128.   data = arg(1)
  129.  
  130.   n = filenames.0 + 1
  131.   filenames.0 = n
  132.  
  133.   parse var data filename linenum rest
  134.   filenames.n.fname = filename  /* save file name */
  135.   filenames.n.lnum = linenum    /* save line with first occurance of text */
  136.  
  137.   say "File:"format(n,3) filename
  138.  
  139.   return /* processfile */
  140.  
  141.  
  142. /* selects the user selected file and starts another
  143.    process by starting the editor */
  144. ProcessUser: Procedure,
  145.   expose filenames.
  146.   
  147.   fnum = arg(1)
  148.  
  149.   '@start epm.exe' filenames.fnum.fname "'"filenames.fnum.lnum"'"
  150.  
  151.   return /* processfile */
  152.  
  153.  
  154. Help: Procedure
  155.   say "Command:   mtseek filename(s) search_text"
  156.   say "example: mtseek *.* january"
  157.   exit
  158.