package obus

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file oBus_name.ml

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
(*
 * oBus_name.ml
 * ------------
 * Copyright : (c) 2008, Jeremie Dimino <jeremie@dimino.org>
 * Licence   : BSD3
 *
 * This file is a part of obus, an ocaml implementation of D-Bus.
 *)

open String
open OBus_string

type bus = string
type interface = string
type member = string
type error = string

(* +-----------------------------------------------------------------+
   | Bus names                                                       |
   +-----------------------------------------------------------------+ *)

let is_unique name = length name > 0 && unsafe_get name 0 = ':'

let validate_unique_connection str =
  let fail i msg = Some{ typ = "unique connection name"; str = str; ofs = i; msg = msg }
  and len = length str in

  let rec element_start i =
    if i = len then
      fail i "empty element"
    else match unsafe_get str i with
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
          element (i + 1)
      | '.' ->
          fail i "empty element"
      | _ ->
          fail i "invalid character"

  and element i =
    if i = len then
      None
    else match unsafe_get str i with
      | '.' ->
          element_start (i + 1)
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
          element (i + 1)
      | _ ->
          fail i "invalid character"

  and first_element i =
    if i = len then
      fail (-1) "must contains at least two elements"
    else match unsafe_get str i with
      | '.' ->
          element_start (i + 1)
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
          first_element (i + 1)
      | _ ->
          fail i "invalid character"
  in

  if len > OBus_protocol.max_name_length then
    fail (-1) "name too long"
  else if len = 1 then
    fail 1 "premature end of name"
  else match unsafe_get str 1 with
    | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
        first_element 2
    | '.' ->
        fail 1 "empty element"
    | _ ->
        fail 1 "invalid character"

let validate_bus_other str =
  let fail i msg = Some{ typ = "unique connection name"; str = str; ofs = i; msg = msg }
  and len = length str in

  let rec element_start i =
    if i = len then
      fail i "empty element"
    else match unsafe_get str i with
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' ->
          element (i + 1)
      | '.' ->
          fail i "empty element"
      | _ ->
          fail i "invalid character"

  and element i =
    if i = len then
      None
    else match unsafe_get str i with
      | '.' ->
          element_start (i + 1)
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
          element (i + 1)
      | _ ->
          fail i "invalid character"

  and first_element i =
    if i = len then
      fail (-1) "must contains at least two elements"
    else match unsafe_get str i with
      | '.' ->
          element_start (i + 1)
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
          first_element (i + 1)
      | _ ->
          fail i "invalid character"
  in

  if len > OBus_protocol.max_name_length then
    fail (-1) "name too long"
  else match unsafe_get str 1 with
    | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' | '0' .. '9'->
        first_element 2
    | '.' ->
        element_start 2
    | _ ->
        fail 1 "invalid character"

let validate_bus = function
  | "" ->
      Some{ typ = "bus name"; str = ""; ofs = -1; msg = "empty name" }
  | str ->
      match unsafe_get str 0 with
        | ':' -> validate_unique_connection str
        | 'A' .. 'Z' | 'a' .. 'z' | '_' | '-' -> validate_bus_other str
        | '.' -> Some{ typ = "bus name"; str = str; ofs = 0; msg = "empty element" }
        | _ -> Some{ typ = "bus name"; str = str; ofs = 0; msg = "invalid character" }

(* +-----------------------------------------------------------------+
   | Interface names                                                 |
   +-----------------------------------------------------------------+ *)

let validate_interface str =
  let fail i msg = Some{ typ = "interface name"; str = str; ofs = i; msg = msg }
  and len = length str in

  let rec element_start i =
    if i = len then
      fail i "empty element"
    else match unsafe_get str i with
      | 'A' .. 'Z' | 'a' .. 'z' | '_' ->
          element (i + 1)
      | '.' ->
          fail i "empty element"
      | _ ->
          fail i "invalid character"

  and element i =
    if i = len then
      None
    else match unsafe_get str i with
      | '.' ->
          element_start (i + 1)
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '0' .. '9' ->
          element (i + 1)
      | _ ->
          fail i "invalid character"

  and first_element i =
    if i = len then
      fail (-1) "must contains at least two elements"
    else match unsafe_get str i with
      | '.' ->
          element_start (i + 1)
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '0' .. '9' ->
          first_element (i + 1)
      | _ ->
          fail i "invalid character"
  in

  if len > OBus_protocol.max_name_length then
    fail (-1) "name too long"
  else if len = 0 then
    fail (-1) "empty name"
  else match unsafe_get str 0 with
    | 'A' .. 'Z' | 'a' .. 'z' | '_' ->
        first_element 1
    | '.' ->
        fail 0 "empty element"
    | _ ->
        fail 0 "invalid character"

(* +-----------------------------------------------------------------+
   | Member names                                                    |
   +-----------------------------------------------------------------+ *)

let validate_member str =
  let fail i msg = Some{ typ = "member name"; str = str; ofs = i; msg = msg }
  and len = length str in

  let rec aux i =
    if i = len then
      None
    else match unsafe_get str i with
      | 'A' .. 'Z' | 'a' .. 'z' | '_' | '0' .. '9' ->
          aux (i + 1)
      | _ ->
          fail i "invalid character"
  in

  if len > OBus_protocol.max_name_length then
    fail (-1) "name too long"
  else if len = 0 then
    fail (-1) "empty name"
  else match unsafe_get str 0 with
    | 'A' .. 'Z' | 'a' .. 'z' | '_' ->
        aux 1
    | _ ->
        fail 0 "invalid character"

(* +-----------------------------------------------------------------+
   | Error names                                                     |
   +-----------------------------------------------------------------+ *)

let validate_error str =
  (* Error names have the same restriction as interface names *)
  match validate_interface str with
    | None ->
        None
    | Some error ->
        Some{ error with typ = "error name" }

(* +-----------------------------------------------------------------+
   | Name translation                                                |
   +-----------------------------------------------------------------+ *)

(* Split a name into blocks. Blocks are the longest sub-strings
   matched by the regulare expression: "[A-Z]*[^A-Z.]*" *)
let split name =

  (* Recognize the first part of a block: "[A-Z]*" *)
  let rec part1 i =
    if i = String.length name then
      i
    else
      match name.[i] with
        | 'A' .. 'Z' ->
            part1 (i + 1)
        | _ ->
            part2 i

  (* Recognize the second part of a block: "[^A-Z.]*" *)
  and part2 i =
    if i = String.length name then
      i
    else
      match name.[i] with
        | 'A' .. 'Z' | '.' ->
            i
        | _ ->
            part2 (i + 1)

  in

  let rec split i =
    if i = String.length name then
      []
    else
      let j = part1 i in
      if j = i then
        (* Skip empty blocks *)
        split (i + 1)
      else
        String.sub name i (j - i) :: split j

  in
  split 0

let ocaml_lid name = String.uncapitalize_ascii (String.concat "_" (List.map String.lowercase_ascii (split name)))
let ocaml_uid name = String.capitalize_ascii (String.concat "_" (List.map String.lowercase_ascii (split name)))

let haskell_lid name = String.uncapitalize_ascii (String.concat "" (split name))
let haskell_uid name = String.capitalize_ascii (String.concat "" (split name))
OCaml

Innovation. Community. Security.