raz / examples / Flags.raz

44 lines · 1.0 KiB · raw · history · last change 2411fde8

1(* Enumerations, sets, record literals and defer: Unix permissions as
2 sets, which are their bits, and a report that closes its brackets
3 however it leaves. *)
4module Flags;
5import Out;
6
7type
8 Perm = (x, w, r); (* bits 0, 1, 2, as chmod has them *)
9 Perms = set of Perm;
10 Mode = record user, group, other: Perms; end;
11
12proc digit(p: Perms): INT;
13begin
14 return INT(U8(p));
15end;
16
17proc show(p: Perms);
18begin
19 if r in p then Out.str("r"); else Out.str("-"); end;
20 if w in p then Out.str("w"); else Out.str("-"); end;
21 if x in p then Out.str("x"); else Out.str("-"); end;
22end;
23
24proc report(m: Mode);
25begin
26 Out.str("[");
27 defer Out.str("]\n");
28 show(m.user); show(m.group); show(m.other);
29 if m.other = {} then return; end;
30 Out.str(" octal ");
31 Out.int(digit(m.user)); Out.int(digit(m.group)); Out.int(digit(m.other));
32end;
33
34proc main*;
35var m: Mode;
36begin
37 m := {user: {r, w, x}, group: {r, x}, other: {r}};
38 report(m);
39 m.user := m.user - {x};
40 m.other := {};
41 report(m);
42end;
43
44end Flags.