[mw-devel] MW3 r1049 - trunk/src
pwb at sucs.org
pwb at sucs.org
Sat Jul 19 15:39:18 BST 2008
Author: pwb
Date: 2008-07-19 15:39:17 +0100 (Sat, 19 Jul 2008)
New Revision: 1049
Modified:
trunk/src/strings.c
Log:
Abstract the "turn '\033' into "[ESC]" part of escprintf into another function. Also fixes a buffer overflow.
Modified: trunk/src/strings.c
===================================================================
--- trunk/src/strings.c 2008-07-16 14:38:54 UTC (rev 1048)
+++ trunk/src/strings.c 2008-07-19 14:39:17 UTC (rev 1049)
@@ -373,13 +373,46 @@
}
}
+/* replace ESC characters ('\033') with "[ESC]", maybe in black on white */
+void strip_esc(char *const in, int hilite)
+{
+ static char szOutText[MAXTEXTLENGTH];
+ int oldlen = strlen(in), nInPos, nOutPos;
+ if (oldlen >= MAXTEXTLENGTH-1) oldlen = MAXTEXTLENGTH;
+ for (nInPos = 0, nOutPos = 0; nInPos < oldlen && nOutPos < MAXTEXTLENGTH-1; nInPos++)
+ {
+ /* escape character found */
+ if (in[nInPos] == 033)
+ {
+ const char*const esc = "\033[7m[ESC]\033[0m";
+ const char*const noesc = "[ESC]";
+ if (hilite) {
+ /* like strncpy, but avoid writing incomplete escape sequences */
+ if (nOutPos >= (MAXTEXTLENGTH << 1) + strlen(esc)) {
+ break; /* not enough space left */
+ }
+ strcpy(szOutText+nOutPos, esc);
+ nOutPos += strlen(esc);
+ } else {
+ strncpy(szOutText+nOutPos, noesc, MAXTEXTLENGTH-nOutPos-1);
+ nOutPos += strlen(noesc);
+ }
+ }
+ /* normal character */
+ else
+ {
+ szOutText[nOutPos++] = in[nInPos];
+ }
+ }
+ szOutText[nOutPos] = 0;
+ strcpy(in, szOutText);
+}
+
void escprintf(char *szFormat, ...)
{
static char szText[MAXTEXTLENGTH];
- static char szOutText[MAXTEXTLENGTH << 1];
va_list vaList;
- int nInPos, nOutPos;
/* get text to process */
va_start(vaList, szFormat);
@@ -387,32 +420,7 @@
va_end(vaList);
/* process control characters into strings */
- for (nInPos = 0, nOutPos = 0; nInPos < strlen(szText); nInPos++)
- {
- /* escape character found */
- if (szText[nInPos] == 27)
- {
- szOutText[nOutPos++] = 033;
- szOutText[nOutPos++] = '[';
- szOutText[nOutPos++] = '7';
- szOutText[nOutPos++] = 'm';
- szOutText[nOutPos++] = '[';
- szOutText[nOutPos++] = 'E';
- szOutText[nOutPos++] = 'S';
- szOutText[nOutPos++] = 'C';
- szOutText[nOutPos++] = ']';
- szOutText[nOutPos++] = 033;
- szOutText[nOutPos++] = '[';
- szOutText[nOutPos++] = '0';
- szOutText[nOutPos++] = 'm';
- }
- /* normal character */
- else
- {
- szOutText[nOutPos++] = szText[nInPos];
- }
- }
- szOutText[nOutPos] = 0;
+ strip_esc(szText,1);
- printf("%s", szOutText);
+ printf("%s", szText);
}
More information about the mw-devel
mailing list