| 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. *) |
| 4 | module Flags; |
| 5 | import Out; |
| 6 | |
| 7 | type |
| 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 | |
| 12 | proc digit(p: Perms): INT; |
| 13 | begin |
| 14 | return INT(U8(p)); |
| 15 | end; |
| 16 | |
| 17 | proc show(p: Perms); |
| 18 | begin |
| 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; |
| 22 | end; |
| 23 | |
| 24 | proc report(m: Mode); |
| 25 | begin |
| 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)); |
| 32 | end; |
| 33 | |
| 34 | proc main*; |
| 35 | var m: Mode; |
| 36 | begin |
| 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); |
| 42 | end; |
| 43 | |
| 44 | end Flags. |