Skip to content

Commit

Permalink
Update codebase (#31)
Browse files Browse the repository at this point in the history
* fix: use stdlib isspace function

* chore: Minor updates

* fix: more minor changes
  • Loading branch information
cyuria committed May 31, 2024
1 parent f012409 commit c8df695
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 23 deletions.
6 changes: 5 additions & 1 deletion docs/build.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Building Wrasm

To compile wrasm, ensure you have [meson](https://mesonbuild.com) as well as
[ninja](https://ninja-build.org) and a compatible C compiler installed on your
[ninja](https://ninja-build.org) and a compatible C[^1] compiler installed on your
system.

To compile run the following commands in a terminal or command prompt:
Expand All @@ -22,6 +22,10 @@ To compile from another directory, just add the `-C` option to meson:
meson compile -C path/to/build/directory
```

[^1]: The C compiler should support the c17 standard (this may be updated to
c23 once proper compiler support is established). At the time of writing,
wrasm successfully builds with c99, however support is not guaranteed.

## Choosing a different compiler and linker

It is possible to manually set the C compiler meson uses with the `CC` variable
Expand Down
1 change: 0 additions & 1 deletion h/stringutil.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

int is_terminating(char);
int is_whitespace(char);
char *trim_whitespace(const char *);
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project(
'c_std=c17',
'warning_level=3',
'b_lto=true',
'b_pie=true',
],
version: '0.0.1-alpha',
)
Expand Down
3 changes: 2 additions & 1 deletion src/directives.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "directives.h"

#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -42,7 +43,7 @@ static struct directive_t get_directive(const char *name)
int parse_directive(char *line)
{
char *directivename = line;
while (!is_whitespace(*line) && *line)
while (!isspace(*line))
line++;
if (*line)
*(line++) = '\0';
Expand Down
6 changes: 2 additions & 4 deletions src/elf/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ int fill_symtab(void)
struct elf64sym_t entry = (struct elf64sym_t){
.name = strtab_addr,
.info = sym->binding,
.other =
0, /* TODO: add other visibility attributes */
.other = 0, /* TODO: add other attributes */
.shndx = (uint16_t)sym->section,
.value = (uint64_t)sym->value,
.size = 0, /* TODO: support for symbol sizes? */
Expand All @@ -159,8 +158,7 @@ int fill_symtab(void)
&entry, sizeof(entry),
(struct sectionpos_t){
.section = section_symtab,
.offset = count *
sizeof(struct elf64sym_t) });
.offset = count * sizeof(entry) });
strtab_addr += (uint32_t)sym->name_sz;
count++;
}
Expand Down
18 changes: 4 additions & 14 deletions src/stringutil.c
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@

#include "stringutil.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "debug.h"
#include "xmalloc.h"

int is_terminating(char c)
{
return c == '\0' || c == ';' || c == '\n';
}

int is_whitespace(char c)
{
return c == ' ' || c == '\t' || c == '\n';
return !c || c == ';' || c == '\n';
}

char *trim_whitespace(const char *str)
{
const char *start = str;
while (is_whitespace(*start) && !is_terminating(*start))
while (isspace(*start) && !is_terminating(*start))
start++;

const char *end = start;
while (!is_terminating(*end))
end++;

while (is_whitespace(*end) && end > start)
while (isspace(*end) && end > start)
end--;

char *newstr = xmalloc((size_t)(end - start + 1));
if (newstr == NULL) {
logger(ERROR, error_internal,
"Unable to allocate memory for stripped string");
return NULL;
}
memcpy(newstr, start, (size_t)(end - start));
newstr[end - start] = '\0';

Expand Down
6 changes: 4 additions & 2 deletions src/symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ static size_t hash_str(const char *str)
struct symbol_t *get_symbol(const char *name)
{
const size_t hash = hash_str(name);
for (size_t i = 0; i < symbols[hash].count; i++)
if (!strcmp(name, symbols[hash].data[i].name))
for (size_t i = 0; i < symbols[hash].count; i++) {
if (!strcmp(name, symbols[hash].data[i].name)) {
return &symbols[hash].data[i];
}
}
return NULL;
}

Expand Down

0 comments on commit c8df695

Please sign in to comment.