TK checking in for Elan Amir
SCCS-vsn: usr.bin/sed/defs.h 5.4
SCCS-vsn: usr.bin/sed/extern.h 5.6
SCCS-vsn: usr.bin/sed/misc.c 5.4
SCCS-vsn: usr.bin/sed/process.c 5.14
*
* %sccs.include.redist.c%
*
*
* %sccs.include.redist.c%
*
- * @(#)defs.h 5.3 (Berkeley) %G%
+ * @(#)defs.h 5.4 (Berkeley) %G%
/*
* An internally compiled command.
/*
* An internally compiled command.
- * Initialy, label references are stored in u.t, on a second pass they
+ * Initialy, label references are stored in t, on a second pass they
* are updated to pointers.
*/
struct s_command {
* are updated to pointers.
*/
struct s_command {
char code; /* Command code */
u_int nonsel:1; /* True if ! */
u_int inrange:1; /* True if in range */
char code; /* Command code */
u_int nonsel:1; /* True if ! */
u_int inrange:1; /* True if in range */
+ u_int lused:1; /* True if label used. */
struct s_appends {
enum {AP_STRING, AP_FILE} type;
char *s;
struct s_appends {
enum {AP_STRING, AP_FILE} type;
char *s;
};
enum e_spflag {
APPEND, /* Append to the contents. */
};
enum e_spflag {
APPEND, /* Append to the contents. */
- APPENDNL, /* Append, with newline. */
REPLACE, /* Replace the contents. */
};
REPLACE, /* Replace the contents. */
};
*
* %sccs.include.redist.c%
*
*
* %sccs.include.redist.c%
*
- * @(#)extern.h 5.5 (Berkeley) %G%
+ * @(#)extern.h 5.6 (Berkeley) %G%
*/
extern struct s_command *prog;
*/
extern struct s_command *prog;
extern int aflag, eflag, nflag;
extern char *fname;
extern int aflag, eflag, nflag;
extern char *fname;
+void cfclose __P((struct s_command *, struct s_command *));
void compile __P((void));
void compile __P((void));
+void cspace __P((SPACE *, char *, size_t, enum e_spflag));
char *cu_fgets __P((char *, int));
void err __P((int, const char *, ...));
int mf_fgets __P((SPACE *, enum e_spflag));
void process __P((void));
char *cu_fgets __P((char *, int));
void err __P((int, const char *, ...));
int mf_fgets __P((SPACE *, enum e_spflag));
void process __P((void));
+char *strnchr __P((const char *, int c, size_t));
char *strregerror __P((int, regex_t *));
void *xmalloc __P((u_int));
void *xrealloc __P((void *, u_int));
char *strregerror __P((int, regex_t *));
void *xmalloc __P((u_int));
void *xrealloc __P((void *, u_int));
-void cfclose __P((struct s_command *, struct s_command *));
-void cspace __P((SPACE *, char *, size_t, enum e_spflag));
-static char sccsid[] = "@(#)misc.c 5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)misc.c 5.4 (Berkeley) %G%";
#endif /* not lint */
#include <sys/types.h>
#endif /* not lint */
#include <sys/types.h>
+char *
+strnchr(buf, c, len)
+ const char *buf;
+ int c;
+ size_t len;
+{
+ const char *cp;
+
+ for (cp = buf; cp - buf < len; cp++)
+ if (*cp == c)
+ return ((char *)cp);
+ return (NULL);
+}
+
/*
* Return a string for a regular expression error passed. This is a overkill,
* because of the silly semantics of regerror (we can never know the size of
/*
* Return a string for a regular expression error passed. This is a overkill,
* because of the silly semantics of regerror (we can never know the size of
-static char sccsid[] = "@(#)process.c 5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)process.c 5.14 (Berkeley) %G%";
#endif /* not lint */
#include <sys/types.h>
#endif /* not lint */
#include <sys/types.h>
static int lastaddr; /* Set by applies if last address of a range. */
static int sdone; /* If any substitutes since last line input. */
/* Iov structure for 'w' commands. */
static int lastaddr; /* Set by applies if last address of a range. */
static int sdone; /* If any substitutes since last line input. */
/* Iov structure for 'w' commands. */
-static struct iovec iov[2] = { NULL, 0, "\n", 1 };
-
static regex_t *defpreg;
size_t maxnsub;
static regex_t *defpreg;
size_t maxnsub;
-regmatch_t *match, startend;
-#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); putchar('\n'); }
+#define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); }
cspace(&PS, hs, hsl, REPLACE);
break;
case 'G':
cspace(&PS, hs, hsl, REPLACE);
break;
case 'G':
- cspace(&PS, hs, hsl, APPENDNL);
+ cspace(&PS, hs, hsl, 0);
break;
case 'h':
cspace(&HS, ps, psl, REPLACE);
break;
case 'H':
break;
case 'h':
cspace(&HS, ps, psl, REPLACE);
break;
case 'H':
- cspace(&HS, ps, psl, APPENDNL);
+ cspace(&HS, ps, psl, 0);
break;
case 'i':
(void)printf("%s", cp->t);
break;
case 'i':
(void)printf("%s", cp->t);
break;
case 'N':
flush_appends();
break;
case 'N':
flush_appends();
- if (!mf_fgets(&PS, APPENDNL)) {
+ if (!mf_fgets(&PS, 0)) {
if (!nflag && !pd)
OUT(ps)
exit(0);
if (!nflag && !pd)
OUT(ps)
exit(0);
DEFFILEMODE)) == -1)
err(FATAL, "%s: %s\n",
cp->t, strerror(errno));
DEFFILEMODE)) == -1)
err(FATAL, "%s: %s\n",
cp->t, strerror(errno));
- iov[0].iov_base = ps;
- iov[0].iov_len = psl;
- if (writev(cp->u.fd, iov, 2) != psl + 1)
+ if (write(cp->u.fd, ps, psl) != psl)
err(FATAL, "%s: %s\n",
cp->t, strerror(errno));
break;
err(FATAL, "%s: %s\n",
cp->t, strerror(errno));
break;
- for (p = ps, len = psl; len--; ++p)
+ for (p = ps, len = psl; --len; ++p)
*p = cp->u.y[*p];
break;
case ':':
*p = cp->u.y[*p];
break;
case ':':
if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
- iov[0].iov_base = ps;
- iov[0].iov_len = psl;
- if (writev(cp->u.s->wfd, iov, 2) != psl + 1)
+ if (write(cp->u.s->wfd, ps, psl) != psl)
err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
}
return (1);
err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
}
return (1);
*/
if ((f = fopen(appends[i].s, "r")) == NULL)
break;
*/
if ((f = fopen(appends[i].s, "r")) == NULL)
break;
- while (count = fread(buf, sizeof(char), sizeof(buf),
- f))
+ while (count = fread(buf, sizeof(char), sizeof(buf), f))
(void)fwrite(buf, sizeof(char), count, stdout);
(void)fclose(f);
break;
(void)fwrite(buf, sizeof(char), count, stdout);
(void)fclose(f);
break;
(void)putchar("\\abfnrtv"[p - escapes]);
count += 2;
} else {
(void)putchar("\\abfnrtv"[p - escapes]);
count += 2;
} else {
- (void)printf("%03o", (u_char)*s);
+ (void)printf("%03o", *(u_char *)s);
- /* So we can work with binary files */
- startend.rm_so = 0;
- startend.rm_eo = slen;
- match[0] = startend;
-
-
- eflags |= REG_STARTEND;
-
if (preg == NULL) {
if (defpreg == NULL)
err(FATAL, "first RE may not be empty");
} else
defpreg = preg;
if (preg == NULL) {
if (defpreg == NULL)
err(FATAL, "first RE may not be empty");
} else
defpreg = preg;
+ match[0].rm_so = 0;
+ match[0].rm_eo = slen - 1; /* Length minus trailing newline. */
+
eval = regexec(defpreg, string,
eval = regexec(defpreg, string,
- nomatch ? 0 : maxnsub + 1, match, eflags);
+ nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
switch(eval) {
case 0:
return (1);
switch(eval) {
case 0:
return (1);
- /*
- * Make sure SPACE has enough memory and ramp up quickly. Appends
- * need two extra bytes, one for the newline, one for a terminating
- * NULL.
- */
- tlen = sp->len + len + (spflag == APPENDNL ? 2 : 1);
+ /* Make sure SPACE has enough memory and ramp up quickly. */
+ tlen = sp->len + len + 1;
if (tlen > sp->blen) {
sp->blen = tlen + 1024;
sp->space = sp->back = xrealloc(sp->back, sp->blen);
}
if (tlen > sp->blen) {
sp->blen = tlen + 1024;
sp->space = sp->back = xrealloc(sp->back, sp->blen);
}
- if (spflag == APPENDNL)
- sp->space[sp->len++] = '\n';
- else if (spflag == REPLACE)
sp->len = 0;
memmove(sp->space + sp->len, p, len);
sp->len = 0;
memmove(sp->space + sp->len, p, len);