* sed now supports addresses (numeric or regexp, with negation) and

has an append command, thanks to Marco Pantaleoni <panta@prosa.it>
* Fixed dmesg.  It wasn't parsing its options (-n or -s) properly.
* Some cosmetic fixes to ls output formatting to make it behave more
    like GNU ls.
 -Erik
This commit is contained in:
Erik Andersen 1999-12-29 22:19:46 +00:00
parent a89910829c
commit 1266a13e1f
7 changed files with 571 additions and 202 deletions

View File

@ -17,6 +17,11 @@
them so they should now work as expected. them so they should now work as expected.
* New app: loadacm contributed by Peter Novodvorsky <petya@logic.ru> * New app: loadacm contributed by Peter Novodvorsky <petya@logic.ru>
for loading application character maps for working with Unicode fonts. for loading application character maps for working with Unicode fonts.
* sed now supports addresses (numeric or regexp, with negation) and
has an append command, thanks to Marco Pantaleoni <panta@prosa.it>
* Fixed dmesg. It wasn't parsing its options (-n or -s) properly.
* Some cosmetic fixes to ls output formatting to make it behave more
like GNU ls.
-Erik Andersen -Erik Andersen

View File

@ -193,11 +193,13 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
fputs(" ", stdout); fputs(" ", stdout);
#ifdef BB_FEATURE_LS_USERNAME #ifdef BB_FEATURE_LS_USERNAME
if (!(opts & DISP_NUMERIC)) { if (!(opts & DISP_NUMERIC)) {
scratch[0]='\0'; memset ( scratch, 0, sizeof (scratch));
my_getpwuid( scratch, info->st_uid); my_getpwuid( scratch, info->st_uid);
scratch[8]='\0'; if (*scratch) {
if (*scratch) fputs(scratch, stdout);
wr(scratch,8); if ( strlen( scratch) <= 8 )
wr(" ", 8-strlen( scratch));
}
else { else {
writenum((long) info->st_uid,(short)8); writenum((long) info->st_uid,(short)8);
fputs(" ", stdout); fputs(" ", stdout);
@ -208,20 +210,21 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
writenum((long) info->st_uid,(short)8); writenum((long) info->st_uid,(short)8);
fputs(" ", stdout); fputs(" ", stdout);
} }
tab(16);
#ifdef BB_FEATURE_LS_USERNAME #ifdef BB_FEATURE_LS_USERNAME
if (!(opts & DISP_NUMERIC)) { if (!(opts & DISP_NUMERIC)) {
scratch[0]='\0'; memset ( scratch, 0, sizeof (scratch));
my_getgrgid( scratch, info->st_gid); my_getgrgid( scratch, info->st_gid);
scratch[8]='\0'; if (*scratch) {
if (*scratch) fputs(scratch, stdout);
wr(scratch,8); if ( strlen( scratch) <= 8 )
wr(" ", 8-strlen( scratch));
}
else else
writenum((long) info->st_gid,(short)8); writenum((long) info->st_gid,(short)8);
} else } else
#endif #endif
writenum((long) info->st_gid,(short)8); writenum((long) info->st_gid,(short)8);
tab(17); //tab(26);
if (S_ISBLK(mode) || S_ISCHR(mode)) { if (S_ISBLK(mode) || S_ISCHR(mode)) {
writenum((long)MAJOR(info->st_rdev),(short)3); writenum((long)MAJOR(info->st_rdev),(short)3);
fputs(", ", stdout); fputs(", ", stdout);
@ -230,6 +233,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
else else
writenum((long)info->st_size,(short)8); writenum((long)info->st_size,(short)8);
fputs(" ", stdout); fputs(" ", stdout);
//tab(32);
#ifdef BB_FEATURE_LS_TIMESTAMPS #ifdef BB_FEATURE_LS_TIMESTAMPS
{ {
time_t cal; time_t cal;

18
dmesg.c
View File

@ -44,10 +44,15 @@ int dmesg_main( int argc, char** argv )
int level = 0; int level = 0;
int lastc; int lastc;
int cmd = 3; int cmd = 3;
int stopDoingThat;
argc--;
argv++;
/* Parse any options */ /* Parse any options */
while (argc && **argv == '-') { while (argc && **argv == '-') {
while (*++(*argv)) stopDoingThat = FALSE;
while (stopDoingThat == FALSE && *++(*argv)) {
switch (**argv) { switch (**argv) {
case 'c': case 'c':
cmd = 4; cmd = 4;
@ -57,19 +62,22 @@ int dmesg_main( int argc, char** argv )
if (--argc == 0) if (--argc == 0)
goto end; goto end;
level = atoi (*(++argv)); level = atoi (*(++argv));
--argc; if (--argc > 0)
++argv; ++argv;
stopDoingThat = TRUE;
break; break;
case 's': case 's':
if (--argc == 0) if (--argc == 0)
goto end; goto end;
bufsize = atoi (*(++argv)); bufsize = atoi (*(++argv));
--argc; if (--argc > 0)
++argv; ++argv;
stopDoingThat = TRUE;
break; break;
default: default:
goto end; goto end;
} }
}
} }
if (argc > 1) { if (argc > 1) {

View File

@ -5,6 +5,11 @@
* Copyright (C) 1999 by Lineo, inc. * Copyright (C) 1999 by Lineo, inc.
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* *
* Modifications for addresses and append command have been
* written by Marco Pantaleoni <panta@prosa.it>, <panta@elasticworld.org>
* and are:
* Copyright (C) 1999 Marco Pantaleoni.
*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -32,47 +37,143 @@
#include <ctype.h> #include <ctype.h>
static const char sed_usage[] = static const char sed_usage[] =
"sed [-n] [-e script] [file...]\n\n" "sed [-n] -e script [file...]\n\n"
"Allowed sed scripts come in the following form:\n" "Allowed sed scripts come in the following form:\n"
"\t's/regexp/replacement/[gp]'\n" "\t'ADDR [!] COMMAND'\n\n"
"which attempt to match regexp against the pattern space\n" "\twhere address ADDR can be:\n"
"and if successful replaces the matched portion with replacement.\n\n" "\t NUMBER Match specified line number\n"
"Options:\n" "\t $ Match last line\n"
"-e\tadd the script to the commands to be executed\n" "\t /REGEXP/ Match specified regexp\n"
"-n\tsuppress automatic printing of pattern space\n\n" "\t (! inverts the meaning of the match)\n\n"
"\tand COMMAND can be:\n"
"\t s/regexp/replacement/[gp]\n"
"\t which attempt to match regexp against the pattern space\n"
"\t and if successful replaces the matched portion with replacement.\n\n"
"\t aTEXT\n"
"\t which appends TEXT after the pattern space\n"
"Options:\n"
"-e\tadd the script to the commands to be executed\n"
"-n\tsuppress automatic printing of pattern space\n\n"
#if defined BB_REGEXP #if defined BB_REGEXP
"This version of sed matches full regular expresions.\n"; "This version of sed matches full regular expresions.\n";
#else #else
"This version of sed matches strings (not full regular expresions).\n"; "This version of sed matches strings (not full regular expresions).\n";
#endif #endif
/* Flags & variables */
static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag) typedef enum { f_none, f_replace, f_append } sed_function;
#define NO_LINE -2
#define LAST_LINE -1
static int addr_line = NO_LINE;
static char *addr_pattern = NULL;
static int negated = 0;
#define SKIPSPACES(p) do { while (isspace(*(p))) (p)++; } while (0)
#define BUFSIZE 1024
static inline int at_last(FILE * fp)
{ {
int foundOne=FALSE; int res = 0;
char haystack[1024];
while (fgets (haystack, 1023, fp)) { if (feof(fp))
foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); return 1;
if (foundOne==TRUE && printFlag==TRUE) { else {
fprintf(stdout, haystack); char ch;
} if ((ch = fgetc(fp)) == EOF)
if (quietFlag==FALSE) { res++;
ungetc(ch, fp);
}
return res;
}
static void do_sed_repl(FILE * fp, char *needle, char *newNeedle,
int ignoreCase, int printFlag, int quietFlag)
{
int foundOne = FALSE;
char haystack[BUFSIZE];
int line = 1, doit;
while (fgets(haystack, BUFSIZE - 1, fp)) {
doit = 0;
if (addr_pattern) {
doit = !find_match(haystack, addr_pattern, FALSE);
} else if (addr_line == NO_LINE)
doit = 1;
else if (addr_line == LAST_LINE) {
if (at_last(fp))
doit = 1;
} else {
if (line == addr_line)
doit = 1;
}
if (negated)
doit = 1 - doit;
if (doit) {
foundOne =
replace_match(haystack, needle, newNeedle, ignoreCase);
if (foundOne == TRUE && printFlag == TRUE) {
fprintf(stdout, haystack);
}
}
if (quietFlag == FALSE) {
fprintf(stdout, haystack); fprintf(stdout, haystack);
} }
line++;
} }
} }
extern int sed_main (int argc, char **argv) static void do_sed_append(FILE * fp, char *appendline, int quietFlag)
{
char buffer[BUFSIZE];
int line = 1, doit;
while (fgets(buffer, BUFSIZE - 1, fp)) {
doit = 0;
if (addr_pattern) {
doit = !find_match(buffer, addr_pattern, FALSE);
} else if (addr_line == NO_LINE)
doit = 1;
else if (addr_line == LAST_LINE) {
if (at_last(fp))
doit = 1;
} else {
if (line == addr_line)
doit = 1;
}
if (negated)
doit = 1 - doit;
if (quietFlag == FALSE) {
fprintf(stdout, buffer);
}
if (doit) {
fputs(appendline, stdout);
fputc('\n', stdout);
}
line++;
}
}
extern int sed_main(int argc, char **argv)
{ {
FILE *fp; FILE *fp;
char *needle=NULL, *newNeedle=NULL; char *needle = NULL, *newNeedle = NULL;
char *name; char *name;
char *cp; char *cp;
int ignoreCase=FALSE; int ignoreCase = FALSE;
int printFlag=FALSE; int printFlag = FALSE;
int quietFlag=FALSE; int quietFlag = FALSE;
int stopNow; int stopNow;
char *line_s = NULL, saved;
char *appendline = NULL;
char *pos;
sed_function sed_f = f_none;
argc--; argc--;
argv++; argv++;
@ -83,68 +184,119 @@ extern int sed_main (int argc, char **argv)
if (**argv == '-') { if (**argv == '-') {
argc--; argc--;
cp = *argv++; cp = *argv++;
stopNow=FALSE; stopNow = FALSE;
while (*++cp && stopNow==FALSE) { while (*++cp && stopNow == FALSE) {
switch (*cp) { switch (*cp) {
case 'n': case 'n':
quietFlag = TRUE; quietFlag = TRUE;
break; break;
case 'e': case 'e':
if (*(cp+1)==0 && --argc < 0) { if (*(cp + 1) == 0 && --argc < 0) {
usage( sed_usage); usage(sed_usage);
} }
if ( *++cp != 's') if (*++cp != 's')
cp = *argv++; cp = *argv++;
while( *cp ) {
if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') { /* Read address if present */
char* pos=needle=cp+2; SKIPSPACES(cp);
for(;;) { if (*cp == '$') {
pos = strchr(pos, '/'); addr_line = LAST_LINE;
if (pos==NULL) { cp++;
usage( sed_usage); } else {
} if (isdigit(*cp)) { /* LINE ADDRESS */
if (*(pos-1) == '\\') { line_s = cp;
pos++; while (isdigit(*cp))
continue; cp++;
} if (cp > line_s) {
break; /* numeric line */
saved = *cp;
*cp = '\0';
addr_line = atoi(line_s);
*cp = saved;
} }
*pos=0; } else if (*cp == '/') { /* PATTERN ADDRESS */
newNeedle=++pos; pos = addr_pattern = cp + 1;
for(;;) { pos = strchr(pos, '/');
pos = strchr(pos, '/'); if (!pos)
if (pos==NULL) { usage(sed_usage);
usage( sed_usage); *pos = '\0';
} cp = pos + 1;
if (*(pos-1) == '\\') { }
pos++; }
continue;
} SKIPSPACES(cp);
break; if (*cp == '!') {
negated++;
cp++;
}
/* Read command */
SKIPSPACES(cp);
switch (*cp) {
case 's': /* REPLACE */
if (strlen(cp) <= 3 || *(cp + 1) != '/')
break;
sed_f = f_replace;
pos = needle = cp + 2;
for (;;) {
pos = strchr(pos, '/');
if (pos == NULL) {
usage(sed_usage);
} }
*pos=0; if (*(pos - 1) == '\\') {
if (pos+2 != 0) { pos++;
while (*++pos) { continue;
switch (*pos) { }
case 'i': break;
ignoreCase=TRUE; }
break; *pos = 0;
case 'p': newNeedle = ++pos;
printFlag=TRUE; for (;;) {
break; pos = strchr(pos, '/');
case 'g': if (pos == NULL) {
break; usage(sed_usage);
default: }
usage( sed_usage); if (*(pos - 1) == '\\') {
} pos++;
continue;
}
break;
}
*pos = 0;
if (pos + 2 != 0) {
while (*++pos) {
switch (*pos) {
case 'i':
ignoreCase = TRUE;
break;
case 'p':
printFlag = TRUE;
break;
case 'g':
break;
default:
usage(sed_usage);
} }
} }
} }
cp++; cp = pos;
/* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
break;
case 'a': /* APPEND */
if (strlen(cp) < 2)
break;
sed_f = f_append;
appendline = ++cp;
/* fprintf(stderr, "append '%s'\n", appendline); */
break;
} }
//fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
stopNow=TRUE; stopNow = TRUE;
break; break;
default: default:
@ -153,30 +305,48 @@ extern int sed_main (int argc, char **argv)
} }
} }
if (argc==0) { if (argc == 0) {
do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag); switch (sed_f) {
case f_none:
break;
case f_replace:
do_sed_repl(stdin, needle, newNeedle, ignoreCase, printFlag,
quietFlag);
break;
case f_append:
do_sed_append(stdin, appendline, quietFlag);
break;
}
} else { } else {
while (argc-- > 0) { while (argc-- > 0) {
name = *argv++; name = *argv++;
fp = fopen (name, "r"); fp = fopen(name, "r");
if (fp == NULL) { if (fp == NULL) {
perror (name); perror(name);
continue; continue;
} }
do_sed( fp, needle, newNeedle, ignoreCase, printFlag, quietFlag); switch (sed_f) {
case f_none:
break;
case f_replace:
do_sed_repl(fp, needle, newNeedle, ignoreCase, printFlag,
quietFlag);
break;
case f_append:
do_sed_append(fp, appendline, quietFlag);
break;
}
if (ferror (fp)) if (ferror(fp))
perror (name); perror(name);
fclose (fp); fclose(fp);
} }
} }
exit( TRUE); exit(TRUE);
} }
/* END CODE */ /* END CODE */

26
ls.c
View File

@ -193,11 +193,13 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
fputs(" ", stdout); fputs(" ", stdout);
#ifdef BB_FEATURE_LS_USERNAME #ifdef BB_FEATURE_LS_USERNAME
if (!(opts & DISP_NUMERIC)) { if (!(opts & DISP_NUMERIC)) {
scratch[0]='\0'; memset ( scratch, 0, sizeof (scratch));
my_getpwuid( scratch, info->st_uid); my_getpwuid( scratch, info->st_uid);
scratch[8]='\0'; if (*scratch) {
if (*scratch) fputs(scratch, stdout);
wr(scratch,8); if ( strlen( scratch) <= 8 )
wr(" ", 8-strlen( scratch));
}
else { else {
writenum((long) info->st_uid,(short)8); writenum((long) info->st_uid,(short)8);
fputs(" ", stdout); fputs(" ", stdout);
@ -208,20 +210,21 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
writenum((long) info->st_uid,(short)8); writenum((long) info->st_uid,(short)8);
fputs(" ", stdout); fputs(" ", stdout);
} }
tab(16);
#ifdef BB_FEATURE_LS_USERNAME #ifdef BB_FEATURE_LS_USERNAME
if (!(opts & DISP_NUMERIC)) { if (!(opts & DISP_NUMERIC)) {
scratch[0]='\0'; memset ( scratch, 0, sizeof (scratch));
my_getgrgid( scratch, info->st_gid); my_getgrgid( scratch, info->st_gid);
scratch[8]='\0'; if (*scratch) {
if (*scratch) fputs(scratch, stdout);
wr(scratch,8); if ( strlen( scratch) <= 8 )
wr(" ", 8-strlen( scratch));
}
else else
writenum((long) info->st_gid,(short)8); writenum((long) info->st_gid,(short)8);
} else } else
#endif #endif
writenum((long) info->st_gid,(short)8); writenum((long) info->st_gid,(short)8);
tab(17); //tab(26);
if (S_ISBLK(mode) || S_ISCHR(mode)) { if (S_ISBLK(mode) || S_ISCHR(mode)) {
writenum((long)MAJOR(info->st_rdev),(short)3); writenum((long)MAJOR(info->st_rdev),(short)3);
fputs(", ", stdout); fputs(", ", stdout);
@ -230,6 +233,7 @@ static void list_single(const char *name, struct stat *info, const char *fullnam
else else
writenum((long)info->st_size,(short)8); writenum((long)info->st_size,(short)8);
fputs(" ", stdout); fputs(" ", stdout);
//tab(32);
#ifdef BB_FEATURE_LS_TIMESTAMPS #ifdef BB_FEATURE_LS_TIMESTAMPS
{ {
time_t cal; time_t cal;

336
sed.c
View File

@ -5,6 +5,11 @@
* Copyright (C) 1999 by Lineo, inc. * Copyright (C) 1999 by Lineo, inc.
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* *
* Modifications for addresses and append command have been
* written by Marco Pantaleoni <panta@prosa.it>, <panta@elasticworld.org>
* and are:
* Copyright (C) 1999 Marco Pantaleoni.
*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -32,47 +37,143 @@
#include <ctype.h> #include <ctype.h>
static const char sed_usage[] = static const char sed_usage[] =
"sed [-n] [-e script] [file...]\n\n" "sed [-n] -e script [file...]\n\n"
"Allowed sed scripts come in the following form:\n" "Allowed sed scripts come in the following form:\n"
"\t's/regexp/replacement/[gp]'\n" "\t'ADDR [!] COMMAND'\n\n"
"which attempt to match regexp against the pattern space\n" "\twhere address ADDR can be:\n"
"and if successful replaces the matched portion with replacement.\n\n" "\t NUMBER Match specified line number\n"
"Options:\n" "\t $ Match last line\n"
"-e\tadd the script to the commands to be executed\n" "\t /REGEXP/ Match specified regexp\n"
"-n\tsuppress automatic printing of pattern space\n\n" "\t (! inverts the meaning of the match)\n\n"
"\tand COMMAND can be:\n"
"\t s/regexp/replacement/[gp]\n"
"\t which attempt to match regexp against the pattern space\n"
"\t and if successful replaces the matched portion with replacement.\n\n"
"\t aTEXT\n"
"\t which appends TEXT after the pattern space\n"
"Options:\n"
"-e\tadd the script to the commands to be executed\n"
"-n\tsuppress automatic printing of pattern space\n\n"
#if defined BB_REGEXP #if defined BB_REGEXP
"This version of sed matches full regular expresions.\n"; "This version of sed matches full regular expresions.\n";
#else #else
"This version of sed matches strings (not full regular expresions).\n"; "This version of sed matches strings (not full regular expresions).\n";
#endif #endif
/* Flags & variables */
static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag) typedef enum { f_none, f_replace, f_append } sed_function;
#define NO_LINE -2
#define LAST_LINE -1
static int addr_line = NO_LINE;
static char *addr_pattern = NULL;
static int negated = 0;
#define SKIPSPACES(p) do { while (isspace(*(p))) (p)++; } while (0)
#define BUFSIZE 1024
static inline int at_last(FILE * fp)
{ {
int foundOne=FALSE; int res = 0;
char haystack[1024];
while (fgets (haystack, 1023, fp)) { if (feof(fp))
foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); return 1;
if (foundOne==TRUE && printFlag==TRUE) { else {
fprintf(stdout, haystack); char ch;
} if ((ch = fgetc(fp)) == EOF)
if (quietFlag==FALSE) { res++;
ungetc(ch, fp);
}
return res;
}
static void do_sed_repl(FILE * fp, char *needle, char *newNeedle,
int ignoreCase, int printFlag, int quietFlag)
{
int foundOne = FALSE;
char haystack[BUFSIZE];
int line = 1, doit;
while (fgets(haystack, BUFSIZE - 1, fp)) {
doit = 0;
if (addr_pattern) {
doit = !find_match(haystack, addr_pattern, FALSE);
} else if (addr_line == NO_LINE)
doit = 1;
else if (addr_line == LAST_LINE) {
if (at_last(fp))
doit = 1;
} else {
if (line == addr_line)
doit = 1;
}
if (negated)
doit = 1 - doit;
if (doit) {
foundOne =
replace_match(haystack, needle, newNeedle, ignoreCase);
if (foundOne == TRUE && printFlag == TRUE) {
fprintf(stdout, haystack);
}
}
if (quietFlag == FALSE) {
fprintf(stdout, haystack); fprintf(stdout, haystack);
} }
line++;
} }
} }
extern int sed_main (int argc, char **argv) static void do_sed_append(FILE * fp, char *appendline, int quietFlag)
{
char buffer[BUFSIZE];
int line = 1, doit;
while (fgets(buffer, BUFSIZE - 1, fp)) {
doit = 0;
if (addr_pattern) {
doit = !find_match(buffer, addr_pattern, FALSE);
} else if (addr_line == NO_LINE)
doit = 1;
else if (addr_line == LAST_LINE) {
if (at_last(fp))
doit = 1;
} else {
if (line == addr_line)
doit = 1;
}
if (negated)
doit = 1 - doit;
if (quietFlag == FALSE) {
fprintf(stdout, buffer);
}
if (doit) {
fputs(appendline, stdout);
fputc('\n', stdout);
}
line++;
}
}
extern int sed_main(int argc, char **argv)
{ {
FILE *fp; FILE *fp;
char *needle=NULL, *newNeedle=NULL; char *needle = NULL, *newNeedle = NULL;
char *name; char *name;
char *cp; char *cp;
int ignoreCase=FALSE; int ignoreCase = FALSE;
int printFlag=FALSE; int printFlag = FALSE;
int quietFlag=FALSE; int quietFlag = FALSE;
int stopNow; int stopNow;
char *line_s = NULL, saved;
char *appendline = NULL;
char *pos;
sed_function sed_f = f_none;
argc--; argc--;
argv++; argv++;
@ -83,68 +184,119 @@ extern int sed_main (int argc, char **argv)
if (**argv == '-') { if (**argv == '-') {
argc--; argc--;
cp = *argv++; cp = *argv++;
stopNow=FALSE; stopNow = FALSE;
while (*++cp && stopNow==FALSE) { while (*++cp && stopNow == FALSE) {
switch (*cp) { switch (*cp) {
case 'n': case 'n':
quietFlag = TRUE; quietFlag = TRUE;
break; break;
case 'e': case 'e':
if (*(cp+1)==0 && --argc < 0) { if (*(cp + 1) == 0 && --argc < 0) {
usage( sed_usage); usage(sed_usage);
} }
if ( *++cp != 's') if (*++cp != 's')
cp = *argv++; cp = *argv++;
while( *cp ) {
if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') { /* Read address if present */
char* pos=needle=cp+2; SKIPSPACES(cp);
for(;;) { if (*cp == '$') {
pos = strchr(pos, '/'); addr_line = LAST_LINE;
if (pos==NULL) { cp++;
usage( sed_usage); } else {
} if (isdigit(*cp)) { /* LINE ADDRESS */
if (*(pos-1) == '\\') { line_s = cp;
pos++; while (isdigit(*cp))
continue; cp++;
} if (cp > line_s) {
break; /* numeric line */
saved = *cp;
*cp = '\0';
addr_line = atoi(line_s);
*cp = saved;
} }
*pos=0; } else if (*cp == '/') { /* PATTERN ADDRESS */
newNeedle=++pos; pos = addr_pattern = cp + 1;
for(;;) { pos = strchr(pos, '/');
pos = strchr(pos, '/'); if (!pos)
if (pos==NULL) { usage(sed_usage);
usage( sed_usage); *pos = '\0';
} cp = pos + 1;
if (*(pos-1) == '\\') { }
pos++; }
continue;
} SKIPSPACES(cp);
break; if (*cp == '!') {
negated++;
cp++;
}
/* Read command */
SKIPSPACES(cp);
switch (*cp) {
case 's': /* REPLACE */
if (strlen(cp) <= 3 || *(cp + 1) != '/')
break;
sed_f = f_replace;
pos = needle = cp + 2;
for (;;) {
pos = strchr(pos, '/');
if (pos == NULL) {
usage(sed_usage);
} }
*pos=0; if (*(pos - 1) == '\\') {
if (pos+2 != 0) { pos++;
while (*++pos) { continue;
switch (*pos) { }
case 'i': break;
ignoreCase=TRUE; }
break; *pos = 0;
case 'p': newNeedle = ++pos;
printFlag=TRUE; for (;;) {
break; pos = strchr(pos, '/');
case 'g': if (pos == NULL) {
break; usage(sed_usage);
default: }
usage( sed_usage); if (*(pos - 1) == '\\') {
} pos++;
continue;
}
break;
}
*pos = 0;
if (pos + 2 != 0) {
while (*++pos) {
switch (*pos) {
case 'i':
ignoreCase = TRUE;
break;
case 'p':
printFlag = TRUE;
break;
case 'g':
break;
default:
usage(sed_usage);
} }
} }
} }
cp++; cp = pos;
/* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
break;
case 'a': /* APPEND */
if (strlen(cp) < 2)
break;
sed_f = f_append;
appendline = ++cp;
/* fprintf(stderr, "append '%s'\n", appendline); */
break;
} }
//fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
stopNow=TRUE; stopNow = TRUE;
break; break;
default: default:
@ -153,30 +305,48 @@ extern int sed_main (int argc, char **argv)
} }
} }
if (argc==0) { if (argc == 0) {
do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag); switch (sed_f) {
case f_none:
break;
case f_replace:
do_sed_repl(stdin, needle, newNeedle, ignoreCase, printFlag,
quietFlag);
break;
case f_append:
do_sed_append(stdin, appendline, quietFlag);
break;
}
} else { } else {
while (argc-- > 0) { while (argc-- > 0) {
name = *argv++; name = *argv++;
fp = fopen (name, "r"); fp = fopen(name, "r");
if (fp == NULL) { if (fp == NULL) {
perror (name); perror(name);
continue; continue;
} }
do_sed( fp, needle, newNeedle, ignoreCase, printFlag, quietFlag); switch (sed_f) {
case f_none:
break;
case f_replace:
do_sed_repl(fp, needle, newNeedle, ignoreCase, printFlag,
quietFlag);
break;
case f_append:
do_sed_append(fp, appendline, quietFlag);
break;
}
if (ferror (fp)) if (ferror(fp))
perror (name); perror(name);
fclose (fp); fclose(fp);
} }
} }
exit( TRUE); exit(TRUE);
} }
/* END CODE */ /* END CODE */

View File

@ -44,10 +44,15 @@ int dmesg_main( int argc, char** argv )
int level = 0; int level = 0;
int lastc; int lastc;
int cmd = 3; int cmd = 3;
int stopDoingThat;
argc--;
argv++;
/* Parse any options */ /* Parse any options */
while (argc && **argv == '-') { while (argc && **argv == '-') {
while (*++(*argv)) stopDoingThat = FALSE;
while (stopDoingThat == FALSE && *++(*argv)) {
switch (**argv) { switch (**argv) {
case 'c': case 'c':
cmd = 4; cmd = 4;
@ -57,19 +62,22 @@ int dmesg_main( int argc, char** argv )
if (--argc == 0) if (--argc == 0)
goto end; goto end;
level = atoi (*(++argv)); level = atoi (*(++argv));
--argc; if (--argc > 0)
++argv; ++argv;
stopDoingThat = TRUE;
break; break;
case 's': case 's':
if (--argc == 0) if (--argc == 0)
goto end; goto end;
bufsize = atoi (*(++argv)); bufsize = atoi (*(++argv));
--argc; if (--argc > 0)
++argv; ++argv;
stopDoingThat = TRUE;
break; break;
default: default:
goto end; goto end;
} }
}
} }
if (argc > 1) { if (argc > 1) {