00001
00005
#include <stdio.h>
00006
#include <stdlib.h>
00007
#include <string.h>
00008
#include <ctype.h>
00009
00010
#include "wattcp.h"
00011
#include "misc.h"
00012
#include "strings.h"
00013
00014
00015
00016
00017
#include "nochkstk.h"
00018
00019
#ifdef __WATCOMC__
00020
extern void _outchar (
char c);
00021
#pragma aux _outchar = \
00022
"mov ah, 2" \
00023
"int 21h" \
00024
parm [dl] \
00025
modify [ax];
00026
#endif
00027
00028
00029
00030
00031
static void outch (
char chr)
00032 {
00033
if (chr == (
char)0x1A)
00034
return;
00035
00036
#if defined(WIN32)
00037
{
00038 HANDLE hnd = GetStdHandle (STD_OUTPUT_HANDLE);
00039
00040
if (hnd != INVALID_HANDLE_VALUE)
00041 WriteConsoleA (hnd, &chr, 1, NULL, NULL);
00042 }
00043
#elif !(DOSX)
00044
00045
00046 {
00047
union REGS r;
00048 r.h.ah = 2;
00049 r.h.dl = chr;
00050 intdos (&r, &r);
00051 }
00052
00053
#elif (DOSX & POWERPAK)
00054
{
00055
union REGS r;
00056 r.h.ah = 2;
00057 r.h.dl = chr;
00058 int386 (0x21, &r, &r);
00059 }
00060
00061
#elif defined(__BORLANDC__)
00062 _ES = _DS;
00063 _DL = chr;
00064 _AL = 2;
00065 geninterrupt (0x21);
00066
00067
#elif defined(_MSC_VER) || defined(__DMC__)
00068
asm mov dl, chr
00069
asm mov ah, 2
00070
asm int 21h
00071
00072
#elif defined(__CCDL__)
00073
asm mov dl, [chr]
00074
asm mov ah, 2
00075
asm int 0x21
00076
00077
#elif defined(__WATCOMC__)
00078
_outchar (chr);
00079
00080
#elif defined(__HIGHC__)
00081 _inline (0x8A,0x55,0x08);
00082 _inline (0xB4,0x02);
00083 _inline (0xCD,0x21);
00084
00085
#elif defined(__GNUC__)
00086
__asm__ __volatile__
00087 (
"movb %b0, %%dl\n\t"
00088
"movb $2, %%ah\n\t"
00089
"int $0x21\n\t"
00090 :
00091 :
"r" (chr)
00092 :
"%eax",
"%edx" );
00093
00094
#else
00095
#error Tell me how to do this
00096
#endif
00097
}
00098
00099 void (*_outch)(
char c) = outch;
00100
00101
00102
00103
#if defined(USE_DEBUG)
00104
#if defined(WATT32_DOS_DLL)
00105 int (MS_CDECL *_printf) (
const char*, ...) = NULL;
00106
#else
00107
int (MS_CDECL *_printf) (
const char*, ...) = printf;
00108
#endif
00109
#else
00110
static int MS_CDECL EmptyPrint (
const char *fmt, ...)
00111 {
00112 outsnl (
"`(*_printf)()' called outside `USE_DEBUG'");
00113 ARGSUSED (fmt);
00114
return (0);
00115 }
00116 int (MS_CDECL *_printf) (
const char*, ...) = EmptyPrint;
00117
#endif
00118
00119
00120
00121
void outs (
const char *s)
00122 {
00123
if (!_outch)
00124
return;
00125
00126
while (s && *s)
00127 {
00128
if (*s ==
'\n')
00129 (*_outch) (
'\r');
00130 (*_outch) (*s++);
00131 }
00132 }
00133
00134
void outsnl (
const char *s)
00135 {
00136
if (!_outch)
00137
return;
00138
00139 outs (s);
00140 (*_outch) (
'\r');
00141 (*_outch) (
'\n');
00142 }
00143
00144
void outsn (
const char *s,
int n)
00145 {
00146
if (!_outch)
00147
return;
00148
00149
while (*s !=
'\0' && n-- >= 0)
00150 {
00151
if (*s ==
'\n')
00152 (*_outch) (
'\r');
00153 (*_outch) (*s++);
00154 }
00155 }
00156
00157
void outhex (
char c)
00158 {
00159
char lo, hi;
00160
00161
if (!_outch)
00162
return;
00163
00164 hi = (c & 0xF0) >> 4;
00165 lo = c & 15;
00166
00167
if (hi > 9)
00168 (*_outch) ((
char)(hi-10+
'A'));
00169
else (*_outch) ((
char)(hi+
'0'));
00170
00171
if (lo > 9)
00172 (*_outch) ((
char)(lo-10+
'A'));
00173
else (*_outch) ((
char)(lo+
'0'));
00174 }
00175
00176
void outhexes (
const char *s,
int n)
00177 {
00178
if (!_outch)
00179
return;
00180
00181
while (n-- > 0)
00182 {
00183 outhex (*s++);
00184 (*_outch) (
' ');
00185 }
00186 }
00187
00193 char *
rip (
char *s)
00194 {
00195
char *p;
00196
00197
if ((p = strrchr(s,
'\n')) != NULL) *p =
'\0';
00198
if ((p = strrchr(s,
'\r')) != NULL) *p =
'\0';
00199
return (s);
00200 }
00201
00207 BYTE atox (
const char *hex)
00208 {
00209
unsigned hi = toupper (hex[2]);
00210
unsigned lo = toupper (hex[3]);
00211
00212 hi -= isdigit (hi) ?
'0' :
'A'-10;
00213 lo -= isdigit (lo) ?
'0' :
'A'-10;
00214
return (
BYTE) ((hi << 4) + lo);
00215 }
00216
00220 char *
strreplace (
int ch1,
int ch2,
char *str)
00221 {
00222
char *s;
00223
00224 WATT_ASSERT (str != NULL);
00225
00226 s = str;
00227
while (*s)
00228 {
00229
if (*s == ch1)
00230 *s = ch2;
00231 s++;
00232 }
00233
return (str);
00234 }
00235
00236
00243 char *
StrLcpy (
char *dst,
const char *src, size_t len)
00244 {
00245 WATT_ASSERT (src != NULL);
00246 WATT_ASSERT (dst != NULL);
00247 WATT_ASSERT (len > 0);
00248
00249
if (strlen(src) < len)
00250
return strcpy (dst, src);
00251
00252 memcpy (dst, src, len);
00253 dst [len-1] =
'\0';
00254
return (dst);
00255 }
00256
00260 char *
strltrim (
const char *s)
00261 {
00262 WATT_ASSERT (s != NULL);
00263
00264
while (s[0] && s[1] && (s[0] ==
' ' || s[0] ==
'\t'))
00265 s++;
00266
return (
char*)s;
00267 }
00268
00272 char *
strrtrim (
char *s)
00273 {
00274 size_t n;
00275
00276 WATT_ASSERT (s != NULL);
00277
00278 n = strlen (s);
00279
while (n)
00280 {
00281
if (!isspace(s[--n]))
00282
break;
00283 s[n] =
'\0';
00284 }
00285
return (s);
00286 }
00287
00294 size_t
strntrimcpy (
char *dst,
const char *src, size_t n)
00295 {
00296 size_t len;
00297
const char *s;
00298
00299 WATT_ASSERT (src != NULL);
00300 WATT_ASSERT (dst != NULL);
00301
00302 len = 0;
00303 s = src;
00304
00305
if (n && s[0])
00306 {
00307
while (isspace(*s))
00308 ++s;
00309 len = strlen (s);
00310
if (len)
00311 {
00312
const char *e = &s[len-1];
00313
while (isspace(*e))
00314 {
00315 --e;
00316 --len;
00317 }
00318
if (len > n)
00319 len = n;
00320 strncpy (dst, s, len);
00321 }
00322 }
00323
if (len < n)
00324 dst[len] =
'\0';
00325
return (len);
00326 }
00327
00328
#ifdef NOT_USED
00329
int isstring (
const char *str, size_t len)
00330 {
00331
if (strlen(str) > len-1)
00332
return (0);
00333
00334
while (*str)
00335 {
00336
if (!isprint(*str++))
00337 {
00338 str--;
00339
if (!isspace(*str++))
00340
return (0);
00341 }
00342 }
00343
return (1);
00344 }
00345
#endif