grep.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 669 行 · 第 1/2 页
C
669 行
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
* DESCRIBE IT HERE!
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "posix.h"
#include <fcntl.h>
#include <assert.h>
#include "walloca.h"
#include "vi.h"
#include "keys.h"
#include "rxsupp.h"
#include "win.h"
#ifdef __WIN__
#include "winvi.h"
#include "filelist.h"
#include "font.h"
#endif
#define MAX_DISP 60
static void fileGrep( char *, char **, int *, window_id );
static int fSearch( char *, char * );
static int eSearch( char *, char * );
static int doGREP( char * );
static regexp *cRx;
static char *sString;
static char *origString;
static char *cTable;
static bool isFgrep,caseIgn;
/*
* DoFGREP - do a fast grep
*/
int DoFGREP( char *dirlist, char *string, bool ci )
{
char table[256];
int i;
int rc;
origString = string;
AddString( &sString, string );
isFgrep = TRUE;
caseIgn = ci;
for( i=0;i<sizeof(table);i++ ) {
table[i] = i;
}
cTable = table;
if( ci ) {
for( i='A';i<='Z';i++) {
table[i] = i-'A'+'a';
}
strlwr( sString );
}
rc = doGREP( dirlist );
MemFree( sString );
return( rc );
} /* DoFGREP */
/*
* DoEGREP - do an extended grep
*/
int DoEGREP( char *dirlist, char *string )
{
int rc;
cRx = RegComp( string );
if( RegExpError ) {
return( RegExpError );
}
sString = string;
origString = string;
isFgrep = FALSE;
rc = doGREP( dirlist );
MemFree( cRx );
return( rc );
} /* DoEGREP */
static int getFile( char *fname )
{
char dir[ MAX_STR ];
char *dirptr, ch;
int rc;
NextWord1( fname, dir );
rc = EditFile( dir, FALSE );
if( rc != ERR_NO_ERR ) {
return( rc );
}
dirptr = dir;
if( isFgrep ) {
if( caseIgn ) {
ch = '~';
} else {
ch = '@';
}
dirptr += 2;
strcpy( dirptr, origString );
MakeExpressionNonRegular( dirptr );
dirptr--;
*dirptr = ch;
if( !EditFlags.Magic ) {
if( strchr( Majick, ch ) != NULL ) {
dirptr--;
*dirptr = '\\';
}
}
} else {
strcpy( dir, origString );
}
AddString2( &(FindHist.data[ FindHist.curr % FindHist.max ] ), dirptr );
FindHist.curr += 1;
ColorFind( dirptr, FINDFL_NOERROR );
return( rc );
}
static int initList( window_id w, char *dirlist, char **list )
{
char dir[ MAX_STR ];
int clist;
#ifdef __WIN__
InitGrepDialog();
#endif
/*
* go after each directory given on the command line
*/
clist = 0;
EditFlags.WatchForBreak = TRUE;
if( NextWord1( dirlist, dir ) <= 0 ) {
fileGrep( GrepDefault, list, &clist, w );
} else {
do {
if( IsDirectory( dir ) ) {
strcat( dir, FILE_SEP_STR );
strcat( dir, GrepDefault );
}
fileGrep( dir, list, &clist, w );
if( EditFlags.BreakPressed ) {
break;
}
} while( NextWord1( dirlist, dir ) > 0 );
}
if( EditFlags.BreakPressed ) {
#ifdef __WIN__
EditFlags.BreakPressed = FALSE;
#else
ClearBreak();
#endif
}
EditFlags.WatchForBreak = FALSE;
#ifdef __WIN__
FiniGrepDialog();
#endif
return( clist );
}
static void finiList( int clist, char **list )
{
MemFreeList( clist, list );
}
#ifdef __WIN__
static void getOneFile( HWND dlg, char **files, int *count, bool leave )
{
int i, j;
HWND list_box;
list_box = GetDlgItem( dlg, ID_FILE_LIST );
i = SendMessage( list_box, LB_GETCURSEL, 0, 0L );
getFile( files[ i ] );
if( leave ) {
EndDialog( dlg, ERR_NO_ERR );
} else {
/* remove it from the list box */
j = SendMessage( list_box, LB_DELETESTRING, i, 0L );
assert( (j+1) == (*count) );
if( SendMessage( list_box, LB_SETCURSEL, i, 0L ) == LB_ERR ) {
SendMessage( list_box, LB_SETCURSEL, i-1, 0L );
}
MemFree( files[ i ] );
for( j = i; j < *count; j++ ) {
files[ j ] = files[ j + 1 ];
}
(*count)--;
if( *count == 0 ) {
EndDialog( dlg, ERR_NO_ERR );
}
}
}
static void getAllFiles( HWND dlg, char **files, int *count )
{
int i;
for( i = 0; i < *count; i++ ) {
getFile( files[ i ] );
}
EndDialog( dlg, ERR_NO_ERR );
} /* editFiles */
BOOL WINEXP GrepListProc( HWND dlg, UINT msg, UINT wparam, LONG lparam )
{
static char **fileList;
static int fileCount;
HWND list_box;
char tmp[MAX_STR];
WORD cmd;
switch( msg ) {
case WM_INITDIALOG:
list_box = GetDlgItem( dlg, ID_FILE_LIST );
SendMessage( list_box, WM_SETFONT, (UINT)FontHandle( dirw_info.text.font ), 0L );
MySprintf( tmp,"Files Containing \"%s\"", sString );
SetWindowText( dlg, tmp );
fileList = (char **)MemAlloc( sizeof( char * ) * MAX_FILES );
fileCount = initList( list_box, (char *)lparam, fileList );
if( fileCount == 0 ) {
/* tell him that there are no matches and close down? */
Message1( "String \"%s\" not found", sString );
EndDialog( dlg, DO_NOT_CLEAR_MESSAGE_WINDOW );
} else {
SendMessage( list_box, LB_SETCURSEL, 0, 0L );
BringWindowToTop( dlg );
SetFocus( dlg );
}
break;
case WM_COMMAND:
cmd = LOWORD( wparam );
switch( cmd ) {
case ID_FILE_LIST:
if( GET_WM_COMMAND_CMD( wparam, lparam ) == LBN_DBLCLK ) {
getOneFile( dlg, fileList, &fileCount, TRUE );
}
break;
case ID_EDIT:
case ID_GOTO:
getOneFile( dlg, fileList, &fileCount, cmd == ID_GOTO );
break;
case ID_GETALL:
getAllFiles( dlg, fileList, &fileCount );
break;
case IDCANCEL:
EndDialog( dlg, ERR_NO_ERR );
return( TRUE );
}
break;
case WM_DESTROY:
finiList( fileCount, fileList );
break;
}
return( FALSE );
} /* GrepListProc */
static int doGREP( char *dirlist )
{
DLGPROC grep_proc;
int rc;
grep_proc = (DLGPROC) MakeProcInstance( (FARPROC) GrepListProc, InstanceHandle );
rc = DialogBoxParam( InstanceHandle, "GREPLIST", Root, grep_proc, (LONG)(LPVOID)dirlist );
FreeProcInstance( (FARPROC) grep_proc );
return( rc );
}
#else
/*
* doGREP - perform GREP on a specified file
*/
static int doGREP( char *dirlist )
{
int i,clist,rc,n=0;
window_id wn,optwin;
char **list;
window_info tw,wi;
int evlist[4] = { VI_KEY( F1 ), VI_KEY( F2 ), VI_KEY( F3 ), -1 };
int s,e,cnt;
bool show_lineno;
selectitem si;
/*
* prepare list array
*/
list = (char **) MemAlloc( sizeof( char *) * MAX_FILES );
/*
* create info. window
*/
i = NewWindow( &wn, dirw_info.x1, dirw_info.y1+4, dirw_info.x2,
dirw_info.y1+6, 1, dirw_info.border_color1, dirw_info.border_color2,
&dirw_info.text );
if( i ) {
MemFree( list );
return( i );
}
WindowTitle( wn, "File Being Searched" );
clist = initList( wn, dirlist, list );
/*
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?