aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-10-28 20:08:09 +0100
committerbptato <nincsnevem662@gmail.com>2024-10-28 20:08:09 +0100
commit6c5922d5f0b2d9d7b6e4c39593c6892a65f74e54 (patch)
tree2485ae5f313f0dd017ba85e86404393e5945b470
parentbd4386923056c53e97f0865d846ef52d0666ca56 (diff)
downloadchawan-6c5922d5f0b2d9d7b6e4c39593c6892a65f74e54.tar.gz
config: refactor, accept more possible config dirs
I'm starting to favor dotfiles over XDG basedirs, but there's no reason why we couldn't have both. So now the search path is: 0. if config was set through -C, use that 1. $CHA_CONFIG_DIR is set -> $CHA_CONFIG_DIR/config.toml 2. $XDG_CONFIG_HOME is set -> $XDG_CONFIG_HOME/chawan/config.toml 3. ~/.config/chawan/config.toml exists -> use that 4. ~/.chawan/config.toml exists -> use that Notably, this makes it so the default directory is ~/.chawan *if* you don't have an existing config.toml file. So in that case known_hosts will be placed in ~/.chawan/known_hosts. However, configurations with a config in ~/.config/chawan/config.toml continue to work as expected, as for those the known_hosts file remains inside ~/.config/chawan/. Finally, I've added a default user CGI directory to reduce friction in setting CGI up. (Like known_hosts, it's also relative to whatever config dir you have.)
-rw-r--r--adapter/protocol/gemini.nim5
-rw-r--r--adapter/protocol/lcgi.nim6
-rw-r--r--bonus/Makefile3
-rw-r--r--bonus/README.md11
-rw-r--r--bonus/w3m.toml4
-rw-r--r--doc/cha-api.58
-rw-r--r--doc/cha-config.557
-rw-r--r--doc/cha-image.530
-rw-r--r--doc/cha-localcgi.58
-rw-r--r--doc/cha-mailcap.52
-rw-r--r--doc/cha-mime.types.52
-rw-r--r--doc/cha-protocols.590
-rw-r--r--doc/cha-troubleshooting.516
-rw-r--r--doc/cha-urimethodmap.54
-rw-r--r--doc/cha.13
-rw-r--r--doc/config.md17
-rw-r--r--doc/image.md2
-rw-r--r--doc/localcgi.md4
-rw-r--r--doc/protocols.md15
-rw-r--r--doc/urimethodmap.md2
-rw-r--r--res/config.toml2
-rw-r--r--src/config/config.nim106
-rw-r--r--src/local/pager.nim3
-rw-r--r--src/main.nim44
-rw-r--r--src/utils/twtstr.nim6
25 files changed, 216 insertions, 234 deletions
diff --git a/adapter/protocol/gemini.nim b/adapter/protocol/gemini.nim
index 6bd656e4..29e10310 100644
--- a/adapter/protocol/gemini.nim
+++ b/adapter/protocol/gemini.nim
@@ -15,15 +15,10 @@ proc fopen(filename, mode: cstring): pointer {.importc, nodecl.}
proc openKnownHosts(os: PosixStream): (File, string) =
var path = getEnv("GMIFETCH_KNOWN_HOSTS")
if path == "":
- let oldPath = getConfigDir() & "gmifetch/known_hosts"
let ourDir = getEnv("CHA_CONFIG_DIR")
if ourDir == "":
os.die("InternalError", "config dir missing")
path = ourDir & '/' & "gemini_known_hosts"
- # for backwards compat, TODO eventually remove this
- # (this has a race, but oh well.)
- if fileExists(oldPath) and not fileExists(path):
- moveFile(oldPath, path)
createDir(path.beforeLast('/'))
let f = cast[File](fopen(cstring(path), "a+"))
if f == nil:
diff --git a/adapter/protocol/lcgi.nim b/adapter/protocol/lcgi.nim
index c73926ec..590c63c8 100644
--- a/adapter/protocol/lcgi.nim
+++ b/adapter/protocol/lcgi.nim
@@ -200,9 +200,3 @@ proc connectSocket*(os: PosixStream; host, port: string; outIpv6: var bool):
proc connectSocket*(os: PosixStream; host, port: string): PosixStream =
var dummy = false
return os.connectSocket(host, port, dummy)
-
-proc getEnvEmpty*(name: string; fallback = ""): string =
- let res = getEnv(name, fallback)
- if res != "":
- return res
- return fallback
diff --git a/bonus/Makefile b/bonus/Makefile
index cab38f89..015b46b9 100644
--- a/bonus/Makefile
+++ b/bonus/Makefile
@@ -1,5 +1,4 @@
-#TODO curse XDG
-CHA_CGI_DIR ?= "$$HOME/cgi-bin"
+CHA_CGI_DIR ?= "$$(cha -r 'pager.extern(`echo "$$CHA_CONFIG_DIR/cgi-bin"`)')"
prefix = /usr/local
diff --git a/bonus/README.md b/bonus/README.md
index b01c671c..5878b9b6 100644
--- a/bonus/README.md
+++ b/bonus/README.md
@@ -12,17 +12,10 @@ Warning: some of the installers will modify your ~/.urimethodmap file.
Because the entries are inserted to the file's start, you can usually
just remove these if you no longer want to use the script.
-Also, the /cgi-bin/ directory is assumed to be configured to ~/cgi-bin.
+Also, the /cgi-bin/ directory is assumed to be configured as the default
+(~/.chawan/cgi-bin or ~/.config/chawan/cgi-bin if you use XDG basedir).
Use `make CHA_CGI_DIR=...` to override this.
-If you haven't configured a CGI directory yet, then please add the
-following to your ~/.config/chawan/config.toml:
-
-```
-[external]
-cgi-dir = ["~/cgi-bin", "${%CHA_LIBEXEC_DIR}/cgi-bin"]
-```
-
## Summary
Additional documentation is embedded at the beginning of each file.
diff --git a/bonus/w3m.toml b/bonus/w3m.toml
index 4a3e6cdf..22960e05 100644
--- a/bonus/w3m.toml
+++ b/bonus/w3m.toml
@@ -1,7 +1,7 @@
# w3m-like keybindings for Chawan.
# Copy-paste this into your config file, or just include it (place it in
-# ~/.config/chawan/w3m.toml, then at the beginning of
-# ~/.config/chawan/chawan.toml, include = "w3m.toml".)
+# ~/.chawan/w3m.toml ~/.config/chawan/w3m.toml, then at the beginning of
+# ~/.chawan/config.toml, include = "w3m.toml".)
[cmd.w3m.buffer]
pageDown = 'n => pager.scrollDown(pager.height * (n ?? 1))'
diff --git a/doc/cha-api.5 b/doc/cha-api.5
index 499df580..9343d33f 100644
--- a/doc/cha-api.5
+++ b/doc/cha-api.5
@@ -1,5 +1,5 @@
'\" t
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-api" "5" "" "" "Chawan\[cq]s command API"
.SH Chawan\[cq]s command API
@@ -298,11 +298,11 @@ Save the source of the current buffer.
T}@T{
T}
T{
-\f[CR]extern(cmd, options = {setenv: true, suspend: true, wait: false})\f[R]
+\f[CR]extern(cmd, options = {env: { ... }, suspend: true, wait: false})\f[R]
T}@T{
Run an external command \f[CR]cmd\f[R].
-The \f[CR]$CHA_URL\f[R] and \f[CR]$CHA_CHARSET\f[R] variables are set
-when \f[CR]options.setenv\f[R] is true.
+By default, the \f[CR]$CHA_URL\f[R] and \f[CR]$CHA_CHARSET\f[R]
+variables are set; change this using the \f[CR]env\f[R] option.
\f[CR]options.suspend\f[R] suspends the pager while the command is being
executed, and \f[CR]options.wait\f[R] makes it so the user must press a
key before the pager is resumed.
diff --git a/doc/cha-config.5 b/doc/cha-config.5
index 237d28ad..3a0d4f1a 100644
--- a/doc/cha-config.5
+++ b/doc/cha-config.5
@@ -1,12 +1,11 @@
'\" t
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-config" "5" "" "" "Configuration of Chawan"
.SH Configuration of Chawan
Chawan supports configuration of various options like keybindings, user
stylesheets, site preferences, etc.
-The configuration format is very similar to toml, with the following
-exceptions:
+The configuration format is almost toml, with the following exceptions:
.IP \[bu] 2
Inline tables may span across multiple lines.
.IP \[bu] 2
@@ -22,12 +21,18 @@ omnirule = [] # note: this must be placed at the beginning of the file.
[[omnirule]] # this is legal. all default omni\-rules are now disabled.
.EE
.PP
-Chawan will look for a config file in the $XDG_CONFIG_HOME/chawan/
-directory called \f[CR]config.toml\f[R].
-(Chawan defaults to \[ti]/.config if the XDG_CONFIG_HOME environment
-variable is not set.)
-See the default configuration file in the res/ folder, and bonus
-configuration files in the bonus/ folder for further examples.
+The canonical configuration file path is \[ti]/.chawan/config.toml, but
+the search path accomodates XDG basedirs as well:
+.IP "1." 3
+config file specified through \-C switch \-> use that
+.IP "2." 3
+$CHA_CONFIG_DIR is set \-> use $CHA_CONFIG_DIR/config.toml
+.IP "3." 3
+$XDG_CONFIG_HOME is set \-> use $XDG_CONFIG_HOME/chawan/config.toml
+.IP "4." 3
+\[ti]/.config/chawan/config.toml exists \-> use that
+.IP "5." 3
+\[ti]/.chawan/config.toml exists \-> use that
.SS Start
Start\-up options are to be placed in the \f[CR][start]\f[R] section.
.PP
@@ -90,6 +95,21 @@ T}
.SS Buffer
Buffer options are to be placed in the \f[CR][buffer]\f[R] section.
.PP
+These options are global to all buffers.
+For more granular filtering, use \f[CR][[siteconf]]\f[R].
+.PP
+Example:
+.IP
+.EX
+\f[B][buffer]\f[R]
+\f[I]# show images on all websites\f[R]
+images = true
+\f[I]# disable website CSS\f[R]
+styling = false
+\f[I]# You *can* set scripting to true here, but I strongly recommend using\f[R]
+\f[I]# [[siteconf]] to enable it on a per\-site basis instead.\f[R]
+.EE
+.PP
Following is a list of buffer options:
.PP
.TS
@@ -264,7 +284,7 @@ T}@T{
Character set for keyboard input and displaying documents.
Used in dump mode as well.
(This means that e.g.\ \f[CR]cha \-I EUC\-JP \-O UTF\-8 a > b\f[R] is
-equivalent to \f[CR]iconv \-f EUC\-JP \-t UTF\-8\f[R].)
+roughly equivalent to \f[CR]iconv \-f EUC\-JP \-t UTF\-8\f[R].)
T}@T{
T}
.TE
@@ -795,6 +815,23 @@ host = \[aq](.*.)?sr.ht\[aq] # either \[aq]something.sr.ht\[aq] or \[aq]sr.ht\[a
cookie = true # enable cookies
share\-cookie\-jar = \[aq]sr.ht\[aq] # use the cookie jar of \[aq]sr.ht\[aq] for all matched hosts
third\-party\-cookie = \[aq].*.sr.ht\[aq] # allow cookies from subdomains
+
+# Use the \[dq]vector\[dq] skin on Wikipedia, which Chawan can handle better.
+[[siteconf]]
+url = \[aq]\[ha]https?://[a\-z]+.wikipedia.org/wiki/(?!.*useskin=.*)\[aq]
+rewrite\-url = \[aq]x => x.searchParams.append(\[dq]useskin\[dq], \[dq]vector\[dq])\[aq]
+
+# Make imgur send us images.
+[[siteconf]]
+host = \[aq](i.)?imgur.com\[aq]
+default\-headers = {
+User\-Agent = \[dq]Mozilla/5.0 chawan\[dq],
+Accept = \[dq]*/*\[dq],
+Accept\-Encoding = \[dq]gzip, deflate\[dq],
+Accept\-Language = \[dq]en;q=1.0\[dq],
+Pragma = \[dq]no\-cache\[dq],
+Cache\-Control = \[dq]no\-cache\[dq]
+}
.EE
.PP
Siteconf options:
diff --git a/doc/cha-image.5 b/doc/cha-image.5
index 43309d32..9224f6ac 100644
--- a/doc/cha-image.5
+++ b/doc/cha-image.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-image" "5" "" "" "Image support in Chawan"
.SH Inline images
@@ -15,11 +15,19 @@ Defaults to \[lq]auto\[rq], but may also be set to \[lq]sixel\[rq] or
\[lq]kitty\[rq] manually.
.PP
In most cases, all you need to do is to set \[lq]buffer.images\[rq] to
-true; with the default image\-mode, Chawan will find the best image
-display method supported by your terminal.
-.PP
-If your terminal does not tell Chawan that it can display sixels, you
-may also have to set \[lq]display.image\-mode\[rq] appropriately.
+true:
+.IP
+.EX
+\f[I]# in \[ti]/.chawan/config.toml (or \[ti]/.config/chawan/config.toml)\f[R]
+\f[B][buffer]\f[R]
+images = true
+.EE
+.PP
+With the default image\-mode, Chawan will find the best image display
+method supported by your terminal.
+However, if your terminal fails to tell Chawan that it can display
+sixels, you may also have to set \[lq]display.image\-mode\[rq]
+appropriately.
See below for further discussion of sixel configuration.
.SS Output formats
Supported output formats are:
@@ -73,6 +81,8 @@ On slow computers, you can try setting
Transparency \f[I]is\f[R] supported, but looks weird because we
approximate an 8\-bit alpha channel with Sixel\[cq]s 1\-bit alpha
channel.
+Also, some terminals don\[cq]t emulate it correctly \- when in doubt,
+try XTerm (which does).
.SS Kitty
On terminals that support it, Kitty\[cq]s protocol is preferred over
Sixel.
@@ -179,8 +189,8 @@ decode stdout, always
.IP \[bu] 2
encode stdin, always
.PP
-This makes it possible to mmap(2) stdin/stdout instead of streaming
-through them with read(2) and write(2).
+This makes it possible to mmap(3) stdin/stdout instead of streaming
+through them with read(3) and write(3).
When doing this, mind the following:
.IP \[bu] 2
When reading, you must check your initial position in the file with
@@ -193,3 +203,7 @@ This \f[I]is\f[R] an implementation detail, and might change at any time
in the future (e.g.\ if we add a \[lq]no cache files\[rq] mode).
Always check for S_ISREG to ensure that you are actually dealing with a
file.
+(Use io/dynstream.nim\[cq]s recvDataLoopOrMmap and maybeMmapForSend to
+deal with this automatically.)
+.SS See also
+\f[B]cha\f[R](1)
diff --git a/doc/cha-localcgi.5 b/doc/cha-localcgi.5
index 08f0e42b..5653e74f 100644
--- a/doc/cha-localcgi.5
+++ b/doc/cha-localcgi.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-localcgi" "5" "" "" "Local CGI support in Chawan"
.SH Local CGI support in Chawan
@@ -9,6 +9,8 @@ All local CGI scripts must be placed in a directory specified in
\f[CR]external.cgi\-dir\f[R].
Multiple directories can be specified in an array too, and directories
specified first have higher precedence.
+By default, this is set to \[ti]/.chawan/cgi\-bin and
+/usr/local/libexec/chawan/cgi\-bin.
.IP \[bu] 2
Then, a CGI script in one of these directories can be executed by
visiting the URL \f[CR]cgi\-bin:script\-name\f[R].
@@ -188,6 +190,10 @@ if this results in it returning an error.
\f[CR]HTTP_COOKIE=\f[R] if set, the Cookie header.
.IP \[bu] 2
\f[CR]HTTP_REFERER=\f[R] if set, the Referer header.
+.IP \[bu] 2
+\f[CR]CHA_TMP_DIR=\f[R] directory used for storing temporary files.
+.IP \[bu] 2
+\f[CR]CHA_CONFIG_DIR=\f[R] location of the config file.
.PP
For requests originating from a urimethodmap rewrite, Chawan will also
set the parsed URL\[cq]s parts as environment variables.
diff --git a/doc/cha-mailcap.5 b/doc/cha-mailcap.5
index 11abde02..d6c62df8 100644
--- a/doc/cha-mailcap.5
+++ b/doc/cha-mailcap.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-mailcap" "5" "" "" "Mailcap support in Chawan"
.SH Mailcap
diff --git a/doc/cha-mime.types.5 b/doc/cha-mime.types.5
index 3a228b56..0ee025da 100644
--- a/doc/cha-mime.types.5
+++ b/doc/cha-mime.types.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-config" "5" "" "" "MIME type detection in Chawan"
.SH mime.types
diff --git a/doc/cha-protocols.5 b/doc/cha-protocols.5
index 4cbb64ac..33a6b863 100644
--- a/doc/cha-protocols.5
+++ b/doc/cha-protocols.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-protocols" "5" "" "" "Protocol support in Chawan"
.SH Protocols
@@ -6,10 +6,15 @@ Chawan supports downloading resources from various protocols: HTTP, FTP,
Gopher, Gemini, and Finger.
Details on these protocols, and information on how users can add support
to their preferred protocols is outlined in this document.
+.PP
+In general, you can find network adapters in the
+\f[CR]adapter/protocol\f[R] directory.
+For protocol\-specific file formats (like gemtext or gopher directories)
+you will also find an appropriate HTML converter in
+\f[CR]adapter/format\f[R].
.SS HTTP
HTTP/s support is based on libcurl; supported features largely depend on
your libcurl version.
-The adapter is found at \f[CR]adapter/protocol/http.nim\f[R].
.PP
The libcurl HTTP adapter can take arbitrary headers and POST data, is
able to use passed userinfo data
@@ -27,33 +32,32 @@ Note that for curl\-impersonate to work, you must set
(Otherwise, the libcurl adapter will happily override curl\-impersonate
headers, which is probably not what you want.)
.PP
-The \f[CR]bonus/libfetch\f[R] directory contains an alternative HTTP
-client, which is based on FreeBSD libfetch.
+The \f[CR]bonus\f[R] directory contains an alternative HTTP client based
+on FreeBSD libfetch.
It is mostly a proof of concept, as FreeBSD libfetch HTTP support is
-very limited; in particular, it does not support HTTP headers (beyond
-some basic request headers), so e.g.\ cookies will not work.
+very limited; in particular, it does not support arbitrary HTTP headers,
+so e.g.
+cookies will not work.
.SS FTP
-Chawan supports FTP through the \f[CR]adapter/protocol/ftp.nim\f[R]
-adapter.
-Only passive mode is supported.
+Chawan supports FTP passive mode browsing and downloads.
.PP
For directory listings, it assumes UNIX output style, and will probably
break horribly on receiving anything else.
Otherwise, the directory listing view is identical to the file://
directory listing.
.SS SFTP
-The sftp adapter wraps libcurl.
+The sftp adapter (\f[CR]adapter/protocol/sftp.nim\f[R]) wraps libcurl.
It works for me, but YMMV.
+.PP
Note that if an IdentityFile declaration is found in your ssh config,
then it will prompt for the identity file password, but there is no way
-to tell whether it is really asking for that.
+to tell whether it is really asking for that (or just normal password
+auth).
Also, settings covered by the Match field are ignored.
.SS Gopher
-Gopher is supported through the \f[CR]adapter/protocol/gopher.nim\f[R]
-adapter.
+Chawan supports the Gopher protocol through the gopher CGI program.
Gopher directories are passed as the \f[CR]text/gopher\f[R] type, and
-\f[CR]adapter/format/gopher.nim\f[R] takes care of converting this to
-HTML.
+gopher2html takes care of converting this to HTML.
.PP
Gopher selector types are converted to MIME types when possible; note
however, that this is very limited, as most of them (like \f[CR]s\f[R]
@@ -62,38 +66,25 @@ some other sniffing method.
Chawan will fall back to extension\-based detection in these cases, and
in the worst case may end up with \f[CR]application/octet\-stream\f[R].
.SS Gemini
-Chawan\[cq]s gemini adapter (in \f[CR]adapter/protocol/gmifetch.c\f[R])
-is a C program.
-It requires OpenSSL to work.
-.PP
-It still has some limitations:
-.IP \[bu] 2
-It does not support proxies yet.
-.IP \[bu] 2
-It does not support sites that require private key authentication.
-.PP
-\f[CR]adapter/format/gmi2html.nim\f[R] is its companion program to
-convert the \f[CR]text/gemini\f[R] file format to HTML.
-Note that the gemtext specification insists on line breaks being
-visually significant, and forbids their collapsing onto a single line;
-gmi2html respects this.
-However, inline whitespace is still collapsed outside of preformatted
-blocks.
+Chawan\[cq]s Gemini adapter has been rewritten as a Nim program.
+It still requires OpenSSL to work.
+.PP
+A limitation that remains is that the Gemini adapter does not support
+sites that require private key authentication.
+.PP
+gmi2html is its companion program to convert the \f[CR]text/gemini\f[R]
+file format to HTML.
.SS Finger
-Finger is supported through the \f[CR]adapter/protocol/cha\-finger\f[R]
-shell script.
+Finger is supported through the \f[CR]finger\f[R] shell script.
It is implemented as a shell script because of the protocol\[cq]s
simplicity.
-cha\-finger uses the \f[CR]curl\f[R] program\[cq]s telnet:// protocol to
-make requests.
-As such, it will not work if \f[CR]curl\f[R] is not installed.
.PP
-Aspiring protocol adapter writers are encouraged to study cha\-finger
-for a simple example of how a custom protocol handler could be written.
+For portability, \f[CR]finger\f[R] uses Chawan\[cq]s \f[CR]nc\f[R] tool
+(a very limited netcat clone) to make requests.
.SS Spartan
Spartan is a protocol similar to Gemini, but without TLS.
-It is supported through the \f[CR]adapter/protocol/spartan\f[R] shell
-script, which uses \f[CR]nc\f[R] to make requests.
+It is supported through the \f[CR]spartan\f[R] shell script, and like
+Finger, it uses Chawan\[cq]s \f[CR]nc\f[R] to make requests.
.PP
Spartan has the very strange property of extending gemtext with a
protocol\-specific line type.
@@ -198,21 +189,8 @@ Here we will add a protocol called \[lq]cowsay\[rq], so that the URL
cowsay:text prints the output of \f[CR]cowsay text\f[R] after a second
of waiting.
.PP
-First, make sure you have a local CGI path \f[CR]\[ti]/cgi\-bin\f[R] set
-up in your \f[CR]\[ti]/.config/chawan/config.toml\f[R]:
-.IP
-.EX
-cgi\-dir = [\[dq]\[ti]/cgi\-bin\[dq], \[dq]${%CHA_LIBEXEC_DIR}/cgi\-bin\[dq]]
-.EE
-.PP
-It is also possible to just put your CGI scripts to
-\f[CR]/usr/local/libexec/chawan/cgi\-bin\f[R]; this is enabled by
-default, so you need no edits in your config.
-But it seems more convenient to use a dedicated cgi\-bin in your home
-directory.
-.PP
-\f[CR]mkdir \[ti]/cgi\-bin\f[R], and create a CGI script in
-\f[CR]\[ti]/cgi\-bin\f[R] called \f[CR]cowsay.cgi\f[R]:
+\f[CR]mkdir \-p \[ti]/.chawan/cgi\-bin\f[R], and create a CGI script in
+\f[CR]\[ti]/.chawan/cgi\-bin/cowsay.cgi\f[R]:
.IP
.EX
\f[I]#!/bin/sh\f[R]
diff --git a/doc/cha-troubleshooting.5 b/doc/cha-troubleshooting.5
index 3748c99c..dfdf42c2 100644
--- a/doc/cha-troubleshooting.5
+++ b/doc/cha-troubleshooting.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-troubleshooting" "5" "" "" "Troubleshooting Chawan"
.SH Troubleshooting Chawan
@@ -31,9 +31,6 @@ you can correct this using the
variables.
.PP
See config.md for details.
-.SS Can I view Markdown files using Chawan?
-Yes; Chawan now has a built\-in markdown converter.
-If you don\[cq]t like it, you can always replace it with e.g.\ pandoc.
.SS I set my \f[CR]$PAGER\f[R] to \f[CR]cha\f[R] and now man pages are unreadable.
Most \f[CR]man\f[R] implementations print formatted manual pages by
default, which Chawan \f[I]can\f[R] parse if they are passed through
@@ -65,15 +62,6 @@ exec cha \-T text/x\-ansi \[dq]$\[at]\[dq]
.EE
.PP
and \f[CR]export PAGER=pcha\f[R].
-.SS Where are the keybindings?
-Please run \f[CR]cha about:chawan\f[R] for a list of default
-keybindings.
-Users familiar with \f[I]vi\f[R], \f[I]vim\f[R], etc.
-should find these defaults familiar.
-.PP
-A w3m\-like keymap also exists at bonus/w3m.toml.
-Note that not every w3m feature is implemented yet, so it\[cq]s not 100%
-compatible.
.SS How do I view text files with wrapping?
By default, text files are not auto\-wrapped, so viewing plain text
files that were not wrapped properly by the authors is somewhat
@@ -146,3 +134,5 @@ follow step 3.
of the previous answer.
Warning: remote JavaScript execution is inherently unsafe.
Please only enable JavaScript on websites you trust.
+.SS See also
+\f[B]cha\f[R](1)
diff --git a/doc/cha-urimethodmap.5 b/doc/cha-urimethodmap.5
index 1347bcc0..015903ee 100644
--- a/doc/cha-urimethodmap.5
+++ b/doc/cha-urimethodmap.5
@@ -1,4 +1,4 @@
-.\" Automatically generated by Pandoc 3.4
+.\" Automatically generated by Pandoc 3.5
.\"
.TH "cha\-urimethodmap" "5" "" "" "URI method map support in Chawan"
.SH URI method map support in Chawan
@@ -89,7 +89,7 @@ the user\[cq]s configuration.)
# Following sets the urimethodmap search path to the path relative to the
# configuration file. So if your configuration file is in
# \[ti]/.config/chawan/config.toml, Chawan will use \[ti]/.config/chawan/urimethodmap.
-# in the same directory.
+# If it\[aq]s in \[ti]/.chawan/config.toml, then it uses \[ti]/.chawan/urimethodmap.
[external]
urimethodmap = \[dq]urimethodmap\[dq]
.EE
diff --git a/doc/cha.1 b/doc/cha.1
index 12ba2236..2813dcc5 100644
--- a/doc/cha.1
+++ b/doc/cha.1
@@ -110,6 +110,9 @@ When set, Chawan starts in visual mode by default and opens the page specified
by one of these variables. (\fBHTTP_HOME\fR takes precedence over
\fBWWW_HOME\fR.)
.TP
+\fBCHA_CONFIG_DIR\fR
+When set, it switches the configuration directory to the path specified.
+.TP
\fBCOLORTERM\fR
When set to \fI24bit\fR or \fItruecolor\fR, and the \fIdisplay.color-mode\fR
configuration option is set to \fIauto\fR, Chawan sets the color mode to
diff --git a/doc/config.md b/doc/config.md
index 1d5081d8..ee4120ce 100644
--- a/doc/config.md
+++ b/doc/config.md
@@ -5,8 +5,8 @@ MANOFF -->
# Configuration of Chawan
Chawan supports configuration of various options like keybindings, user
-stylesheets, site preferences, etc. The configuration format is very similar
-to toml, with the following exceptions:
+stylesheets, site preferences, etc. The configuration format is almost
+toml, with the following exceptions:
* Inline tables may span across multiple lines.
* Table arrays can be cleared by setting a variable by the same to the
@@ -19,11 +19,14 @@ omnirule = [] # note: this must be placed at the beginning of the file.
[[omnirule]] # this is legal. all default omni-rules are now disabled.
```
-Chawan will look for a config file in the $XDG_CONFIG_HOME/chawan/ directory
-called `config.toml`. (Chawan defaults to ~/.config if the XDG_CONFIG_HOME
-environment variable is not set.) See the default configuration file in the
-res/ folder, and bonus configuration files in the bonus/ folder for further
-examples.
+The canonical configuration file path is ~/.chawan/config.toml, but
+the search path accomodates XDG basedirs as well:
+
+1. config file specified through -C switch -> use that
+2. $CHA_CONFIG_DIR is set -> use $CHA_CONFIG_DIR/config.toml
+3. $XDG_CONFIG_HOME is set -> use $XDG_CONFIG_HOME/chawan/config.toml
+4. ~/.config/chawan/config.toml exists -> use that
+5. ~/.chawan/config.toml exists -> use that
<!-- MANOFF -->
**Table of contents**
diff --git a/doc/image.md b/doc/image.md
index c707d160..a5e97e0e 100644
--- a/doc/image.md
+++ b/doc/image.md
@@ -19,7 +19,7 @@ There are actually two switches for images in the config:
In most cases, all you need to do is to set "buffer.images" to true:
```toml
-# in ~/.config/chawan/config.toml
+# in ~/.chawan/config.toml (or ~/.config/chawan/config.toml)
[buffer]
images = true
```
diff --git a/doc/localcgi.md b/doc/localcgi.md
index a266f2c4..e526ce35 100644
--- a/doc/localcgi.md
+++ b/doc/localcgi.md
@@ -9,7 +9,9 @@ used in the following way:
* All local CGI scripts must be placed in a directory specified in
`external.cgi-dir`. Multiple directories can be specified in an array too,
- and directories specified first have higher precedence.
+ and directories specified first have higher precedence.
+ By default, this is set to ~/.chawan/cgi-bin and
+ /usr/local/libexec/chawan/cgi-bin.
* Then, a CGI script in one of these directories can be executed by visiting
the URL `cgi-bin:script-name`. $PATH_INFO and $QUERY_STRING are set as
normal, i.e. `cgi-bin:script-name/abcd?defgh=ijkl` will set $PATH_INFO to
diff --git a/doc/protocols.md b/doc/protocols.md
index dbf271bf..4b3064eb 100644
--- a/doc/protocols.md
+++ b/doc/protocols.md
@@ -197,19 +197,8 @@ of the retrieved resource. If an error is encountered, it prints a
Here we will add a protocol called "cowsay", so that the URL cowsay:text
prints the output of `cowsay text` after a second of waiting.
-First, make sure you have a local CGI path `~/cgi-bin` set up in your
-`~/.config/chawan/config.toml`:
-
-```toml
-cgi-dir = ["~/cgi-bin", "${%CHA_LIBEXEC_DIR}/cgi-bin"]
-```
-
-It is also possible to just put your CGI scripts to
-`/usr/local/libexec/chawan/cgi-bin`; this is enabled by default, so you need no
-edits in your config. But it seems more convenient to use a dedicated cgi-bin in
-your home directory.
-
-`mkdir ~/cgi-bin`, and create a CGI script in `~/cgi-bin` called `cowsay.cgi`:
+`mkdir -p ~/.chawan/cgi-bin`, and create a CGI script in
+`~/.chawan/cgi-bin/cowsay.cgi`:
```sh
#!/bin/sh
diff --git a/doc/urimethodmap.md b/doc/urimethodmap.md
index a2047eb2..f2af3c3a 100644
--- a/doc/urimethodmap.md
+++ b/doc/urimethodmap.md
@@ -91,7 +91,7 @@ w3mdict.cgi in the file viewer. (Unlike in w3m, where it could run
# Following sets the urimethodmap search path to the path relative to the
# configuration file. So if your configuration file is in
# ~/.config/chawan/config.toml, Chawan will use ~/.config/chawan/urimethodmap.
-# in the same directory.
+# If it's in ~/.chawan/config.toml, then it uses ~/.chawan/urimethodmap.
[external]
urimethodmap = "urimethodmap"
```
diff --git a/res/config.toml b/res/config.toml
index 56847db7..9fa79079 100644
--- a/res/config.toml
+++ b/res/config.toml
@@ -269,7 +269,7 @@ urimethodmap = [
tmpdir = "/tmp/cha-tmp-$LOGNAME"
sockdir = "/tmp/cha-sock-$LOGNAME"
editor = "${EDITOR:-vi}"
-cgi-dir = "${%CHA_LIBEXEC_DIR}/cgi-bin"
+cgi-dir = ["$CHA_CONFIG_DIR/cgi-bin", "$CHA_LIBEXEC_DIR/cgi-bin"]
download-dir = "/tmp/"
w3m-cgi-compat = false
diff --git a/src/config/config.nim b/src/config/config.nim
index ad9510fb..86220fa4 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -160,7 +160,7 @@ type
meta_refresh* {.jsgetset.}: MetaRefresh
Config* = ref object
- jsctx: JSContext
+ jsctx*: JSContext
jsvfns*: seq[JSValueFunction]
configdir* {.jsget.}: string
`include` {.jsget.}: seq[ChaPathResolved]
@@ -668,21 +668,13 @@ proc parseConfigValue(ctx: var ConfigParser; x: var CommandConfig; v: TomlValue;
else: # tvtString
x.init.add((kkk.substr("cmd.".len), vv.s))
-type ParseConfigResult* = object
- success*: bool
- warnings*: seq[string]
- errorMsg*: string
+proc parseConfig*(config: Config; dir, buf: string; warnings: var seq[string];
+ name = "<input>"; laxnames = false): Err[string]
-proc parseConfig*(config: Config; dir, buf: string; name = "<input>";
- laxnames = false; setdir = true): ParseConfigResult
-
-proc parseConfig(config: Config; dir: string; t: TomlValue; setdir: bool):
- ParseConfigResult =
+proc parseConfig(config: Config; dir: string; t: TomlValue;
+ warnings: var seq[string]): Err[string] =
var ctx = ConfigParser(config: config, dir: dir)
- if setdir:
- config.configdir = dir
try:
- var myRes = ParseConfigResult(success: true)
ctx.parseConfigValue(config[], t, "")
#TODO: for omnirule/siteconf, check if substitution rules are specified?
while config.`include`.len > 0:
@@ -692,34 +684,19 @@ proc parseConfig(config: Config; dir: string; t: TomlValue; setdir: bool):
for s in includes:
let fs = openFileExpand(dir, s)
if fs == nil:
- return ParseConfigResult(
- success: false,
- warnings: ctx.warnings,
- errorMsg: "include file not found: " & s
- )
- let res = config.parseConfig(dir, fs.readAll())
- if not res.success:
- return res
- myRes.warnings.add(res.warnings)
- myRes.warnings.add(ctx.warnings)
- return myRes
+ return err("include file not found: " & s)
+ ?config.parseConfig(dir, fs.readAll(), warnings)
+ warnings.add(ctx.warnings)
+ return ok()
except ValueError as e:
- return ParseConfigResult(
- success: false,
- warnings: ctx.warnings,
- errorMsg: e.msg
- )
-
-proc parseConfig*(config: Config; dir, buf: string; name = "<input>";
- laxnames = false; setdir = true): ParseConfigResult =
+ return err(e.msg)
+
+proc parseConfig*(config: Config; dir, buf: string; warnings: var seq[string];
+ name = "<input>"; laxnames = false): Err[string] =
let toml = parseToml(buf, dir / name, laxnames)
if toml.isSome:
- return config.parseConfig(dir, toml.get, setdir)
- else:
- return ParseConfigResult(
- success: false,
- errorMsg: "Fatal error: failed to parse config\n" & toml.error & '\n'
- )
+ return config.parseConfig(dir, toml.get, warnings)
+ return err("Fatal error: failed to parse config\n" & toml.error)
proc getNormalAction*(config: Config; s: string): string =
return config.page.getOrDefault(s)
@@ -727,39 +704,26 @@ proc getNormalAction*(config: Config; s: string): string =
proc getLinedAction*(config: Config; s: string): string =
return config.line.getOrDefault(s)
-type ReadConfigResult = tuple
- config: Config
- res: ParseConfigResult
-
-const defaultConfig = staticRead"res/config.toml"
-
-proc readConfig0(config: Config; dir, name: string): ParseConfigResult =
- let path = if name.len > 0 and name[0] == '/':
- name
- else:
- dir / name
- let fs = newFileStream(path)
- if fs != nil:
- return config.parseConfig(parentDir(path), fs.readAll())
- return ParseConfigResult(success: true)
-
-proc readConfig*(pathOverride: Option[string]; jsctx: JSContext):
- ReadConfigResult =
- let config = Config(jsctx: jsctx)
- var res = config.parseConfig("res", defaultConfig)
- if not res.success:
- return (nil, res)
- if pathOverride.isNone:
- when defined(debug):
- res = config.readConfig0(getCurrentDir() / "res", "config.toml")
- if not res.success:
- return (nil, res)
- res = config.readConfig0(getConfigDir() / "chawan", "config.toml")
- else:
- res = config.readConfig0(getCurrentDir(), pathOverride.get)
- if not res.success:
- return (nil, res)
- return (config, res)
+proc openConfig*(dir: var string; override: Option[string]): FileStream =
+ if override.isSome:
+ if override.get.len > 0 and override.get[0] == '/':
+ dir = parentDir(override.get)
+ return newFileStream(override.get)
+ else:
+ dir = getCurrentDir()
+ return newFileStream(dir / override.get)
+ dir = getEnv("CHA_CONFIG_DIR")
+ if dir != "":
+ return newFileStream(dir / "config.toml")
+ dir = getEnv("XDG_CONFIG_HOME")
+ if dir != "":
+ dir = dir / "chawan"
+ return newFileStream(dir / "config.toml")
+ dir = expandTilde("~/.config/chawan")
+ if (let fs = newFileStream(dir / "config.toml"); fs != nil):
+ return fs
+ dir = expandTilde("~/.chawan")
+ return newFileStream(dir / "config.toml")
# called after parseConfig returns
proc initCommands*(config: Config): Err[string] =
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 44ffaa94..d41818fb 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -1168,7 +1168,8 @@ proc runCommand(pager: Pager; cmd: string; suspend, wait: bool;
newPosixStream(STDERR_FILENO).safeClose()
newPosixStream(STDIN_FILENO).safeClose()
else:
- discard dup2(pager.term.istream.fd, STDIN_FILENO)
+ if pager.term.istream != nil:
+ discard dup2(pager.term.istream.fd, STDIN_FILENO)
discard execl("/bin/sh", "sh", "-c", cstring(cmd), nil)
exitnow(127)
else:
diff --git a/src/main.nim b/src/main.nim
index 4f39a5f3..f7d6a87a 100644
--- a/src/main.nim
+++ b/src/main.nim
@@ -3,6 +3,7 @@ import version
import std/options
import std/os
import std/posix
+import std/streams
import chagashi/charset
import config/chapath
@@ -200,6 +201,28 @@ proc parse(ctx: var ParamParseContext) =
ctx.pages.add(param)
inc ctx.i
+const defaultConfig = staticRead"res/config.toml"
+
+proc initConfig(ctx: ParamParseContext; config: Config;
+ warnings: var seq[string]): Err[string] =
+ let fs = openConfig(config.configdir, ctx.configPath)
+ if fs == nil and ctx.configPath.isSome:
+ # The user specified a non-existent config file.
+ return err("Failed to open config file " & ctx.configPath.get)
+ putEnv("CHA_CONFIG_DIR", config.configdir)
+ ?config.parseConfig("res", defaultConfig, warnings)
+ when defined(debug):
+ if (let fs = newFileStream(getCurrentDir() / "res/config.toml"); fs != nil):
+ ?config.parseConfig(getCurrentDir(), fs.readAll(), warnings)
+ if fs != nil:
+ ?config.parseConfig(config.configdir, fs.readAll(), warnings)
+ for opt in ctx.opts:
+ ?config.parseConfig(getCurrentDir(), opt, warnings, laxnames = true)
+ config.css.stylesheet &= ctx.stylesheet
+ ?config.initCommands()
+ isCJKAmbiguous = config.display.double_width_ambiguous
+ return ok()
+
proc main() =
putEnv("CHA_LIBEXEC_DIR", ChaPath"${%CHA_LIBEXEC_DIR}".unquoteGet())
let forkserver = newForkServer()
@@ -208,25 +231,10 @@ proc main() =
let jsrt = newJSRuntime()
let jsctx = jsrt.newJSContext()
var warnings = newSeq[string]()
- let (config, res) = readConfig(ctx.configPath, jsctx)
- if not res.success:
- stderr.writeLine(res.errorMsg)
+ let config = Config(jsctx: jsctx)
+ if (let res = ctx.initConfig(config, warnings); res.isNone):
+ stderr.writeLine(res.error)
quit(1)
- warnings.add(res.warnings)
- for opt in ctx.opts:
- let res = config.parseConfig(getCurrentDir(), opt, laxnames = true,
- setdir = false)
- if not res.success:
- stderr.writeLine(res.errorMsg)
- quit(1)
- warnings.add(res.warnings)
- config.css.stylesheet &= ctx.stylesheet
- block commands:
- let res = config.initCommands()
- if res.isNone:
- stderr.writeLine("Error parsing commands: " & res.error)
- quit(1)
- isCJKAmbiguous = config.display.double_width_ambiguous
if ctx.pages.len == 0 and stdin.isatty():
if ctx.visual:
ctx.pages.add(config.start.visual_home)
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 1f43c443..99e5f6ee 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -717,3 +717,9 @@ func btoa*(data: openArray[uint8]): string =
func btoa*(data: openArray[char]): string =
return btoa(data.toOpenArrayByte(0, data.len - 1))
+
+proc getEnvEmpty*(name: string; fallback = ""): string =
+ let res = getEnv(name, fallback)
+ if res != "":
+ return res
+ return fallback