about summary refs log tree commit diff stats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-11 00:40:51 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-11 00:40:51 +0100
commitb0583b4760a11bd062781e0ea948c61c8b66ff8f (patch)
tree5bfb46cb1aa57d62fb40d6b6869ac8a875071561 /src/local
parentd0690cfea6a87c7b7d801b968b5a1c85d1e99b4f (diff)
downloadchawan-b0583b4760a11bd062781e0ea948c61c8b66ff8f.tar.gz
Get rid of LOAD_PIPE BufferSource
Instead, use a stream: scheme and associate hostnames with file
descriptors directly from the pager.
Diffstat (limited to 'src/local')
-rw-r--r--src/local/client.nim6
-rw-r--r--src/local/container.nim33
-rw-r--r--src/local/pager.nim24
3 files changed, 34 insertions, 29 deletions
diff --git a/src/local/client.nim b/src/local/client.nim
index ae838360..76cbd4e7 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -516,7 +516,7 @@ proc addConsole(pager: Pager, interactive: bool, clearFun, showFun, hideFun:
     var pipefd: array[0..1, cint]
     if pipe(pipefd) == -1:
       raise newException(Defect, "Failed to open console pipe.")
-    let url = newURL("javascript:console.show()").get
+    let url = newURL("stream:console").get
     let container = pager.readPipe0(some("text/plain"), CHARSET_UNKNOWN,
       pipefd[0], some(url), ConsoleTitle, canreinterpret = false)
     let err = newPosixStream(pipefd[1])
@@ -543,7 +543,7 @@ proc clearConsole(client: Client) =
   var pipefd: array[0..1, cint]
   if pipe(pipefd) == -1:
     raise newException(Defect, "Failed to open console pipe.")
-  let url = newURL("javascript:console.show()").get
+  let url = newURL("stream:console").get
   let pager = client.pager
   let replacement = pager.readPipe0(some("text/plain"), CHARSET_UNKNOWN,
     pipefd[0], some(url), ConsoleTitle, canreinterpret = false)
@@ -613,7 +613,7 @@ proc launchClient*(client: Client, pages: seq[string],
       module = ismodule)
 
   if not stdin.isatty():
-    client.pager.readPipe(contentType, cs, stdin.getFileHandle())
+    client.pager.readPipe(contentType, cs, stdin.getFileHandle(), "*stdin*")
 
   for page in pages:
     client.pager.loadURL(page, ctype = contentType, cs = cs)
diff --git a/src/local/container.nim b/src/local/container.nim
index 2bf53756..770297f4 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -15,6 +15,7 @@ import js/javascript
 import js/jstypes
 import js/regex
 import loader/connecterror
+import loader/loader
 import loader/request
 import local/select
 import server/buffer
@@ -99,6 +100,7 @@ type
     bpos: seq[CursorPosition]
     highlights: seq[Highlight]
     process* {.jsget.}: Pid
+    loaderPid* {.jsget.}: Pid
     loadinfo*: string
     lines: SimpleFlexibleGrid
     lineshift: int
@@ -129,10 +131,19 @@ jsDestructor(Container)
 
 proc newBuffer*(forkserver: ForkServer, config: BufferConfig,
     source: BufferSource, title = "", redirectdepth = 0,
-    canreinterpret = true): Container =
+    canreinterpret = true, fd = FileHandle(-1)): Container =
   let attrs = getWindowAttributes(stdout)
+  let (process, loaderPid) = forkserver.forkBuffer(source, config, attrs)
+  if fd != -1:
+    loaderPid.passFd(source.location.host, fd)
+    if fd == 0:
+      # We are passing stdin.
+      closeStdin()
+    else:
+      discard close(fd)
   return Container(
-    process: forkserver.forkBuffer(source, config, attrs),
+    process: process,
+    loaderPid: loaderPid,
     source: source,
     width: attrs.width,
     height: attrs.height - 1,
@@ -1377,10 +1388,12 @@ proc redirectToFd*(container: Container, fdin: FileHandle, wait: bool):
     EmptyPromise =
   return container.iface.redirectToFd(fdin, wait)
 
-proc readFromFd*(container: Container, fdout: FileHandle, ishtml: bool):
-    EmptyPromise =
+proc readFromFd*(container: Container, fdout: FileHandle, id: string,
+    ishtml: bool): EmptyPromise =
   container.ishtml = ishtml
-  return container.iface.readFromFd(fdout, ishtml)
+  let url = newURL("stream:" & id).get
+  container.loaderPid.passFd(url.host, fdout)
+  return container.iface.readFromFd(url, ishtml)
 
 proc quit*(container: Container) =
   container.triggerEvent(QUIT)
@@ -1510,16 +1523,6 @@ proc handleCommand(container: Container) =
 proc setStream*(container: Container, stream: Stream) =
   if not container.cloned:
     container.iface = newBufferInterface(stream)
-    if container.source.t == LOAD_PIPE:
-      container.iface.passFd(container.source.fd).then(proc() =
-        if container.source.fd == 0:
-          # We are closing stdin.
-          # Leaving the stdin fileno open to grab is a bad idea.
-          closeStdin()
-        else:
-          discard close(container.source.fd)
-      )
-      stream.flush()
     container.load()
   else:
     container.iface = cloneInterface(stream)
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 9633293a..6a4f3522 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -444,14 +444,16 @@ proc addContainer*(pager: Pager, container: Container) =
   pager.setContainer(container)
 
 proc newBuffer(pager: Pager, bufferConfig: BufferConfig, source: BufferSource,
-    title = "", redirectdepth = 0, canreinterpret = true): Container =
+    title = "", redirectdepth = 0, canreinterpret = true,
+    fd = FileHandle(-1)): Container =
   return newBuffer(
     pager.forkserver,
     bufferConfig,
     source,
     title,
     redirectdepth,
-    canreinterpret
+    canreinterpret,
+    fd
   )
 
 proc dupeBuffer2(pager: Pager, container: Container, location: URL,
@@ -772,21 +774,21 @@ proc loadURL*(pager: Pager, url: string, ctype = none(string),
 proc readPipe0*(pager: Pager, ctype: Option[string], cs: Charset,
     fd: FileHandle, location: Option[URL], title: string,
     canreinterpret: bool): Container =
-  var location = location.get(newURL("file://-").get)
+  var location = location.get(newURL("stream:-").get)
   let bufferconfig = pager.applySiteconf(location)
   let source = BufferSource(
-    t: LOAD_PIPE,
-    fd: fd,
+    t: LOAD_REQUEST,
+    request: newRequest(location),
     contentType: some(ctype.get("text/plain")),
     charset: cs,
     location: location
   )
   return pager.newBuffer(bufferconfig, source, title = title,
-    canreinterpret = canreinterpret)
+    canreinterpret = canreinterpret, fd = fd)
 
-proc readPipe*(pager: Pager, ctype: Option[string], cs: Charset,
-    fd: FileHandle) =
-  let container = pager.readPipe0(ctype, cs, fd, none(URL), "*pipe*", true)
+proc readPipe*(pager: Pager, ctype: Option[string], cs: Charset, fd: FileHandle,
+    title: string) =
+  let container = pager.readPipe0(ctype, cs, fd, none(URL), title, true)
   pager.addContainer(container)
 
 proc command(pager: Pager) {.jsfunc.} =
@@ -973,7 +975,7 @@ proc runMailcapReadPipe(pager: Pager, container: Container,
   let p2 = p.then(proc(): auto =
     discard close(fdin)
     let ishtml = HTMLOUTPUT in entry.flags
-    return container.readFromFd(fdout, ishtml)
+    return container.readFromFd(fdout, $pid, ishtml)
   ).then(proc() =
     discard close(fdout)
   )
@@ -1045,7 +1047,7 @@ proc runMailcapReadFile(pager: Pager, container: Container,
     discard close(pipefd[1])
     let fdout = pipefd[0]
     let ishtml = HTMLOUTPUT in entry.flags
-    return container.readFromFd(fdout, ishtml).then(proc() =
+    return container.readFromFd(fdout, $pid, ishtml).then(proc() =
       discard close(fdout)
     )
   )