unix.c (2332B)
1 #include <stdio.h> 2 #include <stddef.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include "cpp.h" 6 7 extern int getopt(int, char *const *, const char *); 8 extern char *optarg, rcsid[]; 9 extern int optind; 10 int verbose; 11 int Mflag; /* only print active include files */ 12 char *objname; /* "src.$O: " */ 13 int Cplusplus = 1; 14 15 void 16 setup(int argc, char **argv) 17 { 18 int c, fd, i; 19 char *fp, *dp; 20 Tokenrow tr; 21 extern void setup_kwtab(void); 22 23 setup_kwtab(); 24 while ((c = getopt(argc, argv, "MNOVv+I:D:U:F:lg")) != -1) 25 switch (c) { 26 case 'N': 27 for (i=0; i<NINCLUDE; i++) 28 if (includelist[i].always==1) 29 includelist[i].deleted = 1; 30 break; 31 case 'I': 32 for (i=NINCLUDE-2; i>=0; i--) { 33 if (includelist[i].file==NULL) { 34 includelist[i].always = 1; 35 includelist[i].file = optarg; 36 break; 37 } 38 } 39 if (i<0) 40 error(FATAL, "Too many -I directives"); 41 break; 42 case 'D': 43 case 'U': 44 setsource("<cmdarg>", -1, optarg); 45 maketokenrow(3, &tr); 46 gettokens(&tr, 1); 47 doadefine(&tr, c); 48 unsetsource(); 49 break; 50 case 'M': 51 Mflag++; 52 break; 53 case 'v': 54 fprintf(stderr, "%s %s\n", argv[0], rcsid); 55 break; 56 case 'V': 57 verbose++; 58 break; 59 case '+': 60 Cplusplus++; 61 break; 62 default: 63 break; 64 } 65 dp = "."; 66 fp = "<stdin>"; 67 fd = 0; 68 if (optind<argc) { 69 if ((fp = strrchr(argv[optind], '/')) != NULL) { 70 int len = fp - argv[optind]; 71 dp = (char*)newstring((uchar*)argv[optind], len+1, 0); 72 dp[len] = '\0'; 73 } 74 fp = (char*)newstring((uchar*)argv[optind], strlen(argv[optind]), 0); 75 if ((fd = open(fp, 0)) <= 0) 76 error(FATAL, "Can't open input file %s", fp); 77 } 78 if (optind+1<argc) { 79 int fdo = creat(argv[optind+1], 0666); 80 if (fdo<0) 81 error(FATAL, "Can't open output file %s", argv[optind+1]); 82 dup2(fdo, 1); 83 } 84 if(Mflag) 85 setobjname(fp); 86 includelist[NINCLUDE-1].always = 0; 87 includelist[NINCLUDE-1].file = dp; 88 setsource(fp, fd, NULL); 89 } 90 91 92 93 /* memmove is defined here because some vendors don't provide it at 94 all and others do a terrible job (like calling malloc) */ 95 void * 96 memmove(void *dp, const void *sp, size_t n) 97 { 98 unsigned char *cdp, *csp; 99 100 if (n<=0) 101 return 0; 102 cdp = dp; 103 csp = (unsigned char *)sp; 104 if (cdp < csp) { 105 do { 106 *cdp++ = *csp++; 107 } while (--n); 108 } else { 109 cdp += n; 110 csp += n; 111 do { 112 *--cdp = *--csp; 113 } while (--n); 114 } 115 return 0; 116 }