home *** CD-ROM | disk | FTP | other *** search
- /* Systems Sciences Laboratory, Webster Research Center */
-
- static char *PROGRAM_information[] =
- {
- "Copyright (c) 1988 Xerox Corporation. All rights reserved.",
- "$Header$",
- "$Locker$"
- }
- ;
-
- /*
- * Copyright protection claimed includes all forms and matters of copyrightable
- * material and information now allowed by statutory or judicial lay or
- * herinafter granted, including without limitation, material generated from
- * the software programs which are displayed on the screen such as icons,
- * screen display looks, etc.
- */
- static int errornum;
- static int hitfilesize;
-
- #define MAXDISPLAY 256
- #define BLKSIZE 8192
- #define ESIZE 1024
-
- #define INIT register char *sp = instring;
- #define GETC() (*sp++)
- #define PEEKC() (*sp)
- #define UNGETC(c) (--sp)
- #define RETURN(c) return;
- #define ERROR(c) { errornum = c; return; }
-
- #include <regexp.h>
- #include <sys/file.h>
- #include <ctype.h>
- #include "xfilebrowser.h"
-
-
- static short recursion = 1;
- /* 1 no recursive search of directories
- 2 search one level of directories
- 3 search all levels of directories */
-
- static Widget searchshell = NULL;
- static Widget searchoption; /* option for sorting files */
- static Widget optioncaller;
- static Widget caseoption; /* togglewidget selecting case sensitive */
-
- static short disp_option = 0;
-
- static Boolean filefound = False;
- static char *searchbuf;
- static int blksize;
- static Boolean recursive = False;
- static XtState ignore_case = XtToggleOff; /* is grep case insensitive */
-
-
- extern char *re_comp();
-
-
-
-
- /*************************************
- ** build options dialog for search
- **************************************/
-
- check_search()
- {
- return disp_option;
- }
-
- static void DoApply()
- {
- recursion = XtOptionGetSelection(searchoption);
-
- ignore_case = GetToggle(caseoption);
-
- disp_option = 0;
- change_sensitive(optioncaller, TRUE);
- XtPopdown(searchshell);
- }
-
- static void DoCancel()
- {
- XtState toggle;
-
- /* reset the option dialog window */
- if ( !(recursion == XtOptionGetSelection(searchoption)) )
- /* sorting was modified by user */
- XtOptionSetSelection(searchoption, recursion);
-
- SetToggle(caseoption, ignore_case);
-
- disp_option = 0;
- change_sensitive(optioncaller, TRUE);
- XtPopdown(searchshell);
- }
-
- build_searchoptions()
- {
- Arg popargs[1];
- Widget listpane, listrow1, listrow2;
-
- static Arg paneargs[] = {
- { XtNallowResize, (XtArgVal)True },
- };
-
- static Arg toggleargs[] = {
- { XtNstate, (XtArgVal)XtToggleOff },
- };
-
- static Arg optionargs[] = {
- { XtNlabel, (XtArgVal)NULL },
- { XtNorientation,(XtArgVal)XtorientVertical },
- { XtNfromHoriz, (XtArgVal) NULL },
- { XtNfromVert, (XtArgVal) NULL },
- { XtNleft, (XtArgVal) XtChainLeft },
- { XtNright, (XtArgVal) XtChainLeft },
- { XtNtop, (XtArgVal) XtChainTop },
- { XtNbottom, (XtArgVal) XtChainTop }
-
- };
-
- XtSetArg( popargs[0], XtNborderWidth, 2 );
-
- searchshell = XtCreatePopupShell("searchshell",
- overrideShellWidgetClass,
- toplevel, popargs, XtNumber(popargs));
-
- listpane = XtCreateManagedWidget( "vpaned", vPanedWidgetClass,
- searchshell, paneargs , XtNumber(paneargs) );
- listrow1 = XtCreateManagedWidget("row1", boxWidgetClass,
- listpane, NULL,0);
- listrow2 = XtCreateManagedWidget("row2", boxWidgetClass,
- listpane, NULL,0);
- makeCommandButton(listrow1, "Apply", DoApply);
- makeCommandButton(listrow1, "Cancel", DoCancel);
-
- /* define option menu for searching directories */
- optionargs[0].value = (XtArgVal)"Select Search Option:";
- searchoption = XtCreateManagedWidget("searching", optionWidgetClass,
- listrow2, optionargs, XtNumber(optionargs));
- XtOptionAddOption(searchoption, "skip directories", TRUE);
- XtOptionAddOption(searchoption, "search next-level directory", FALSE);
- XtOptionAddOption(searchoption, "search all directories", FALSE);
-
- /* define toggle for selecting case sensitivity */
- caseoption = XtCreateManagedWidget("case insensitive search",
- toggleWidgetClass,
- listrow2, toggleargs, XtNumber(toggleargs));
-
- XtSetMappedWhenManaged(searchshell, FALSE);
- XtRealizeWidget(searchshell);
- }
-
-
- display_searchoptions(caller)
- Widget caller;
- {
- if (searchshell == NULL) {
- build_searchoptions();
- optioncaller = caller;
- }
-
- disp_option = 1;
- move_popup(searchshell, caller);
- change_sensitive(caller, FALSE);
- XtMapWidget(searchshell);
- XtPopup(searchshell, XtGrabNonexclusive);
- }
-
- /**********************************************
- ** build data file structure for grep files
- ***********************************************/
-
- free_hitfiles()
- {
- register int i;
- register LineElement *p, *tmp;
-
- if (hitfiles != (SearchElement **)NULL) {
- for (i = 0; i < numhitfiles; i++) {
- p = hitfiles[i]->lines;
- while (p != (LineElement *)NULL) {
- tmp = p->next;
- XtFree(p);
- p = tmp;
- }
- XtFree(hitfiles[i]);
- }
- numhitfiles = 0;
- }
- }
-
- LineElement *add_line(pos1, pos2, line, next)
- XtTextPosition pos1, pos2;
- int line;
- LineElement *next;
- {
- register LineElement *p;
-
- p = (LineElement *)XtMalloc(sizeof(LineElement));
- p->pa = pos1;
- p->pe = pos2;
- p->linenumber = line;
- p->next = next;
- return p;
- }
-
- add_file(fname, pos1, pos2, linenr)
- char *fname;
- XtTextPosition pos1, pos2;
- int linenr;
- {
- SearchElement *selem;
- if (numhitfiles == hitfilesize) {
- hitfilesize += (numfiles > 64) ? (2 * numfiles): 128;
- if (hitfiles != (SearchElement **)NULL)
- hitfiles = (SearchElement **)XtRealloc(hitfiles,
- hitfilesize * sizeof(SearchElement *));
- else
- hitfiles =
- (SearchElement **)XtMalloc(hitfilesize*sizeof(SearchElement *));
- }
-
- if ( numhitfiles <= 0 ||
- strcmp(fname, hitfiles[numhitfiles-1]->name) ) {
- selem = (SearchElement *)XtMalloc(sizeof(SearchElement));
- selem->pos1 = pos1;
- selem->pos2 = pos2;
- selem->lines = add_line(pos1, pos2, linenr, (LineElement *)NULL);
- strcpy(selem->name, fname);
- hitfiles[numhitfiles] = (SearchElement *)selem;
-
- numhitfiles++;
- }
- else {
- hitfiles[numhitfiles-1]->lines =
- add_line(pos1, pos2, linenr, hitfiles[numhitfiles-1]->lines);
- hitfiles[numhitfiles-1]->pos2 = pos2;
- }
- }
-
- disp_searchline(file, line, linenr)
- char *file, *line;
- int linenr;
- {
- XtTextBlock text;
- XtTextPosition pos;
-
- text.length = strlen(line);
- text.ptr = line;
- text.firstPos = 0;
- allowedit = 1;
-
- pos = (*grepsource->Scan)(grepsource, 0, XtstAll, XtsdRight, 1, 0);
-
- XtTextReplace(grepwidget, pos, pos, &text);
- allowedit = 0;
- add_file(file, pos, (pos + text.length + 1), linenr);
- }
-
- det_surround(file, start, end, linenr)
- char *file, *start, *end;
- int linenr;
- {
- int i, len;
- char tmpline[MAXDISPLAY + 1];
-
- sprintf(tmpline, "%s: ", file);
- len = MAXDISPLAY - strlen(tmpline) - 2;
- if ( (end - start) < len )
- strcat(tmpline, start);
- else if ( (i = loc2 - start) < len) {
- strncat(tmpline, start, i);
- tmpline[i] = '\0';
- }
- else {
- strncat(tmpline, loc1, len);
- tmpline[MAXDISPLAY - 2] = '\0';
- }
- strcat(tmpline, "\n");
- disp_searchline(file, tmpline, linenr);
- filefound = True;
- }
-
- int check_textfile(p)
- char *p;
- {
- switch (*(int *)p) {
- case 0177555:
- case 0177545:
- case 070707: return(-1);
- }
- switch (*(short *)(p+2)) {
- case 0407:
- case 0413: return(-1);
- }
-
- if (strncmp(p, "Interpress", 10) == 0) return(-1);
- else if (strncmp(p, "\037\036", 2) == 0) return(-1);
- else if (strncmp(p, "\377\037", 2) == 0) return(-1);
- else if (strncmp(p, "\037\235", 2) == 0) return(-1);
- return(0);
- }
-
- int search_file(filelist, index, expbuf, stop)
- struct afile **filelist;
- int index;
- char *expbuf;
- Boolean stop;
- {
- int fp, first = 1;
- int count = 0, i, j;
- int linenr = 0;
- char *p;
- char *q, *h;
- char *linebuf;
- struct stat statbuf;
-
- char *filename = filelist[index]->d_name;
-
- /* check file type */
- switch (filelist[index]->d_type) {
- case 'b':
- case 'c':
- case 's': return(-1);
- case 'd': /* handle directories */
- {
- struct afile **dirfiles;
- int numdirfiles, i;
-
- if ( (recursion == 1) ||
- (recursion == 2 && recursive) ) return(-1);
- recursive = True;
- setup_filepattern("*");
- if (prepare_list(filename, 0, &dirfiles, &numdirfiles,
- filename))
- return(-1);
- setup_filepattern(filepattern);
-
- for (i = 0; (i < numdirfiles && (!stop || !filefound) ); i++)
- search_file(dirfiles, i, expbuf, stop);
- free_direct(&dirfiles, &numdirfiles);
- recursive = False;
- return(0);
- }
- default: break;
- }
- /* check if file pattern matches */
- if (! re_exec(filename)) return(-1);
- if ( (fp = open(filename, O_RDONLY)) < 0) {
- disp_message("\nSearch: cannot open file %s", filename);
- return(-1);
- }
- if (fstat(fp, &statbuf) == -1) { close(fp); return(-1); }
- else if ( !(statbuf.st_mode & S_IFREG )) {
- disp_message("\nSearch: %s not regular file!", filename);
- close(fp);
- return(-1);
- }
-
- if (searchbuf == NULL) {
- if (statbuf.st_blksize > 0)
- blksize = statbuf.st_blksize;
- else blksize = BLKSIZE;
- searchbuf = (char *)XtMalloc(blksize+ (2*MAXDISPLAY));
- }
- p = searchbuf;
- while ( (i = read(fp, p, blksize)) > 0) {
- if (first) {
- /* check if file can be scanned */
- if (check_textfile(p) == -1) {
- disp_message("\nSearch: %s does not contain text!",
- filename);
- close(fp);
- return(-1);
- }
- first = 0;
- }
-
- if (count == 0) {
- while (*p == '\n') { i--; p++; linenr++; }
- if (i == 0) continue; else count = i;
- }
- else count += i;
- NEXT:
- linebuf = p; p++; j = 1;
- while ( (j < min(MAXDISPLAY, count)) && *p != '\n' )
- { p++; j++; }
- if ( j < MAXDISPLAY && *p != '\n' ) {
- q = searchbuf; h = linebuf; j = 1;
- while (j++ <= count) *q++ = *h++;
- linebuf = searchbuf;
- p = linebuf + count;
- continue;
- }
-
- if (*p == '\n') linenr++;
- *p = '\0';
- if (step(linebuf, expbuf))
- det_surround(filename, linebuf, p, linenr);
- count -= j + 1;
- if (count == 0) { p = searchbuf; continue; }
- else {
- p++;
- while (*p == '\n' && count > 0) { p++; count--; linenr++; }
- }
- if (count > 0) goto NEXT; else { p = searchbuf; continue; }
- }
-
- if (count > 0) {
- *(p+1) = '\0';
- if (step(linebuf, expbuf))
- det_surround(filename, linebuf, p, linenr);
- }
- close(fp);
- return(0);
- }
-
- int examine_files(fstart, fend, stop)
- int fstart, fend;
- Boolean stop;
- {
- int i;
- char expbuf[ESIZE+1];
- char *greppattern;
- char *setup_search();
-
- greppattern = setup_search(searchpattern);
- errornum = 0;
- compile(greppattern, expbuf, &expbuf[ESIZE], '\0');
- XtFree(greppattern);
- if (errornum != 0) {
- switch (errornum) {
- case 50:
- disp_message("\nSearch: Regular expression overflow");
- break;
- case 49:
- disp_message("\nSearch: [ ] imbalance");
- break;
- case 46:
- disp_message("\nSearch: First number exceeds second in \\{ \\}");
- break;
- case 45:
- disp_message("\nSearch: } expected after \\");
- break;
- case 44:
- disp_message("\nSearch: More than 2 numbers given in \\{ \\}");
- break;
- case 43:
- disp_message("\nSearch: Too many \\(");
- break;
- case 42:
- disp_message("\nSearch: \\( \\) imbalance");
- break;
- case 36:
- disp_message("\nSearch: Illegal or missing delimiter");
- break;
-
- default:
- disp_message("\nSearch: search pattern failure %d", errornum);
- }
- return(-1);
- }
-
- setup_filepattern(filepattern);
- filefound = False;
- for (i = fstart; (i <= fend && (!stop || !filefound) ); i++)
- search_file(files, i, expbuf, stop);
- return(0);
- }
-
- /**********************************************
- ** search utility routines
- ***********************************************/
-
- int setup_filepattern(pattern)
- char *pattern;
- {
- char tmppattern[64];
- char *error;
-
- setup_pattern(pattern, tmppattern);
- if (error = re_comp(tmppattern)) {
- disp_message("\nsearch pattern for files %s?", error);
- return(-1);
- }
- return(0);
- }
-
- char *setup_search(s)
- char *s;
- {
- char *target, *src = s, *dest;
- char c;
-
- if (ignore_case == XtToggleOff) {
- target = XtMalloc(strlen(s) + 1);
- strcpy(target, s);
- }
- else {
- /* allocate maximum memory */
- target = XtMalloc(4 * strlen(s) + 1);
- dest = target;
-
- while (c = *src++) {
- if (isalpha(c)) {
- *dest++ = '[';
- *dest++ = c;
- *dest++ = ',';
-
- if (islower(c)) *dest++ = toupper(c);
- else *dest++ = tolower(c);
-
- *dest++ = ']';
- }
- else *dest++ = c;
- }
- *dest = '\0';
- }
- return(target);
- }
-
- int select_searchfile(start, end, line)
- int start, end;
- int *line;
- {
- int i = 0;
- register LineElement *p;
-
- while (i < numhitfiles && hitfiles[i]->pos1 <= start) i++;
- if ( (hitfiles[i-1]->pos2 + 1) < end) { *line = 0; return(-1); }
- else {
- p = hitfiles[i-1]->lines;
- while (p != (LineElement *)NULL && (p->pa > start) ) p = p->next;
-
- if (p == (LineElement *)NULL)
- *line = 0;
- else *line = p->linenumber;
-
- return(i-1);
- }
- }
-