xref: /inferno-os/appl/cmd/getfile.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1implement Getfile;
2
3include "sys.m";
4	sys: Sys;
5	stderr: ref Sys->FD;
6include "draw.m";
7	draw: Draw;
8	Rect: import draw;
9include "tk.m";
10	tk: Tk;
11include "wmlib.m";
12	wmlib: Wmlib;
13include "arg.m";
14
15Getfile: module
16{
17	init:	fn(ctxt: ref Draw->Context, argv: list of string);
18};
19
20usage()
21{
22	sys->fprint(stderr, "usage: getfile [-g geom] [-d startdir] [pattern...]\n");
23	raise "fail:usage";
24}
25
26init(ctxt: ref Draw->Context, argv: list of string)
27{
28	sys = load Sys Sys->PATH;
29	stderr = sys->fildes(2);
30	draw = load Draw Draw->PATH;
31	tk = load Tk Tk->PATH;
32	wmlib = load Wmlib Wmlib->PATH;
33	if (wmlib == nil) {
34		sys->fprint(stderr, "getfile: cannot load %s: %r\n", Wmlib->PATH);
35		raise "fail:bad module";
36	}
37	arg := load Arg Arg->PATH;
38	if (arg == nil) {
39		sys->fprint(stderr, "getfile: cannot load %s: %r\n", Arg->PATH);
40		raise "fail:bad module";
41	}
42
43	if (ctxt == nil) {
44		sys->fprint(stderr, "getfile: no window context\n");
45		raise "fail:bad context";
46	}
47
48	wmlib->init();
49
50	startdir := ".";
51	geom := "-x " + string (ctxt.screen.image.r.dx() / 5) +
52			" -y " + string (ctxt.screen.image.r.dy() / 5);
53	title := "Select a file";
54	arg->init(argv);
55	while (opt := arg->opt()) {
56		case opt {
57		'g' =>
58			geom = arg->arg();
59		'd' =>
60			startdir = arg->arg();
61		't' =>
62			title = arg->arg();
63		* =>
64			sys->fprint(stderr, "getfile: unknown option -%c\n", opt);
65			usage();
66		}
67	}
68	if (geom == nil || startdir == nil || title == nil)
69		usage();
70	top := tk->toplevel(ctxt.screen, geom);
71	argv = arg->argv();
72	arg = nil;
73	sys->print("%s\n", wmlib->filename(ctxt.screen, top, title, argv, startdir));
74}
75