as: Move writeout to binfmt.c - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 98857fdfe653b04c16f4d3520fe4d1d24a3cb4f0
 (DIR) parent 6035e8511b69b44b65eb8771c3dd84ed21e50cb3
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
       Date:   Sun, 11 Feb 2024 12:34:44 +0100
       
       as: Move writeout to binfmt.c
       
       More changes are expected to be able to write out binaries
       formats that are not just pure dumps.
       
       Diffstat:
         M src/cmd/as/Makefile                 |       1 +
         M src/cmd/as/as.h                     |       5 ++++-
         A src/cmd/as/binfmt.c                 |      35 +++++++++++++++++++++++++++++++
         M src/cmd/as/main.c                   |      26 --------------------------
       
       4 files changed, 40 insertions(+), 27 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/as/Makefile b/src/cmd/as/Makefile
       @@ -6,6 +6,7 @@ include $(PROJECTDIR)/scripts/rules.mk
        MORE_CFLAGS = -I$(INCDIR)/$(STD)
        
        OBJS = \
       +        binfmt.o\
                main.o\
                symbol.o\
                ins.o\
 (DIR) diff --git a/src/cmd/as/as.h b/src/cmd/as/as.h
       @@ -199,6 +199,9 @@ extern Node *moperand(void);
        /* ins.c */
        extern char *tobytes(TUINT v, int n, int inc);
        
       +/* binfmt.c */
       +extern void writeout(char *fname);
       +
        /*
         * Definition of global variables
         */
       @@ -211,7 +214,7 @@ extern int pass;
        extern TUINT maxaddr;
        extern int endian;
        extern Symbol *linesym, *symlist;
       -extern char *infile;
       +extern char *infile, *outfile;
        extern int endpass;
        extern int yytoken;
        extern size_t yylen;
 (DIR) diff --git a/src/cmd/as/binfmt.c b/src/cmd/as/binfmt.c
       @@ -0,0 +1,35 @@
       +#include <errno.h>
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <string.h>
       +
       +#include <scc/scc.h>
       +
       +#include "as.h"
       +
       +void
       +writeout(char *fname)
       +{
       +        Section *sp;
       +        FILE *fp;
       +
       +        if ((fp = fopen(fname, "wb")) == NULL)
       +                goto error;
       +
       +        for (sp = seclist; sp; sp = sp->next) {
       +                if (!sp->mem)
       +                        continue;
       +                fwrite(sp->mem, sp->max - sp->base, 1, fp);
       +        }
       +
       +        if (fclose(fp))
       +                goto error;
       +        outfile = NULL;
       +
       +        return;
       +
       +error:
       +        fprintf(stderr, "as: %s: %s\n", fname, strerror(errno));
       +        exit(EXIT_FAILURE);
       +}
       +
 (DIR) diff --git a/src/cmd/as/main.c b/src/cmd/as/main.c
       @@ -14,32 +14,6 @@ char *outfile = "a.out", *infile;
        int endpass;
        
        static void
       -writeout(char *fname)
       -{
       -        Section *sp;
       -        FILE *fp;
       -
       -        if ((fp = fopen(fname, "wb")) == NULL)
       -                goto error;
       -
       -        for (sp = seclist; sp; sp = sp->next) {
       -                if (!sp->mem)
       -                        continue;
       -                fwrite(sp->mem, sp->max - sp->base, 1, fp);
       -        }
       -
       -        if (fclose(fp))
       -                goto error;
       -        outfile = NULL;
       -
       -        return;
       -
       -error:
       -        fprintf(stderr, "as: %s: %s\n", fname, strerror(errno));
       -        exit(EXIT_FAILURE);
       -}
       -
       -static void
        cleanup(void)
        {
                if (outfile)