diff options
author | Thomas White <taw@bitwiz.org.uk> | 2017-03-02 22:40:58 +0100 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2017-03-02 22:45:07 +0100 |
commit | e7eede3273394f3479bb63f92e57780dfc8f2e3d (patch) | |
tree | ad8467da22a4c1f2cdca113b77f096e5e643b98b /src/sc_parse.c | |
parent | 397fbd7032b57fec6aee9b8eac4ccf693aedc63a (diff) |
sc_block_delete() may change the top block
Diffstat (limited to 'src/sc_parse.c')
-rw-r--r-- | src/sc_parse.c | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/src/sc_parse.c b/src/sc_parse.c index ccc6f6a..a497334 100644 --- a/src/sc_parse.c +++ b/src/sc_parse.c @@ -213,23 +213,34 @@ static SCBlock *sc_find_parent(SCBlock *top, SCBlock *find) void sc_block_substitute(SCBlock **top, SCBlock *old, SCBlock *new) { + if ( old == NULL ) { + fprintf(stderr, "Substituting nothing!\n"); + return; + } + if ( old == *top ) { /* It is the first block */ new->next = old->next; *top = new; } else { - sc_block_unlink(*top, old); - sc_block_append_p(new, *top); + sc_block_unlink(top, old); + sc_block_append_p(*top, new); } } -/* Delete "deleteme", which is somewhere under "top" */ -void sc_block_unlink(SCBlock *top, SCBlock *deleteme) +/* Unlink "deleteme", which is somewhere under "top" */ +void sc_block_unlink(SCBlock **top, SCBlock *deleteme) { - SCBlock *parent = sc_find_parent(top, deleteme); + SCBlock *parent = sc_find_parent(*top, deleteme); if ( parent == NULL ) { - fprintf(stderr, "Couldn't find block parent!\n"); + /* Maybe it's the first block? */ + if ( *top == deleteme ) { + fprintf(stderr, "Unlinking at top\n"); + *top = (*top)->next; + } else { + fprintf(stderr, "Couldn't find block parent!\n"); + } return; } @@ -243,12 +254,14 @@ void sc_block_unlink(SCBlock *top, SCBlock *deleteme) } -void sc_block_delete(SCBlock *top, SCBlock *deleteme) +/* Delete "deleteme", which is somewhere under "top" */ +void sc_block_delete(SCBlock **top, SCBlock *deleteme) { sc_block_unlink(top, deleteme); sc_block_free(deleteme); } + /* Frees "bl" and all its children (but not the blocks following it) */ void sc_block_free(SCBlock *bl) { |