1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
*/
#include <stdarg.h>
#define import_spp
#define import_libc
#define import_xnames
#define import_stdio
#include <iraf.h>
static void u_doarg(FILE *fp, XCHAR *formspec, int prec[], int varprec);
#define todigit(c) ((c)+'0')
/* PRINTF -- Emulation of the UNIX printf facilities with the IRAF FMTIO
** interface as the backend. All features of the UNIX printf are supported
** without modification. Additional format codes are supported in conformance
** with the IRAF printf, e.g., hms format, variable radix, tabstops, etc.,
** but these are upward compatible with standard UNIX usage.
*/
#define SZ_FMTSPEC 25 /* max size single format spec */
#define SZ_OBUF SZ_COMMAND /* sz intermediate buffer */
#define MAX_PREC 4 /* max "*" deferred args */
#define NOARG (-1) /* % spec with no data value */
/* PRINTF -- Formatted print to the standard output.
*/
void
printf (char *format, ...)
{
va_list argp;
va_start (argp, format);
vfprintf (stdout, format, argp);
va_end (argp);
}
/* FPRINTF -- Formatted print to a file.
*/
void
fprintf (FILE *fp, char *format, ...)
{
va_list argp;
va_start (argp, format);
vfprintf (fp, format, argp);
va_end (argp);
}
/* VFPRINTF -- Process the format to the output file, taking arguments from
** the list pointed to by argp as % format specs are encountered in the input.
** The main point of this routine is to handle the variable number of arguments.
** The actual encoding is all handled by the IRAF FPRINF and PARG calls.
** N.B. we assume chars are stacked as ints, and floats are stacked as doubles.
*/
void
vfprintf (
FILE *fp, /* output file */
char *format, /* "%w.dC" etc. format spec */
va_list argp /* pointer to first value arg */
)
{
register int ch; /* next format char reference */
XCHAR formspec[SZ_FMTSPEC]; /* copy of single format spec */
XCHAR *fsp; /* pointer into formspec */
int done, dotseen; /* one when at end of a format */
int varprec; /* runtime precision is used */
int prec[MAX_PREC]; /* values of prec args */
XINT ival;
XCHAR sbuf[SZ_OBUF+1];
XDOUBLE dval;
char *cptr;
while ( (ch = *format++) ) {
if (ch == '%') {
fsp = formspec;
*fsp++ = ch;
varprec = 0;
dotseen = 0;
done = 0;
while (!done) {
ch = *fsp++ = *format++;
switch (ch) {
case EOS:
--format;
done++;
break;
case 'l':
/* arg size modifier; ignored for now */
fsp--;
break;
case '*':
prec[varprec++] = va_arg (argp, int);
break;
case '.':
dotseen++;
break;
case 'r': /* nonstandard UNIX */
if ((ch = *fsp++ = *format++) == '*') {
int radix;
int radchar;
radix = va_arg (argp, int);
if (radix < 0)
radchar = 'A';
else if (radix > 9)
radchar = radix - 10 + 'A';
else
radchar = todigit (radix);
*(fsp-1) = radchar;
} else if (ch == EOS) {
--format;
break;
}
/* fall through */
case 'b': /* nonstandard UNIX */
case 'c':
case 'd':
case 'o':
case 'x':
case 'u':
*fsp = EOS;
u_doarg (fp, formspec, prec, varprec);
ival = va_arg (argp, int);
PARGI (&ival);
done++;
break;
case 'E': /* ANSI emulation */
*(fsp-1) = 'e';
goto rval;
case 'G': /* ANSI emulation */
*(fsp-1) = 'g';
goto rval;
case 'z': /* nonstandard UNIX */
case 'h': /* nonstandard UNIX */
case 'H': /* nonstandard UNIX */
case 'm': /* nonstandard UNIX */
case 'M': /* nonstandard UNIX */
case 'e':
case 'f':
case 'g':
/* If no precision was specified, default to 14 digits
* for %[efgz] and 3 digits for %[hm].
*/
rval: if (!dotseen) {
*(fsp-1) = '.';
if (ch == 'h' || ch == 'm' ||
ch == 'H' || ch == 'M') {
*fsp++ = '3';
} else {
*fsp++ = '1';
*fsp++ = '4';
}
*fsp++ = ch;
}
*fsp = XEOS;
u_doarg (fp, formspec, prec, varprec);
dval = va_arg (argp, double);
PARGD (&dval);
done++;
break;
case 's':
*fsp = EOS;
u_doarg (fp, formspec, prec, varprec);
cptr = va_arg (argp, char *);
PARGSTR (c_strupk (cptr, sbuf, SZ_OBUF));
done++;
break;
case 't': /* nonstandard UNIX */
case 'w': /* nonstandard UNIX */
*fsp = EOS;
u_doarg (fp, formspec, prec, varprec);
done++;
break;
case '%':
putc (ch, fp);
done++;
break;
}
}
} else
putc (ch, fp);
}
}
/* U_DOARG -- Encode a single argument acording to the simplified format
** specification given by formspec. This is the interface to the IRAF
** formatted output procedures.
*/
static void
u_doarg (
FILE *fp, /* output file */
XCHAR *formspec, /* format string */
int prec[], /* varprec args, if any */
int varprec /* number of varprec args */
)
{
register int p;
XINT fd = fileno (fp);
XINT ival;
/* Pass format string and any variable precision arguments.
*/
FPRINTF (&fd, formspec);
for (p=0; p < varprec; p++) {
ival = prec[p];
PARGI (&ival);
}
}
|