home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
278.lha
/
RegexLibrary_v1.0
/
tinygrep.c
< prev
Wrap
C/C++ Source or Header
|
1989-08-06
|
3KB
|
183 lines
/*
* Test file for the GNU regular expression package.
* Edwin Hoogerbeets 18/07/89
*
* This file may be copied and distributed under the GNU Public
* Licence. See the comment at the top of regex.c for details.
*
* Adapted from Elib by Jim Mackraz, mklib by Edwin Hoogerbeets, and the
* GNU regular expression package by the Free Software Foundation.
*/
#include "regex.h"
#include <libraries/dos.h>
#include <exec/memory.h>
#define BUFSIZ 8*1024
extern char *AllocMem();
extern char *OpenLibrary();
extern void searchfile();
void puts(str)
char *str;
{
Write(Output(),str,strlen(str));
}
char *inputbuffer;
long inputcounter = -1, inputsize, eof;
main (argc, argv)
int argc;
char **argv;
{
struct re_pattern_buffer buf;
int index;
long input;
if ( argc < 2 || *argv[1] == '\0' ) {
puts("Usage: ");
puts(argv[0]);
puts("regular_expression filename [...]\n");
exit(1);
}
if ( (RegexBase =
(struct RegexBase *) OpenLibrary("regex.library",0L)) == NULL) {
puts("Could not open regex.library");
exit(2);
}
inputbuffer = AllocMem(BUFSIZ,MEMF_PUBLIC|MEMF_CLEAR);
re_initialize_buffer(&buf,__Upcase);
re_compile_pattern(argv[1], strlen(argv[1]), &buf, RE_SYNTAX_GREP);
if ( argc < 3 ) {
input = Input();
searchfile(input,&buf,"");
} else {
register int printnames = (argc - 2) > 1 ? 1 : 0;
char name[256];
index = 2;
while ( index < argc ) {
if ( !(input = Open(argv[index],MODE_OLDFILE)) ) {
puts("Could not open ");
puts(argv[index]);
puts("\n");
} else {
if ( printnames ) {
strcpy(name,argv[index]);
strcat(name,": ");
} else {
name[0] = '\0';
}
inputcounter = -1;
searchfile(input,&buf, name);
Close(input);
}
++index;
}
}
FreeMem(inputbuffer,BUFSIZ);
re_terminate_buffer(&buf);
CloseLibrary(RegexBase);
}
long newbuffer(f)
long f;
{
inputsize = Read(f,inputbuffer,BUFSIZ);
inputcounter = 0;
return( inputsize );
}
char
getc(f)
long f;
{
char result = inputbuffer[inputcounter++];
if ( inputsize == 0 ) {
return('\0');
}
if ( inputcounter >= inputsize ) {
newbuffer(f);
}
return(result);
}
char *
fgets(buf,len,f)
char *buf;
long len, f;
{
long counter = 0;
char c;
if ( inputcounter < 0 ) {
if ( !newbuffer(f) ) {
return(NULL);
}
}
while ( (c = getc(f)) != '\0' && counter < len ) {
buf[counter++] = c;
if ( c == '\n' ) {
if ( counter < len ) {
buf[counter] = '\0';
}
return(buf);
}
}
if ( c == '\0' ) {
return(NULL);
}
if ( counter < len ) {
buf[counter] = '\0';
}
return(buf);
}
void searchfile(f,buf,name)
long f;
struct re_pattern_buffer *buf;
char *name;
{
char buffer[256];
int result;
/* struct re_registers foo; */
while ( fgets(buffer,256L,f) != NULL ) {
if ( (result = re_search(buf, buffer, strlen(buffer), 0L,
strlen(buffer)-2, NULL)) >= 0 ) {
puts(name);
puts(buffer);
}
}
}
_abort()
{
puts("DON'T TOUCH THAT KEY, SILLY!");
}