Blame view

updusbdevs 18.3 KB
6998b45d   Mario Lang   Imported Upstream...
1
2
3
4
5
#!/usr/bin/env tclsh
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
ec859a60   samuel thibault   New upstream vers...
6
# Copyright (C) 1995-2018 by The BRLTTY Developers.
6998b45d   Mario Lang   Imported Upstream...
7
8
9
10
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
ec859a60   samuel thibault   New upstream vers...
11
12
13
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
6998b45d   Mario Lang   Imported Upstream...
14
#
ca6019f1   Mario Lang   Imported Upstream...
15
# Web Page: http://brltty.com/
6998b45d   Mario Lang   Imported Upstream...
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
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

source [file join [file dirname [info script]] "prologue.tcl"]

proc logDriverWarning {message} {
   logMessage warning $message
}

proc logSourceWarning {location message} {
   logDriverWarning "$message: [dict get $location file]\[[dict get $location line]\]"
}

proc makeDecimalNumber {value} {
   return [expr {$value + 0}]
}

proc formatDeviceIdentifier {vendor product} {
   return [format "%04X:%04X" [makeDecimalNumber $vendor] [makeDecimalNumber $product]]
}

proc makeGenericDeviceTable {definitions} {
   set table [dict create]

   foreach definition $definitions {
      set entry [dict create]

      foreach {index property} {
         2 vendor
         3 product
      } {
         if {$index < [llength $definition]} {
            if {[string length [set value [lindex $definition $index]]] > 0} {
               dict set entry $property $value
            }
         }
      }

      dict set table [formatDeviceIdentifier [lindex $definition 0] [lindex $definition 1]] $entry
   }

   return $table
}

proc getMakeProperty {file property} {
   set result ""

   if {[catch [list open $file {RDONLY}] stream] == 0} {
      while {[gets $stream line] >= 0} {
         if {[regexp "^$property\\s*=\\s*(.*?)\\s*\$" $line x value]} {
            set result $value
            break
         }
      }

      close $stream
   } else {
      writeProgramMessage $stream
   }

   return $result
}

proc getDriverProperty {driverDirectory propertyName} {
   return [getMakeProperty [file join $driverDirectory "Makefile.in"] $propertyName]
}

proc getDriverCode {driverDirectory} {
   return [getDriverProperty $driverDirectory DRIVER_CODE]
}

proc parseChannelDefinition {channelVariable lines location} {
   set channel [dict create]
   set ok 1

   foreach line $lines {
      if {[regexp {\{\s*/\*\s*(.*?)\s*\*/\s*$} $line x model]} {
         dict set channel model $model
         continue
      }

      if {[regexp {\.vendor\s*=\s*(\S*?)\s*,} $line x vendor]} {
         dict set channel vendor $vendor
      }

      if {[regexp {\.product\s*=\s*(\S*?)\s*,} $line x vendor]} {
         dict set channel product $vendor
      }
   }

   foreach property {model vendor product} {
      if {![dict exists $channel $property]} {
         logSourceWarning $location "property not defined: $property"
         set ok 0
      }
   }

   if {$ok} {
      foreach property {vendor product} {
         if {[string is integer -strict [set value [dict get $channel $property]]]} {
            dict set channel $property [makeDecimalNumber $value]
         } else {
            logSourceWarning $location "$property is not numeric: $value"
            dict unset channel $property
            set ok 0
         }
      }

      if {$ok} {
         uplevel 1 [list set $channelVariable $channel]
         return 1
      }
   }

   return 0
}

proc parseChannelDefinitions {channelsVariable lines location} {
   upvar 1 $channelsVariable channels
   set lineNumber [dict get $location line]

   foreach lineText $lines {
      if {[info exists definition]} {
         if {[regexp {\}} $lineText]} {
            if {[parseChannelDefinition channel $definition $location]} {
               lappend channels $channel
            }

            unset definition
         } else {
            lappend definition $lineText
         }
      } elseif {[regexp {\{} $lineText]} {
         set definition [list $lineText]
         dict set location line $lineNumber
      }

      incr lineNumber
   }
}

proc parseSourceFile {channelsVariable file} {
   upvar 1 $channelsVariable channels

   if {[catch [list open $file {RDONLY}] stream] == 0} {
      set location [dict create]
      dict set location file $file
      set lineNumber 0

      while {[gets $stream lineText] >= 0} {
         incr lineNumber

         if {[info exists definitions]} {
ca6019f1   Mario Lang   Imported Upstream...
170
            if {[regexp {^\s*END_USB_CHANNEL_DEFINITIONS} $lineText]} {
6998b45d   Mario Lang   Imported Upstream...
171
172
173
174
175
               parseChannelDefinitions channels $definitions $location
               unset definitions
            } else {
               lappend definitions $lineText
            }
ca6019f1   Mario Lang   Imported Upstream...
176
         } elseif {[regexp {\sBEGIN_USB_CHANNEL_DEFINITIONS} $lineText]} {
6998b45d   Mario Lang   Imported Upstream...
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
            set definitions [list]
            dict set location line [expr {$lineNumber + 1}]
         }
      }

      close $stream
   } else {
      writeProgramMessage $stream
   }
}

proc parseSourceFiles {channelsVariable driverDirectory} {
   upvar 1 $channelsVariable channels

   foreach file [glob -directory $driverDirectory -nocomplain {*.[ch]}] {
      parseSourceFile channels $file
   }

   return $channels
}

proc parseDriver {directory} {
   set driver [dict create]
   dict set driver name [set name [file tail $directory]]

   set channels [list]

   if {[string length [set code [getDriverCode $directory]]] > 0} {
      dict set driver code $code
      parseSourceFiles channels $directory
   } else {
      logDriverWarning "driver code not defined: $name"
   }

   dict set driver channels $channels
   return $driver
}

proc parseDrivers {} {
  global sourceDirectory

  set drivers [list]

  foreach directory [lsort [glob -directory [file join $sourceDirectory Drivers Braille] -types {d r x} -nocomplain *]] {
     if {[llength [dict get [set driver [parseDriver $directory]] channels]] > 0} {
        lappend drivers $driver
     }
  }

  return $drivers
}

proc formatDeviceDescription {device} {
   return "[dict get $device name]\[[dict get $device model]\]"
}

proc makeDeviceTable {drivers} {
   global genericDevices
   set deviceTable [dict create]
   dict set deviceTable vendors [dict create]

   foreach driver $drivers {
      dict with driver {
         foreach channel $channels {
            dict with channel {
               set identifier [formatDeviceIdentifier $vendor $product]
               set device [dict create code $code name $name model $model]

               if {[dict exists $deviceTable vendors $vendor products $product]} {
                  set devices [dict get $deviceTable vendors $vendor products $product devices]
                  logMessage notice "device specified more than once: $identifier: [formatDeviceDescription [lindex $devices 0]] & [formatDeviceDescription $device]"
               } else {
                  set devices [list]
                  dict set deviceTable vendors $vendor products $product generic [dict exists $genericDevices $identifier]
               }

               lappend devices $device
               dict set deviceTable vendors $vendor products $product devices $devices

               if {[dict get $deviceTable vendors $vendor products $product generic]} {
                  logMessage notice "generic device identifier: $identifier: [formatDeviceDescription $device]"
               }
            }
         }
      }
   }

   return $deviceTable
}

proc getSchemes {} {
   set schemes [list]
   set length [string length [set prefix makeLines_]]

   foreach command [info procs $prefix*] {
      lappend schemes [string range $command $length end]
   }

   return $schemes
}

proc parseFileArguments {filesArray schemes arguments} {
   upvar 1 $filesArray files

   foreach argument $arguments {
      if {[set index [string first : $argument]] < 0} {
         syntaxError "scheme not specified: $argument"
      }

      if {[string length [set scheme [string range $argument 0 $index-1]]] == 0} {
         syntaxError "scheme not specified: $argument"
      }

      if {[lsearch -exact $schemes $scheme] < 0} {
         syntaxError "unknown scheme: $scheme"
      }

      if {[string length [set path [string range $argument $index+1 end]]] == 0} {
         syntaxError "path not specified: $argument"
      }

      if {![file exists $path]} {
         semanticError "file not found: $path"
      }

      if {![file isfile $path]} {
         semanticError "not a file: $path"
      }

      if {![file readable $path]} {
         semanticError "file not readable: $path"
      }

      if {![file writable $path]} {
         semanticError "file not writable: $path"
      }

      lappend files($scheme) $path
   }
}

67e7bfbc   samuel thibault   New upstream vers...
318
proc xmlMakeComment {comment} {
6998b45d   Mario Lang   Imported Upstream...
319
320
321
   return "<!-- $comment -->"
}

67e7bfbc   samuel thibault   New upstream vers...
322
323
324
325
proc makeComment_android {comment} {
   return [xmlMakeComment $comment]
}

90c515c4   Mario Lang   Imported Upstream...
326
proc makeLines_android {vendor product drivers descriptions exclude} {
6998b45d   Mario Lang   Imported Upstream...
327
328
329
330
331
332
   set lines [list]

   foreach description $descriptions {
      lappend lines [makeComment_android $description]
   }

90c515c4   Mario Lang   Imported Upstream...
333
334
335
336
337
338
   set line "<usb-device vendor-id=\"$vendor\" product-id=\"$product\" />"
   if {$exclude} {
      set line [makeComment_android $line]
   }
   lappend lines $line

6998b45d   Mario Lang   Imported Upstream...
339
340
341
342
343
344
345
   return $lines
}

proc makeComment_hotplug {comment} {
   return "# $comment"
}

90c515c4   Mario Lang   Imported Upstream...
346
proc makeLines_hotplug {vendor product drivers descriptions exclude} {
6998b45d   Mario Lang   Imported Upstream...
347
348
349
350
351
352
   set lines [list]

   foreach description $descriptions {
      lappend lines [makeComment_hotplug $description]
   }

ca6019f1   Mario Lang   Imported Upstream...
353
   set line [format "brltty 0x%04x 0x%04x 0x%04x" 3 $vendor $product]
90c515c4   Mario Lang   Imported Upstream...
354
355
356
357
358
   if {$exclude} {
      set line [makeComment_hotplug $line]
   }
   lappend lines $line

6998b45d   Mario Lang   Imported Upstream...
359
360
361
   return $lines
}

67e7bfbc   samuel thibault   New upstream vers...
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
proc makeComment_metainfo {comment} {
   return [xmlMakeComment $comment]
}

proc makeLines_metainfo {vendor product drivers descriptions exclude} {
   set lines [list]

   foreach description $descriptions {
      lappend lines [makeComment_metainfo $description]
   }

   set line [format "<modalias>usb:v%04Xp%04X*</modalias>" $vendor $product]
   if {$exclude} {
      set line [makeComment_metainfo $line]
   }
   lappend lines $line

   return $lines
}

6998b45d   Mario Lang   Imported Upstream...
382
383
384
385
proc makeComment_udev {comment} {
   return "# $comment"
}

90c515c4   Mario Lang   Imported Upstream...
386
proc makeLines_udev {vendor product drivers descriptions exclude} {
6998b45d   Mario Lang   Imported Upstream...
387
388
389
390
391
392
   set lines [list]

   foreach description $descriptions {
      lappend lines [makeComment_udev $description]
   }

46c840f8   Mario Lang   Imported Upstream...
393
   set line [format "ENV\{PRODUCT\}==\"%x/%x/*\", ENV\{BRLTTY_BRAILLE_DRIVER\}=\"%s\", GOTO=\"brltty_usb_run\"" $vendor $product [join $drivers ,]]
90c515c4   Mario Lang   Imported Upstream...
394
395
396
397
398
   if {$exclude} {
      set line [makeComment_udev $line]
   }
   lappend lines $line

6998b45d   Mario Lang   Imported Upstream...
399
400
401
402
403
404
405
   return $lines
}

proc makeComment_windows {comment} {
   return "; $comment"
}

90c515c4   Mario Lang   Imported Upstream...
406
proc makeLines_windows {vendor product drivers descriptions exclude} {
6998b45d   Mario Lang   Imported Upstream...
407
   set lines [list]
90c515c4   Mario Lang   Imported Upstream...
408
409
410
411
412
413
414
   set line [format "\"\$1: %s\"=LIBUSB_DEV, USB\\VID_%04X&PID_%04X" [join $descriptions ", "] $vendor $product]

   if {$exclude} {
      set line [makeComment_windows $line]
   }

   lappend lines $line
6998b45d   Mario Lang   Imported Upstream...
415
416
417
418
419
   return $lines
}

proc makeLines {linesArray schemes deviceTable} {
   global genericDevices
90c515c4   Mario Lang   Imported Upstream...
420
   upvar #0 optionValues(nogeneric) excludeGenericDevices
6998b45d   Mario Lang   Imported Upstream...
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448

   foreach scheme $schemes {
      set makeLines makeLines_$scheme
      set makeComment makeComment_$scheme

      upvar 1 ${linesArray}($scheme) lines
      set lines [list ""]

      set vendors [dict get $deviceTable vendors]

      foreach vendor [lsort -integer [dict keys $vendors]] {
         set vendorEntry [dict get $vendors $vendor]
         set products [dict get $vendorEntry products]

         foreach product [lsort -integer [dict keys $products]] {
            set productEntry [dict get $products $product]

            set identifier [formatDeviceIdentifier $vendor $product]
            lappend lines [$makeComment "Device: $identifier"]

            set codes [list]
            set descriptions [list]

            foreach deviceEntry [dict get $productEntry devices] {
               lappend codes [dict get $deviceEntry code]
               lappend descriptions "[dict get $deviceEntry name] \[[dict get $deviceEntry model]\]"
            }

90c515c4   Mario Lang   Imported Upstream...
449
450
            set exclude 0

6998b45d   Mario Lang   Imported Upstream...
451
452
453
454
455
456
457
458
459
460
461
462
            if {[dict get $productEntry generic]} {
               lappend lines [$makeComment "Generic Identifier"]
               set generic [dict get $genericDevices $identifier]

               foreach {property header} {
                  vendor Vendor
                  product Product
               } {
                  if {[dict exists $generic $property]} {
                     lappend lines [$makeComment "$header: [dict get $generic $property]"]
                  }
               }
90c515c4   Mario Lang   Imported Upstream...
463
464

               set exclude $excludeGenericDevices
6998b45d   Mario Lang   Imported Upstream...
465
466
            }

ca6019f1   Mario Lang   Imported Upstream...
467
468
469
470
            set drivers [lsort -unique $codes]
            set descriptions [lsort $descriptions]

            eval [list lappend lines] [$makeLines $vendor $product $drivers $descriptions $exclude]
6998b45d   Mario Lang   Imported Upstream...
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
            lappend lines ""
         }
      }
   }
}

proc readFile {linesVariable file} {
   upvar 1 $linesVariable lines
   set lines [list]

   if {[catch [list open $file {RDONLY}] stream] == 0} {
      while {[gets $stream line] >= 0} {
         lappend lines $line
      }

      close $stream
      return 1
   } else {
      writeProgramMessage $stream
   }

   return 0
}

proc writeFile {lines file} {
   if {[catch [list open $file {WRONLY CREAT TRUNC}] stream] == 0} {
      puts $stream [join $lines \n]
      close $stream
      return 1
   } else {
      writeProgramMessage $stream
   }

   return 0
}

proc updateFile {file newLines} {
   upvar #0 optionValues(test) testMode

   if {[readFile lines $file]} {
      set markerSuffix "_USB_DEVICES"
      set beginMarker "BEGIN$markerSuffix"
      set endMarker "END$markerSuffix"

      set ranges [list]
      set range [list]
      set index 0

      foreach line $lines {
         set location "$file\[[expr {$index + 1}]\]"

         if {[regexp "\\s${beginMarker}(?:\\s+(.*?)\\s*)?\$" $line x arguments]} {
            if {[llength $range] > 0} {
               writeProgramMessage "nested begin marker: $location"
               return 0
            }

            lappend range $index [lindex [regexp -inline -- {^\s*} $line] 0] [regexp -inline -all {\S+} $arguments]
         } elseif {[regexp "\\s${endMarker}(?:\\s+.*)?\$" $line]} {
            if {[llength $range] == 0} {
               writeProgramMessage "missing begin marker: $location"
               return 0
            }

            lappend range $index
            lappend ranges $range
            set range [list]
         }

         incr index
      }

      if {[llength $range] > 0} {
         writeProgramMessage "missing end marker: $file\[[lindex $range 0]\]"
         return 0
      }

      if {[llength $ranges] == 0} {
         writeProgramMessage "no region(s) found: $file"
         return 0
      }

      set hasChanged 0

      foreach range [lreverse $ranges] {
         set first [expr {[lindex $range 0] + 1}]
         set prefix [lindex $range 1]
         set argumentCount [llength [set argumentList [lindex $range 2]]]
         set last [expr {[lindex $range 3] - 1}]
         set count [expr {$last - $first + 1}]

         set replacement [list]
         foreach line $newLines {
            foreach match [lreverse [regexp -inline -all -indices {\$[1-9][0-9]*} $line]] {
               set from [lindex $match 0]
               set to [lindex $match 1]
               set argumentIndex [string range $line $from+1 $to]

               if {[incr argumentIndex -1] < $argumentCount} {
                  set line [string replace $line $from $to [lindex $argumentList $argumentIndex]]
               }
            }

            if {[string length $line] > 0} {
               set line "$prefix$line"
            }

            lappend replacement "$line"
         }

         if {!$hasChanged} {
            if {[llength $newLines] != $count} {
               set hasChanged 1
            } else {
               set index 0

               while {$index < $count} {
                  if {[string compare [lindex $lines $first+$index] [lindex $replacement $index]] != 0} {
                     set hasChanged 1
                     break
                  }

                  incr index
               }
            }
         }

         if {$hasChanged} {
            if {$count == 0} {
               set lines [eval [list linsert $lines $first] $replacement]
            } else {
               set lines [eval [list lreplace $lines $first $last] $replacement]
            }
         }
      }

      if {!$hasChanged} {
         return 1
      }

      if {$testMode} {
         writeProgramMessage "test mode - file not updated: $file"
         return 1
      }

      if {[writeFile $lines $file]} {
67e7bfbc   samuel thibault   New upstream vers...
617
         logMessage warning "file updated: $file"
6998b45d   Mario Lang   Imported Upstream...
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
         return 1
      }
   }

   return 0
}

proc updateFiles {filesArray linesArray} {
   upvar 1 $filesArray files
   upvar 1 $linesArray lines

   foreach scheme [array names files] {
      foreach file $files($scheme) {
         updateFile $file $lines($scheme)
      }
   }
}

set genericDevices [makeGenericDeviceTable {
   {  0X0403 0X6001 
      "Future Technology Devices International, Ltd" 
      "FT232 USB-Serial (UART) IC"
   }

   {  0X10C4 0XEA60 
      "Cygnal Integrated Products, Inc." 
      "CP210x UART Bridge / myAVR mySmartUSB light"
   }

   {  0X10C4 0XEA80 
      "Cygnal Integrated Products, Inc." 
      "CP210x UART Bridge"
   }
}]

set optionDefinitions {
90c515c4   Mario Lang   Imported Upstream...
654
655
656
657
   {quiet     counter}
   {verbose   counter}
   {test      flag}
   {nogeneric flag}
6998b45d   Mario Lang   Imported Upstream...
658
659
660
661
662
663
664
665
666
667
668
}

if {![processOptions optionValues argv $optionDefinitions]} {
   syntaxError
}

incr logLevel $optionValues(quiet)
incr logLevel -$optionValues(verbose)

if {[llength $argv] == 0} {
   lappend argv "android:[file join $sourceDirectory Android Application res xml usb_devices.xml]"
90c515c4   Mario Lang   Imported Upstream...
669
   lappend argv "hotplug:[file join $sourceDirectory Autostart Hotplug brltty.usermap]"
67e7bfbc   samuel thibault   New upstream vers...
670
671
   lappend argv "metainfo:[file join $sourceDirectory Autostart AppStream org.a11y.brltty.metainfo.xml]"
   lappend argv "udev:[file join $sourceDirectory Autostart Udev rules]"
d8488191   samuel thibault   Imported Upstream...
672
   lappend argv "windows:[file join $sourceDirectory Autostart Windows brltty-libusb.inf]"
90c515c4   Mario Lang   Imported Upstream...
673
   lappend argv "windows:[file join $sourceDirectory Autostart Windows brltty-libusb-1.0.inf]"
6998b45d   Mario Lang   Imported Upstream...
674
675
676
677
678
679
680
681
682
683
684
}

if {[llength $argv] == 0} {
   syntaxError "files not specified"
}

set schemes [getSchemes]
parseFileArguments files $schemes $argv
makeLines lines [array names files] [makeDeviceTable [parseDrivers]]
updateFiles files lines
exit 0