[go: up one dir, main page]

Menu

[1aafb6]: / scheme / file.scm  Maximize  Restore  History

Download this file

138 lines (121 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
;;;; $Id$
;;;; Copyright (C) 1998, 1999, 2000 Maciej Stachowiak and Greg J. Badros
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;;
;;;; This program 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 General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING. If not, write to
;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;
(define-module (app scwm file)
#:use-module (ice-9 string-fun)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim))
;;;;
;;;
;;; Miscellaneous file operations. Probably should be in Guile.
;;;
(define-public (filename-is-complete? fname)
"Return true if FNAME is a fully qualified pathname.
This is considered to be the case if the string FNAME starts with \"/\",
\"./\" or \"../\", following the convention of many Unix programs."
(and (> (string-length fname) 0)
(or (char=? (string-ref fname 0) #\/)
(and (char=? (string-ref fname 0) #\.)
(or
(and (> (string-length fname) 1)
(char=? (string-ref fname 1) #\/))
(and (> (string-length fname) 2)
(char=? (string-ref fname 1) #\.)
(char=? (string-ref fname 2) #\/)))))))
(define-public (find-file-in-path fname path)
"Search for file named FNAME in PATH.
FNAME is a string specifying a file; if it is a fully qualified filename,
as indicated by `filename-is-complete?', it is checked for as is. Otherwise,
each element of the list PATH is used as a directory name to check for the
file. If the file is found, the full pathname to it is returned; if not,
#f is returned."
(if (filename-is-complete? fname)
(if (file-exists? fname)
fname
#f)
(or-map (lambda (prefix)
(let ((fp (string-append prefix "/" fname)))
(if (file-exists? fp)
fp
#f)))
path)))
(define-public (string-list->string l)
"Convert L, a list of strings or characters, to a string."
(apply string-append l))
;; (string-list->string '("123" "456"))
(define-public (path-list->string-with-colons l)
"Convert L, a list of string directory names, to a single colon-separated string.
Returns that string."
(define (insert-colons l)
(cond ((not (pair? l)) '())
((null? (cdr l)) l)
(#t
(append (list (car l)) '(":") (insert-colons (cdr l))))))
(string-list->string (insert-colons l)))
;; (use-modules (ice-9 string-fun))
;; (use-modules (app scwm file))
;; (string-with-colons->path-list "this:is:a:test")
;; (string-with-colons->path-list "")
;; (path-list->string-with-colons '())
;; (path-list->string-with-colons '("foo"))
;; (path-list->string-with-colons '("foo" "bar"))
;; (insert-colons '("foo"))
;; (insert-colons '("foo" "bar"))
(define-public (string-with-colons->path-list s)
"Convert S, a colon-separated directory pathlist, into a list of directory strings.
Returns that list."
(separate-fields-discarding-char #\: s list))
(define-public (read-until-eof in)
"Return all the text from input port IN until eof.
IN should be a newline-terminated Ascii input port."
(let ((l (read-line in))
(answer ""))
(while (not (eof-object? l))
(set! answer (string-append answer l "\n"))
(set! l (read-line in)))
answer))
(define-public (output-of-system-cmd cmd)
"Return the output of command shell execution of CMD.
CMD is run synchronously and its output is piped into the return value
of this function, as a string."
(let* ((p (open-input-pipe cmd))
(answer (read-until-eof p)))
(close-pipe p)
answer))
(define-public (first-line-output-of-system-cmd cmd)
"Return the first line of output of command shell execution of CMD.
CMD is run synchronously and its output is piped into the return value
of this function, as a string. See also `output-of-system-cmd'
if you want to read all of the output of CMD."
(let* ((p (open-input-pipe cmd))
(answer (read-line p)))
(close-pipe p)
answer))
(define-public (execute-with-pidprop command)
"Execute COMMAND in the background and permit use of `window-pid' on its windows.
Returns the PID of COMMAND."
(let ((pidprop-so (string-append (scwm-path-exec-prefix) "/bin/scwm_set_pid_property.so")))
(string->number
(sans-final-newline
(first-line-output-of-system-cmd
(string-append "LD_PRELOAD=" pidprop-so " " command " & echo $!"))))))
;; (use-scwm-modules file xprop-extras stringops (ice-9 string-fun))
;; (use-scwm-modules foo)
;; (string->number (sans-final-newline (first-line-output-of-system-cmd "sleep 3 & echo $!")))
;; (execute-with-pidprop "xeyes")
;; (window-pid (get-window))