LCOV - code coverage report
Current view: top level - lib/cmdline - cmdline_vt100.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 23 42 54.8 %
Date: 2024-12-01 18:57:19 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 13 24 54.2 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation.
       3                 :            :  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
       4                 :            :  * All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include <stdint.h>
       8                 :            : #include <stdio.h>
       9                 :            : #include <string.h>
      10                 :            : 
      11                 :            : #include "cmdline_vt100.h"
      12                 :            : 
      13                 :            : const char *cmdline_vt100_commands[] = {
      14                 :            :         vt100_up_arr,
      15                 :            :         vt100_down_arr,
      16                 :            :         vt100_right_arr,
      17                 :            :         vt100_left_arr,
      18                 :            :         "\177",
      19                 :            :         "\n",
      20                 :            :         "\001",
      21                 :            :         "\005",
      22                 :            :         "\013",
      23                 :            :         "\031",
      24                 :            :         "\003",
      25                 :            :         "\006",
      26                 :            :         "\002",
      27                 :            :         vt100_suppr,
      28                 :            :         vt100_tab,
      29                 :            :         "\004",
      30                 :            :         "\014",
      31                 :            :         "\r",
      32                 :            :         "\033\177",
      33                 :            :         vt100_word_left,
      34                 :            :         vt100_word_right,
      35                 :            :         "?",
      36                 :            :         "\027",
      37                 :            :         "\020",
      38                 :            :         "\016",
      39                 :            :         "\033\144",
      40                 :            :         vt100_bs,
      41                 :            : };
      42                 :            : 
      43                 :            : void
      44                 :        248 : vt100_init(struct cmdline_vt100 *vt)
      45                 :            : {
      46         [ +  + ]:        248 :         if (!vt)
      47                 :            :                 return;
      48                 :        247 :         vt->state = CMDLINE_VT100_INIT;
      49                 :            : }
      50                 :            : 
      51                 :            : 
      52                 :            : static int
      53                 :       2289 : match_command(char *buf, unsigned int size)
      54                 :            : {
      55                 :            :         const char *cmd;
      56                 :            :         size_t cmdlen;
      57                 :            :         unsigned int i = 0;
      58                 :            : 
      59         [ +  + ]:      61342 :         for (i=0 ; i<sizeof(cmdline_vt100_commands)/sizeof(const char *) ; i++) {
      60                 :      59178 :                 cmd = *(cmdline_vt100_commands + i);
      61                 :            : 
      62                 :      59178 :                 cmdlen = strnlen(cmd, CMDLINE_VT100_BUF_SIZE);
      63         [ +  + ]:      59178 :                 if (size == cmdlen &&
      64         [ +  + ]:      39202 :                     !strncmp(buf, cmd, cmdlen)) {
      65                 :        125 :                         return i;
      66                 :            :                 }
      67                 :            :         }
      68                 :            : 
      69                 :            :         return -1;
      70                 :            : }
      71                 :            : 
      72                 :            : int
      73                 :       2290 : vt100_parser(struct cmdline_vt100 *vt, char ch)
      74                 :            : {
      75                 :            :         unsigned int size;
      76                 :       2290 :         uint8_t c = (uint8_t) ch;
      77                 :            : 
      78         [ +  + ]:       2290 :         if (!vt)
      79                 :            :                 return -1;
      80                 :            : 
      81         [ -  + ]:       2289 :         if (vt->bufpos >= CMDLINE_VT100_BUF_SIZE) {
      82                 :          0 :                 vt->state = CMDLINE_VT100_INIT;
      83                 :          0 :                 vt->bufpos = 0;
      84                 :            :         }
      85                 :            : 
      86                 :       2289 :         vt->buf[vt->bufpos++] = c;
      87                 :       2289 :         size = vt->bufpos;
      88                 :            : 
      89   [ +  -  -  - ]:       2289 :         switch (vt->state) {
      90                 :       2289 :         case CMDLINE_VT100_INIT:
      91         [ -  + ]:       2289 :                 if (c == 033) {
      92                 :          0 :                         vt->state = CMDLINE_VT100_ESCAPE;
      93                 :            :                 }
      94                 :            :                 else {
      95                 :       2289 :                         vt->bufpos = 0;
      96                 :       2289 :                         goto match_command;
      97                 :            :                 }
      98                 :          0 :                 break;
      99                 :            : 
     100                 :          0 :         case CMDLINE_VT100_ESCAPE:
     101         [ #  # ]:          0 :                 if (c == 0133) {
     102                 :          0 :                         vt->state = CMDLINE_VT100_ESCAPE_CSI;
     103                 :            :                 }
     104         [ #  # ]:          0 :                 else if (c >= 060 && c <= 0177) { /* XXX 0177 ? */
     105                 :          0 :                         vt->bufpos = 0;
     106                 :          0 :                         vt->state = CMDLINE_VT100_INIT;
     107                 :          0 :                         goto match_command;
     108                 :            :                 }
     109                 :            :                 break;
     110                 :            : 
     111                 :          0 :         case CMDLINE_VT100_ESCAPE_CSI:
     112         [ #  # ]:          0 :                 if (c >= 0100 && c <= 0176) {
     113                 :          0 :                         vt->bufpos = 0;
     114                 :          0 :                         vt->state = CMDLINE_VT100_INIT;
     115                 :          0 :                         goto match_command;
     116                 :            :                 }
     117                 :            :                 break;
     118                 :            : 
     119                 :          0 :         default:
     120                 :          0 :                 vt->bufpos = 0;
     121                 :          0 :                 break;
     122                 :            :         }
     123                 :            : 
     124                 :            :         return -2;
     125                 :            : 
     126                 :       2289 :  match_command:
     127                 :       2289 :         return match_command(vt->buf, size);
     128                 :            : }

Generated by: LCOV version 1.14