/tags/main-premerge/SWIG/Source/Modules/inputmodule.c

# · C · 186 lines · 148 code · 19 blank · 19 comment · 42 complexity · eb02dd18e7ffe36dd091ff756c531d6c MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. * inputmodule.c
  3. *
  4. * This module is responsible for reading input files and setting up all
  5. * of the proper search paths.
  6. *
  7. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8. *
  9. * Copyright (C) 1999-2000. The University of Chicago
  10. * See the file LICENSE for information on usage and redistribution.
  11. * ----------------------------------------------------------------------------- */
  12. #include "swig.h"
  13. #include "swigconfig.h"
  14. #ifndef SWIG_LIB
  15. #define SWIG_LIB "./swiglib"
  16. #endif
  17. static int checkout = 0;
  18. static List *includedirs;
  19. static List *libfiles;
  20. static String *outfile = 0;
  21. static int debug_path = 0;
  22. static int debug_input = 0;
  23. static const char *usage = "File options:\n\
  24. -I<dir> - Look for SWIG files in <dir>\n\
  25. -l<ifile> - Include SWIG library file.\n\
  26. -o outfile - Set name of the output file.\n\
  27. -co - Check file out of SWIG library.\n\
  28. ";
  29. static
  30. int input_init(int argc, char **argv) {
  31. char *c;
  32. int i;
  33. /* Directories included with the -I option */
  34. includedirs = NewList();
  35. /* Files added with the -l option */
  36. libfiles = NewList();
  37. /* Look for command line options */
  38. for (i = 1; i < argc; i++) {
  39. if (argv[i]) {
  40. if (strncmp(argv[i],"-I",2) == 0) {
  41. Append(includedirs,argv[i]+2);
  42. Swig_mark_arg(i);
  43. } else if (strcmp(argv[i],"-debug_path") == 0) {
  44. debug_path = 1;
  45. Swig_mark_arg(i);
  46. } else if (strcmp(argv[i],"-debug_input") == 0) {
  47. debug_input = 1;
  48. Swig_mark_arg(i);
  49. } else if (strncmp(argv[i],"-l",2) == 0) {
  50. Append(libfiles, argv[i]+2);
  51. Swig_mark_arg(i);
  52. } else if (strcmp(argv[i],"-co") == 0) {
  53. checkout = 1;
  54. Swig_mark_arg(i);
  55. } else if (strcmp(argv[i],"-o") == 0) {
  56. Swig_mark_arg(i);
  57. if (argv[i+1]) {
  58. outfile = NewString(argv[i+1]);
  59. Swig_mark_arg(i+1);
  60. i++;
  61. } else {
  62. Swig_arg_error();
  63. }
  64. } else if (strcmp(argv[i],"-help") == 0) {
  65. Printf(stderr,"%s",usage);
  66. Swig_mark_arg(i);
  67. }
  68. }
  69. }
  70. /* Set the location of the SWIG library */
  71. if (!(c = getenv("SWIG_LIB"))) {
  72. Append(includedirs,SWIG_LIB);
  73. } else {
  74. Append(includedirs,c);
  75. }
  76. return 0;
  77. }
  78. static
  79. DOH *input_run(DOH *node) {
  80. int i;
  81. String *infile;
  82. FILE *f;
  83. String *input;
  84. DOH *result;
  85. String *swiglib;
  86. String *lang_config;
  87. infile = Getname(node);
  88. /* Register all of the include directories */
  89. swiglib = Swig_swiglib_get();
  90. for (i = Len(includedirs); i > 0; i--) {
  91. if (swiglib) {
  92. String *l = NewStringf("%s%s%s", Getitem(includedirs,i-1),SWIG_FILE_DELIMETER,swiglib);
  93. Swig_add_directory(l);
  94. }
  95. Swig_add_directory(Getitem(includedirs,i-1));
  96. }
  97. if (debug_path) {
  98. List *l;
  99. Printf(stdout,"SWIG search path:\n");
  100. l = Swig_search_path();
  101. if (l) {
  102. String *s;
  103. for (s = Firstitem(l); s; s = Nextitem(l)) {
  104. Printf(stdout," %s\n", s);
  105. }
  106. }
  107. }
  108. /* user has requested to simply check out a file */
  109. if (checkout) {
  110. String *outfilename;
  111. String *s;
  112. outfilename = outfile ? outfile : infile;
  113. /* Grab the file */
  114. s = Swig_include(infile);
  115. if (!s) {
  116. Printf(stderr,"Unable to locate '%s' in the SWIG library.\n", infile);
  117. Swig_exit(EXIT_FAILURE);
  118. } else {
  119. File *f = NewFile(outfilename,"r");
  120. if (f) {
  121. Delete(f);
  122. Printf(stderr,"File '%s' already exists. Checkout aborted.\n", outfilename);
  123. } else {
  124. f = NewFile(outfilename,"w");
  125. if (!f) {
  126. Printf(stderr,"Unable to create file '%s'\n", outfilename);
  127. } else {
  128. Printf(stderr,"'%s' checked out from the SWIG library.\n", infile);
  129. Dump(s,f);
  130. Delete(f);
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. /* Try to find input files */
  137. f = Swig_open(infile);
  138. if (!f) {
  139. Printf(stderr,"Unable to find '%s'\n", infile);
  140. Swig_exit (EXIT_FAILURE);
  141. }
  142. fclose(f);
  143. input = NewString("%include \"swig.swg\"\n");
  144. lang_config = Swig_get_config_file();
  145. if (lang_config) {
  146. Printf(input,"\n%%include \"%s\"\n", lang_config);
  147. }
  148. Printf(input,"\n%%include \"%s\"\n", infile);
  149. for (i = 0; i < Len(libfiles); i++) {
  150. Printf(input,"\n%%include \"%s\"\n", Getitem(libfiles,i));
  151. }
  152. result = NewHash();
  153. Settag(result,"swig:input");
  154. Setattr(result,"name", infile);
  155. Setattr(result,"path", Getfile(input));
  156. Setattr(result,"outfile", outfile);
  157. Setattr(result,"data",input);
  158. Setattr(result,"last",node);
  159. if (debug_input) {
  160. Printf(stdout,"::: inputmodule :::\n");
  161. Printf(stdout,"%s\n", input);
  162. }
  163. return result;
  164. }
  165. void inputmodule() {
  166. Swig_register_module("input","swig:initial", input_init, input_run);
  167. }