LCOV - code coverage report
Current view: top level - lib/cmdline - cmdline.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 60 78 76.9 %
Date: 2025-05-01 17:49:45 Functions: 9 11 81.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 56 69.6 %

           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 <stdio.h>
       8                 :            : #include <string.h>
       9                 :            : #include <unistd.h>
      10                 :            : #include <stdlib.h>
      11                 :            : #include <stdarg.h>
      12                 :            : #include <errno.h>
      13                 :            : 
      14                 :            : #include <eal_export.h>
      15                 :            : #include <rte_string_fns.h>
      16                 :            : 
      17                 :            : #include "cmdline_private.h"
      18                 :            : 
      19                 :            : static void
      20                 :        126 : cmdline_valid_buffer(struct rdline *rdl, const char *buf,
      21                 :            :                      __rte_unused unsigned int size)
      22                 :            : {
      23                 :        126 :         struct cmdline *cl = rdl->opaque;
      24                 :            :         int ret;
      25                 :        126 :         ret = cmdline_parse(cl, buf);
      26         [ -  + ]:        121 :         if (ret == CMDLINE_PARSE_AMBIGUOUS)
      27                 :          0 :                 cmdline_printf(cl, "Ambiguous command\n");
      28         [ -  + ]:        121 :         else if (ret == CMDLINE_PARSE_NOMATCH)
      29                 :          0 :                 cmdline_printf(cl, "Command not found\n");
      30         [ -  + ]:        121 :         else if (ret == CMDLINE_PARSE_BAD_ARGS)
      31                 :          0 :                 cmdline_printf(cl, "Bad arguments\n");
      32                 :        121 : }
      33                 :            : 
      34                 :            : static int
      35                 :          0 : cmdline_complete_buffer(struct rdline *rdl, const char *buf,
      36                 :            :                         char *dstbuf, unsigned int dstsize,
      37                 :            :                         int *state)
      38                 :            : {
      39                 :          0 :         struct cmdline *cl = rdl->opaque;
      40                 :          0 :         return cmdline_complete(cl, buf, state, dstbuf, dstsize);
      41                 :            : }
      42                 :            : 
      43                 :            : RTE_EXPORT_SYMBOL(cmdline_write_char)
      44                 :            : int
      45                 :       3678 : cmdline_write_char(struct rdline *rdl, char c)
      46                 :            : {
      47                 :            :         int ret = -1;
      48                 :            :         struct cmdline *cl;
      49                 :            : 
      50         [ +  + ]:       3678 :         if (!rdl)
      51                 :            :                 return -1;
      52                 :            : 
      53                 :       3677 :         cl = rdl->opaque;
      54                 :            : 
      55         [ +  + ]:       3677 :         if (cl->s_out >= 0)
      56                 :       3661 :                 ret = write(cl->s_out, &c, 1);
      57                 :            : 
      58                 :            :         return ret;
      59                 :            : }
      60                 :            : 
      61                 :            : 
      62                 :            : RTE_EXPORT_SYMBOL(cmdline_set_prompt)
      63                 :            : void
      64                 :        130 : cmdline_set_prompt(struct cmdline *cl, const char *prompt)
      65                 :            : {
      66         [ +  + ]:        130 :         if (!cl || !prompt)
      67                 :            :                 return;
      68                 :        129 :         strlcpy(cl->prompt, prompt, sizeof(cl->prompt));
      69                 :            : }
      70                 :            : 
      71                 :            : RTE_EXPORT_SYMBOL(cmdline_new)
      72                 :            : struct cmdline *
      73                 :        135 : cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
      74                 :            : {
      75                 :            :         struct cmdline *cl;
      76                 :            :         int ret;
      77                 :            : 
      78         [ +  + ]:        135 :         if (!ctx || !prompt)
      79                 :            :                 return NULL;
      80                 :            : 
      81                 :        129 :         cl = malloc(sizeof(struct cmdline));
      82         [ +  - ]:        129 :         if (cl == NULL)
      83                 :            :                 return NULL;
      84                 :            :         memset(cl, 0, sizeof(struct cmdline));
      85                 :        129 :         cl->s_in = s_in;
      86                 :        129 :         cl->s_out = s_out;
      87                 :        129 :         cl->ctx = ctx;
      88                 :            : 
      89                 :        129 :         ret = rdline_init(&cl->rdl, cmdline_write_char, cmdline_valid_buffer,
      90                 :            :                         cmdline_complete_buffer, cl);
      91         [ -  + ]:        129 :         if (ret != 0) {
      92                 :          0 :                 free(cl);
      93                 :          0 :                 return NULL;
      94                 :            :         }
      95                 :            : 
      96                 :        129 :         cmdline_set_prompt(cl, prompt);
      97                 :        129 :         rdline_newline(&cl->rdl, cl->prompt);
      98                 :            : 
      99                 :        129 :         return cl;
     100                 :            : }
     101                 :            : 
     102                 :            : RTE_EXPORT_SYMBOL(cmdline_get_rdline)
     103                 :            : struct rdline*
     104                 :          0 : cmdline_get_rdline(struct cmdline *cl)
     105                 :            : {
     106                 :          0 :         return &cl->rdl;
     107                 :            : }
     108                 :            : 
     109                 :            : RTE_EXPORT_SYMBOL(cmdline_free)
     110                 :            : void
     111                 :        125 : cmdline_free(struct cmdline *cl)
     112                 :            : {
     113                 :            :         dprintf("called\n");
     114                 :            : 
     115         [ +  + ]:        125 :         if (!cl)
     116                 :            :                 return;
     117                 :            : 
     118         [ +  + ]:        124 :         if (cl->s_in > 2)
     119                 :          1 :                 close(cl->s_in);
     120   [ +  +  -  + ]:        124 :         if (cl->s_out != cl->s_in && cl->s_out > 2)
     121                 :          0 :                 close(cl->s_out);
     122                 :        124 :         free(cl);
     123                 :            : }
     124                 :            : 
     125                 :            : RTE_EXPORT_SYMBOL(cmdline_printf)
     126                 :            : void
     127                 :          1 : cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
     128                 :            : {
     129                 :            :         va_list ap;
     130                 :            : 
     131         [ -  + ]:          1 :         if (!cl || !fmt)
     132                 :          1 :                 return;
     133                 :            : 
     134         [ #  # ]:          0 :         if (cl->s_out < 0)
     135                 :            :                 return;
     136                 :          0 :         va_start(ap, fmt);
     137                 :          0 :         cmdline_vdprintf(cl->s_out, fmt, ap);
     138                 :          0 :         va_end(ap);
     139                 :            : }
     140                 :            : 
     141                 :            : RTE_EXPORT_SYMBOL(cmdline_in)
     142                 :            : int
     143                 :        132 : cmdline_in(struct cmdline *cl, const char *buf, int size)
     144                 :            : {
     145                 :            :         const char *history, *buffer;
     146                 :            :         size_t histlen, buflen;
     147                 :            :         int ret = 0;
     148                 :            :         int i, same;
     149                 :            : 
     150         [ +  + ]:        132 :         if (!cl || !buf)
     151                 :            :                 return -1;
     152                 :            : 
     153         [ +  + ]:       2429 :         for (i=0; i<size; i++) {
     154                 :       2305 :                 ret = rdline_char_in(&cl->rdl, buf[i]);
     155                 :            : 
     156         [ +  + ]:       2300 :                 if (ret == RDLINE_RES_VALIDATED) {
     157                 :        120 :                         buffer = rdline_get_buffer(&cl->rdl);
     158                 :        120 :                         history = rdline_get_history_item(&cl->rdl, 0);
     159         [ -  + ]:        120 :                         if (history) {
     160                 :          0 :                                 histlen = strnlen(history, RDLINE_BUF_SIZE);
     161         [ #  # ]:          0 :                                 same = !memcmp(buffer, history, histlen) &&
     162         [ #  # ]:          0 :                                         buffer[histlen] == '\n';
     163                 :            :                         }
     164                 :            :                         else
     165                 :            :                                 same = 0;
     166                 :        120 :                         buflen = strnlen(buffer, RDLINE_BUF_SIZE);
     167         [ +  - ]:        120 :                         if (buflen > 1 && !same)
     168                 :        120 :                                 rdline_add_history(&cl->rdl, buffer);
     169                 :        120 :                         rdline_newline(&cl->rdl, cl->prompt);
     170                 :            :                 }
     171         [ +  - ]:       2180 :                 else if (ret == RDLINE_RES_EOF)
     172                 :            :                         return -1;
     173         [ +  + ]:       2180 :                 else if (ret == RDLINE_RES_EXITED)
     174                 :            :                         return -1;
     175                 :            :         }
     176                 :            :         return i;
     177                 :            : }
     178                 :            : 
     179                 :            : RTE_EXPORT_SYMBOL(cmdline_quit)
     180                 :            : void
     181                 :          2 : cmdline_quit(struct cmdline *cl)
     182                 :            : {
     183         [ +  + ]:          2 :         if (!cl)
     184                 :            :                 return;
     185                 :          1 :         cmdline_cancel(cl);
     186                 :          1 :         rdline_quit(&cl->rdl);
     187                 :            : }
     188                 :            : 
     189                 :            : RTE_EXPORT_SYMBOL(cmdline_interact)
     190                 :            : void
     191                 :          2 : cmdline_interact(struct cmdline *cl)
     192                 :            : {
     193                 :            :         char c;
     194                 :            : 
     195         [ +  + ]:          2 :         if (!cl)
     196                 :          1 :                 return;
     197                 :            : 
     198                 :          1 :         c = -1;
     199                 :            :         while (1) {
     200         [ +  - ]:          5 :                 if (cmdline_read_char(cl, &c) <= 0)
     201                 :            :                         break;
     202         [ +  + ]:          5 :                 if (cmdline_in(cl, &c, 1) < 0)
     203                 :            :                         break;
     204                 :            :         }
     205                 :            : }

Generated by: LCOV version 1.14