PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mk/check/check-portability.awk

https://bitbucket.org/dabomb69/pkgsrc
AWK | 87 lines | 58 code | 13 blank | 16 comment | 0 complexity | f8d7c290f76ead636e7b690f869c7c99 MD5 | raw file
Possible License(s): Unlicense, Cube, BSD-2-Clause, IPL-1.0, BSD-3-Clause, LGPL-2.1, MIT, CC-BY-SA-3.0, Apache-2.0, MPL-2.0, MPL-2.0-no-copyleft-exception, GPL-3.0, WTFPL, LGPL-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, 0BSD, AGPL-3.0
  1. # $NetBSD: check-portability.awk,v 1.4 2006/11/13 23:21:53 rillig Exp $
  2. #
  3. # Checks a shell file for possible portability problems.
  4. #
  5. # ENVIRONMENT
  6. # (See check-subr.awk)
  7. #
  8. BEGIN {
  9. found_random = no;
  10. found_test_eqeq = no;
  11. }
  12. # Check for $RANDOM, which is specific to ksh and bash.
  13. function check_random(line) {
  14. # $RANDOM together with the PID is often found in GNU-style
  15. # configure scripts and is considered acceptable.
  16. if (line ~ /\$\$-\$RANDOM/ || line ~ /\$RANDOM-\$\$/) {
  17. # Assume that this is ok.
  18. } else if (line ~ /\$RANDOM[A-Z_]+/) {
  19. # That's ok, too.
  20. } else if (line ~ /\$RANDOM/) {
  21. found_random = yes;
  22. cs_warning_heading("Found $RANDOM:");
  23. cs_warning_msg(cs_fname ": " $0);
  24. }
  25. }
  26. function check_test_eqeq(line, n, word, i) {
  27. n = split(line, word);
  28. for (i = 3; i < n; i++) {
  29. if (word[i] == "==") {
  30. if (word[i-2] == "test" || word[i-2] == "[") {
  31. found_test_eqeq = yes;
  32. cs_error_heading("Found test ... == ...:");
  33. cs_error_msg(cs_fname ": " $0);
  34. }
  35. }
  36. }
  37. }
  38. /./ {
  39. # Note: This code does not find _all_ instances of
  40. # unportable code. If a single line contains an unsafe and
  41. # a safe usage of $RANDOM, it will pass the test.
  42. # Strip comments
  43. line = $0;
  44. gsub(/^#.*/, "", line);
  45. gsub(/[[:space:]]#.*/, "", line);
  46. check_random(line);
  47. check_test_eqeq(line);
  48. }
  49. END {
  50. if (found_random) {
  51. h = "The variable $RANDOM is not required for a POSIX-conforming shell, and\n";
  52. h = h "many implementations of /bin/sh do not support it. It should therefore\n";
  53. h = h "not be used in shell programs that are meant to be portable across a\n";
  54. h = h "large number of POSIX-like systems.\n"
  55. cs_explain(h);
  56. }
  57. if (found_test_eqeq) {
  58. h = "The \"test\" command, as well as the \"[\" command, are not required to know\n";
  59. h = h "the \"==\" operator. Only a few implementations like bash and some\n";
  60. h = h "versions of ksh support it.\n";
  61. h = h "\n";
  62. h = h "When you run \"test foo == foo\" on a platform that does not support the\n";
  63. h = h "\"==\" operator, the result will be \"false\" instead of \"true\". This can\n";
  64. h = h "lead to unexpected behavior.\n";
  65. h = h "\n";
  66. h = h "There are two ways to fix this error message. If the file that contains\n";
  67. h = h "the \"test ==\" is needed for building the package, you should create a\n";
  68. h = h "patch for it, replacing the \"==\" operator with \"=\". If the file is not\n";
  69. h = h "needed, add its name to the CHECK_PORTABILITY_SKIP variable in the\n";
  70. h = h "package Makefile.\n";
  71. cs_explain(h);
  72. }
  73. cs_exit();
  74. }