about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-08-11 13:58:05 +0200
committerbptato <nincsnevem662@gmail.com>2024-08-11 13:58:05 +0200
commit4393412d971269055401f2e8a6be766f4402e193 (patch)
tree8ac55ed62d46bc91f431249434bb2483d3a35e4f
parent77aaafba20fd354625985bd65dce046cab759c63 (diff)
downloadchawan-4393412d971269055401f2e8a6be766f4402e193.tar.gz
urldec: merge into urlenc
also, move the ln command to make all
-rw-r--r--Makefile18
-rw-r--r--adapter/tools/urldec.nim6
-rw-r--r--adapter/tools/urlenc.nim34
3 files changed, 30 insertions, 28 deletions
diff --git a/Makefile b/Makefile
index 86179793..cfc1b71b 100644
--- a/Makefile
+++ b/Makefile
@@ -54,6 +54,7 @@ all: $(OUTDIR_BIN)/cha $(OUTDIR_BIN)/mancha $(OUTDIR_CGI_BIN)/http \
 	$(OUTDIR_CGI_BIN)/stbi $(OUTDIR_CGI_BIN)/jebp \
 	$(OUTDIR_LIBEXEC)/urldec $(OUTDIR_LIBEXEC)/urlenc \
 	$(OUTDIR_LIBEXEC)/md2html $(OUTDIR_LIBEXEC)/ansi2html
+	ln -sf "$(OUTDIR)/$(TARGET)/bin/cha" cha
 
 $(OUTDIR_BIN)/cha: src/*.nim src/**/*.nim src/**/*.c res/* res/**/* \
 		res/map/idna_gen.nim nim.cfg
@@ -61,7 +62,6 @@ $(OUTDIR_BIN)/cha: src/*.nim src/**/*.nim src/**/*.c res/* res/**/* \
 	$(NIMC) --nimcache:"$(OBJDIR)/$(TARGET)/cha" -d:libexecPath=$(LIBEXECDIR) \
                 -d:disableSandbox=$(DANGER_DISABLE_SANDBOX) $(FLAGS) \
 		-o:"$(OUTDIR_BIN)/cha" src/main.nim
-	ln -sf "$(OUTDIR)/$(TARGET)/bin/cha" cha
 
 $(OUTDIR_BIN)/mancha: adapter/tools/mancha.nim
 	@mkdir -p "$(OUTDIR_BIN)"
@@ -109,7 +109,6 @@ $(OUTDIR_CGI_BIN)/stbi: adapter/img/stbi.nim adapter/img/stb_image.c \
 		adapter/img/stb_image.h src/utils/sandbox.nim
 $(OUTDIR_CGI_BIN)/jebp: adapter/img/jebp.c adapter/img/jebp.h \
 		src/utils/sandbox.nim
-$(OUTDIR_LIBEXEC)/urldec: $(twtstr)
 $(OUTDIR_LIBEXEC)/urlenc: $(twtstr)
 $(OUTDIR_LIBEXEC)/gopher2html: adapter/gophertypes.nim $(twtstr)
 $(OUTDIR_LIBEXEC)/ansi2html: src/types/color.nim $(twtstr)
@@ -138,6 +137,9 @@ $(OUTDIR_LIBEXEC)/%: adapter/tools/%.nim
 	$(NIMC) $(FLAGS) --nimcache:"$(OBJDIR)/$(TARGET)/$(subst $(OUTDIR_LIBEXEC)/,,$@)" \
 		-o:"$@" $<
 
+$(OUTDIR_LIBEXEC)/urldec: $(OUTDIR_LIBEXEC)/urlenc
+	(cd "$(OUTDIR_LIBEXEC)"; ln -sf urlenc urldec)
+
 $(OBJDIR)/man/cha-%.md: doc/%.md md2manpreproc
 	@mkdir -p "$(OBJDIR)/man"
 	./md2manpreproc $< > $@
@@ -164,7 +166,7 @@ manpage: $(manpages:%=doc/%)
 
 protocols = http about file ftp gopher gmifetch cha-finger man spartan stbi jebp
 converters = gopher2html md2html ansi2html gmi2html
-tools = urldec urlenc
+tools = urlenc
 
 .PHONY: install
 install:
@@ -173,10 +175,14 @@ install:
 	install -m755 "$(OUTDIR_BIN)/mancha" "$(DESTDIR)$(PREFIX)/bin"
 # intentionally not quoted
 	mkdir -p $(LIBEXECDIR_CHAWAN)/cgi-bin
-	for f in $(protocols); \
-	do install -m755 "$(OUTDIR_CGI_BIN)/$$f" $(LIBEXECDIR_CHAWAN)/cgi-bin; done
+	for f in $(protocols); do \
+	install -m755 "$(OUTDIR_CGI_BIN)/$$f" $(LIBEXECDIR_CHAWAN)/cgi-bin; \
+	done
 	for f in $(converters) $(tools); \
-	do install -m755 "$(OUTDIR_LIBEXEC)/$$f" $(LIBEXECDIR_CHAWAN); done
+	do install -m755 "$(OUTDIR_LIBEXEC)/$$f" $(LIBEXECDIR_CHAWAN); \
+	done
+# urldec is just a symlink to urlenc
+	(cd $(LIBEXECDIR_CHAWAN); ln -sf urlenc urldec)
 	mkdir -p "$(DESTDIR)$(MANPREFIX1)"
 	for f in $(manpages1); do install -m644 "doc/$$f" "$(DESTDIR)$(MANPREFIX1)"; done
 	mkdir -p "$(DESTDIR)$(MANPREFIX5)"
diff --git a/adapter/tools/urldec.nim b/adapter/tools/urldec.nim
deleted file mode 100644
index b26fcb68..00000000
--- a/adapter/tools/urldec.nim
+++ /dev/null
@@ -1,6 +0,0 @@
-# Percent-decode input received on stdin.
-#TODO a streaming implementation of this could be useful
-
-import utils/twtstr
-
-stdout.write(percentDecode(stdin.readAll()))
diff --git a/adapter/tools/urlenc.nim b/adapter/tools/urlenc.nim
index 79d3e452..93a913b6 100644
--- a/adapter/tools/urlenc.nim
+++ b/adapter/tools/urlenc.nim
@@ -1,11 +1,12 @@
-# Percent-encode input received on stdin with a specified percent-encoding set.
+# Percent-encode or decode input received on stdin with a specified
+# percent-encoding set.
 #TODO a streaming implementation of this could be useful
 
 import std/os
 
 import utils/twtstr
 
-proc usage() =
+proc usage() {.noreturn.} =
   stderr.write("""
 Usage: urlenc [set]
 The input to be decoded is read from stdin.
@@ -18,24 +19,25 @@ The input to be decoded is read from stdin.
     component
     application-x-www-form-urlencoded
 """)
+  quit(1)
 
 proc main() =
-  if paramCount() != 1:
+  let isdec = paramStr(0).afterLast('/') == "urldec"
+  if not isdec and paramCount() != 1:
     usage()
-    quit(1)
   let s = stdin.readAll()
-  let enc = case paramStr(1)
-  of "control": percentEncode(s, ControlPercentEncodeSet)
-  of "fragment": percentEncode(s, FragmentPercentEncodeSet)
-  of "query": percentEncode(s, QueryPercentEncodeSet)
-  of "path": percentEncode(s, PathPercentEncodeSet)
-  of "userinfo": percentEncode(s, UserInfoPercentEncodeSet)
-  of "component": percentEncode(s, ComponentPercentEncodeSet)
-  of "application-x-www-form-urlencoded":
-    percentEncode(s, ApplicationXWWWFormUrlEncodedSet)
+  if isdec:
+    stdout.write(s.percentDecode())
   else:
-    usage()
-    quit(1)
-  stdout.write(enc)
+    let set = case paramStr(1)
+    of "control": ControlPercentEncodeSet
+    of "fragment": FragmentPercentEncodeSet
+    of "query": QueryPercentEncodeSet
+    of "path": PathPercentEncodeSet
+    of "userinfo": UserInfoPercentEncodeSet
+    of "component": ComponentPercentEncodeSet
+    of "application-x-www-form-urlencoded": ApplicationXWWWFormUrlEncodedSet
+    else: usage()
+    stdout.write(s.percentEncode(set))
 
 main()