about summary refs log tree commit diff stats
path: root/src/config/chapath.nim
blob: d9ba3c509087e67c0d6b644018805941ef19dce8 (plain) (blame)
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
import std/options
import std/os
import std/posix

import js/fromjs
import js/javascript
import js/jserror
import js/tojs
import types/opt
import utils/twtstr

const libexecPath {.strdefine.} = "${%CHA_BIN_DIR}/../libexec/chawan"

type ChaPath* = distinct string

func `$`*(p: ChaPath): string =
  return string(p)

type
  UnquoteContext = object
    state: UnquoteState
    s: string
    p: string
    i: int
    identStr: string
    subChar: char
    hasColon: bool
    terminal: Option[char]

  UnquoteState = enum
    usNormal, usTilde, usDollar, usIdent, usBslash, usCurlyStart, usCurly,
    usCurlyHash, usCurlyPerc, usCurlyColon, usCurlyExpand, usDone

  ChaPathError = string

  ChaPathResult[T] = Result[T, ChaPathError]

proc unquote*(p: ChaPath): ChaPathResult[string]
proc unquote(p: string; starti: var int; terminal: Option[char]):
    ChaPathResult[string]

proc stateNormal(ctx: var UnquoteContext; c: char) =
  case c
  of '$': ctx.state = usDollar
  of '\\': ctx.state = usBslash
  of '~':
    if ctx.i == 0:
      ctx.state = usTilde
    else:
      ctx.s &= c
  elif ctx.terminal.isSome and ctx.terminal.get == c:
    ctx.state = usDone
  else:
    ctx.s &= c

proc flushTilde(ctx: var UnquoteContext) =
  if ctx.identStr == "":
    ctx.s &= getHomeDir()
  else:
    let p = getpwnam(cstring(ctx.identStr))
    if p != nil:
      ctx.s &= $p.pw_dir
    ctx.identStr = ""
  ctx.state = usNormal

proc stateTilde(ctx: var UnquoteContext; c: char) =
  if c != '/':
    ctx.identStr &= c
  else:
    ctx.flushTilde()

# Kind of a hack. We special case `\$' (backslash-dollar) in TOML, so that
# it produces itself in dquote strings.
# Thus by applying stateBSlash we get '\$' -> "$", but also "\$" -> "$".
proc stateBSlash(ctx: var UnquoteContext; c: char) =
  if c != '$':
    ctx.s &= '\\'
  ctx.s &= c
  ctx.state = usNormal

proc stateDollar(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # $
  case c
  of '$':
    ctx.s &= $getCurrentProcessId()
    ctx.state = usNormal
  of '0':
    # Note: we intentionally use getAppFileName so that any symbolic links
    # are resolved.
    ctx.s &= getAppFileName()
    ctx.state = usNormal
  of '1'..'9':
    return err("Parameter substitution is not supported")
  of AsciiAlpha:
    ctx.identStr = $c
    ctx.state = usIdent
  of '{':
    ctx.state = usCurlyStart
  else:
    # > If an unquoted '$' is followed by a character that is not one of
    # > the following: [...] the result is unspecified.
    # just error out here to be safe
    return err("Invalid dollar substitution")
  ok()

proc flushIdent(ctx: var UnquoteContext) =
  ctx.s &= getEnv(ctx.identStr)
  ctx.identStr = ""

const BareChars = AsciiAlphaNumeric + {'_'}

proc stateIdent(ctx: var UnquoteContext; c: char) =
  # $ident
  if c in BareChars:
    ctx.identStr &= c
  else:
    ctx.flushIdent()
    dec ctx.i
    ctx.state = usNormal

proc stateCurlyStart(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # ${
  case c
  of '#':
    ctx.state = usCurlyHash
  of '%':
    ctx.state = usCurlyPerc
  of BareChars:
    ctx.state = usCurly
    dec ctx.i
  else:
    return err("unexpected character in substitution: '" & c & "'")
  return ok()

proc stateCurly(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # ${ident
  case c
  of '}':
    ctx.s &= $getEnv(ctx.identStr)
    ctx.state = usNormal
    return ok()
  of '$': # allow $ as first char only
    if ctx.identStr.len > 0:
      return err("unexpected dollar sign in substitution")
    ctx.identStr &= c
    return ok()
  of ':', '-', '?', '+': # note: we don't support `=' (assign)
    if ctx.identStr.len == 0:
      return err("substitution without parameter name")
    if c == ':':
      ctx.state = usCurlyColon
    else:
      ctx.subChar = c
      ctx.state = usCurlyExpand
    return ok()
  of '1'..'9':
    return err("Parameter substitution is not supported")
  of BareChars - {'1'..'9'}:
    ctx.identStr &= c
    return ok()
  else:
    return err("unexpected character in substitution: '" & c & "'")

proc stateCurlyHash(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # ${#ident
  if c == '}':
    let s = getEnv(ctx.identStr)
    ctx.s &= $s.len
    ctx.identStr = ""
    ctx.state = usNormal
    return ok()
  if c == '$': # allow $ as first char only
    if ctx.identStr.len > 0:
      return err("unexpected dollar sign in substitution")
    # fall through
  elif c notin BareChars:
    return err("unexpected character in substitution: '" & c & "'")
  ctx.identStr &= c
  return ok()

proc stateCurlyPerc(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # ${%ident
  if c == '}':
    if ctx.identStr == "CHA_BIN_DIR":
      ctx.s &= getAppFileName().beforeLast('/')
    elif ctx.identStr == "CHA_LIBEXEC_DIR":
      ctx.s &= ?ChaPath(libexecPath).unquote()
    else:
      return err("Unknown internal variable " & ctx.identStr)
    ctx.identStr = ""
    ctx.state = usNormal
    return ok()
  if c notin BareChars:
    return err("unexpected character in substitution: '" & c & "'")
  ctx.identStr &= c
  return ok()

proc stateCurlyColon(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # ${ident:
  if c notin {'-', '?', '+'}: # Note: we don't support `=' (assign)
    return err("unexpected character after colon: '" & c & "'")
  ctx.hasColon = true
  ctx.subChar = c
  ctx.state = usCurlyExpand
  return ok()

proc flushCurlyExpand(ctx: var UnquoteContext; word: string):
    ChaPathResult[void] =
  case ctx.subChar
  of '-':
    if ctx.hasColon:
      ctx.s &= getEnv(ctx.identStr, word)
    else:
      if existsEnv(ctx.identStr):
        ctx.s &= getEnv(ctx.identStr)
      else:
        ctx.s &= word
  of '?':
    if ctx.hasColon:
      let s = getEnv(ctx.identStr)
      if s.len == 0:
        return err(word)
      ctx.s &= s
    else:
      if not existsEnv(ctx.identStr):
        return err(word)
      ctx.s &= getEnv(ctx.identStr)
  of '+':
    if ctx.hasColon:
      if getEnv(ctx.identStr).len > 0:
        ctx.s &= word
    else:
      if existsEnv(ctx.identStr):
        ctx.s &= word
  else: assert false
  ctx.subChar = '\0'
  ctx.hasColon = false
  ctx.state = usNormal
  return ok()

proc stateCurlyExpand(ctx: var UnquoteContext; c: char): ChaPathResult[void] =
  # ${ident:-[word], ${ident:=[word], ${ident:?[word], ${ident:+[word]
  # word must be unquoted too.
  let word = ?unquote(ctx.p, ctx.i, some('}'))
  return ctx.flushCurlyExpand(word)

proc unquote(p: string; starti: var int; terminal: Option[char]):
    ChaPathResult[string] =
  var ctx = UnquoteContext(p: p, i: starti, terminal: terminal)
  while ctx.i < p.len:
    let c = p[ctx.i]
    case ctx.state
    of usNormal: ctx.stateNormal(c)
    of usTilde: ctx.stateTilde(c)
    of usBslash: ctx.stateBSlash(c)
    of usDollar: ?ctx.stateDollar(c)
    of usIdent: ctx.stateIdent(c)
    of usCurlyStart: ?ctx.stateCurlyStart(c)
    of usCurly: ?ctx.stateCurly(c)
    of usCurlyHash: ?ctx.stateCurlyHash(c)
    of usCurlyPerc: ?ctx.stateCurlyPerc(c)
    of usCurlyColon: ?ctx.stateCurlyColon(c)
    of usCurlyExpand: ?ctx.stateCurlyExpand(c)
    of usDone: break
    inc ctx.i
  case ctx.state
  of usNormal, usDone: discard
  of usTilde: ctx.flushTilde()
  of usBslash: ctx.s &= '\\'
  of usDollar: ctx.s &= '$'
  of usIdent: ctx.flushIdent()
  of usCurlyStart, usCurly, usCurlyHash, usCurlyPerc, usCurlyColon:
    return err("} expected")
  of usCurlyExpand:
    ?ctx.flushCurlyExpand("")
  starti = ctx.i
  return ok(ctx.s)

proc unquote(p: string): ChaPathResult[string] =
  var dummy = 0
  return unquote(p, dummy, none(char))

proc toJS*(ctx: JSContext; p: ChaPath): JSValue =
  toJS(ctx, $p)

proc fromJSChaPath*(ctx: JSContext; val: JSValue): JSResult[ChaPath] =
  return cast[JSResult[ChaPath]](fromJS[string](ctx, val))

proc unquote*(p: ChaPath): ChaPathResult[string] =
  let s = ?unquote(string(p))
  return ok(normalizedPath(s))

proc unquoteGet*(p: ChaPath): string =
  return p.unquote().get