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