1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 #include <stdio.h> 23 #include <stddef.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <ctype.h> 27 #include <fcntl.h> 28 #if (defined(_WIN32) || defined(_MSDOS) || defined(__IBMC__)) 29 #include <io.h> 30 #else 31 #include <unistd.h> 32 #endif 33 34 #include "cpp.h" 35 36 #if defined MACOSX || !defined HAVE_GETOPT 37 extern int stgetopt(int, char *const *, const char *); 38 extern char *optarg; 39 extern int optind; 40 #else 41 #include <getopt.h> 42 #endif 43 44 extern char rcsid[]; 45 46 int Pflag = 0; /* print no line information */ 47 int Iflag = 0; /* print includes */ 48 int Mflag = 0; /* print macor expansion */ 49 int Aflag = 0; /* translate character sets */ 50 int Xflag = 0; /* print pragma for include/import */ 51 int Vflag = 0; /* verbose flag */ 52 int Cflag = 0; /* do not remove any comments */ 53 int Dflag = 0; /* add parameter check to delete op */ 54 int Cplusplus = 0; 55 56 extern void setup_kwtab(void); 57 58 void 59 setup(int argc, char **argv) 60 { 61 int c, fd, i, n; 62 char *fp, *dp; 63 Tokenrow tr; 64 65 setup_kwtab(); 66 #if defined MACOSX || !defined HAVE_GETOPT 67 while ((c = stgetopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1) 68 #else 69 while ((c = getopt(argc, argv, "NOPV:I:D:U:F:A:X:u:l:+")) != -1) 70 #endif 71 switch (c) 72 { 73 case 'N': 74 for (i = 0; i < NINCLUDE; i++) 75 if (includelist[i].always == 1) 76 includelist[i].deleted = 1; 77 break; 78 79 case 'I': 80 for (i = NINCLUDE - 2; i >= 0; i--) 81 { 82 if (includelist[i].file == NULL) 83 { 84 includelist[i].always = 1; 85 includelist[i].file = optarg; 86 break; 87 } 88 } 89 if (i < 0) 90 error(FATAL, "Too many -I directives"); 91 break; 92 93 case 'D': 94 case 'U': 95 case 'A': 96 setsource("<cmdarg>", -1, -1, optarg, 0); 97 maketokenrow(3, &tr); 98 gettokens(&tr, 1); 99 doadefine(&tr, c); 100 unsetsource(); 101 break; 102 103 case 'P': /* Lineinfo */ 104 Pflag++; 105 break; 106 107 case 'V': 108 for (n = 0; (c = optarg[n]) != '\0'; n++) 109 switch (c) 110 { 111 case 'i': 112 Iflag++; 113 break; 114 115 case 'm': 116 Mflag = 1; 117 break; 118 119 case 'x': 120 Mflag = 2; 121 break; 122 123 case 't': 124 Vflag++; 125 break; 126 127 case 'v': 128 fprintf(stderr, "%s %s\n", argv[0], rcsid); 129 break; 130 131 default: 132 error(WARNING, "Unknown verbose option %c", c); 133 } 134 break; 135 136 case 'X': 137 for (n = 0; (c = optarg[n]) != '\0'; n++) 138 switch (c) 139 { 140 case 'a': 141 Aflag++; 142 break; 143 144 case 'i': 145 Xflag++; 146 break; 147 148 case 'c': 149 Cflag++; 150 break; 151 152 case 'd': 153 Dflag++; 154 break; 155 156 case 'w': 157 dp = &optarg[n + 1]; 158 n += strlen(dp); 159 while (isspace(*dp)) dp++; 160 161 for (i = NINCLUDE - 1; i >= 0; i--) 162 { 163 if (wraplist[i].file == NULL) 164 { 165 wraplist[i].file = dp; 166 break; 167 } 168 } 169 if (i < 0) 170 error(WARNING, "Too many -Xw directives"); 171 break; 172 173 default: 174 error(WARNING, "Unknown extension option %c", c); 175 } 176 break; 177 178 case '+': 179 Cplusplus++; 180 break; 181 182 case 'u': /* -undef fuer GCC (dummy) */ 183 case 'l': /* -lang-c++ fuer GCC (dummy) */ 184 break; 185 186 default: 187 break; 188 } 189 dp = "."; 190 fp = "<stdin>"; 191 fd = 0; 192 if (optind < argc) 193 { 194 if ((fp = strrchr(argv[optind], '/')) != NULL) 195 { 196 int len = fp - argv[optind]; 197 198 dp = (char *) newstring((uchar *) argv[optind], len + 1, 0); 199 dp[len] = '\0'; 200 } 201 fp = (char *) newstring((uchar *) argv[optind], strlen(argv[optind]), 0); 202 if ((fd = open(fp, O_RDONLY)) <= 0) 203 error(FATAL, "Can't open input file %s", fp); 204 } 205 206 if (optind + 1 < argc) 207 { 208 int fdo = creat(argv[optind + 1], 0666); 209 210 if (fdo < 0) 211 error(FATAL, "Can't open output file %s", argv[optind + 1]); 212 213 dup2(fdo, 1); 214 } 215 includelist[NINCLUDE - 1].always = 0; 216 includelist[NINCLUDE - 1].file = dp; 217 setsource(fp, -1, fd, NULL, 0); 218 } 219 220 221 /* memmove is defined here because some vendors don't provide it at 222 all and others do a terrible job (like calling malloc) */ 223 224 #if !defined(__IBMC__) && !defined(_WIN32) && !defined(__GLIBC__) 225 226 void * 227 memmove(void *dp, const void *sp, size_t n) 228 { 229 unsigned char *cdp, *csp; 230 231 if (n <= 0) 232 return 0; 233 cdp = dp; 234 csp = (unsigned char *) sp; 235 if (cdp < csp) 236 { 237 do 238 { 239 *cdp++ = *csp++; 240 } while (--n); 241 } 242 else 243 { 244 cdp += n; 245 csp += n; 246 do 247 { 248 *--cdp = *--csp; 249 } while (--n); 250 } 251 return 0; 252 } 253 254 #endif 255 256