Debian Codegraph @

docsrc/xlisp/xlisp-doc/tutorials/shell-utilities.htm

raw
1 <html><head>
2
3 <title>Shell Utilities</title>
4
5 <style type="text/css">
6 .example {
7 color: #000000;
8 background-color: #F5F5F5;
9 padding: 8px;
10 border: #808080;
11 border-style: solid;
12 border-width: 1px;
13 width:auto;
14 }
15 .button {
16 color: #000000;
17 background-color: #F5F5F5;
18 padding-top: 1px;
19 padding-bottom: 1px;
20 padding-left: 4px;
21 padding-right: 8px;
22 border: #808080;
23 border-style: solid;
24 border-width: 1px;
25 white-space: pre;
26 }
27 .box {
28 color: #000000;
29 padding-top: 4px;
30 padding-bottom: 4px;
31 padding-left: 16px;
32 padding-right: 16px;
33 border: #808080;
34 border-style: solid;
35 border-width: 1px;
36 }
37 </style>
38
39 </head>
40
41 <body>
42
43 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
44 <a href="../manual/contents.htm">Contents</a> |
45 <a href="tutorials.htm">Tutorials</a> |
46 <a href="../examples/examples.htm">Examples</a> |
47 <a href="../reference/reference-index.htm">Reference</a>
48
49 <hr>
50
51 <h1>Shell Utilities</h1>
52
53 <hr>
54
55 <ul>
56 <li><nobr><a href="#cd">cd</a> - display and change directories</nobr></li>
57 <li><nobr><a href="#ls">ls</a> - display files and sub-directories</nobr></li>
58 <li><nobr><a href="#hd">hd</a> - hexdump a file</nobr></li>
59 </ul>
60
61 <p>Nyquist is not a system programming language, so Nyquist/XLISP cannot
62 create or remove files and directories, and the <a
63 href="../reference/system.htm">system</a> function does not work on Windows.
64 <nobr>But sometimes</nobr> it helps to know the name of the current working
65 directory, the name of the directory where the soundfiles are stored, or the
66 names of files and <nobr>sub-directories</nobr>.</p>
67
68 <a name="cd"></a>
69
70 <hr>
71
72 <h2>cd</h2>
73
74 <hr>
75
76 <p>The 'cd' function displays or changes the current working directory, it
77 also displays the name of the *<nobr>default-sf-dir</nobr>* directory, where
78 Nyquist stores its sound files:</p>
79
80 <pre class="example">
81 &gt; (cd)
82 ;; *default-sf-dir* = /tmp/
83 ;; working directory = /home/edgar
84 NIL
85
86 &gt; (cd "test")
87 ;; directory changed to "test"
88 ;; *default-sf-dir* = /tmp/
89 ;; working directory = /home/edgar/test
90 T
91
92 &gt; (cd "..")
93 ;; directory changed to "edgar"
94 ;; *default-sf-dir* = /tmp/
95 ;; working directory = /home/edgar
96 T
97
98 &gt; (cd "foo")
99 ;; directory not changed, "foo" not found
100 ;; *default-sf-dir* = /tmp/
101 ;; working directory = /home/edgar
102 NIL
103
104 &gt; (cd 123)
105 ;; directory not changed, 123 is not a string
106 ;; *default-sf-dir* = /tmp/
107 ;; working directory = /home/edgar
108 NIL
109 </pre>
110
111 <p>The 'cd' function is intended for interactive use, in program code it's
112 better to use the Nyquist <a href="../reference/setdir.htm">setdir</a>
113 function.</p>
114
115 <pre class="example">
116 (defun <font color="#0000CC">cd</font> (&amp;optional dirname)
117 (let ((old-dir (setdir <font color="#880000">"."</font>))
118 (new-dir (when (stringp dirname) (setdir dirname))))
119 (when dirname
120 (if new-dir
121 (when (string/= old-dir new-dir)
122 (let ((string-end (length new-dir))
123 (subseq-start 0))
124 (dotimes (index string-end)
125 (when (char= (char new-dir index) <font color="#AA5500">*file-separator*</font>)
126 (setq subseq-start index)))
127 (incf subseq-start)
128 (format t <font color="#880000">";; directory changed to ~s~%"</font>
129 (if (&lt; subseq-start string-end)
130 (subseq new-dir subseq-start)
131 (string <font color="#AA5500">*file-separator*</font>)))))
132 (format t <font color="#880000">";; directory not changed, ~s ~a~%"</font> dirname
133 (if (stringp dirname) <font color="#880000">"not found" "is not a string"</font>))))
134 (format t <font color="#880000">";; *default-sf-dir* = ~a~%"</font> <font color="#AA5500">*default-sf-dir*</font>)
135 (format t <font color="#880000">";; working directory = ~a~%"</font> (setdir <font color="#880000">"."</font>))
136 (when new-dir t)))
137 </pre>
138
139 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
140
141 <a name="ls"></a>
142
143 <hr>
144
145 <h2>ls</h2>
146
147 <hr>
148
149 <p>The 'ls' function lists files and directories:</p>
150
151 <pre class="example">
152 &gt; (ls)
153 ;;; /home/edgar/Downloads/nyquist/svn/nyquist
154 ;; advantages.txt cmt/ comp-ide.bat convert.dsp
155 ;; convert.dsw demos/ doc/ docsrc/
156 ;; fft/ ffts/ files.txt howtorelease.txt
157 ;; jny jnyqide.bat jnyqide/ lib/
158 ;; liblo/ license.txt lpc/ macosxproject/
159 ;; macproject/ Makefile misc/ nylsf/
160 ;; nyqide/ nyqsrc/ nyqstk/ nyquist.dsp
161 ;; nyquist.dsw nyquist.sln nyquist.vcproj nyqwin.dsp
162 ;; nyqwin.vcproj portaudio-oldv19/ portaudio/ portaudio_test/
163 ;; Readme.txt release.bat releasenyqide.bat releasenyqwin.bat
164 ;; runtime/ snd/ sys/ test/
165 ;; todo.txt tran/ xlisp/
166 47
167 </pre>
168
169 <p>The algorithm to find the number of colums is
170 <nobr>trial-and-error</nobr>, for example it starts with one column:</p>
171
172 <pre class="example">
173 <font color="#008844">&lt;- maximum-width -&gt;|</font>
174 item-1
175 item-2
176 item-3
177 item-4
178 item-5
179 </pre>
180
181 <p>When no line was longer than the <nobr>maximum-width</nobr> the
182 layout is saved and a new test is started with two columns:</p>
183
184 <pre class="example">
185 <font color="#008844">&lt;- maximum-width -&gt;|</font>
186 item-1 item-2
187 item-3 item-4
188 item-5
189 </pre>
190
191 <p>When no line was longer than the <nobr>maximum-width</nobr> the
192 layout is saved and a new test is started with three columns:</p>
193
194 <pre class="example">
195 <font color="#008844">&lt;- maximum-width -&gt;|</font>
196 item-1 item-2 item-3
197 </pre>
198
199 <p>As soon as a line becomes longer than the <nobr>maximum-width</nobr>, the
200 test is aborted and the saved layout from the previous run is used.</p>
201
202 <p>The main reason why arrays are used instead of lists is that we need
203 access to predefined numbers. With lists we always first need to test if an
204 element exists because <nobr>non-existent</nobr> list elements are NIL and
205 not numbers:</p>
206
207 <pre class="example">
208 (&lt; (nth 3 '(1 2)) 0) =&gt; <font color="#AA0000">error: bad argument type - NIL</font>
209 </pre>
210
211 <p>It's also no good idea to use <a href="../reference/setf.htm">setf</a>
212 with <nobr>non-existent</nobr> list elements:</p>
213
214 <pre class="example">
215 (setf (nth 2 nil) 'value) =&gt; VALUE
216 (nth 2 nil) =&gt; <font color="#AA0000">error: bad argument type - NIL</font>
217 </pre>
218
219 <p><b>Caution:</b> The XLISP <a href="../reference/setf.htm">setf</a>
220 special form does not signal an error if values are assigned to
221 <nobr>non-existent</nobr> places.</p>
222
223 <p>We use the Common Lisp 'incf' macro because the Nyquist
224 <a href="../reference/incf.htm">incf</a> macro has no 'increment'
225 argument:</p>
226
227 <pre class="example">
228 (defmacro <font color="#0000CC">cl:incf</font> (place &amp;optional (increment 1))
229 `(setf ,place (+ ,place ,increment)))
230 </pre>
231
232 <pre class="example">
233 (defun <font color="#0000CC">ls</font> (&amp;rest args)
234 (let* ((dirname (if (stringp (car args))
235 (prog1 (car args) (setq args (cdr args)))
236 (setdir ".")))
237 (show-all (car args))
238 (raw-list (listdir dirname)))
239 (cond ((null raw-list)
240 (format t <font color="#880000">";; directory ~s not found~%"</font> dirname))
241 ((&lt;= (length raw-list) 2)
242 (format t <font color="#880000">";;; ~a~%"</font> dirname)
243 (format t <font color="#880000">";; [directory is empty]~%"</font>) 0)
244 (t
245 (format t <font color="#880000">";;; ~a~%"</font> dirname)
246 (let ((file-separator (string <font color="#AA5500">*file-separator*</font>))
247 (dir-list nil))
248 (dolist (item raw-list)
249 (when (or show-all (not (ls:hidden-p item)))
250 (if (listdir (strcat dirname file-separator item))
251 (push (strcat item <font color="#880000">"/"</font>) dir-list)
252 (push item dir-list))))
253 (ls:list-items (sort dir-list #'string-lessp)))))))
254
255 (setq <font color="#AA5500">*ls-hidden-start*</font> (list <font color="#880000">"." "#"</font>))
256 (setq <font color="#AA5500">*ls-hidden-end*</font> (list <font color="#880000">"~" "#"</font>))
257
258 (defun <font color="#0000CC">ls:hidden-p</font> (string)
259 (let ((string-length (length string)))
260 (or (dolist (item <font color="#AA5500">*ls-hidden-start*</font> nil)
261 (let ((subseq-end (length item)))
262 (when (and (&gt;= string-length subseq-end)
263 (string= item (subseq string 0 subseq-end)))
264 (return t))))
265 (dolist (item <font color="#AA5500">*ls-hidden-end*</font> nil)
266 (let ((subseq-start (- string-length (length item))))
267 (when (and (&lt;= 0 subseq-start)
268 (string= item (subseq string subseq-start)))
269 (return t)))))))
270
271 (defmacro <font color="#0000CC">ls:reset-array</font> (array)
272 (let ((index (gensym)))
273 `(dotimes (,index (length ,array))
274 (setf (aref ,array ,index) 0))))
275
276 (defmacro <font color="#0000CC">ls:copy-array</font> (from-array to-array end)
277 (let ((index (gensym)))
278 `(dotimes (,index ,end)
279 (setf (aref ,to-array ,index)
280 (aref ,from-array ,index)))))
281
282 (defun <font color="#0000CC">ls:fill-string</font> (length)
283 (let ((string <font color="#880000">""</font>))
284 (dotimes (i length)
285 (setq string (strcat string <font color="#880000">" "</font>)))
286 string))
287
288 (defun <font color="#0000CC">ls:list-items</font> (item-list &amp;optional (terminal-width 80))
289 (let* ((separator 2)
290 (width (- terminal-width 4))
291 (width-max (+ width separator))
292 (num-items (length item-list))
293 (num-columns 1) <font color="#008844">; number of columns</font>
294 (item-array (make-array num-items))
295 (length-array (make-array num-items))
296 (length-min width) <font color="#008844">; shortest item</font>
297 (length-max 0) <font color="#008844">; longest item</font>
298 (length-all 0) <font color="#008844">; all items + separators</font>
299 <font color="#008844">;; the maximum possible number of columns is</font>
300 <font color="#008844">;; width-max / (1 char + separator)</font>
301 (max-columns (/ width-max (1+ separator)))
302 (column-array (make-array max-columns)))
303
304 <font color="#008844">;; initialize the column-array</font>
305 (ls:reset-array column-array)
306
307 <font color="#008844">;; copy the items from the list into the item-array</font>
308 (let ((item-index 0))
309 (dolist (item item-list)
310 (setf (aref item-array item-index) item)
311 (incf item-index)))
312
313 <font color="#008844">;; find the length of all items and store them in the length-array</font>
314 (dotimes (item-index num-items)
315 (let ((length-item (length (aref item-array item-index))))
316 (setf (aref length-array item-index) length-item
317 length-all (+ length-all length-item separator)
318 length-min (min length-min length-item)
319 length-max (max length-max length-item))))
320
321 <font color="#008844">;; find the number and widths of the columns</font>
322 (cond ((&lt;= length-all width-max)
323 <font color="#008844">;; if all items together fit into a single line</font>
324 (setq num-columns num-items)
325 (ls:copy-array length-array column-array num-items))
326 ((and (&gt; num-items 1)
327 (&lt;= (+ length-min length-max separator) width))
328 <font color="#008844">;; if there is more than one item and the</font>
329 <font color="#008844">;; longest + shortest item + separator fit into one line</font>
330 <font color="#008844">;; we start with two columns, one column is the fallback</font>
331 (incf num-columns)
332 <font color="#008844">;; the test-array must be 1+ because we need 1 failure-run</font>
333 (do ((test-array (make-array (1+ max-columns)))
334 (item-index 0 0))
335 ((progn
336 (ls:reset-array test-array)
337 <font color="#008844">;; loop until there are no more items in the list</font>
338 (do ((line-length 0 0))
339 ((&gt;= item-index num-items))
340 <font color="#008844">;; compute a complete line</font>
341 (dotimes (column-index num-columns)
342 <font color="#008844">;; loop through all columns in the test-array</font>
343 (when (and (&lt; item-index num-items)
344 (&lt; (aref test-array column-index)
345 (aref length-array item-index)))
346 <font color="#008844">;; if there are still items in the list and the</font>
347 <font color="#008844">;; item is wider than the column, update the array</font>
348 (setf (aref test-array column-index)
349 (aref length-array item-index)))
350 <font color="#008844">;; compute the line-length from the value in the array</font>
351 (cl:incf line-length
352 (+ (aref test-array column-index) separator))
353 (incf item-index))
354 <font color="#008844">;; analyze the result from computing the line</font>
355 (cond ((&gt; line-length width-max)
356 <font color="#008844">;; if the line is too long, abort completely, use</font>
357 <font color="#008844">;; the column-array values from the previous run</font>
358 (decf num-columns)
359 (return t)) <font color="#008844">; abort both 'do' loops</font>
360 ((&gt;= item-index num-items)
361 <font color="#008844">;; if no items is left and no line was too long</font>
362 <font color="#008844">;; first save the test-array in the column-array</font>
363 (ls:copy-array test-array column-array num-columns)
364 <font color="#008844">;; then try again with one more column</font>
365 (incf num-columns)))))))))
366
367 <font color="#008844">;; print the items on the screen</font>
368 (do ((item-index 0)
369 (last-item (1- num-items))
370 (last-column (1- num-columns))
371 (line <font color="#880000">";; " ";; "</font>))
372 ((&gt;= item-index num-items))
373 (dotimes (column-index num-columns)
374 <font color="#008844">;; loop through all columns</font>
375 (when (&lt; item-index num-items)
376 <font color="#008844">;; if there are still items in the list</font>
377 (setq line
378 (if (and (&lt; column-index last-column)
379 (&lt; item-index last-item))
380 <font color="#008844">;; if not the last column and not the last item</font>
381 (strcat line (aref item-array item-index)
382 <font color="#008844">;; add a fill-string</font>
383 (let ((column (aref column-array column-index))
384 (item (aref length-array item-index)))
385 (ls:fill-string (+ (- column item) separator))))
386 <font color="#008844">;; if the last column or the last item</font>
387 (strcat line (aref item-array item-index))))
388 (incf item-index)))
389 <font color="#008844">;; display the line on the screen</font>
390 (format t <font color="#880000">"~a~%"</font> line))
391
392 <font color="#008844">;; return the number of items listed on the screen</font>
393 num-items))
394 </pre>
395
396 <p><b>Note:</b> The code works, but this section is still too much mess.</p>
397
398 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
399
400 <a name="hd"></a>
401
402 <hr>
403
404 <h2>hd</h2>
405
406 <hr>
407
408 <p>The 'hd' function prints the hexdump of a file on the screen:</p>
409
410 <pre class="example">
411 &gt; (hd "/tmp/edgar-temp.wav")
412 0000000000 52 49 46 46 ac 58 01 00 57 41 56 45 66 6d 74 20 RIFF.X..WAVEfmt
413 0000000016 10 00 00 00 01 00 01 00 44 ac 00 00 88 58 01 00 ........D....X..
414 0000000032 02 00 10 00 64 61 74 61 88 58 01 00 00 00 4a 04 ....data.X....J.
415 0000000048 93 08 da 0c 1b 11 57 15 8b 19 b7 1d d7 21 ec 25 ......W......!.%
416 0000000064 f3 29 eb 2d d3 31 a9 35 6c 39 1a 3d b3 40 35 44 .).-.1.5l9.=.@5D
417 0000000080 9e 47 ee 4a 24 4e 3d 51 3a 54 19 57 d9 59 78 5c .G.J$N=Q:T.W.Yx\
418 0000000096 f7 5e 54 61 8f 63 a6 65 99 67 67 69 10 6b 92 6c .^Ta.c.e.ggi.k.l
419 0000000112 ee 6d 23 6f 31 70 16 71 d3 71 68 72 d4 72 17 73 .m#o1p.q.qhr.r.s
420 0000000128 31 73 23 73 eb 72 8a 72 01 72 4f 71 75 70 73 6f 1s#s.r.r.rOqupso
421 0000000144 49 6e f8 6c 80 6b e2 69 1f 68 36 66 29 64 f8 61 In.l.k.i.h6f)d.a
422 0000000160 a5 5f 2f 5d 98 5a e2 57 0b 55 17 52 05 4f d8 4b ._/].Z.W.U.R.O.K
423 0000000176 8f 48 2c 45 b1 41 1f 3e 76 3a b9 36 e8 32 05 2f .H,E.A.>v:.6.2./
424 0000000192 11 2b 0e 27 fe 22 e0 1e b8 1a 86 16 4c 12 0c 0e .+.'."......L...
425 0000000208 c7 09 7e 05 33 01 e9 fc 9f f8 57 f4 14 f0 d6 eb ..~.3.....W.....
426 0000000224 a0 e7 72 e3 4e df 36 db 2b d7 2f d3 42 cf 67 cb ..r.N.6.+./.B.g.
427 0000000240 9f c7 ea c3 4b c0 c3 bc 53 b9 fb b5 be b2 9d af ....K...S.......
428 ;; type "q" to quit or press Return to continue...
429 </pre>
430
431 <pre class="example">
432 (defun <font color="#0000CC">hd</font> (filename &amp;key (start 0) end)
433 (cond
434 ((not (stringp filename))
435 (format t <font color="#880000">";; not a string ~s~%"</font> filename))
436 ((listdir filename)
437 (format t <font color="#880000">";; not a file ~s~%"</font> string))
438 ((or (not (integerp start)) (minusp start))
439 (format t <font color="#880000">";; not a non-negative integer ~s~%"</font> start))
440 ((and end (or (not (integerp end)) (minusp end)))
441 (format t <font color="#880000">";; not a non-negative integer ~s~%"</font> end))
442 ((and end (&gt;= start end))
443 (format t <font color="#880000">";; :start ~s is greater then :end ~s~%"</font> start end))
444 (t (let ((file-stream (open-binary filename)))
445 (if (null file-stream)
446 (format t <font color="#880000">";; file not found ~s~%"</font> filename)
447 (unwind-protect
448 (hd:dump file-stream start end)
449 (when file-stream (close file-stream))))))))
450
451 (defun <font color="#0000CC">hd:dump</font> (file-stream start end)
452 (let ((file-position (hd:skip file-stream start))
453 (break (+ start 255))
454 (end-of-file nil)
455 (end-of-dump nil))
456 (if (&lt; file-position start)
457 (setq end-of-file t)
458 (flet ((read-eight-bytes (start-position)
459 (let (byte-list)
460 (dotimes (offset 8)
461 (let* ((position (+ start-position offset))
462 (read-p (and (&lt;= start position)
463 (or (null end)
464 (&gt;= end position))))
465 (byte (when read-p
466 (read-byte file-stream))))
467 (push byte byte-list)
468 (when byte (incf file-position))
469 (when (and read-p (null byte))
470 (setq end-of-file t))))
471 (reverse byte-list))))
472 (read-line)
473 (do ((line-start (* (/ start 16) 16) (+ line-start 16)))
474 ((or end-of-file end-of-dump))
475 (let* ((number (hd:line-number line-start))
476 (list-1 (read-eight-bytes line-start))
477 (list-2 (read-eight-bytes (+ line-start 8)))
478 (bytes-1 (hd:byte-string list-1))
479 (bytes-2 (hd:byte-string list-2))
480 (chars (hd:char-string (append list-1 list-2))))
481 (format t <font color="#880000">"~a ~a ~a ~a~%"</font> number bytes-1 bytes-2 chars)
482 (when (and end (&gt; file-position end))
483 (setq end-of-dump t))
484 (when (&gt; file-position break)
485 (format t <font color="#880000">";; type \"q\" to quit or press Return to continue... "</font>)
486 (if (string-equal "q" (read-line))
487 (setq end-of-dump t)
488 (setq break (+ break 256))))))))
489 (when (and end (&gt;= file-position end))
490 (format t <font color="#880000">";; reached specified :end at byte number ~a~%"</font> end))
491 (when end-of-file
492 (format t <font color="#880000">";; end of file at byte number ~a~%"</font> file-position))))
493
494 (defun <font color="#0000CC">hd:line-number</font> (integer)
495 (progv '(<font color="#AA5500">*integer-format*</font>) '(<font color="#880000">"%.10d"</font>)
496 (format nil <font color="#880000">"~s"</font> integer)))
497
498 (defun <font color="#0000CC">hd:byte-string</font> (byte-list)
499 (let ((string <font color="#880000">""</font>))
500 (dolist (byte byte-list)
501 (setq string (strcat string (if byte
502 (progv '(<font color="#AA5500">*integer-format*</font>) '(<font color="#880000">"%.2x"</font>)
503 (format nil <font color="#880000">"~s "</font> byte))
504 <font color="#880000">" "</font>))))
505 (subseq string 0 (1- (length string)))))
506
507 (defun <font color="#0000CC">hd:char-string</font> (byte-list)
508 (let ((string <font color="#880000">""</font>))
509 (dolist (byte byte-list)
510 (setq string (strcat string (if byte
511 (if (&lt;= 32 byte 126)
512 (string byte)
513 <font color="#880000">"."</font>)
514 <font color="#880000">" "</font>))))
515 string))
516
517 (defun <font color="#0000CC">hd:skip</font> (file-stream offset)
518 (if (= offset 0)
519 offset
520 (let ((count 0))
521 (format t <font color="#880000">";; skipping ~a bytes...~%"</font> offset)
522 (dotimes (ignore offset)
523 (if (read-byte file-stream)
524 (incf count)
525 (return)))
526 count)))
527 </pre>
528
529 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
530
531 <hr>
532
533 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
534 <a href="../manual/contents.htm">Contents</a> |
535 <a href="tutorials.htm">Tutorials</a> |
536 <a href="../examples/examples.htm">Examples</a> |
537 <a href="../reference/reference-index.htm">Reference</a>
538
539 </body></html>