Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Ilya Dmitrichenko | 8e9482a | 2014-04-12 16:37:17 +0100 | [diff] [blame] | 27 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 28 | #include "misc.h" |
Ilya Dmitrichenko | 8e9482a | 2014-04-12 16:37:17 +0100 | [diff] [blame] | 29 | |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 30 | #if MICROPY_HELPER_LEXER_UNIX |
Ilya Dmitrichenko | 8e9482a | 2014-04-12 16:37:17 +0100 | [diff] [blame] | 31 | |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> |
Damien George | b502156 | 2014-04-13 13:25:05 +0100 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 37 | |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 38 | #include "qstr.h" |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 39 | #include "lexer.h" |
Damien George | 9193f89 | 2014-01-08 15:28:26 +0000 | [diff] [blame] | 40 | #include "lexerunix.h" |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 41 | |
Damien George | ffa9bdd | 2014-04-10 15:00:58 +0100 | [diff] [blame] | 42 | typedef struct _mp_lexer_file_buf_t { |
| 43 | int fd; |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame^] | 44 | byte buf[20]; |
| 45 | mp_uint_t len; |
| 46 | mp_uint_t pos; |
Damien George | ffa9bdd | 2014-04-10 15:00:58 +0100 | [diff] [blame] | 47 | } mp_lexer_file_buf_t; |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 48 | |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame^] | 49 | STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) { |
Damien George | ffa9bdd | 2014-04-10 15:00:58 +0100 | [diff] [blame] | 50 | if (fb->pos >= fb->len) { |
Damien George | 185f9c1 | 2014-04-28 11:43:28 +0100 | [diff] [blame] | 51 | if (fb->len == 0) { |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame^] | 52 | return MP_LEXER_EOF; |
Damien George | ffa9bdd | 2014-04-10 15:00:58 +0100 | [diff] [blame] | 53 | } else { |
| 54 | int n = read(fb->fd, fb->buf, sizeof(fb->buf)); |
| 55 | if (n <= 0) { |
Damien George | 185f9c1 | 2014-04-28 11:43:28 +0100 | [diff] [blame] | 56 | fb->len = 0; |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame^] | 57 | return MP_LEXER_EOF; |
Damien George | ffa9bdd | 2014-04-10 15:00:58 +0100 | [diff] [blame] | 58 | } |
| 59 | fb->len = n; |
| 60 | fb->pos = 0; |
| 61 | } |
| 62 | } |
| 63 | return fb->buf[fb->pos++]; |
| 64 | } |
| 65 | |
| 66 | STATIC void file_buf_close(mp_lexer_file_buf_t *fb) { |
| 67 | close(fb->fd); |
| 68 | m_del_obj(mp_lexer_file_buf_t, fb); |
| 69 | } |
| 70 | |
| 71 | mp_lexer_t *mp_lexer_new_from_file(const char *filename) { |
| 72 | mp_lexer_file_buf_t *fb = m_new_obj(mp_lexer_file_buf_t); |
| 73 | fb->fd = open(filename, O_RDONLY); |
| 74 | if (fb->fd < 0) { |
| 75 | m_del_obj(mp_lexer_file_buf_t, fb); |
| 76 | return NULL; |
| 77 | } |
| 78 | int n = read(fb->fd, fb->buf, sizeof(fb->buf)); |
| 79 | fb->len = n; |
| 80 | fb->pos = 0; |
Damien George | 94fbe97 | 2014-07-30 11:46:05 +0100 | [diff] [blame^] | 81 | return mp_lexer_new(qstr_from_str(filename), fb, (mp_lexer_stream_next_byte_t)file_buf_next_byte, (mp_lexer_stream_close_t)file_buf_close); |
Damien | a5185f4 | 2013-10-20 14:41:27 +0100 | [diff] [blame] | 82 | } |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 83 | |
Damien George | 58ebde4 | 2014-05-21 20:32:59 +0100 | [diff] [blame] | 84 | #endif // MICROPY_HELPER_LEXER_UNIX |