-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbin.c
139 lines (119 loc) · 4.18 KB
/
fbin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/****************************************************************************
*
* Code to support the bin (binary) output format
* Copyright (C) 2023 Sean Conner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
****************************************************************************/
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "a09.h"
/**************************************************************************/
char const format_bin_usage[] = "";
/**************************************************************************/
static bool fbin_align(struct format *fmt,struct opcdata *opd)
{
assert(fmt != NULL);
assert(opd != NULL);
assert((opd->pass == 1) || (opd->pass == 2));
assert(fmt->backend == BACKEND_BIN);
(void)fmt;
if (opd->pass == 2)
{
if (fseek(opd->a09->out,opd->datasz,SEEK_CUR) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
}
return true;
}
/**************************************************************************/
static bool fbin_org(struct format *format,struct opcdata *opd)
{
assert(format != NULL);
assert(opd != NULL);
assert((opd->pass == 1) || (opd->pass == 2));
assert(format->backend == BACKEND_BIN);
if (opd->pass == 2)
{
/*-------------------------------------------------------------------
; format->data is being used as a flag to do seeks *after* the first
; write to the output file. That is all. (Why yes, I did question this
; when I came across this to make a change, and found out the hard way
; what I was doing back in the past).
;--------------------------------------------------------------------*/
if (format->data)
{
long int delta = opd->value.value - opd->a09->pc;
if (fseek(opd->a09->out,delta,SEEK_CUR) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
}
format->data = format;
}
opd->a09->pc = opd->value.value;
return true;
}
/**************************************************************************/
static bool fbin_rmb(struct format *format,struct opcdata *opd)
{
assert(format != NULL);
assert(opd != NULL);
assert((opd->pass == 1 ) || (opd->pass == 2));
assert(format->backend == BACKEND_BIN);
(void)format;
if (opd->pass == 2)
{
if (opd->value.value == 0)
return message(opd->a09,MSG_ERROR,"E0099: Can't reserve 0 bytes of memory");
if (fseek(opd->a09->out,opd->value.value,SEEK_CUR) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
}
return true;
}
/**************************************************************************/
bool format_bin_init(struct a09 *a09)
{
static struct format const callbacks =
{
.backend = BACKEND_BIN,
.cmdline = fdefault_cmdline,
.pass_start = fdefault_pass,
.pass_end = fdefault_pass,
.write = fdefault_write,
.opt = fdefault__opt,
.dp = fdefault,
.code = fdefault,
.align = fbin_align,
.end = fdefault_end,
.org = fbin_org,
.rmb = fbin_rmb,
.setdp = fdefault,
.test = fdefault__test,
.tron = fdefault,
.troff = fdefault,
.Assert = fdefault,
.endtst = fdefault,
.Float = freal__ieee,
.fini = fdefault_fini,
.data = NULL,
};
assert(a09 != NULL);
a09->format = callbacks;
a09->format.data = NULL;
return true;
}
/**************************************************************************/