Added package with the documentation and the examples
[lwc.git] / preproc.c
blobe744a4b6f00fe34a71948dad9e76d3cd0239539d
1 /*
3 */
4 #include "global.h"
5 #include "SYS.h"
7 static void RUN (char *outfile, char **argv)
9 #if 0
10 int i;
11 fprintf (stderr, "Running: ");
12 for (i = 0; argv [i]; i++)
13 fprintf (stderr, "%s ", argv [i]);
14 fprintf (stderr, "\n");
15 #endif
17 int pid = fork ();
18 if (pid == 0) {
19 if (outfile) if(!freopen (outfile, "w", stdout))
20 exit (127);
21 execvp (argv [0], argv);
22 exit (127);
24 int status;
25 waitpid (pid, &status, 0);
26 if (WEXITSTATUS (status) != 0) {
27 fprintf (stderr, "Preprocessor failed..\n");
28 exit (1);
32 static bool issource (char *f)
34 char *e = f + strlen (f) - 1;
35 if (*(e-1) == '.')
36 return *e == 'c' || *e == 'h' || *e == 'C' || *e == 'i';
37 if (*(e-2) == '.')
38 return *(e-1) == 'c' && *e == '+';
39 return *(e-3) == '.' && (*(e-2) == 'c' || *(e-2) == 'C');
42 const static char banner [] =
43 "Lightweight C++ preprocessor Version "LWC_VERSION" [compiled "__DATE__", adapted to: "COMPILER"]\n"
44 "Developed by Stelios Xanthakis and Carl Rosmann\n"
45 "Internet: http://students.ceid.upatras.gr/~sxanth/lwc/\n"
46 "This program is *Freeware*. Save the trees\n\n";
48 char *main_file;
49 bool sys_cpp =
50 #ifdef DO_CPP
52 #else
54 #endif
57 void preproc (int argc, char **argv)
59 char **cppopt = (char**) alloca ((argc + 5) * sizeof (char*));
60 int ncppopt, i;
62 #if 0
63 /* The -gcc option seems to have been obsoleted and cpp reports
64 that it doesn't recognize it. Still, it makes a difference */
65 cppopt [0] = "cpp";
66 cppopt [1] = "-D__LWC__";
67 #ifdef __GNUC__
68 cppopt [2] = "-x";
69 cppopt [3] = "c";
70 cppopt [4] = "-C";
71 cppopt [5] = "-gcc";
72 ncppopt = 6;
73 #else
74 ncppopt = 2;
75 #endif
76 #else
77 cppopt [0] = "gcc";
78 cppopt [1] = "-D__LWC__";
79 cppopt [2] = "-E";
80 cppopt [3] = "-C";
81 cppopt [4] = "-x";
82 cppopt [5] = "c";
83 ncppopt = 6;
84 #endif
87 for (i = 0; i < argc; i++)
88 if (issource (cppopt [ncppopt++] = argv [i]))
89 if (current_file) fatal ("multiple files?");
90 else current_file = strdup (argv [i]);
91 else if (!strcmp (argv [i], "-D__REENTRANT"))
92 Reentrant = true;
93 else if (!strcmp (argv [i], "-sys_cpp"))
94 sys_cpp = true;
95 cppopt [ncppopt] = NULL;
97 if (!current_file) {
98 fputs (banner, stderr);
99 exit (1);
102 if (sys_cpp) {
103 main_file = PREPROCFILE;
104 RUN (PREPROCFILE, cppopt);
105 } else {
106 #ifdef DO_CPP
107 main_file = strdup (current_file);
108 setup_cpp (argc, argv);
109 #endif