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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
#!/usr/bin/env bun
import { closeSync, openSync, readSync } from "node:fs";
import { stat } from "node:fs/promises";
import { createFdRecordReader, displayWidth, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathDisplayName, readAll, shellEscapeLsName, splitFiles0ByteNames, systemErrorMessage, wcFileNameIsDash, wcFileNameIsEmpty, wcFiles0SourceIsNonRegular } from "../shared/common.js";
import { UsageError, stderr, stdout } from "../shared/diagnostics.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const UINTMAX_MAX = 18446744073709551615n;
export const WC_LONG_OPTIONS = ["bytes", "chars", "lines", "max-line-length", "words", "files0-from", "total", "debug", "help", "version"];
export function wcMetaOption(args) {
const longValueOptions = new Set(["files0-from", "total"]);
const shortKnownOptions = new Set(["c", "m", "l", "L", "w"]);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") return null;
if (arg.startsWith("--")) {
const option = normalizeWcLongOption(arg);
const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2);
if (!WC_LONG_OPTIONS.includes(name)) return null;
if ((option === "--help" || option === "--version") && inlineValue == null) return option;
if (name === "total" && inlineValue != null) validateWcTotalMode(inlineValue);
if (inlineValue == null && longValueOptions.has(name)) i++;
continue;
}
if (!arg.startsWith("-") || arg === "-") continue;
for (let j = 1; j < arg.length; j++) if (!shortKnownOptions.has(arg[j])) return null;
}
return null;
}
export async function wc(args) {
args = normalizeWcLongOptions(args);
const parsed = parseOptions(args, {
short: { c: false, m: false, l: false, L: false, w: false },
long: { bytes: false, chars: false, lines: false, "max-line-length": false, words: false, "files0-from": "value", total: "value", debug: false, help: false, version: false },
});
const flags = parsed.opts;
const showAll = !(flags.c || flags.m || flags.l || flags.L || flags.w || flags.bytes || flags.chars || flags.lines || flags["max-line-length"] || flags.words);
const totalMode = flags.total ?? "auto";
validateWcTotalMode(totalMode);
let files = parsed.operands.length ? parsed.operands : ["-"];
let files0From = null;
let files0EntryCount = null;
if (flags["files0-from"] !== undefined) {
if (parsed.operands.length) throw new UsageError(`extra operand '${parsed.operands[0]}'\nfile operands cannot be combined with --files0-from`, true);
files0From = flags["files0-from"];
if (wcFiles0SourceIsNonRegular(files0From)) {
return wcStreamFiles0(files0From, flags, showAll, totalMode);
}
let nameBytes;
try {
nameBytes = await readAll(files0From);
} catch (error) {
stderr(error?.code === "EISDIR"
? `wc: ${wcFiles0DiagnosticName(files0From)}: read error: ${systemErrorMessage(error)}\n`
: `wc: cannot open ${wcFiles0OpenName(files0From)} for reading: ${systemErrorMessage(error)}\n`);
return 1;
}
files = splitFiles0ByteNames(nameBytes);
files0EntryCount = files.length;
if (files0From === "-" && files.some(wcFileNameIsDash)) {
stderr("wc: when reading file names from standard input, no file name of '-' allowed\n");
return 1;
}
}
const rows = [];
const total = { lines: 0, words: 0, bytes: 0n, chars: 0, maxLineLength: 0 };
const bytesOnly = (flags.c || flags.bytes) && !(flags.m || flags.chars || flags.l || flags.lines || flags.L || flags["max-line-length"] || flags.w || flags.words);
let failed = false;
let totalByteOverflow = false;
for (let index = 0; index < files.length; index++) {
const file = files[index];
if (files0From != null && wcFileNameIsEmpty(file)) {
stderr(`wc: ${wcFiles0DiagnosticName(files0From)}:${index + 1}: invalid zero-length file name\n`);
failed = true;
continue;
}
let bytes;
try {
if (bytesOnly && file === "-") {
const size = wcCountFdBytes(0);
const name = parsed.operands.length === 0 && flags["files0-from"] === undefined ? "" : file;
const row = { lines: 0, words: 0, bytes: size, chars: 0, maxLineLength: 0, name };
if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true;
total.bytes = addWcByteTotal(total.bytes, row.bytes);
rows.push(row);
continue;
}
if (bytesOnly && file !== "-") {
const info = await stat(file, { bigint: true });
if (info.isDirectory()) throw Object.assign(new Error("Is a directory"), { code: "EISDIR" });
const size = info.size;
bytes = null;
const row = { lines: 0, words: 0, bytes: size, chars: 0, maxLineLength: 0, name: file, specialFile: wcStatIsSpecial(info) };
if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true;
total.bytes = addWcByteTotal(total.bytes, row.bytes);
rows.push(row);
continue;
}
bytes = await readAll(file);
} catch (error) {
if (error?.code === "EISDIR") {
stderr(`wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`);
rows.push({ lines: 0, words: 0, bytes: 0, chars: 0, maxLineLength: 0, name: file, directoryError: true });
} else {
stderr(file === "-" ? `wc: ${nodeErrorMessage(error)}\n` : `wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`);
}
failed = true;
continue;
}
const row = wcRow(bytes, file === "-" && parsed.operands.length === 0 && flags["files0-from"] === undefined ? "" : file);
if (file !== "-") {
const info = await stat(file).catch(() => null);
if (info && wcStatIsSpecial(info)) row.specialFile = true;
}
total.lines += row.lines;
total.words += row.words;
if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true;
total.bytes = addWcByteTotal(total.bytes, row.bytes);
total.chars += row.chars;
total.maxLineLength = Math.max(total.maxLineLength, row.maxLineLength);
rows.push(row);
}
const totalInputCount = files0EntryCount ?? files.length;
const includeTotal = totalMode === "always" || totalMode === "only" || (totalMode === "auto" && totalInputCount > 1);
if (includeTotal && totalByteOverflow) failed = true;
const outputRows = totalMode === "only" ? [] : [...rows];
if (includeTotal) outputRows.push({ ...total, name: totalMode === "only" ? "" : "total" });
const selectedValues = (row) => {
const values = [];
if (showAll || flags.l || flags.lines) values.push(row.lines);
if (showAll || flags.w || flags.words) values.push(row.words);
if (flags.m || flags.chars) values.push(row.chars);
if (showAll || flags.c || flags.bytes) values.push(row.bytes);
if (flags.L || flags["max-line-length"]) values.push(row.maxLineLength);
return values;
};
const widthValues = (row) => showAll ? selectedValues(row) : [row.lines, row.words, row.chars, row.bytes, row.maxLineLength];
const hasNamedOutput = outputRows.some((row) => row.name);
const widthRows = [...outputRows];
if (hasNamedOutput && totalInputCount > 1 && totalMode === "never") widthRows.push(total);
const minCountWidth = showAll && outputRows.some((row) => row.directoryError || row.specialFile) ? 7 : 1;
const countWidth = hasNamedOutput
? Math.max(minCountWidth, ...widthRows.flatMap(widthValues).map((n) => String(n).length))
: 7;
for (const row of outputRows) {
const cols = selectedValues(row);
const unpadded = totalMode === "only" || (!row.name && cols.length === 1) || (files0From != null && cols.length === 1);
stdout(`${cols.map((n) => unpadded ? String(n) : String(n).padStart(countWidth)).join(" ")}${row.name ? ` ${wcDisplayName(row.name)}` : ""}\n`);
}
return failed ? 1 : 0;
}
export function wcCountFdBytes(fd) {
const buffer = Buffer.allocUnsafe(64 * 1024);
let total = 0n;
while (true) {
const bytesRead = readSync(fd, buffer, 0, buffer.length, null);
if (bytesRead === 0) return total;
total += BigInt(bytesRead);
}
}
export async function wcStreamFiles0(source, flags, showAll, totalMode) {
let fd = 0;
let closeFd = false;
try {
if (source !== "-") {
fd = openSync(source, "r");
closeFd = true;
}
} catch (error) {
stderr(`wc: cannot open ${wcFiles0OpenName(source)} for reading: ${systemErrorMessage(error)}\n`);
return 1;
}
const reader = createFdRecordReader(fd, 0);
const total = { lines: 0, words: 0, bytes: 0n, chars: 0, maxLineLength: 0 };
const bytesOnly = (flags.c || flags.bytes) && !(flags.m || flags.chars || flags.l || flags.lines || flags.L || flags["max-line-length"] || flags.w || flags.words);
const selectedValues = (row) => {
const values = [];
if (showAll || flags.l || flags.lines) values.push(row.lines);
if (showAll || flags.w || flags.words) values.push(row.words);
if (flags.m || flags.chars) values.push(row.chars);
if (showAll || flags.c || flags.bytes) values.push(row.bytes);
if (flags.L || flags["max-line-length"]) values.push(row.maxLineLength);
return values;
};
let failed = false;
let totalByteOverflow = false;
let entryCount = 0;
try {
while (true) {
const file = reader.next();
if (file == null) break;
entryCount++;
if (wcFileNameIsEmpty(file)) {
stderr(`wc: ${wcFiles0DiagnosticName(source)}:${entryCount}: invalid zero-length file name\n`);
failed = true;
continue;
}
if (source === "-" && wcFileNameIsDash(file)) {
stderr("wc: when reading file names from standard input, no file name of '-' allowed\n");
failed = true;
continue;
}
let row;
try {
if (bytesOnly) {
const info = await stat(file, { bigint: true });
if (info.isDirectory()) throw Object.assign(new Error("Is a directory"), { code: "EISDIR" });
row = { lines: 0, words: 0, bytes: info.size, chars: 0, maxLineLength: 0, name: file, specialFile: wcStatIsSpecial(info) };
} else {
const bytes = await readAll(file);
row = wcRow(bytes, file);
const info = await stat(file).catch(() => null);
if (info && wcStatIsSpecial(info)) row.specialFile = true;
}
} catch (error) {
if (error?.code === "EISDIR") {
stderr(`wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`);
row = { lines: 0, words: 0, bytes: 0, chars: 0, maxLineLength: 0, name: file, directoryError: true };
} else {
stderr(`wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`);
failed = true;
continue;
}
failed = true;
}
total.lines += row.lines;
total.words += row.words;
if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true;
total.bytes = addWcByteTotal(total.bytes, row.bytes);
total.chars += row.chars;
total.maxLineLength = Math.max(total.maxLineLength, row.maxLineLength);
if (totalMode !== "only") {
const cols = selectedValues(row);
stdout(`${cols.map(String).join(" ")} ${wcDisplayName(row.name)}\n`);
}
}
} catch (error) {
if (isWriteError(error)) throw error;
stderr(`wc: ${wcFiles0DiagnosticName(source)}: read error: ${systemErrorMessage(error)}\n`);
failed = true;
} finally {
if (closeFd) closeSync(fd);
}
const includeTotal = totalMode === "always" || totalMode === "only" || (totalMode === "auto" && entryCount > 1);
if (includeTotal) {
if (totalByteOverflow) failed = true;
const cols = selectedValues(total);
stdout(`${cols.map(String).join(" ")}${totalMode === "only" ? "" : " total"}\n`);
}
return failed ? 1 : 0;
}
export function validateWcTotalMode(totalMode) {
if (["auto", "always", "only", "never"].includes(totalMode)) return;
throw new UsageError(`${totalMode === "" ? "ambiguous" : "invalid"} argument ${localeQuotedEscapedDiagnostic(totalMode)} for ${localeQuotedDiagnostic("--total")}\nValid arguments are:\n - ${localeQuotedDiagnostic("auto")}\n - ${localeQuotedDiagnostic("always")}\n - ${localeQuotedDiagnostic("only")}\n - ${localeQuotedDiagnostic("never")}`, true);
}
export function wcDisplayName(name) {
const text = pathDisplayName(name);
if (!text.includes("\n")) return text;
const parts = text.split("\n");
const trailingNewline = parts.at(-1) === "";
if (trailingNewline) parts.pop();
return parts.map((part) => `'${part.replaceAll("'", "'\\''")}'`).join("$'\\n'") + (trailingNewline ? "$'\\n'" : "");
}
export function wcDiagnosticName(name) {
return shellEscapeLsName(pathDisplayName(name));
}
export function wcFiles0OpenName(name) {
return name === "-" ? "-" : shellEscapeLsName(pathDisplayName(name), true);
}
export function wcFiles0DiagnosticName(name) {
return name === "-" ? "-" : shellEscapeLsName(pathDisplayName(name));
}
export function wcStatIsSpecial(info) {
return info.isCharacterDevice() || info.isBlockDevice() || info.isFIFO() || info.isSocket();
}
export function addWcByteTotal(total, bytes) {
const next = total + BigInt(bytes);
return next > UINTMAX_MAX ? UINTMAX_MAX : next;
}
export function wcByteTotalWouldOverflow(total, bytes) {
return total + BigInt(bytes) > UINTMAX_MAX;
}
export function normalizeWcLongOptions(args) {
return normalizeLongOptionsByPrefix(args, WC_LONG_OPTIONS);
}
export function normalizeWcLongOption(arg) {
return normalizeLongOptionByPrefix(arg, WC_LONG_OPTIONS);
}
export function wcRow(bytes, name) {
const text = new TextDecoder().decode(bytes);
return {
lines: (text.match(/\n/g) || []).length,
words: countWcWords(bytes, text),
bytes: bytes.byteLength,
chars: [...text].length,
maxLineLength: maxLineLength(text),
name,
};
}
export function countWcWords(bytes, text) {
if (text.includes("\uFFFD")) return countWcWordsBytes(bytes);
let count = 0;
let inWord = false;
for (const ch of text) {
if (isWcWordSeparator(ch)) {
inWord = false;
} else if (!inWord) {
count++;
inWord = true;
}
}
return count;
}
export function countWcWordsBytes(bytes) {
let count = 0;
let inWord = false;
for (const byte of bytes) {
if (isWcByteSeparator(byte)) {
inWord = false;
} else if (!inWord) {
count++;
inWord = true;
}
}
return count;
}
export function isWcWordSeparator(ch) {
return /\s/u.test(ch) || ch === "\u2060";
}
export function isWcByteSeparator(byte) {
return byte === 0x20 || (byte >= 0x09 && byte <= 0x0d) || byte === 0xa0 || byte === 0x9a;
}
export function maxLineLength(text) {
let max = 0;
let column = 0;
for (const ch of text) {
if (ch === "\n" || ch === "\r" || ch === "\f") {
max = Math.max(max, column);
column = 0;
} else if (ch === "\t") {
column += 8 - (column % 8);
} else {
column += displayWidth(ch);
}
}
return Math.max(max, column);
}
const singleCall = defineCommand("wc", wc, wcMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|