Changeset View
Changeset View
Standalone View
Standalone View
bin/ed/io.c
| Show First 20 Lines • Show All 292 Lines • ▼ Show 20 Lines | |||||
| /* put_tty_line: print text to stdout */ | /* put_tty_line: print text to stdout */ | ||||
| int | int | ||||
| put_tty_line(const char *s, int l, long n, int gflag) | put_tty_line(const char *s, int l, long n, int gflag) | ||||
| { | { | ||||
| int col = 0; | int col = 0; | ||||
| int lc = 0; | int lc = 0; | ||||
| char *cp; | char *cp; | ||||
| wchar_t wc; | |||||
| mbstate_t mbs; | |||||
| size_t clen; | |||||
| int w; | |||||
| if (gflag & GNP) { | if (gflag & GNP) { | ||||
| printf("%ld\t", n); | printf("%ld\t", n); | ||||
| col = 8; | col = 8; | ||||
| } | } | ||||
| for (; l--; s++) { | for (; l > 0;) { | ||||
| if ((gflag & GLS) && ++col > cols) { | if (!(gflag & GLS)) { | ||||
| putchar(*s++); | |||||
| l--; | |||||
| continue; | |||||
| } | |||||
| /* GLS mode: try to decode a multibyte character */ | |||||
| memset(&mbs, 0, sizeof(mbs)); | |||||
| clen = mbrtowc(&wc, s, l, &mbs); | |||||
| if (clen != (size_t)-1 && clen != (size_t)-2 && | |||||
| clen > 1 && iswprint(wc) && (w = wcwidth(wc)) >= 0) { | |||||
| /* printable multibyte character */ | |||||
| if (col + w > cols) { | |||||
| fputs("\\\n", stdout); | fputs("\\\n", stdout); | ||||
| col = 0; | |||||
| #ifndef BACKWARDS | |||||
| if (!scripted && !isglobal && ++lc > rows) { | |||||
| lc = 0; | |||||
| fputs("Press <RETURN> to continue... ", | |||||
| stdout); | |||||
| fflush(stdout); | |||||
| if (get_tty_line() < 0) | |||||
| return ERR; | |||||
| } | |||||
| #endif | |||||
| } | |||||
| col += w; | |||||
| fwrite(s, 1, clen, stdout); | |||||
| s += clen; | |||||
| l -= clen; | |||||
| continue; | |||||
| } | |||||
| /* single byte: ASCII printable, escape sequence, or octal */ | |||||
| if (++col > cols) { | |||||
| fputs("\\\n", stdout); | |||||
| col = 1; | col = 1; | ||||
| #ifndef BACKWARDS | #ifndef BACKWARDS | ||||
| if (!scripted && !isglobal && ++lc > rows) { | if (!scripted && !isglobal && ++lc > rows) { | ||||
| lc = 0; | lc = 0; | ||||
| fputs("Press <RETURN> to continue... ", stdout); | fputs("Press <RETURN> to continue... ", stdout); | ||||
| fflush(stdout); | fflush(stdout); | ||||
| if (get_tty_line() < 0) | if (get_tty_line() < 0) | ||||
| return ERR; | return ERR; | ||||
| } | } | ||||
| #endif | #endif | ||||
| } | } | ||||
| if (gflag & GLS) { | |||||
| if (31 < *s && *s < 127 && *s != '\\') | if (31 < *s && *s < 127 && *s != '\\') | ||||
| putchar(*s); | putchar(*s); | ||||
| else { | else { | ||||
| putchar('\\'); | putchar('\\'); | ||||
| col++; | col++; | ||||
| if (*s && (cp = strchr(ESCAPES, *s)) != NULL) | if (*s && (cp = strchr(ESCAPES, *s)) != NULL) | ||||
| putchar(ESCCHARS[cp - ESCAPES]); | putchar(ESCCHARS[cp - ESCAPES]); | ||||
| else { | else { | ||||
| putchar((((unsigned char) *s & 0300) >> 6) + '0'); | putchar((((unsigned char) *s & 0300) >> 6) + '0'); | ||||
| putchar((((unsigned char) *s & 070) >> 3) + '0'); | putchar((((unsigned char) *s & 070) >> 3) + '0'); | ||||
| putchar(((unsigned char) *s & 07) + '0'); | putchar(((unsigned char) *s & 07) + '0'); | ||||
| col += 2; | col += 2; | ||||
| } | } | ||||
| } | } | ||||
| s++; | |||||
| } else | l--; | ||||
| putchar(*s); | |||||
| } | } | ||||
| #ifndef BACKWARDS | #ifndef BACKWARDS | ||||
| if (gflag & GLS) | if (gflag & GLS) | ||||
| putchar('$'); | putchar('$'); | ||||
| #endif | #endif | ||||
| putchar('\n'); | putchar('\n'); | ||||
| return 0; | return 0; | ||||
| } | } | ||||