Index: cpp.c
===================================================================
RCS file: /cvsroot/pcc/cc/cpp/cpp.c,v
retrieving revision 1.196
retrieving revision 1.197
diff -u -p -u -r1.196 -r1.197
--- cpp.c	18 Oct 2014 15:02:03 -0000	1.196
+++ cpp.c	19 Oct 2014 17:40:36 -0000	1.197
@@ -1,4 +1,4 @@
-/*	Id: cpp.c,v 1.196 2014/10/18 15:02:03 ragge Exp 	*/	
-/*	$NetBSD: stdio.diff,v 1.1.1.1 2016/02/09 20:28:45 plunky Exp $	*/
+/*	Id: cpp.c,v 1.197 2014/10/19 17:40:36 ragge Exp 	*/	
+/*	$NetBSD: stdio.diff,v 1.1.1.1 2016/02/09 20:28:45 plunky Exp $	*/
 
 /*
  * Copyright (c) 2004,2010 Anders Magnusson (ragge@ludd.luth.se).
@@ -74,15 +74,13 @@ static void prrep(const usch *s);
 #define IMP(x)
 #endif
 
-int ofd;
-usch outbuf[CPPBUF], lastoch;
-int obufp, istty;
 int Aflag, Cflag, Eflag, Mflag, dMflag, Pflag, MPflag, MMDflag;
 usch *Mfile, *MPfile, *Mxfile;
 struct initar *initar;
 int readmac;
 int defining;
 int warnings;
+FILE *of;
 
 /* include dirs */
 struct incs {
@@ -141,7 +139,6 @@ int bidx;			/* Top of bptr stack */
 static int readargs(struct symtab *sp, const usch **args);
 static void exparg(int);
 static void subarg(struct symtab *sp, const usch **args, int);
-static void flbuf(void);
 static void usage(void);
 static usch *xstrdup(const usch *str);
 static void addidir(char *idir, struct incs **ww);
@@ -324,11 +321,10 @@ main(int argc, char **argv)
 	}
 
 	if (argc == 2) {
-		if ((ofd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
+		if ((of = freopen(argv[1], "w", stdout)) == NULL)
 			error("Can't creat %s", argv[1]);
 	} else
-		ofd = 1; /* stdout */
-	istty = isatty(ofd);
+		of = stdout;
 
 	if (argc && strcmp(argv[0], "-")) {
 		fn1 = fn2 = (usch *)argv[0];
@@ -339,8 +335,7 @@ main(int argc, char **argv)
 	if (pushfile(fn1, fn2, 0, NULL))
 		error("cannot open %s", argv[0]);
 
-	flbuf();
-	close(ofd);
+	fclose(of);
 #ifdef TIMING
 	(void)gettimeofday(&t2, NULL);
 	t2.tv_sec -= t1.tv_sec;
@@ -1054,21 +1049,15 @@ void
 warning(const char *fmt, ...)
 {
 	va_list ap;
-	usch *sb;
 
-	flbuf();
-	savch(0);
-
-	sb = stringbuf;
 	if (ifiles != NULL)
-		sheap("%s:%d: warning: ", ifiles->fname, ifiles->lineno);
+		fprintf(stderr, "%s:%d: warning: ",
+		    ifiles->fname, ifiles->lineno);
 
 	va_start(ap,fmt);
-	vsheap(fmt, ap);
+	vfprintf(stderr, fmt, ap);
 	va_end(ap);
-	savch('\n');
-	xwrite(2, sb, stringbuf - sb);
-	stringbuf = sb;
+	fputc('\n', stderr);
 
 	warnings++;
 }
@@ -1077,22 +1066,15 @@ void
 error(const char *fmt, ...)
 {
 	va_list ap;
-	usch *sb;
 
-	flbuf();
-	savch(0);
-
-	sb = stringbuf;
 	if (ifiles != NULL)
-		sheap("%s:%d: error: ", ifiles->fname, ifiles->lineno);
+		fprintf(stderr, "%s:%d: error: ",
+		    ifiles->fname, ifiles->lineno);
 
 	va_start(ap, fmt);
-	vsheap(fmt, ap);
+	vfprintf(stderr, fmt, ap);
 	va_end(ap);
-	savch('\n');
-	xwrite(2, sb, stringbuf - sb);
-	stringbuf = sb;
-
+	fputc('\n', stderr);
 	exit(1);
 }
 
@@ -1287,7 +1269,7 @@ delwarn(void)
 		} else if (c == EBLOCK) {
 			sss();
 		} else if (c == '\n') {
-			putch(cinput());
+			fputc(cinput(), stdout);
 		} else
 			savstr(yytext);
 	}
@@ -1310,7 +1292,7 @@ kfind(struct symtab *sp)
 	struct symtab *nl;
 	const usch *argary[MAXARGS+1], *cbp;
 	usch *sbp;
-	int c, o, chkf;
+	int c, o;
 
 	DPRINT(("%d:enter kfind(%s)\n",0,sp->namep));
 	IMP("KFIND");
@@ -1331,11 +1313,6 @@ kfind(struct symtab *sp)
 		exparg(1);
 
 upp:		sbp = stringbuf;
-		chkf = 1;
-		if (obufp != 0)
-			lastoch = outbuf[obufp-1];
-		if (iswsnl(lastoch))
-			chkf = 0;
 		if (Cflag)
 			readmac++;
 		while ((c = sloscan()) != WARN) {
@@ -1391,12 +1368,9 @@ upp:		sbp = stringbuf;
 				break;
 
 			default:
-				if (chkf && c < 127)
-					putch(' ');
 				savstr(yytext);
 				break;
 			}
-			chkf = 0;
 		}
 		if (Cflag)
 			readmac--;
@@ -1423,7 +1397,7 @@ upp:		sbp = stringbuf;
 	/* Found one, output \n to be in sync */
 	for (; *sbp; sbp++) {
 		if (*sbp == '\n')
-			putch('\n'), ifiles->lineno++;
+			fputc('\n', stdout), ifiles->lineno++;
 	}
 
 	/* fetch arguments */
@@ -1576,7 +1550,7 @@ chkdir(void)
 		while ((ch = cinput()) != '\n')
 			;
 		ifiles->lineno++;
-		putch('\n');
+		fputc('\n', stdout);
 	}
 }
 
@@ -1611,7 +1585,7 @@ readargs(struct symtab *sp, const usch *
 		while ((c = sloscan()) == WSPACE || c == '\n')
 			if (c == '\n') {
 				ifiles->lineno++;
-				putch(cinput());
+				fputc(cinput(), stdout);
 				chkdir();
 			}
 		for (;;) {
@@ -1632,7 +1606,7 @@ readargs(struct symtab *sp, const usch *
 			savstr(yytext);
 oho:			while ((c = sloscan()) == '\n') {
 				ifiles->lineno++;
-				putch(cinput());
+				fputc(cinput(), stdout);
 				chkdir();
 				savch(' ');
 			}
@@ -2010,37 +1984,6 @@ unpstr(const usch *c)
 	}
 }
 
-static void
-flbuf(void)
-{
-	if (obufp == 0)
-		return;
-	if (Mflag == 0)
-		xwrite(ofd, outbuf, obufp);
-	lastoch = outbuf[obufp-1];
-	obufp = 0;
-}
-
-void
-putch(int ch)
-{
-	outbuf[obufp++] = (usch)ch;
-	if (obufp == CPPBUF || (istty && ch == '\n'))
-		flbuf();
-}
-
-void
-putstr(const usch *s)
-{
-	for (; *s; s++) {
-		if (*s == PHOLD)
-			continue;
-		outbuf[obufp++] = *s;
-		if (obufp == CPPBUF || (istty && *s == '\n'))
-			flbuf();
-	}
-}
-
 /*
  * convert a number to an ascii string. Store it on the heap.
  */
Index: cpp.h
===================================================================
RCS file: /cvsroot/pcc/cc/cpp/cpp.h,v
retrieving revision 1.70
retrieving revision 1.71
diff -u -p -u -r1.70 -r1.71
--- cpp.h	18 Oct 2014 15:02:03 -0000	1.70
+++ cpp.h	19 Oct 2014 17:40:36 -0000	1.71
@@ -1,4 +1,4 @@
-/*	Id: cpp.h,v 1.70 2014/10/18 15:02:03 ragge Exp 	*/	
-/*	$NetBSD: stdio.diff,v 1.1.1.1 2016/02/09 20:28:45 plunky Exp $	*/
+/*	Id: cpp.h,v 1.71 2014/10/19 17:40:36 ragge Exp 	*/	
+/*	$NetBSD: stdio.diff,v 1.1.1.1 2016/02/09 20:28:45 plunky Exp $	*/
 
 /*
  * Copyright (c) 2004,2010 Anders Magnusson (ragge@ludd.luth.se).
@@ -39,8 +39,8 @@ extern	int	dflag;
 extern	int	tflag, Aflag, Cflag, Pflag;
 extern	int	Mflag, dMflag, MPflag, MMDflag;
 extern	usch	*Mfile, *MPfile;
-extern	int	ofd;
 extern	int	defining;
+extern	FILE	*of;
 
 /* args for lookup() */
 #define FIND    0
Index: token.c
===================================================================
RCS file: /cvsroot/pcc/cc/cpp/token.c,v
retrieving revision 1.119
retrieving revision 1.120
diff -u -p -u -r1.119 -r1.120
--- token.c	18 Oct 2014 15:02:03 -0000	1.119
+++ token.c	19 Oct 2014 17:40:36 -0000	1.120
@@ -1,4 +1,4 @@
-/*	Id: token.c,v 1.119 2014/10/18 15:02:03 ragge Exp 	*/	
-/*	$NetBSD: stdio.diff,v 1.1.1.1 2016/02/09 20:28:45 plunky Exp $	*/
+/*	Id: token.c,v 1.120 2014/10/19 17:40:36 ragge Exp 	*/	
+/*	$NetBSD: stdio.diff,v 1.1.1.1 2016/02/09 20:28:45 plunky Exp $	*/
 
 /*
  * Copyright (c) 2004,2009 Anders Magnusson. All rights reserved.
@@ -65,7 +65,7 @@ static void undefstmt(void);
 static void pragmastmt(void);
 static void elifstmt(void);
 
-#define	PUTCH(ch) if (!flslvl) putch(ch)
+#define	PUTCH(ch) if (!flslvl) fputc(ch, stdout)
 /* protection against recursion in #include */
 #define MAX_INCLEVEL	100
 static int inclevel;
@@ -312,7 +312,7 @@ eatcmnt(void)
 		ch = inch();
 		if (ch == '\n') {
 			ifiles->lineno++;
-			putch('\n');
+			fputc('\n', stdout);
 			continue;
 		}
 		if (ch == -1)
@@ -373,9 +373,11 @@ xloop:		if (ch == -1)
 
 		case '/': /* Comments */
 			if ((ch = inch()) == '/') {
-cppcmt:				if (Cflag) { PUTCH(ch); } else { PUTCH(' '); }
+				if (!flslvl) {
+cppcmt:					fputc(Cflag ? ch : ' ', stdout);
+				}
 				do {
-					if (Cflag) PUTCH(ch);
+					if (Cflag && !flslvl) fputc(ch, stdout);
 					ch = inch();
 					if (ch == -1)
 						goto eof;
@@ -385,7 +387,7 @@ cppcmt:				if (Cflag) { PUTCH(ch); } els
 				if (eatcmnt() == -1)
 					goto eof;
 			} else {
-				PUTCH('/');
+				if (!flslvl) fputc('/', stdout);
 				goto xloop;
 			}
 			break;
@@ -395,7 +397,7 @@ cppcmt:				if (Cflag) { PUTCH(ch); } els
 			ifiles->lineno += i;
 			ifiles->escln = 0;
 			while (i-- > 0)
-				putch('\n');
+				fputc('\n', stdout);
 run:			for(;;) {
 				ch = inch();
 				if (ch == '/') {
@@ -412,7 +414,7 @@ run:			for(;;) {
 				}
 				if (ch != ' ' && ch != '\t')
 					break;
-				PUTCH(ch);
+				if (!flslvl) fputc(ch, stdout);
 			}
 			if (ch == '#') {
 				ppdir();
@@ -537,9 +539,9 @@ con:			PUTCH(ch);
 			if (flslvl == 0) {
 				cp = stringbuf;
 				if ((nl = lookup(yytext, FIND)) && kfind(nl))
-					putstr(stringbuf);
+					printf("%s", stringbuf);
 				else
-					putstr(yytext);
+					printf("%s", (char *)yytext);
 				stringbuf = cp;
 			}
 			if (ch == -1)
@@ -559,7 +561,7 @@ con:			PUTCH(ch);
 	}
 
 eof:	warning("unexpected EOF");
-	putch('\n');
+	fputc('\n', stdout);
 }
 
 int
@@ -667,7 +669,7 @@ chlit:
 				if (c == -1)
 					return 0;
 				if (c == '\n')
-					putch(c), ifiles->lineno++;
+					fputc(c, stdout), ifiles->lineno++;
 				else if (c == EBLOCK) {
 					(void)inch();
 					(void)inch();
@@ -976,7 +978,7 @@ pushfile(const usch *file, const usch *f
 		prinit(initar, ic);
 		initar = NULL;
 		if (dMflag)
-			xwrite(ofd, ic->buffer, strlen((char *)ic->buffer));
+			printf("%s", (char *)ic->buffer);
 		fastscan();
 		prtline();
 		ic->infil = oin;
@@ -1004,27 +1006,22 @@ pushfile(const usch *file, const usch *f
 void
 prtline(void)
 {
-	usch *sb = stringbuf;
-
 	if (Mflag) {
 		if (dMflag)
 			return; /* no output */
 		if (ifiles->lineno == 1 &&
 		    (MMDflag == 0 || ifiles->idx != SYSINC)) {
-			sheap("%s: %s\n", Mfile, ifiles->fname);
+			printf("%s: %s\n", Mfile, ifiles->fname);
 			if (MPflag &&
 			    strcmp((const char *)ifiles->fname, (char *)MPfile))
-				sheap("%s:\n", ifiles->fname);
-			xwrite(ofd, sb, stringbuf - sb);
+				printf("%s:\n", ifiles->fname);
 		}
 	} else if (!Pflag) {
-		sheap("\n# %d \"%s\"", ifiles->lineno, ifiles->fname);
+		printf("\n# %d \"%s\"", ifiles->lineno, ifiles->fname);
 		if (ifiles->idx == SYSINC)
-			sheap(" 3");
-		sheap("\n");
-		putstr(sb);
+			printf(" 3");
+		printf("\n");
 	}
-	stringbuf = sb;
 }
 
 void
@@ -1344,7 +1341,7 @@ pragmastmt(void)
 	sb = stringbuf;
 	savstr((const usch *)"\n#pragma ");
 	savln();
-	putstr(sb);
+	printf("%s", ((char *)sb));
 	prtline();
 	stringbuf = sb;
 }
@@ -1416,7 +1413,7 @@ redo:	while ((ch = inch()) == ' ' || ch 
 						goto redo;
 					unch(ch);
 				} else if (ch == '\n') {
-					putch('\n');
+					fputc('\n', stdout);
 					ifiles->lineno++;
 				}
 			}
