Home | History | Annotate | Line # | Download | only in lint1
ckgetopt.c revision 1.1
      1  1.1  rillig /* $NetBSD: ckgetopt.c,v 1.1 2021/02/19 12:28:56 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.1  rillig #include <sys/cdefs.h>
     33  1.1  rillig #if defined(__RCSID) && !defined(lint)
     34  1.1  rillig __RCSID("$NetBSD: ckgetopt.c,v 1.1 2021/02/19 12:28:56 rillig Exp $");
     35  1.1  rillig #endif
     36  1.1  rillig 
     37  1.1  rillig #include <stdbool.h>
     38  1.1  rillig #include <stdlib.h>
     39  1.1  rillig #include <string.h>
     40  1.1  rillig 
     41  1.1  rillig #include "lint1.h"
     42  1.1  rillig 
     43  1.1  rillig /*
     44  1.1  rillig  * In a typical while loop for parsing getopt options, ensure that each
     45  1.1  rillig  * option from the options string is handled, and that each handled option
     46  1.1  rillig  * is listed in the options string.
     47  1.1  rillig  */
     48  1.1  rillig 
     49  1.1  rillig struct {
     50  1.1  rillig 	pos_t options_pos;
     51  1.1  rillig 	char *options;
     52  1.1  rillig 	char *unhandled_options;
     53  1.1  rillig 	int while_level;
     54  1.1  rillig 	int switch_level;
     55  1.1  rillig } ck;
     56  1.1  rillig 
     57  1.1  rillig 
     58  1.1  rillig static bool
     59  1.1  rillig is_getopt_call(const tnode_t *tn, char **out_options)
     60  1.1  rillig {
     61  1.1  rillig 	if (tn == NULL)
     62  1.1  rillig 		return false;
     63  1.1  rillig 	if (tn->tn_op != NE)
     64  1.1  rillig 		return false;
     65  1.1  rillig 	if (tn->tn_left->tn_op != ASSIGN)
     66  1.1  rillig 		return false;
     67  1.1  rillig 
     68  1.1  rillig 	const tnode_t *call = tn->tn_left->tn_right;
     69  1.1  rillig 	if (call->tn_op != CALL)
     70  1.1  rillig 		return false;
     71  1.1  rillig 	if (call->tn_left->tn_op != ADDR)
     72  1.1  rillig 		return false;
     73  1.1  rillig 	if (call->tn_left->tn_left->tn_op != NAME)
     74  1.1  rillig 		return false;
     75  1.1  rillig 	if (strcmp(call->tn_left->tn_left->tn_sym->s_name, "getopt") != 0)
     76  1.1  rillig 		return false;
     77  1.1  rillig 
     78  1.1  rillig 	if (call->tn_right->tn_op != PUSH)
     79  1.1  rillig 		return false;
     80  1.1  rillig 
     81  1.1  rillig 	const tnode_t *last_arg = call->tn_right->tn_left;
     82  1.1  rillig 	if (last_arg->tn_op != CVT)
     83  1.1  rillig 		return false;
     84  1.1  rillig 	if (last_arg->tn_left->tn_op != ADDR)
     85  1.1  rillig 		return false;
     86  1.1  rillig 	if (last_arg->tn_left->tn_left->tn_op != STRING)
     87  1.1  rillig 		return false;
     88  1.1  rillig 	if (last_arg->tn_left->tn_left->tn_string->st_tspec != CHAR)
     89  1.1  rillig 		return false;
     90  1.1  rillig 
     91  1.1  rillig 	*out_options = xstrdup(
     92  1.1  rillig 	    (const char *)last_arg->tn_left->tn_left->tn_string->st_cp);
     93  1.1  rillig 	return true;
     94  1.1  rillig }
     95  1.1  rillig 
     96  1.1  rillig static void
     97  1.1  rillig check_unlisted_option(char opt)
     98  1.1  rillig {
     99  1.1  rillig 	if (opt == '?')
    100  1.1  rillig 		return;
    101  1.1  rillig 
    102  1.1  rillig 	const char *optptr = strchr(ck.options, opt);
    103  1.1  rillig 	if (optptr != NULL)
    104  1.1  rillig 		ck.unhandled_options[optptr - ck.options] = ' ';
    105  1.1  rillig 	else {
    106  1.1  rillig 		/* option '%c' should be listed in the options string */
    107  1.1  rillig 		warning(339, opt);
    108  1.1  rillig 		return;
    109  1.1  rillig 	}
    110  1.1  rillig }
    111  1.1  rillig 
    112  1.1  rillig static void
    113  1.1  rillig check_unhandled_option(void)
    114  1.1  rillig {
    115  1.1  rillig 	for (const char *opt = ck.unhandled_options; *opt != '\0'; opt++) {
    116  1.1  rillig 		if (*opt == ' ' || *opt == ':')
    117  1.1  rillig 			continue;
    118  1.1  rillig 
    119  1.1  rillig 		pos_t prev_pos = curr_pos;
    120  1.1  rillig 		curr_pos = ck.options_pos;
    121  1.1  rillig 		/* option '%c' should be handled in the switch */
    122  1.1  rillig 		warning(338, *opt);
    123  1.1  rillig 		curr_pos = prev_pos;
    124  1.1  rillig 	}
    125  1.1  rillig }
    126  1.1  rillig 
    127  1.1  rillig 
    128  1.1  rillig void
    129  1.1  rillig check_getopt_begin_while(const tnode_t *tn)
    130  1.1  rillig {
    131  1.1  rillig 	if (ck.while_level == 0 && is_getopt_call(tn, &ck.options)) {
    132  1.1  rillig 		ck.unhandled_options = xstrdup(ck.options);
    133  1.1  rillig 		ck.options_pos = curr_pos;
    134  1.1  rillig 	}
    135  1.1  rillig 	ck.while_level++;
    136  1.1  rillig }
    137  1.1  rillig 
    138  1.1  rillig void
    139  1.1  rillig check_getopt_begin_switch(void)
    140  1.1  rillig {
    141  1.1  rillig 	if (ck.while_level > 0)
    142  1.1  rillig 		ck.switch_level++;
    143  1.1  rillig }
    144  1.1  rillig 
    145  1.1  rillig 
    146  1.1  rillig void
    147  1.1  rillig check_getopt_case_label(int64_t value)
    148  1.1  rillig {
    149  1.1  rillig 	if (ck.switch_level == 1 && value == (char)value)
    150  1.1  rillig 		check_unlisted_option((char)value);
    151  1.1  rillig }
    152  1.1  rillig 
    153  1.1  rillig void
    154  1.1  rillig check_getopt_end_switch(void)
    155  1.1  rillig {
    156  1.1  rillig 	if (ck.switch_level == 0)
    157  1.1  rillig 		return;
    158  1.1  rillig 
    159  1.1  rillig 	ck.switch_level--;
    160  1.1  rillig 	if (ck.switch_level == 0)
    161  1.1  rillig 		check_unhandled_option();
    162  1.1  rillig }
    163  1.1  rillig 
    164  1.1  rillig void
    165  1.1  rillig check_getopt_end_while(void)
    166  1.1  rillig {
    167  1.1  rillig 	if (ck.while_level == 0)
    168  1.1  rillig 		return;
    169  1.1  rillig 
    170  1.1  rillig 	ck.while_level--;
    171  1.1  rillig 	if (ck.while_level != 0)
    172  1.1  rillig 		return;
    173  1.1  rillig 
    174  1.1  rillig 	free(ck.options);
    175  1.1  rillig 	free(ck.unhandled_options);
    176  1.1  rillig 	ck.options = NULL;
    177  1.1  rillig 	ck.unhandled_options = NULL;
    178  1.1  rillig }
    179