Skip to content

Commit

Permalink
Merge pull request #329 from NieDzejkob/allow-stdin-input
Browse files Browse the repository at this point in the history
Allow using - to indicate input from stdin (resolves #305)
  • Loading branch information
AntonioND authored Jul 4, 2019
2 parents 3cd1d46 + df15c97 commit 847cae5
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 33 deletions.
2 changes: 1 addition & 1 deletion include/asm/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct yy_buffer_state {
/* Address where the data is initially written after a safety margin */
char *pBufferStart;
char *pBuffer;
uint32_t nBufferSize;
size_t nBufferSize;
uint32_t oAtLineStart;
};

Expand Down
18 changes: 10 additions & 8 deletions src/asm/fstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,23 +405,25 @@ void fstk_RunRept(uint32_t count)
/*
* Initialize the filestack routines
*/
void fstk_Init(char *s)
void fstk_Init(char *pFileName)
{
char tzFileName[_MAX_PATH + 1];
char tzSymFileName[_MAX_PATH + 1 + 2];

snprintf(tzSymFileName, sizeof(tzSymFileName), "\"%s\"", s);
snprintf(tzSymFileName, sizeof(tzSymFileName), "\"%s\"", pFileName);
sym_AddString("__FILE__", tzSymFileName);

strcpy(tzFileName, s);
pFileStack = NULL;
pCurrentFile = fopen(tzFileName, "rb");
if (pCurrentFile == NULL)
err(1, "Unable to open file '%s'", tzFileName);
if (strcmp(pFileName, "-") == 0) {
pCurrentFile = stdin;
} else {
pCurrentFile = fopen(pFileName, "rb");
if (pCurrentFile == NULL)
err(1, "Unable to open file '%s'", pFileName);
}

nMacroCount = 0;
nCurrentStatus = STAT_isInclude;
snprintf(tzCurrentFileName, _MAX_PATH + 1, "%s", tzFileName);
snprintf(tzCurrentFileName, _MAX_PATH + 1, "%s", pFileName);
CurrentFlexHandle = yy_create_buffer(pCurrentFile);
yy_switch_to_buffer(CurrentFlexHandle);
nLineNo = 1;
Expand Down
69 changes: 56 additions & 13 deletions src/asm/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,71 @@ YY_BUFFER_STATE yy_create_buffer(FILE *f)
if (pBuffer == NULL)
fatalerror("%s: Out of memory!", __func__);

uint32_t size;
size_t size = 0, capacity = -1;
char *buf = NULL;

fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);

/* Give extra room for 2 newlines and terminator */
uint32_t capacity = size + 3;
/*
* Check if we can get the file size without implementation-defined
* behavior:
*
* From ftell(3p):
* [On error], ftell() and ftello() shall return −1, and set errno to
* indicate the error.
*
* The ftell() and ftello() functions shall fail if: [...]
* ESPIPE The file descriptor underlying stream is associated with a
* pipe, FIFO, or socket.
*
* From fseek(3p):
* The behavior of fseek() on devices which are incapable of seeking
* is implementation-defined.
*/
if (ftell(f) != -1) {
fseek(f, 0, SEEK_END);
capacity = ftell(f);
rewind(f);
}

pBuffer->pBufferRealStart = malloc(capacity + SAFETYMARGIN);
// If ftell errored or the block above wasn't executed
if (capacity == -1)
capacity = 4096;
// Handle 0-byte files gracefully
else if (capacity == 0)
capacity = 1;

while (!feof(f)) {
if (buf == NULL || size >= capacity) {
if (buf)
capacity *= 2;
/* Give extra room for 2 newlines and terminator */
buf = realloc(buf, capacity + SAFETYMARGIN + 3);

if (buf == NULL)
fatalerror("%s: Out of memory for buffer!",
__func__);
}

if (pBuffer->pBufferRealStart == NULL)
fatalerror("%s: Out of memory for buffer!", __func__);
char *bufpos = buf + SAFETYMARGIN + size;
size_t read_count = fread(bufpos, 1, capacity - size, f);

pBuffer->pBufferStart = pBuffer->pBufferRealStart + SAFETYMARGIN;
pBuffer->pBuffer = pBuffer->pBufferRealStart + SAFETYMARGIN;
if (read_count == 0 && !feof(f))
fatalerror("%s: fread error", __func__);

size = fread(pBuffer->pBuffer, sizeof(uint8_t), size, f);
size += read_count;
}

pBuffer->pBufferRealStart = buf;
pBuffer->pBufferStart = buf + SAFETYMARGIN;
pBuffer->pBuffer = buf + SAFETYMARGIN;
pBuffer->pBuffer[size] = 0;
pBuffer->nBufferSize = size;

/* This is added here to make the buffer scaling above easy to express,
* while taking the newline space into account
* for the `yy_buffer_append`s below.
*/
capacity += 3;

/* Convert all line endings to LF and spaces */

char *mem = pBuffer->pBuffer;
Expand Down
Empty file added test/asm/bank-noexist.out.pipe
Empty file.
2 changes: 2 additions & 0 deletions test/asm/divzero-instr.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR: -(2):
Division by zero
4 changes: 4 additions & 0 deletions test/asm/divzero-section-bank.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ERROR: -(1):
Invalid integer constant
ERROR: -(1):
Division by zero
Empty file added test/asm/equ-charmap.out.pipe
Empty file.
3 changes: 3 additions & 0 deletions test/asm/label-redefinition.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ERROR: -(7):
'Sym' already defined in m(6)
error: Assembly aborted (1 errors)!
2 changes: 2 additions & 0 deletions test/asm/line-continuation.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR: -(2) -> @(-1):
Macro '@' not defined
3 changes: 3 additions & 0 deletions test/asm/local-wrong-parent.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ERROR: -(5):
Not currently in the scope of 'WrongParent'
error: Assembly aborted (1 errors)!
2 changes: 2 additions & 0 deletions test/asm/macro-@.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR: -(1) -> @(-1):
Macro '@' not defined
Empty file.
2 changes: 2 additions & 0 deletions test/asm/null-in-macro.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR: -(1):
Unterminated MACRO definition.
3 changes: 3 additions & 0 deletions test/asm/reference-undefined-sym.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ERROR: -(4):
'X' already referenced at -(2)
error: Assembly aborted (1 errors)!
Empty file.
2 changes: 2 additions & 0 deletions test/asm/remote-local-noexist.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ERROR: -(7):
'Parent.child.fail' is a nonsensical reference to a nested local symbol
Empty file added test/asm/remote-local.out.pipe
Empty file.
2 changes: 2 additions & 0 deletions test/asm/strlen.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$3
$4
31 changes: 31 additions & 0 deletions test/asm/strsub.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
warning: -(13) -> xstrsub(1):
STRSUB: Length too big: 32
warning: -(14) -> xstrsub(1):
STRSUB: Length too big: 300
warning: -(15) -> xstrsub(1):
STRSUB: Position starts at 1
warning: -(15) -> xstrsub(1):
STRSUB: Length too big: 300
warning: -(16) -> xstrsub(1):
STRSUB: Position 4 is past the end of the string
warning: -(17) -> xstrsub(1):
STRSUB: Position 4 is past the end of the string
warning: -(17) -> xstrsub(1):
STRSUB: Length too big: 1
warning: -(20) -> xstrsub(1):
STRSUB: Length too big: 10
A
B
C
AB
BC
BC
BC
ABC


カタ
カナ
カナ
g
29 changes: 18 additions & 11 deletions test/asm/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ after=$(mktemp)
rc=0

for i in *.asm; do
../../rgbasm -o $o $i > $after 2>&1
diff -u ${i%.asm}.out $after
rc=$(($? || $rc))
bin=${i%.asm}.out.bin
if [ -f $bin ]; then
../../rgblink -o $gb $o > $after 2>&1
head -c $(wc -c < $bin) $gb > $after 2>&1
hexdump -C $after > $before && mv $before $after
hexdump -C $bin > $before
diff -u $before $after
for variant in '' '.pipe'; do
if [ -z "$variant" ]; then
../../rgbasm -o $o $i > $after 2>&1
else
cat $i | ../../rgbasm -o $o - > $after 2>&1
fi

diff -u ${i%.asm}.out$variant $after
rc=$(($? || $rc))
fi
bin=${i%.asm}.out.bin
if [ -f $bin ]; then
../../rgblink -o $gb $o > $after 2>&1
head -c $(wc -c < $bin) $gb > $after 2>&1
hexdump -C $after > $before && mv $before $after
hexdump -C $bin > $before
diff -u $before $after
rc=$(($? || $rc))
fi
done
done

rm -f $o $gb $before $after
Expand Down
1 change: 1 addition & 0 deletions test/asm/undefined-dot.out.pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: -(3) : '.' not defined
Empty file added test/asm/utf-8.out.pipe
Empty file.
Empty file added test/asm/zero-byte-file.asm
Empty file.
Empty file added test/asm/zero-byte-file.out
Empty file.
Empty file.

0 comments on commit 847cae5

Please sign in to comment.