| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
#include "jove.h" |
| 9 |
#include "chars.h" |
| 10 |
#include "fp.h" |
| 11 |
#include "jctype.h" |
| 12 |
#include "disp.h" |
| 13 |
#include "extend.h" |
| 14 |
#include "fmt.h" |
| 15 |
|
| 16 |
#ifdef MAC |
| 17 |
# include "mac.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
private void |
| 21 |
doformat(File *, const char *, va_list), |
| 22 |
pad(DAPchar, int); |
| 23 |
|
| 24 |
char mesgbuf[MESG_SIZE]; |
| 25 |
|
| 26 |
/* Formatting codes supported: |
| 27 |
* |
| 28 |
* %%: => '%' |
| 29 |
* %O, %D, %X: long => octal, decimal, or hex |
| 30 |
* %lo, %ld, %lx: long => octal, decimal, or hex |
| 31 |
* %o, %d, %x: int => octal, decimal, or hex |
| 32 |
* %c: char => character |
| 33 |
* %s: char* => string |
| 34 |
* |
| 35 |
* %b: buffer pointer => buffer's name |
| 36 |
* %f: void => current command's name |
| 37 |
* %n: int => int == 1? "" : "s" |
| 38 |
* %p: char => visible rep |
| 39 |
*/ |
| 40 |
|
| 41 |
void |
| 42 |
format(char *buf, size_t len, const char *fmt, va_list ap) |
| 43 |
{ |
| 44 |
File strbuf; |
| 45 |
|
| 46 |
strbuf.f_ptr = strbuf.f_base = buf; |
| 47 |
strbuf.f_fd = -1; /* Not legit for files */ |
| 48 |
strbuf.f_bufsize = strbuf.f_cnt = len; |
| 49 |
strbuf.f_flags = F_STRING; |
| 50 |
|
| 51 |
doformat(&strbuf, fmt, ap); |
| 52 |
f_putc('\0', &strbuf); /* f_putc will place this, even if overflow */ |
| 53 |
} |
| 54 |
|
| 55 |
/* pretty-print character c into buffer cp (up to PPWIDTH bytes) */ |
| 56 |
|
| 57 |
void |
| 58 |
PPchar(ZXchar c, char *cp) |
| 59 |
{ |
| 60 |
if (jisprint(c)) { |
| 61 |
cp[0] = c; |
| 62 |
cp[1] = '\0'; |
| 63 |
} else if (c < DEL) { |
| 64 |
strcpy(cp, "^?"); |
| 65 |
cp[1] = c +'@'; |
| 66 |
} else if (c == DEL) { |
| 67 |
strcpy(cp, "^?"); |
| 68 |
} else { |
| 69 |
cp[0] = '\\'; |
| 70 |
cp[1] = '0'+(c >> 6); |
| 71 |
cp[2] = '0'+((c >> 3) & 07); |
| 72 |
cp[3] = '0'+(c & 07); |
| 73 |
cp[4] = '\0'; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private struct fmt_state { |
| 78 |
int precision, |
| 79 |
width; |
| 80 |
jbool leftadj; |
| 81 |
char padc; |
| 82 |
File *iop; |
| 83 |
} current_fmt; |
| 84 |
|
| 85 |
/* TODO: Make this unsigned long when we dump support for pre-ANSI C (use flag to ask for sign?) */ |
| 86 |
private void |
| 87 |
putld(long d, int base) |
| 88 |
{ |
| 89 |
static const char chars[] = {'0', '1', '2', '3', '4', '5', '6', |
| 90 |
'7', '8', '9', 'a', 'b', 'c', 'd', |
| 91 |
'e', 'f'}; |
| 92 |
int len = 0; |
| 93 |
long tmpd = d; |
| 94 |
char ubuf[32], |
| 95 |
*ep = ubuf + sizeof(ubuf), |
| 96 |
*up = ep; |
| 97 |
|
| 98 |
if (d < 0) { |
| 99 |
len += 1; |
| 100 |
tmpd = -d; |
| 101 |
} |
| 102 |
if (current_fmt.width == 0 && current_fmt.precision) { |
| 103 |
current_fmt.width = current_fmt.precision; |
| 104 |
current_fmt.padc = '0'; |
| 105 |
} |
| 106 |
do { |
| 107 |
int i = tmpd % base; |
| 108 |
tmpd = tmpd / base; |
| 109 |
*--up = chars[i]; |
| 110 |
len += 1; |
| 111 |
} while (tmpd != 0); |
| 112 |
if (!current_fmt.leftadj) |
| 113 |
pad(current_fmt.padc, current_fmt.width - len); |
| 114 |
if (d < 0) |
| 115 |
f_putc('-', current_fmt.iop); |
| 116 |
while (up != ep) { |
| 117 |
f_putc((int)*up, current_fmt.iop); |
| 118 |
up++; |
| 119 |
} |
| 120 |
if (current_fmt.leftadj) |
| 121 |
pad(current_fmt.padc, current_fmt.width - len); |
| 122 |
} |
| 123 |
|
| 124 |
private void |
| 125 |
fmt_puts(const char *str) |
| 126 |
{ |
| 127 |
int len; |
| 128 |
const char *cp; |
| 129 |
|
| 130 |
if (str == NULL) |
| 131 |
str = "(null)"; |
| 132 |
len = strlen(str); |
| 133 |
if (current_fmt.precision == 0 || len < current_fmt.precision) |
| 134 |
current_fmt.precision = len; |
| 135 |
else |
| 136 |
len = current_fmt.precision; |
| 137 |
cp = str; |
| 138 |
if (!current_fmt.leftadj) |
| 139 |
pad(' ', current_fmt.width - len); |
| 140 |
while (--current_fmt.precision >= 0) |
| 141 |
f_putc(*cp++, current_fmt.iop); |
| 142 |
if (current_fmt.leftadj) |
| 143 |
pad(' ', current_fmt.width - len); |
| 144 |
} |
| 145 |
|
| 146 |
private void |
| 147 |
pad(DAPchar c, int amount) |
| 148 |
{ |
| 149 |
while (c && --amount >= 0) |
| 150 |
f_putc(c, current_fmt.iop); |
| 151 |
} |
| 152 |
|
| 153 |
/* |
| 154 |
* TODO: Support U and u for unsigned long and unsigned when |
| 155 |
* we dump support for pre-ANSI C, OXox should probably use |
| 156 |
* unsigned too. |
| 157 |
*/ |
| 158 |
private void |
| 159 |
doformat(File *sp, const char *fmt, va_list ap) |
| 160 |
{ |
| 161 |
char c; |
| 162 |
struct fmt_state |
| 163 |
prev_fmt; |
| 164 |
|
| 165 |
prev_fmt = current_fmt; |
| 166 |
current_fmt.iop = sp; |
| 167 |
|
| 168 |
while ((c = *fmt++) != '\0') { |
| 169 |
if (c != '%') { |
| 170 |
f_putc(c, current_fmt.iop); |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
current_fmt.padc = ' '; |
| 175 |
current_fmt.precision = current_fmt.width = 0; |
| 176 |
current_fmt.leftadj = NO; |
| 177 |
c = *fmt++; |
| 178 |
if (c == '-') { |
| 179 |
current_fmt.leftadj = YES; |
| 180 |
c = *fmt++; |
| 181 |
} |
| 182 |
if (c == '0') { |
| 183 |
current_fmt.padc = '0'; |
| 184 |
c = *fmt++; |
| 185 |
} |
| 186 |
while (c >= '0' && c <= '9') { |
| 187 |
current_fmt.width = current_fmt.width * 10 + (c - '0'); |
| 188 |
c = *fmt++; |
| 189 |
} |
| 190 |
if (c == '*') { |
| 191 |
current_fmt.width = va_arg(ap, int); |
| 192 |
c = *fmt++; |
| 193 |
} |
| 194 |
if (c == '.') { |
| 195 |
c = *fmt++; |
| 196 |
while (c >= '0' && c <= '9') { |
| 197 |
current_fmt.precision = current_fmt.precision * 10 + (c - '0'); |
| 198 |
c = *fmt++; |
| 199 |
} |
| 200 |
if (c == '*') { |
| 201 |
current_fmt.precision = va_arg(ap, int); |
| 202 |
c = *fmt++; |
| 203 |
} |
| 204 |
} |
| 205 |
reswitch: |
| 206 |
/* At this point, fmt points at one past the format letter. */ |
| 207 |
switch (c) { |
| 208 |
case '%': |
| 209 |
f_putc('%', current_fmt.iop); |
| 210 |
break; |
| 211 |
|
| 212 |
case 'O': |
| 213 |
case 'D': |
| 214 |
case 'X': |
| 215 |
putld(va_arg(ap, long), (c == 'O') ? 8 : |
| 216 |
(c == 'D') ? 10 : 16); |
| 217 |
break; |
| 218 |
|
| 219 |
case 'b': |
| 220 |
{ |
| 221 |
Buffer *b = va_arg(ap, Buffer *); |
| 222 |
|
| 223 |
fmt_puts(b->b_name); |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
case 'c': |
| 228 |
f_putc(va_arg(ap, DAPchar), current_fmt.iop); |
| 229 |
break; |
| 230 |
|
| 231 |
case 'o': |
| 232 |
case 'd': |
| 233 |
case 'x': |
| 234 |
putld((long) va_arg(ap, int), (c == 'o') ? 8 : |
| 235 |
(c == 'd') ? 10 : 16); |
| 236 |
break; |
| 237 |
|
| 238 |
case 'f': /* current command name gets inserted here! */ |
| 239 |
fmt_puts(LastCmd->Name); |
| 240 |
break; |
| 241 |
|
| 242 |
case 'l': |
| 243 |
c = CharUpcase(*++fmt); |
| 244 |
goto reswitch; |
| 245 |
|
| 246 |
case 'n': |
| 247 |
if (va_arg(ap, int) != 1) |
| 248 |
fmt_puts("s"); |
| 249 |
break; |
| 250 |
|
| 251 |
case 'p': |
| 252 |
{ |
| 253 |
ZXchar cc = ZXC(va_arg(ap, DAPchar)); |
| 254 |
|
| 255 |
if (cc == ESC) { |
| 256 |
fmt_puts("ESC"); |
| 257 |
} else { |
| 258 |
char cbuf[PPWIDTH]; |
| 259 |
|
| 260 |
PPchar(cc, cbuf); |
| 261 |
fmt_puts(cbuf); |
| 262 |
} |
| 263 |
} |
| 264 |
break; |
| 265 |
|
| 266 |
case 's': |
| 267 |
fmt_puts(va_arg(ap, char *)); |
| 268 |
break; |
| 269 |
|
| 270 |
default: |
| 271 |
complain("Unknown format directive: \"%%%c\"", c); |
| 272 |
/* NOTREACHED */ |
| 273 |
} |
| 274 |
} |
| 275 |
current_fmt = prev_fmt; |
| 276 |
} |
| 277 |
|
| 278 |
char * |
| 279 |
sprint(const char *fmt, ...) |
| 280 |
{ |
| 281 |
va_list ap; |
| 282 |
static char line[LBSIZE]; |
| 283 |
|
| 284 |
va_start(ap, fmt); |
| 285 |
format(line, sizeof line, fmt, ap); |
| 286 |
va_end(ap); |
| 287 |
return line; |
| 288 |
} |
| 289 |
|
| 290 |
void |
| 291 |
writef(const char *fmt, ...) |
| 292 |
{ |
| 293 |
va_list ap; |
| 294 |
|
| 295 |
va_start(ap, fmt); |
| 296 |
#ifdef NO_JSTDOUT |
| 297 |
/* Can't use sprint because caller might have |
| 298 |
* passed the result of sprint as an arg. |
| 299 |
*/ |
| 300 |
{ |
| 301 |
char line[100]; |
| 302 |
|
| 303 |
format(line, sizeof(line), fmt, ap); |
| 304 |
putstr(line); |
| 305 |
} |
| 306 |
#else /* !NO_JSTDOUT */ |
| 307 |
doformat(jstdout, fmt, ap); |
| 308 |
#endif /* !NO_JSTDOUT */ |
| 309 |
va_end(ap); |
| 310 |
} |
| 311 |
|
| 312 |
void |
| 313 |
fwritef(File *fp, const char *fmt, ...) |
| 314 |
{ |
| 315 |
va_list ap; |
| 316 |
|
| 317 |
va_start(ap, fmt); |
| 318 |
doformat(fp, fmt, ap); |
| 319 |
va_end(ap); |
| 320 |
} |
| 321 |
|
| 322 |
void |
| 323 |
swritef(char *str, size_t size, const char *fmt, ...) |
| 324 |
{ |
| 325 |
va_list ap; |
| 326 |
|
| 327 |
va_start(ap, fmt); |
| 328 |
format(str, size, fmt, ap); |
| 329 |
va_end(ap); |
| 330 |
} |
| 331 |
|
| 332 |
/* send a message (supressed if input pending) */ |
| 333 |
|
| 334 |
void |
| 335 |
s_mess(const char *fmt, ...) |
| 336 |
{ |
| 337 |
va_list ap; |
| 338 |
|
| 339 |
if (InJoverc) |
| 340 |
return; |
| 341 |
|
| 342 |
va_start(ap, fmt); |
| 343 |
format(mesgbuf, sizeof mesgbuf, fmt, ap); |
| 344 |
va_end(ap); |
| 345 |
message(mesgbuf); |
| 346 |
} |
| 347 |
|
| 348 |
/* force a message: display it now no matter what. |
| 349 |
* If you wish it to stick, set stickymsg on after calling f_mess. |
| 350 |
*/ |
| 351 |
|
| 352 |
void |
| 353 |
f_mess(const char *fmt, ...) |
| 354 |
{ |
| 355 |
va_list ap; |
| 356 |
|
| 357 |
va_start(ap, fmt); |
| 358 |
format(mesgbuf, sizeof mesgbuf, fmt, ap); |
| 359 |
va_end(ap); |
| 360 |
DrawMesg(NO); |
| 361 |
stickymsg = NO; |
| 362 |
UpdMesg = YES; /* still needs updating (for convenience) */ |
| 363 |
} |
| 364 |
|
| 365 |
void |
| 366 |
add_mess(const char *fmt, ...) |
| 367 |
{ |
| 368 |
int mesg_len = strlen(mesgbuf); |
| 369 |
va_list ap; |
| 370 |
|
| 371 |
if (InJoverc) |
| 372 |
return; |
| 373 |
|
| 374 |
va_start(ap, fmt); |
| 375 |
format(&mesgbuf[mesg_len], (sizeof mesgbuf) - mesg_len, fmt, ap); |
| 376 |
va_end(ap); |
| 377 |
message(mesgbuf); |
| 378 |
} |
| 379 |
|
| 380 |
jbool jdebug = YES; /* so that first jdprintf is called */ |
| 381 |
const char *jdpath = NULL; /* if non-NULL, will be opened on first jdprintf */ |
| 382 |
|
| 383 |
void |
| 384 |
jdprintf(const char *fmt, ...) |
| 385 |
{ |
| 386 |
static jbool first_time = YES; |
| 387 |
static File *dfp = NULL; |
| 388 |
va_list ap; |
| 389 |
|
| 390 |
if (first_time && jdpath != NULL) { |
| 391 |
dfp = f_open(jdpath, F_WRITE | F_LOCKED, NULL, LBSIZE); |
| 392 |
jdebug = (dfp != NULL); |
| 393 |
first_time = NO; |
| 394 |
} |
| 395 |
va_start(ap, fmt); |
| 396 |
if (dfp != NULL) { |
| 397 |
doformat(dfp, fmt, ap); |
| 398 |
flushout(dfp); |
| 399 |
} |
| 400 |
va_end(ap); |
| 401 |
} |