summaryrefslogtreecommitdiffstats
path: root/text-utils/colcrt.c
diff options
context:
space:
mode:
authorKarel Zak2006-12-07 00:25:44 +0100
committerKarel Zak2006-12-07 00:25:44 +0100
commit66ee8158b69525e12060ef558cb5d77feadab1dc (patch)
tree08b30f2d07df9213f5647bc6f60b5090a263ef43 /text-utils/colcrt.c
parentImported from util-linux-2.10m tarball. (diff)
downloadkernel-qcow2-util-linux-66ee8158b69525e12060ef558cb5d77feadab1dc.tar.gz
kernel-qcow2-util-linux-66ee8158b69525e12060ef558cb5d77feadab1dc.tar.xz
kernel-qcow2-util-linux-66ee8158b69525e12060ef558cb5d77feadab1dc.zip
Imported from util-linux-2.10s tarball.
Diffstat (limited to 'text-utils/colcrt.c')
-rw-r--r--text-utils/colcrt.c179
1 files changed, 101 insertions, 78 deletions
diff --git a/text-utils/colcrt.c b/text-utils/colcrt.c
index 55138f161..efb176f31 100644
--- a/text-utils/colcrt.c
+++ b/text-utils/colcrt.c
@@ -39,6 +39,7 @@
*/
#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h> /* for close() */
#include <string.h>
#include "nls.h"
@@ -72,13 +73,11 @@ char suppresul;
char printall;
char *progname;
+void colcrt(FILE *f);
int
-main(int argc, char **argv)
-{
+main(int argc, char **argv) {
FILE *f;
- wint_t c;
- wchar_t *cp, *dp;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@@ -113,78 +112,7 @@ main(int argc, char **argv)
argc--;
argv++;
}
- for (;;) {
- c = getwc(f);
- if (c == -1) {
- pflush(outline);
- fflush(stdout);
- break;
- }
- switch (c) {
- case '\n':
- if (outline >= 265)
- pflush(62);
- outline += 2;
- outcol = 0;
- continue;
- case '\016':
- case '\017':
- continue;
- case 033:
- c = getwc(f);
- switch (c) {
- case '9':
- if (outline >= 266)
- pflush(62);
- outline++;
- continue;
- case '8':
- if (outline >= 1)
- outline--;
- continue;
- case '7':
- outline -= 2;
- if (outline < 0)
- outline = 0;
- continue;
- default:
- continue;
- }
- case '\b':
- if (outcol)
- outcol--;
- continue;
- case '\t':
- outcol += 8;
- outcol &= ~7;
- outcol--;
- c = ' ';
- default:
- if (outcol >= 132) {
- outcol++;
- continue;
- }
- cp = &page[outline][outcol];
- outcol++;
- if (c == '_') {
- if (suppresul)
- continue;
- cp += 132;
- c = '-';
- }
- if (*cp == 0) {
- *cp = c;
- dp = cp - outcol;
- for (cp--; cp >= dp && *cp == 0; cp--)
- *cp = ' ';
- } else
- if (plus(c, *cp) || plus(*cp, c))
- *cp = '+';
- else if (*cp == ' ' || *cp == 0)
- *cp = c;
- continue;
- }
- }
+ colcrt(f);
if (f != stdin)
fclose(f);
} while (argc > 0);
@@ -194,6 +122,95 @@ main(int argc, char **argv)
return 0;
}
+void
+colcrt(FILE *f) {
+ wint_t c;
+ wchar_t *cp, *dp;
+ int i, w;
+
+ for (;;) {
+ c = getwc(f);
+ if (c == WEOF) {
+ pflush(outline);
+ fflush(stdout);
+ break;
+ }
+ switch (c) {
+ case '\n':
+ if (outline >= 265)
+ pflush(62);
+ outline += 2;
+ outcol = 0;
+ continue;
+ case '\016':
+ case '\017':
+ continue;
+ case 033:
+ c = getwc(f);
+ switch (c) {
+ case '9':
+ if (outline >= 266)
+ pflush(62);
+ outline++;
+ continue;
+ case '8':
+ if (outline >= 1)
+ outline--;
+ continue;
+ case '7':
+ outline -= 2;
+ if (outline < 0)
+ outline = 0;
+ continue;
+ default:
+ continue;
+ }
+ case '\b':
+ if (outcol)
+ outcol--;
+ continue;
+ case '\t':
+ outcol += 8;
+ outcol &= ~7;
+ outcol--;
+ c = ' ';
+ default:
+ w = wcwidth(c);
+ if (outcol + w > 132) {
+ outcol++;
+ continue;
+ }
+ cp = &page[outline][outcol];
+ outcol += w;
+ if (c == '_') {
+ if (suppresul)
+ continue;
+ cp += 132;
+ c = '-';
+ }
+ if (*cp == 0) {
+ /* trick! */
+ for (i=0; i<w; i++)
+ cp[i] = c;
+ dp = cp - (outcol-w);
+ for (cp--; cp >= dp && *cp == 0; cp--)
+ *cp = ' ';
+ } else {
+ if (plus(c, *cp) || plus(*cp, c))
+ *cp = '+';
+ else if (*cp == ' ' || *cp == 0) {
+ for (i=1; i<w; i++)
+ if (cp[i] != ' ' && cp[i] != 0)
+ continue;
+ for (i=0; i<w; i++)
+ cp[i] = c;
+ }
+ }
+ continue;
+ }
+ }
+}
+
int plus(wchar_t c, wchar_t d)
{
@@ -207,7 +224,7 @@ void pflush(int ol)
register int i;
register wchar_t *cp;
char lastomit;
- int l;
+ int l, w;
l = ol;
lastomit = 0;
@@ -226,7 +243,13 @@ void pflush(int ol)
continue;
}
lastomit = 0;
- fputws(cp, stdout);
+ while (*cp) {
+ if ((w = wcwidth(*cp)) > 0) {
+ putwchar(*cp);
+ cp += w;
+ } else
+ cp++;
+ }
putwchar('\n');
}
bcopy(page[ol], page, (267 - ol) * 132 * sizeof(wchar_t));