From: "Dr. Arne Babenhauserheide" <arne_bab@web.de>
To: guile-user@gnu.org
Subject: guile-lib: new (string posix) module, filename resolution procedures - proposal for inclusion
Date: Tue, 25 Aug 2020 23:14:24 +0200 [thread overview]
Message-ID: <87tuwqwhqn.fsf@web.de> (raw)
In-Reply-To: 87wo1mwkwx.fsf@web.de
[-- 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 --]
parent reply other threads:[~2020-08-25 21:14 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <87wo1mwkwx.fsf@web.de>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87tuwqwhqn.fsf@web.de \
--to=arne_bab@web.de \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).