Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cpp.ansi / options.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 /* $Id: options.c,v 1.10 1994/06/24 11:37:49 ceriel Exp $ */
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 'A' :      /* for Amake */
43         case 'd' :      /* dependency generation */
44                 do_dependencies = 1;
45                 if (*text) {
46                         dep_file = text;
47                 }
48                 else {
49                         do_preprocess = 0;
50                 }
51                 break;
52         case 'i':
53         case 'm':
54         case 'o':       /* ignore garbage after #else or #endif */
55         case 'C' :      /* comment output               */
56                 options[*(text-1)] = 1;
57                 break;
58         case 'D' :      /* -Dname :     predefine name          */
59         {
60                 register char *cp = text, *name, *mactext;
61                 unsigned maclen;
62
63                 if (class(*cp) != STIDF && class(*cp) != STELL) {
64                         error("identifier missing in -D%s", text);
65                         break;
66                 }
67                 name = cp;
68                 while (*cp && in_idf(*cp))
69                         ++cp;
70                 if (!*cp) {                     /* -Dname */
71                         maclen = 1;
72                         mactext = Salloc("1", 2);
73                 } else
74                 if (*cp == '=') {               /* -Dname=text  */
75                         *cp++ = '\0';           /* end of name  */
76                         maclen = strlen(cp);
77                         mactext = Salloc(cp, maclen + 1);
78                 }
79                 else    {                       /* -Dname?? */
80                         error("malformed option -D%s", text);
81                         break;
82                 }
83                 macro_def(str2idf(name, 0), mactext, -1, (int)maclen, 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((char *)inctable,
94                                           (unsigned)((inc_max+=10)*sizeof(char *)));
95                         }
96
97                         for(i = inc_pos++; i < inc_total; i++) {
98                                 char *tmp = inctable[i];
99
100                                 inctable[i] = new;
101                                 new = tmp;
102                         }
103                 }
104                 else    inctable[inc_pos] = 0;
105                 break;
106         case 'M':       /* maximum identifier length */
107                 idfsize = txt2int(&text);
108                 if (*text)
109                         error("malformed -M option");
110                 if (idfsize > IDFSIZE) {
111                         warning("maximum identifier length is %d", IDFSIZE);
112                         idfsize = IDFSIZE;
113                 }
114                 if (idfsize < 8) {
115                         warning("minimum identifier length is 8");
116                         idfsize = 8;
117                 }
118                 break;
119         case 'P' :      /* run preprocessor stand-alone, without #'s    */
120                 options['P'] = 1;
121                 break;
122         case 'U' :      /* -Uname :     undefine predefined     */
123                 if (*text) do_undef(text);
124                 break;
125         }
126 }
127
128 int
129 txt2int(tp)
130         char **tp;
131 {
132         /*      the integer pointed to by *tp is read, while increasing
133                 *tp; the resulting value is yielded.
134         */
135         register int val = 0;
136         register int ch;
137         
138         while (ch = **tp, ch >= '0' && ch <= '9')       {
139                 val = val * 10 + ch - '0';
140                 (*tp)++;
141         }
142         return val;
143 }