unix.c 2.3 KB

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