* guile-lib: new (string posix) module, filename resolution procedures - proposal for inclusion
[not found] <87wo1mwkwx.fsf@web.de>
@ 2020-08-25 21:14 ` Dr. Arne Babenhauserheide
0 siblings, 0 replies; only message in thread
From: Dr. Arne Babenhauserheide @ 2020-08-25 21:14 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 3839 bytes --]
* (filename-resolve-user filename)
resolves ~ and ~user in string filename
* (filename-resolve-variables filename)
resolves variables in string filename
Please comment!
I used the LGPL v2 or later to add the option to include into Guile
later on. I would like to hear your opinions on that.
---
src/Makefile.am | 1 +
src/string/posix.scm | 79 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 80 insertions(+)
create mode 100644 src/string/posix.scm
diff --git a/src/Makefile.am b/src/Makefile.am
index 889a575..0425bd8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -93,6 +93,7 @@ SOURCES = \
scheme/kwargs.scm \
search/basic.scm \
string/completion.scm \
+ string/posix.scm \
string/soundex.scm \
string/transform.scm \
string/wrap.scm \
diff --git a/src/string/posix.scm b/src/string/posix.scm
new file mode 100644
index 0000000..5370657
--- /dev/null
+++ b/src/string/posix.scm
@@ -0,0 +1,79 @@
+;;; (string posix) -- posix string operations
+
+;; Copyright (C) 2020 Dr. Arne Babenhauserheide <arne_bab@web.de> and Leo Prikler
+
+;; This library is free software; you can redistribute it and/or
+;; modify it under the terms of the GNU Lesser General Public
+;; License as published by the Free Software Foundation; either
+;; version 2 of the License, or (at your option) any later version.
+
+;; This library is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;; Lesser General Public License for more details.
+
+;; You should have received a copy of the GNU Lesser General Public
+;; License along with this library. If not, see
+;; <http://www.gnu.org/licenses/>.
+#!
+;;; Commentary:
+Resolving ~ in filenames following POSIX rules.
+;;; Code:
+!#
+
+(define-module (string posix)
+ #:export (filename-resolve-user filename-resolve-variables)
+ #:use-module (ice-9 regex))
+
+(define (filename-resolve-user filename)
+ "Replaces ~ and ~user in string FILENAME as by posix rules.
+
+@lisp
+ (filename-resolve-user \"~\")
+=> (getenv \"HOME\") or /home/user or ~
+ (filename-resolve-user \"~other\")
+=> \"/home/other\" (or whatever dir entry other has in passwd
+@end lisp
+"
+ (cond
+ ((string= filename "~")
+ (or (getenv "HOME")
+ (passwd:dir (getpw (getlogin)))
+ "~"))
+ ((string-prefix? "~/" filename)
+ (string-replace filename
+ (filename-resolve-user "~")
+ 0 1))
+ ((string-prefix? "~" filename)
+ (let ((matched (string-match "~([^/]*)" filename)))
+ (string-replace filename
+ (passwd:dir (getpw (match:substring matched 1)))
+ 0 (match:end matched))))
+ (else filename)))
+
+(define (filename-resolve-variables filename)
+ "Resolve environment variables in string FILENAME
+
+@lisp
+ (filename-resolve-variables \"$HOME\")
+=> the home directory
+ (filename-resolve-user \"foo-$PWD-bar\")
+=> \"foo-thepwd-bar\" (with thepwd the working directory
+@end lisp
+
+"
+ (regexp-substitute/global #f "\\$([A-Za-z0-9_]+)" filename
+ 'pre
+ (lambda (m)
+ (or (getenv (match:substring m 1))
+ (error "Environment variable ~s not set"
+ (match:substring m 1))))
+ 'post))
+
+;; (define (test s)
+;; (format #t "~a -> ~a~%" s (filename-resolve-user s)))
+;;
+;; (test "~")
+;; (test "~/")
+;; (test "~gdm")
+;; (test "~gdm/")
--
2.28.0
--
Unpolitisch sein
heißt politisch sein
ohne es zu merken
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1076 bytes --]
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2020-08-25 21:14 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <87wo1mwkwx.fsf@web.de>
2020-08-25 21:14 ` guile-lib: new (string posix) module, filename resolution procedures - proposal for inclusion Dr. Arne Babenhauserheide
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).