int _fcmbget(short *len) { static struct Extra extra; register int i, c, n; if(_Fcin.fcleft) { if((c = mbsize(extra.next)) < 0) c = 1; if((_Fcin.fcleft -= c) <=0) { _Fcin.fcptr = (unsigned char*)fcfirst() - _Fcin.fcleft; _Fcin.fcleft = 0; } *len = c; if(c==1) c = *extra.next++; else if(c==0) _Fcin.fcleft = 0; else c = mbchar(extra.next); return(c); } switch(*len = mbsize(_Fcin.fcptr)) { case -1: if(_Fcin._fcfile && (n=(_Fcin.fclast-_Fcin.fcptr)) < MB_LEN_MAX) { memcpy(extra.buff, _Fcin.fcptr, n); _Fcin.fcptr = _Fcin.fclast; for(i=n; i < MB_LEN_MAX+n; i++) { if((extra.buff[i] = fcgetc(c))==0) break; } _Fcin.fcleft = n; extra.next = extra.buff; return(fcmbget(len)); } *len = 1; /* fall through */ case 0: case 1: c=fcget(); break; default: c = mbchar(_Fcin.fcptr); } return(c); }
void sh_trim(register char *sp) /*@ assume sp!=NULL; promise strlen(in sp) <= in strlen(sp); @*/ { register char *dp; register int c; if(sp) { dp = sp; while(c= *sp) { #if SHOPT_MULTIBYTE int len; if(mbwide() && (len=mbsize(sp))>1) { memmove(dp, sp, len); dp += len; sp += len; continue; } #endif /* SHOPT_MULTIBYTE */ sp++; if(c == '\\') c = *sp++; if(c) *dp++ = c; } *dp = 0; } }
/* * look for the substring <oldsp> in <string> and replace with <newsp> * The new string is put on top of the stack */ char *sh_substitute(const char *string,const char *oldsp,char *newsp) /*@ assume string!=NULL && oldsp!=NULL && newsp!=NULL; return x satisfying x==NULL || strlen(x)==(strlen(in string)+strlen(in newsp)-strlen(in oldsp)); @*/ { register const char *sp = string; register const char *cp; const char *savesp = 0; stakseek(0); if(*sp==0) return((char*)0); if(*(cp=oldsp) == 0) goto found; #if SHOPT_MULTIBYTE mbinit(); #endif /* SHOPT_MULTIBYTE */ do { /* skip to first character which matches start of oldsp */ while(*sp && (savesp==sp || *sp != *cp)) { #if SHOPT_MULTIBYTE /* skip a whole character at a time */ int c = mbsize(sp); if(c < 0) sp++; while(c-- > 0) #endif /* SHOPT_MULTIBYTE */ stakputc(*sp++); } if(*sp == 0) return((char*)0); savesp = sp; for(;*cp;cp++) { if(*cp != *sp++) break; } if(*cp==0) /* match found */ goto found; sp = savesp; cp = oldsp; } while(*sp); return((char*)0); found: /* copy new */ stakputs(newsp); /* copy rest of string */ stakputs(sp); return(stakfreeze(1)); }
char* fmtquote(const char* as, const char* qb, const char* qe, size_t n, int flags) { register unsigned char* s = (unsigned char*)as; register unsigned char* e = s + n; register char* b; register int c; register int m; register int escaped; register int spaced; register int doublequote; register int singlequote; int shell; char* f; char* buf; c = 4 * (n + 1); if (qb) c += strlen((char*)qb); if (qe) c += strlen((char*)qe); b = buf = fmtbuf(c); shell = 0; doublequote = 0; singlequote = 0; if (qb) { if (qb[0] == '$' && qb[1] == '\'' && qb[2] == 0) shell = 1; else if ((flags & FMT_SHELL) && qb[1] == 0) { if (qb[0] == '"') doublequote = 1; else if (qb[0] == '\'') singlequote = 1; } while (*b = *qb++) b++; } else if (flags & FMT_SHELL) doublequote = 1; f = b; escaped = spaced = !!(flags & FMT_ALWAYS); while (s < e) { if ((m = mbsize(s)) > 1 && (s + m) <= e) { #if _hdr_wchar && _hdr_wctype c = mbchar(s); if (!spaced && !escaped && (iswspace(c) || iswcntrl(c))) spaced = 1; s -= m; #endif while (m--) *b++ = *s++; } else { c = *s++; if (!(flags & FMT_ESCAPED) && (iscntrl(c) || !isprint(c) || c == '\\')) { escaped = 1; *b++ = '\\'; switch (c) { case CC_bel: c = 'a'; break; case '\b': c = 'b'; break; case '\f': c = 'f'; break; case '\n': c = 'n'; break; case '\r': c = 'r'; break; case '\t': c = 't'; break; case CC_vt: c = 'v'; break; case CC_esc: c = 'E'; break; case '\\': break; default: if (!(flags & FMT_WIDE) || !(c & 0200)) { *b++ = '0' + ((c >> 6) & 07); *b++ = '0' + ((c >> 3) & 07); c = '0' + (c & 07); } else b--; break; } }