From patchwork Mon Jan 5 12:01:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 5567261 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-dash@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 4D17BBF6C3 for ; Mon, 5 Jan 2015 12:02:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1150420176 for ; Mon, 5 Jan 2015 12:02:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DBB0620166 for ; Mon, 5 Jan 2015 12:02:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753130AbbAEMCH (ORCPT ); Mon, 5 Jan 2015 07:02:07 -0500 Received: from helcar.apana.org.au ([209.40.204.226]:40720 "EHLO helcar.apana.org.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753370AbbAEMCG (ORCPT ); Mon, 5 Jan 2015 07:02:06 -0500 Received: from gondolin.me.apana.org.au ([192.168.0.6]) by fornost.hengli.com.au with esmtp (Exim 4.80 #3 (Debian)) id 1Y86M7-0005xe-LH; Mon, 05 Jan 2015 23:01:55 +1100 Received: from herbert by gondolin.me.apana.org.au with local (Exim 4.80) (envelope-from ) id 1Y86M6-0001n0-OX; Mon, 05 Jan 2015 23:01:54 +1100 Subject: [PATCH 3/4] input: Move all input state into parsefile References: <20150105120030.GA6101@gondor.apana.org.au> To: Jilles Tjoelker , Eric Blake , Oleg Bulatov , dash@vger.kernel.org, Juergen Daubert Message-Id: From: Herbert Xu Date: Mon, 05 Jan 2015 23:01:54 +1100 Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently we maintain a copy of the input state outside of parsefile. This is redundant and makes reentrancy difficult. This patch kills the duplicate global states and now everyone simply uses parsefile. Signed-off-by: Herbert Xu --- src/input.c | 107 ++++++++++++++++++++---------------------------------------- src/input.h | 33 ++++++++++++++++-- 2 files changed, 67 insertions(+), 73 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/src/input.c b/src/input.c index 232bb9c..6223a73 100644 --- a/src/input.c +++ b/src/input.c @@ -61,38 +61,7 @@ #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */ #define IBUFSIZ (BUFSIZ + 1) -MKINIT -struct strpush { - struct strpush *prev; /* preceding string on stack */ - char *prevstring; - int prevnleft; - struct alias *ap; /* if push was associated with an alias */ - char *string; /* remember the string since it may change */ -}; -/* - * The parsefile structure pointed to by the global variable parsefile - * contains information about the current file being read. - */ - -MKINIT -struct parsefile { - struct parsefile *prev; /* preceding file on stack */ - int linno; /* current line */ - int fd; /* file descriptor (or -1 if string) */ - int nleft; /* number of chars left in this line */ - int lleft; /* number of chars left in this buffer */ - char *nextc; /* next char in buffer */ - char *buf; /* input buffer */ - struct strpush *strpush; /* for pushing strings at this level */ - struct strpush basestrpush; /* so pushing one is fast */ -}; - - -int plinno = 1; /* input line number */ -int parsenleft; /* copy of parsefile->nleft */ -MKINIT int parselleft; /* copy of parsefile->lleft */ -char *parsenextc; /* copy of parsefile->nextc */ MKINIT struct parsefile basepf; /* top level input file */ MKINIT char basebuf[IBUFSIZ]; /* buffer for top level input file */ struct parsefile *parsefile = &basepf; /* current input file */ @@ -114,10 +83,12 @@ INCLUDE "error.h" INIT { basepf.nextc = basepf.buf = basebuf; + basepf.linno = 1; } RESET { - parselleft = parsenleft = 0; /* clear input buffer */ + /* clear input buffer */ + basepf.lleft = basepf.nleft = 0; popallfiles(); } #endif @@ -131,8 +102,8 @@ RESET { int pgetc(void) { - if (--parsenleft >= 0) - return (signed char)*parsenextc++; + if (--parsefile->nleft >= 0) + return (signed char)*parsefile->nextc++; else return preadbuffer(); } @@ -158,7 +129,7 @@ preadfd(void) { int nr; char *buf = parsefile->buf; - parsenextc = buf; + parsefile->nextc = buf; retry: #ifndef SMALL @@ -225,29 +196,32 @@ static int preadbuffer(void) while (unlikely(parsefile->strpush)) { if ( - parsenleft == -1 && parsefile->strpush->ap && - parsenextc[-1] != ' ' && parsenextc[-1] != '\t' + parsefile->nleft == -1 && + parsefile->strpush->ap && + parsefile->nextc[-1] != ' ' && + parsefile->nextc[-1] != '\t' ) { return PEOA; } popstring(); - if (--parsenleft >= 0) - return (signed char)*parsenextc++; + if (--parsefile->nleft >= 0) + return (signed char)*parsefile->nextc++; } - if (unlikely(parsenleft == EOF_NLEFT || parsefile->buf == NULL)) + if (unlikely(parsefile->nleft == EOF_NLEFT || + parsefile->buf == NULL)) return PEOF; flushall(); - more = parselleft; + more = parsefile->lleft; if (more <= 0) { again: if ((more = preadfd()) <= 0) { - parselleft = parsenleft = EOF_NLEFT; + parsefile->lleft = parsefile->nleft = EOF_NLEFT; return PEOF; } } - q = parsenextc; + q = parsefile->nextc; /* delete nul characters */ #ifndef SMALL @@ -265,7 +239,7 @@ again: q++; if (c == '\n') { - parsenleft = q - parsenextc - 1; + parsefile->nleft = q - parsefile->nextc - 1; break; } @@ -282,13 +256,13 @@ again: } if (more <= 0) { - parsenleft = q - parsenextc - 1; - if (parsenleft < 0) + parsefile->nleft = q - parsefile->nextc - 1; + if (parsefile->nleft < 0) goto again; break; } } - parselleft = more; + parsefile->lleft = more; savec = *q; *q = '\0'; @@ -298,13 +272,13 @@ again: HistEvent he; INTOFF; history(hist, &he, whichprompt == 1? H_ENTER : H_APPEND, - parsenextc); + parsefile->nextc); INTON; } #endif if (vflag) { - out2str(parsenextc); + out2str(parsefile->nextc); #ifdef FLUSHERR flushout(out2); #endif @@ -312,7 +286,7 @@ again: *q = savec; - return (signed char)*parsenextc++; + return (signed char)*parsefile->nextc++; } /* @@ -323,8 +297,8 @@ again: void pungetc(void) { - parsenleft++; - parsenextc--; + parsefile->nleft++; + parsefile->nextc--; } /* @@ -346,15 +320,15 @@ pushstring(char *s, void *ap) parsefile->strpush = sp; } else sp = parsefile->strpush = &(parsefile->basestrpush); - sp->prevstring = parsenextc; - sp->prevnleft = parsenleft; + sp->prevstring = parsefile->nextc; + sp->prevnleft = parsefile->nleft; sp->ap = (struct alias *)ap; if (ap) { ((struct alias *)ap)->flag |= ALIASINUSE; sp->string = s; } - parsenextc = s; - parsenleft = len; + parsefile->nextc = s; + parsefile->nleft = len; INTON; } @@ -365,7 +339,8 @@ popstring(void) INTOFF; if (sp->ap) { - if (parsenextc[-1] == ' ' || parsenextc[-1] == '\t') { + if (parsefile->nextc[-1] == ' ' || + parsefile->nextc[-1] == '\t') { checkkwd |= CHKALIAS; } if (sp->string != sp->ap->val) { @@ -376,8 +351,8 @@ popstring(void) unalias(sp->ap->name); } } - parsenextc = sp->prevstring; - parsenleft = sp->prevnleft; + parsefile->nextc = sp->prevstring; + parsefile->nleft = sp->prevnleft; /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/ parsefile->strpush = sp->prev; if (sp != &(parsefile->basestrpush)) @@ -426,7 +401,7 @@ setinputfd(int fd, int push) parsefile->fd = fd; if (parsefile->buf == NULL) parsefile->buf = ckmalloc(IBUFSIZ); - parselleft = parsenleft = 0; + parsefile->lleft = parsefile->nleft = 0; plinno = 1; } @@ -440,8 +415,8 @@ setinputstring(char *string) { INTOFF; pushfile(); - parsenextc = string; - parsenleft = strlen(string); + parsefile->nextc = string; + parsefile->nleft = strlen(string); parsefile->buf = NULL; plinno = 1; INTON; @@ -459,10 +434,6 @@ pushfile(void) { struct parsefile *pf; - parsefile->nleft = parsenleft; - parsefile->lleft = parselleft; - parsefile->nextc = parsenextc; - parsefile->linno = plinno; pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile)); pf->prev = parsefile; pf->fd = -1; @@ -486,10 +457,6 @@ popfile(void) popstring(); parsefile = pf->prev; ckfree(pf); - parsenleft = parsefile->nleft; - parselleft = parsefile->lleft; - parsenextc = parsefile->nextc; - plinno = parsefile->linno; INTON; } diff --git a/src/input.h b/src/input.h index 90ff6c3..ad8b463 100644 --- a/src/input.h +++ b/src/input.h @@ -41,14 +41,41 @@ enum { INPUT_NOFILE_OK = 2, }; +struct alias; + +struct strpush { + struct strpush *prev; /* preceding string on stack */ + char *prevstring; + int prevnleft; + struct alias *ap; /* if push was associated with an alias */ + char *string; /* remember the string since it may change */ +}; + +/* + * The parsefile structure pointed to by the global variable parsefile + * contains information about the current file being read. + */ + +struct parsefile { + struct parsefile *prev; /* preceding file on stack */ + int linno; /* current line */ + int fd; /* file descriptor (or -1 if string) */ + int nleft; /* number of chars left in this line */ + int lleft; /* number of chars left in this buffer */ + char *nextc; /* next char in buffer */ + char *buf; /* input buffer */ + struct strpush *strpush; /* for pushing strings at this level */ + struct strpush basestrpush; /* so pushing one is fast */ +}; + +extern struct parsefile *parsefile; + /* * The input line number. Input.c just defines this variable, and saves * and restores it when files are pushed and popped. The user of this * package must set its value. */ -extern int plinno; -extern int parsenleft; /* number of characters left in input buffer */ -extern char *parsenextc; /* next character in input buffer */ +#define plinno (parsefile->linno) int pgetc(void); int pgetc2(void);