aboutsummaryrefslogtreecommitdiff
path: root/src/commands/head.js
blob: 9a9bdea2aa2f4fada0658f46de93197c4ffe63a4 (plain)
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
#!/usr/bin/env bun

import { closeSync, fstatSync, openSync, readSync } from "node:fs";
import { stat } from "node:fs/promises";
import { SEEK_CUR, headTailDiagnosticName, isWriteError, libc, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, readAll, systemErrorMessage } from "../shared/common.js";
import { UsageError, stderr, stdout } from "../shared/diagnostics.js";
import { headTailHeaderMode, headTailShouldPrintHeader, isTailStreamingDevice, normalizeHeadTailArgs, parseByteCount, splitDelimitedByteRecords, tailFirstBytes } from "../shared/head-tail.js";
import { outputWriteErrorMessage } from "../shared/runtime.js";
import { fdIsSeekable } from "../shared/text.js";
import { defineCommand, runAsMain } from "../shared/command.js";

export const HEAD_LONG_OPTIONS = ["bytes", "lines", "quiet", "silent", "verbose", "zero-terminated", "help", "version"];

export function headMetaOption(args) {
  const longValueOptions = new Set(["bytes", "lines"]);
  const shortValueOptions = new Set(["c", "n"]);
  const shortKnownOptions = new Set(["c", "n", "q", "v", "z"]);
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
    if (arg === "--") return null;
    if (arg.startsWith("--")) {
      const option = normalizeHeadLongOption(arg);
      const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2);
      if (!HEAD_LONG_OPTIONS.includes(name)) return null;
      if ((option === "--help" || option === "--version") && inlineValue == null) return option;
      if (longValueOptions.has(name)) {
        const value = inlineValue ?? args[i + 1];
        if (value !== undefined) {
          if (name === "bytes") parseCount(value, 10, "bytes");
          if (name === "lines") parseCount(value, 10, "lines");
        }
        if (inlineValue == null) i++;
      }
      continue;
    }
    if (!arg.startsWith("-") || arg === "-") continue;
    if (/^-\d/.test(arg)) continue;
    for (let j = 1; j < arg.length; j++) {
      const ch = arg[j];
      if (!shortKnownOptions.has(ch)) return null;
      if (shortValueOptions.has(ch)) {
        const inlineValue = arg.slice(j + 1);
        const value = inlineValue === "" ? args[i + 1] : inlineValue;
        if (value !== undefined) parseCount(value, 10, ch === "c" ? "bytes" : "lines");
        if (inlineValue === "") i++;
        break;
      }
    }
  }
  return null;
}

export function parseCount(value, defaultValue, kind = "lines") {
  if (value == null) return defaultValue;
  const countText = String(value).replace(/^[+-]/, "");
  let n;
  try {
    n = parseByteCount(countText);
  } catch {
    throw new UsageError(`invalid number of ${kind}: ${localeQuotedEscapedDiagnostic(countText)}`);
  }
  if (!Number.isFinite(n) || n < 0) throw new UsageError(`invalid number of ${kind}: ${localeQuotedEscapedDiagnostic(countText)}`);
  return Math.trunc(n);
}

export async function head(args) {
  args = normalizeHeadTailArgs(normalizeHeadLongOptions(args), "head");
  args = args.filter((arg) => arg !== "---presume-input-pipe");
  const headerMode = headTailHeaderMode(args);
  const { opts, operands } = parseOptions(args, { short: { n: "value", c: "value", q: false, v: false, z: false }, long: { lines: "value", bytes: "value", quiet: false, silent: false, verbose: false, "zero-terminated": false, help: false, version: false } });
  const files = operands.length ? operands : ["-"];
  const bytesMode = opts.c ?? opts.bytes;
  const count = parseCount(bytesMode ?? opts.n ?? opts.lines, 10, bytesMode != null ? "bytes" : "lines");
  const delimiter = opts.z || opts["zero-terminated"] ? "\0" : "\n";
  let failed = false;
  let renderedAnyFile = false;
  try {
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      const raw = String(bytesMode ?? opts.n ?? opts.lines ?? "");
      const header = headTailShouldPrintHeader(headerMode, files.length) ? `${renderedAnyFile ? "\n" : ""}==> ${file} <==\n` : "";
      if (file === "-" && files.length === 1 && headerMode !== "verbose") {
        if (bytesMode != null) {
          headWrite(raw.startsWith("-") ? readHeadFdExceptLastBytes(0, count) : readHeadStdinBytes(count));
        } else {
          headWrite(raw.startsWith("-") ? readHeadFdExceptLastRecords(0, count, delimiter) : readHeadStdinRecords(count, delimiter));
        }
        continue;
      }
      if (raw.startsWith("-") && file !== "-" && !(await stat(file).catch(() => null))?.isDirectory()) {
        if (header) headWrite(header);
        let fd;
        try {
          fd = openSync(file, "r");
          if (bytesMode != null) readHeadFdExceptLastBytes(fd, count);
          else readHeadFdExceptLastRecords(fd, count, delimiter);
          renderedAnyFile = true;
        } catch (error) {
          if (isWriteError(error)) throw error;
          stderr(`head: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`);
          failed = true;
        } finally {
          if (fd != null) closeSync(fd);
        }
        continue;
      }
      if (bytesMode != null && !raw.startsWith("-") && file !== "-" && await isTailStreamingDevice(file)) {
        if (header) headWrite(header);
        try {
          await tailFirstBytes(file, count);
          renderedAnyFile = true;
        } catch (error) {
          stderr(`head: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`);
          failed = true;
        }
        continue;
      }
      let data;
      try {
        data = await readAll(file);
      } catch (error) {
        if (error?.code === "EISDIR") {
          if (header) headWrite(header);
          renderedAnyFile = true;
          stderr(`head: error reading ${headTailDiagnosticName(file)}: ${systemErrorMessage(error)}\n`);
        } else {
          stderr(file === "-" ? `head: ${nodeErrorMessage(error)}\n` : `head: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`);
        }
        failed = true;
        continue;
      }
      if (header) headWrite(header);
      renderedAnyFile = true;
      if (bytesMode != null) {
        headWrite(raw.startsWith("-") ? data.slice(0, Math.max(0, data.length - count)) : data.slice(0, count));
      } else {
        const lines = splitDelimitedByteRecords(data, delimiter);
        headWrite(Buffer.concat(raw.startsWith("-") ? lines.slice(0, Math.max(0, lines.length - count)) : lines.slice(0, count)));
      }
    }
  } catch (error) {
    if (!isWriteError(error)) throw error;
    stderr(`head: error writing 'standard output': ${outputWriteErrorMessage(error)}\n`);
    return 1;
  }
  return failed ? 1 : 0;
}

export function headWrite(data) {
  stdout(data);
}

export function normalizeHeadLongOptions(args) {
  return normalizeLongOptionsByPrefix(args, HEAD_LONG_OPTIONS);
}

export function normalizeHeadLongOption(arg) {
  return normalizeLongOptionByPrefix(arg, HEAD_LONG_OPTIONS);
}

export function readHeadStdinBytes(count) {
  if (count > 2 ** 32) return readRemainingStdinSync();
  const out = Buffer.alloc(count);
  let offset = 0;
  while (offset < count) {
    const n = readSync(0, out, offset, count - offset, null);
    if (n === 0) break;
    offset += n;
  }
  return out.subarray(0, offset);
}

export function readHeadStdinRecords(count, delimiter) {
  if (count === 0) return "";
  const chunks = [];
  let seen = 0;
  const byte = Buffer.alloc(1);
  const delim = delimiter.charCodeAt(0);
  while (seen < count) {
    const n = readSync(0, byte, 0, 1, null);
    if (n === 0) break;
    chunks.push(byte[0]);
    if (byte[0] === delim) seen++;
  }
  return Buffer.from(chunks);
}

export function readHeadFdExceptLastBytes(fd, count) {
  if (fstatSync(fd).isFile() && fdIsSeekable(fd)) {
    const data = readRemainingFdSync(fd);
    const keep = Math.max(0, data.length - count);
    headWrite(data.subarray(0, keep));
    rewindFd(fd, data.length - keep);
    return "";
  }
  const buffer = Buffer.alloc(8192);
  let tail = Buffer.alloc(0);
  while (true) {
    const n = readSync(fd, buffer, 0, buffer.length, null);
    if (n === 0) break;
    const chunk = Buffer.from(buffer.subarray(0, n));
    if (count === 0) {
      headWrite(chunk);
      continue;
    }
    const combined = Buffer.concat([tail, chunk]);
    const emit = Math.max(0, combined.length - count);
    if (emit) headWrite(combined.subarray(0, emit));
    tail = combined.subarray(emit);
  }
  return "";
}

export function readHeadFdExceptLastRecords(fd, count, delimiter) {
  if (fstatSync(fd).isFile() && fdIsSeekable(fd)) {
    const data = readRemainingFdSync(fd);
    const keep = headPrefixLengthExceptLastRecords(data, count, delimiter.charCodeAt(0));
    headWrite(data.subarray(0, keep));
    rewindFd(fd, data.length - keep);
    return "";
  }
  const delim = delimiter.charCodeAt(0);
  const byte = Buffer.alloc(1);
  let current = [];
  const records = [];
  while (true) {
    const n = readSync(fd, byte, 0, 1, null);
    if (n === 0) break;
    current.push(byte[0]);
    if (byte[0] === delim) {
      records.push(Buffer.from(current));
      current = [];
      while (records.length > count) headWrite(records.shift());
    }
  }
  if (current.length) {
    records.push(Buffer.from(current));
    while (records.length > count) headWrite(records.shift());
  }
  return "";
}

export function readRemainingStdinSync() {
  return readRemainingFdSync(0);
}

export function readRemainingFdSync(fd) {
  const chunks = [];
  const buffer = Buffer.alloc(8192);
  while (true) {
    const n = readSync(fd, buffer, 0, buffer.length, null);
    if (n === 0) break;
    chunks.push(Buffer.from(buffer.subarray(0, n)));
  }
  return Buffer.concat(chunks);
}

export function headPrefixLengthExceptLastRecords(data, count, delimiter) {
  if (count === 0) return data.length;
  let seen = 0;
  for (let i = data.length - 1; i >= 0; i--) {
    if (data[i] === delimiter) {
      seen++;
      if (seen > count) return i + 1;
    }
  }
  return 0;
}

export function rewindFd(fd, bytes) {
  if (bytes > 0) libc.symbols.lseek(fd, -BigInt(bytes), SEEK_CUR);
}

const singleCall = defineCommand("head", head, headMetaOption);
export default singleCall;

if (import.meta.main) await runAsMain(singleCall);