Pristine Ack-5.5
[Ack-5.5.git] / util / cpp / options.c
1 /* $Id: options.c,v 1.12 1994/06/24 10:18:47 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /* USER-OPTION HANDLING */
7
8 #include        <alloc.h>
9 #include        "idfsize.h"
10 #include        "class.h"
11 #include        "macro.h"
12 #include        "idf.h"
13
14 char options[128];                      /* one for every char   */
15 int inc_pos = 1;                        /* place where next -I goes */
16 int inc_max;
17 int inc_total;
18 int do_preprocess = 1;
19 int do_dependencies = 0;
20 char **inctable;
21 char *dep_file = 0;
22
23 extern int idfsize;
24 int txt2int();
25
26 do_option(text)
27         char *text;
28 {
29         switch(*text++) {
30         case '-':
31                 options[*text] = 1;
32                 break;
33         case 'u':
34                 if (! strcmp(text,"ndef")) {
35                         /* ignore -undef */
36                         break;
37                 }
38                 /* fall through */
39         default:
40                 error("illegal option: %c", text[-1]);
41                 break;
42         case 'C' :      /* comment output               */
43         case 'P' :      /* run preprocessor stand-alone, without #'s    */
44                 options[*(text-1)] = 1;
45                 break;
46         case 'A' :      /* for Amake */
47         case 'd' :      /* dependency generation */
48                 do_dependencies = 1;
49                 if (*text) {
50                         dep_file = text;
51                 }
52                 else {
53                         do_preprocess = 0;
54                 }
55                 break;
56         case 'm':
57         case 'i':
58                 options[*(text-1)] = 1;
59                 break;
60
61         case 'D' :      /* -Dname :     predefine name          */
62         {
63                 register char *cp = text, *name, *mactext;
64
65                 if (class(*cp) != STIDF)        {
66                         error("identifier missing in -D%s", text);
67                         break;
68                 }
69                 name = cp;
70                 while (*cp && in_idf(*cp))
71                         ++cp;
72                 if (!*cp)                       /* -Dname */
73                         mactext = "1";
74                 else
75                 if (*cp == '=') {               /* -Dname=text  */
76                         *cp++ = '\0';           /* end of name  */
77                         mactext = cp;
78                 }
79                 else    {                       /* -Dname?? */
80                         error("malformed option -D%s", text);
81                         break;
82                 }
83                 macro_def(str2idf(name, 0), mactext, -1, strlen(mactext), NOFLAG);
84                 break;
85         }
86         case 'I' :      /* -Ipath : insert "path" into include list     */
87                 if (*text)      {
88                         register int i;
89                         register char *new = text;
90
91                         if (++inc_total > inc_max) {
92                                 inctable = (char **)
93                                   Realloc(inctable,(inc_max+=10)*sizeof(char *));
94                         }
95
96                         for(i = inc_pos++; i < inc_total; i++) {
97                                 char *tmp = inctable[i];
98
99                                 inctable[i] = new;
100                                 new = tmp;
101                         }
102                 }
103                 else    inctable[inc_pos] = 0;
104                 break;
105         case 'M':       /* maximum identifier length */
106                 idfsize = txt2int(&text);
107                 if (*text)
108                         error("malformed -M option");
109                 if (idfsize > IDFSIZE) {
110                         warning("maximum identifier length is %d", IDFSIZE);
111                         idfsize = IDFSIZE;
112                 }
113                 if (idfsize < 8) {
114                         warning("minimum identifier length is 8");
115                         idfsize = 8;
116                 }
117                 break;
118         case 'U' :      /* -Uname :     undefine predefined     */
119                 if (*text)      {
120                         register struct idf *idef = findidf(text);
121
122                         if (idef && idef->id_macro) {
123                                 free_macro(idef->id_macro);
124                                 idef->id_macro = (struct macro *) 0;
125                         }
126                 }
127                 break;
128         }
129 }
130
131 int
132 txt2int(tp)
133         char **tp;
134 {
135         /*      the integer pointed to by *tp is read, while increasing
136                 *tp; the resulting value is yielded.
137         */
138         register int val = 0;
139         register int ch;
140         
141         while (ch = **tp, ch >= '0' && ch <= '9')       {
142                 val = val * 10 + ch - '0';
143                 (*tp)++;
144         }
145         return val;
146 }