ems@prls.UUCP (Gordon Vickers) (02/04/86)
Brief discription: para.shar - prints the paragraph containing 'string' where a paragraph is defined as being a block of text bordered by blank lines, or eighteen lines ( which ever occurs first). Syntex: para string file Searches 'file' for 'string'. For each first occurance of 'string' in 'file', the paragraph is printed and the line containing the string is boardered by blank lines (better visibility). When string is found, user may; quit, search for next occurance, invole an editor. I have been using this for a few months now so there should be no bugs. It was compiled and tested on : VAX 11/750 running Ultrix 32 V1.1, VAX 11/750 running Ultrix 32 V1.0 and on a VAX 11/780 running 4.2BSD. Since I keep several rather large text files (notes, etc) , this has been a most helpful program for me. Hope you will enjoy it to. -------------- fold, spindle, or mutalate at will --(it's YOUR crt)------ #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # para.c # para.l # This archive created: Thu Dec 5 14:35:53 1985 export PATH; PATH=/bin:$PATH if test -f 'para.c' then echo shar: will not over-write existing file "'para.c'" else cat << \SHAR_EOF > 'para.c' #include <stdio.h> #include <strings.h> #include <ctype.h> #define LINESIZE 132 #define MAXLINES 18 /*********************************************************************** * para - print the paragraph from argv[2] that contains the string * * argv[1]. * **********************************************************************/ main(argc,argv) int argc; char **argv; { int j, len, lfind, mln, ret ; int i = 0, ln = 0, fln; char c; char more[LINESIZE], sin[LINESIZE]; char sout[MAXLINES][LINESIZE]; char fname[LINESIZE], findit[LINESIZE]; FILE *search; if ( 2 == 1 ) /* never executes but will reside with code. see strings(1) */ { printf(" "); printf(" Written by: Gordon P. Vickers,"); printf(" Signetics Corp."); printf(" Sunnyvale, Ca."); printf(" COPYRIGHT 1985 ALL RIGHTS RESERVED."); printf(" Reproduction for personnal , non commercial use is"); printf(" authorized to all. Profitable, promotional, and other"); printf(" commercial use requires written authorization by author."); printf(" "); } if (argc != 3) { printf("\nUSAGE: para string filename.\n\n"); exit(0); } sprintf(fname,"%s",argv[2]); sprintf(findit,"%s",argv[1]); lfind = strlen(findit); if ( (search = fopen(fname,"r")) <= NULL) { write(0,"PARA: ",6); perror(fname); exit(0); } while ( (fgets(sin,LINESIZE,search)) != NULL) { fln++; sprintf(sout[ln],"%s",sin); len = strlen(sin); /* do a walking comparision of argv[1] to sin */ j = 0; for ( i = 0; ( (i <= len) && (j != lfind) ); i++) j = (findit[j] == sin[i]) ? j + 1 : 0; if ( j == lfind ) /* found it */ { for ( i = 0; i < ln; i++) printf("%s", sout[i]); printf("\n%s\n", sout[ln]); /* make it easy to see */ /* now print remainder of paragraph or til MAXLINES */ mln = 0; while ( (fgets(sin,LINESIZE,search)) != NULL) { mln++; len = strlen(sin); ln++; printf("%s", sin); ; /* measure amount of white space at start of line */ for (i = 0; ((sin[i] != '\0') && (isspace(sin[i]))); i++); if ( (i == len) || (ln == MAXLINES) ) { printf(" M O R E --- e,ex v,vi q,quit else MORE ----\n"); system("stty raw -echo"); while ( (ret = read(0,&c,1)) < 1 ) ; system("stty -raw echo"); c = c & (char)0x7f; /* strip 8th bit */ switch (c) { case 'e': sprintf(more,"ex +%d %s",fln,fname); system(more); break; case 'v': sprintf(more,"vi +%d %s",fln,fname); system(more); break; case 'q': fclose(search); exit(0); default: break; } break; } } fln = fln + mln; } for (i = 0; ((sin[i] != '\0') && (isspace(sin[i]))); i++); ln = ( i == len) ? 0 : ln + 1; if ( ln >= MAXLINES - 3) { sprintf(sout[0],"%s",sout[ln - 2]); sprintf(sout[1],"%s",sout[ln - 1]); sprintf(sout[2],"%s",sout[ln]); ln = 3; } } fclose(search); exit(0); } SHAR_EOF chmod 640 'para.c' fi # end of overwriting check if test -f 'para.l' then echo shar: will not over-write existing file "'para.l'" else cat << \SHAR_EOF > 'para.l' PARA(LOCAL) NAME para - find the paragraph containing 'string'. SYNTAX para string filename DESCRIPTION Every paragraph within 'filename' containing 'string' is printed. The line containing 'string' will be seperated from the paragraph by newlines so it will be easier to see. After each printed paragraph, a "--- M O R E ---" prompt is issued, along with the options availiable to you ( 'e' invokes ex(1), 'v' invokes vi(1), 'q' to quit and the default action is to continue searching for subsequent occurances of 'string'). When one of the selected editor is invoked, it will start up on the line containing the found string. EXAMPLES para oneword searchfile Each line of 'searchfile' is scanned for the occurance of 'oneword'. If 'oneword' is found, then the entire paragraph containing it will be printed. Only the first occurance of 'oneword' within any single paragraph will be found. RESTRICTIONS Text bounded by lines of white space is defined as a paragraph. If a paragraph is more than 18 lines long, only that portion containing 'string' is printed. Calls are made to the obsoleted routine stty(3). The stty calls are used instead of ioctl(2) to insure easier porting and to reduce the size of the file (at the expense of some speed). AUTHOR Gordon P. Vickers , Sunnyvale, Ca. Copyright 1985 ALL RIGHTS RESERVED However, anyone may copy this and use the source for there own personal use. Reproduction in any form , for profitable purposes, promotional purposes, or for any other commercial use is exspressly forbidden except to those who have my written consent to do so. SHAR_EOF chmod 640 'para.l' fi # end of overwriting check # End of shell archive exit 0 -- Gordon P. Vickers Signetics, 811 E.Arques Sunnyvale, CA. 94086 I've hugged all of my cats today. 'I hav grate kuntempt fore any man who can't spel a word more than one way' - Ben Franklin