File: check-wmaker-loaddef-callbacks.sh

package info (click to toggle)
wmaker 0.96.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,416 kB
  • sloc: ansic: 99,483; sh: 7,068; perl: 3,423; makefile: 1,647; lisp: 219; python: 34
file content (510 lines) | stat: -rwxr-xr-x 17,444 bytes parent folder | download
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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
#!/bin/sh
###########################################################################
#
#  Window Maker window manager
#
#  Copyright (c) 2015 Christophe CURIS
#  Copyright (c) 2015 Window Maker Team
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################
#
# check-wmaker-loaddef-callbacks.sh:
#   Compare the type defined in a variable against the type use for the
#   associated call-back function.
#
# To load the configuration file, Window Maker is using a list of the known
# keywords in a structure with the name of the keyword, the call-back
# function that converts the string (from file) into the appropriate type,
# and a pointer to the variable where the result is saved.
#
# Because the structure requires a little bit of genericity to be usefull,
# it is not possible to provide the C compiler with the information to
# check that the variable from the pointer has the same type as what the
# conversion call-back assumes, so this script does that check for us.
#
# Unfortunately, the script cannot be completely generic and smart, but
# it still tries to be, despite being made explicitely for the case of the
# structures "staticOptionList" and "optionList" from Window Maker's source
# file "src/defaults.c"
#
###########################################################################
#
# For portability, we stick to the same sh+awk constraint as Autotools to
# limit problems, see for example:
#   http://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Portable-Shell.html
#
###########################################################################

# Report an error on stderr and exit with status 2 to tell make that we could
# not do what we were asked
arg_error() {
    echo "`basename $0`: $@" >&2
    exit 2
}

# print help and exit with success status
print_help() {
    echo "$0: check variable type against call-back expectation for WMaker's defaults.c"
    echo "Usage: $0 options..."
    echo "valid options are:"
    echo "  --callback \"name=type\" : specify that function 'name' expects a variable of 'type'"
    echo "  --field-callback idx   : index (from 1) of the callback function in the struct"
    echo "  --field-value-ptr idx  : index (from 1) of the pointer-to-value in the struct"
    echo "  --source file          : C source file with the array to check"
    echo "  --structure name       : name of the variable with the array of struct to check"
    echo "  --struct-def name=file : specify to get definition of struct 'name' from 'file'"
    exit 0
}

# Extract command line arguments
while [ $# -gt 0 ]; do
    case $1 in
        --callback)
            shift
            deflist="$1,"
            while [ -n "$deflist" ]; do
                name_and_type=`echo "$deflist" | cut -d, -f1 | sed -e 's/^[ \t]*//;s/[ \t]*$//' `
                deflist=`echo "$deflist" | cut -d, -f2-`
                echo "$name_and_type" | grep '^[A-Za-z][A-Za-z_0-9]*[ \t]*=[^=][^=]*$' > /dev/null ||  \
                    arg_error "invalid callback function type specification '$name_and_type' (options: --callback)"
                name=`echo "$name_and_type" | sed -e 's/=.*$// ; s/[ \t]//g' `
                type=`echo "$name_and_type" | sed -e 's/^[^=]*=// ; s/^[ \t]*// ; s/[ \t][ \t]*/ /g' `
                awk_callback_types="$awk_callback_types
  callback[\"$name\"] = \"$type\";"
            done
          ;;

        --field-callback)
            shift
            [ -z "$field_callback" ] || arg_error "field number specified more than once (option: --field-callback)"
            field_callback="$1"
          ;;

        --field-value-ptr)
            shift
            [ -z "$field_value" ] || arg_error "field number specified more than once (option: --field-value-ptr)"
            field_value="$1"
          ;;

        --source)
            shift
            [ -z "$source_file" ] || arg_error "only 1 source file can be used (option: --source)"
            source_file="$1"
          ;;

        --structure)
            shift
            [ -z "$struct_name" ] || arg_error "only 1 structure can be checked (option: --structure)"
            struct_name="$1"
          ;;

        --struct-def)
	    shift
            echo "$1" | grep '^[A-Za-z][A-Z_a-z0-9]*=' > /dev/null || arg_error "invalid syntax in \"$1\" for --struct-def"
            [ -r "`echo $1 | sed -e 's/^[^=]*=//' `" ] || arg_error "file not readable in struct-def \"$1\""
            list_struct_def="$list_struct_def
$1"
          ;;

        -h|-help|--help) print_help ;;
        -*) arg_error "unknown option '$1'" ;;

        *)
            arg_error "argument '$1' is not understood"
          ;;
    esac
    shift
done

# Check consistency of command-line
[ -z "$source_file" ] && arg_error "no source file given (option: --source)"
[ -z "$struct_name" ] && arg_error "no variable name given for the array to check (option: --structure)"
[ -z "$field_value" ] && arg_error "index of the value pointer in the struct no specified (option: --field-value-ptr)"
[ -z "$field_callback" ] && arg_error "index of the call-back in the struct no specified (option: --field-callback)"

echo "$field_value"    | grep '^[1-9][0-9]*$' > /dev/null || arg_error "invalid index for the value pointer, expecting a number (option: --field-value-ptr)"
echo "$field_callback" | grep '^[1-9][0-9]*$' > /dev/null || arg_error "invalid index for the call-back function, expecting a number (option: --field-callback)"

###########################################################################

# This AWK script is extracting the types associated with the field members
# from a specific structure defined in the parsed C source file
awk_extract_struct='
# Parse all the lines until the end of current structure is found
function parse_structure(prefix) {
  while (getline) {

    # Discard C comments, with support for multi-line comments
    while (1) {
      idx = index($0, "/*");
      if (idx == 0) { break; }

      comment = substr($0, idx+2);
      $0 = substr($0, 1, idx-1);
      while (1) {
        idx = index(comment, "*/");
        if (idx > 0) { break; }
        getline comment;
      }
      $0 = $0 substr(comment, idx+2);
    }

    # skip line that define nothing interresting
    gsub(/^[ \t]+/, "");
    if (/^$/) { continue; }
    if (/^#/) { continue; }
    gsub(/[ \t]+$/, "");

    # end of current structure: extract the name and return it
    if (/^[ \t]*\}/) {
      gsub(/^\}[ \t]*/, "");
      name = $0;
      gsub(/[ \t]*;.*$/, "", name);
      gsub(/^[^;]*;/, "");
      return name;
    }

    # Handle structure inside structure
    if (/^(struct|union)[ \t]+\{/) {
      name = parse_structure(prefix ",!");

      # Update the name tracking the content of this struct to contain the name
      # of the struct itself
      match_prefix = "^" prefix ",!:";
      for (var_id in member) {
        if (var_id !~ match_prefix) { continue; }
        new_id = var_id;
        gsub(/,!:/, ":" name ".", new_id);
        member[new_id] = member[var_id];
        delete member[var_id];
      }
      continue;
    }

    if (!/;$/) {
      print "Warning: line " FILENAME ":" NR " not understood inside struct" > "/dev/stderr";
      continue;
    }
    gsub(/;$/, "");

    # Skip the lines that define a bit-field because they cannot be safely
    # pointed to anyway
    if (/:/) { continue; }

    separate_type_and_names();

    # In some rare case we cannot extract the name, that is likely a function pointer type
    if (type == "") { continue; }

    # Save this information in an array
    for (i = 1; i <= nb_names; i++) {
      # If it is an array, push that information into the type
      idx = index(name_list[i], "[");
      if (idx > 0) {
        member[prefix ":" substr(name_list[i], 1, idx-1) ] = type substr(name_list[i], idx);
      } else {
        member[prefix ":" name_list[i] ] = type;
      }
    }
  }
}

# Separate the declaration of an member of the struct: its type and the list of names
# The result is returned through variables "name_list + nb_names" and "type"
function separate_type_and_names() {
  # Separate by names first
  nb_names = split($0, name_list, /[ \t]*,[ \t]*/);

  # Separate the type from the 1st name
  if (name_list[1] ~ /\]$/) {
    idx = index(name_list[1], "[") - 1;
  } else {
    idx = length(name_list[1]);
  }
  while (substr(name_list[1], idx, 1) ~ /[A-Za-z_0-9]/) { idx--; }

  type = substr(name_list[1], 1, idx);
  name_list[1] = substr(name_list[1], idx + 1);

  if (type ~ /\(/) {
    # If therese is a parenthesis in the type, that means we are parsing a function pointer
    # declaration. This is not supported at current time (not needed), so we silently ignore
    type = "";
    return;
  }

  # Remove size information from array declarations
  for (i in name_list) {
    gsub(/\[[^\]]+\]/, "[]", name_list[i]);
  }

  # Parse the type to make it into a "standard" format
  gsub(/[ \t]+$/, "", type);
  nb_elem = split(type, type_list, /[ \t]+/);
  type = "";
  for (i = 1; i <= nb_elem; i++) {
    if (type_list[i] == "signed" || type_list[i] == "unsigned") {
      # The sign information is not a problem for pointer compatibility, so we do not keep it
      continue;
    }
    if (type_list[i] ~ /^\*+$/) {
      # If we have a pointer mark by itself, we glue it to the previous keyword
      type = type type_list[i];
    } else {
      type = type " " type_list[i];
    }
  }
  gsub(/^ /, "", type);
  if (type ~ /^\*/ || type == "") {
    # We have a signed/unsigned without explicit "int" specification, add it now
    type = "int" type;
  }
}

# The name of the variable is at the end, so find all structure definition
/^([a-z]*[ \t][ \t]*)*struct[ \t]/  {

  # Discard all words to find the first ; or {
  gsub(/^([A-Za-z_0-9]*[ \t][ \t]*)+/, "");

  # If not an { it is probably not what we are looking for
  if (/^[^\{]/) { next; }

  # Read everything until we find the end of the structure; we assume a
  # definition is limited to one line
  name = parse_structure("@");

  # If the name is what we expect, generate the appropriate stuff
  if (name == expected_name) {
    struct_found++;
    for (i in member) {
      $0 = i;
      gsub(/^@:/, expected_name ".");
      print "  variable[\"" $0 "\"] = \"" member[i] "\";";
    }
  }

  # Purge the array to not mix fields between the different structures
  for (i in member) { delete member[i]; }
}

# Check that everything was ok
END {
  if (struct_found == 0) {
    print "Error: structure \"" expected_name "\" was not found in " FILENAME > "/dev/stderr";
    exit 1;
  } else if (struct_found > 1) {
    print "Error: structure \"" expected_name "\" was defined more than once in " FILENAME > "/dev/stderr";
    exit 1;
  }
}

# Do not print anything else than what is generated while parsing structures
{ }
'

# Extract the information for all the structures specified on the command line
awk_array_types=`echo "$list_struct_def" | while
  IFS="=" read name file
do
  [ -z "$name" ] && continue

  awk_script="
BEGIN {
  struct_found = 0;
  expected_name = \"$name\";
}
$awk_extract_struct"

  echo "  # $file"

  awk "$awk_script" "$file"
  [ $? -ne 0 ] && exit $?

done`

###########################################################################

# Parse the source file to extract the list of call-back functions that are
# used; take the opportunity to extract information about the variable
# being pointed to now to avoid re-parsing too many times the file
awk_check_callbacks='
# Search the final } for the current element in the array, then split the
# elements into the array "entry_elements" and remove that content from $0
function get_array_element_and_split() {
  nb_elements = 1;
  entry_elements[nb_elements] = "";

  $0 = substr($0, 2);
  count_braces = 1;
  while (count_braces > 0) {
    char = substr($0, 1, 1);
    $0 = substr($0, 2);
    if (char == "{") {
      count_braces++;
    } else if (char == "}") {
      count_braces--;
    } else if (char ~ /[ \t]/) {
      # Just discard
    } else if (char == ",") {
      if (count_braces == 1) { nb_elements++; entry_elements[nb_elements] = ""; }
    } else if (char == "\"") {
      entry_elements[nb_elements] = entry_elements[nb_elements] extract_string_to_element();
    } else if (char == "/") {
      if (substr($0, 1, 1) == "/") {
        getline;
        while (/^#/) { getline; }
      } else if (substr($0, 1, 1) == "*") {
        $0 = substr($0, 2);
        skip_long_comment();
      } else {
        entry_elements[nb_elements] = entry_elements[nb_elements] char;
      }
    } else if (char == "") {
      getline;
      while (/^#/) { print "skip: " $0; getline; }
    } else {
      entry_elements[nb_elements] = entry_elements[nb_elements] char;
    }
  }
}

# When a string enclosed in "" is encountered as part of the elements of the
# array, it requires special treatment, not to extract the information (we are
# unlikely to care about these fields) but to avoid mis-parsing the fields
function extract_string_to_element() {
  content = "\"";
  while (1) {
    char = substr($0, 1, 1);
    $0 = substr($0, 2);
    if (char == "\\") {
      content = content char substr($0, 1, 1);
      $0 = substr($0, 2);
    } else if (char == "\"") {
      break;
    } else if (char == "") {
      getline;
    } else {
      content = content char;
    }
  }
  return content "\"";
}

# Wherever a long C comment (/* comment */) is encountered, it is discarded
function skip_long_comment() {
  while (1) {
    idx = index($0, "*/");
    if (idx > 0) {
      $0 = substr($0, idx + 2);
      break;
    }
    getline;
  }
}

# Search for the definition of an array with the good name
/^[ \t]*([A-Z_a-z][A-Z_a-z0-9*]*[ \t]+)+'$struct_name'[ \t]*\[\][ \t]*=[ \t]*\{/ {
  struct_found++;

  $0 = substr($0, index($0, "{") + 1);

  # Parse all the elements of the array
  while (1) {

    # Search for start of an element
    while (1) {
      gsub(/^[ \t]+/, "");
      if (substr($0, 1, 1) == "{") { break; }
      if (substr($0, 1, 1) == "}") { break; }
      if (substr($0, 1, 1) == "#") { getline; continue; }
      if ($0 == "") { getline; continue; }

      # Remove comments
      if (substr($0, 1, 2) == "//") { getline; continue; }
      if (substr($0, 1, 2) == "/*") {
        $0 = substr($0, 3);
        skip_long_comment();
      } else {
        print "Warning: line " NR " not understood in " FILENAME ", skipped" > "/dev/stderr";
        getline;
      }
    }

    # Did we find the end of the array?
    if (substr($0, 1, 1) == "}") { break; }

    # Grab the whole content of the entry
    entry_start_line = NR;
    get_array_element_and_split();
    gsub(/^[ \t]*,/, "");

    # Extract the 2 fields we are interrested in
    if ((entry_elements[src_fvalue] != "NULL") && (entry_elements[src_ffunct] != "NULL")) {

      if (substr(entry_elements[src_fvalue], 1, 1) == "&") {
        entry_elements[src_fvalue] = substr(entry_elements[src_fvalue], 2);
      } else {
        print "Warning: value field used in entry at line " entry_start_line " does not looke like a pointer" > "/dev/stderr";
      }

      if (variable[entry_elements[src_fvalue]] == "") {
        print "Warning: type is not known for \"" entry_elements[src_fvalue] "\" at line " entry_start_line ", cannot check" > "/dev/stderr";
      } else if (callback[entry_elements[src_ffunct]] == "") {
        print "Error: expected type for callback function \"" entry_elements[src_ffunct] "\" is not known, from " FILENAME ":" entry_start_line > "/dev/stderr";
        error_count++;
      } else if (callback[entry_elements[src_ffunct]] != variable[entry_elements[src_fvalue]]) {
        print "Error: type mismatch between function and variable in " FILENAME ":" entry_start_line > "/dev/stderr";
        print "  Function: " entry_elements[src_ffunct] " expects \"" callback[entry_elements[src_ffunct]] "\"" > "/dev/stderr";
        print "  Variable: " entry_elements[src_fvalue] " has type \"" variable[entry_elements[src_fvalue]] "\"" > "/dev/stderr";
        error_count++;
      }

    }
  }
}

# Final checks
END {
  if (error_count > 0) { exit 1; }
  if (struct_found == 0) {
    print "Error: structure \"'$struct_name'\" was not found in " FILENAME > "/dev/stderr";
    exit 1;
  } else if (struct_found > 1) {
    print "Error: structure \"'$struct_name'\" was defined more than once in " FILENAME > "/dev/stderr";
    exit 1;
  }
}

# Do not print anything else than what is generated while parsing the structure
{ }
'

awk_script="BEGIN {
$awk_array_types
$awk_callback_types

  # Info on structure to be checked
  src_fvalue = $field_value;
  src_ffunct = $field_callback;

  # For checks
  struct_found = 0;
  error_count  = 0;
}
$awk_check_callbacks"

awk "$awk_script" "$source_file" || exit $?