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