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.

97 lines
2.6 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 - 2004 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(op, "==") ) {
  38. char regex[strlen(right)+3];
  39. char *text = getenv(left);
  40. regex_t re;
  41. sprintf(regex, "^%s$", right);
  42. if ( !text ) text = "_undef_";
  43. if ( regcomp(&re, regex, REG_EXTENDED|REG_NOSUB) ) {
  44. fprintf(stderr, "failed to compile regex: '%s'.\n", right);
  45. exit(1);
  46. }
  47. if ( !regexec(&re, text, 0, 0, 0) ) retcode = 1;
  48. regfree(&re);
  49. } else {
  50. fprintf(stderr, "Unknown operator: '%s'.\n", op);
  51. exit(1);
  52. }
  53. free(t);
  54. return retcode;
  55. }
  56. int main()
  57. {
  58. char line[4096];
  59. int condstack[128];
  60. int condcount = -1;
  61. int falselevel = 0;
  62. while ( fgets(line, 4096, stdin) ) {
  63. if (line[0] == '#') {
  64. if ( !strncmp(line, "#if ", 4) ) {
  65. condstack[++condcount] = check_condition(line+4);
  66. if ( !condstack[condcount] ) falselevel++;
  67. } else
  68. if ( !strncmp(line, "#else", 5) ) {
  69. falselevel += condstack[condcount] ? +1 : -1;
  70. condstack[condcount] = !condstack[condcount];
  71. } else
  72. if ( !strncmp(line, "#elsif ", 7) ) {
  73. if ( !condstack[condcount] ) {
  74. condstack[condcount] = check_condition(line+7);
  75. if ( condstack[condcount] ) falselevel--;
  76. } else
  77. falselevel++;
  78. } else
  79. if ( !strncmp(line, "#endif", 6) ) {
  80. if ( !condstack[condcount--] ) falselevel--;
  81. }
  82. } else
  83. if ( !falselevel )
  84. fputs(line, stdout);
  85. }
  86. return 0;
  87. }