Use duck for util-io.

This commit is contained in:
Dominik Pantůček 2023-07-05 21:38:04 +02:00
parent 375c4ed857
commit 201093ab8c
5 changed files with 102 additions and 48 deletions

View file

@ -124,3 +124,62 @@ Returns a dictionary with the following keys:
Returns annotated source with information about originating commits Returns annotated source with information about originating commits
for each line. for each line.
## util-io [module]
(import util-io)
Module implementing advanced I/O.
### read-lines/no-bom [procedure]
(read-lines/no-bom ip)
* ```port``` - an input port
Reads lines using ```read-lines``` and if the first line contains
UTF-8 BOM, removes it.
### get-process-output-lines [procedure]
(get-process-output-lines cmd
. args)
* ```cmd``` - a string with the command
* ```args``` - list of arguments to pass to process
Returns a list of strings representing all the lines produced by
running the command given.
### get-process-exit+output-lines [procedure]
(get-process-exit+output-lines cmd
. args)
* ```cmd``` - a string with the command
* ```args``` - list of arguments to pass to process
Returns two values - an exit code and a list of strings representing
all the lines produced by running the command given.
### process-send/recv [procedure]
(process-send/recv cmd
args
. lines)
* ```cmd``` - a string with command
* ```args``` - list of arguments
* ```lines``` - lines to feed to stdin of the process
Executes given command ```cmd``` with given argument list ```args```
writing all ```lines``` to its standard input and then reads all the
process output.

View file

@ -139,45 +139,6 @@ Converts given number to a string with two-digit fractional
part. Should the fractional part be 0, it is replaced with the string part. Should the fractional part be 0, it is replaced with the string
"--". "--".
### IO
(import util-io)
Module implementing advanced I/O.
(read-lines/no-bom port)
* ```port``` - an input port
Reads lines using ```read-lines``` and if the first line contains
UTF-8 BOM, removes it.
(get-process-output-lines cmd . args)
* ```cmd``` - a string with the command
* ```args``` - list of arguments to pass to process
Returns a list of strings representing all the lines produced by
running the command given.
(get-process-exit+output-lines cmd . args)
* ```cmd``` - a string with the command
* ```args``` - list of arguments to pass to process
Returns two values - an exit code and a list of strings representing
all the lines produced by running the command given.
(process-send/recv cmd args . lines)
* ```cmd``` - a string with command
* ```args``` - list of arguments
* ```lines``` - lines to feed to stdin of the process
Executes given command ```cmd``` with given argument list ```args```
writing all ```lines``` to its standard input and then reads all the
process output.
### List ### List
(import util-list) (import util-list)

View file

@ -59,10 +59,11 @@ HACKERBASE-OBJS=hackerbase.o testing.o listing.o cal-month.o \
export-web-static.o util-dir.o dokuwiki.o racket-kwargs.o export-web-static.o util-dir.o dokuwiki.o racket-kwargs.o
GENDOC-SOURCES=gendoc.scm duck-extract.import.scm \ GENDOC-SOURCES=gendoc.scm duck-extract.import.scm \
util-time.import.scm util-csv.import.scm util-git.import.scm util-time.import.scm util-csv.import.scm util-git.import.scm \
util-io.import.scm
GENDOC-OBJS=gendoc.o duck-extract.o util-time.o util-csv.o util-io.o \ GENDOC-OBJS=gendoc.o duck-extract.o util-time.o util-csv.o util-io.o \
progress.o testing.o util-proc.o util-git.o progress.o testing.o util-proc.o util-git.o util-io.o
.PHONY: imports .PHONY: imports
imports: $(HACKERBASE-DEPS) imports: $(HACKERBASE-DEPS)
@ -285,7 +286,7 @@ UTIL-TAG-SOURCES=util-tag.scm testing.import.scm
util-tag.o: util-tag.import.scm util-tag.o: util-tag.import.scm
util-tag.import.scm: $(UTIL-TAG-SOURCES) util-tag.import.scm: $(UTIL-TAG-SOURCES)
UTIL-IO-SOURCES=util-io.scm UTIL-IO-SOURCES=util-io.scm duck.import.scm
util-io.o: util-io.import.scm util-io.o: util-io.import.scm
util-io.import.scm: $(UTIL-IO-SOURCES) util-io.import.scm: $(UTIL-IO-SOURCES)

View file

@ -22,4 +22,5 @@
"These are various utility modules for other HackerBase libraries." "These are various utility modules for other HackerBase libraries."
util-time util-time
util-csv util-csv
util-git) util-git
util-io)

View file

@ -25,8 +25,11 @@
(declare (unit util-io)) (declare (unit util-io))
(module (import duck)
(module*
util-io util-io
#:doc ("Module implementing advanced I/O.")
( (
read-lines/no-bom read-lines/no-bom
get-process-output-lines get-process-output-lines
@ -51,7 +54,13 @@
;; Reads lines from given input port, discarding BOM at the beginning ;; Reads lines from given input port, discarding BOM at the beginning
;; of the first line if there is any. ;; of the first line if there is any.
(define (read-lines/no-bom ip) (define/doc (read-lines/no-bom ip)
("
* ```port``` - an input port
Reads lines using ```read-lines``` and if the first line contains
UTF-8 BOM, removes it.
")
(let ((lines (read-lines ip))) (let ((lines (read-lines ip)))
(if (null? lines) (if (null? lines)
lines lines
@ -60,7 +69,14 @@
;; Very simple shell command wrapper that returns lines produced by ;; Very simple shell command wrapper that returns lines produced by
;; given command. ;; given command.
(define (get-process-output-lines cmd . args) (define/doc (get-process-output-lines cmd . args)
("
* ```cmd``` - a string with the command
* ```args``` - list of arguments to pass to process
Returns a list of strings representing all the lines produced by
running the command given.
")
(let-values (((stdout stdin pid stderr) (let-values (((stdout stdin pid stderr)
(process* cmd (process* cmd
(map (lambda (x) (map (lambda (x)
@ -73,7 +89,14 @@
;; Very simple shell command wrapper that returns lines produced by ;; Very simple shell command wrapper that returns lines produced by
;; given command. ;; given command.
(define (get-process-exit+output-lines cmd . args) (define/doc (get-process-exit+output-lines cmd . args)
("
* ```cmd``` - a string with the command
* ```args``` - list of arguments to pass to process
Returns two values - an exit code and a list of strings representing
all the lines produced by running the command given.
")
(let-values (((stdout stdin pid stderr) (let-values (((stdout stdin pid stderr)
(process* cmd (process* cmd
(map (lambda (x) (map (lambda (x)
@ -86,7 +109,16 @@
;; Invokes given command with given arguments, gives it all input ;; Invokes given command with given arguments, gives it all input
;; lines and returns the output lines. ;; lines and returns the output lines.
(define (process-send/recv cmd args . lines) (define/doc (process-send/recv cmd args . lines)
("
* ```cmd``` - a string with command
* ```args``` - list of arguments
* ```lines``` - lines to feed to stdin of the process
Executes given command ```cmd``` with given argument list ```args```
writing all ```lines``` to its standard input and then reads all the
process output.
")
(let-values (((stdout stdin pid stderr) (process* cmd args))) (let-values (((stdout stdin pid stderr) (process* cmd args)))
(let loop ((lines lines)) (let loop ((lines lines))
(when (not (null? lines)) (when (not (null? lines))