mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.0 KiB

  1. /*
  2. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  3. *
  4. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  5. * Please add additional copyright information _after_ the line containing
  6. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  7. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  8. *
  9. * ROCK Linux: rock-src/misc/tools-source/descparser.c
  10. * ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version. A copy of the GNU General Public
  16. * License can be found at Documentation/COPYING.
  17. *
  18. * Many people helped and are helping developing ROCK Linux. Please
  19. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  20. * file for details.
  21. *
  22. * --- ROCK-COPYRIGHT-NOTE-END ---
  23. */
  24. /* this is a 1st proof-of-concept implementation */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <sys/types.h>
  29. #include <regex.h>
  30. int check_condition(const char *cond)
  31. {
  32. char *t = strdup(cond);
  33. char *left = strtok(t, " \t\n");
  34. char *op = strtok(0, " \t\n");
  35. char *right = strtok(0, " \t\n");
  36. int retcode = 0;
  37. if ( !strcmp(left, "xpkg") && getenv("descparser_ign_xpkg") ) {
  38. free(t);
  39. return 1;
  40. }
  41. if ( !strcmp(op, "==") ) {
  42. char regex[strlen(right)+3];
  43. char *text = getenv(left);
  44. regex_t re;
  45. sprintf(regex, "^%s$", right);
  46. if ( !text ) text = "_undef_";
  47. if ( regcomp(&re, regex, REG_EXTENDED|REG_NOSUB) ) {
  48. fprintf(stderr, "failed to compile regex: '%s'.\n", right);
  49. exit(1);
  50. }
  51. if ( !regexec(&re, text, 0, 0, 0) ) retcode = 1;
  52. regfree(&re);
  53. } else {
  54. fprintf(stderr, "Unknown operator: '%s'.\n", op);
  55. exit(1);
  56. }
  57. free(t);
  58. return retcode;
  59. }
  60. char line[4096];
  61. int condstack[128];
  62. int condcount = -1;
  63. int falselevel = 0;
  64. void parse_file(FILE *f);
  65. void parse_file(FILE *f)
  66. {
  67. while ( fgets(line, 4096, f) ) {
  68. if (line[0] == '#') {
  69. if ( !strncmp(line, "#if ", 4) ) {
  70. condstack[++condcount] = check_condition(line+4);
  71. if ( !condstack[condcount] ) falselevel++;
  72. } else
  73. if ( !strncmp(line, "#else", 5) ) {
  74. falselevel += condstack[condcount] ? +1 : -1;
  75. condstack[condcount] = !condstack[condcount];
  76. } else
  77. if ( !strncmp(line, "#elsif ", 7) ) {
  78. if ( !condstack[condcount] ) {
  79. condstack[condcount] = check_condition(line+7);
  80. if ( condstack[condcount] ) falselevel--;
  81. } else
  82. falselevel++;
  83. } else
  84. if ( !strncmp(line, "#endif", 6) ) {
  85. if ( !condstack[condcount--] ) falselevel--;
  86. } else
  87. if ( !strncmp(line, "#include ", 9) ) {
  88. FILE *i;
  89. if (strchr(line, '\n')) *strchr(line, '\n') = 0;
  90. i = fopen(line+9, "r");
  91. if (i) {
  92. parse_file(i);
  93. fclose(i);
  94. } else
  95. fprintf(stderr, "Can't #include '%s'.\n", line+9);
  96. }
  97. } else
  98. if ( !falselevel )
  99. fputs(line, stdout);
  100. }
  101. }
  102. int main()
  103. {
  104. parse_file(stdin);
  105. return 0;
  106. }