1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

SHOPT_MULTIBYTE-related cleanup (re: 8477d2ce)

As of 8477d2ce, the mbwide() macro (which tests if we're in a
multibyte locale, i.e. UTF-8) is redefined as a constant 0 if we're
compiling without SHOPT_MULTIBYTE. See src/cmd/ksh93/include/defs.h

The other multibyte macros use mbwide() as well, so they all revert
to the single-byte fallbacks in that case, and the multibyte code
in them is never compiled. See src/lib/libast/include/ast.h

Consequently we can now do a bit of cleanup and get rid of many of
the '#if SHOPT_MULTIBYTE' directives, as the compiler optimiser
will happily remove the multibyte-specific code. This increases the
legibility of the ksh code.

I'm taking the opportunity to fix a few typos and whitespace
formatting glitches as well.
This commit is contained in:
Martijn Dekker 2020-08-29 22:45:04 +01:00
parent 7c5d39fa04
commit f8feed1bd2
9 changed files with 28 additions and 76 deletions

View file

@ -1056,7 +1056,6 @@ static int fmtvecho(const char *string, struct printf *pp)
register const char *cp = string, *cpmax; register const char *cp = string, *cpmax;
register int c; register int c;
register int offset = staktell(); register int offset = staktell();
#if SHOPT_MULTIBYTE
int chlen; int chlen;
if(mbwide()) if(mbwide())
{ {
@ -1070,8 +1069,8 @@ static int fmtvecho(const char *string, struct printf *pp)
} }
} }
else else
#endif /* SHOPT_MULTIBYTE */ while((c= *cp++) && (c!='\\'))
while((c= *cp++) && (c!='\\')); ;
if(c==0) if(c==0)
return(-1); return(-1);
c = --cp - string; c = --cp - string;
@ -1079,14 +1078,12 @@ static int fmtvecho(const char *string, struct printf *pp)
stakwrite((void*)string,c); stakwrite((void*)string,c);
for(; c= *cp; cp++) for(; c= *cp; cp++)
{ {
#if SHOPT_MULTIBYTE
if (mbwide() && ((chlen = mbsize(cp)) > 1)) if (mbwide() && ((chlen = mbsize(cp)) > 1))
{ {
stakwrite(cp,chlen); stakwrite(cp,chlen);
cp += (chlen-1); cp += (chlen-1);
continue; continue;
} }
#endif /* SHOPT_MULTIBYTE */
if( c=='\\') switch(*++cp) if( c=='\\') switch(*++cp)
{ {
case 'E': case 'E':

View file

@ -425,8 +425,7 @@ int sh_readline(register Shell_t *shp,char **names, volatile int fd, int flags,s
if(f) if(f)
sfread(iop,cp,c); sfread(iop,cp,c);
cur += c; cur += c;
#if SHOPT_MULTIBYTE if(mbwide() && !binary)
if(!binary && mbwide())
{ {
int x; int x;
int z; int z;
@ -442,12 +441,9 @@ int sh_readline(register Shell_t *shp,char **names, volatile int fd, int flags,s
if((size -= x) > 0 && (up >= cur || z < 0) && ((flags & NN_FLAG) || z < 0 || m > c)) if((size -= x) > 0 && (up >= cur || z < 0) && ((flags & NN_FLAG) || z < 0 || m > c))
continue; continue;
} }
#endif
} }
#if SHOPT_MULTIBYTE if(mbwide() && !binary && (up == var || (flags & NN_FLAG) && size))
if(!binary && mbwide() && (up == var || (flags & NN_FLAG) && size))
cur = var; cur = var;
#endif
*cur = 0; *cur = 0;
if(c>=size || (flags&N_FLAG) || m==0) if(c>=size || (flags&N_FLAG) || m==0)
{ {

View file

@ -31,10 +31,6 @@
#include "edit.h" #include "edit.h"
#include "history.h" #include "history.h"
#if !SHOPT_MULTIBYTE
#define mbchar(p) (*(unsigned char*)p++)
#endif
static char *fmtx(const char *string) static char *fmtx(const char *string)
{ {
register const char *cp = string; register const char *cp = string;

View file

@ -1013,9 +1013,7 @@ int hist_match(register History_t *hp,off_t offset,char *string,int *coffset)
{ {
register unsigned char *first, *cp; register unsigned char *first, *cp;
register int m,n,c=1,line=0; register int m,n,c=1,line=0;
#if SHOPT_MULTIBYTE
mbinit(); mbinit();
#endif /* SHOPT_MULTIBYTE */
sfseek(hp->histfp,offset,SEEK_SET); sfseek(hp->histfp,offset,SEEK_SET);
if(!(cp = first = (unsigned char*)sfgetr(hp->histfp,0,0))) if(!(cp = first = (unsigned char*)sfgetr(hp->histfp,0,0)))
return(-1); return(-1);
@ -1033,10 +1031,8 @@ int hist_match(register History_t *hp,off_t offset,char *string,int *coffset)
break; break;
if(*cp=='\n') if(*cp=='\n')
line++; line++;
#if SHOPT_MULTIBYTE
if((c=mbsize(cp)) < 0) if((c=mbsize(cp)) < 0)
c = 1; c = 1;
#endif /* SHOPT_MULTIBYTE */
cp += c; cp += c;
m -= c; m -= c;
} }

View file

@ -30,8 +30,9 @@
#include <ast.h> #include <ast.h>
#if !SHOPT_MULTIBYTE #if !SHOPT_MULTIBYTE
/* disable multibyte without need for further '#if SHOPT_MULTIBYTE' */
# undef mbwide # undef mbwide
# define mbwide() (0) /* disable multibyte without need for further '#if SHOPT_MULTIBTYE' */ # define mbwide() 0
#endif #endif
#include <sfio.h> #include <sfio.h>

View file

@ -1521,7 +1521,7 @@ breakloop:
lp->lex.reservok = 0; lp->lex.reservok = 0;
else if(c==TIMESYM) else if(c==TIMESYM)
{ {
/* yech - POSIX requires time -p */ /* POSIX requires time -p */
while(fcgetc(n)==' ' || n=='\t'); while(fcgetc(n)==' ' || n=='\t');
if(n>0) if(n>0)
fcseek(-LEN); fcseek(-LEN);
@ -2187,7 +2187,6 @@ struct argnod *sh_endword(Shell_t *shp,int mode)
Stk_t *stkp=shp->stk; Stk_t *stkp=shp->stk;
sfputc(stkp,0); sfputc(stkp,0);
sp = stkptr(stkp,ARGVAL); sp = stkptr(stkp,ARGVAL);
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
{ {
do do
@ -2216,8 +2215,8 @@ struct argnod *sh_endword(Shell_t *shp,int mode)
while(n == 0); while(n == 0);
} }
else else
#endif /* SHOPT_MULTIBYTE */ while((n = state[*sp++]) == 0)
while((n=state[*sp++])==0); ;
dp = sp; dp = sp;
if(mode<0) if(mode<0)
inquote = 1; inquote = 1;
@ -2396,7 +2395,6 @@ struct argnod *sh_endword(Shell_t *shp,int mode)
} }
break; break;
} }
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
{ {
do do
@ -2426,8 +2424,8 @@ struct argnod *sh_endword(Shell_t *shp,int mode)
while(n == 0); while(n == 0);
} }
else else
#endif /* SHOPT_MULTIBYTE */ while((n=state[*dp++ = *sp++])==0)
while((n=state[*dp++ = *sp++])==0); ;
} }
} }

View file

@ -51,13 +51,6 @@
# define STR_GROUP 0 # define STR_GROUP 0
#endif #endif
#if SHOPT_MULTIBYTE
# undef isascii
# define isacii(c) ((c)<=UCHAR_MAX)
#else
# define mbchar(p) (*(unsigned char*)p++)
#endif /* SHOPT_MULTIBYTE */
#if _WINIX #if _WINIX
static int Skip; static int Skip;
#endif /*_WINIX */ #endif /*_WINIX */
@ -75,7 +68,7 @@ typedef struct _mac_
char atmode; /* when processing $@ */ char atmode; /* when processing $@ */
char quote; /* set within double quoted contexts */ char quote; /* set within double quoted contexts */
char lit; /* set within single quotes */ char lit; /* set within single quotes */
char split; /* set when word splittin is possible */ char split; /* set when word splitting is possible */
char pattern; /* set when file expansion follows */ char pattern; /* set when file expansion follows */
char patfound; /* set if pattern character found */ char patfound; /* set if pattern character found */
char assign; /* set for assignments */ char assign; /* set for assignments */
@ -119,6 +112,8 @@ static char *mac_getstring(char*);
static int charlen(const char*,int); static int charlen(const char*,int);
#if SHOPT_MULTIBYTE #if SHOPT_MULTIBYTE
static char *lastchar(const char*,const char*); static char *lastchar(const char*,const char*);
#else
# define lastchar(string,endstring) (endstring)
#endif /* SHOPT_MULTIBYTE */ #endif /* SHOPT_MULTIBYTE */
void *sh_macopen(Shell_t *shp) void *sh_macopen(Shell_t *shp)
@ -293,7 +288,6 @@ void sh_machere(Shell_t *shp,Sfio_t *infile, Sfio_t *outfile, char *string)
cp = fcseek(0); cp = fcseek(0);
while(1) while(1)
{ {
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
{ {
do do
@ -315,8 +309,8 @@ void sh_machere(Shell_t *shp,Sfio_t *infile, Sfio_t *outfile, char *string)
while(n == 0); while(n == 0);
} }
else else
#endif /* SHOPT_MULTIBYTE */ while((n=state[*(unsigned char*)cp++])==0)
while((n=state[*(unsigned char*)cp++])==0); ;
if(n==S_NL || n==S_QUOTE || n==S_RBRA) if(n==S_NL || n==S_QUOTE || n==S_RBRA)
continue; continue;
if(c=(cp-1)-fcseek(0)) if(c=(cp-1)-fcseek(0))
@ -460,7 +454,6 @@ static void copyto(register Mac_t *mp,int endch, int newquote)
cp++; cp++;
while(1) while(1)
{ {
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
{ {
ssize_t len; ssize_t len;
@ -484,7 +477,6 @@ static void copyto(register Mac_t *mp,int endch, int newquote)
c = (cp-len) - first; c = (cp-len) - first;
} }
else else
#endif /* SHOPT_MULTIBYTE */
{ {
while((n=state[*(unsigned char*)cp++])==0); while((n=state[*(unsigned char*)cp++])==0);
c = (cp-1) - first; c = (cp-1) - first;
@ -501,8 +493,7 @@ static void copyto(register Mac_t *mp,int endch, int newquote)
c = chresc(cp,&addr); c = chresc(cp,&addr);
cp = addr; cp = addr;
first = fcseek(cp-first); first = fcseek(cp-first);
#if SHOPT_MULTIBYTE if(mbwide() && c > UCHAR_MAX)
if(c > UCHAR_MAX && mbwide())
{ {
int i; int i;
unsigned char mb[8]; unsigned char mb[8];
@ -512,7 +503,6 @@ static void copyto(register Mac_t *mp,int endch, int newquote)
sfputc(stkp,mb[i]); sfputc(stkp,mb[i]);
} }
else else
#endif /* SHOPT_MULTIBYTE */
sfputc(stkp,c); sfputc(stkp,c);
if(c==ESCAPE && mp->pattern) if(c==ESCAPE && mp->pattern)
sfputc(stkp,ESCAPE); sfputc(stkp,ESCAPE);
@ -717,8 +707,7 @@ static void copyto(register Mac_t *mp,int endch, int newquote)
if(first[c-2]=='.') if(first[c-2]=='.')
offset = stktell(stkp); offset = stktell(stkp);
if(isastchar(*cp) && cp[1]==']') if(isastchar(*cp) && cp[1]==']')
errormsg(SH_DICT,ERROR_exit(1), errormsg(SH_DICT,ERROR_exit(1),e_badsubscript,*cp);
e_badsubscript,*cp);
} }
first = fcseek(c); first = fcseek(c);
mp->pattern = 4; mp->pattern = 4;
@ -1713,7 +1702,6 @@ retry1:
type = 0; type = 0;
if(vsize < type) if(vsize < type)
v = 0; v = 0;
#if SHOPT_MULTIBYTE
else if(mbwide()) else if(mbwide())
{ {
mbinit(); mbinit();
@ -1721,7 +1709,6 @@ retry1:
mbchar(v); mbchar(v);
c = ':'; c = ':';
} }
#endif /* SHOPT_MULTIBYTE */
else else
v += type; v += type;
vsize = v?strlen(v):0; vsize = v?strlen(v):0;
@ -1745,7 +1732,6 @@ retry1:
} }
else if(type < vsize) else if(type < vsize)
{ {
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
{ {
char *vp = v; char *vp = v;
@ -1759,7 +1745,6 @@ retry1:
type = vp-v; type = vp-v;
c = ':'; c = ':';
} }
#endif /* SHOPT_MULTIBYTE */
vsize = type; vsize = type;
} }
else else
@ -2322,14 +2307,12 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
/* insert \ before file expansion characters */ /* insert \ before file expansion characters */
while(size-->0) while(size-->0)
{ {
#if SHOPT_MULTIBYTE
if(mbwide() && (len=mbsize(cp))>1) if(mbwide() && (len=mbsize(cp))>1)
{ {
cp += len; cp += len;
size -= (len-1); size -= (len-1);
continue; continue;
} }
#endif
c = state[n= *(unsigned char*)cp++]; c = state[n= *(unsigned char*)cp++];
if(mp->assign==3 && mp->pattern!=4) if(mp->assign==3 && mp->pattern!=4)
{ {
@ -2401,7 +2384,6 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
while(size-->0) while(size-->0)
{ {
n=state[c= *(unsigned char*)cp++]; n=state[c= *(unsigned char*)cp++];
#if SHOPT_MULTIBYTE
if(mbwide() && n!=S_MBYTE && (len=mbsize(cp-1))>1) if(mbwide() && n!=S_MBYTE && (len=mbsize(cp-1))>1)
{ {
sfwrite(stkp,cp-1, len); sfwrite(stkp,cp-1, len);
@ -2409,7 +2391,6 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
size -= len; size -= len;
continue; continue;
} }
#endif
if(n==S_ESC || n==S_EPAT) if(n==S_ESC || n==S_EPAT)
{ {
/* don't allow extended patterns in this case */ /* don't allow extended patterns in this case */
@ -2420,8 +2401,7 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
mp->patfound = mp->pattern; mp->patfound = mp->pattern;
else if(n && mp->ifs) else if(n && mp->ifs)
{ {
#if SHOPT_MULTIBYTE if(mbwide() && n==S_MBYTE)
if(n==S_MBYTE)
{ {
if(sh_strchr(mp->ifsp,cp-1)<0) if(sh_strchr(mp->ifsp,cp-1)<0)
{ {
@ -2441,13 +2421,11 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
size -= n; size -= n;
n= S_DELIM; n= S_DELIM;
} }
#endif /* SHOPT_MULTIBYTE */
if(n==S_SPACE || n==S_NL) if(n==S_SPACE || n==S_NL)
{ {
while(size>0 && ((n=state[c= *(unsigned char*)cp++])==S_SPACE||n==S_NL)) while(size>0 && ((n=state[c= *(unsigned char*)cp++])==S_SPACE||n==S_NL))
size--; size--;
#if SHOPT_MULTIBYTE if(mbwide() && n==S_MBYTE && sh_strchr(mp->ifsp,cp-1)>=0)
if(n==S_MBYTE && sh_strchr(mp->ifsp,cp-1)>=0)
{ {
n = mbsize(cp-1) - 1; n = mbsize(cp-1) - 1;
if(n==-2) if(n==-2)
@ -2456,9 +2434,7 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
size -= n; size -= n;
n=S_DELIM; n=S_DELIM;
} }
else else if(n==S_DELIM)
#endif /* SHOPT_MULTIBYTE */
if(n==S_DELIM)
size--; size--;
} }
endfield(mp,n==S_DELIM||mp->quoted); endfield(mp,n==S_DELIM||mp->quoted);
@ -2499,7 +2475,7 @@ static void mac_copy(register Mac_t *mp,register const char *str, register int s
/* /*
* Terminate field. * Terminate field.
* If field is null count field if <split> is non-zero * If field is null count field if <split> is non-zero
* Do filename expansion of required * Do filename expansion if required
*/ */
static void endfield(register Mac_t *mp,int split) static void endfield(register Mac_t *mp,int split)
{ {
@ -2566,10 +2542,8 @@ static int substring(register const char *string,const char *pat,int match[], in
sp += size; sp += size;
while(sp>=string) while(sp>=string)
{ {
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
sp = lastchar(string,sp); sp = lastchar(string,sp);
#endif /* SHOPT_MULTIBYTE */
if(n=strgrpmatch(sp,pat,smatch,elementsof(smatch)/2,STR_RIGHT|STR_LEFT|STR_MAXIMAL)) if(n=strgrpmatch(sp,pat,smatch,elementsof(smatch)/2,STR_RIGHT|STR_LEFT|STR_MAXIMAL))
{ {
nmatch = n; nmatch = n;
@ -2611,7 +2585,6 @@ static int charlen(const char *string,int len)
{ {
if(!string) if(!string)
return(0); return(0);
#if SHOPT_MULTIBYTE
if(mbwide()) if(mbwide())
{ {
register const char *str = string, *strmax=string+len; register const char *str = string, *strmax=string+len;
@ -2627,7 +2600,6 @@ static int charlen(const char *string,int len)
return(n); return(n);
} }
else else
#endif /* SHOPT_MULTIBYTE */
{ {
if(len<0) if(len<0)
return(strlen(string)); return(strlen(string));

View file

@ -117,10 +117,6 @@ static void(*nullscan)(Namval_t*,void*);
# define _data data # define _data data
#endif #endif
#if !SHOPT_MULTIBYTE
# define mbchar(p) (*(unsigned char*)p++)
#endif /* SHOPT_MULTIBYTE */
/* ======== name value pair routines ======== */ /* ======== name value pair routines ======== */
#include "shnodes.h" #include "shnodes.h"
@ -1881,9 +1877,11 @@ void nv_putval(register Namval_t *np, const char *string, int flags)
} }
else if((nv_isattr(np, NV_RJUST|NV_ZFILL|NV_LJUST)) && sp) else if((nv_isattr(np, NV_RJUST|NV_ZFILL|NV_LJUST)) && sp)
{ {
for(;*sp == ' '|| *sp=='\t';sp++); for( ; *sp == ' ' || *sp == '\t'; sp++)
;
if((nv_isattr(np,NV_ZFILL)) && (nv_isattr(np,NV_LJUST))) if((nv_isattr(np,NV_ZFILL)) && (nv_isattr(np,NV_LJUST)))
for(;*sp=='0';sp++); for( ; *sp == '0'; sp++)
;
size = nv_size(np); size = nv_size(np);
#if SHOPT_MULTIBYTE #if SHOPT_MULTIBYTE
if(size) if(size)

View file

@ -249,7 +249,6 @@ void sh_trim(register char *sp)
dp = sp; dp = sp;
while(c= *sp) while(c= *sp)
{ {
#if SHOPT_MULTIBYTE
int len; int len;
if(mbwide() && (len=mbsize(sp))>1) if(mbwide() && (len=mbsize(sp))>1)
{ {
@ -258,7 +257,6 @@ void sh_trim(register char *sp)
sp += len; sp += len;
continue; continue;
} }
#endif /* SHOPT_MULTIBYTE */
sp++; sp++;
if(c == '\\') if(c == '\\')
c = *sp++; c = *sp++;