comparison tcc.c @ 581:8595fe33590f

Support filename "-" as a synonym for /dev/stdin.
author Rob Landley <rob@landley.net>
date Sat, 29 Mar 2008 13:51:33 -0500
parents 1f1a692f7563
children 7fc19a001568
comparison
equal deleted inserted replaced
580:7ddeaeed4e94 581:8595fe33590f
913 { 913 {
914 int fd; 914 int fd;
915 BufferedFile *bf; 915 BufferedFile *bf;
916 int i, len; 916 int i, len;
917 917
918 fd = open(filename, O_RDONLY | O_BINARY); 918 if (!filename) fd = 0;
919 else fd = open(filename, O_RDONLY | O_BINARY);
919 if (fd < 0) 920 if (fd < 0)
920 return NULL; 921 return NULL;
921 bf = xmalloc(sizeof(BufferedFile)); 922 bf = xmalloc(sizeof(BufferedFile));
922 bf->fd = fd; 923 bf->fd = fd;
923 bf->buf_ptr = bf->buffer; 924 bf->buf_ptr = bf->buffer;
924 bf->buf_end = bf->buffer; 925 bf->buf_end = bf->buffer;
925 bf->buffer[0] = CH_EOB; /* put eob symbol */ 926 bf->buffer[0] = CH_EOB; /* put eob symbol */
926 pstrcpy(bf->filename, sizeof(bf->filename), filename); 927 pstrcpy(bf->filename, sizeof(bf->filename),
928 filename ? filename : "*stdin*");
927 len = strlen(bf->filename); 929 len = strlen(bf->filename);
928 for (i = 0; i < len; i++) 930 for (i = 0; i < len; i++)
929 if (bf->filename[i] == '\\') 931 if (bf->filename[i] == '\\')
930 bf->filename[i] = '/'; 932 bf->filename[i] = '/';
931 bf->line_num = 1; 933 bf->line_num = 1;
932 bf->ifndef_macro = 0; 934 bf->ifndef_macro = 0;
933 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; 935 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
934 // printf("opening '%s'\n", filename); 936
935 return bf; 937 return bf;
936 } 938 }
937 939
938 void tcc_close(BufferedFile *bf) 940 void tcc_close(BufferedFile *bf)
939 { 941 {
8915 free(s1); 8917 free(s1);
8916 } 8918 }
8917 8919
8918 int tcc_add_file_internal(TCCState *s1, char *filename, int flags) 8920 int tcc_add_file_internal(TCCState *s1, char *filename, int flags)
8919 { 8921 {
8920 char *ext, *filename1; 8922 char *ext = 0;
8921 int fd, ret; 8923 int fd, ret;
8922 BufferedFile *saved_file; 8924 BufferedFile *saved_file;
8923 8925
8924 /* find source file type with extension */
8925 filename1 = strrchr(filename, '/');
8926 if (filename1) filename1++;
8927 else filename1 = filename;
8928 ext = strrchr(filename1, '.');
8929 if (ext) ext++;
8930
8931 /* open the file */
8932 saved_file = file; 8926 saved_file = file;
8933 file = tcc_open(s1, filename); 8927
8928 // Treat filename "-" as /dev/stdin
8929 if (*filename=='-' && !filename[1]) file = tcc_open(s1, 0);
8930 else {
8931 char *filename1;
8932
8933 /* find source file type with extension */
8934 filename1 = strrchr(filename, '/');
8935 if (filename1) filename1++;
8936 else filename1 = filename;
8937 ext = strrchr(filename1, '.');
8938 if (ext) ext++;
8939
8940 file = tcc_open(s1, filename);
8941 }
8942
8934 if (tccg_verbose > !file) 8943 if (tccg_verbose > !file)
8935 printf("%s file '%s'\n", file ? "Read" : "Tried", filename); 8944 printf("%s file '%s'\n", file ? "Read" : "Tried", filename);
8936 if (!file) { 8945 if (!file) {
8937 if (flags & AFF_PRINT_ERROR) { 8946 if (flags & AFF_PRINT_ERROR) {
8938 error_noabort("file '%s' not found", filename); 8947 error_noabort("file '%s' not found", filename);