home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / plbin.zip / pl / library / shell.pl < prev    next >
Text File  |  1992-05-26  |  5KB  |  249 lines

  1. /*  File:    shell.pl
  2.     Purpose: Limited Unix Shell Emulation
  3.     Author:  Jan Wielemaker
  4.     Date:    Sep 16 1989
  5. */
  6.  
  7. :- module(shell,
  8.     [ (ls)/0
  9.     , (ls)/1
  10.     , (cd)/0
  11.     , (cd)/1
  12.     , (p)/0
  13.     , (p)/1
  14.     , d/0
  15.     , pd/0
  16.     , mv/2
  17.     , (rm)/1
  18.     , (grep)/1
  19.     , (grep)/2
  20.     , (tg)/1
  21.     ]).
  22.  
  23. :- op(900, fy, [ls, cd, p, rm, grep, tg]).
  24.  
  25. /*  Shell Emulation Library
  26.  
  27.     This library is meant for systems that do not allow us to get access
  28.     to the operating system via shell/[0,1,2].  It is developed  on  the
  29.     ST-MINIX version.  MINIX does not have a vfork() call, and thus only
  30.     allows  shell/[0,1,2]  if  Prolog  uses less than half the amount of
  31.     available memory.  This library offers a number  of  predicates  for
  32.     listing, directory management, deleting, copying and renaming files.
  33.     It should be combined with the linked-in version of Richard O'Keefes
  34.     `thief' editor (via the $thief/1 predicate).
  35.  
  36.  ** Sun Sep 17 12:04:54 1989  jan@swi.psy.uva.nl */
  37.  
  38. %    cd
  39. %    cd(Dir)
  40. %    Change working directory
  41.  
  42. (cd) :-
  43.     cd(~).
  44.  
  45. cd(Dir) :-
  46.     name_to_atom(Dir, Name),
  47.     chdir(Name).
  48.  
  49. %    d    -- Print Directory Stack
  50. %    p    -- Push Directory Stack
  51. %    pd    -- Pop Directory Stack
  52.  
  53. :- dynamic
  54.     stack/1.
  55.  
  56. (p) :-
  57.     p(+1).
  58.  
  59. p(N) :-
  60.     integer(N), !,
  61.     findall(D, stack(D), Ds),
  62.     (   nth1(N, Ds, Go),
  63.         retract(stack(Go))
  64.     ->  p(Go)
  65.     ;   warning('Directory stack not that deep'),
  66.         fail
  67.     ).
  68. p(Dir) :-
  69.     name_to_atom(Dir, Name),
  70.     absolute_file_name('', Old),
  71.     chdir(Name),
  72.     asserta(stack(Old)).
  73.  
  74. pd :-
  75.     retract(stack(Dir)), !,
  76.     chdir(Dir).
  77. pd :-
  78.     warning('Directory stack empty'),
  79.     fail.
  80.  
  81. d :-
  82.     (   absolute_file_name('', D)
  83.     ;   stack(D)
  84.     ),
  85.     dir_name(D, Name),
  86.     format('~w ', [Name]),
  87.     fail.
  88. d :-
  89.     nl.
  90.  
  91. dir_name('/', '/') :- !.
  92. dir_name(Path, Name) :-
  93.     concat(P, /, Path), !,
  94.     dir_name(P, Name).
  95. dir_name(Path, Name) :-
  96.     absolute_file_name('~', Home),
  97.     concat(Home, FromHome, Path), !,
  98.     sformat(Name, '~~~w', [FromHome]).
  99. dir_name(Path, Path).
  100.  
  101. %    ls
  102. %    ls(Dir|Files)
  103. %    List a directory, flag directories with a '/'
  104.  
  105. (ls) :-
  106.     ls('.').
  107.  
  108. ls(Spec) :-
  109.     name_to_atom(Spec, Atom),
  110.     expand_file_name(Atom, Matches),
  111.     ls_(Matches).
  112.  
  113. ls_([Dir]) :-
  114.     exists_directory(Dir), !,
  115.     (   Dir == '.'
  116.     ->  expand_file_name('*', Files)
  117.     ;   concat(Dir, '/*', Spec),
  118.         expand_file_name(Spec, Files)
  119.     ),
  120.     ls__(Files).
  121. ls_(Files) :-
  122.     ls__(Files).
  123.  
  124. ls__([]) :- !,
  125.     warning('No Match'),
  126.     fail.
  127. ls__(Files) :-
  128.     maplist(tag_file, Files, Tagged),
  129.     list_atoms(Tagged, 78).
  130.  
  131. tag_file(File, Dir) :-
  132.     exists_directory(File),    
  133.     concat(File, /, Dir).
  134. tag_file(File, File).
  135.  
  136. %    mv(+From, +To)    --- Move (Rename) a file
  137. %    rm(+File)    --- Remove (unlink) a file
  138.  
  139. mv(From, To) :-
  140.     name_to_atom(From, A0),
  141.     name_to_atom(To, A1),
  142.     rename_file(A0, A1).
  143.  
  144. rm(File) :-
  145.     name_to_atom(File, A),
  146.     delete_file(A).
  147.  
  148. %    grep(String)        --- grep through all source files
  149. %    grep(File, String)    --- grep through specified files
  150.  
  151. grep(S) :-
  152.     flag(grep_, _, 0),
  153.     source_file(File),
  154.         File \== user,
  155.         grep_(File, S),
  156.     fail.
  157. grep(_) :-
  158.     flag(grep_, 1, 1).
  159.  
  160. grep(File, S) :-
  161.     flag(grep_, _, 0),
  162.     name_to_atom(File, F),
  163.     expand_file_name(F, Files),
  164.     member(F2, Files),
  165.         grep_(F2, S),
  166.     fail.
  167. grep(_, _) :-
  168.     flag(grep_, 1, 1).
  169.  
  170. grep_(File, S) :-
  171.     '$file_base_name'(File, Base),
  172.     '$grep'(File, S, Line),
  173.         flag(grep_, _, 1),
  174.         format('~w: ~w~n', [Base, Line]),
  175.     fail.
  176. grep_(_, _) :-
  177.     flag(grep_, 1, 1).
  178.  
  179. tg(String) :-
  180.     source_file(File),
  181.         File \== user,
  182.         (   '$grep'(File, String, _)
  183.         ->  '$confirm'('Edit ~w', [File]),
  184.         concat('-', String, Search),
  185.         \+ '$thief'(['-f', File, Search])    % succeed on ^C
  186.         ).
  187. tg(_) :-
  188.     make.
  189.  
  190. %    name_to_atom(Typed, Atom)
  191. %    Convert a typed name into an atom
  192.  
  193. name_to_atom(Atom, Atom) :-
  194.     atomic(Atom), !.
  195. name_to_atom(Term, Atom) :-
  196.     term_to_atom(Term, Raw),
  197.     name(Raw, S0),
  198.     sublist(non_blank, S0, S1),
  199.     name(Atom, S1).
  200.  
  201. non_blank(C) :-
  202.     between(0, 32, C), !,
  203.     fail.
  204. non_blank(_).
  205.  
  206.  
  207. %    list_atoms(+List, +Width)
  208. %    List a set of atoms multicolumn on a Width wide output device.
  209.  
  210. list_atoms(List, W) :-
  211.     length(List, L),
  212.     Term =.. [l|List],
  213.     longest(List, Longest),
  214.     Columns is W // (Longest + 3),
  215.     Rows is integer(L / Columns + 0.49999),    % should be ceil/1
  216.     ColumnWidth is W // Columns,
  217.     Max is Columns * Rows - 1,
  218.     between(0, Max, N),
  219.         Index is N // Columns + (N mod Columns) * Rows + 1,
  220.         (    arg(Index, Term, Atom),
  221.         atom_length(Atom, AL),
  222.         write(Atom), tab(ColumnWidth - AL)
  223.         ->  true
  224.         ;   true
  225.         ),
  226.         (N+1) mod Columns =:= 0,
  227.         nl,
  228.     fail.
  229. list_atoms(_, _).
  230.  
  231. longest(List, Longest) :-
  232.     longest(List, 0, Longest).
  233.  
  234. longest([], M, M) :- !.
  235. longest([H|T], Sofar, M) :-
  236.     atom_length(H, L),
  237.     L >= Sofar, !,
  238.     longest(T, L, M).
  239. longest([_|T], S, M) :-
  240.     longest(T, S, M).
  241.  
  242. %    warning(Fmt, [Args]).
  243.  
  244. warning(Fmt) :-
  245.     warning(Fmt, []).
  246.  
  247. warning(Fmt, Args) :-
  248.     '$break'('$warning'(Fmt, Args)).
  249.