FreeBSD Bugzilla – Attachment 150536 Details for
Bug 195929
usr.bin/sed -- constify, remove line-length limits
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Constify, remove command-length limits
sed.patch (text/plain), 10.61 KB, created by
Mikhail T.
on 2014-12-13 06:24:37 UTC
(
hide
)
Description:
Constify, remove command-length limits
Filename:
MIME Type:
Creator:
Mikhail T.
Created:
2014-12-13 06:24:37 UTC
Size:
10.61 KB
patch
obsolete
>Index: compile.c >=================================================================== >--- compile.c (revision 274711) >+++ compile.c (working copy) >@@ -64,17 +64,17 @@ > int lh_ref; > } *labels[LHSZ]; > >-static char *compile_addr(char *, struct s_addr *); >-static char *compile_ccl(char **, char *); >-static char *compile_delimited(char *, char *, int); >-static char *compile_flags(char *, struct s_subst *); >-static regex_t *compile_re(char *, int); >-static char *compile_subst(char *, struct s_subst *); >-static char *compile_text(void); >-static char *compile_tr(char *, struct s_tr **); >+static const char *compile_addr(const char *, struct s_addr *); >+static char *compile_ccl(const char **, char *); >+static const char *compile_delimited(const char *, char *, int); >+static const char *compile_flags(const char *, struct s_subst *); >+static regex_t *compile_re(const char *, int); >+static const char *compile_subst(const char *, struct s_subst *); >+static char *compile_text(void); >+static const char *compile_tr(const char *, struct s_tr **); > static struct s_command > **compile_stream(struct s_command **); >-static char *duptoeol(char *, const char *); >+static char *duptoeol(const char *, const char *); > static void enterlabel(struct s_command *); > static struct s_command > *findlabel(char *); >@@ -153,8 +153,7 @@ > static struct s_command ** > compile_stream(struct s_command **link) > { >- char *p; >- static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ >+ const char *p; > struct s_command *cmd, *cmd2, *stack; > struct s_format *fp; > char re[_POSIX2_LINE_MAX + 1]; >@@ -162,7 +161,7 @@ > > stack = 0; > for (;;) { >- if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { >+ if ((p = cu_fgets(NULL)) == NULL) { > if (stack != 0) > errx(1, "%lu: %s: unexpected EOF (pending }'s)", > linenum, fname); >@@ -283,7 +282,7 @@ > cmd->t = duptoeol(p, "w command"); > if (aflag) > cmd->u.fd = -1; >- else if ((cmd->u.fd = open(p, >+ else if ((cmd->u.fd = open(cmd->t, > O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, > DEFFILEMODE)) == -1) > err(1, "%s", p); >@@ -308,7 +307,7 @@ > p++; > EATSPACE(); > cmd->t = duptoeol(p, "label"); >- if (strlen(p) == 0) >+ if (cmd->t[0] == '\0') > errx(1, "%lu: %s: empty label", linenum, fname); > enterlabel(cmd); > break; >@@ -372,8 +371,8 @@ > * in the case of a non-terminated string. The character array d is filled > * with the processed string. > */ >-static char * >-compile_delimited(char *p, char *d, int is_tr) >+static const char * >+compile_delimited(const char *p, char *d, int is_tr) > { > char c; > >@@ -416,10 +415,10 @@ > > /* compile_ccl: expand a POSIX character class */ > static char * >-compile_ccl(char **sp, char *t) >+compile_ccl(const char **sp, char *t) > { > int c, d; >- char *s = *sp; >+ const char *s = *sp; > > *t++ = *s++; > if (*s == '^') >@@ -442,7 +441,7 @@ > * Cflags are passed to regcomp. > */ > static regex_t * >-compile_re(char *re, int case_insensitive) >+compile_re(const char *re, int case_insensitive) > { > regex_t *rep; > int eval, flags; >@@ -466,14 +465,13 @@ > * point to a saved copy of it. Nsub is the number of parenthesized regular > * expressions. > */ >-static char * >-compile_subst(char *p, struct s_subst *s) >+static const char * >+compile_subst(const char *p, struct s_subst *s) > { >- static char lbuf[_POSIX2_LINE_MAX + 1]; > int asize, size; > u_char ref; > char c, *text, *op, *sp; >- int more = 1, sawesc = 0; >+ int more = 0, sawesc = 0; > > c = *p++; /* Terminator character */ > if (c == '\0') >@@ -523,8 +521,11 @@ > *sp++ = '\\'; > } else if (*p == c) { > if (*++p == '\0' && more) { >- if (cu_fgets(lbuf, sizeof(lbuf), &more)) >- p = lbuf; >+ const char *nextp; >+ >+ nextp = cu_fgets(&more); >+ if (nextp != NULL) >+ p = nextp; > } > *sp++ = '\0'; > size += sp - op; >@@ -544,7 +545,7 @@ > if ((text = realloc(text, asize)) == NULL) > err(1, "realloc"); > } >- } while (cu_fgets(p = lbuf, sizeof(lbuf), &more)); >+ } while ((p = cu_fgets(&more))); > errx(1, "%lu: %s: unterminated substitute in regular expression", > linenum, fname); > /* NOTREACHED */ >@@ -553,8 +554,8 @@ > /* > * Compile the flags of the s command > */ >-static char * >-compile_flags(char *p, struct s_subst *s) >+static const char * >+compile_flags(const char *p, struct s_subst *s) > { > int gn; /* True if we have seen g or n */ > unsigned long nval; >@@ -594,12 +595,12 @@ > "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); > gn = 1; > errno = 0; >- nval = strtol(p, &p, 10); >+ nval = strtol(p, &q, 10); > if (errno == ERANGE || nval > INT_MAX) > errx(1, > "%lu: %s: overflow in the 'N' substitute flag", linenum, fname); > s->n = nval; >- p--; >+ p = q - 1; > break; > case 'w': > p++; >@@ -637,8 +638,8 @@ > /* > * Compile a translation set of strings into a lookup table. > */ >-static char * >-compile_tr(char *p, struct s_tr **py) >+static const char * >+compile_tr(const char *p, struct s_tr **py) > { > struct s_tr *y; > int i; >@@ -733,16 +734,15 @@ > compile_text(void) > { > int asize, esc_nl, size; >- char *text, *p, *op, *s; >- char lbuf[_POSIX2_LINE_MAX + 1]; >+ char *text, *s; >+ const char *p, *op; > > asize = 2 * _POSIX2_LINE_MAX + 1; > if ((text = malloc(asize)) == NULL) > err(1, "malloc"); > size = 0; >- while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { >+ while ((p = cu_fgets(NULL))) { > op = s = text + size; >- p = lbuf; > EATSPACE(); > for (esc_nl = 0; *p != '\0'; p++) { > if (*p == '\\' && p[1] != '\0' && *++p == '\n') >@@ -761,9 +761,9 @@ > } > } > text[size] = '\0'; >- if ((p = realloc(text, size + 1)) == NULL) >+ if ((text = realloc(text, size + 1)) == NULL) > err(1, "realloc"); >- return (p); >+ return (text); > } > > /* >@@ -770,8 +770,8 @@ > * Get an address and return a pointer to the first character after > * it. Fill the structure pointed to according to the address. > */ >-static char * >-compile_addr(char *p, struct s_addr *a) >+static const char * >+compile_addr(const char *p, struct s_addr *a) > { > char *end, re[_POSIX2_LINE_MAX + 1]; > int icase; >@@ -825,22 +825,24 @@ > * Return a copy of all the characters up to \n or \0. > */ > static char * >-duptoeol(char *s, const char *ctype) >+duptoeol(const char *s, const char *ctype) > { > size_t len; > int ws; >- char *p, *start; >+ char *p; >+ const char *start; > > ws = 0; > for (start = s; *s != '\0' && *s != '\n'; ++s) > ws = isspace((unsigned char)*s); >- *s = '\0'; > if (ws) > warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); >- len = s - start + 1; >- if ((p = malloc(len)) == NULL) >+ len = s - start; >+ if ((p = malloc(len + 1)) == NULL) > err(1, "malloc"); >- return (memmove(p, start, len)); >+ memmove(p, start, len); >+ p[len] = '\0'; >+ return p; > } > > /* >Index: extern.h >=================================================================== >--- extern.h (revision 274711) >+++ extern.h (working copy) >@@ -48,9 +48,9 @@ > void cfclose(struct s_command *, struct s_command *); > void compile(void); > void cspace(SPACE *, const char *, size_t, enum e_spflag); >-char *cu_fgets(char *, int, int *); >+const char *cu_fgets(int *); > int mf_fgets(SPACE *, enum e_spflag); > int lastline(void); > void process(void); > void resetstate(void); >-char *strregerror(int, regex_t *); >+const char *strregerror(int, const regex_t *); >Index: main.c >=================================================================== >--- main.c (revision 274711) >+++ main.c (working copy) >@@ -73,7 +73,7 @@ > struct s_compunit { > struct s_compunit *next; > enum e_cut {CU_FILE, CU_STRING} type; >- char *s; /* Pointer to string or fname */ >+ const char *s; /* Pointer to string or fname */ > }; > > /* >@@ -86,7 +86,7 @@ > * Linked list of files to be processed > */ > struct s_flist { >- char *fname; >+ const char *fname; > struct s_flist *next; > }; > >@@ -117,8 +117,8 @@ > static const char *inplace; /* Inplace edit file extension. */ > u_long linenum; > >-static void add_compunit(enum e_cut, char *); >-static void add_file(char *); >+static void add_compunit(enum e_cut, const char *); >+static void add_file(const char *); > static void usage(void); > > int >@@ -214,14 +209,16 @@ > * Like fgets, but go through the chain of compilation units chaining them > * together. Empty strings and files are ignored. > */ >-char * >-cu_fgets(char *buf, int n, int *more) >+const char * >+cu_fgets(int *more) > { > static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; > static FILE *f; /* Current open file */ >- static char *s; /* Current pointer inside string */ >- static char string_ident[30]; >+ static const char *s; /* Current pointer inside string */ >+ static char string_ident[30], *lastresult; >+ static size_t lastsize; > char *p; >+ const char *start; > > again: > switch (state) { >@@ -251,14 +248,16 @@ > goto again; > } > case ST_FILE: >- if ((p = fgets(buf, n, f)) != NULL) { >+ p = lastresult; >+ if (getline(&p, &lastsize, f) != -1) { > linenum++; >- if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') >+ if (linenum == 1 && p[0] == '#' && p[1] == 'n') > nflag = 1; > if (more != NULL) > *more = !feof(f); >- return (p); >- } >+ return (lastresult = p); >+ } else if (ferror(f)) >+ err(1, "%s", script->s); > script = script->next; > (void)fclose(f); > state = ST_EOF; >@@ -266,39 +265,22 @@ > case ST_STRING: > if (linenum == 0 && s[0] == '#' && s[1] == 'n') > nflag = 1; >- p = buf; >+ else if (s[0] == '\0') { >+ state = ST_EOF; >+ goto again; >+ } >+ start = s; > for (;;) { >- if (n-- <= 1) { >- *p = '\0'; >- linenum++; >- if (more != NULL) >- *more = 1; >- return (buf); >- } > switch (*s) { > case '\0': >- state = ST_EOF; >- if (s == script->s) { >- script = script->next; >- goto again; >- } else { >- script = script->next; >- *p = '\0'; >- linenum++; >- if (more != NULL) >- *more = 0; >- return (buf); >- } > case '\n': >- *p++ = '\n'; >- *p = '\0'; >- s++; >+ script = script->next; > linenum++; > if (more != NULL) > *more = 0; >- return (buf); >+ return (start); > default: >- *p++ = *s++; >+ s++; > } > } > } >@@ -452,7 +434,7 @@ > * Add a compilation unit to the linked list > */ > static void >-add_compunit(enum e_cut type, char *s) >+add_compunit(enum e_cut type, const char *s) > { > struct s_compunit *cu; > >@@ -469,7 +451,7 @@ > * Add a file to the linked list > */ > static void >-add_file(char *s) >+add_file(const char *s) > { > struct s_flist *fp; > >Index: misc.c >=================================================================== >--- misc.c (revision 274711) >+++ misc.c (working copy) >@@ -55,8 +55,8 @@ > * because of the silly semantics of regerror (we can never know the size of > * the buffer). > */ >-char * >-strregerror(int errcode, regex_t *preg) >+const char * >+strregerror(int errcode, const regex_t *preg) > { > static char *oe; > size_t s;
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 195929
:
150515
|
150516
|
150518
|
150536
|
150558
|
150663
|
152797
|
153006
|
171898
|
171900
|
172273
|
172402
|
172403
|
172409
|
172442
|
172615
|
172616
|
172618