-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos.pl
More file actions
282 lines (246 loc) · 8.23 KB
/
os.pl
File metadata and controls
282 lines (246 loc) · 8.23 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
% os.pl - Steven Smith 2017 January 02
% Natural Language Processing (NLP) of text entered at the command line and
% the execution of commands in an OS shell.
%
% The main predicate is process_commands/0. (requires et.pl)
%
% The system is capable of executing commands on
% Windows
% Linux/Mac OS (Posix)
%
% Windows DOS commands supported - see tr/4.
% dir X:
% dir Y:*.X
% copy X Y
% dir X
% more X
% cd
% cd X
% mkdir X
% rmdir X
% ver
%
% Linux/Mac OS commands supported - see tr/4.
% cp X Y
% ls X
% less X
% pwd
% cd X
% mkdir X
% rmdir X
% uname -a
% sr(+List,-Result)
% Replace List with Result when matched.
% equivalent phrases - like synonyms
sr([directory,to|X],[directory|X]).
sr([disk,in,drive|X],[drive|X]).
sr([disk,in|X],[drive|X]).
sr([what,files|X],[files|X]).
sr([everything|X],[all,files|X]).
sr([any,files|X],[all,files|X]).
sr([files,contents|X],[contents,files|X]).
sr([to,make|X],[make|X]).
sr([to,remove|X],[remove|X]).
sr([to,change|X],[change|X]).
sr([to,copy|X],[copy|X]).
sr([to,show|X],[show|X]).
% synonyms - equivalent words
sr([disk|X],[drive|X]).
sr([file|X],[files|X]).
sr([every|X],[all|X]).
sr([content|X],[contents|X]).
sr([in|X],[on|X]).
sr([create|X],[make|X]).
sr([delete|X],[remove|X]).
sr([switch|X],[change|X]).
sr([bye|X],[quit|X]).
sr([exit|X],[quit|X]).
sr([running|X],[using|X]).
sr([path|X],[directory|X]).
% stop phrases - phrases that are ignored
sr([i,would|X],X).
sr([can,i|X],X).
sr([can,you|X],X).
sr([could,i|X],X).
sr([could,you|X],X).
sr([would,you|X],X).
sr([will,you|X],X).
sr([give,me|X],X).
sr([like,you,to|X],X).
sr([like,to|X],X).
sr([am,i|X],X).
sr([i,am|X],X).
% stop words - words that are ignored
sr([please|X],X).
sr([me|X],X).
sr([the|X],X).
sr([is|X],X).
sr([are|X],X).
sr([a|X],X).
sr([there|X],X).
sr([these|X],X).
sr([any|X],X).
sr([like|X],X).
sr([of|X],X).
sr([see|X],X).
sr([list|X],X).
sr([show|X],X).
sr([tell|X],X).
sr([what|X],X).
sr([which|X],X).
sr([you|X],X).
sr([my|X],X).
% simplify(+List,-Result)
% Reduces List to it's simplified version,Result
simplify(List,Result) :-
sr(List,NewList),
!,
simplify(NewList,Result).
simplify([W|Words],[W|NewWords]) :-
simplify(Words,NewWords).
simplify([],[]).
% tr(+OS,+SimplifiedWords,-Command,+OriginalWords)
% Translates the SimplifiedWords into a command based on the OS and
% using the OriginalWords (to make sure file/directory names are in the
% correct case where necessary - posix)
tr(_,[quit],[quit],_).
% windows translations
tr(windows,[all,files,on,drive,X],['dir ',X,':'],_).
tr(windows,[X,files,on,drive,Y],['dir ',Y,':*.',X],_).
tr(windows,[copy,files,from,X,to,Y],['copy ',X,' ',Y],_).
tr(windows,[files,on,directory,X],['dir ',X],_).
tr(windows,[contents,files,X],['c:\\windows\\system32\\more ',X],_).
tr(windows,[current,directory],['cd'],_).
tr(windows,[change,directory,X],['cd ',X],_).
tr(windows,[make,directory,X],['mkdir ',X],_).
tr(windows,[remove,directory,X],['rmdir ',X],_).
tr(windows,[os,using],['ver'],_).
% posix translations - X and Y are converted to there original case as Linux
% and Mac OS are usually case sensitive
tr(posix,[copy,files,from,X,to,Y],Command,OriginalWords) :-
find_in_list(X,OriginalWords,ActualX),
find_in_list(Y,OriginalWords,ActualY),
Command=['cp ',ActualX,' ',ActualY].
tr(posix,[files,on,directory,X],Command,OriginalWords) :-
find_in_list(X,OriginalWords,ActualX),
Command=['ls ',ActualX].
tr(posix,[contents,files,X],Command,OriginalWords) :-
find_in_list(X,OriginalWords,ActualX),
Command=['less ',ActualX].
tr(posix,[current,directory],['pwd'],_).
tr(posix,[change,directory,X],Command,OriginalWords) :-
find_in_list(X,OriginalWords,ActualX),
Command=['cd ',ActualX].
tr(posix,[make,directory,X],Command,OriginalWords) :-
find_in_list(X,OriginalWords,ActualX),
Command=['mkdir ',ActualX].
tr(posix,[remove,directory,X],Command,OriginalWords) :-
find_in_list(X,OriginalWords,ActualX),
Command=['rmdir ',ActualX].
tr(posix,[os,using],['uname -a'],_).
% find_in_list(+X,+List,-Result)
% Tries to find X in List using a case insensitive search but returns Result,
% the original case of X in the List.
% When X cannot be found then X is the Result
find_in_list(X,[],X) :- !.
% X is found so set Result to the original case of X in List
find_in_list(X,[H|_],Result) :-
string_lower(H,LowerH),
string_lower(X,LowerX),
LowerX = LowerH,
!,
Result = H.
% Current item in List is not X so move to the next item
find_in_list(X,[_|T],Result) :- find_in_list(X,T,Result).
% translate(+OS,+SimplifiedWords,-Command,+OriginalWords)
% Translates the SimplifiedWords into a command based on the OS and
% using the OriginalWords (to make sure file/directory names are in the
% correct case where necessary - posix)
translate(OS,SimplifiedWords,Command,OriginalWords) :-
tr(OS,SimplifiedWords,Command,OriginalWords),
!.
% no match has been found so notify the user that the SimplifiedWords
% could not be understood
translate(_,SimplifiedWords,[],_) :-
format('I do not understand: ~k~n', [SimplifiedWords]).
% current_os(-OS)
% Determines if the current OS is windows or posix (Linux/Mac OS)
% is windows if / is mapped to \ using prolog_to_os_filename
current_os(OS) :-
prolog_to_os_filename('/',FilePath),
FilePath = '\\',
!,
OS=windows.
% otherwise the OS is posix
current_os(OS) :- OS=posix.
% process_commands
% the main loop for natural language processing of text entered at the
% keyboard and executing the associated commands on the underlying OS.
% The loop terminates when the user types quit
process_commands :-
current_os(OS),
format('The current operating system is ~a~n',OS),
repeat,
write('Command -> '),
process_input(OS,user,_,_,Command),
pass_to_os(OS,Command),
Command == [quit],
!.
% process_input(+OS,+Stream,-Words,-SimplifiedWords,-Command)
% takes the OS and the Stream and creates a list of starting Words,
% SimplifiedWords and the associated Command that needs executing on the OS
process_input(OS,Stream,Words,SimplifiedWords,Command) :-
get_input(Stream,Words,OriginalWords),
simplify(Words,SimplifiedWords),
translate(OS,SimplifiedWords,Command,OriginalWords).
% get_input(+Stream,-Words,-OriginalWords)
% outputs Words and the OriginalWords from a Stream
get_input(Stream,Words,OriginalWords) :-
read_string(Stream,'\n','\r',_,Input),
split_string(Input,' ','\r\t\s',OriginalWords),
maplist(atom_lower,OriginalWords,Words).
% atom_lower(+String,-Atom)
% String is converted to lower case and then to an Atom
atom_lower(String,Atom) :-
string_lower(String,LowerString),
atom_string(Atom,LowerString).
% pass_to_os(+OS,+Command)
% executes the Command on the OS
% an empty Command causes no command to be executed on the OS
pass_to_os(_,[]) :- !.
% [quit] causes no command to be executed on the OS
pass_to_os(_,[quit]) :- !.
% ['cd ',X] does not result is a shell command being executed, but the
% directory is changed using the working_directory function.
% If cd is executed in the shell, the change of directory is lost when the
% shell exits.
% NOTE: the call to working_directory is wrapped in a catch to display the
% error message but return successfully.
pass_to_os(_,['cd ',X]) :-
catch(
working_directory(_,X),
E,
(print_message(error,E),true)),
!.
% a windows Command is executed using process_create rather than shell or
% win_exec. shell does not display output and win_exec displays the output
% too late, as it is asynchronous. process_create executes synchronously.
% NOTE: the call to process_create is wrapped in a catch to display the
% error message but return successfully.
pass_to_os(windows,Command) :-
atomics_to_string(Command,'',CommandString),
split_string(CommandString," ","\s\t\r\n",Args),
getenv('COMSPEC',CMD), % gets the path to cmd.exe
catch(
process_create(CMD,['/C'|Args],[]),
E,
print_message(error,E)).
% a posix Command can be executed using shell as it executes synchronously.
% NOTE: the call to shell is wrapped in a catch to display the
% error message but return successfully.
pass_to_os(posix,Command) :-
atomics_to_string(Command,' ',CommandString),
catch(
shell(CommandString),
E,
print_message(error,E)).