home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 6 File
/
06-File.zip
/
GRES2.ZIP
/
GRES.C
next >
Wrap
C/C++ Source or Header
|
1989-12-27
|
7KB
|
254 lines
/* gres - grep and substitute Author: Martin C. Atkins */
/* $Header: d:/rcs/D:/RCS/RCS/gres.c 1.1 89/12/27 03:54:15 RCA Exp $
* $Log: gres.c $
* Revision 1.1 89/12/27 03:54:15 RCA
* Initial revision
*
*/
/*
* globally search, and replace
*
* <-xtx-*>cc -o gres gres.c -lregexp
*/
/*
* From Minix to DOS to OS/2 by R. Ames
*/
/*
* This program was written by: Martin C. Atkins, University of
* York, Heslington, York. Y01 5DD England and is released into
* the public domain, on the condition that this comment is
* always included without alteration.
*/
#include <stdio.h>
#include "regexp.h"
#define MAXLINE (1024)
int status = 1;
/*
* char *usagemsg = "Usage: gres [-g] search replace [file
* ...]\n";
*/
char *progname;
int gflag = 0; /* != 0 => only do first substitution on
* line */
/*
* STRCHR, not INDEX extern char *index();
*/
FILE *fopen();
main(argc, argv)
int argc;
char *argv[];
{
regexp *exp;
char *repstr;
char **argp = &argv[1];
progname = argv[0];
if (*argp != 0 && argp[0][0] == '-' && argp[0][1] == 'g')
{
gflag = 1;
argp++, argc--;
}
if (argc < 3)
{
printf("▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\n");
printf("█ GRES (Global Regular Expression Substitute) $Author: RCA $ █\n");
printf("█ $Date: 89/12/27 03:54:15 $ $Revision: 1.1 $ █\n");
printf("█ Usage: GRES [-g] \"search\" \"replace\" <infile >outfile █\n");
printf("█ Purpose: Search by regular expression (like GREP) and █\n");
printf("█ substitute a string. The -g flag only changes the █\n");
printf("█ first occurrence per line. █\n");
printf("█ OS: DOS or OS/2. █\n");
printf("█ Credits: M.C. Atkins, H. Spencer █\n");
printf("▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n");
printf("- more -\n");
getch();
printf("▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\n");
printf("█ Expressions: █\n");
printf("█ 1. Any displayable character matches itself. █\n");
printf("█ 2. . (period) matches any character except line feed. █\n");
printf("█ 3. ^ (caret) matches the start of a line. █\n");
printf("█ 4. $ (dollar sign) matches the end of a line. █\n");
printf("█ 5. \\c matches character c (including period, etc). █\n");
printf("█ 6. [string] matches any characters in the string. █\n");
printf("█ 7. [^string] matches any characters EXCEPT in string. █\n");
printf("█ 8. [x-y] matches any characters between x and y. █\n");
printf("█ 9. pattern* matches any number of occurrences of █\n");
printf("█ pattern. █\n");
printf("█ Examples: █\n");
printf("█ Guild matches any occurrence of \"Guild\" █\n");
printf("█ ^$ matches any empty line █\n");
printf("█ ^A.*\\.$ matches any line beginning with A, ending █\n");
printf("█ with period. █\n");
printf("█ ^[A-Z]*$ matches any line containing only capital █\n");
printf("█ letters (or empty) █\n");
printf("█ █\n");
printf("▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n");
done(2);
}
if (argp[0][0] == '\0')
{
printf("gres: null match string is silly\n");
done(2);
}
if ((exp = regcomp(*argp++)) == NULL)
{
printf("gres: regcomp failed\n");
done(2);
}
repstr = *argp++;
if (*argp == 0)
process(stdin, exp, repstr);
else
while (*argp)
{
FILE *inf;
if (strcmp(*argp, "-") == 0)
process(stdin, exp, repstr);
else
{
if ((inf = fopen(*argp, "r")) == NULL)
{
printf("gres: Can't open ");
printf(*argp);
printf("\n");
status = 2;
} else
{
process(inf, exp, repstr);
fclose(inf);
}
}
argp++;
}
done(status);
}
/*
* This routine does the processing
*/
process(inf, exp, repstr)
FILE *inf;
regexp *exp;
char *repstr;
{
char ibuf[MAXLINE];
while (fgets(ibuf, MAXLINE, inf) != NULL)
{
/*
* char *cr = index(ibuf,'\n');
*/
char *cr = strchr(ibuf, '\n');
if (cr == 0)
printf("gres: Line broken\n");
else
*cr = '\0';
if (regexec(exp, ibuf, 1))
{
pline(exp, ibuf, repstr);
if (status != 2)
status = 0;
} else
printf("%s\n", ibuf);
}
}
void
regerror(s)
char *s;
{
printf("gres: ");
printf(s);
printf("\n");
done(2);
}
char *
getbuf(exp, repstr)
regexp *exp;
char *repstr;
{
char *malloc();
void free();
static bufsize = 0;
static char *buf = 0;
int guess = 10;
int ch;
while (*repstr)
{
switch (*repstr)
{
case '&':
guess += exp->endp[0] - exp->startp[0];
break;
case '\\':
if ((ch = *++repstr) < '0' || ch > '9')
guess += 2;
else
{
ch -= '0';
guess += exp->endp[ch] - exp->startp[ch];
}
break;
default:
guess++;
}
repstr++;
}
if (bufsize < guess)
{
if (buf != 0)
free((char *) buf);
buf = malloc(guess);
}
return buf;
}
pline(exp, ibuf, repstr)
regexp *exp;
char ibuf[];
char *repstr;
{
do
{
dosub(exp, ibuf, repstr);
ibuf = exp->endp[0];
if (*ibuf == '\0')
break;
if (ibuf == exp->startp[0])
putchar(*ibuf++);
} while (!gflag && regexec(exp, ibuf, 0));
printf("%s\n", ibuf);
}
/*
* print one subsitution
*/
dosub(exp, ibuf, repstr)
regexp *exp;
char ibuf[];
char *repstr;
{
char *buf = getbuf(exp, repstr);
char *end = exp->startp[0];
int ch = *end;
*end = '\0';
fputs(ibuf, stdout); /* output the initial part of
* line */
*end = ch;
regsub(exp, repstr, buf);
fputs(buf, stdout);
}
done(n)
int n;
{
/*
* _cleanup(); || flush stdio's internal buffers
* ||
*/
exit(n);
}