[5298] | 1 | /* $Id: vprintf.c,v 1.10 2001-03-11 16:47:45 bird Exp $
|
---|
[1271] | 2 | *
|
---|
| 3 | * vprintf and printf
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) 1999 knut st. osmundsen
|
---|
| 6 | *
|
---|
[1678] | 7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 8 | *
|
---|
[1271] | 9 | */
|
---|
| 10 |
|
---|
| 11 | /*******************************************************************************
|
---|
| 12 | * Defined Constants *
|
---|
| 13 | *******************************************************************************/
|
---|
| 14 | #define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
|
---|
| 15 | #define MAX(a, b) ((a) >= (b) ? (a) : (b))
|
---|
| 16 | #define MIN(a, b) ((a) < (b) ? (a) : (b))
|
---|
| 17 |
|
---|
| 18 | #define NTSF_CAPITAL 0x0001
|
---|
| 19 | #define NTSF_LEFT 0x0002
|
---|
| 20 | #define NTSF_ZEROPAD 0x0004
|
---|
| 21 | #define NTSF_SPECIAL 0x0008
|
---|
| 22 | #define NTSF_VALSIGNED 0x0010
|
---|
| 23 | #define NTSF_PLUS 0x0020
|
---|
| 24 | #define NTSF_BLANK 0x0040
|
---|
| 25 |
|
---|
| 26 |
|
---|
[4164] | 27 |
|
---|
| 28 | #if defined(RING0) && !defined(R3TST)
|
---|
| 29 | #define COMOUTPUT
|
---|
| 30 | #endif
|
---|
| 31 | #if !defined(RING0) || defined(R3TST)
|
---|
| 32 | #define DOSWRITEOUTPUT
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #ifndef DOSWRITEOUTPUT
|
---|
[1678] | 36 | #define INCL_NOAPI
|
---|
| 37 | #else
|
---|
| 38 | #define INCL_DOSPROCESS
|
---|
| 39 | #define INCL_DOSERRORS
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
[1271] | 42 | /*******************************************************************************
|
---|
| 43 | * Header Files *
|
---|
| 44 | *******************************************************************************/
|
---|
| 45 | #include <os2.h>
|
---|
| 46 |
|
---|
| 47 | #include <stdarg.h>
|
---|
| 48 |
|
---|
[4164] | 49 | #include "devSegDf.h" /* Win32k segment definitions. */
|
---|
[1467] | 50 | #include "dev32.h"
|
---|
| 51 | #include "vprintf.h"
|
---|
[4164] | 52 | #ifdef COMOUTPUT
|
---|
| 53 | #include <builtin.h>
|
---|
| 54 | #endif
|
---|
[1271] | 55 | #ifdef RING0
|
---|
[1467] | 56 | #include "options.h"
|
---|
[1271] | 57 | #endif
|
---|
[1536] | 58 | #include "Yield.h"
|
---|
[1271] | 59 |
|
---|
| 60 |
|
---|
| 61 | /*******************************************************************************
|
---|
[1467] | 62 | * Global Variables *
|
---|
| 63 | *******************************************************************************/
|
---|
| 64 | static char chNewLine = '\n';
|
---|
| 65 | static char chReturn = '\r';
|
---|
| 66 |
|
---|
| 67 | /*******************************************************************************
|
---|
[1271] | 68 | * Internal Functions *
|
---|
| 69 | *******************************************************************************/
|
---|
| 70 | static int _atoi_skip(const char **ppsz);
|
---|
| 71 | static unsigned _strnlen(const char *psz, unsigned cchMax);
|
---|
[1467] | 72 | static void chout(int ch);
|
---|
| 73 | static char * strout(char *psz, signed cchMax);
|
---|
[1271] | 74 |
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Read an integer (decimal) and update the string pointer while reading.
|
---|
| 78 | * Do not support sign values.
|
---|
| 79 | * @returns Integer value.
|
---|
| 80 | * @param ppsz Pointer to pointer to string. The string to be converted.
|
---|
| 81 | */
|
---|
| 82 | static int _atoi_skip(const char **ppsz)
|
---|
| 83 | {
|
---|
| 84 | int iValue = 0;
|
---|
| 85 |
|
---|
| 86 | while (ISDIGIT(**ppsz))
|
---|
| 87 | {
|
---|
| 88 | iValue *= 10;
|
---|
| 89 | iValue += **ppsz - '0';
|
---|
| 90 | (*ppsz)++;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return iValue;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Finds the length of a string up to cchMax.
|
---|
| 99 | * @returns Length.
|
---|
| 100 | * @param psz Pointer to string.
|
---|
| 101 | * @param cchMax Max length.
|
---|
| 102 | */
|
---|
| 103 | static unsigned _strnlen(const char *psz, unsigned cchMax)
|
---|
| 104 | {
|
---|
| 105 | const char *pszC = psz;
|
---|
| 106 |
|
---|
[4773] | 107 | while (cchMax-- > 0 && *psz != '\0')
|
---|
[1271] | 108 | psz++;
|
---|
| 109 |
|
---|
| 110 | return psz - pszC;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | /**
|
---|
| 115 | * Formats a number according to the parameters.
|
---|
| 116 | * @returns Pointer to next char. (after number)
|
---|
| 117 | * @param psz Pointer to output string.
|
---|
| 118 | * @param lValue Value
|
---|
| 119 | * @param uiBase Number representation base.
|
---|
| 120 | * @param cchWidth Width
|
---|
| 121 | * @param cchPrecision Precision.
|
---|
| 122 | * @param fFlags Flags (NTFS_*).
|
---|
| 123 | */
|
---|
| 124 | static char * numtostr(long lValue, unsigned int uiBase,
|
---|
| 125 | signed int cchWidth, signed int cchPrecision,
|
---|
| 126 | unsigned int fFlags
|
---|
| 127 | )
|
---|
| 128 | {
|
---|
| 129 | char * achDigits = "0123456789abcdef";
|
---|
| 130 | int cchValue;
|
---|
| 131 | unsigned long ul;
|
---|
| 132 | int i;
|
---|
| 133 | int j;
|
---|
| 134 |
|
---|
| 135 | #ifdef DEBUG
|
---|
| 136 | if (uiBase < 2 || uiBase > 16)
|
---|
| 137 | return NULL;
|
---|
| 138 | #endif
|
---|
| 139 | if (fFlags & NTSF_CAPITAL)
|
---|
| 140 | achDigits = "0123456789ABCDEF";
|
---|
| 141 | if (fFlags & NTSF_LEFT)
|
---|
| 142 | fFlags &= ~NTSF_ZEROPAD;
|
---|
| 143 |
|
---|
| 144 | /* determin value length */
|
---|
| 145 | cchValue = 0;
|
---|
| 146 | ul = (unsigned long)((fFlags & NTSF_VALSIGNED) && lValue < 0L ? -lValue : lValue);
|
---|
| 147 | do
|
---|
| 148 | {
|
---|
| 149 | cchValue++;
|
---|
| 150 | ul /= uiBase;
|
---|
| 151 | } while (ul > 0);
|
---|
| 152 |
|
---|
| 153 | i = 0;
|
---|
| 154 | if (fFlags & NTSF_VALSIGNED)
|
---|
| 155 | {
|
---|
| 156 | if (lValue < 0)
|
---|
| 157 | {
|
---|
| 158 | lValue = -lValue;
|
---|
| 159 | chout('-');
|
---|
| 160 | i++;
|
---|
| 161 | }
|
---|
| 162 | else if (fFlags & (NTSF_PLUS | NTSF_BLANK))
|
---|
| 163 | {
|
---|
| 164 | chout(fFlags & NTSF_PLUS ? '+' : ' ');
|
---|
| 165 | i++;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | if (fFlags & NTSF_SPECIAL && (uiBase % 8) == 0)
|
---|
| 170 | {
|
---|
| 171 | chout('0');
|
---|
| 172 | i++;
|
---|
| 173 | if (uiBase == 16)
|
---|
| 174 | {
|
---|
| 175 | chout(fFlags & NTSF_CAPITAL ? 'X' : 'x');
|
---|
| 176 | i++;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | /* width - only if ZEROPAD */
|
---|
| 182 | cchWidth -= i + cchValue;
|
---|
| 183 | if (fFlags & NTSF_ZEROPAD)
|
---|
| 184 | while (--cchWidth >= 0)
|
---|
| 185 | {
|
---|
| 186 | chout('0');
|
---|
| 187 | cchPrecision--;
|
---|
| 188 | }
|
---|
| 189 | #if 0
|
---|
| 190 | else if (!(fFlags & NTSF_LEFT) && cchWidth > 0)
|
---|
[1467] | 191 | { /* not yet supported! */
|
---|
[1271] | 192 | /*
|
---|
| 193 | for (j = i-1; j >= 0; j--)
|
---|
| 194 | psz[cchWidth + j] = psz[j];
|
---|
| 195 | for (j = 0; j < cchWidth; j++)
|
---|
| 196 | psz[j] = ' ';
|
---|
| 197 | i += cchWidth;
|
---|
| 198 | */
|
---|
| 199 | }
|
---|
| 200 | #endif
|
---|
| 201 |
|
---|
| 202 | /* percision */
|
---|
| 203 | while (--cchPrecision >= cchValue)
|
---|
| 204 | chout('0');
|
---|
| 205 |
|
---|
| 206 | ul = 1;
|
---|
| 207 | for (i = 1; i < cchValue; i++)
|
---|
| 208 | ul *= uiBase;
|
---|
| 209 | for (i = 0; i < cchValue; i++)
|
---|
| 210 | {
|
---|
| 211 | chout(achDigits[lValue / ul]);
|
---|
| 212 | lValue %= ul;
|
---|
| 213 | ul /= uiBase;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /* width if NTSF_LEFT */
|
---|
| 217 | if (fFlags & NTSF_LEFT)
|
---|
| 218 | while (--cchWidth >= 0)
|
---|
| 219 | chout(' ');
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | return NULL;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | /**
|
---|
| 227 | * Partial vprintf implementation.
|
---|
[1678] | 228 | * This function checks the fLogging flag, and will therefore only write if logging is enabled.
|
---|
[1271] | 229 | * @returns number of
|
---|
| 230 | * @param pszFormat Format string.
|
---|
| 231 | * @param args Argument list.
|
---|
| 232 | */
|
---|
| 233 | int vprintf(const char *pszFormat, va_list args)
|
---|
| 234 | {
|
---|
[4164] | 235 | #if defined(RING0) && !defined(R3TST)
|
---|
[1678] | 236 | if (!options.fLogging)
|
---|
| 237 | return 0;
|
---|
[1683] | 238 | #endif
|
---|
[1678] | 239 | return vprintf2(pszFormat, args);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | #pragma info(notrd)
|
---|
| 243 | /**
|
---|
| 244 | * Partial vprintf implementation.
|
---|
| 245 | * This function don't check the fLogging flag, and will therefore allways write.
|
---|
| 246 | * @returns number of
|
---|
| 247 | * @param pszFormat Format string.
|
---|
| 248 | * @param args Argument list.
|
---|
| 249 | */
|
---|
| 250 | int vprintf2(const char *pszFormat, va_list args)
|
---|
| 251 | {
|
---|
[4164] | 252 | int cch = 0;
|
---|
[1467] | 253 |
|
---|
[1271] | 254 | while (*pszFormat != '\0')
|
---|
| 255 | {
|
---|
| 256 | if (*pszFormat == '%')
|
---|
| 257 | {
|
---|
[4164] | 258 | if (cch > 0)
|
---|
| 259 | {
|
---|
| 260 | strout((char*)(pszFormat - cch), cch);
|
---|
| 261 | cch = 0;
|
---|
| 262 | }
|
---|
[1467] | 263 |
|
---|
[1271] | 264 | pszFormat++; /* skip '%' */
|
---|
| 265 | if (*pszFormat == '%') /* '%%'-> '%' */
|
---|
| 266 | chout(*pszFormat++);
|
---|
| 267 | else
|
---|
| 268 | {
|
---|
| 269 | long lValue;
|
---|
| 270 | unsigned int fFlags = 0;
|
---|
| 271 | int cchWidth = -1;
|
---|
| 272 | int cchPrecision = -1;
|
---|
| 273 | unsigned int uiBase = 10;
|
---|
| 274 | char chArgSize;
|
---|
| 275 |
|
---|
| 276 | /* flags */
|
---|
| 277 | #pragma info(none)
|
---|
| 278 | while (1)
|
---|
| 279 | #pragma info(restore)
|
---|
| 280 | {
|
---|
| 281 | switch (*pszFormat++)
|
---|
| 282 | {
|
---|
| 283 | case '#': fFlags |= NTSF_SPECIAL; continue;
|
---|
| 284 | case '-': fFlags |= NTSF_LEFT; continue;
|
---|
| 285 | case '+': fFlags |= NTSF_PLUS; continue;
|
---|
| 286 | case ' ': fFlags |= NTSF_BLANK; continue;
|
---|
| 287 | case '0': fFlags |= NTSF_ZEROPAD; continue;
|
---|
| 288 | }
|
---|
| 289 | pszFormat--;
|
---|
| 290 | break;
|
---|
| 291 | }
|
---|
| 292 | /* width */
|
---|
| 293 | if (ISDIGIT(*pszFormat))
|
---|
| 294 | cchWidth = _atoi_skip(SSToDS(&pszFormat));
|
---|
| 295 | else if (*pszFormat == '*')
|
---|
| 296 | {
|
---|
| 297 | pszFormat++;
|
---|
| 298 | cchWidth = va_arg(args, int);
|
---|
| 299 | if (cchWidth < 0)
|
---|
| 300 | {
|
---|
| 301 | cchWidth = -cchWidth;
|
---|
| 302 | fFlags |= NTSF_LEFT;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | /* precision */
|
---|
| 307 | if (*pszFormat == '.')
|
---|
| 308 | {
|
---|
| 309 | pszFormat++;
|
---|
| 310 | if (ISDIGIT(*pszFormat))
|
---|
| 311 | cchPrecision = _atoi_skip(SSToDS(&pszFormat));
|
---|
| 312 | else if (*pszFormat == '*')
|
---|
| 313 | {
|
---|
| 314 | pszFormat++;
|
---|
| 315 | cchPrecision = va_arg(args, int);
|
---|
| 316 | }
|
---|
| 317 | if (cchPrecision < 0)
|
---|
| 318 | cchPrecision = 0;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | /* argsize */
|
---|
| 322 | chArgSize = *pszFormat;
|
---|
| 323 | if (chArgSize != 'l' && chArgSize != 'L' && chArgSize != 'H')
|
---|
| 324 | chArgSize = 0;
|
---|
| 325 | else
|
---|
| 326 | pszFormat++;
|
---|
| 327 |
|
---|
| 328 | /* type */
|
---|
| 329 | switch (*pszFormat++)
|
---|
| 330 | {
|
---|
| 331 | /* char */
|
---|
| 332 | case 'c':
|
---|
| 333 | if (!(fFlags & NTSF_LEFT))
|
---|
| 334 | while (--cchWidth > 0)
|
---|
| 335 | chout(' ');
|
---|
| 336 |
|
---|
| 337 | chout(va_arg(args, int));
|
---|
| 338 |
|
---|
| 339 | while (--cchWidth > 0)
|
---|
| 340 | chout(' ');
|
---|
| 341 | continue;
|
---|
| 342 |
|
---|
| 343 | case 'd': /* signed decimal integer */
|
---|
| 344 | case 'i':
|
---|
| 345 | fFlags |= NTSF_VALSIGNED;
|
---|
| 346 | break;
|
---|
| 347 |
|
---|
| 348 | case 'o':
|
---|
| 349 | uiBase = 8;
|
---|
| 350 | break;
|
---|
| 351 |
|
---|
| 352 | case 'p':
|
---|
| 353 | fFlags |= NTSF_SPECIAL; /* Note not standard behaviour (but I like it this way!) */
|
---|
| 354 | uiBase = 16;
|
---|
| 355 | break;
|
---|
| 356 |
|
---|
| 357 | case 's': /* string */
|
---|
| 358 | {
|
---|
| 359 | int cchStr;
|
---|
| 360 | char *pszStr = va_arg(args, char*);
|
---|
| 361 |
|
---|
| 362 | if (pszStr < (char*)0x10000)
|
---|
| 363 | pszStr = "<NULL>";
|
---|
| 364 | cchStr = _strnlen(pszStr, (unsigned)cchPrecision);
|
---|
| 365 | if (!(fFlags & NTSF_LEFT))
|
---|
| 366 | while (--cchWidth >= cchStr)
|
---|
| 367 | chout(' ');
|
---|
| 368 |
|
---|
[1467] | 369 | pszStr = strout(pszStr, cchStr);
|
---|
| 370 |
|
---|
[1271] | 371 | while (--cchWidth >= cchStr)
|
---|
| 372 | chout(' ');
|
---|
| 373 | continue;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | case 'u':
|
---|
| 377 | uiBase = 10;
|
---|
| 378 | break;
|
---|
| 379 |
|
---|
| 380 | case 'x':
|
---|
| 381 | case 'X':
|
---|
| 382 | uiBase = 16;
|
---|
| 383 | break;
|
---|
| 384 |
|
---|
| 385 | default:
|
---|
| 386 | continue;
|
---|
| 387 | }
|
---|
| 388 | /*-----------------*/
|
---|
| 389 | /* integer/pointer */
|
---|
| 390 | /*-----------------*/
|
---|
| 391 | /* get value */
|
---|
| 392 | if (pszFormat[-1] == 'p')
|
---|
| 393 | lValue = (signed long)va_arg(args, char *);
|
---|
| 394 | else if (chArgSize == 'l')
|
---|
| 395 | lValue = va_arg(args, signed long);
|
---|
| 396 | else if (chArgSize == 'h')
|
---|
[21916] | 397 | #ifdef __GNUC__
|
---|
| 398 | // shut up stupid warning and avoid stupid abort()
|
---|
| 399 | lValue = va_arg(args, int);
|
---|
| 400 | #else
|
---|
[1271] | 401 | lValue = va_arg(args, signed short);
|
---|
[21916] | 402 | #endif
|
---|
[1271] | 403 | else
|
---|
| 404 | lValue = va_arg(args, signed int);
|
---|
| 405 | numtostr(lValue, uiBase, cchWidth, cchPrecision, fFlags);
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 | else
|
---|
[1467] | 409 | {
|
---|
[4164] | 410 | cch++;
|
---|
| 411 | pszFormat++;
|
---|
[1467] | 412 | }
|
---|
[1271] | 413 | }
|
---|
| 414 |
|
---|
[4164] | 415 | if (cch > 0)
|
---|
| 416 | {
|
---|
| 417 | strout((char*)(pszFormat - cch), cch);
|
---|
| 418 | cch = 0;
|
---|
| 419 | }
|
---|
[1467] | 420 |
|
---|
[1271] | 421 | return 0UL;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | /* stub */
|
---|
| 425 | int _vprintfieee(const char *pszFormat, va_list args)
|
---|
| 426 | {
|
---|
| 427 | return vprintf(pszFormat, args);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 |
|
---|
| 431 | /**
|
---|
| 432 | * printf - wraps arguments into a vprintf call.
|
---|
| 433 | * @returns number of bytes written.
|
---|
| 434 | * @param pszFormat Pointer to format string.
|
---|
| 435 | * @param ... Optional parameters.
|
---|
| 436 | */
|
---|
| 437 | int printf(const char *pszFormat, ...)
|
---|
| 438 | {
|
---|
| 439 | int cch;
|
---|
| 440 | va_list arguments;
|
---|
| 441 |
|
---|
| 442 | va_start(arguments, pszFormat);
|
---|
| 443 | cch = vprintf(pszFormat, arguments);
|
---|
| 444 | va_end(arguments);
|
---|
| 445 |
|
---|
| 446 | return cch;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 |
|
---|
| 450 | /* stub */
|
---|
| 451 | int _printfieee(const char *pszFormat, ...)
|
---|
| 452 | {
|
---|
| 453 | int cch;
|
---|
| 454 | va_list arguments;
|
---|
| 455 |
|
---|
| 456 | va_start(arguments, pszFormat);
|
---|
| 457 | cch = vprintf(pszFormat, arguments);
|
---|
| 458 | va_end(arguments);
|
---|
| 459 |
|
---|
| 460 | return cch;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /* stub */
|
---|
| 464 | int _printf_ansi(const char *pszFormat, ...)
|
---|
| 465 | {
|
---|
| 466 | int cch;
|
---|
| 467 | va_list arguments;
|
---|
| 468 |
|
---|
| 469 | va_start(arguments, pszFormat);
|
---|
| 470 | cch = vprintf(pszFormat, arguments);
|
---|
| 471 | va_end(arguments);
|
---|
| 472 |
|
---|
| 473 | return cch;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 |
|
---|
| 477 |
|
---|
| 478 |
|
---|
| 479 | /**
|
---|
| 480 | * Writes a char to output device.
|
---|
| 481 | * @param ch Char to write.
|
---|
| 482 | * @status completely
|
---|
| 483 | * @author knut st. osmundsen
|
---|
| 484 | */
|
---|
[1467] | 485 | static void chout(int ch)
|
---|
[1271] | 486 | {
|
---|
[4164] | 487 | #ifdef DOSWRITEOUTPUT
|
---|
| 488 | ULONG ulWrote;
|
---|
| 489 | #endif
|
---|
[1271] | 490 |
|
---|
| 491 | if (ch != '\r')
|
---|
| 492 | {
|
---|
| 493 | if (ch == '\n')
|
---|
| 494 | {
|
---|
[4164] | 495 | #ifdef COMOUTPUT
|
---|
[2501] | 496 | #pragma info(noeff)
|
---|
[1467] | 497 | while (!(_inp(options.usCom + 5) & 0x20)); /* Waits for the port to be ready. */
|
---|
[4164] | 498 | _outp(options.usCom, chReturn); /* Put the char. */
|
---|
[2501] | 499 | #pragma info(restore)
|
---|
[1271] | 500 | #endif
|
---|
[4164] | 501 | #ifdef DOSWRITEOUTPUT
|
---|
| 502 | DosWrite(1, (void*)&chReturn, 1, SSToDS(&ulWrote));
|
---|
| 503 | #endif
|
---|
| 504 | }
|
---|
| 505 | #ifdef COMOUTPUT
|
---|
| 506 | #pragma info(noeff)
|
---|
| 507 | while (!(_inp(options.usCom + 5) & 0x20)); /* Waits for the port to be ready. */
|
---|
| 508 | _outp(options.usCom, ch); /* Put the char. */
|
---|
| 509 | #pragma info(restore)
|
---|
| 510 | Yield();
|
---|
| 511 | #endif
|
---|
| 512 | #ifdef DOSWRITEOUTPUT
|
---|
| 513 | DosWrite(1, SSToDS(&ch), 1, SSToDS(&ulWrote));
|
---|
| 514 | #endif
|
---|
[1271] | 515 | }
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[1467] | 518 |
|
---|
| 519 | /**
|
---|
| 520 | * Write a string to the output device.
|
---|
| 521 | * @returns pointer end of string.
|
---|
| 522 | * @param psz Pointer to the string to write.
|
---|
| 523 | * @param cchMax Max count of chars to write. (or until '\0')
|
---|
| 524 | * @status completely implemented.
|
---|
| 525 | * @author knut st. osmundsen
|
---|
| 526 | */
|
---|
| 527 | static char *strout(char *psz, signed cchMax)
|
---|
| 528 | {
|
---|
[1535] | 529 | int cchYield = 0;
|
---|
[1467] | 530 | while (cchMax > 0 && *psz != '\0')
|
---|
| 531 | {
|
---|
| 532 | ULONG cch = 0;
|
---|
| 533 | ULONG ul;
|
---|
| 534 |
|
---|
| 535 | while (cchMax - cch > 0 && psz[cch] != '\0' && psz[cch] != '\r' && psz[cch] != '\n')
|
---|
| 536 | cch++;
|
---|
| 537 |
|
---|
| 538 | /* write string part */
|
---|
[4164] | 539 | #ifdef COMOUTPUT
|
---|
| 540 | for (ul = 0; ul < cch; ul++)
|
---|
| 541 | {
|
---|
| 542 | #pragma info(noeff)
|
---|
| 543 | while (!(_inp(options.usCom + 5) & 0x20)); /* Waits for the port to be ready. */
|
---|
| 544 | _outp(options.usCom, psz[ul]); /* Put the char. */
|
---|
| 545 | #pragma info(restore)
|
---|
| 546 | }
|
---|
| 547 | #endif
|
---|
| 548 | #ifdef DOSWRITEOUTPUT
|
---|
| 549 | DosWrite(1, psz, cch, SSToDS(&ul));
|
---|
| 550 | #endif
|
---|
[1467] | 551 |
|
---|
| 552 | /* cr and lf check + skip */
|
---|
[4164] | 553 | if (cch < cchMax && (psz[cch] == '\n' || psz[cch] == '\r'))
|
---|
[1467] | 554 | {
|
---|
| 555 | if (psz[cch] == '\n')
|
---|
| 556 | {
|
---|
[4164] | 557 | #ifdef COMOUTPUT
|
---|
[2501] | 558 | #pragma info(noeff)
|
---|
[1467] | 559 | while (!(_inp(options.usCom + 5) & 0x20)); /* Waits for the port to be ready. */
|
---|
| 560 | _outp(options.usCom, chReturn); /* Put the char. */
|
---|
| 561 | while (!(_inp(options.usCom + 5) & 0x20)); /* Waits for the port to be ready. */
|
---|
| 562 | _outp(options.usCom, chNewLine); /* Put the char. */
|
---|
[4164] | 563 | cchYield++;
|
---|
[2501] | 564 | #pragma info(restore)
|
---|
[1467] | 565 | #endif
|
---|
[4164] | 566 | #ifdef DOSWRITEOUTPUT
|
---|
| 567 | DosWrite(1, (void*)&chReturn, 1, SSToDS(&ul));
|
---|
| 568 | DosWrite(1, (void*)&chNewLine, 1, SSToDS(&ul));
|
---|
| 569 | #endif
|
---|
[1467] | 570 | }
|
---|
[5298] | 571 | cch++;
|
---|
[1467] | 572 | }
|
---|
| 573 |
|
---|
| 574 | /* next */
|
---|
| 575 | psz += cch;
|
---|
| 576 | cchMax -= cch;
|
---|
[1535] | 577 | cchYield += cch;
|
---|
| 578 | if (cchYield > 3)
|
---|
| 579 | if (Yield())
|
---|
| 580 | cchYield = 0;
|
---|
[1467] | 581 | }
|
---|
| 582 | return psz;
|
---|
| 583 | }
|
---|
| 584 |
|
---|