summaryrefslogtreecommitdiff
path: root/src/preproc/pmain.c
blob: 9cc721ae99b9a5bdc862705ce3c5b0183afcb271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "../preproc/preproc.h"
#include "../preproc/pproto.h"

char *progname = "pp";

/*
 * Establish command-line options.
 */
static char *ostr = "+CPD:I:U:o:";
static char *options =
   "[-C] [-P] [-Dname[=[text]]] [-Uname] [-Ipath] [-ofile] [files]";

extern line_cntrl;

/*
 * getopt() variables
 */
extern int optind;		/* index into parent argv vector */
extern int optopt;		/* character checked for validity */
extern char *optarg;		/* argument associated with option */

int main(argc, argv)
int argc;
char **argv;
   {
   int c;
   char *opt_lst;
   char **opt_args;
   int nopts;
   FILE *out_file;

   /*
    * By default, keep the image of white space, but replace each comment
    *  by a space. By default, output #line directives.
    */
   whsp_image = NoComment;
   line_cntrl = 1;

   /*
    * The number of options that must be passed on to other phases
    *  of the preprocessor are at most as large as the entire option
    *  list.
    */
   opt_lst = alloc(argc);
   opt_args = alloc(argc * sizeof (char *));
   nopts = 0;
   out_file = stdout;

   /*
    * Process options.
    */
   while ((c = getopt(argc, argv, ostr)) != EOF)
      switch (c) {

         case 'C':  /* -C - retan comments */
            whsp_image = FullImage;
            break;

         case 'P': /* -P - do not output #line directives */
            line_cntrl = 0;
            break;

         case 'D': /* -D<id><definition> - predefine an identifier */
         case 'I': /* -I<path> - location to search for standard header files */
         case 'U': /* -U<id> - undefine predefined identifier */
            opt_lst[nopts] = c;
            opt_args[nopts] = optarg;
            ++nopts;
            break;

         case 'o': /* -o<file> - write output to this file */
            out_file = fopen(optarg, "w");
            if (out_file == NULL)
               err2("cannot open output file ", optarg);
            break;

         default:
            show_usage();
         }

   opt_lst[nopts] = '\0';

   /*
    * Scan file name arguments. If there are none, process standard input,
    *  indicated by the name "-".
    */
   if (optind == argc) {
      init_preproc("-", opt_lst, opt_args);
      output(out_file);
      }
   else {
      while (optind < argc)  {
         init_preproc(argv[optind], opt_lst, opt_args);
         output(out_file);
         optind++;
         }
      }

   return EXIT_SUCCESS;
   }

/*
 * Print an error message if called incorrectly.
 */
void show_usage()
   {
   fprintf(stderr, "usage: %s %s\n", progname, options);
   exit(EXIT_FAILURE);
   }