Now that printf and scanf contain no FILE*-specific code, we can move them into

core (and split them up).
This commit is contained in:
David Given 2019-06-15 13:53:20 +02:00
parent 9109d7af7f
commit 1387c8713b
36 changed files with 141 additions and 155 deletions

View file

@ -39,6 +39,8 @@ for _, plat in ipairs(vars.plats) do
"./core/math/*.c",
"./core/math/*.e",
"./core/misc/*.c",
"./core/printf/*.c",
"./core/scanf/*.c",
"./core/setjmp/*.c",
"./core/setjmp/*.e",
"./core/stdlib/*.c",
@ -57,7 +59,6 @@ for _, plat in ipairs(vars.plats) do
"./core/stdlib/ext_fmt.h",
"./core/time/loc_time.h",
"./sys/malloc/malloc.h",
"./sys/stdio/loc_incl.h",
},
vars = { plat = plat }
}

View file

@ -7,7 +7,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
#include "doprnt.h"
#if ACKCONF_WANT_STDIO
@ -44,15 +44,13 @@ gnum(register const char* f, int* ip, va_list* app)
#define set_pointer(flags) /* compilation might continue */
#endif
int (*_doprnt_put)(int c);
#define PUTC(c) \
do \
{ \
int i = putc(c, stream); \
if (i == EOF) \
{ \
if (ferror(stream)) \
if (_doprnt_put(c)) \
return -1; \
} \
} while (0)
/* print an ordinal number */
@ -157,7 +155,7 @@ o_print(va_list* ap, int flags, char* s, char c, int precision, int is_signed)
return s;
}
int _doprnt(register const char* fmt, va_list ap, void (*put)(int))
int _doprnt(register const char* fmt, va_list ap)
{
register char* s;
register int j;
@ -176,7 +174,7 @@ int _doprnt(register const char* fmt, va_list ap, void (*put)(int))
PUTC('\r');
}
#endif
put(c);
PUTC(c);
nrchars++;
continue;
}
@ -263,7 +261,7 @@ int _doprnt(register const char* fmt, va_list ap, void (*put)(int))
nrchars++;
}
#endif
put(c);
PUTC(c);
nrchars++;
continue;
case 'n':
@ -366,32 +364,32 @@ int _doprnt(register const char* fmt, va_list ap, void (*put)(int))
{
j--;
nrchars++;
put(*s1++);
PUTC(*s1++);
}
else
{
j -= 2;
nrchars += 2;
put(*s1++);
put(*s1++);
PUTC(*s1++);
PUTC(*s1++);
}
}
do
{
put(zfill);
PUTC(zfill);
} while (--i);
}
nrchars += j;
while (--j >= 0)
{
put(*s1++);
PUTC(*s1++);
}
if (i > 0)
nrchars += i;
while (--i >= 0)
put(zfill);
PUTC(zfill);
}
return nrchars;
}

View file

@ -1,29 +1,5 @@
/*
* loc_incl.h - local include file for stdio library
*/
/* $Id$ */
#include <stdio.h>
#include <ack/config.h>
#define fileno(p) ((p)->_fd)
#define io_testflag(p,x) ((p)->_flags & (x))
#include <stdarg.h>
int _doprnt(const char *format, va_list ap, void (*put)(int c));
int _doscan(const char *format, va_list ap, int (*get)(void), void (*unget)(int c));
char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
char *_f_print(va_list *ap, int flags, char *s, char c, int precision);
extern void __register_stdio_cleanup(void);
FILE *popen(const char *command, const char *type);
FILE *fdopen(int fd, const char *mode);
#if ACKCONF_WANT_STDIO_FLOAT
char *_ecvt(long double value, int ndigit, int *decpt, int *sign);
char *_fcvt(long double value, int ndigit, int *decpt, int *sign);
#endif
#ifndef DOPRNT_H
#define DOPRNT_H
#define FL_LJUST 0x0001 /* left-justify field */
#define FL_SIGN 0x0002 /* sign in signed conversions */
@ -38,3 +14,15 @@ char *_fcvt(long double value, int ndigit, int *decpt, int *sign);
#define FL_SIGNEDCONV 0x0400 /* may contain a sign */
#define FL_NOASSIGN 0x0800 /* do not assign (in scanf) */
#define FL_NOMORE 0x1000 /* all flags collected */
extern int (*_doprnt_put)(int c);
extern int _doprnt(register const char* fmt, va_list ap);
extern char* _f_print(va_list* ap, int flags, char* s, char c, int precision);
#if ACKCONF_WANT_STDIO_FLOAT
char *_ecvt(long double value, int ndigit, int *decpt, int *sign);
char *_fcvt(long double value, int ndigit, int *decpt, int *sign);
#endif
#endif

View file

@ -5,7 +5,7 @@
#include <string.h>
#include <stdarg.h>
#include "loc_incl.h"
#include "doprnt.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_STDIO_FLOAT

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -3,7 +3,7 @@
*/
/* $Id$ */
#include "loc_incl.h"
#include <stdio.h>
#if ACKCONF_WANT_STDIO

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -0,0 +1,23 @@
/*
* sprintf - print formatted output on an array
*/
/* $Id$ */
#include <stdio.h>
#include <stdarg.h>
#if ACKCONF_WANT_STDIO
int snprintf(char* s, size_t len, const char* format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
retval = vsnprintf(s, len, format, ap);
va_end(ap);
return retval;
}
#endif

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -5,21 +5,23 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#include "doprnt.h"
#if ACKCONF_WANT_STDIO
static FILE* vfprintf_stream;
static void vfprintf_putc(int c)
static int vfprintf_putc(int c)
{
putc(c, vfprintf_stream);
return ferror(vfprintf_stream);
}
int vfprintf(FILE* stream, const char* format, va_list arg)
{
vfprintf_stream = stream;
return _doprnt(format, arg, vfprintf_putc);
_doprnt_put = vfprintf_putc;
return _doprnt(format, arg);
}
#endif

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -5,20 +5,21 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#include "doprnt.h"
#if ACKCONF_WANT_STDIO
static char* output_buffer;
static size_t output_buffer_len;
static void snprintf_putc(int c)
static int snprintf_putc(int c)
{
if (output_buffer_len)
{
*output_buffer++ = c;
output_buffer_len--;
}
return 0;
}
int vsnprintf(char* s, size_t len, const char* format, va_list ap)
@ -27,7 +28,8 @@ int vsnprintf(char* s, size_t len, const char* format, va_list ap)
output_buffer = s;
output_buffer_len = len;
retval = _doprnt(format, ap, snprintf_putc);
_doprnt_put = snprintf_putc;
retval = _doprnt(format, ap);
snprintf_putc('\0');
return retval;

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -7,7 +7,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include "loc_incl.h"
#include "doscan.h"
#if ACKCONF_WANT_STDIO
@ -26,14 +26,16 @@
static char Xtable[NR_CHARS];
static char inp_buf[NUMLEN];
int (*_doscan_get)(void);
void (*_doscan_unget)(int c);
/* Collect a number of characters which constitite an ordinal number.
* When the type is 'i', the base can be 8, 10, or 16, depending on the
* first 1 or 2 characters. This means that the base must be adjusted
* according to the format of the number. At the end of the function, base
* is then set to 0, so strtol() will get the right argument.
*/
static char*
o_collect(register int c, char type, int width, int* basep, int (*get)(void), void (*unget)(int c))
static char* o_collect(register int c, char type, int width, int* basep)
{
register char* bufp = inp_buf;
register int base;
@ -62,14 +64,14 @@ o_collect(register int c, char type, int width, int* basep, int (*get)(void), vo
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
if (width && c == '0' && base == 16)
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
if (c != 'x' && c != 'X')
{
if (type == 'i')
@ -79,7 +81,7 @@ o_collect(register int c, char type, int width, int* basep, int (*get)(void), vo
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
}
else if (type == 'i')
@ -94,14 +96,14 @@ o_collect(register int c, char type, int width, int* basep, int (*get)(void), vo
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
else
break;
}
if (width && c != EOF)
unget(c);
_doscan_unget(c);
if (type == 'i')
base = 0;
*basep = base;
@ -118,8 +120,7 @@ o_collect(register int c, char type, int width, int* basep, int (*get)(void), vo
* not necessary, although the use of the width field can cause incomplete
* numbers to be passed to strtod(). (e.g. 1.3e+)
*/
static char*
f_collect(register int c, register int width, int (*get)(void), void (*unget)(int c))
static char* f_collect(register int c, register int width)
{
register char* bufp = inp_buf;
int digit_seen = 0;
@ -128,7 +129,7 @@ f_collect(register int c, register int width, int (*get)(void), void (*unget)(in
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
while (width && isdigit(c))
@ -136,26 +137,26 @@ f_collect(register int c, register int width, int (*get)(void), void (*unget)(in
digit_seen++;
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
if (width && c == '.')
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
while (width && isdigit(c))
{
digit_seen++;
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
}
if (!digit_seen)
{
if (width && c != EOF)
unget(c);
_doscan_unget(c);
return inp_buf - 1;
}
else
@ -165,30 +166,30 @@ f_collect(register int c, register int width, int (*get)(void), void (*unget)(in
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
if (width && (c == '+' || c == '-'))
{
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
while (width && isdigit(c))
{
digit_seen++;
*bufp++ = c;
if (--width)
c = get();
c = _doscan_get();
}
if (!digit_seen)
{
if (width && c != EOF)
unget(c);
_doscan_unget(c);
return inp_buf - 1;
}
}
if (width && c != EOF)
unget(c);
_doscan_unget(c);
*bufp = '\0';
return bufp - 1;
}
@ -198,7 +199,7 @@ f_collect(register int c, register int width, int (*get)(void), void (*unget)(in
* the routine that does the scanning
*/
int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int c))
int _doscan(const char* format, va_list ap)
{
int done = 0; /* number of items done */
int nrchars = 0; /* number of characters read */
@ -225,15 +226,15 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
{
while (isspace(*format))
format++; /* skip whitespace */
ic = get();
ic = _doscan_get();
nrchars++;
while (isspace(ic))
{
ic = get();
ic = _doscan_get();
nrchars++;
}
if (ic != EOF)
unget(ic);
_doscan_unget(ic);
nrchars--;
}
if (!*format)
@ -241,12 +242,12 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (*format != '%')
{
ic = get();
ic = _doscan_get();
nrchars++;
if (ic != *format++)
{
if (ic != EOF)
unget(ic);
_doscan_unget(ic);
nrchars--;
break; /* error */
}
@ -255,7 +256,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
format++;
if (*format == '%')
{
ic = get();
ic = _doscan_get();
nrchars++;
if (ic == '%')
{
@ -298,7 +299,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
{
do
{
ic = get();
ic = _doscan_get();
nrchars++;
} while (isspace(ic));
if (ic == EOF)
@ -306,7 +307,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
}
else if (kind != 'n')
{ /* %c or %[ */
ic = get();
ic = _doscan_get();
if (ic == EOF)
break; /* outer while */
nrchars++;
@ -343,7 +344,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (!width)
return done;
str = o_collect(ic, kind, width, &base, get, unget);
str = o_collect(ic, kind, width, &base);
if (str < inp_buf
|| (str == inp_buf
&& (*str == '-'
@ -384,7 +385,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
*str++ = (char)ic;
if (--width)
{
ic = get();
ic = _doscan_get();
nrchars++;
}
}
@ -392,7 +393,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (width)
{
if (ic != EOF)
unget(ic);
_doscan_unget(ic);
nrchars--;
}
break;
@ -410,7 +411,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
*str++ = (char)ic;
if (--width)
{
ic = get();
ic = _doscan_get();
nrchars++;
}
}
@ -420,7 +421,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (width)
{
if (ic != EOF)
unget(ic);
_doscan_unget(ic);
nrchars--;
}
break;
@ -467,7 +468,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (!*format || !(Xtable[ic] ^ reverse))
{
if (ic != EOF)
unget(ic);
_doscan_unget(ic);
return done;
}
@ -480,7 +481,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
*str++ = (char)ic;
if (--width)
{
ic = get();
ic = _doscan_get();
nrchars++;
}
} while (width && ic != EOF && (Xtable[ic] ^ reverse));
@ -488,7 +489,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (width)
{
if (ic != EOF)
unget(ic);
_doscan_unget(ic);
nrchars--;
}
if (!(flags & FL_NOASSIGN))
@ -507,7 +508,7 @@ int _doscan(const char* format, va_list ap, int (*get)(void), void (*unget)(int
if (!width)
return done;
str = f_collect(ic, width, get, unget);
str = f_collect(ic, width);
if (str < inp_buf
|| (str == inp_buf

View file

@ -0,0 +1,23 @@
#ifndef DOSCAN_H
#define DOSCAN_H
#define FL_LJUST 0x0001 /* left-justify field */
#define FL_SIGN 0x0002 /* sign in signed conversions */
#define FL_SPACE 0x0004 /* space in signed conversions */
#define FL_ALT 0x0008 /* alternate form */
#define FL_ZEROFILL 0x0010 /* fill with zero's */
#define FL_SHORT 0x0020 /* optional h */
#define FL_LONG 0x0040 /* optional l */
#define FL_LONGDOUBLE 0x0080 /* optional L */
#define FL_WIDTHSPEC 0x0100 /* field width is specified */
#define FL_PRECSPEC 0x0200 /* precision is specified */
#define FL_SIGNEDCONV 0x0400 /* may contain a sign */
#define FL_NOASSIGN 0x0800 /* do not assign (in scanf) */
#define FL_NOMORE 0x1000 /* all flags collected */
extern int (*_doscan_get)(void);
extern void (*_doscan_unget)(int c);
extern int _doscan(const char* format, va_list ap);
#endif

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -6,7 +6,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "loc_incl.h"
#include "doscan.h"
#if ACKCONF_WANT_STDIO
@ -35,7 +35,9 @@ int sscanf(const char* s, const char* format, ...)
va_start(ap, format);
input_buffer = s;
retval = _doscan(format, ap, sscanf_getc, sscanf_ungetc);
_doscan_get = sscanf_getc;
_doscan_unget = sscanf_ungetc;
retval = _doscan(format, ap);
va_end(ap);

View file

@ -5,7 +5,7 @@
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#include "doscan.h"
#if ACKCONF_WANT_STDIO
@ -24,7 +24,9 @@ static void vfscanf_ungetc(int c)
int vfscanf(FILE* stream, const char* format, va_list ap)
{
vfscanf_stream = stream;
return _doscan(format, ap, vfscanf_getc, vfscanf_ungetc);
_doscan_get = vfscanf_getc;
_doscan_unget = vfscanf_ungetc;
return _doscan(format, ap);
}
#endif

View file

@ -56,4 +56,7 @@ extern int fileno(FILE *_stream);
extern FILE* fdopen(int fildes, const char *type);
#define fileno(stream) ((stream)->_fd)
#define io_testflag(p,x) ((p)->_flags & (x))
extern void __register_stdio_cleanup(void);
#endif

View file

@ -76,6 +76,9 @@ extern int feof(FILE *_stream);
extern int ferror(FILE *_stream);
extern void perror(const char *_s);
/* Internal function used by several places which is approximately itoa(). */
extern char *_i_compute(unsigned long val, int base, char *s, int nrdigits);
#if ACKCONF_WANT_EMULATED_FILE
#include <ack/emufile.h>
#else

View file

@ -6,7 +6,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "../stdio/loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -6,7 +6,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -6,7 +6,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -6,7 +6,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -8,7 +8,6 @@
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -7,7 +7,6 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -6,7 +6,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -6,7 +6,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -4,7 +4,6 @@
/* $Id$ */
#include <stdio.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -5,7 +5,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE

View file

@ -1,41 +0,0 @@
/*
* sprintf - print formatted output on an array
*/
/* $Id$ */
#include <stdio.h>
#include <stdarg.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO
static char* output_buffer;
static size_t output_buffer_len;
static void snprintf_putc(int c)
{
if (output_buffer_len)
{
*output_buffer++ = c;
output_buffer_len--;
}
}
int snprintf(char* s, size_t len, const char* format, ...)
{
va_list ap;
int retval;
va_start(ap, format);
output_buffer = s;
output_buffer_len = len;
retval = _doprnt(format, ap, snprintf_putc);
snprintf_putc('\0');
va_end(ap);
return retval;
}
#endif

View file

@ -7,7 +7,6 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -7,7 +7,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO

View file

@ -4,7 +4,6 @@
/* $Id$ */
#include <stdio.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE