Home | History | Annotate | Line # | Download | only in lint1
ckgetopt.c revision 1.13
      1  1.13    rillig /* $NetBSD: ckgetopt.c,v 1.13 2021/11/01 19:10:07 rillig Exp $ */
      2   1.1    rillig 
      3   1.1    rillig /*-
      4   1.1    rillig  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5   1.1    rillig  * All rights reserved.
      6   1.1    rillig  *
      7   1.1    rillig  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    rillig  * by Roland Illig <rillig (at) NetBSD.org>.
      9   1.1    rillig  *
     10   1.1    rillig  * Redistribution and use in source and binary forms, with or without
     11   1.1    rillig  * modification, are permitted provided that the following conditions
     12   1.1    rillig  * are met:
     13   1.1    rillig  * 1. Redistributions of source code must retain the above copyright
     14   1.1    rillig  *    notice, this list of conditions and the following disclaimer.
     15   1.1    rillig  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    rillig  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    rillig  *    documentation and/or other materials provided with the distribution.
     18   1.1    rillig  *
     19   1.1    rillig  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1    rillig  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1    rillig  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1    rillig  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1    rillig  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1    rillig  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1    rillig  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1    rillig  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1    rillig  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1    rillig  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1    rillig  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    rillig  */
     31   1.1    rillig 
     32   1.2    rillig #if HAVE_NBTOOL_CONFIG_H
     33   1.2    rillig #include "nbtool_config.h"
     34   1.2    rillig #endif
     35   1.2    rillig 
     36   1.1    rillig #include <sys/cdefs.h>
     37   1.1    rillig #if defined(__RCSID) && !defined(lint)
     38  1.13    rillig __RCSID("$NetBSD: ckgetopt.c,v 1.13 2021/11/01 19:10:07 rillig Exp $");
     39   1.1    rillig #endif
     40   1.1    rillig 
     41   1.1    rillig #include <stdbool.h>
     42   1.1    rillig #include <stdlib.h>
     43   1.1    rillig #include <string.h>
     44   1.1    rillig 
     45   1.1    rillig #include "lint1.h"
     46   1.1    rillig 
     47   1.1    rillig /*
     48   1.1    rillig  * In a typical while loop for parsing getopt options, ensure that each
     49   1.1    rillig  * option from the options string is handled, and that each handled option
     50   1.1    rillig  * is listed in the options string.
     51   1.1    rillig  */
     52   1.1    rillig 
     53   1.7    rillig static struct {
     54   1.4    rillig 	/*
     55   1.4    rillig 	 * 0	means outside a while loop with a getopt call.
     56   1.4    rillig 	 * 1	means directly inside a while loop with a getopt call.
     57   1.4    rillig 	 * > 1	means in a nested while loop; this is used for finishing the
     58   1.4    rillig 	 *	check at the correct point.
     59   1.4    rillig 	 */
     60   1.4    rillig 	int while_level;
     61   1.4    rillig 
     62   1.4    rillig 	/*
     63   1.4    rillig 	 * The options string from the getopt call.  Whenever an option is
     64   1.5    rillig 	 * handled by a case label, it is set to ' '.  In the end, only ' '
     65   1.5    rillig 	 * and ':' should remain.
     66   1.4    rillig 	 */
     67   1.1    rillig 	pos_t options_pos;
     68   1.1    rillig 	char *options;
     69   1.4    rillig 
     70   1.4    rillig 	/*
     71   1.4    rillig 	 * The nesting level of switch statements, is only modified if
     72   1.4    rillig 	 * while_level > 0.  Only the case labels at switch_level == 1 are
     73   1.4    rillig 	 * relevant, all nested case labels are ignored.
     74   1.4    rillig 	 */
     75   1.1    rillig 	int switch_level;
     76   1.1    rillig } ck;
     77   1.1    rillig 
     78   1.6    rillig #define NEED(cond) \
     79   1.6    rillig 	do {				\
     80   1.6    rillig 		if (!(cond))		\
     81   1.6    rillig 			return false;	\
     82   1.6    rillig 	} while (false)
     83   1.1    rillig 
     84  1.10    rillig /* Return whether tn has the form 'getopt(argc, argv, "literal") != -1'. */
     85   1.1    rillig static bool
     86  1.10    rillig is_getopt_condition(const tnode_t *tn, char **out_options)
     87   1.1    rillig {
     88  1.13    rillig 	const tnode_t *call, *last_arg;
     89  1.13    rillig 
     90   1.6    rillig 	NEED(tn != NULL);
     91   1.6    rillig 	NEED(tn->tn_op == NE);
     92   1.6    rillig 	NEED(tn->tn_left->tn_op == ASSIGN);
     93   1.1    rillig 
     94  1.13    rillig 	call = tn->tn_left->tn_right;
     95   1.6    rillig 	NEED(call->tn_op == CALL);
     96   1.6    rillig 	NEED(call->tn_left->tn_op == ADDR);
     97   1.6    rillig 	NEED(call->tn_left->tn_left->tn_op == NAME);
     98   1.6    rillig 	NEED(strcmp(call->tn_left->tn_left->tn_sym->s_name, "getopt") == 0);
     99   1.1    rillig 
    100   1.6    rillig 	NEED(call->tn_right->tn_op == PUSH);
    101   1.1    rillig 
    102  1.13    rillig 	last_arg = call->tn_right->tn_left;
    103   1.6    rillig 	NEED(last_arg->tn_op == CVT);
    104   1.6    rillig 	NEED(last_arg->tn_left->tn_op == ADDR);
    105   1.6    rillig 	NEED(last_arg->tn_left->tn_left->tn_op == STRING);
    106   1.6    rillig 	NEED(last_arg->tn_left->tn_left->tn_string->st_tspec == CHAR);
    107   1.1    rillig 
    108   1.1    rillig 	*out_options = xstrdup(
    109   1.1    rillig 	    (const char *)last_arg->tn_left->tn_left->tn_string->st_cp);
    110   1.1    rillig 	return true;
    111   1.1    rillig }
    112   1.1    rillig 
    113   1.1    rillig static void
    114   1.1    rillig check_unlisted_option(char opt)
    115   1.1    rillig {
    116  1.13    rillig 	char *optptr;
    117  1.13    rillig 
    118   1.4    rillig 	lint_assert(ck.options != NULL);
    119   1.4    rillig 
    120  1.12    rillig 	if (opt == ':' && ck.options[0] != ':')
    121  1.12    rillig 		goto warn;
    122  1.12    rillig 
    123  1.13    rillig 	optptr = strchr(ck.options, opt);
    124   1.1    rillig 	if (optptr != NULL)
    125   1.5    rillig 		*optptr = ' ';
    126  1.11    rillig 	else if (opt != '?') {
    127  1.12    rillig 	warn:
    128   1.1    rillig 		/* option '%c' should be listed in the options string */
    129   1.1    rillig 		warning(339, opt);
    130   1.1    rillig 	}
    131   1.1    rillig }
    132   1.1    rillig 
    133   1.1    rillig static void
    134   1.1    rillig check_unhandled_option(void)
    135   1.1    rillig {
    136   1.5    rillig 	lint_assert(ck.options != NULL);
    137   1.3  christos 
    138   1.5    rillig 	for (const char *opt = ck.options; *opt != '\0'; opt++) {
    139   1.1    rillig 		if (*opt == ' ' || *opt == ':')
    140   1.1    rillig 			continue;
    141   1.1    rillig 
    142   1.1    rillig 		/* option '%c' should be handled in the switch */
    143   1.9    rillig 		warning_at(338, &ck.options_pos, *opt);
    144   1.1    rillig 	}
    145   1.1    rillig }
    146   1.1    rillig 
    147   1.1    rillig 
    148   1.1    rillig void
    149   1.1    rillig check_getopt_begin_while(const tnode_t *tn)
    150   1.1    rillig {
    151   1.4    rillig 	if (ck.while_level == 0) {
    152  1.10    rillig 		if (!is_getopt_condition(tn, &ck.options))
    153   1.4    rillig 			return;
    154   1.1    rillig 		ck.options_pos = curr_pos;
    155   1.1    rillig 	}
    156   1.1    rillig 	ck.while_level++;
    157   1.1    rillig }
    158   1.1    rillig 
    159   1.1    rillig void
    160   1.1    rillig check_getopt_begin_switch(void)
    161   1.1    rillig {
    162   1.1    rillig 	if (ck.while_level > 0)
    163   1.1    rillig 		ck.switch_level++;
    164   1.1    rillig }
    165   1.1    rillig 
    166   1.1    rillig void
    167   1.1    rillig check_getopt_case_label(int64_t value)
    168   1.1    rillig {
    169   1.1    rillig 	if (ck.switch_level == 1 && value == (char)value)
    170   1.1    rillig 		check_unlisted_option((char)value);
    171   1.1    rillig }
    172   1.1    rillig 
    173   1.1    rillig void
    174   1.1    rillig check_getopt_end_switch(void)
    175   1.1    rillig {
    176   1.1    rillig 	if (ck.switch_level == 0)
    177   1.1    rillig 		return;
    178   1.1    rillig 
    179   1.1    rillig 	ck.switch_level--;
    180   1.1    rillig 	if (ck.switch_level == 0)
    181   1.1    rillig 		check_unhandled_option();
    182   1.1    rillig }
    183   1.1    rillig 
    184   1.1    rillig void
    185   1.1    rillig check_getopt_end_while(void)
    186   1.1    rillig {
    187   1.1    rillig 	if (ck.while_level == 0)
    188   1.1    rillig 		return;
    189   1.1    rillig 
    190   1.1    rillig 	ck.while_level--;
    191   1.1    rillig 	if (ck.while_level != 0)
    192   1.1    rillig 		return;
    193   1.1    rillig 
    194   1.1    rillig 	free(ck.options);
    195   1.1    rillig 	ck.options = NULL;
    196   1.1    rillig }
    197