summaryrefslogtreecommitdiffstats
path: root/tools/mof2wiki.sh
blob: 4ddb48be52300e2160d9886005c809faf8a37bf1 (plain)
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
#!/bin/bash

schemasurl='http://schemas.dmtf.org/wbem/cim-html/2.34.0/'

re_anyclass="^class[[:blank:]]*([[:alnum:]_]*):[[:blank:]]*([[:alnum:]_]*)"
re_property="^[[:blank:]]*([[:alnum:]_]*)[[:blank:]]*([[:alnum:]_]*)[[:blank:]]*;"
re_reference="^[[:blank:]]*([[:alnum:]_]*)[[:blank:]]*REF[[:blank:]]*([[:alnum:]_]*)[[:blank:]]*;"
re_endclass="^[[:blank:]]*}[[:blank:]]*;[[:blank:]]*"

usage()
{
  printf "$0 usage: "
  printf "COMMAND [PARAMETER] FILE [SUBDIR]\n"
  printf "Converts mof FILE to wiki type documentation\n"
  printf "Optionaly you can specify wiki SUBDIR"
  printf "Possible COMMANDs:\n"
  printf "all: creates list, properties and methods for all classes\n"
  printf "list: convert only all class names to pretty wiki table\n"
  printf "properties: need PARAMETER class name; converts specified class properties\n"
  printf "methods: need PARAMETER class name; converts specified class methods\n"
  printf "Example: $0 list LMI_Account.mof account\n"
  printf "Example: $0 properties LMI_Account LMI_Account.mof account\n"
}

die_usage()
{
  printf "%s\n" "$@"
  usage;
  exit 1
} >&2

die()
{
  printf "%s\n" "$@"
  exit 1
} >&2


all()
{
  moffile="$1"
  if [[ $2 && $2 =~ [^/]$ ]]; then
    subdir="$2"/
  else
    subdir=$2
  fi
  classes=()
  printf "||= Defined class name =||= Inherited from =||\n"
  while IFS= read line; do
    if [[ $line  =~ $re_anyclass ]]; then
      classes+=(${BASH_REMATCH[1]})
      newclass=${BASH_REMATCH[1]}
      derived=${BASH_REMATCH[2]}
      printf "|| [wiki:$subdir%s %s] || [$schemasurl%s.html %s] ||\n" "$newclass" "$newclass" "$derived" "$derived"
    fi
  done < "$moffile"
  echo
  for i in "${classes[@]}"; do
    props "$i" "$moffile" $subdir
    echo
  done
}

list()
{
  moffile="$1"
  if [[ $2 && $2 =~ [^/]$ ]]; then
    subdir="$2"/
  else
    subdir=$2
  fi
  printf "||= Defined class name =||= Inherited from =||\n"
  while IFS= read line; do
    if [[ $line  =~ $re_anyclass ]]; then
      newclass=${BASH_REMATCH[1]}
      derived=${BASH_REMATCH[2]}
      printf "|| [wiki:$subdir%s %s] || [$schemasurl%s.html %s] ||\n" "$newclass" "$newclass" "$derived" "$derived"
    fi
  done < "$moffile"
}

props()
{
  classname="$1"
  moffile="$2"
  if [[ $3 && $3 =~ [^/]$ ]]; then
    subdir="$3"/
  else
    subdir=$3
  fi

  unset properties references
  declare -A properties
  declare -A references
  declare -i found=0

  while IFS= read line; do
    if (( ! $found )); then
      if [[ $line  =~ ^class[[:blank:]]*($classname)":"[[:blank:]]*([[:alnum:]_]*) ]]; then
        newclass=${BASH_REMATCH[1]}
        derived=${BASH_REMATCH[2]}
        found=1
       fi
    else
      # we have found the class, now show new defined properties
      if [[ $line =~ $re_property ]]; then
        ptype=${BASH_REMATCH[1]}
        pname=${BASH_REMATCH[2]}
        properties["$pname"]="$ptype"
      elif [[ $line =~ $re_reference ]]; then
        ptype=${BASH_REMATCH[1]}
        pname=${BASH_REMATCH[2]}
        references["$pname"]="$ptype"
      elif [[ $line =~ $re_endclass ]]; then
          break;
      fi
    fi
  done < "$moffile"

  if (( $found )); then
    printf "= %s =\n" "$classname"
    printf "Along with inherited properties from [$schemasurl%s.html %s], %s defines also following properties\n" "$derived" "$derived" "$classname"
    printf "||= Property type =||= Property name =||\n"
    for prop in "${!properties[@]}"; do
      printf "|| {{{%s}}} || {{{%s}}}\n" "${properties[$prop]}" "$prop"
    done
    printf "Following references\n"
    printf "||= Class name =||= Reference name =||\n"
    for ref in "${!references[@]}"; do
      cname="${references[$ref]}"
      if [[ "$cname" =~ ^CIM ]]; then
        printf "|| [$schemasurl%s.html %s] || {{{%s}}}\n" "$cname" "$cname" "$ref"
      else
        printf "|| [wiki:$subdir%s %s] || {{{%s}}}\n" "$cname" "$cname" "$ref"
      fi
    done
  else
    printf "Class %s not found in the file %s\n" >&2 "$classname" "$moffile"
    exit 1
  fi
}

methods()
{
  die "Not implemented yet!"
  classname="$1"
  moffile="$2"
  if [[ $3 && $3 =~ [^/]$ ]]; then
    subdir="$3"/
  else
    subdir=$3
  fi

  unset methods
  methods=()

  while IFS= read line; do
    if (( ! $found )); then
      if [[ $line  =~ ^class[[:blank:]]*($classname)":"[[:blank:]]*([[:alnum:]_]*) ]]; then
        newclass=${BASH_REMATCH[1]}
        derived=${BASH_REMATCH[2]}
        found=1
       fi
    else
      # we have found the class, now show new defined methods
      if [[ $line =~ $re_property ]]; then
        ptype=${BASH_REMATCH[1]}
        pname=${BASH_REMATCH[2]}
        properties["$pname"]="$ptype"
      elif [[ $line =~ $re_reference ]]; then
        ptype=${BASH_REMATCH[1]}
        pname=${BASH_REMATCH[2]}
        references["$pname"]="$ptype"
      elif [[ $line =~ $re_endclass ]]; then
          break;
      fi
    fi
  done < "$moffile"

  if (( $found )); then
    printf "= %s =\n" "$classname"
    printf "Along with inherited properties from [$schemasurl%s.html %s], %s defines also following properties\n" "$derived" "$derived" "$classname"
    printf "||= Property type =||= Property name =||\n"
    for prop in "${!properties[@]}"; do
      printf "|| {{{%s}}} || {{{%s}}}\n" "${properties[$prop]}" "$prop"
    done
    printf "Following references\n"
    printf "||= Class name =||= Reference name =||\n"
    for ref in "${!references[@]}"; do
      cname="${references[$ref]}"
      if [[ "$cname" =~ ^CIM ]]; then
        printf "|| [$schemasurl%s.html %s] || {{{%s}}}\n" "$cname" "$cname" "$ref"
      else
        printf "|| [wiki:$subdir%s %s] || {{{%s}}}\n" "$cname" "$cname" "$ref"
      fi
    done
  else
    printf "Class %s not found in the file %s\n" >&2 "$classname" "$moffile"
    exit 1
  fi

}

export LC_ALL=C
case "$1" in
  all)
    if [[ ! -r "$2" ]]; then
      die "File $2 is not readable"
    fi
    all "$2" $3;;

  list)
    if [[ ! -r "$2" ]]; then
      die "File $2 is not readable"
    fi
    list "$2" $3;;

  properties)
    if [[ ! "$2" ]]; then
      die "You must specify class name"
    fi
    if [[ ! -r "$3" ]]; then
      die "File $3 is not readable"
    fi
    props "$2" "$3" $4;;

  methods)
    if [[ ! "$2" ]]; then
      die "You must specify class name"
    fi
    if [[ ! -r "$3" ]]; then
      die "File $3 is not readable"
    fi
    methods "$2" "$3" $4;;

  *) die_usage "Invalid command $1";;
esac

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  usage
  exit 0
fi