Improved linebuffering to stdout

This commit is contained in:
Michiel Broek 2002-07-29 20:31:18 +00:00
parent bc61853ff3
commit 1ed82f8b40
2 changed files with 242 additions and 230 deletions

View File

@ -46,7 +46,7 @@ int termmode; /* 0 = tty, 1 = ANSI */
void TermInit(int mode) void TermInit(int mode)
{ {
termmode = mode; termmode = mode;
} }
@ -56,10 +56,11 @@ void TermInit(int mode)
*/ */
void Enter(int num) void Enter(int num)
{ {
int i; int i;
for(i = 0; i < num; i++) for (i = 0; i < num; i++)
printf("\n"); fprintf(stdout, "\n");
fflush(stdout);
} }
@ -67,24 +68,26 @@ void Enter(int num)
void pout(int fg, int bg, char *Str) void pout(int fg, int bg, char *Str)
{ {
colour(fg, bg); colour(fg, bg);
printf(Str); fprintf(stdout, Str);
fflush(stdout);
} }
void poutCenter(int fg, int bg, char *Str) void poutCenter(int fg, int bg, char *Str)
{ {
colour(fg, bg); colour(fg, bg);
Center(Str); Center(Str);
} }
void poutCR(int fg, int bg, char *Str) void poutCR(int fg, int bg, char *Str)
{ {
colour(fg, bg); colour(fg, bg);
puts(Str); fputs(Str, stdout);
fflush(stdout);
} }
@ -94,83 +97,87 @@ void poutCR(int fg, int bg, char *Str)
*/ */
void colour(int fg, int bg) void colour(int fg, int bg)
{ {
if (termmode == 1) { if (termmode == 1) {
int att=0, fore=37, back=40; int att=0, fore=37, back=40;
if (fg<0 || fg>31 || bg<0 || bg>7) { if (fg<0 || fg>31 || bg<0 || bg>7) {
printf("ANSI: Illegal colour specified: %i, %i\n", fg, bg); fprintf(stdout, "ANSI: Illegal colour specified: %i, %i\n", fg, bg);
return; fflush(stdout);
} return;
printf("[");
if ( fg > 15) {
printf("5;");
fg-=16;
}
if (fg > 7) {
att=1;
fg=fg-8;
}
if (fg==0) fore=30;
else if (fg==1) fore=34;
else if (fg==2) fore=32;
else if (fg==3) fore=36;
else if (fg==4) fore=31;
else if (fg==5) fore=35;
else if (fg==6) fore=33;
else fore=37;
if (bg==1) back=44;
else if (bg==2) back=42;
else if (bg==3) back=46;
else if (bg==4) back=41;
else if (bg==5) back=45;
else if (bg==6) back=43;
else if (bg==7) back=47;
else back=40;
printf("%d;%d;%dm", att, fore, back);
} }
fprintf(stdout, "[");
if ( fg > 15) {
fprintf(stdout, "5;");
fg-=16;
}
if (fg > 7) {
att=1;
fg=fg-8;
}
if (fg==0) fore=30;
else if (fg==1) fore=34;
else if (fg==2) fore=32;
else if (fg==3) fore=36;
else if (fg==4) fore=31;
else if (fg==5) fore=35;
else if (fg==6) fore=33;
else fore=37;
if (bg==1) back=44;
else if (bg==2) back=42;
else if (bg==3) back=46;
else if (bg==4) back=41;
else if (bg==5) back=45;
else if (bg==6) back=43;
else if (bg==7) back=47;
else back=40;
fprintf(stdout, "%d;%d;%dm", att, fore, back);
fflush(stdout);
}
} }
void Center(char *string) void Center(char *string)
{ {
int Strlen; int Strlen;
int Maxlen = 70; int Maxlen = 70;
int i, x, z; int i, x, z;
char *Str; char *Str;
Str = calloc(81, sizeof(char)); Str = calloc(81, sizeof(char));
Strlen = strlen(string); Strlen = strlen(string);
if(Strlen == Maxlen) if (Strlen == Maxlen)
printf("%s\n", string); fprintf(stdout, "%s\n", string);
else { else {
x = Maxlen - Strlen; x = Maxlen - Strlen;
z = x / 2; z = x / 2;
for(i = 0; i < z; i++) for (i = 0; i < z; i++)
strcat(Str, " "); strcat(Str, " ");
strcat(Str, string); strcat(Str, string);
printf("%s\n", Str); fprintf(stdout, "%s\n", Str);
} }
free(Str); fflush(stdout);
free(Str);
} }
void clear() void clear()
{ {
if (termmode == 1) { if (termmode == 1) {
colour(LIGHTGRAY, BLACK); colour(LIGHTGRAY, BLACK);
printf(ANSI_HOME); fprintf(stdout, ANSI_HOME);
printf(ANSI_CLEAR); fprintf(stdout, ANSI_CLEAR);
} else fflush(stdout);
Enter(1); } else
Enter(1);
} }
@ -180,38 +187,42 @@ void clear()
*/ */
void locate(int y, int x) void locate(int y, int x)
{ {
if (termmode > 0) { if (termmode > 0) {
if (exitinfo.iScreenLen != 0) { if (exitinfo.iScreenLen != 0) {
if (y > exitinfo.iScreenLen || x > 80) { if (y > exitinfo.iScreenLen || x > 80) {
printf("ANSI: Invalid screen coordinates: %i, %i\n", y, x); fprintf(stdout, "ANSI: Invalid screen coordinates: %i, %i\n", y, x);
printf("ANSI: exitinfo.iScreenLen: %i\n", exitinfo.iScreenLen); fprintf(stdout, "ANSI: exitinfo.iScreenLen: %i\n", exitinfo.iScreenLen);
return; fflush(stdout);
} return;
} else { }
if (y > 25 || x > 80) { } else {
printf("ANSI: Invalid screen coordinates: %i, %i\n", y, x); if (y > 25 || x > 80) {
return; fprintf(stdout, "ANSI: Invalid screen coordinates: %i, %i\n", y, x);
} fflush(stdout);
} return;
printf("\x1B[%i;%iH", y, x); }
} }
fprintf(stdout, "\x1B[%i;%iH", y, x);
fflush(stdout);
}
} }
void fLine(int Len) void fLine(int Len)
{ {
int x; int x;
if (termmode == 0) if (termmode == 0)
for (x = 0; x < Len; x++) for (x = 0; x < Len; x++)
printf("-"); fprintf(stdout, "-");
if (termmode == 1) if (termmode == 1)
for (x = 0; x < Len; x++) for (x = 0; x < Len; x++)
printf("%c", 196); fprintf(stdout, "%c", 196);
printf(" \n"); fprintf(stdout, " \n");
fflush(stdout);
} }
@ -219,7 +230,7 @@ void fLine(int Len)
void sLine() void sLine()
{ {
fLine(79); fLine(79);
} }
@ -229,18 +240,19 @@ void sLine()
*/ */
void mvprintw(int y, int x, const char *format, ...) void mvprintw(int y, int x, const char *format, ...)
{ {
char *outputstr; char *outputstr;
va_list va_ptr; va_list va_ptr;
outputstr = calloc(2048, sizeof(char)); outputstr = calloc(2048, sizeof(char));
va_start(va_ptr, format); va_start(va_ptr, format);
vsprintf(outputstr, format, va_ptr); vsprintf(outputstr, format, va_ptr);
va_end(va_ptr); va_end(va_ptr);
locate(y, x); locate(y, x);
printf(outputstr); fprintf(stdout, outputstr);
free(outputstr); free(outputstr);
fflush(stdout);
} }

View File

@ -51,25 +51,25 @@ int bbs_free;
void clrtoeol() void clrtoeol()
{ {
int i; int i;
printf("\r"); fprintf(stdout, "\r");
for (i = 0; i < COLS; i++) for (i = 0; i < COLS; i++)
putchar(' '); fputc(' ', stdout);
printf("\r"); fprintf(stdout, "\r");
fflush(stdout); fflush(stdout);
} }
void hor_lin(int y, int x, int len) void hor_lin(int y, int x, int len)
{ {
int i; int i;
locate(y, x); locate(y, x);
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
putchar('-'); fputc('-', stdout);
fflush(stdout); fflush(stdout);
} }
@ -99,51 +99,51 @@ static time_t lasttime;
*/ */
void show_date(int fg, int bg, int y, int x) void show_date(int fg, int bg, int y, int x)
{ {
time_t now; time_t now;
char *p, buf[128]; char *p, buf[128];
now = time(NULL); now = time(NULL);
if (now != lasttime) { if (now != lasttime) {
lasttime = now; lasttime = now;
set_color(LIGHTGREEN, BLUE); set_color(LIGHTGREEN, BLUE);
p = ctime(&now); p = ctime(&now);
Striplf(p); Striplf(p);
mvprintw(1, 44, (char *)"%s TZUTC %s", p, gmtoffset(now)); mvprintw(1, 44, (char *)"%s TZUTC %s", p, gmtoffset(now));
p = asctime(gmtime(&now)); p = asctime(gmtime(&now));
Striplf(p); Striplf(p);
mvprintw(2, 44, (char *)"%s UTC", p); mvprintw(2, 44, (char *)"%s UTC", p);
/* /*
* Indicator if bbs is free * Indicator if bbs is free
*/ */
strcpy(buf, SockR("SFRE:0;")); strcpy(buf, SockR("SFRE:0;"));
if (strncmp(buf, "100:0;", 6) == 0) { if (strncmp(buf, "100:0;", 6) == 0) {
strcpy(buf, SockR("SBBS:0;")); strcpy(buf, SockR("SBBS:0;"));
if (strncmp(buf, "100:2,1", 7) == 0) { if (strncmp(buf, "100:2,1", 7) == 0) {
set_color(WHITE, RED); set_color(WHITE, RED);
mvprintw(2,74, (char *)" Down "); mvprintw(2,74, (char *)" Down ");
} else { } else {
set_color(WHITE, BLUE); set_color(WHITE, BLUE);
mvprintw(2,74, (char *)" Free "); mvprintw(2,74, (char *)" Free ");
} }
bbs_free = TRUE; bbs_free = TRUE;
} else { } else {
set_color(WHITE, RED); set_color(WHITE, RED);
mvprintw(2,74, (char *)" Busy "); mvprintw(2,74, (char *)" Busy ");
bbs_free = FALSE; bbs_free = FALSE;
}
if (y && x)
locate(y, x);
set_color(fg, bg);
} }
if (y && x)
locate(y, x);
set_color(fg, bg);
}
} }
void center_addstr(int y, char *s) void center_addstr(int y, char *s)
{ {
mvprintw(y, (COLS / 2) - (strlen(s) / 2), s); mvprintw(y, (COLS / 2) - (strlen(s) / 2), s);
} }
@ -153,33 +153,33 @@ void center_addstr(int y, char *s)
*/ */
void screen_start(char *name) void screen_start(char *name)
{ {
int i; int i;
TermInit(1); TermInit(1);
/* /*
* Overwrite screen the first time, if user had it black on white * Overwrite screen the first time, if user had it black on white
* it will change to white on black. clear() won't do the trick. * it will change to white on black. clear() won't do the trick.
*/ */
set_color(LIGHTGRAY, BLUE); set_color(LIGHTGRAY, BLUE);
locate(1, 1); locate(1, 1);
for (i = 0; i < LINES; i++) { for (i = 0; i < LINES; i++) {
if (i == 3) if (i == 3)
colour(LIGHTGRAY, BLACK); colour(LIGHTGRAY, BLACK);
clrtoeol(); clrtoeol();
if (i < LINES) if (i < LINES)
printf("\n"); fprintf(stdout, "\n");
} }
fflush(stdout); fflush(stdout);
set_color(WHITE, BLUE); set_color(WHITE, BLUE);
locate(1, 1); locate(1, 1);
printf((char *)"%s for MBSE BBS version %s", name, VERSION); printf((char *)"%s for MBSE BBS version %s", name, VERSION);
set_color(YELLOW, BLUE); set_color(YELLOW, BLUE);
locate(2, 1); locate(2, 1);
printf((char *)SHORTRIGHT); printf((char *)SHORTRIGHT);
set_color(LIGHTGRAY, BLACK); set_color(LIGHTGRAY, BLACK);
show_date(LIGHTGRAY, BLACK, 0, 0); show_date(LIGHTGRAY, BLACK, 0, 0);
fflush(stdout); fflush(stdout);
} }
@ -189,9 +189,9 @@ void screen_start(char *name)
*/ */
void screen_stop() void screen_stop()
{ {
set_color(LIGHTGRAY, BLACK); set_color(LIGHTGRAY, BLACK);
clear(); clear();
fflush(stdout); fflush(stdout);
} }
@ -201,56 +201,56 @@ void screen_stop()
*/ */
void working(int txno, int y, int x) void working(int txno, int y, int x)
{ {
int i; int i;
if (init) if (init)
return; return;
/* /*
* If txno not 0 there will be something written. The * If txno not 0 there will be something written. The
* reversed attributes for mono, or white on red for * reversed attributes for mono, or white on red for
* color screens is set. The cursor is turned off and * color screens is set. The cursor is turned off and
* original cursor position is saved. * original cursor position is saved.
*/ */
show_date(LIGHTGRAY, BLACK, 0, 0); show_date(LIGHTGRAY, BLACK, 0, 0);
if (txno != 0) if (txno != 0)
set_color(YELLOW, RED); set_color(YELLOW, RED);
else else
set_color(LIGHTGRAY, BLACK); set_color(LIGHTGRAY, BLACK);
switch (txno) { switch (txno) {
case 0: mvprintw(4, 66, (char *)" "); case 0: mvprintw(4, 66, (char *)" ");
break; break;
case 1: mvprintw(4, 66, (char *)"Working . . ."); case 1: mvprintw(4, 66, (char *)"Working . . .");
break; break;
case 2: mvprintw(4, 66, (char *)">>> ERROR <<<"); case 2: mvprintw(4, 66, (char *)">>> ERROR <<<");
for (i = 1; i <= 5; i++) { for (i = 1; i <= 5; i++) {
putchar(7); fputc(7, stdout);
fflush(stdout); fflush(stdout);
usleep(150000); usleep(150000);
} }
usleep(550000); usleep(550000);
break; break;
case 3: mvprintw(4, 66, (char *)"Form inserted"); case 3: mvprintw(4, 66, (char *)"Form inserted");
putchar(7); fputc(7, stdout);
fflush(stdout); fflush(stdout);
sleep(1); sleep(1);
break; break;
case 4: mvprintw(4, 66, (char *)"Form deleted "); case 4: mvprintw(4, 66, (char *)"Form deleted ");
putchar(7); fputc(7, stdout);
fflush(stdout); fflush(stdout);
sleep(1); sleep(1);
break; break;
case 5: mvprintw(4, 66, (char *)"Moving . . . "); case 5: mvprintw(4, 66, (char *)"Moving . . . ");
break; break;
} }
show_date(LIGHTGRAY, BLACK, 0, 0); show_date(LIGHTGRAY, BLACK, 0, 0);
set_color(LIGHTGRAY, BLACK); set_color(LIGHTGRAY, BLACK);
if (y && x) if (y && x)
locate(y, x); locate(y, x);
fflush(stdout); fflush(stdout);
} }
@ -260,13 +260,13 @@ void working(int txno, int y, int x)
*/ */
void clr_index() void clr_index()
{ {
int i; int i;
set_color(LIGHTGRAY, BLACK); set_color(LIGHTGRAY, BLACK);
for (i = 3; i <= (LINES - 1); i++) { for (i = 3; i <= (LINES - 1); i++) {
locate(i, 1); locate(i, 1);
clrtoeol(); clrtoeol();
} }
} }
@ -276,31 +276,31 @@ void clr_index()
*/ */
void showhelp(char *T) void showhelp(char *T)
{ {
int f, i, x, forlim; int f, i, x, forlim;
f = FALSE; f = FALSE;
locate(LINES-1, 1); locate(LINES-1, 1);
set_color(WHITE, RED); set_color(WHITE, RED);
clrtoeol(); clrtoeol();
x = 0; x = 0;
forlim = strlen(T); forlim = strlen(T);
for (i = 0; i < forlim; i++) { for (i = 0; i < forlim; i++) {
if (T[i] == '^') { if (T[i] == '^') {
if (f == FALSE) { if (f == FALSE) {
f = TRUE; f = TRUE;
set_color(YELLOW, RED); set_color(YELLOW, RED);
} else { } else {
f = FALSE; f = FALSE;
set_color(WHITE, RED); set_color(WHITE, RED);
} }
} else { } else {
putchar(T[i]); fputc(T[i], stdout);
x++; x++;
}
} }
set_color(LIGHTGRAY, BLACK); }
fflush(stdout); set_color(LIGHTGRAY, BLACK);
fflush(stdout);
} }