Over the years, to keep various e-mails and newsgroup postings, I’ve printed them out and kept them at home. I’ve thought about storing them as files on disk, but finding the information I need would not be too easy. I was looking for a utility that would search the files on disk, find files with from a search string, display the files, showing the lines where the search string was found.
While at SGI, we had a tool that did this and it was used heavily by the technical support teams.
Creating something similar on the QL could be difficult if building it from scratch. Being a Unix user for years, I know that it is easier to build a new tools by hooking together a number of existing tools. On the QL the Unix search utility GREP is available. It has options to report back the file name, the line number, and the line of text, where a search string has been found. Now I needed a way to view the information in such a way as to browse through it. HTML and an HTML browser was not quite the way to go.
Dilwyn Jones had written a text browser, that allows links to other files, but it does not allow links to specific lines with a file. After some badgering by myself and Darren Branaugh, Dilwyn has now added that feature to his VIEWER program (as of version 1.20).
So, with a tool to search files and a tool to display the results, all this is left to write is a program that links the two together. Listed below is that program.
Basically, here is what the program does. The user enters a search string. The program then calls GREP with the command line options to report back the name of the file, the line number on which the string is found and the line itself. The program searches all ‘txt’ files in a given directory. The results of the GREP are sent to a tempory file on RAM1. The program then reads in this file, creating proper links for VIEWER. Since VIEWER is not designed for lines greater than 80 characters, the line in which the string is found, is shortened to about 30 or so characters. The program then calls VIEWER with the links file as an argument. Using VIEWER the user can then browse through the results of the search, looking to find the right text file.
There is one possible major drawback with the way the program is written. Since the program has to wait for GREP to finish executing before it can continue, it uses EXEC_W instead of EXEC to execute the program. If there are other multitasking programs, this might interfere with them. It would be possible to write a polling routine to wait for GREP to finish, but would complicate the simple program. A more polished version would find a proper solution to the problem.
For those interested, GREP can be found in the C68 distribution and VIEWER is available off of Dilwyn Jones’ web page.
100 PRINT "Enter Search String"
110 INPUT search$
120 cmd_line$ = "-n "&search$&" *txt > ram1_temp_txt"
130 EXEC_W grep;cmd_line$
140 OPEN #4,ram1_temp_txt
150 OPEN_NEW #5,ram1_link_txt
160 REPeat loop
170 INPUT #4,in$
180 IF EOF(#4) THEN EXIT loop
190 x = ":" INSTR in$
200 file$ = in$( 1 TO x-1)
210 in$ = in$ (x+1 TO )
220 x = ":" INSTR in$
230 line_num$ = in$(1 TO x-1)
240 text$ = in$(x+1 TO )
250 x = search$ INSTR text$
260 length = LEN(text$)
270 IF x > 15 THEN
280 first = 15
290 ELSE
300 first = x-1
310 END IF
320 IF length > x+15 THEN
330 last = 15
340 ELSE
350 last = length
360 END IF
370 text$ = text$(x-first TO x+last)
380 PRINT #5,"<<";file$;"$";text$;"@";line_num$;">>"
390 END REPeat loop
400 CLOSE #4
410 CLOSE #5
420 DELETE ram1_temp_txt
430 EXEC_W flp1_viewer_rtm;"ram1_link_txt"