LCOV - code coverage report
Current view: top level - src - alias.c (source / functions) Hit Total Coverage
Test: rapport Lines: 30 45 66.7 %
Date: 2021-12-10 16:22:55 Functions: 1 2 50.0 %
Branches: 19 27 70.4 %

           Branch data     Line data    Source code
       1                 :            : /*-
       2                 :            :  * Copyright (c) 2015 Lars Engels <lme@FreeBSD.org>
       3                 :            :  * All rights reserved.
       4                 :            :  * 
       5                 :            :  * Redistribution and use in source and binary forms, with or without
       6                 :            :  * modification, are permitted provided that the following conditions
       7                 :            :  * are met:
       8                 :            :  * 1. Redistributions of source code must retain the above copyright
       9                 :            :  *    notice, this list of conditions and the following disclaimer
      10                 :            :  *    in this position and unchanged.
      11                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      12                 :            :  *    notice, this list of conditions and the following disclaimer in the
      13                 :            :  *    documentation and/or other materials provided with the distribution.
      14                 :            :  * 
      15                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      16                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      17                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      18                 :            :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      19                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      20                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      21                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      22                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      23                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      24                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include <sys/param.h>
      28                 :            : 
      29                 :            : #include <err.h>
      30                 :            : #include <errno.h>
      31                 :            : #include <libgen.h>
      32                 :            : #include <stdio.h>
      33                 :            : #include <string.h>
      34                 :            : #include <unistd.h>
      35                 :            : #include <getopt.h>
      36                 :            : 
      37                 :            : #include <pkg.h>
      38                 :            : 
      39                 :            : #include "pkgcli.h"
      40                 :            : 
      41                 :            : void
      42                 :          0 : usage_alias(void)
      43                 :            : {
      44                 :          0 :         fprintf(stderr, "Usage: pkg alias [-ql] [alias]\n\n");
      45                 :          0 :         fprintf(stderr, "For more information see 'pkg help alias'.\n");
      46                 :          0 : }
      47                 :            : 
      48                 :            : int
      49                 :        143 : exec_alias(int argc, char **argv)
      50                 :            : {
      51                 :            :         const pkg_object *all_aliases;
      52                 :            :         const pkg_object *alias;
      53                 :        143 :         pkg_iter it = NULL;
      54                 :            :         int ch;
      55                 :        143 :         int ret = EXIT_SUCCESS;
      56                 :        143 :         bool list = false;
      57                 :            : 
      58                 :        143 :         struct option longopts[] = {
      59                 :            :                 { "quiet",    no_argument,    NULL, 'q' },
      60                 :            :                 { "list",     no_argument,    NULL, 'l' },
      61                 :            :                 { NULL,         0,              NULL, 0 },
      62                 :            :         };
      63                 :            : 
      64         [ +  + ]:        208 :         while ((ch = getopt_long(argc, argv, "+ql", longopts, NULL)) != -1) {
      65      [ +  -  - ]:         65 :                 switch (ch) {
      66                 :            :                 case 'q':
      67                 :         65 :                         quiet = true;
      68                 :         65 :                         break;
      69                 :            :                 case 'l':
      70                 :          0 :                         list = true;
      71                 :          0 :                         break;
      72                 :            :                 default:
      73                 :          0 :                         usage_alias();
      74                 :          0 :                         return (EXIT_FAILURE);
      75                 :            :                 }
      76                 :            :         }
      77                 :            : 
      78                 :        143 :         argc -= optind;
      79                 :        143 :         argv += optind;
      80                 :            : 
      81                 :        143 :         all_aliases = pkg_config_get("ALIAS");
      82                 :            : 
      83         [ +  + ]:        143 :         if (argc == 0) {
      84   [ +  +  -  + ]:        104 :                 if (!quiet && list)
      85                 :          0 :                         printf("%s\n", "ALIAS");
      86         [ +  + ]:        104 :                 else if (!quiet)
      87                 :         39 :                         printf("%-20s %s\n", "ALIAS", "ARGUMENTS");
      88         [ +  + ]:        260 :                 while ((alias = pkg_object_iterate(all_aliases, &it))) {
      89         [ -  + ]:        156 :                         if (list)
      90                 :          0 :                                 printf("%s\n", pkg_object_key(alias));
      91                 :            :                         else
      92                 :        156 :                                 printf("%-20s '%s'\n", pkg_object_key(alias), pkg_object_string(alias));
      93                 :            :                 }
      94                 :        104 :                 return (ret);
      95                 :            :         }
      96                 :            : 
      97         [ +  + ]:         78 :         for (int i = 0; i < argc; i++) {
      98                 :         39 :                 it = NULL;
      99         [ +  + ]:         91 :                 while ((alias = pkg_object_iterate(all_aliases, &it))) {
     100         [ +  - ]:         52 :                         if (strcmp(argv[i], pkg_object_key(alias)) == 0)
     101                 :          0 :                                 break;
     102                 :            :                 }
     103         [ -  + ]:         39 :                 if (alias) {
     104         [ #  # ]:          0 :                         if (list)
     105                 :          0 :                                 printf("%s\n", argv[i]);
     106                 :            :                         else
     107                 :          0 :                                 printf("%-20s '%s'\n", argv[i], pkg_object_string(alias));
     108                 :          0 :                 } else {
     109                 :         39 :                         warnx("No such alias: '%s'", argv[i]);
     110                 :         39 :                         ret = EXIT_FAILURE;
     111                 :            :                 }
     112                 :         39 :         }
     113                 :            : 
     114                 :         39 :         return (ret);
     115                 :        143 : }
     116                 :            : 

Generated by: LCOV version 1.15