aboutsummaryrefslogtreecommitdiff
path: root/lib/macshell.py
blob: 884c89c9c56de7d8d098c2c09778987417a09f8b (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Macintosh 'shell'
# vi:set tabsize=4:

# XXX string quoting in arguments
# XXX directory stack
# XXX $macros?
# XXX ^C during any command
# XXX 'sh' builtin command?
# XXX why does open require absolute path? (need to fix chdir.c)


import mac

import macpath
import string
import glob
from macpath import isfile, isdir, exists
import TclUtil # For splitting/quoting mechanisms

class Struct(): pass
G = Struct()

def reset():
	G.debug = 0
	G.ps1 = '$ '
	G.homedir = mac.getcwd()
	G.commands = mkcmdtab()
	G.aliases = {}

def mkcmdtab():
	tab = {}
	tab['alias'] = do_alias
	tab['cd'] = do_cd
	tab['debug'] = do_debug
	tab['grep'] = do_grep
	tab['help'] = do_help
	tab['ls'] = do_ls
	tab['mkdir'] = do_mkdir
	tab['mv'] = do_mv
	tab['page'] = do_page
	tab['pwd'] = do_pwd
	tab['reset'] = do_reset
	tab['rm'] = do_rm
	tab['rmdir'] = do_rmdir
	tab['sync'] = do_sync
	tab['unalias'] = do_unalias
	return tab

def main():
	while 1:
		try:
			line = raw_input(G.ps1)
		except EOFError:
			print '[EOF]'
			break
		except KeyboardInterrupt:
			print '[Intr]'
			line = ''
		if G.debug:
			print 'line:', `line`
		words = TclUtil.SplitList(line)
		if G.debug:
			print 'words:', words
		if words and words[0][0] <> '#':
			run(words)

def run(words):
	expandaliases(words)
	cmd = words[0]
	args = words[1:]
	if G.commands.has_key(cmd):
		if args:
			try:
				args = expandgloblist(args)
			except glob_error, msg:
				print cmd, ': glob error :', msg
				return
		G.commands[cmd](args)
		return
	if hasglobchar(cmd):
		if args:
			print cmd, ': cannot glob pattern with arguments'
			return
		try:
			words = expandglobword(cmd)
		except glob_error, msg:
			print cmd, ': glob error :', msg
			return
		if len(words) > 1:
			columnize(words)
			return
		cmd = words[0]
		print cmd
	if isfile(cmd):
		if args:
			print cmd, ': file command expects no arguments'
			return
		do_page([cmd])
	elif isdir(cmd):
		if args:
			print cmd, ': directory command expects no arguments'
			return
		do_cd([cmd])
	else:
		print cmd, ': no such command, file or directory'

glob_error = 'glob error'

def expandgloblist(words):
	res = []
	for word in words:
		if hasglobchar(word):
			res = res + expandglobword(word)
		else:
			res.append(word)
	return res

def expandglobword(word):
	names = glob.globlist(mac.listdir(':'), word)
	if not names: raise glob_error, 'no match for pattern ' + word
	return names

def hasglobchar(word):
	return '*' in word or '?' in word

def expandaliases(words):
	seen = []
	cmd = words[0]
	while cmd not in seen and G.aliases.has_key(cmd):
		seen.append(cmd)
		words[:1] = G.aliases[cmd]
		cmd = words[0]

def do_alias(args):
	if not args:
		listaliases()
	elif len(args) = 1:
		listalias(args[0])
	else:
		defalias(args[0], args[1:])

def listaliases():
	names = G.aliases.keys()
	names.sort()
	for name in names: listalias(name)

def listalias(name):
	if not G.aliases.has_key(name):
		print name, ': no such alias'
		return
	print 'alias', name,
	printlist(G.aliases[name])
	print

def defalias(name, expansion):
	G.aliases[name] = expansion

def do_cd(args):
	if len(args) > 1:
		print 'usage: cd [dirname]'
	elif args:
		chdirto(args[0])
	else:
		chdirto(G.homedir)

def chdirto(dirname):
	try:
		mac.chdir(dirname)
	except mac.error, msg:
		print dirname, ':', msg
		return

def do_debug(args):
	G.debug = (not G.debug)

def do_grep(args):
	if len(args) < 2:
		print 'usage: grep regexp file ...'
		return
	import regexp
	try:
		prog = regexp.compile(args[0])
	except regexp.error, msg:
		print 'regexp.compile error for', args[0], ':', msg
		return
	for file in args[1:]:
		grepfile(prog, file)

def grepfile(prog, file):
	try:
		fp = open(file, 'r')
	except RuntimeError, msg:
		print file, ': cannot open :', msg
		return
	lineno = 0
	while 1:
		line = fp.readline()
		if not line: break
		lineno = lineno+1
		if prog.exec(line):
			print file+'('+`lineno`+'):', line,

def do_help(args):
	if args:
		print 'usage: help'
		return
	names = G.commands.keys()
	names.sort()
	columnize(names)

def do_ls(args):
	if not args:
		lsdir(':')
	else:
		for dirname in args:
			lsdir(dirname)

def lsdir(dirname):
	if not isdir(dirname):
		print dirname, ': no such directory'
		return
	names = mac.listdir(dirname)
	lsfiles(names, dirname)

def lsfiles(names, dirname):
	names = names[:] # Make a copy so we can modify it
	for i in range(len(names)):
		name = names[i]
		if G.debug: print i, name
		if isdir(macpath.cat(dirname, name)):
			names[i] = ':' + name + ':'
	columnize(names)

def columnize(list):
	COLUMNS = 80-1
	n = len(list)
	colwidth = maxwidth(list)
	ncols = (COLUMNS + 1) / (colwidth + 1)
	if ncols < 1: ncols = 1
	nrows = (n + ncols - 1) / ncols
	for irow in range(nrows):
		line = ''
		for icol in range(ncols):
			i = irow + nrows*icol
			if 0 <= i < n:
				word = list[i]
				if i+nrows < n:
					word = string.ljust(word, colwidth)
				if icol > 0:
					word = ' ' + word
				line = line + word
		print line

def maxwidth(list):
	width = 0
	for word in list:
		if len(word) > width:
			width = len(word)
	return width

def do_mv(args):
	if len(args) <> 2:
		print 'usage: mv src dst'
		return
	src, dst = args[0], args[1]
	if not exists(src):
		print src, ': source does not exist'
		return
	if exists(dst):
		print src, ': destination already exists'
		return
	try:
		mac.rename(src, dst)
	except mac.error, msg:
		print src, dst, ': rename failed:', msg

def do_mkdir(args):
	if not args:
		print 'usage: mkdir name ...'
		return
	for name in args:
		makedir(name)

def makedir(name):
	if exists(name):
		print name, ': already exists'
		return
	try:
		mac.mkdir(name, 0777)
	except mac.error, msg:
		print name, ': mkdir failed:', msg

def do_page(args):
	if not args:
		print 'usage: page file ...'
		return
	for name in args:
		pagefile(name)

def pagefile(name):
	if not isfile(name):
		print name, ': no such file'
		return
	LINES = 24 - 1
	# For THINK C 3.0, make the path absolute:
	# if not macpath.isabs(name):
	# 	name = macpath.cat(mac.getcwd(), name)
	try:
		fp = open(name, 'r')
	except:
		print name, ': cannot open'
		return
	line = fp.readline()
	while line:
		for i in range(LINES):
			print line,
			line = fp.readline()
			if not line: break
		if line:
			try:
				more = raw_input('[more]')
			except (EOFError, KeyboardInterrupt):
				print
				break
			if string.strip(more)[:1] in ('q', 'Q'):
				break

def do_pwd(args):
	if args:
		print 'usage: pwd'
	else:
		print mac.getcwd()

def do_reset(args):
	if args:
		print 'usage: reset'
	else:
		reset()

def do_rm(args):
	if not args:
		print 'usage: rm file ...'
		return
	for name in args:
		remove(name)

def remove(name):
	if not isfile(name):
		print name, ': no such file'
		return
	try:
		mac.unlink(name)
	except mac.error, msg:
		print name, ': unlink failed:', msg

def do_rmdir(args):
	if not args:
		print 'usage: rmdir dir ...'
		return
	for name in args:
		rmdir(name)

def rmdir(name):
	if not isdir(name):
		print name, ': no such directory'
		return
	try:
		mac.rmdir(name)
	except mac.error, msg:
		print name, ': rmdir failed:', msg

def do_sync(args):
	if args:
		print 'usage: sync'
		return
	try:
		mac.sync()
	except mac.error, msg:
		print 'sync failed:', msg

def do_unalias(args):
	if not args:
		print 'usage: unalias name ...'
		return
	for name in args:
		unalias(name)

def unalias(name):
	if not G.aliases.has_key(name):
		print name, ': no such alias'
		return
	del G.aliases[name]

def printlist(list):
	for word in list:
		print word,

reset()
main()