summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Lowther <victor.lowther@gmail.com>2009-11-16 11:07:39 -0600
committerVictor Lowther <victor.lowther@gmail.com>2009-11-16 11:07:39 -0600
commitea9c198040f9b70f474ef8297e227e70f56aa505 (patch)
tree051ff77ad45c48e24cf1d699af8efac285dd8dfa
parentc4f1ddf6b31dc24e0181dea1d0495fc359f4d2d9 (diff)
Actaully add the new .fdi importing script.
-rw-r--r--src/import-fdi-quirkdb.in129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/import-fdi-quirkdb.in b/src/import-fdi-quirkdb.in
new file mode 100644
index 0000000..987b21d
--- /dev/null
+++ b/src/import-fdi-quirkdb.in
@@ -0,0 +1,129 @@
+#!/bin/bash
+. "@PM-UTILS-LIBDIR@/pm-functions"
+
+begin_match='<match key="([a-z._]+)" ([a-z_]+)="([^"]+)">'
+end_match='</match>'
+merge='<merge key="([^"]+)" type="bool">true</merge>'
+remove='<remove key="([^"]+)"></remove>'
+begin_comment='<!--'
+end_comment='-->'
+in_comment=0
+in_match=0
+
+# test to see if the parameter passed is a decimal or hexidecimal number
+# Note the complete lack of floating point support.
+isnum() {
+ [[ $1 =~ ^[0-9]+\$ || $1 =~ ^0[xX][0-9a-fA-F]+\$ ]]
+}
+
+space_echo() {
+ for ((i=$1; i>0; i--)); do
+ printf ' '
+ done
+ shift
+ echo "$1"
+}
+
+# Helper function for when translating from .fdi to native format,
+# we need to massage things a little.
+escape_match() {
+ local fun lit
+ fun="regex"
+ # first, escape special ere characters
+ lit="$2"
+ lit="${lit//\\/\\\\}"
+ lit="${lit//./\\.}"
+ lit="${lit//^/\\^}"
+ lit="${lit//[/\\\[}"
+ lit="${lit//]/\\\]}"
+ lit="${lit//(/\\\(}"
+ lit="${lit//)/\\\)}"
+ lit="${lit//\*/\\\*}"
+ lit="${lit//\+/\\\+}"
+ lit="${lit//\?/\\\?}"
+ lit="${lit//\{/\\\{}"
+ lit="${lit//\}/\\\}}"
+ lit="${lit//\|/\\\|}"
+ lit="${lit//\$/\\\$}"
+ # nuke leading and trailing spaces, they just confuse things.
+ lit="${lit%%+( )}"
+ lit="${lit##+( )}"
+ # second, handle the various comparison cases, munging them into
+ # extended regular expressions that bash understands.
+ case $1 in
+ string) lit="^${lit}\$" ;;
+ int|uint64) fun=numeric_compare_eq ;;
+ string_outof) lit="${lit%;}"; lit="^(${lit//@(*( );*( ))/|})\$" ;;
+ int_outof) fun=numeric_compare_eq_list ;;
+ contains) ;;
+ contains_outof) lit="${lit%;}"; lit="${lit//@(*( );*( ))/|}" ;;
+ prefix) lit="^${lit}" ;;
+ prefix_outof) lit="${lit%;}"; lit="^(${lit//@(*( );*( ))/|})" ;;
+ suffix) lit="${lit}\$" ;;
+ contains_ncase) fun=regex_ncase ;;
+ contains_not) fun=regex_inverse ;;
+ prefix_ncase) fun=regex_ncase; lit="^${lit}" ;;
+ suffix_ncase) fun=regex_ncase; lit="${lit}\$" ;;
+ compare_lt|compare_le|compare_gt|compare_ge|compare_ne)
+ # I know, this may not always be the right thing to do.
+ # It is the right thing most of the time, though.
+ if isnum "$lit"; then
+ fun="numeric_$1"
+ else
+ fun="$1"
+ fi
+ ;;
+ *) echo "$1 not implemented." >&2 ; exit 1 ;;
+ esac
+ echo "$fun $lit"
+}
+
+# This is so not the right way to parse XML, but it happens to work with
+# the current hal-info video quirk .fdi files. Maybe someday I will rewrite
+# this in a language that actually understands XML, even though XML is the devil.
+translate_xml() {
+ local line key val matcher in_comment in_matcher
+ while read line; do
+ [[ "$line" ]] || continue
+ if [[ $line =~ $begin_comment && $line =~ $end_comment ]]; then
+ space_echo $in_match "# $line"
+ elif [[ $line =~ $begin_comment ]]; then
+ space_echo $in_match "# $line"
+ ((in_comment++))
+ elif [[ $line =~ $end_comment ]]; then
+ space_echo $in_match "# $line"
+ ((in_comment--))
+ elif ((in_comment > 0)); then
+ space_echo $in_match "# $line"
+ elif [[ $line =~ $begin_match ]]; then
+ ((in_match++))
+ key="${BASH_REMATCH[1]}"
+ matcher="${BASH_REMATCH[2]}"
+ val="${BASH_REMATCH[3]}"
+ space_echo $in_match "match $key $(escape_match "$matcher" "$val")"
+ elif [[ $line =~ $end_match ]]; then
+ space_echo $in_match "endmatch"
+ ((in_match--))
+ elif [[ $line =~ $merge ]]; then
+ key="${BASH_REMATCH[1]##*.}"
+ space_echo $((in_match + 1)) "addquirk --quirk-${key//_/-}"
+ elif [[ $line =~ $remove ]]; then
+ key="${BASH_REMATCH[1]##*.}"
+ space_echo $((in_match + 1)) "delquirk --quirk-${key//_/-}"
+ fi
+ done
+
+}
+
+mkdir -p "$PM_QUIRKDB"
+for f in /etc/hal/fdi/information/99local-pm-utils-quirks.fdi \
+ /usr/share/hal/fdi/information/10freedesktop/20-video-quirk*.fdi;
+do
+ lf="$PM_QUIRKDB/${f##*/}"
+ lf="${lf%.*}.quirkdb"
+ [[ -f $f ]] || continue
+ [[ $f -nt $lf ]] || continue
+ translate_xml < "$f" > "$lf"
+ rm "$PM_LKW_QUIRKS" >/dev/null 2>&1
+done
+