diff --git a/assembler/assembler.c b/assembler/assembler.c index 2e53580..e3deb26 100644 --- a/assembler/assembler.c +++ b/assembler/assembler.c @@ -91,8 +91,8 @@ void transform_relative_labels(tokenized_expression_t *expression, int last_rela free(token->symbol); token->symbol = malloc(size); if (snprintf(token->symbol, size, fmtstring, relative_label) != size - 1) { - scas_log(L_ERROR, "UNREACHABLE"); - exit(1); + scas_log(L_ERROR, "UNREACHABLE"); + exit(1); } scas_log(L_DEBUG, "Transformed relative label with offset %d to %s, %d - %d", offset, token->symbol, i, j); @@ -245,8 +245,8 @@ int try_expand_macro(struct assembler_state *state, char **line) { for (k = 0; k < macro->parameters->length; ++k) { char *p = macro->parameters->items[k]; if (!strcmp(p, "")) { - scas_log(L_ERROR, "Attempt to insert '%s' where no parameter is expected...", (char*)userparams->items[k]); - break; + scas_log(L_ERROR, "Attempt to insert '%s' where no parameter is expected...", (char*)userparams->items[k]); + break; } scas_log(L_DEBUG, "Replacing '%s' with '%s' for line '%s'", p, (char *)userparams->items[k], (char *)newlines->items[j]); @@ -453,7 +453,7 @@ int try_match_instruction(struct assembler_state *state, char **_line) { late_imm->expression = expression; list_add(state->current_area->late_immediates, late_imm); } else { - free_expression(expression); + free_expression(expression); if (imm->type == IMM_TYPE_RELATIVE) { result = result - ((state->PC + scas_runtime.options.origin) + (match->instruction->width / 8)); @@ -619,8 +619,8 @@ object_t *assemble(FILE *file, const char *file_name, assembler_settings_t *sett source_map_entry_t *entry = map->entries->items[map->entries->length - 1]; entry->length = state.PC - entry->address; if (entry->length == 0) { - free(entry->source_code); - free(entry); + free(entry->source_code); + free(entry); list_del(map->entries, map->entries->length - 1); } } @@ -694,17 +694,17 @@ object_t *assemble(FILE *file, const char *file_name, assembler_settings_t *sett stack_free(state.line_number_stack); list_free(state.extra_lines); for (int i = 0; i < state.macros->length; i += 1) { - macro_t *macro = state.macros->items[i]; - list_free(macro->parameters); - free_flat_list(macro->macro_lines); - free(macro->name); - free(macro); + macro_t *macro = state.macros->items[i]; + list_free(macro->parameters); + free_flat_list(macro->macro_lines); + free(macro->name); + free(macro); } list_free(state.macros); for (int i = 0; i < state.equates->length; i += 1) { - symbol_t *sym = (symbol_t*)state.equates->items[i]; - free(sym->name); - free(sym); + symbol_t *sym = (symbol_t*)state.equates->items[i]; + free(sym->name); + free(sym); } list_free(state.equates); stack_free(state.source_map_stack); diff --git a/assembler/directives.c b/assembler/directives.c index 5ae7622..2186626 100644 --- a/assembler/directives.c +++ b/assembler/directives.c @@ -280,7 +280,7 @@ int handle_db(struct assembler_state *state, char **argv, int argc) { append_to_area(state->current_area, state->instruction_buffer, 1); ++state->PC; if (!keep) { - free_expression(expression); + free_expression(expression); } } } @@ -560,7 +560,7 @@ int handle_dw(struct assembler_state *state, char **argv, int argc) { } } if (!keep) { - free_expression(expression); + free_expression(expression); } append_to_area(state->current_area, state->instruction_buffer, 2); state->PC += 2; @@ -1146,7 +1146,7 @@ int handle_macro(struct assembler_state *state, char **argv, int argc) { return 1; } char *location = strchr(argv[0], '('); - + if (location == argv[0]) { ERROR(ERROR_INVALID_DIRECTIVE, state->column, "macro without a name"); return 1; @@ -1179,8 +1179,8 @@ int handle_macro(struct assembler_state *state, char **argv, int argc) { // TODO: Free everything } else if (end == location && *end == ')') { - // No parameters - break; + // No parameters + break; } char *parameter = malloc(end - location + 1); strncpy(parameter, location, end - location); @@ -1478,7 +1478,7 @@ int try_handle_directive(struct assembler_state *state, char **line) { int ret = d->function(state, argv, argc); deindent_log(); for (int i = 0; i < argc; i += 1) { - free(argv[i]); + free(argv[i]); } free(argv); return ret; diff --git a/common/expression.c b/common/expression.c index 2fd7736..c95885d 100644 --- a/common/expression.c +++ b/common/expression.c @@ -275,14 +275,14 @@ enum { }; void free_expression(tokenized_expression_t *expression) { - for (int i = 0; i < expression->tokens->length; i += 1) { - free_expression_token((expression_token_t*)expression->tokens->items[i]); - } - list_free(expression->tokens); - if (expression->symbols) { - free_flat_list(expression->symbols); - } - free(expression); + for (int i = 0; i < expression->tokens->length; i += 1) { + free_expression_token((expression_token_t*)expression->tokens->items[i]); + } + list_free(expression->tokens); + if (expression->symbols) { + free_flat_list(expression->symbols); + } + free(expression); } // Based on shunting-yard diff --git a/common/functions.c b/common/functions.c index ae3268b..9ba23f8 100644 --- a/common/functions.c +++ b/common/functions.c @@ -103,7 +103,7 @@ void remove_unused_functions(object_t *object) { area_t *area = object->areas->items[i]; metadata_t *meta = get_area_metadata(area, "scas.functions"); if (meta) { - list_t *decoded = decode_function_metadata(area, meta->value); + list_t *decoded = decode_function_metadata(area, meta->value); list_cat(functions, decoded); list_free(decoded); } @@ -115,11 +115,11 @@ void remove_unused_functions(object_t *object) { symbol_t *end = get_symbol_by_name(meta->area, meta->end_symbol); if (!start || !end) { scas_log(L_ERROR, "Warning: function %s has unknown start and end symbols", meta->name); - function_metadata_t *func = functions->items[i]; - free(func->name); - free(func->start_symbol); - free(func->end_symbol); - free(func); + function_metadata_t *func = functions->items[i]; + free(func->name); + free(func->start_symbol); + free(func->end_symbol); + free(func); list_del(functions, i); --i; } else { @@ -198,11 +198,11 @@ void remove_unused_functions(object_t *object) { } } for (int i = 0; i < functions->length; i++) { - function_metadata_t *func = functions->items[i]; - free(func->name); - free(func->start_symbol); - free(func->end_symbol); - free(func); + function_metadata_t *func = functions->items[i]; + free(func->name); + free(func->start_symbol); + free(func->end_symbol); + free(func); } list_free(functions); } diff --git a/common/match.c b/common/match.c index 5df54bc..b0714cd 100644 --- a/common/match.c +++ b/common/match.c @@ -26,7 +26,7 @@ char *get_operand_string(instruction_t *inst, int *i, const char *code, int j) { } } if (delimiter == '\0') { - return strdup(code + j); + return strdup(code + j); } const char *significant_delimiters = "%@&^"; if (strchr(significant_delimiters, delimiter)) { diff --git a/common/objects.c b/common/objects.c index b4df787..0a690b2 100644 --- a/common/objects.c +++ b/common/objects.c @@ -118,14 +118,14 @@ void set_area_metadata(area_t *area, const char *key, char *value, uint64_t valu for (int i = 0; i < area->metadata->length; ++i) { metadata_t *meta = area->metadata->items[i]; if (strcmp(meta->key, key) == 0) { - free(meta->key); - if (meta->value != value) { - free(meta->value); - } - else { - dupe = false; - } - free(meta); + free(meta->key); + if (meta->value != value) { + free(meta->value); + } + else { + dupe = false; + } + free(meta); list_del(area->metadata, i); break; } @@ -137,7 +137,7 @@ void set_area_metadata(area_t *area, const char *key, char *value, uint64_t valu newmeta->value = strdup(value); } else { - newmeta->value = value; + newmeta->value = value; } scas_log(L_DEBUG, "Set area metadata '%s' to new value with length %d", newmeta->key, newmeta->value_length); list_add(area->metadata, newmeta); diff --git a/linker/merge.c b/linker/merge.c index 67a2dae..4af7f98 100644 --- a/linker/merge.c +++ b/linker/merge.c @@ -89,12 +89,12 @@ bool merge_areas(object_t *merged, object_t *source) { uint64_t len; char *merged_metadata = encode_function_metadata(merged, &len); for (int i = 0; i < merged->length; i++) { - function_metadata_t *func = merged->items[i]; - free(func->name); - free(func->start_symbol); - free(func->end_symbol); + function_metadata_t *func = merged->items[i]; + free(func->name); + free(func->start_symbol); + free(func->end_symbol); } - free_flat_list(merged); + free_flat_list(merged); set_area_metadata(merged_area, "scas.functions", merged_metadata, len); free(merged_metadata); } diff --git a/scas/main.c b/scas/main.c index 67c1927..cf12695 100644 --- a/scas/main.c +++ b/scas/main.c @@ -222,8 +222,8 @@ int main(int argc, char **argv) { validate_scas_runtime(); instruction_set_t *instruction_set = find_inst(); if (instruction_set == NULL) { - fprintf(stderr, "Failed to load instruction set definition, unable to continue!\n"); - return 1; + fprintf(stderr, "Failed to load instruction set definition, unable to continue!\n"); + return 1; } scas_log(L_INFO, "Loaded instruction set: %s", instruction_set->arch); list_t *include_path = split_include_path(); @@ -312,8 +312,8 @@ int main(int argc, char **argv) { else { scas_log(L_ERROR, "Failed to merge"); if (out != stdout) { - remove(scas_runtime.output_file); - } + remove(scas_runtime.output_file); + } } fclose(out); } @@ -337,7 +337,7 @@ int main(int argc, char **argv) { fprintf(stderr, "\n"); } if (error->line) { - free (error->line); + free (error->line); } free(error->file_name); free(error->message); @@ -368,7 +368,7 @@ int main(int argc, char **argv) { list_free(scas_runtime.input_files); free_flat_list(include_path); for (int i = 0; i < objects->length; i += 1) { - object_free((object_t*)objects->items[i]); + object_free((object_t*)objects->items[i]); } list_free(objects); list_free(errors); diff --git a/tables/generate.c b/tables/generate.c index 2642b3a..8a59305 100644 --- a/tables/generate.c +++ b/tables/generate.c @@ -8,117 +8,117 @@ // a/b/sej bool make_containing_folder(const char *const path) { - int last_slash = strlen(path); - while (path[last_slash - 1] != '/') { - last_slash -= 1; - if (last_slash == 0) { - fprintf(stderr, "Path appears to be relative to current dir, not making any folder.\n"); - return true; - } - } - char *buffer = malloc(last_slash + 1); - strncpy(buffer, path, last_slash); - const int result = mkdir(buffer, S_IRWXU | S_IRWXG | S_IROTH); - if (errno == 0) { - fprintf(stderr, "Created %s\n", buffer); - } - else if (errno != EEXIST) { - fprintf(stderr, "Failed to create %s\n", buffer); - } - free(buffer); - return result == 0 || errno == EEXIST; + int last_slash = strlen(path); + while (path[last_slash - 1] != '/') { + last_slash -= 1; + if (last_slash == 0) { + fprintf(stderr, "Path appears to be relative to current dir, not making any folder.\n"); + return true; + } + } + char *buffer = malloc(last_slash + 1); + strncpy(buffer, path, last_slash); + const int result = mkdir(buffer, S_IRWXU | S_IRWXG | S_IROTH); + if (errno == 0) { + fprintf(stderr, "Created %s\n", buffer); + } + else if (errno != EEXIST) { + fprintf(stderr, "Failed to create %s\n", buffer); + } + free(buffer); + return result == 0 || errno == EEXIST; } int main(int argc, const char **argv) { - if (argc != 4) { - printf("Usage: %s SOURCE DESTINATION HEADER\n", argv[0]); - return 1; - } - FILE *source = fopen(argv[1], "rb"); - if (!source) { - perror("Error opening source file"); - return 1; - } - if (!make_containing_folder(argv[2])) { - fprintf(stderr, "Error creating output's containing folder!\n"); - return 1; - } - FILE *destination = fopen(argv[2], "wb"); - if (!destination) { - perror("Error opening destination file"); - fclose(source); - return 1; - } - FILE *header = fopen(argv[3], "wb"); - if (!header) { - perror("Error opening header file"); - fclose(source); - fclose(destination); - return 1; - } - int return_code = 1; + if (argc != 4) { + printf("Usage: %s SOURCE DESTINATION HEADER\n", argv[0]); + return 1; + } + FILE *source = fopen(argv[1], "rb"); + if (!source) { + perror("Error opening source file"); + return 1; + } + if (!make_containing_folder(argv[2])) { + fprintf(stderr, "Error creating output's containing folder!\n"); + return 1; + } + FILE *destination = fopen(argv[2], "wb"); + if (!destination) { + perror("Error opening destination file"); + fclose(source); + return 1; + } + FILE *header = fopen(argv[3], "wb"); + if (!header) { + perror("Error opening header file"); + fclose(source); + fclose(destination); + return 1; + } + int return_code = 1; - if (fseek(source, 0, SEEK_END) != 0) { - puts("Seek failed!"); - goto cleanup; - } - long length = ftell(source); - if (length == -1) { - puts("Failed to determine source file size!"); - goto cleanup; - } - rewind(source); - char *buf = malloc(length); - if (!buf) { - puts("out of memory!"); - goto cleanup; - } - if (fread(buf, 1, length, source) != (size_t)length) { - puts("Failed to read source file into buffer!"); - goto cleanup; - } + if (fseek(source, 0, SEEK_END) != 0) { + puts("Seek failed!"); + goto cleanup; + } + long length = ftell(source); + if (length == -1) { + puts("Failed to determine source file size!"); + goto cleanup; + } + rewind(source); + char *buf = malloc(length); + if (!buf) { + puts("out of memory!"); + goto cleanup; + } + if (fread(buf, 1, length, source) != (size_t)length) { + puts("Failed to read source file into buffer!"); + goto cleanup; + } - if (fprintf(destination, "const char z80_tab[%lu] = {", length + 1) < 0) { - puts("Failed to print to file!"); - goto cleanup; - } - if (fprintf(header, "#ifndef Z80_TABLE\n#define Z80_TABLE\n\nextern const char z80_tab[%lu];\n\n#endif\n", length + 1) < 0) { - puts("Failed to write header!"); - goto cleanup; - } - for (long i = 0; i < length; i++) { - if (fprintf(destination, "%s0x%02x,", i % 8 == 0 ? "\n\t" : " ", buf[i]) < 0) { - puts("Failed to print to file!"); - goto cleanup; - } - } + if (fprintf(destination, "const char z80_tab[%lu] = {", length + 1) < 0) { + puts("Failed to print to file!"); + goto cleanup; + } + if (fprintf(header, "#ifndef Z80_TABLE\n#define Z80_TABLE\n\nextern const char z80_tab[%lu];\n\n#endif\n", length + 1) < 0) { + puts("Failed to write header!"); + goto cleanup; + } + for (long i = 0; i < length; i++) { + if (fprintf(destination, "%s0x%02x,", i % 8 == 0 ? "\n\t" : " ", buf[i]) < 0) { + puts("Failed to print to file!"); + goto cleanup; + } + } - if (fwrite("\n\t0,\n};\n", 1, 8, destination) != 8) { - puts("Failed to write to file!"); - goto cleanup; - } - return_code = 0; + if (fwrite("\n\t0,\n};\n", 1, 8, destination) != 8) { + puts("Failed to write to file!"); + goto cleanup; + } + return_code = 0; cleanup: - if (fflush(destination) != 0) { - puts("Failed to flush file!"); - return_code = 1; - } - if (fflush(header) != 0) { - puts("Failed to flush header file!"); - return_code = 1; - } - if (fclose(destination) != 0) { - puts("Failed to close output file!"); - return_code = 1; - } - if (fclose(header) != 0) { - puts("Failed to close header file!"); - return_code = 1; - } - if (fclose(source) != 0) { - puts("Failed to close input file!"); - return_code = 1; - } + if (fflush(destination) != 0) { + puts("Failed to flush file!"); + return_code = 1; + } + if (fflush(header) != 0) { + puts("Failed to flush header file!"); + return_code = 1; + } + if (fclose(destination) != 0) { + puts("Failed to close output file!"); + return_code = 1; + } + if (fclose(header) != 0) { + puts("Failed to close header file!"); + return_code = 1; + } + if (fclose(source) != 0) { + puts("Failed to close input file!"); + return_code = 1; + } - return return_code; + return return_code; }