~ubuntu-branches/ubuntu/trusty/guile-2.0/trusty

« back to all changes in this revision

Viewing changes to doc/ref/misc-modules.texi

  • Committer: Package Import Robot
  • Author(s): Rob Browning
  • Date: 2012-02-04 11:33:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120204113328-op7l4os5fo5f8mm3
Tags: 2.0.5+1-1
* Incorporate upstream version 2.0.5.

* Delete 0001-Fix-the-SRFI-60-copy-bit-documentation.patch,
  0002-Define-_GNU_SOURCE-to-fix-the-GNU-kFreeBSD-build.patch, and
  0003-Include-gc.h-rather-than-gc-gc_version.h-in-pthread-.patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
@c -*-texinfo-*-
2
2
@c This is part of the GNU Guile Reference Manual.
3
 
@c Copyright (C)  1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2009, 2010, 2011
4
 
@c   Free Software Foundation, Inc.
 
3
@c Copyright (C)  1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2009,
 
4
@c   2010, 2011, 2012  Free Software Foundation, Inc.
5
5
@c See the file guile.texi for copying conditions.
6
6
 
7
7
@node Pretty Printing
1098
1098
@section File Tree Walk
1099
1099
@cindex file tree walk
1100
1100
 
 
1101
@cindex file system traversal
 
1102
@cindex directory traversal
 
1103
 
1101
1104
The functions in this section traverse a tree of files and
1102
 
directories, in a fashion similar to the C @code{ftw} and @code{nftw}
1103
 
routines (@pxref{Working with Directory Trees,,, libc, GNU C Library
1104
 
Reference Manual}).
 
1105
directories.  They come in two flavors: the first one is a high-level
 
1106
functional interface, and the second one is similar to the C @code{ftw}
 
1107
and @code{nftw} routines (@pxref{Working with Directory Trees,,, libc,
 
1108
GNU C Library Reference Manual}).
1105
1109
 
1106
1110
@example
1107
1111
(use-modules (ice-9 ftw))
1108
1112
@end example
1109
1113
@sp 1
1110
1114
 
1111
 
@defun ftw startname proc ['hash-size n]
 
1115
@deffn {Scheme Procedure} file-system-tree file-name [enter? [stat]]
 
1116
Return a tree of the form @code{(@var{file-name} @var{stat}
 
1117
@var{children} ...)} where @var{stat} is the result of @code{(@var{stat}
 
1118
@var{file-name})} and @var{children} are similar structures for each
 
1119
file contained in @var{file-name} when it designates a directory.
 
1120
 
 
1121
The optional @var{enter?} predicate is invoked as @code{(@var{enter?}
 
1122
@var{name} @var{stat})} and should return true to allow recursion into
 
1123
directory @var{name}; the default value is a procedure that always
 
1124
returns @code{#t}.  When a directory does not match @var{enter?}, it
 
1125
nonetheless appears in the resulting tree, only with zero children.
 
1126
 
 
1127
The @var{stat} argument is optional and defaults to @code{lstat}, as for
 
1128
@code{file-system-fold} (see below.)
 
1129
 
 
1130
The example below shows how to obtain a hierarchical listing of the
 
1131
files under the @file{module/language} directory in the Guile source
 
1132
tree, discarding their @code{stat} info:
 
1133
 
 
1134
@example
 
1135
(use-modules (ice-9 match))
 
1136
 
 
1137
(define remove-stat
 
1138
  ;; Remove the `stat' object the `file-system-tree' provides
 
1139
  ;; for each file in the tree.
 
1140
  (match-lambda
 
1141
    ((name stat)              ; flat file
 
1142
     name)
 
1143
    ((name stat children ...) ; directory
 
1144
     (list name (map remove-stat children)))))
 
1145
 
 
1146
(let ((dir (string-append (assq-ref %guile-build-info 'top_srcdir)
 
1147
                          "/module/language")))
 
1148
  (remove-stat (file-system-tree dir)))
 
1149
 
 
1150
@result{}
 
1151
("language"
 
1152
 (("value" ("spec.go" "spec.scm"))
 
1153
  ("scheme"
 
1154
   ("spec.go"
 
1155
    "spec.scm"
 
1156
    "compile-tree-il.scm"
 
1157
    "decompile-tree-il.scm"
 
1158
    "decompile-tree-il.go"
 
1159
    "compile-tree-il.go"))
 
1160
  ("tree-il"
 
1161
   ("spec.go"
 
1162
    "fix-letrec.go"
 
1163
    "inline.go"
 
1164
    "fix-letrec.scm"
 
1165
    "compile-glil.go"
 
1166
    "spec.scm"
 
1167
    "optimize.scm"
 
1168
    "primitives.scm"
 
1169
    @dots{}))
 
1170
  @dots{}))
 
1171
@end example
 
1172
@end deffn
 
1173
 
 
1174
@cindex file system combinator
 
1175
 
 
1176
It is often desirable to process directories entries directly, rather
 
1177
than building up a tree of entries in memory, like
 
1178
@code{file-system-tree} does.  The following procedure, a
 
1179
@dfn{combinator}, is designed to allow directory entries to be processed
 
1180
directly as a directory tree is traversed; in fact,
 
1181
@code{file-system-tree} is implemented in terms of it.
 
1182
 
 
1183
@deffn {Scheme Procedure} file-system-fold enter? leaf down up skip error init file-name [stat]
 
1184
Traverse the directory at @var{file-name}, recursively, and return the
 
1185
result of the successive applications of the @var{leaf}, @var{down},
 
1186
@var{up}, and @var{skip} procedures as described below.
 
1187
 
 
1188
Enter sub-directories only when @code{(@var{enter?} @var{path}
 
1189
@var{stat} @var{result})} returns true.  When a sub-directory is
 
1190
entered, call @code{(@var{down} @var{path} @var{stat} @var{result})},
 
1191
where @var{path} is the path of the sub-directory and @var{stat} the
 
1192
result of @code{(false-if-exception (@var{stat} @var{path}))}; when it is
 
1193
left, call @code{(@var{up} @var{path} @var{stat} @var{result})}.
 
1194
 
 
1195
For each file in a directory, call @code{(@var{leaf} @var{path}
 
1196
@var{stat} @var{result})}.
 
1197
 
 
1198
When @var{enter?} returns @code{#f}, or when an unreadable directory is
 
1199
encountered, call @code{(@var{skip} @var{path} @var{stat}
 
1200
@var{result})}.
 
1201
 
 
1202
When @var{file-name} names a flat file, @code{(@var{leaf} @var{path}
 
1203
@var{stat} @var{init})} is returned.
 
1204
 
 
1205
When an @code{opendir} or @var{stat} call fails, call @code{(@var{error}
 
1206
@var{path} @var{stat} @var{errno} @var{result})}, with @var{errno} being
 
1207
the operating system error number that was raised---e.g.,
 
1208
@code{EACCES}---and @var{stat} either @code{#f} or the result of the
 
1209
@var{stat} call for that entry, when available.
 
1210
 
 
1211
The special @file{.} and @file{..} entries are not passed to these
 
1212
procedures.  The @var{path} argument to the procedures is a full file
 
1213
name---e.g., @code{"../foo/bar/gnu"}; if @var{file-name} is an absolute
 
1214
file name, then @var{path} is also an absolute file name.  Files and
 
1215
directories, as identified by their device/inode number pair, are
 
1216
traversed only once.
 
1217
 
 
1218
The optional @var{stat} argument defaults to @code{lstat}, which means
 
1219
that symbolic links are not followed; the @code{stat} procedure can be
 
1220
used instead when symbolic links are to be followed (@pxref{File System,
 
1221
stat}).
 
1222
 
 
1223
The example below illustrates the use of @code{file-system-fold}:
 
1224
 
 
1225
@example
 
1226
(define (total-file-size file-name)
 
1227
  "Return the size in bytes of the files under FILE-NAME (similar
 
1228
to `du --apparent-size' with GNU Coreutils.)"
 
1229
 
 
1230
  (define (enter? name stat result)
 
1231
    ;; Skip version control directories.
 
1232
    (not (member (basename name) '(".git" ".svn" "CVS"))))
 
1233
  (define (leaf name stat result)
 
1234
    ;; Return RESULT plus the size of the file at NAME.
 
1235
    (+ result (stat:size stat)))
 
1236
 
 
1237
  ;; Count zero bytes for directories.
 
1238
  (define (down name stat result) result)
 
1239
  (define (up name stat result) result)
 
1240
 
 
1241
  ;; Likewise for skipped directories.
 
1242
  (define (skip name stat result) result)
 
1243
 
 
1244
  ;; Ignore unreadable files/directories but warn the user.
 
1245
  (define (error name stat errno result)
 
1246
    (format (current-error-port) "warning: ~a: ~a~%"
 
1247
            name (strerror errno))
 
1248
    result)
 
1249
 
 
1250
  (file-system-fold enter? leaf down up skip error
 
1251
                           0  ; initial counter is zero bytes
 
1252
                           file-name))
 
1253
 
 
1254
(total-file-size ".")
 
1255
@result{} 8217554
 
1256
 
 
1257
(total-file-size "/dev/null")
 
1258
@result{} 0
 
1259
@end example
 
1260
@end deffn
 
1261
 
 
1262
The alternative C-like functions are described below.
 
1263
 
 
1264
@deffn {Scheme Procedure} scandir name [select? [entry<?]]
 
1265
Return the list of the names of files contained in directory @var{name}
 
1266
that match predicate @var{select?} (by default, all files).  The
 
1267
returned list of file names is sorted according to @var{entry<?}, which
 
1268
defaults to @code{string-locale<?} such that file names are sorted in
 
1269
the locale's alphabetical order (@pxref{Text Collation}).  Return
 
1270
@code{#f} when @var{name} is unreadable or is not a directory.
 
1271
 
 
1272
This procedure is modeled after the C library function of the same name
 
1273
(@pxref{Scanning Directory Content,,, libc, GNU C Library Reference
 
1274
Manual}).
 
1275
@end deffn
 
1276
 
 
1277
@deffn {Scheme Procedure} ftw startname proc ['hash-size n]
1112
1278
Walk the file system tree descending from @var{startname}, calling
1113
1279
@var{proc} for each file and directory.
1114
1280
 
1161
1327
In the current implementation, returning non-@code{#t} from @var{proc}
1162
1328
is the only valid way to terminate @code{ftw}.  @var{proc} must not
1163
1329
use @code{throw} or similar to escape.
1164
 
@end defun
1165
 
 
1166
 
 
1167
 
@defun nftw startname proc ['chdir] ['depth] ['hash-size n] ['mount] ['physical]
 
1330
@end deffn
 
1331
 
 
1332
 
 
1333
@deffn {Scheme Procedure} nftw startname proc ['chdir] ['depth] ['hash-size n] ['mount] ['physical]
1168
1334
Walk the file system tree starting at @var{startname}, calling
1169
1335
@var{proc} for each file and directory.  @code{nftw} has extra
1170
1336
features over the basic @code{ftw} described above.
1267
1433
In the current implementation, returning non-@code{#t} from @var{proc}
1268
1434
is the only valid way to terminate @code{ftw}.  @var{proc} must not
1269
1435
use @code{throw} or similar to escape.
1270
 
@end defun
 
1436
@end deffn
1271
1437
 
1272
1438
 
1273
1439
@node Queues
1433
1599
@end example
1434
1600
 
1435
1601
@sp 1
1436
 
@defun make-stream proc initial-state
 
1602
@deffn {Scheme Procedure} make-stream proc initial-state
1437
1603
Return a new stream, formed by calling @var{proc} successively.
1438
1604
 
1439
1605
Each call is @code{(@var{proc} @var{state})}, it should return a pair,
1441
1607
being the new @var{state} for the next call.  For the first call
1442
1608
@var{state} is the given @var{initial-state}.  At the end of the
1443
1609
stream, @var{proc} should return some non-pair object.
1444
 
@end defun
 
1610
@end deffn
1445
1611
 
1446
 
@defun stream-car stream
 
1612
@deffn {Scheme Procedure} stream-car stream
1447
1613
Return the first element from @var{stream}.  @var{stream} must not be
1448
1614
empty.
1449
 
@end defun
 
1615
@end deffn
1450
1616
 
1451
 
@defun stream-cdr stream
 
1617
@deffn {Scheme Procedure} stream-cdr stream
1452
1618
Return a stream which is the second and subsequent elements of
1453
1619
@var{stream}.  @var{stream} must not be empty.
1454
 
@end defun
 
1620
@end deffn
1455
1621
 
1456
 
@defun stream-null? stream
 
1622
@deffn {Scheme Procedure} stream-null? stream
1457
1623
Return true if @var{stream} is empty.
1458
 
@end defun
 
1624
@end deffn
1459
1625
 
1460
 
@defun list->stream list
1461
 
@defunx vector->stream vector
 
1626
@deffn {Scheme Procedure} list->stream list
 
1627
@deffnx {Scheme Procedure} vector->stream vector
1462
1628
Return a stream with the contents of @var{list} or @var{vector}.
1463
1629
 
1464
1630
@var{list} or @var{vector} should not be modified subsequently, since
1465
1631
it's unspecified whether changes there will be reflected in the stream
1466
1632
returned.
1467
 
@end defun
 
1633
@end deffn
1468
1634
 
1469
 
@defun port->stream port readproc
 
1635
@deffn {Scheme Procedure} port->stream port readproc
1470
1636
Return a stream which is the values obtained by reading from
1471
1637
@var{port} using @var{readproc}.  Each read call is
1472
1638
@code{(@var{readproc} @var{port})}, and it should return an EOF object
1477
1643
@example
1478
1644
(port->stream (open-input-file "/foo/bar.txt") read-char)
1479
1645
@end example
1480
 
@end defun
 
1646
@end deffn
1481
1647
 
1482
 
@defun stream->list stream
 
1648
@deffn {Scheme Procedure} stream->list stream
1483
1649
Return a list which is the entire contents of @var{stream}.
1484
 
@end defun
 
1650
@end deffn
1485
1651
 
1486
 
@defun stream->reversed-list stream
 
1652
@deffn {Scheme Procedure} stream->reversed-list stream
1487
1653
Return a list which is the entire contents of @var{stream}, but in
1488
1654
reverse order.
1489
 
@end defun
 
1655
@end deffn
1490
1656
 
1491
 
@defun stream->list&length stream
 
1657
@deffn {Scheme Procedure} stream->list&length stream
1492
1658
Return two values (@pxref{Multiple Values}), being firstly a list
1493
1659
which is the entire contents of @var{stream}, and secondly the number
1494
1660
of elements in that list.
1495
 
@end defun
 
1661
@end deffn
1496
1662
 
1497
 
@defun stream->reversed-list&length stream
 
1663
@deffn {Scheme Procedure} stream->reversed-list&length stream
1498
1664
Return two values (@pxref{Multiple Values}) being firstly a list which
1499
1665
is the entire contents of @var{stream}, but in reverse order, and
1500
1666
secondly the number of elements in that list.
1501
 
@end defun
 
1667
@end deffn
1502
1668
 
1503
 
@defun stream->vector stream
 
1669
@deffn {Scheme Procedure} stream->vector stream
1504
1670
Return a vector which is the entire contents of @var{stream}.
1505
 
@end defun
 
1671
@end deffn
1506
1672
 
1507
 
@defun stream-fold proc init stream0 @dots{} streamN
 
1673
@deffn {Scheme Procedure} stream-fold proc init stream0 @dots{} streamN
1508
1674
Apply @var{proc} successively over the elements of the given streams,
1509
1675
from first to last until the end of the shortest stream is reached.
1510
1676
Return the result from the last @var{proc} call.
1513
1679
@var{elem} is from the corresponding @var{stream}.  @var{prev} is the
1514
1680
return from the previous @var{proc} call, or the given @var{init} for
1515
1681
the first call.
1516
 
@end defun
 
1682
@end deffn
1517
1683
 
1518
 
@defun stream-for-each proc stream0 @dots{} streamN
 
1684
@deffn {Scheme Procedure} stream-for-each proc stream0 @dots{} streamN
1519
1685
Call @var{proc} on the elements from the given @var{stream}s.  The
1520
1686
return value is unspecified.
1521
1687
 
1523
1689
@var{elem} is from the corresponding @var{stream}.
1524
1690
@code{stream-for-each} stops when it reaches the end of the shortest
1525
1691
@var{stream}.
1526
 
@end defun
 
1692
@end deffn
1527
1693
 
1528
 
@defun stream-map proc stream0 @dots{} streamN
 
1694
@deffn {Scheme Procedure} stream-map proc stream0 @dots{} streamN
1529
1695
Return a new stream which is the results of applying @var{proc} to the
1530
1696
elements of the given @var{stream}s.
1531
1697
 
1532
1698
Each call is @code{(@var{proc} elem0 @dots{} elemN)}, where each
1533
1699
@var{elem} is from the corresponding @var{stream}.  The new stream
1534
1700
ends when the end of the shortest given @var{stream} is reached.
1535
 
@end defun
 
1701
@end deffn
1536
1702
 
1537
1703
 
1538
1704
@node Buffered Input
1551
1717
of further input for an application level logical expression is
1552
1718
maintained too, and passed through to the reader.
1553
1719
 
1554
 
@defun make-buffered-input-port reader
 
1720
@deffn {Scheme Procedure} make-buffered-input-port reader
1555
1721
Create an input port which returns characters obtained from the given
1556
1722
@var{reader} function.  @var{reader} is called (@var{reader} cont),
1557
1723
and should return a string or an EOF object.
1566
1732
@code{set-buffered-input-continuation?!} below.  If the user has
1567
1733
entered a partial expression then it allows @var{reader} for instance
1568
1734
to give a different prompt to show more is required.
1569
 
@end defun
 
1735
@end deffn
1570
1736
 
1571
 
@defun make-line-buffered-input-port reader
 
1737
@deffn {Scheme Procedure} make-line-buffered-input-port reader
1572
1738
@cindex Line buffered input
1573
1739
Create an input port which returns characters obtained from the
1574
1740
specified @var{reader} function, similar to
1578
1744
@var{reader} is called (@var{reader} cont), and should return a string
1579
1745
or an EOF object as above.  Each string is a line of input without a
1580
1746
newline character, the port code inserts a newline after each string.
1581
 
@end defun
 
1747
@end deffn
1582
1748
 
1583
 
@defun set-buffered-input-continuation?! port cont
 
1749
@deffn {Scheme Procedure} set-buffered-input-continuation?! port cont
1584
1750
Set the input continuation flag for a given buffered input
1585
1751
@var{port}.
1586
1752
 
1595
1761
(let ((obj (read my-port)))
1596
1762
  ...
1597
1763
@end example
1598
 
@end defun
 
1764
@end deffn
1599
1765
 
1600
1766
 
1601
1767
@c Local Variables: