I need help, I have lost the ability to follow logic and count above five. The follow gives me garbled output almost all of the time. I can not see where I went wrong, can anyone point me in the right direction?
void write_to_buffer( DESCRIPTOR_DATA *d, const char *txt, ... ) {
int length = 0;
bool inColor = IS_SET(d->telOpts, 15);
char outTemp[MAX_STRING_LENGTH];
char outColor[MAX_STRING_LENGTH];
char colBuf[16];
char *pnt = NULL, *lst = NULL;
int first = 0, jump = 0, oCtop = 0;
int fore = 0, back = 0;
if( d == NULL ) {
bug("Write_to_buffer: NULL descriptor");
return;
}
if( d->outbuf == NULL ) // Should never happen, but create if it is
CREATE(d->outbuf, char, d->outsize);
strcpy(outTemp, "");
{
va_list param;
va_start(param, txt);
vsprintf(outTemp + strlen(outTemp), txt, param);
va_end(param);
}
lst = outTemp;
if( inColor ) { //colorize output
first = 0; jump = 0; oCtop = 0;
while( (pnt = strchr(lst, '&')) != NULL ) {
if( pnt > lst ) {
first = pnt - lst;
fore = 9; back = 9;
if( *(pnt + 1) == '(' ) {
fore = getColor(*(pnt + 2));
if( *(pnt + 3) != ')' )
back = getColor(*(pnt + 3));
else
jump = 4;
if( jump == 0 && *(pnt + 4) == ')' )
jump = 5;
else
jump = 4;
} else {
fore = getColor(*(pnt + 1));
jump = 2;
}
strncpy(outColor + oCtop, lst, first);
oCtop += first;
sprintf(colBuf, "\033[0;3%d;4%dm", fore, back);
strncpy(outColor + oCtop, colBuf, 10);
oCtop += 10;
lst = pnt + jump;
} else {
break;
}
}
if( oCtop == 0 ) {
strcat(outColor, outTemp);
oCtop = strlen(outTemp);
} else {
strcat(outColor, "\033[0m");
oCtop += 4;
}
outColor[oCtop] = '\0';
} else { //strip color codes
first = 0; jump = 0; oCtop = 0;
while( (pnt = strchr(lst, '&')) != NULL ) {
if( pnt > lst ) {
first = pnt - lst;
if( *(pnt + 1) == '(' ) {
if( *(pnt + 3) == ')' )
jump = 4;
else if( *(pnt + 4) == ')' )
jump = 5;
} else {
jump = 2;
}
strncpy(outColor + oCtop, lst, first);
oCtop += first;
lst = pnt + jump;
} else {
break;
}
}
if( oCtop == 0 ) {
strcat(outColor, outTemp);
oCtop = strlen(outTemp);
}
outColor[oCtop] = '\0';
}
length = strlen(outColor);
// Expand the buffer as needed.
// This is not released until close_socket.
while( d->outtop + length >= d->outsize ) {
if( d->outsize > MAXOUTSIZE ) {
bug("Buffer overflow. Closing (%d).", d->uniqueNumber);
clear_outbuf(d);
close_socket(d, TRUE);
return;
}
d->outsize *= 2;
RECREATE(d->outbuf, char, d->outsize);
}
// Copy.
strncpy(d->outbuf + d->outtop, outColor, length);
d->outtop += length;
d->outbuf[d->outtop] = '\0';
return;
}
[\code]
|