@portll/cobolwork 0.2.117 → 0.2.150
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/THIRD-PARTY-NOTICES.md +3 -2
- package/bin/cobolwork.mjs +16 -1
- package/lib/baseline.mjs +8 -6
- package/lib/bms.mjs +1 -1
- package/lib/capabilities.mjs +2 -1
- package/lib/cards.mjs +66 -0
- package/lib/cics-commands.mjs +136 -125
- package/lib/compliance.mjs +6 -5
- package/lib/control.mjs +295 -6
- package/lib/dataflow.mjs +72 -88
- package/lib/diff.mjs +65 -89
- package/lib/embedded-sql.mjs +110 -0
- package/lib/equivalence.mjs +30 -12
- package/lib/evidence/journal.mjs +11 -2
- package/lib/evidence/record.mjs +3 -1
- package/lib/evidence/slsa.mjs +17 -2
- package/lib/execution.mjs +77 -0
- package/lib/explain.mjs +5 -4
- package/lib/ftp.mjs +136 -0
- package/lib/inventory.mjs +8 -9
- package/lib/ironwork.mjs +4 -6
- package/lib/jcl.mjs +29 -54
- package/lib/kernel/git.mjs +60 -0
- package/lib/kernel/identity.mjs +7 -6
- package/lib/kernel/source-tree.mjs +204 -34
- package/lib/layout.mjs +202 -0
- package/lib/parser.mjs +58 -207
- package/lib/precompile.mjs +5 -88
- package/lib/revision.json +1 -1
- package/lib/sbom.mjs +125 -13
- package/lib/scan.mjs +11 -4
- package/lib/sets/build.mjs +1 -1
- package/lib/sets/cics.mjs +32 -5
- package/lib/sets/copybook.mjs +2 -1
- package/lib/sets/hidden.mjs +4 -8
- package/lib/sets/jcl.mjs +17 -120
- package/lib/sets/priv.mjs +1 -1
- package/lib/sets/recon.mjs +3 -2
- package/lib/sets/semantics.mjs +1 -1
- package/lib/sets/vendor.mjs +1 -1
- package/lib/sets/zowe.mjs +34 -8
- package/lib/site.mjs +17 -7
- package/lib/sources.mjs +82 -38
- package/lib/utilities.mjs +146 -4
- package/package.json +1 -1
- package/rules/compliance-cobit2019.json +2438 -0
- package/rules/compliance-dora.json +1 -1
- package/rules/compliance-ffiec.json +1 -1
- package/rules/compliance-nist80053.json +1 -1
- package/schema/cobolwork.policy.schema.json +94 -17
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Reading a revision out of a repository with git plumbing only.
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
|
|
5
|
+
// A reviewed repository's .git/config can name commands; with fsmonitor off and plumbing only, git runs none.
|
|
6
|
+
const GIT_ENV = { ...process.env, GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0' };
|
|
7
|
+
export const git = (repo, args, opts = {}) => spawnSync('git', ['-c', 'core.fsmonitor=false', '-C', repo, ...args], { env: GIT_ENV, maxBuffer: 256 * 1024 * 1024, ...opts });
|
|
8
|
+
export const refused = (what, ref, why) => Object.assign(new Error(`${what} ${ref} failed: ${why}`), { code: 'EDIFFREF' });
|
|
9
|
+
const BATCH_BYTES = 64 * 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
export function treeOf(repo, ref) {
|
|
12
|
+
if (!ref || String(ref).startsWith('-')) throw refused('git rev-parse', ref, 'a revision cannot be empty or start with "-"');
|
|
13
|
+
const r = git(repo, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${ref}^{tree}`], { encoding: 'utf8' });
|
|
14
|
+
const oid = String(r.stdout || '').trim();
|
|
15
|
+
if (r.status !== 0 || !/^[0-9a-f]{40}([0-9a-f]{24})?$/.test(oid)) throw refused('git rev-parse', ref, String(r.stderr || '').trim() || 'not a revision');
|
|
16
|
+
return oid;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// The parts of a tree path, or null if a part is empty, climbs, names a drive or stream, or is .git.
|
|
20
|
+
export function treePathParts(path) {
|
|
21
|
+
const parts = path.split('/');
|
|
22
|
+
return parts.some((p) => !p || p === '.' || p === '..' || /[\\:\0]/.test(p) || p.toLowerCase() === '.git') ? null : parts;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Every file in the tree `oid` as committed, as [{ path, bytes }] in tree order: blobs as stored,
|
|
26
|
+
// with no filter or line-ending conversion. Links and submodules are left out and counted.
|
|
27
|
+
export function revisionBlobs(repo, oid, ref = oid) {
|
|
28
|
+
const listed = git(repo, ['ls-tree', '-r', '-z', '-l', '--full-tree', oid]);
|
|
29
|
+
if (listed.status !== 0) throw refused('git ls-tree', ref, String(listed.stderr || '').trim() || `exit ${listed.status}`);
|
|
30
|
+
const blobs = [];
|
|
31
|
+
let links = 0;
|
|
32
|
+
for (const entry of listed.stdout.toString('utf8').split('\0')) {
|
|
33
|
+
const m = /^(\d{6}) blob ([0-9a-f]+) +(\d+)\t(.+)$/s.exec(entry);
|
|
34
|
+
if (!m) continue;
|
|
35
|
+
if (m[1] === '120000') { links++; continue; }
|
|
36
|
+
blobs.push({ oid: m[2], size: Number(m[3]), path: m[4] });
|
|
37
|
+
}
|
|
38
|
+
// Batched by bytes as well as count, and each batch's buffer sized to hold it, so one large blob
|
|
39
|
+
// cannot overflow a buffer sized for the others.
|
|
40
|
+
const batches = [];
|
|
41
|
+
for (const b of blobs) {
|
|
42
|
+
const last = batches[batches.length - 1];
|
|
43
|
+
if (!last || last.length >= 500 || last.bytes + b.size > BATCH_BYTES) batches.push(Object.assign([b], { bytes: b.size }));
|
|
44
|
+
else { last.push(b); last.bytes += b.size; }
|
|
45
|
+
}
|
|
46
|
+
for (const batch of batches) {
|
|
47
|
+
const r = git(repo, ['cat-file', '--batch'], { input: batch.map((b) => b.oid).join('\n') + '\n', maxBuffer: batch.bytes + batch.length * 128 + 4096 });
|
|
48
|
+
if (r.status !== 0) throw refused('git cat-file', ref, String(r.stderr || '').trim() || (r.error ? r.error.code || r.error.message : `exit ${r.status}`));
|
|
49
|
+
let at = 0;
|
|
50
|
+
for (const b of batch) {
|
|
51
|
+
const nl = r.stdout.indexOf(0x0a, at);
|
|
52
|
+
const head = r.stdout.toString('utf8', at, nl).split(' ');
|
|
53
|
+
if (head[1] !== 'blob') throw refused('git cat-file', ref, `${b.oid} is ${head[1] || 'missing'}`);
|
|
54
|
+
const size = Number(head[2]);
|
|
55
|
+
b.bytes = r.stdout.subarray(nl + 1, nl + 1 + size);
|
|
56
|
+
at = nl + 1 + size + 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return { blobs, links };
|
|
60
|
+
}
|
package/lib/kernel/identity.mjs
CHANGED
|
@@ -131,14 +131,15 @@ function codeText(line, kind, format) {
|
|
|
131
131
|
return maskSecrets(s).replace(/\s+/g, ' ').trim();
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
// Reads each file once, however many findings it holds
|
|
134
|
+
// Reads each file once, however many findings it holds, from disk or through the reader the
|
|
135
|
+
// caller's source tree supplies.
|
|
135
136
|
function fileFacts(root, path, cache) {
|
|
136
137
|
let facts = cache.get(path);
|
|
137
138
|
if (facts) return facts;
|
|
138
139
|
facts = { kind: 'other', lines: [], scopes: null, format: null };
|
|
139
140
|
cache.set(path, facts);
|
|
140
141
|
let src;
|
|
141
|
-
try { src = readSource(join(root, path)).text; } catch { return facts; }
|
|
142
|
+
try { src = cache.read ? cache.read(path) : readSource(join(root, path)).text; } catch { return facts; }
|
|
142
143
|
facts.lines = src.split(/\r?\n/);
|
|
143
144
|
if (isJcl(join(root, path))) {
|
|
144
145
|
facts.kind = 'jcl';
|
|
@@ -171,8 +172,8 @@ const digest = (parts) => createHash('sha256').update(parts.join('\u0000')).dige
|
|
|
171
172
|
// Stamps `fingerprint` on every finding and returns how many shared one with an earlier finding.
|
|
172
173
|
// `repo` names the repository when one report holds several, where the same program id in two
|
|
173
174
|
// repositories is two programs.
|
|
174
|
-
export function stampFingerprints(findings, { root, repo = '' } = {}) {
|
|
175
|
-
const cache = new Map();
|
|
175
|
+
export function stampFingerprints(findings, { root, repo = '', read = null } = {}) {
|
|
176
|
+
const cache = Object.assign(new Map(), { read });
|
|
176
177
|
const seen = new Set();
|
|
177
178
|
let shared = 0;
|
|
178
179
|
for (const f of findings) {
|
|
@@ -188,8 +189,8 @@ export function stampFingerprints(findings, { root, repo = '' } = {}) {
|
|
|
188
189
|
// Looser keys for one finding seen in two trees whose line was edited between them: `scope` is the
|
|
189
190
|
// fingerprint without the line's text, and `route` names a data-flow finding by the statement its
|
|
190
191
|
// trace starts at. Null where a finding has no route.
|
|
191
|
-
export function pairingKeys(findings, { root, repo = '' } = {}) {
|
|
192
|
-
const cache = new Map();
|
|
192
|
+
export function pairingKeys(findings, { root, repo = '', read = null } = {}) {
|
|
193
|
+
const cache = Object.assign(new Map(), { read });
|
|
193
194
|
return findings.map((f) => {
|
|
194
195
|
const { scope, subject } = partsOf(f, root, cache);
|
|
195
196
|
const src = f.evidence === 'path' && f.related && f.related[0];
|
|
@@ -26,13 +26,16 @@
|
|
|
26
26
|
//
|
|
27
27
|
// An adapter that does not have a filesystem underneath it has to make that decision itself, and
|
|
28
28
|
// `contains` is where it makes it. For a tree held in memory it is key membership. For a git
|
|
29
|
-
// revision it is membership of the tree object, which cannot name a path outside itself.
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
// revision it is membership of the tree object, which cannot name a path outside itself. For a PDS
|
|
30
|
+
// export it is membership of the members the walk found, each read with no link followed. Anything
|
|
31
|
+
// cleverer than those deserves the six containment tests pointed at it before it ships:
|
|
32
|
+
// test/sources.test.mjs:44, :63, :88, :101, :144 and test/review.test.mjs:114, as
|
|
33
|
+
// test/git-tree.test.mjs and test/pds-export.test.mjs do.
|
|
34
|
+
import { closeSync, constants, fstatSync, openSync, readFileSync, readSync, realpathSync } from 'node:fs';
|
|
35
|
+
import { basename, dirname, relative, resolve, sep } from 'node:path';
|
|
34
36
|
import { buildFileIndex, parseSource } from '../parser.mjs';
|
|
35
|
-
import { readSource, relPath } from '../sources.mjs';
|
|
37
|
+
import { classifyUnder, decodeSource, diskClassifier, heldClassifier, kindOfBytes, looksEbcdic, readSource, relPath } from '../sources.mjs';
|
|
38
|
+
import { revisionBlobs, treeOf, treePathParts } from './git.mjs';
|
|
36
39
|
|
|
37
40
|
// The shape every adapter answers to. Checked rather than documented, because an adapter missing a
|
|
38
41
|
// method fails at the first rule set that happens to call it rather than at the boundary.
|
|
@@ -64,10 +67,14 @@ export function directoryTree(root, opts = {}) {
|
|
|
64
67
|
const idx = buildFileIndex(root);
|
|
65
68
|
const systemDirs = opts.systemDirs || [];
|
|
66
69
|
const top = resolve(root);
|
|
70
|
+
const kindOf = diskClassifier();
|
|
71
|
+
classifyUnder(root, kindOf);
|
|
72
|
+
if (top !== root) classifyUnder(top, kindOf);
|
|
67
73
|
|
|
68
74
|
return {
|
|
69
75
|
kind: 'directory',
|
|
70
76
|
root,
|
|
77
|
+
kindOf,
|
|
71
78
|
// The index itself, for the two callers that need more than a list: the copybook set reads
|
|
72
79
|
// copyDirs, and the inventory reports on unreadable directories and symlinks.
|
|
73
80
|
index: idx,
|
|
@@ -92,40 +99,203 @@ export function directoryTree(root, opts = {}) {
|
|
|
92
99
|
};
|
|
93
100
|
}
|
|
94
101
|
|
|
95
|
-
// A tree
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
// A tree whose files are held rather than on disk. The parser finds copybooks in the index built
|
|
103
|
+
// here and reads them through `readText`, so a COPY resolves to a held file or to a system copy
|
|
104
|
+
// library outside the tree, never to a file on disk that happens to sit under this tree's root.
|
|
105
|
+
function heldTree({ kind, root, store, rel, systemDirs = [], copyDirs = null, classify = heldClassifier,
|
|
106
|
+
symlinks = { followed: 0, outside: 0, broken: 0 }, unreadableDirs = [] }) {
|
|
107
|
+
const index = new Map();
|
|
108
|
+
const dirs = new Set(copyDirs || []);
|
|
109
|
+
for (const p of store.keys()) {
|
|
110
|
+
index.set(resolve(root, p).toLowerCase(), p);
|
|
111
|
+
if (!copyDirs && /\.(cpy|copy|inc|cbl|cob)$/i.test(p)) dirs.add(dirname(resolve(root, p)));
|
|
112
|
+
}
|
|
113
|
+
index.root = root;
|
|
114
|
+
const top = resolve(root);
|
|
115
|
+
const absent = (p) => Object.assign(new Error(`no such file in this tree: ${p}`), { code: 'ENOENT' });
|
|
116
|
+
const bytes = (p) => {
|
|
117
|
+
const b = store.get(p);
|
|
118
|
+
if (!b) throw absent(p);
|
|
119
|
+
return b;
|
|
120
|
+
};
|
|
121
|
+
const kindOf = classify(bytes);
|
|
122
|
+
classifyUnder(top, kindOf);
|
|
123
|
+
const text = (p) => decodeSource(bytes(p));
|
|
124
|
+
const readText = (p) => {
|
|
125
|
+
if (store.has(p)) return text(p).text;
|
|
126
|
+
const r = resolve(root, p);
|
|
127
|
+
if (r === top || r.startsWith(top + sep)) throw absent(p);
|
|
128
|
+
return readSource(p).text;
|
|
129
|
+
};
|
|
104
130
|
return {
|
|
105
|
-
kind
|
|
131
|
+
kind,
|
|
106
132
|
root,
|
|
107
|
-
|
|
133
|
+
kindOf,
|
|
134
|
+
index: { index, copyDirs: [...dirs].sort(), unreadableDirs, symlinks },
|
|
108
135
|
list: () => [...store.keys()].sort(),
|
|
109
|
-
bytes
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return b;
|
|
113
|
-
},
|
|
114
|
-
text: (p) => {
|
|
115
|
-
const b = store.get(p);
|
|
116
|
-
if (!b) throw Object.assign(new Error(`no such file in this tree: ${p}`), { code: 'ENOENT' });
|
|
117
|
-
return { text: b.toString('latin1'), encoding: 'latin1' };
|
|
118
|
-
},
|
|
119
|
-
rel: (p) => String(p).replace(/\\/g, '/').replace(new RegExp('^' + root.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '/?'), ''),
|
|
136
|
+
bytes,
|
|
137
|
+
text,
|
|
138
|
+
rel,
|
|
120
139
|
// Containment by construction: a path this tree does not hold is a path outside it.
|
|
121
140
|
contains: (p) => store.has(p),
|
|
122
|
-
parse: () => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
)
|
|
127
|
-
|
|
141
|
+
parse: (file, t) => parseSource(t ?? text(file).text, file, {
|
|
142
|
+
format: 'auto',
|
|
143
|
+
includeDirs: [...dirs].sort(),
|
|
144
|
+
fileIndex: index,
|
|
145
|
+
mainDir: dirname(resolve(root, file)),
|
|
146
|
+
copyFormat: 'auto',
|
|
147
|
+
systemDirs,
|
|
148
|
+
readText,
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// A tree that was never on disk, for tests. `files` maps a path to its contents, as a string or a
|
|
154
|
+
// Buffer. Paths are used exactly as given, so a test reads the way it writes.
|
|
155
|
+
export function memoryTree(files, { root = '/memory', systemDirs = [] } = {}) {
|
|
156
|
+
const store = new Map(Object.entries(files).map(([p, v]) => [p, Buffer.isBuffer(v) ? v : Buffer.from(v, 'latin1')]));
|
|
157
|
+
const rel = (p) => String(p).replace(/\\/g, '/').replace(new RegExp('^' + root.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '/?'), '');
|
|
158
|
+
return heldTree({ kind: 'memory', root, store, rel, systemDirs });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// A git revision, read with plumbing into memory: nothing is written to disk. Its root is a path
|
|
162
|
+
// beside the repository that does not exist, `<repo>@<tree>`, so every path it answers for is
|
|
163
|
+
// absolute and none of them can be found on disk. The tree object is the containment: it cannot
|
|
164
|
+
// name a path outside itself, and a path that would climb, name a drive or stream, or enter .git
|
|
165
|
+
// is left out, as it is when a revision is written to disk. Links and submodules are not read.
|
|
166
|
+
export function gitTree(repo, ref, opts = {}) {
|
|
167
|
+
const oid = treeOf(repo, ref);
|
|
168
|
+
const { blobs, links } = revisionBlobs(repo, oid, ref);
|
|
169
|
+
const root = `${resolve(repo)}@${oid.slice(0, 12)}`;
|
|
170
|
+
const store = new Map();
|
|
171
|
+
for (const b of blobs) {
|
|
172
|
+
const parts = treePathParts(b.path);
|
|
173
|
+
if (parts) store.set(resolve(root, ...parts), b.bytes);
|
|
174
|
+
}
|
|
175
|
+
const tree = heldTree({ kind: 'git', root, store, rel: (p) => relPath(root, p), systemDirs: opts.systemDirs || [],
|
|
176
|
+
symlinks: { followed: 0, outside: 0, broken: 0, notRead: links } });
|
|
177
|
+
return Object.assign(tree, { ref, oid });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// z/OSMF's record mode (zowe --record) writes each record after its length, four bytes big-endian,
|
|
181
|
+
// and nothing between records. Read as that only when the lengths chain exactly to the end of the
|
|
182
|
+
// file, and given back as lines; anything else is the file as it is.
|
|
183
|
+
const MAX_LRECL = 32760;
|
|
184
|
+
export function unframeRecords(buf) {
|
|
185
|
+
let records = 0;
|
|
186
|
+
for (let at = 0; at < buf.length; records++) {
|
|
187
|
+
if (buf.length - at < 4) return buf;
|
|
188
|
+
const n = buf.readUInt32BE(at);
|
|
189
|
+
if (n > MAX_LRECL || buf.length - at - 4 < n) return buf;
|
|
190
|
+
at += 4 + n;
|
|
191
|
+
}
|
|
192
|
+
if (!records || records * 4 === buf.length) return buf;
|
|
193
|
+
const lines = (end) => {
|
|
194
|
+
const out = Buffer.alloc(buf.length - records * 3);
|
|
195
|
+
let w = 0;
|
|
196
|
+
for (let at = 0; at < buf.length;) {
|
|
197
|
+
const n = buf.readUInt32BE(at);
|
|
198
|
+
w += buf.copy(out, w, at + 4, at + 4 + n);
|
|
199
|
+
out[w++] = end;
|
|
200
|
+
at += 4 + n;
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
128
203
|
};
|
|
204
|
+
const ebcdic = lines(0x15);
|
|
205
|
+
return looksEbcdic(ebcdic) ? ebcdic : lines(0x0A);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A member is read from the file the walk found. A link put in its place since, or a file that is
|
|
209
|
+
// not a regular one, is refused rather than followed or waited on.
|
|
210
|
+
function readMember(file) {
|
|
211
|
+
const fd = openSync(file, constants.O_RDONLY | (constants.O_NOFOLLOW || 0) | (constants.O_NONBLOCK || 0));
|
|
212
|
+
try {
|
|
213
|
+
const st = fstatSync(fd);
|
|
214
|
+
if (!st.isFile()) throw Object.assign(new Error(`not a regular file: ${file}`), { code: 'ENOTFILE' });
|
|
215
|
+
const buf = Buffer.alloc(st.size);
|
|
216
|
+
let n = 0;
|
|
217
|
+
while (n < st.size) {
|
|
218
|
+
const r = readSync(fd, buf, n, st.size - n, n);
|
|
219
|
+
if (!r) break;
|
|
220
|
+
n += r;
|
|
221
|
+
}
|
|
222
|
+
return unframeRecords(buf.subarray(0, n));
|
|
223
|
+
} finally { closeSync(fd); }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const QUALIFIER = /^[A-Z@#$][A-Z0-9@#$-]{0,7}$/;
|
|
227
|
+
const MEMBER = /^([A-Z@#$][A-Z0-9@#$]{0,7})(?:\.[^.]*)?$/i;
|
|
228
|
+
const MAX_LISTED = 8;
|
|
229
|
+
|
|
230
|
+
// Partitioned data sets exported to a directory, a member to a file: what
|
|
231
|
+
// `zowe zos-files download all-members` writes (ibmuser/new/cntl/member.txt, lower-cased unless
|
|
232
|
+
// --preserve-original-letter-case, with whatever extension -e gave), or a directory named for the
|
|
233
|
+
// data set with its members copied in. Each member is held as `DATA.SET.NAME/MEMBER` under a root
|
|
234
|
+
// beside the export that does not exist, so a finding names the member as z/OS does however it was
|
|
235
|
+
// downloaded, and nothing under that root is ever read from disk.
|
|
236
|
+
//
|
|
237
|
+
// The extension is the download's choice, not the language's, so members are classified by their
|
|
238
|
+
// contents. A file whose directory is not a data set name or whose name is not a member name is not
|
|
239
|
+
// read and is counted, as is a second file for a member already held. Every data set that holds
|
|
240
|
+
// anything but JCL and maps is a copy library, searched in name order: the export does not say
|
|
241
|
+
// what SYSLIB concatenated.
|
|
242
|
+
export function pdsExportTree(dir, opts = {}) {
|
|
243
|
+
const idx = buildFileIndex(dir);
|
|
244
|
+
const top = realpathSync(dir);
|
|
245
|
+
const root = `${resolve(dir)}@pds`;
|
|
246
|
+
const origins = new Map();
|
|
247
|
+
const found = new Map();
|
|
248
|
+
const notMembers = [];
|
|
249
|
+
const duplicates = [];
|
|
250
|
+
const dataSets = new Set();
|
|
251
|
+
for (const file of [...idx.index.values()].sort()) {
|
|
252
|
+
const where = relative(dir, dirname(file)).split(sep).filter(Boolean);
|
|
253
|
+
const dsn = where.join('.').toUpperCase();
|
|
254
|
+
const member = MEMBER.exec(basename(file));
|
|
255
|
+
if (!member || dsn.length > 44 || (dsn && !dsn.split('.').every((q) => QUALIFIER.test(q)))) {
|
|
256
|
+
notMembers.push(relPath(dir, file));
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const at = resolve(root, ...(dsn ? [dsn] : []), member[1].toUpperCase());
|
|
260
|
+
if (found.has(at)) { duplicates.push(`${relPath(dir, file)}: ${relPath(root, at)} is ${found.get(at)}`); continue; }
|
|
261
|
+
let real;
|
|
262
|
+
try { real = realpathSync(file); } catch { real = file; }
|
|
263
|
+
if (real !== top && !real.startsWith(top + sep)) { notMembers.push(relPath(dir, file)); continue; }
|
|
264
|
+
origins.set(at, real);
|
|
265
|
+
found.set(at, relPath(dir, file));
|
|
266
|
+
dataSets.add(dirname(at));
|
|
267
|
+
}
|
|
268
|
+
const kinds = new Map();
|
|
269
|
+
const byKind = {};
|
|
270
|
+
const copyDirs = new Set();
|
|
271
|
+
for (const [at, file] of origins) {
|
|
272
|
+
let kind = null;
|
|
273
|
+
try { kind = kindOfBytes(readMember(file)); } catch { kind = null; }
|
|
274
|
+
kinds.set(at, kind);
|
|
275
|
+
byKind[kind || 'other'] = (byKind[kind || 'other'] || 0) + 1;
|
|
276
|
+
if (kind !== 'jcl' && kind !== 'bms') copyDirs.add(dirname(at));
|
|
277
|
+
}
|
|
278
|
+
const store = {
|
|
279
|
+
keys: () => origins.keys(),
|
|
280
|
+
has: (p) => origins.has(p),
|
|
281
|
+
get: (p) => (origins.has(p) ? readMember(origins.get(p)) : undefined),
|
|
282
|
+
};
|
|
283
|
+
const tree = heldTree({
|
|
284
|
+
kind: 'pds-export', root, store, rel: (p) => relPath(root, p), systemDirs: opts.systemDirs || [],
|
|
285
|
+
copyDirs: [...copyDirs], classify: () => (p) => kinds.get(p) ?? null, symlinks: idx.symlinks,
|
|
286
|
+
unreadableDirs: idx.unreadableDirs.map((d) => resolve(root, relative(dir, d))),
|
|
287
|
+
});
|
|
288
|
+
return Object.assign(tree, {
|
|
289
|
+
dir,
|
|
290
|
+
origin: (p) => found.get(p) ?? null,
|
|
291
|
+
export: {
|
|
292
|
+
dataSets: dataSets.size,
|
|
293
|
+
members: origins.size,
|
|
294
|
+
byKind,
|
|
295
|
+
...(notMembers.length ? { notMembers: notMembers.length, notMembersListed: notMembers.slice(0, MAX_LISTED) } : {}),
|
|
296
|
+
...(duplicates.length ? { duplicates: duplicates.length, duplicatesListed: duplicates.slice(0, MAX_LISTED) } : {}),
|
|
297
|
+
},
|
|
298
|
+
});
|
|
129
299
|
}
|
|
130
300
|
|
|
131
301
|
// What a rule set calls. Given a tree it uses it; given none it builds the directory one, so every
|
package/lib/layout.mjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// How a data description is laid out in storage: the bytes a PICTURE and USAGE take, where each item
|
|
3
|
+
// starts within its record, and how wide a report group prints. Items are { level, occurs, redefines,
|
|
4
|
+
// sync, children, ... } as lib/parser.mjs builds them.
|
|
5
|
+
|
|
6
|
+
// The binary sizes a dialect gives COMP by digit count.
|
|
7
|
+
export const BINARY_SIZE = { default: '1-2-4-8', ibm: '2-4-8', mf: '1--8' };
|
|
8
|
+
|
|
9
|
+
function picInfo(pic, constants) {
|
|
10
|
+
const info = { digits: 0, display: 0, signed: false, alphanumeric: false, national: false };
|
|
11
|
+
const re = /(.)\(([A-Za-z0-9_-]+)\)|(.)/g;
|
|
12
|
+
let m;
|
|
13
|
+
while ((m = re.exec(pic))) {
|
|
14
|
+
const ch = (m[1] || m[3]).toUpperCase();
|
|
15
|
+
let n = 1;
|
|
16
|
+
if (m[1]) {
|
|
17
|
+
const raw = m[2];
|
|
18
|
+
n = /^\d+$/.test(raw) ? Number(raw) : Number(constants && constants.get(raw.toUpperCase()));
|
|
19
|
+
if (!Number.isFinite(n) || n < 0) n = 0;
|
|
20
|
+
}
|
|
21
|
+
if (ch === 'S') { info.signed = true; continue; }
|
|
22
|
+
if (ch === 'V' || ch === 'P') continue;
|
|
23
|
+
if (ch === '9') { info.digits += n; info.display += n; continue; }
|
|
24
|
+
if (ch === 'N' || ch === 'G') { info.national = true; info.display += 2 * n; continue; }
|
|
25
|
+
if (ch === 'X' || ch === 'A') info.alphanumeric = true;
|
|
26
|
+
info.display += n;
|
|
27
|
+
}
|
|
28
|
+
return info;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Storage bytes of a literal: hexadecimal literals hold one byte per two digits, a Z literal adds a
|
|
32
|
+
// terminating null, a national literal holds two bytes per character.
|
|
33
|
+
export function literalBytes(tok) {
|
|
34
|
+
const prefix = tok.prefix || '';
|
|
35
|
+
if (prefix === 'X' || prefix === 'BX' || prefix === 'NX') return Math.floor(tok.v.length / (prefix === 'NX' ? 4 : 2)) * (prefix === 'NX' ? 2 : 1);
|
|
36
|
+
if (prefix === 'Z') return tok.v.length + 1;
|
|
37
|
+
if (prefix === 'N' || prefix === 'NC' || prefix === 'U') return tok.v.length * 2;
|
|
38
|
+
return tok.v.length;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function binaryBytes(digits, scheme) {
|
|
42
|
+
if (scheme === '2-4-8') return digits <= 4 ? 2 : digits <= 9 ? 4 : 8;
|
|
43
|
+
if (scheme === '1--8') return [1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8][Math.min(digits, 18)] || 8;
|
|
44
|
+
return digits <= 2 ? 1 : digits <= 4 ? 2 : digits <= 9 ? 4 : 8;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function elementarySize(item, scheme, constants) {
|
|
48
|
+
if (!item.picture && !item.usage) {
|
|
49
|
+
const constBytes = constants && constants.textBytes;
|
|
50
|
+
const fromValue = item.values.reduce((n, v) => n + (v.t === 'lit' ? literalBytes(v) : v.t === 'word' && constBytes && constBytes.has(v.u) ? constBytes.get(v.u) : 0), 0);
|
|
51
|
+
if (item.section === 'SCREEN' && !fromValue && item.screenRefItem) return item.screenRefItem.size || 0;
|
|
52
|
+
if (item.section === 'SCREEN') return Math.max(1, fromValue);
|
|
53
|
+
if (fromValue) return fromValue;
|
|
54
|
+
}
|
|
55
|
+
if (item.section === 'SCREEN' && !item.picture) return 1;
|
|
56
|
+
const usage = item.effectiveUsage || 'DISPLAY';
|
|
57
|
+
const p = item.picture ? picInfo(item.picture, constants) : null;
|
|
58
|
+
const digits = p ? p.digits : 0;
|
|
59
|
+
const u = usage.replace('COMPUTATIONAL', 'COMP');
|
|
60
|
+
if (u === 'COMP-1' || u === 'FLOAT-SHORT') return 4;
|
|
61
|
+
if (u === 'COMP-2' || u === 'FLOAT-LONG' || u === 'FLOAT-DECIMAL-16') return 8;
|
|
62
|
+
if (u === 'FLOAT-DECIMAL-34') return 16;
|
|
63
|
+
if (u === 'INDEX') return 4;
|
|
64
|
+
if (u === 'POINTER' || u === 'PROGRAM-POINTER' || u === 'FUNCTION-POINTER' || u === 'PROCEDURE-POINTER') return 8;
|
|
65
|
+
if (u === 'BINARY-CHAR') return 1;
|
|
66
|
+
if (u === 'BINARY-SHORT' || u === 'SIGNED-SHORT' || u === 'UNSIGNED-SHORT') return 2;
|
|
67
|
+
if (u === 'BINARY-LONG' || u === 'BINARY-INT' || u === 'SIGNED-INT' || u === 'UNSIGNED-INT') return 4;
|
|
68
|
+
if (u === 'BINARY-DOUBLE' || u === 'BINARY-LONG-LONG' || u === 'BINARY-C-LONG' || u === 'SIGNED-LONG' || u === 'UNSIGNED-LONG') return 8;
|
|
69
|
+
if (u === 'COMP-3' || u === 'PACKED-DECIMAL') return Math.floor(digits / 2) + 1;
|
|
70
|
+
if (u === 'COMP-6') return Math.ceil(digits / 2);
|
|
71
|
+
if (u === 'COMP-X' || u === 'COMP-N' || (u === 'COMP-5' && p && p.alphanumeric)) {
|
|
72
|
+
if (p && p.alphanumeric) return p.display;
|
|
73
|
+
return Math.max(1, Math.ceil((digits * Math.log(10)) / Math.log(256)));
|
|
74
|
+
}
|
|
75
|
+
if (u === 'COMP' || u === 'COMP-4' || u === 'COMP-5' || u === 'BINARY') return binaryBytes(digits, scheme);
|
|
76
|
+
let len = p ? p.display : 0;
|
|
77
|
+
if (item.signSeparate && p && p.signed) len++;
|
|
78
|
+
return len;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// SYNCHRONIZED aligns a binary, floating-point, pointer or index item to its own length; packed,
|
|
82
|
+
// COMP-X and display items are not moved.
|
|
83
|
+
const ALIGNED_USAGE = /^(COMP|COMP-[1245]|BINARY(-[A-Z-]+)?|FLOAT-[A-Z0-9-]+|(PROGRAM-|FUNCTION-|PROCEDURE-)?POINTER|INDEX|(UN)?SIGNED-[A-Z]+)$/;
|
|
84
|
+
|
|
85
|
+
export function computeSizes(roots, scheme, constants) {
|
|
86
|
+
// `at` is where the item starts, counted from the start of its record, because the compiler aligns
|
|
87
|
+
// a SYNCHRONIZED item against the record and puts the slack inside the group that holds it. A
|
|
88
|
+
// table's entry is laid out from its own start and rounded up to its widest alignment, so every
|
|
89
|
+
// occurrence aligns alike.
|
|
90
|
+
const visit = (item, inheritedUsage, inheritedSignSeparate, at) => {
|
|
91
|
+
item.effectiveUsage = item.usage || inheritedUsage || null;
|
|
92
|
+
if (inheritedSignSeparate && !item.signExplicit) item.signSeparate = true;
|
|
93
|
+
const structural = item.children.filter(c => c.level !== 88 && c.level !== 66 && c.level !== 78);
|
|
94
|
+
if (!structural.length) {
|
|
95
|
+
const one = elementarySize(item, scheme, constants);
|
|
96
|
+
item.contributes = one * item.occurs;
|
|
97
|
+
// The listing prints the whole table only for POINTER and INDEX; every other usage prints one occurrence.
|
|
98
|
+
const wholeTable = /^(POINTER|INDEX)$/.test(item.effectiveUsage || '');
|
|
99
|
+
item.size = wholeTable ? item.contributes : one;
|
|
100
|
+
const usage = (item.effectiveUsage || '').replace('COMPUTATIONAL', 'COMP');
|
|
101
|
+
item.align = item.sync && ALIGNED_USAGE.test(usage) ? Math.min(one, 8) : 1;
|
|
102
|
+
item.maxAlign = item.align;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const base = item.occurs > 1 ? 0 : at;
|
|
106
|
+
let offset = 0;
|
|
107
|
+
let end = 0;
|
|
108
|
+
let maxAlign = 1;
|
|
109
|
+
const startOf = new Map();
|
|
110
|
+
for (const c of structural) {
|
|
111
|
+
if (c.redefines) {
|
|
112
|
+
// REDEFINES names a SIBLING. Resolving it by name across the whole program reached the
|
|
113
|
+
// first item of that name anywhere, which crossed records.
|
|
114
|
+
const known = startOf.has(c.redefines);
|
|
115
|
+
visit(c, item.effectiveUsage, item.signSeparate, base + (known ? startOf.get(c.redefines) : offset));
|
|
116
|
+
const start = known ? startOf.get(c.redefines) : offset - c.contributes;
|
|
117
|
+
startOf.set(c.name, start);
|
|
118
|
+
c.localStart = start;
|
|
119
|
+
end = Math.max(end, start + c.contributes);
|
|
120
|
+
// A REDEFINES larger than the item it redefines pushes the next sibling past its end.
|
|
121
|
+
offset = Math.max(offset, start + c.contributes);
|
|
122
|
+
maxAlign = Math.max(maxAlign, c.maxAlign);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
visit(c, item.effectiveUsage, item.signSeparate, base + offset);
|
|
126
|
+
const skew = (base + offset) % c.align;
|
|
127
|
+
if (c.align > 1 && skew) offset += c.align - skew;
|
|
128
|
+
startOf.set(c.name, offset);
|
|
129
|
+
c.localStart = offset;
|
|
130
|
+
offset += c.contributes;
|
|
131
|
+
end = Math.max(end, offset);
|
|
132
|
+
maxAlign = Math.max(maxAlign, c.maxAlign);
|
|
133
|
+
}
|
|
134
|
+
if (item.occurs > 1 && end % maxAlign) end += maxAlign - (end % maxAlign);
|
|
135
|
+
item.size = end * item.occurs;
|
|
136
|
+
item.contributes = item.size;
|
|
137
|
+
item.align = 1;
|
|
138
|
+
item.maxAlign = maxAlign;
|
|
139
|
+
};
|
|
140
|
+
for (const r of roots) visit(r, null, false, 0);
|
|
141
|
+
// Offsets are assigned after sizing: a child's absolute start depends on its parent's, which is
|
|
142
|
+
// only known once the parent's own siblings have been laid out.
|
|
143
|
+
const place = (item, at) => {
|
|
144
|
+
item.offset = at;
|
|
145
|
+
for (const c of item.children) if (c.localStart != null) place(c, at + c.localStart);
|
|
146
|
+
};
|
|
147
|
+
for (const r of roots) place(r, 0);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// A report group is laid out by column, not by adding up its items: a line is as wide as the column
|
|
151
|
+
// its rightmost item ends in, COLUMN PLUS counting on from the end of the item before. A group holds
|
|
152
|
+
// its lines one after another, and every 01 group of a report, like the file the report is written
|
|
153
|
+
// to, is as large as the largest group.
|
|
154
|
+
export function layoutReport(rd) {
|
|
155
|
+
const structural = (x) => x.children.filter(c => c.level !== 88 && c.level !== 66 && c.level !== 78);
|
|
156
|
+
const mark = (x) => { x.rd = rd; for (const c of x.children) mark(c); };
|
|
157
|
+
for (const g of rd.groups) mark(g);
|
|
158
|
+
let width = 0;
|
|
159
|
+
// Items printed at the same column under PRESENT WHEN each keep their own storage, so a line is
|
|
160
|
+
// never smaller than its items laid end to end.
|
|
161
|
+
const lineWidth = (line) => {
|
|
162
|
+
let last = 0;
|
|
163
|
+
let right = 0;
|
|
164
|
+
let total = 0;
|
|
165
|
+
const place = (c) => {
|
|
166
|
+
const len = c.contributes || c.size || 0;
|
|
167
|
+
const start = c.rwColumn ? (c.rwColumn.at != null ? c.rwColumn.at : last + c.rwColumn.plus) : last + 1;
|
|
168
|
+
last = start + len - 1;
|
|
169
|
+
right = Math.max(right, last);
|
|
170
|
+
total += len;
|
|
171
|
+
};
|
|
172
|
+
const walk = (x) => { for (const c of structural(x)) { if (structural(c).length) walk(c); else place(c); } };
|
|
173
|
+
if (structural(line).length) walk(line);
|
|
174
|
+
else place(line);
|
|
175
|
+
return Math.max(right, total);
|
|
176
|
+
};
|
|
177
|
+
// The entries that open a line; a group with no LINE clause anywhere in it is one line.
|
|
178
|
+
for (const g of rd.groups) {
|
|
179
|
+
const lines = [];
|
|
180
|
+
const collect = (x) => { if (x.rwLine) { lines.push(x); return; } for (const c of structural(x)) collect(c); };
|
|
181
|
+
collect(g);
|
|
182
|
+
if (!lines.length) lines.push(g);
|
|
183
|
+
let total = 0;
|
|
184
|
+
for (const line of lines) {
|
|
185
|
+
const w = lineWidth(line);
|
|
186
|
+
if (line !== g) { line.size = w; line.contributes = w * (line.occurs || 1); }
|
|
187
|
+
total += w;
|
|
188
|
+
}
|
|
189
|
+
width = Math.max(width, total);
|
|
190
|
+
}
|
|
191
|
+
for (const g of rd.groups) { g.size = width; g.contributes = width; }
|
|
192
|
+
rd.width = width;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Whether an item's bytes are read as decimal digits: zoned (DISPLAY with a numeric picture) or
|
|
196
|
+
// packed. A group, an edited picture, binary and floating point are not.
|
|
197
|
+
export function holdsDecimal(it) {
|
|
198
|
+
if ((it.children || []).some((c) => c.level !== 88)) return false;
|
|
199
|
+
const usage = String(it.effectiveUsage || 'DISPLAY').replace('COMPUTATIONAL', 'COMP');
|
|
200
|
+
if (usage === 'COMP-3' || usage === 'PACKED-DECIMAL' || usage === 'COMP-6') return true;
|
|
201
|
+
return usage === 'DISPLAY' && !!it.picture && /9/.test(it.picture) && /^[S9VP()0-9]+$/i.test(it.picture);
|
|
202
|
+
}
|