about summary refs log tree commit diff stats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-17 21:14:51 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-17 21:20:04 +0100
commitfc8937b53327f99b5809f78e3257e62a05bd1c79 (patch)
tree64655d81478c9fb8ac93d67259c60cb4fd246b5f /src/local
parentd385d07b197cef65c2d2a800378de9152551e3e6 (diff)
downloadchawan-fc8937b53327f99b5809f78e3257e62a05bd1c79.tar.gz
config: clean up/simplify
* Parse the default config at runtime. There's no significant
  performance difference, but this makes it much less painful to write
  config code.
* Add better error reporting
* Make fromJS2 easier to use
* Unquote ChaPaths while parsing config
Diffstat (limited to 'src/local')
-rw-r--r--src/local/client.nim14
-rw-r--r--src/local/pager.nim26
2 files changed, 14 insertions, 26 deletions
diff --git a/src/local/client.nim b/src/local/client.nim
index 7947ce15..60bdf411 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -774,8 +774,9 @@ proc dumpBuffers(client: Client) =
       quit(1)
   stdout.close()
 
-proc launchClient*(client: Client, pages: seq[string],
-    contentType: Option[string], cs: Charset, dump: bool) =
+proc launchClient*(client: Client; pages: seq[string];
+    contentType: Option[string]; cs: Charset; dump: bool;
+    warnings: seq[string]) =
   var infile: File
   var dump = dump
   if not dump:
@@ -794,6 +795,7 @@ proc launchClient*(client: Client, pages: seq[string],
   client.loader.unregisterFun = proc(fd: int) =
     selector.unregister(fd)
   client.pager.launchPager(infile, selector)
+  client.pager.alerts.add(warnings)
   let clearFun = proc() =
     client.clearConsole()
   let showFun = proc() =
@@ -881,17 +883,17 @@ proc addJSModules(client: Client, ctx: JSContext) =
 func getClient(client: Client): Client {.jsfget: "client".} =
   return client
 
-proc newClient*(config: Config, forkserver: ForkServer): Client =
+proc newClient*(config: Config; forkserver: ForkServer; jsctx: JSContext):
+    Client =
   setControlCHook(proc() {.noconv.} = quit(1))
-  let jsrt = newJSRuntime()
+  let jsrt = JS_GetRuntime(jsctx)
   JS_SetModuleLoaderFunc(jsrt, normalizeModuleName, clientLoadJSModule, nil)
-  let jsctx = jsrt.newJSContext()
   let pager = newPager(config, forkserver, jsctx)
   let loader = forkserver.newFileLoader(LoaderConfig(
     urimethodmap: config.getURIMethodMap(),
     w3mCGICompat: config.external.w3m_cgi_compat,
     cgiDir: pager.cgiDir,
-    tmpdir: pager.tmpdir
+    tmpdir: config.external.tmpdir
   ))
   pager.setLoader(loader)
   let client = Client(
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 6448d1a8..e5d7d062 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -12,7 +12,6 @@ when defined(posix):
   import std/posix
 
 import bindings/libregexp
-import config/chapath
 import config/config
 import config/mailcap
 import config/mimetypes
@@ -100,7 +99,7 @@ type
 
   Pager* = ref object
     alertState: PagerAlertState
-    alerts: seq[string]
+    alerts*: seq[string]
     askcharpromise*: Promise[string]
     askcursor: int
     askpromise*: Promise[bool]
@@ -127,7 +126,6 @@ type
     mimeTypes: MimeTypes
     notnum*: bool # has a non-numeric character been input already?
     numload*: int # number of pages currently being loaded
-    omnirules: seq[OmniRule]
     precnum*: int32 # current number prefix (when vi-numeric-prefix is true)
     procmap*: seq[ProcMapItem]
     proxy: URL
@@ -136,10 +134,8 @@ type
     reverseSearch: bool
     scommand*: string
     selector*: Selector[int]
-    siteconf: seq[SiteConfig]
     statusgrid*: FixedGrid
     term*: Terminal
-    tmpdir*: string
     unreg*: seq[Container]
     urimethodmap: URIMethodMap
 
@@ -282,16 +278,9 @@ proc quit*(pager: Pager, code = 0) =
   pager.dumpAlerts()
 
 proc setPaths(pager: Pager): Err[string] =
-  let tmpdir0 = pager.config.external.tmpdir.unquote()
-  if tmpdir0.isErr:
-    return err("Error unquoting external.tmpdir: " & tmpdir0.error)
-  pager.tmpdir = tmpdir0.get
   var cgiDir: seq[string]
   for path in pager.config.external.cgi_dir:
-    let x = path.unquote()
-    if x.isErr:
-      return err("Error unquoting external.cgi-dir: " & x.error)
-    cgiDir.add(x.get)
+    cgiDir.add(path)
   pager.cgiDir = cgiDir
   return ok()
 
@@ -302,9 +291,7 @@ proc newPager*(config: Config; forkserver: ForkServer; ctx: JSContext): Pager =
     forkserver: forkserver,
     mailcap: mailcap,
     mimeTypes: config.getMimeTypes(),
-    omnirules: config.getOmniRules(ctx),
     proxy: config.getProxy(),
-    siteconf: config.getSiteConfig(ctx),
     term: newTerminal(stdout, config),
     urimethodmap: config.getURIMethodMap()
   )
@@ -889,7 +876,7 @@ func getEditorCommand(pager: Pager; file: string; line = 1): string {.jsfunc.} =
 
 proc openInEditor(pager: Pager; input: var string): bool =
   try:
-    let tmpf = getTempFile(pager.tmpdir)
+    let tmpf = getTempFile(pager.config.external.tmpdir)
     if input != "":
       writeFile(tmpf, input)
     let cmd = pager.getEditorCommand(tmpf)
@@ -931,7 +918,7 @@ proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset;
   var charsets = pager.config.encoding.document_charset
   var userstyle = pager.config.css.stylesheet
   var proxy = pager.proxy
-  for sc in pager.siteconf:
+  for sc in pager.config.siteconf:
     if sc.url.isSome and not sc.url.get.match($url):
       continue
     elif sc.host.isSome and not sc.host.get.match(host):
@@ -1019,7 +1006,7 @@ proc gotoURL(pager: Pager, request: Request, prevurl = none(URL),
     pager.container.findAnchor(request.url.anchor)
 
 proc omniRewrite(pager: Pager, s: string): string =
-  for rule in pager.omnirules:
+  for rule in pager.config.omnirule:
     if rule.match.match(s):
       let sub = rule.substitute_url(s)
       if sub.isSome:
@@ -1514,9 +1501,8 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
   let entry = pager.mailcap.getMailcapEntry(contentType, "", url)
   if entry == nil:
     return CheckMailcapResult(connect: true, fdout: stream.fd, found: false)
-  let tmpdir = pager.tmpdir
   let ext = url.pathname.afterLast('.')
-  let tempfile = getTempFile(tmpdir, ext)
+  let tempfile = getTempFile(pager.config.external.tmpdir, ext)
   let outpath = if entry.nametemplate != "":
     unquoteCommand(entry.nametemplate, contentType, tempfile, url)
   else: