aboutsummaryrefslogtreecommitdiff
path: root/src/shared/text.js
blob: 9c62bad85cad0850bd6e492e3afcc23bafac7f68 (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
import { fstatSync, readSync, statSync } from "node:fs";
import { mkdtemp, open, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { SEEK_CUR, displayWidth, libc, pathDisplayName, shellEscapeLsName } from "./common.js";
import { stdout } from "./diagnostics.js";

export const POLLOUT = 0x004;

export const POLLERR = 0x008;

export const POLLHUP = 0x010;

export const POLLNVAL = 0x020;

export async function spoolInputToTemporaryFile(input, prefix, initialChunks = []) {
  const base = process.env.TMPDIR || "/tmp";
  const dir = await mkdtemp(join(base, `${prefix}-`));
  const path = join(dir, "data");
  const source = input === "-" ? null : await open(input, "r");
  let target;
  try {
    target = await open(path, "wx", 0o600);
    const buffer = Buffer.allocUnsafe(64 * 1024);
    const writeChunk = async (chunk) => {
      let offset = 0;
      while (offset < chunk.length) {
        const { bytesWritten } = await target.write(chunk, offset, chunk.length - offset, null);
        if (bytesWritten === 0) throw new Error("short write while spooling input");
        offset += bytesWritten;
      }
    };
    for (const chunk of initialChunks) await writeChunk(chunk);
    while (true) {
      const n = readSync(source?.fd ?? 0, buffer, 0, buffer.length, null);
      if (n === 0) break;
      await writeChunk(buffer.subarray(0, n));
    }
    await target.close();
    target = null;
    return { path, cleanup: () => rm(dir, { recursive: true, force: true }) };
  } catch (error) {
    await target?.close().catch(() => {});
    await rm(dir, { recursive: true, force: true }).catch(() => {});
    throw error;
  } finally {
    await source?.close().catch(() => {});
  }
}

export function readStdinChunks(accept) {
  const buffer = Buffer.allocUnsafe(64 * 1024);
  while (true) {
    const n = readSync(0, buffer, 0, buffer.length, null);
    if (n === 0) break;
    accept(Buffer.from(buffer.subarray(0, n)));
  }
}

export async function writeAll(path, data, append = false) {
  if (path === "-") return stdout(data);
  await writeFile(path, data, append ? { flag: "a" } : undefined);
}

export function isSingleByteLocale() {
  return /^(C|POSIX)$/.test(process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? "");
}

export function concatOutputParts(parts) {
  const chunks = parts.map((part) => part instanceof Uint8Array ? Buffer.from(part) : Buffer.from(String(part)));
  return chunks.length ? Buffer.concat(chunks) : "";
}

export function tabFoldDiagnosticName(file) {
  return shellEscapeLsName(pathDisplayName(file));
}

export function nextUtf8Token(bytes, index) {
  const b0 = bytes[index];
  if (b0 < 0x80) return { bytes: bytes.slice(index, index + 1), char: String.fromCharCode(b0), width: b0 === 0x09 ? 0 : 1, next: index + 1 };
  const len = b0 >= 0xc2 && b0 <= 0xdf ? 2 : b0 >= 0xe0 && b0 <= 0xef ? 3 : b0 >= 0xf0 && b0 <= 0xf4 ? 4 : 1;
  if (len === 1 || index + len > bytes.length) return { bytes: bytes.slice(index, index + 1), char: null, width: 1, next: index + 1 };
  for (let i = 1; i < len; i++) {
    if ((bytes[index + i] & 0xc0) !== 0x80) return { bytes: bytes.slice(index, index + 1), char: null, width: 1, next: index + 1 };
  }
  const slice = bytes.slice(index, index + len);
  const char = new TextDecoder("utf-8", { ignoreBOM: true }).decode(slice);
  return { bytes: slice, char, width: displayWidth(char), next: index + len };
}

export function tacRegexPattern(sep) {
  let out = "";
  let lastAtomKind = null;
  for (let i = 0; i < sep.length; i++) {
    const ch = sep[i];
    if (ch === "\\") {
      const next = sep[++i];
      if (next == null) out += ch;
      else if ("()|".includes(next)) out += next;
      else if ("+?{}".includes(next)) out += `\\${next}`;
      else out += ch + next;
      lastAtomKind = "literal";
    } else if (ch === "[") {
      const end = sep.indexOf("]", i + 1);
      if (end === -1) {
        out += ch;
        lastAtomKind = null;
      } else {
        out += sep.slice(i, end + 1);
        i = end;
        lastAtomKind = "bracket";
      }
    } else if (ch === "+" || ch === "?") {
      if (!tacRegexHasPreviousAtom(out)) {
        out += `\\${ch}`;
        lastAtomKind = "literal";
      } else if (lastAtomKind !== "literal" && lastAtomKind !== "bracket") {
        out += ch;
      }
    } else if ("()|{}+?".includes(ch)) {
      out += `\\${ch}`;
      lastAtomKind = "literal";
    } else {
      out += ch;
      lastAtomKind = ch === "." || ch === "_" ? "repeatable" : "literal";
    }
  }
  return out;
}

export function tacRegexHasPreviousAtom(pattern) {
  if (pattern === "" || pattern.endsWith("|") || pattern.endsWith("(")) return false;
  return true;
}

export function splitSeparator(value) {
  return value === "\\0" ? "\0" : value;
}

export function decodeValidUtf8(bytes) {
  try {
    return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
  } catch {
    return null;
  }
}

export function prInputIsNonRegular(file) {
  try {
    return !(file === "-" ? fstatSync(0) : statSync(file)).isFile();
  } catch {
    return false;
  }
}

export function fdIsSeekable(fd) {
  return libc.symbols.lseek(fd, 0n, SEEK_CUR) >= 0;
}

export function pollFd(fd, events, timeout = 0) {
  const pollfd = Buffer.alloc(8);
  pollfd.writeInt32LE(fd, 0);
  pollfd.writeInt16LE(events, 4);
  if (libc.symbols.poll(pollfd, 1n, timeout) <= 0) return 0;
  return pollfd.readInt16LE(6);
}