LCOV - code coverage report
Current view: top level - ports/unix - input.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 40 42 95.2 %
Date: 2024-04-25 17:00:48 Functions: 2 2 100.0 %
Branches: 16 30 53.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython 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                 :            : 
      27                 :            : #include <errno.h>
      28                 :            : #include <stdio.h>
      29                 :            : #include <stdlib.h>
      30                 :            : #include <string.h>
      31                 :            : #include <fcntl.h>
      32                 :            : 
      33                 :            : #include "py/mpstate.h"
      34                 :            : #include "py/mphal.h"
      35                 :            : #include "input.h"
      36                 :            : 
      37                 :            : #if MICROPY_USE_READLINE == 1
      38                 :            : #include "shared/readline/readline.h"
      39                 :            : #endif
      40                 :            : 
      41                 :            : #if MICROPY_USE_READLINE == 0
      42                 :            : char *prompt(char *p) {
      43                 :            :     // simple read string
      44                 :            :     static char buf[256];
      45                 :            :     fputs(p, stdout);
      46                 :            :     fflush(stdout);
      47                 :            :     char *s = fgets(buf, sizeof(buf), stdin);
      48                 :            :     if (!s) {
      49                 :            :         return NULL;
      50                 :            :     }
      51                 :            :     int l = strlen(buf);
      52                 :            :     if (buf[l - 1] == '\n') {
      53                 :            :         buf[l - 1] = 0;
      54                 :            :     } else {
      55                 :            :         l++;
      56                 :            :     }
      57                 :            :     char *line = malloc(l);
      58                 :            :     memcpy(line, buf, l);
      59                 :            :     return line;
      60                 :            : }
      61                 :            : #endif
      62                 :            : 
      63                 :         28 : void prompt_read_history(void) {
      64                 :            :     #if MICROPY_USE_READLINE_HISTORY
      65                 :            :     #if MICROPY_USE_READLINE == 1
      66                 :         28 :     readline_init0(); // will clear history pointers
      67                 :         28 :     char *home = getenv("HOME");
      68         [ +  - ]:         28 :     if (home != NULL) {
      69                 :         28 :         vstr_t vstr;
      70                 :         28 :         vstr_init(&vstr, 50);
      71                 :         28 :         vstr_printf(&vstr, "%s/.micropython.history", home);
      72                 :         28 :         int fd = open(vstr_null_terminated_str(&vstr), O_RDONLY);
      73         [ +  - ]:         28 :         if (fd != -1) {
      74                 :         28 :             vstr_reset(&vstr);
      75                 :      25248 :             for (;;) {
      76                 :      25248 :                 char c;
      77                 :      25248 :                 int sz = read(fd, &c, 1);
      78         [ -  + ]:      25248 :                 if (sz < 0) {
      79         [ #  # ]:          0 :                     if (errno == EINTR) {
      80                 :          0 :                         continue;
      81                 :            :                     }
      82                 :         28 :                     break;
      83                 :            :                 }
      84   [ +  +  +  + ]:      25248 :                 if (sz == 0 || c == '\n') {
      85                 :       1428 :                     readline_push_history(vstr_null_terminated_str(&vstr));
      86         [ +  + ]:       1428 :                     if (sz == 0) {
      87                 :            :                         break;
      88                 :            :                     }
      89                 :       1400 :                     vstr_reset(&vstr);
      90                 :            :                 } else {
      91                 :      23820 :                     vstr_add_byte(&vstr, c);
      92                 :            :                 }
      93                 :            :             }
      94                 :         28 :             close(fd);
      95                 :            :         }
      96                 :         28 :         vstr_clear(&vstr);
      97                 :            :     }
      98                 :            :     #endif
      99                 :            :     #endif
     100                 :         28 : }
     101                 :            : 
     102                 :         28 : void prompt_write_history(void) {
     103                 :            :     #if MICROPY_USE_READLINE_HISTORY
     104                 :            :     #if MICROPY_USE_READLINE == 1
     105                 :         28 :     char *home = getenv("HOME");
     106         [ +  - ]:         28 :     if (home != NULL) {
     107                 :         28 :         vstr_t vstr;
     108                 :         28 :         vstr_init(&vstr, 50);
     109                 :         28 :         vstr_printf(&vstr, "%s/.micropython.history", home);
     110                 :         28 :         int fd = open(vstr_null_terminated_str(&vstr), O_CREAT | O_TRUNC | O_WRONLY, 0644);
     111         [ +  - ]:         28 :         if (fd != -1) {
     112         [ +  + ]:       1428 :             for (int i = MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist)) - 1; i >= 0; i--) {
     113                 :       1400 :                 const char *line = MP_STATE_PORT(readline_hist)[i];
     114         [ +  - ]:       1400 :                 if (line != NULL) {
     115   [ -  +  -  - ]:       1400 :                     while (write(fd, line, strlen(line)) == -1 && errno == EINTR) {
     116                 :       1400 :                     }
     117   [ -  +  -  - ]:       1400 :                     while (write(fd, "\n", 1) == -1 && errno == EINTR) {
     118                 :       1400 :                     }
     119                 :            :                 }
     120                 :            :             }
     121                 :         28 :             close(fd);
     122                 :            :         }
     123                 :            :     }
     124                 :            :     #endif
     125                 :            :     #endif
     126                 :         28 : }

Generated by: LCOV version 1.15-5-g462f71d