| @@ -635,25 +635,15 @@ PUB_FUNC void warning(const char *fmt, ...) · 634 unmodified lines |
| 635 | 635 | /********************************************************/ |
| 636 | 636 | /* I/O layer */ |
| 637 | 637 | |
| 638 | | −ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename) |
| 638 | +ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen) |
| 639 | 639 | { |
| 640 | | − int fd; |
| 641 | 640 | BufferedFile *bf; |
| 641 | + int buflen = initlen ? initlen : IO_BUF_SIZE; |
| 642 | 642 | |
| 643 | | − if (strcmp(filename, "-") == 0) |
| 644 | | − fd = 0, filename = "stdin"; |
| 645 | | − else |
| 646 | | − fd = open(filename, O_RDONLY | O_BINARY); |
| 647 | | − if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3) |
| 648 | | − printf("%s %*s%s\n", fd < 0 ? "nf":"->", |
| 649 | | − (int)(s1->include_stack_ptr - s1->include_stack), "", filename); |
| 650 | | − if (fd < 0) |
| 651 | | − return NULL; |
| 652 | | − bf = tcc_malloc(sizeof(BufferedFile)); |
| 653 | | − bf->fd = fd; |
| 643 | + bf = tcc_malloc(sizeof(BufferedFile) + buflen); |
| 654 | 644 | bf->buf_ptr = bf->buffer; |
| 655 | | − bf->buf_end = bf->buffer; |
| 656 | | − bf->buffer[0] = CH_EOB; /* put eob symbol */ |
| 645 | + bf->buf_end = bf->buffer + initlen; |
| 646 | + bf->buf_end[0] = CH_EOB; /* put eob symbol */ |
| 657 | 647 | pstrcpy(bf->filename, sizeof(bf->filename), filename); |
| 658 | 648 | #ifdef _WIN32 |
| 659 | 649 | normalize_slashes(bf->filename); |
| @@ -661,17 +651,40 @@ ST_FUNC BufferedFile *tcc_open(TCCState *s1, const char *filename) · 1 unmodified line |
| 661 | 651 | bf->line_num = 1; |
| 662 | 652 | bf->ifndef_macro = 0; |
| 663 | 653 | bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; |
| 664 | | − // printf("opening '%s'\n", filename); |
| 665 | | − return bf; |
| 654 | + bf->fd = -1; |
| 655 | + bf->prev = file; |
| 656 | + file = bf; |
| 666 | 657 | } |
| 667 | 658 | |
| 668 | | −ST_FUNC void tcc_close(BufferedFile *bf) |
| 659 | +ST_FUNC void tcc_close(void) |
| 669 | 660 | { |
| 670 | | − total_lines += bf->line_num; |
| 671 | | − close(bf->fd); |
| 661 | + BufferedFile *bf = file; |
| 662 | + if (bf->fd > 0) { |
| 663 | + close(bf->fd); |
| 664 | + total_lines += bf->line_num; |
| 665 | + } |
| 666 | + file = bf->prev; |
| 672 | 667 | tcc_free(bf); |
| 673 | 668 | } |
| 674 | 669 | |
| 670 | +ST_FUNC int tcc_open(TCCState *s1, const char *filename) |
| 671 | +{ |
| 672 | + int fd; |
| 673 | + if (strcmp(filename, "-") == 0) |
| 674 | + fd = 0, filename = "stdin"; |
| 675 | + else |
| 676 | + fd = open(filename, O_RDONLY | O_BINARY); |
| 677 | + if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3) |
| 678 | + printf("%s %*s%s\n", fd < 0 ? "nf":"->", |
| 679 | + (int)(s1->include_stack_ptr - s1->include_stack), "", filename); |
| 680 | + if (fd < 0) |
| 681 | + return -1; |
| 682 | + |
| 683 | + tcc_open_bf(s1, filename, 0); |
| 684 | + file->fd = fd; |
| 685 | + return fd; |
| 686 | +} |
| 687 | + |
| 675 | 688 | /* compile the C file opened in 'file'. Return non zero if errors. */ |
| 676 | 689 | static int tcc_compile(TCCState *s1) |
| 677 | 690 | { |
| @@ -780,60 +793,38 @@ static int tcc_compile(TCCState *s1) · 102 unmodified lines |
| 780 | 793 | |
| 781 | 794 | LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str) |
| 782 | 795 | { |
| 783 | | − BufferedFile bf1, *bf = &bf1; |
| 784 | | − int ret, len; |
| 785 | | − char *buf; |
| 786 | | − |
| 787 | | − /* init file structure */ |
| 788 | | − bf->fd = -1; |
| 789 | | − /* XXX: avoid copying */ |
| 796 | + int len, ret; |
| 790 | 797 | len = strlen(str); |
| 791 | | − buf = tcc_malloc(len + 1); |
| 792 | | − if (!buf) |
| 793 | | − return -1; |
| 794 | | − memcpy(buf, str, len); |
| 795 | | − buf[len] = CH_EOB; |
| 796 | | − bf->buf_ptr = buf; |
| 797 | | − bf->buf_end = buf + len; |
| 798 | | − pstrcpy(bf->filename, sizeof(bf->filename), "<string>"); |
| 799 | | − bf->line_num = 1; |
| 800 | | − file = bf; |
| 801 | | − ret = tcc_compile(s); |
| 802 | | − file = NULL; |
| 803 | | − tcc_free(buf); |
| 804 | 798 | |
| 805 | | − /* currently, no need to close */ |
| 799 | + tcc_open_bf(s, "<string>", len); |
| 800 | + memcpy(file->buffer, str, len); |
| 801 | + ret = tcc_compile(s); |
| 802 | + tcc_close(); |
| 806 | 803 | return ret; |
| 807 | 804 | } |
| 808 | 805 | |
| 809 | 806 | /* define a preprocessor symbol. A value can also be provided with the '=' operator */ |
| 810 | 807 | LIBTCCAPI void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) |
| 811 | 808 | { |
| 812 | | − BufferedFile bf1, *bf = &bf1; |
| 813 | | − |
| 814 | | − pstrcpy(bf->buffer, IO_BUF_SIZE, sym); |
| 815 | | − pstrcat(bf->buffer, IO_BUF_SIZE, " "); |
| 809 | + int len1, len2; |
| 816 | 810 | /* default value */ |
| 817 | | − if (!value) |
| 811 | + if (!value) |
| 818 | 812 | value = "1"; |
| 819 | | − pstrcat(bf->buffer, IO_BUF_SIZE, value); |
| 820 | | − |
| 813 | + len1 = strlen(sym); |
| 814 | + len2 = strlen(value); |
| 815 | + |
| 821 | 816 | /* init file structure */ |
| 822 | | − bf->fd = -1; |
| 823 | | − bf->buf_ptr = bf->buffer; |
| 824 | | − bf->buf_end = bf->buffer + strlen(bf->buffer); |
| 825 | | − *bf->buf_end = CH_EOB; |
| 826 | | − bf->filename[0] = '\0'; |
| 827 | | − bf->line_num = 1; |
| 828 | | − file = bf; |
| 829 | | − |
| 830 | | − s1->include_stack_ptr = s1->include_stack; |
| 817 | + tcc_open_bf(s1, "<define>", len1 + len2 + 1); |
| 818 | + memcpy(file->buffer, sym, len1); |
| 819 | + file->buffer[len1] = ' '; |
| 820 | + memcpy(file->buffer + len1 + 1, value, len2); |
| 831 | 821 | |
| 832 | 822 | /* parse with define parser */ |
| 833 | 823 | ch = file->buf_ptr[0]; |
| 834 | 824 | next_nomacro(); |
| 835 | 825 | parse_define(); |
| 836 | | − file = NULL; |
| 826 | + |
| 827 | + tcc_close(); |
| 837 | 828 | } |
| 838 | 829 | |
| 839 | 830 | /* undefine a preprocessor symbol */ |
| @@ -1082,9 +1073,6 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 242 unmodified lines |
| 1082 | 1073 | const char *ext; |
| 1083 | 1074 | ElfW(Ehdr) ehdr; |
| 1084 | 1075 | int fd, ret, size; |
| 1085 | | − BufferedFile *saved_file; |
| 1086 | | − |
| 1087 | | − ret = -1; |
| 1088 | 1076 | |
| 1089 | 1077 | /* find source file type with extension */ |
| 1090 | 1078 | ext = tcc_fileextension(filename); |
| @@ -1092,12 +1080,11 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 1 unmodified line |
| 1092 | 1080 | ext++; |
| 1093 | 1081 | |
| 1094 | 1082 | /* open the file */ |
| 1095 | | − saved_file = file; |
| 1096 | | − file = tcc_open(s1, filename); |
| 1097 | | − if (!file) { |
| 1083 | + ret = tcc_open(s1, filename); |
| 1084 | + if (ret < 0) { |
| 1098 | 1085 | if (flags & AFF_PRINT_ERROR) |
| 1099 | 1086 | error_noabort("file '%s' not found", filename); |
| 1100 | | − goto the_end; |
| 1087 | + return ret; |
| 1101 | 1088 | } |
| 1102 | 1089 | |
| 1103 | 1090 | /* update target deps */ |
| @@ -1192,9 +1179,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 88 unmodified lines |
| 1192 | 1179 | error_noabort("unrecognized file type"); |
| 1193 | 1180 | |
| 1194 | 1181 | the_end: |
| 1195 | | − if (file) |
| 1196 | | − tcc_close(file); |
| 1197 | | − file = saved_file; |
| 1182 | + tcc_close(); |
| 1198 | 1183 | return ret; |
| 1199 | 1184 | } |
| 1200 | 1185 | |