-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.cc
187 lines (174 loc) · 4.44 KB
/
file.cc
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#define ICI_CORE
#include "file.h"
#include "archiver.h"
#include "fwd.h"
#include "int.h"
#include "null.h"
#include "parse.h"
#include "primes.h"
#include "str.h"
namespace ici
{
/*
* Return a file object with the given 'ftype' and a file type specific
* pointer 'fp' which is often something like a 'STREAM *' or a file
* descriptor. The 'name' is mostly for error messages and stuff. The
* returned object has a ref count of 1. Returns nullptr on error.
*
* The 'ftype' is a pointer to a struct of stdio-like function pointers that
* will be used to do I/O operations on the file (see 'ftype'). The
* given structure is assumed to exist as long as necessary. (It is normally
* a static srtucture, so this is not a problem.) The core-supplied struct
* 'stdio_ftype' can be used if 'fp' is a 'STREAM *'.
*
* The 'ref' argument is an object reference that the file object will keep in
* case the 'fp' argument is an implicit reference into some object (for
* example, this is used for reading an ICI string as a file). It may be nullptr
* if not required.
*
* This --func-- forms part of the --ici-api--.
*/
file *new_file(void *fp, ftype *ftype, str *name, object *ref)
{
file *f;
if ((f = ici_talloc(file)) == nullptr)
{
return nullptr;
}
set_tfnz(f, TC_FILE, 0, 1, 0);
f->f_file = fp;
f->f_type = ftype;
f->f_name = name;
f->f_ref = ref;
rego(f);
return f;
}
/*
* Close the given ICI file 'f' by calling the lower-level close function
* given in the 'ftype' associated with the file. A guard flag is
* maintained in the file object to prevent multiple calls to the lower level
* function (this is really so we can optionally close the file explicitly,
* and let the garbage collector do it to). Returns non-zero on error, usual
* conventions.
*
* This --func-- forms part of the --ici-api--.
*/
int close_file(file *f)
{
exec *x = nullptr;
int r;
if (f->hasflag(file::closed))
{
return set_error("file already closed");
}
f->set(file::closed);
if (f->hasflag(ftype::nomutex))
{
x = leave();
}
r = f->close();
if (f->hasflag(ftype::nomutex))
{
enter(x);
}
/*
* If this is a pipe opened with popen(), 'r' is actually the exit status
* of the process. If this is non-zero, format it into an error message.
* Note: we can't do this within popen_ftype's close(), because
* modifying error between calls to leave()/enter() is not
* allowed.
*/
if (r != 0 && f->f_type == popen_ftype)
{
set_error("exit status %d", r);
}
return r;
}
size_t file_type::mark(object *o)
{
auto f = fileof(o);
return type::mark(f) + mark_optional(f->f_name) + mark_optional(f->f_ref);
}
void file_type::free(object *o)
{
if (!o->hasflag(file::closed))
{
if (o->hasflag(file::noclose))
{
fileof(o)->flush();
}
else
{
close_file(fileof(o));
}
}
ici_tfree(o, file);
}
int file_type::cmp(object *o1, object *o2)
{
return fileof(o1)->f_file != fileof(o2)->f_file || fileof(o1)->f_type != fileof(o2)->f_type;
}
object *file_type::fetch(object *o, object *k)
{
if (k == SS(name))
{
if (fileof(o)->f_name != nullptr)
{
return fileof(o)->f_name;
}
return null;
}
if (fileof(o)->f_type == parse_ftype && k == SS(line))
{
integer *l;
if ((l = new_int(parseof(fileof(o)->f_file)->p_lineno)) != nullptr)
{
decref(l);
}
return l;
}
return fetch_fail(o, k);
}
int file_type::save(archiver *ar, object *o)
{
if (fileof(o)->f_type == stdio_ftype)
{
FILE *f = (FILE *)fileof(o)->f_file;
if (f == stdin)
{
return ar->write(uint8_t('i'));
}
if (f == stdout)
{
return ar->write(uint8_t('o'));
}
if (f == stderr)
{
return ar->write(uint8_t('e'));
}
}
return type::save(ar, o);
}
object *file_type::restore(archiver *ar)
{
uint8_t code;
if (ar->read(&code))
{
return nullptr;
}
if (code == 'i')
{
return need_stdin();
}
if (code == 'o')
{
return need_stdout();
}
if (code == 'e')
{
return need_stderr();
}
set_error("unexpected stream code (%u) when restoring file", code);
return nullptr;
}
} // namespace ici