ref: 5d9e9d545a1ce2824d0e7ef0db7060194aff1164
parent: a6cbaeb182ac4168f2213aa0afd7495c133e04b0
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed May 6 12:06:40 EDT 2015
Remove yyin It is better to use directly stdin.
--- a/cc1/error.c
+++ b/cc1/error.c
@@ -50,7 +50,6 @@
{
int c;
va_list va;
- extern FILE *yyin;
va_start(va, fmt);
warn_helper(-1, fmt, va);
@@ -77,7 +76,7 @@
goto jump;
break;
}
- } while ((c = getc(yyin)) != EOF);
+ } while ((c = getchar()) != EOF);
jump:
yytoken = c;
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -9,7 +9,6 @@
#include "../inc/cc.h"
#include "cc1.h"
-FILE *yyin;
uint8_t lex_ns = NS_IDEN;
const char *filename;
unsigned linenum;
@@ -28,7 +27,7 @@
size = sign = 0;
type:
- switch (ch = toupper(getc(yyin))) {
+ switch (ch = toupper(getchar())) {
case 'L':
if (size == LONG + LONG)
goto wrong_type;
@@ -39,7 +38,7 @@
goto wrong_type;
goto type;
default:
- ungetc(ch, yyin);
+ ungetc(ch, stdin);
tp = ctype(INT, sign, size);
break;
wrong_type:
@@ -61,20 +60,20 @@
char ch, *bp;
static char base;
- if ((ch = getc(yyin)) == '0') {
- if (toupper(ch = getc(yyin)) == 'X') {
+ if ((ch = getchar()) == '0') {
+ if (toupper(ch = getchar()) == 'X') {
base = 16;
} else {
base = 8;
- ungetc(ch, yyin);
+ ungetc(ch, stdin);
}
} else {
base = 10;
- ungetc(ch, yyin);
+ ungetc(ch, stdin);
}
for (bp = yytext ; bp < &yytext[IDENTSIZ]; *bp++ = ch) {
- ch = getc(yyin);
+ ch = getchar();
switch (base) {
case 8:
if (ch >= '7')
@@ -95,7 +94,7 @@
if (bp == &yytext[IDENTSIZ])
error("identifier too long %s", yytext);
*bp = '\0';
- ungetc(ch, yyin);
+ ungetc(ch, stdin);
return integer(yytext, base);
}
@@ -105,7 +104,7 @@
char c;
repeat:
- switch (getc(yyin)) {
+ switch (getchar()) {
case '\\': c = '\''; break;
case 'a': c = '\a'; break;
case 'f': c = '\f'; break;
@@ -121,7 +120,7 @@
case 'u': /* TODO: */
case '\n':
++linenum;
- if ((c = getc(yyin)) == '\\')
+ if ((c = getchar()) == '\\')
goto repeat;
break;
default:
@@ -139,11 +138,11 @@
static char c;
Symbol *sym;
- getc(yyin); /* discard the initial ' */
- c = getc(yyin);
+ getchar(); /* discard the initial ' */
+ c = getchar();
if (c == '\\')
escape(&c);
- if (getc(yyin) != '\'')
+ if (getchar() != '\'')
error("invalid character constant");
sym = install("", NS_IDEN);
sym->u.i = c;
@@ -160,10 +159,10 @@
int c;
static Symbol *sym;
- getc(yyin); /* discard the initial " */
+ getchar(); /* discard the initial " */
for (bp = buf; bp < &buf[STRINGSIZ]; ) {
- switch (c = getc(yyin)) {
+ switch (c = getchar()) {
case EOF:
error("found EOF while parsing");
case '"':
@@ -195,13 +194,13 @@
Symbol *sym;
for (bp = yytext; bp < &yytext[IDENTSIZ]; *bp++ = c) {
- if (!isalnum(c = getc(yyin)) && c != '_')
+ if (!isalnum(c = getchar()) && c != '_')
break;
}
if (bp == &yytext[IDENTSIZ])
error("identifier too long %s", yytext);
*bp = '\0';
- ungetc(c, yyin);
+ ungetc(c, stdin);
sym = yylval.sym = lookup(yytext, lex_ns);
if (!sym || sym->token == IDEN)
@@ -213,7 +212,7 @@
static uint8_t
follow(int expect, int ifyes, int ifno)
{
- int c = getc(yyin);
+ int c = getchar();
if (c == expect) {
yytext[1] = c;
@@ -220,7 +219,7 @@
yytext[2] = 0;
return ifyes;
}
- ungetc(c, yyin);
+ ungetc(c, stdin);
return ifno;
}
@@ -227,7 +226,7 @@
static uint8_t
minus(void)
{
- int c = getc(yyin);
+ int c = getchar();
yytext[1] = c;
yytext[2] = '\0';
@@ -237,7 +236,7 @@
case '=': return SUB_EQ;
default:
yytext[1] = '\0';
- ungetc(c, yyin);
+ ungetc(c, stdin);
return '-';
}
}
@@ -245,7 +244,7 @@
static uint8_t
plus(void)
{
- int c = getc(yyin);
+ int c = getchar();
yytext[1] = c;
yytext[2] = '\0';
@@ -254,7 +253,7 @@
case '=': return ADD_EQ;
default:
yytext[1] = '\0';
- ungetc(c, yyin);
+ ungetc(c, stdin);
return '+';
}
}
@@ -262,7 +261,7 @@
static uint8_t
relational(uint8_t op, uint8_t equal, uint8_t shift, uint8_t assig)
{
- int c = getc(yyin);
+ int c = getchar();
yytext[1] = c;
yytext[2] = '\0';
@@ -271,7 +270,7 @@
return equal;
if (c == op)
return follow('=', assig, shift);
- ungetc(c, yyin);
+ ungetc(c, stdin);
yytext[1] = '\0';
return op;
}
@@ -279,7 +278,7 @@
static uint8_t
logic(uint8_t op, uint8_t equal, uint8_t logic)
{
- int c = getc(yyin);
+ int c = getchar();
yytext[1] = c;
yytext[2] = '\0';
@@ -288,7 +287,7 @@
return equal;
if (c == op)
return logic;
- ungetc(c, yyin);
+ ungetc(c, stdin);
yytext[1] = '\0';
return op;
}
@@ -298,10 +297,10 @@
{
int c;
- if ((c = getc(yyin)) != '.') {
- ungetc(c, yyin);
+ if ((c = getchar()) != '.') {
+ ungetc(c, stdin);
return '.';
- } else if ((c = getc(yyin)) != '.') {
+ } else if ((c = getchar()) != '.') {
error("incorrect token '%s'", yytext);
} else {
yytext[2] = yytext[1] = '.';
@@ -313,7 +312,7 @@
static uint8_t
operator(void)
{
- uint8_t c = getc(yyin);
+ uint8_t c = getchar();
yytext[0] = c;
yytext[1] = '\0';
@@ -340,7 +339,7 @@
int c;
- while (isspace(c = getc(yyin))) {
+ while (isspace(c = getchar())) {
if (c == '\n')
++linenum;
}
@@ -352,7 +351,7 @@
{
int c;
- ungetc(c = skipspaces(), yyin);
+ ungetc(c = skipspaces(), stdin);
if (isalpha(c) || c == '_') {
yytoken = iden();
@@ -385,7 +384,7 @@
{
int c;
- ungetc(c = skipspaces(), yyin);
+ ungetc(c = skipspaces(), stdin);
return c;
}
@@ -393,13 +392,11 @@
void
lexfile(const char *file)
{
- if (yyin != NULL)
- fclose(yyin);
+
if (file == NULL) {
- yyin = stdin;
filename = "(stdin)";
} else {
- if ((yyin = fopen(file, "r")) == NULL)
+ if (!freopen(file, "r", stdin))
die("file '%s' not found", file);
filename = file;
}