Home | History | Annotate | Line # | Download | only in gen
      1  1.38    rillig /*	$NetBSD: setmode.c,v 1.38 2022/04/19 20:32:15 rillig Exp $	*/
      2  1.10       cgd 
      3   1.1       cgd /*
      4   1.9       cgd  * Copyright (c) 1989, 1993, 1994
      5   1.9       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.9       cgd  *
      7   1.9       cgd  * This code is derived from software contributed to Berkeley by
      8   1.9       cgd  * Dave Borman at Cray Research, Inc.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.16  christos #include <sys/cdefs.h>
     36   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     37  1.10       cgd #if 0
     38  1.10       cgd static char sccsid[] = "@(#)setmode.c	8.2 (Berkeley) 3/25/94";
     39  1.10       cgd #else
     40  1.38    rillig __RCSID("$NetBSD: setmode.c,v 1.38 2022/04/19 20:32:15 rillig Exp $");
     41  1.10       cgd #endif
     42   1.1       cgd #endif /* LIBC_SCCS and not lint */
     43   1.1       cgd 
     44  1.17       jtc #include "namespace.h"
     45   1.6       cgd #include <sys/types.h>
     46   1.1       cgd #include <sys/stat.h>
     47   1.9       cgd 
     48  1.22     lukem #include <assert.h>
     49   1.9       cgd #include <ctype.h>
     50   1.9       cgd #include <errno.h>
     51   1.6       cgd #include <signal.h>
     52   1.9       cgd #include <stdlib.h>
     53  1.31  christos #include <limits.h>
     54  1.14       cgd #include <unistd.h>
     55   1.9       cgd 
     56   1.1       cgd #ifdef SETMODE_DEBUG
     57   1.1       cgd #include <stdio.h>
     58  1.17       jtc #endif
     59  1.17       jtc 
     60  1.17       jtc #ifdef __weak_alias
     61  1.26   mycroft __weak_alias(getmode,_getmode)
     62  1.26   mycroft __weak_alias(setmode,_setmode)
     63   1.1       cgd #endif
     64   1.1       cgd 
     65   1.1       cgd #define	SET_LEN	6		/* initial # of bitcmd struct to malloc */
     66   1.1       cgd #define	SET_LEN_INCR 4		/* # of bitcmd structs to add as needed */
     67   1.1       cgd 
     68   1.6       cgd typedef struct bitcmd {
     69   1.1       cgd 	char	cmd;
     70   1.1       cgd 	char	cmd2;
     71   1.1       cgd 	mode_t	bits;
     72   1.6       cgd } BITCMD;
     73   1.1       cgd 
     74   1.1       cgd #define	CMD2_CLR	0x01
     75   1.1       cgd #define	CMD2_SET	0x02
     76   1.1       cgd #define	CMD2_GBITS	0x04
     77   1.1       cgd #define	CMD2_OBITS	0x08
     78   1.1       cgd #define	CMD2_UBITS	0x10
     79   1.1       cgd 
     80  1.32      matt static BITCMD	*addcmd(BITCMD *, mode_t, mode_t, mode_t, mode_t);
     81  1.32      matt static void	 compress_mode(BITCMD *);
     82   1.6       cgd #ifdef SETMODE_DEBUG
     83  1.32      matt static void	 dumpmode(BITCMD *);
     84   1.6       cgd #endif
     85   1.6       cgd 
     86   1.1       cgd /*
     87   1.1       cgd  * Given the old mode and an array of bitcmd structures, apply the operations
     88   1.1       cgd  * described in the bitcmd structures to the old mode, and return the new mode.
     89   1.1       cgd  * Note that there is no '=' command; a strict assignment is just a '-' (clear
     90   1.1       cgd  * bits) followed by a '+' (set bits).
     91   1.1       cgd  */
     92   1.1       cgd mode_t
     93  1.34       abs getmode(const void *bbox, mode_t omode)
     94   1.1       cgd {
     95  1.19     perry 	const BITCMD *set;
     96  1.19     perry 	mode_t clrval, newmode, value;
     97   1.1       cgd 
     98  1.22     lukem 	_DIAGASSERT(bbox != NULL);
     99  1.22     lukem 
    100  1.14       cgd 	set = (const BITCMD *)bbox;
    101   1.1       cgd 	newmode = omode;
    102   1.1       cgd 	for (value = 0;; set++)
    103   1.1       cgd 		switch(set->cmd) {
    104   1.1       cgd 		/*
    105   1.1       cgd 		 * When copying the user, group or other bits around, we "know"
    106   1.6       cgd 		 * where the bits are in the mode so that we can do shifts to
    107   1.1       cgd 		 * copy them around.  If we don't use shifts, it gets real
    108   1.1       cgd 		 * grundgy with lots of single bit checks and bit sets.
    109   1.1       cgd 		 */
    110   1.1       cgd 		case 'u':
    111   1.1       cgd 			value = (newmode & S_IRWXU) >> 6;
    112   1.1       cgd 			goto common;
    113   1.1       cgd 
    114   1.1       cgd 		case 'g':
    115   1.1       cgd 			value = (newmode & S_IRWXG) >> 3;
    116   1.1       cgd 			goto common;
    117   1.1       cgd 
    118   1.1       cgd 		case 'o':
    119   1.1       cgd 			value = newmode & S_IRWXO;
    120   1.6       cgd common:			if (set->cmd2 & CMD2_CLR) {
    121   1.8       cgd 				clrval =
    122   1.8       cgd 				    (set->cmd2 & CMD2_SET) ?  S_IRWXO : value;
    123   1.1       cgd 				if (set->cmd2 & CMD2_UBITS)
    124   1.8       cgd 					newmode &= ~((clrval<<6) & set->bits);
    125   1.1       cgd 				if (set->cmd2 & CMD2_GBITS)
    126   1.8       cgd 					newmode &= ~((clrval<<3) & set->bits);
    127   1.1       cgd 				if (set->cmd2 & CMD2_OBITS)
    128   1.8       cgd 					newmode &= ~(clrval & set->bits);
    129   1.1       cgd 			}
    130   1.1       cgd 			if (set->cmd2 & CMD2_SET) {
    131   1.1       cgd 				if (set->cmd2 & CMD2_UBITS)
    132   1.1       cgd 					newmode |= (value<<6) & set->bits;
    133   1.1       cgd 				if (set->cmd2 & CMD2_GBITS)
    134   1.1       cgd 					newmode |= (value<<3) & set->bits;
    135   1.1       cgd 				if (set->cmd2 & CMD2_OBITS)
    136   1.1       cgd 					newmode |= value & set->bits;
    137   1.1       cgd 			}
    138   1.1       cgd 			break;
    139   1.1       cgd 
    140   1.1       cgd 		case '+':
    141   1.1       cgd 			newmode |= set->bits;
    142   1.1       cgd 			break;
    143   1.1       cgd 
    144   1.1       cgd 		case '-':
    145   1.1       cgd 			newmode &= ~set->bits;
    146   1.1       cgd 			break;
    147   1.1       cgd 
    148   1.1       cgd 		case 'X':
    149   1.1       cgd 			if (omode & (S_IFDIR|S_IXUSR|S_IXGRP|S_IXOTH))
    150   1.1       cgd 				newmode |= set->bits;
    151   1.1       cgd 			break;
    152   1.1       cgd 
    153   1.1       cgd 		case '\0':
    154   1.1       cgd 		default:
    155   1.1       cgd #ifdef SETMODE_DEBUG
    156   1.6       cgd 			(void)printf("getmode:%04o -> %04o\n", omode, newmode);
    157   1.1       cgd #endif
    158   1.6       cgd 			return (newmode);
    159   1.1       cgd 		}
    160   1.1       cgd }
    161   1.1       cgd 
    162  1.25     enami #define	ADDCMD(a, b, c, d) do {						\
    163   1.9       cgd 	if (set >= endset) {						\
    164  1.25     enami 		BITCMD *newset;						\
    165   1.9       cgd 		setlen += SET_LEN_INCR;					\
    166  1.36       nia 		newset = saveset;					\
    167  1.37  christos 		errno = reallocarr(&newset, setlen, sizeof(*newset));	\
    168  1.37  christos 		if (errno)						\
    169  1.31  christos 			goto out;					\
    170   1.9       cgd 		set = newset + (set - saveset);				\
    171   1.9       cgd 		saveset = newset;					\
    172   1.9       cgd 		endset = newset + (setlen - 2);				\
    173   1.9       cgd 	}								\
    174  1.31  christos 	set = addcmd(set, (mode_t)(a), (mode_t)(b), (mode_t)(c), (d));	\
    175  1.38    rillig } while (0)
    176   1.1       cgd 
    177   1.6       cgd #define	STANDARD_BITS	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
    178   1.6       cgd 
    179   1.1       cgd void *
    180  1.34       abs setmode(const char *p)
    181   1.1       cgd {
    182  1.31  christos 	int serrno;
    183  1.24   mycroft 	char op, *ep;
    184   1.6       cgd 	BITCMD *set, *saveset, *endset;
    185  1.29    kleink 	sigset_t signset, sigoset;
    186  1.31  christos 	mode_t mask, perm, permXbits, who;
    187  1.31  christos 	long lval;
    188  1.16  christos 	int equalopdone = 0;	/* pacify gcc */
    189  1.31  christos 	int setlen;
    190   1.6       cgd 
    191  1.31  christos 	if (!*p) {
    192  1.31  christos 		errno = EINVAL;
    193  1.31  christos 		return NULL;
    194  1.31  christos 	}
    195   1.1       cgd 
    196   1.1       cgd 	/*
    197   1.1       cgd 	 * Get a copy of the mask for the permissions that are mask relative.
    198   1.6       cgd 	 * Flip the bits, we want what's not set.  Since it's possible that
    199   1.6       cgd 	 * the caller is opening files inside a signal handler, protect them
    200   1.6       cgd 	 * as best we can.
    201   1.1       cgd 	 */
    202  1.29    kleink 	sigfillset(&signset);
    203  1.29    kleink 	(void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
    204   1.1       cgd 	(void)umask(mask = umask(0));
    205   1.1       cgd 	mask = ~mask;
    206  1.28     enami 	(void)sigprocmask(SIG_SETMASK, &sigoset, NULL);
    207   1.1       cgd 
    208   1.1       cgd 	setlen = SET_LEN + 2;
    209  1.35       nia 	set = NULL;
    210  1.37  christos 	errno = reallocarr(&set, setlen, sizeof(*set));
    211  1.37  christos 	if (errno)
    212  1.37  christos 		return NULL;
    213   1.1       cgd 	saveset = set;
    214   1.1       cgd 	endset = set + (setlen - 2);
    215   1.1       cgd 
    216   1.1       cgd 	/*
    217   1.1       cgd 	 * If an absolute number, get it and return; disallow non-octal digits
    218   1.1       cgd 	 * or illegal bits.
    219   1.1       cgd 	 */
    220  1.21  christos 	if (isdigit((unsigned char)*p)) {
    221  1.31  christos 		errno = 0;
    222  1.31  christos 		lval = strtol(p, &ep, 8);
    223  1.31  christos 		if (*ep) {
    224  1.31  christos 			errno = EINVAL;
    225  1.31  christos 			goto out;
    226  1.31  christos 		}
    227  1.31  christos 		if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
    228  1.31  christos 			goto out;
    229  1.31  christos 		if (lval & ~(STANDARD_BITS|S_ISTXT)) {
    230  1.31  christos 			errno = EINVAL;
    231  1.31  christos 			goto out;
    232   1.1       cgd 		}
    233  1.31  christos 		perm = (mode_t)lval;
    234   1.1       cgd 		ADDCMD('=', (STANDARD_BITS|S_ISTXT), perm, mask);
    235  1.18   mycroft 		set->cmd = 0;
    236   1.6       cgd 		return (saveset);
    237   1.1       cgd 	}
    238   1.1       cgd 
    239   1.1       cgd 	/*
    240   1.1       cgd 	 * Build list of structures to set/clear/copy bits as described by
    241   1.1       cgd 	 * each clause of the symbolic mode.
    242   1.1       cgd 	 */
    243   1.1       cgd 	for (;;) {
    244   1.1       cgd 		/* First, find out which bits might be modified. */
    245   1.1       cgd 		for (who = 0;; ++p) {
    246   1.1       cgd 			switch (*p) {
    247   1.1       cgd 			case 'a':
    248   1.1       cgd 				who |= STANDARD_BITS;
    249   1.1       cgd 				break;
    250   1.1       cgd 			case 'u':
    251   1.1       cgd 				who |= S_ISUID|S_IRWXU;
    252   1.1       cgd 				break;
    253   1.1       cgd 			case 'g':
    254   1.1       cgd 				who |= S_ISGID|S_IRWXG;
    255   1.1       cgd 				break;
    256   1.1       cgd 			case 'o':
    257   1.1       cgd 				who |= S_IRWXO;
    258   1.1       cgd 				break;
    259   1.1       cgd 			default:
    260   1.1       cgd 				goto getop;
    261   1.1       cgd 			}
    262   1.1       cgd 		}
    263   1.1       cgd 
    264   1.6       cgd getop:		if ((op = *p++) != '+' && op != '-' && op != '=') {
    265  1.31  christos 			errno = EINVAL;
    266  1.31  christos 			goto out;
    267   1.1       cgd 		}
    268   1.7       cgd 		if (op == '=')
    269   1.7       cgd 			equalopdone = 0;
    270   1.1       cgd 
    271   1.1       cgd 		who &= ~S_ISTXT;
    272   1.1       cgd 		for (perm = 0, permXbits = 0;; ++p) {
    273   1.1       cgd 			switch (*p) {
    274   1.1       cgd 			case 'r':
    275   1.1       cgd 				perm |= S_IRUSR|S_IRGRP|S_IROTH;
    276   1.1       cgd 				break;
    277   1.1       cgd 			case 's':
    278  1.15  christos 				/*
    279  1.15  christos 				 * If specific bits where requested and
    280  1.15  christos 				 * only "other" bits ignore set-id.
    281  1.15  christos 				 */
    282  1.15  christos 				if (who == 0 || (who & ~S_IRWXO))
    283   1.1       cgd 					perm |= S_ISUID|S_ISGID;
    284   1.1       cgd 				break;
    285   1.1       cgd 			case 't':
    286  1.15  christos 				/*
    287  1.15  christos 				 * If specific bits where requested and
    288  1.15  christos 				 * only "other" bits ignore set-id.
    289  1.15  christos 				 */
    290  1.15  christos 				if (who == 0 || (who & ~S_IRWXO)) {
    291   1.1       cgd 					who |= S_ISTXT;
    292   1.1       cgd 					perm |= S_ISTXT;
    293   1.1       cgd 				}
    294   1.1       cgd 				break;
    295   1.1       cgd 			case 'w':
    296   1.1       cgd 				perm |= S_IWUSR|S_IWGRP|S_IWOTH;
    297   1.1       cgd 				break;
    298   1.1       cgd 			case 'X':
    299   1.1       cgd 				permXbits = S_IXUSR|S_IXGRP|S_IXOTH;
    300   1.1       cgd 				break;
    301   1.1       cgd 			case 'x':
    302   1.1       cgd 				perm |= S_IXUSR|S_IXGRP|S_IXOTH;
    303   1.1       cgd 				break;
    304   1.1       cgd 			case 'u':
    305   1.1       cgd 			case 'g':
    306   1.1       cgd 			case 'o':
    307   1.1       cgd 				/*
    308   1.1       cgd 				 * When ever we hit 'u', 'g', or 'o', we have
    309   1.1       cgd 				 * to flush out any partial mode that we have,
    310   1.1       cgd 				 * and then do the copying of the mode bits.
    311   1.1       cgd 				 */
    312   1.8       cgd 				if (perm) {
    313   1.1       cgd 					ADDCMD(op, who, perm, mask);
    314   1.1       cgd 					perm = 0;
    315   1.1       cgd 				}
    316   1.8       cgd 				if (op == '=')
    317   1.8       cgd 					equalopdone = 1;
    318   1.1       cgd 				if (op == '+' && permXbits) {
    319   1.1       cgd 					ADDCMD('X', who, permXbits, mask);
    320   1.1       cgd 					permXbits = 0;
    321   1.1       cgd 				}
    322   1.1       cgd 				ADDCMD(*p, who, op, mask);
    323   1.1       cgd 				break;
    324   1.1       cgd 
    325   1.1       cgd 			default:
    326   1.1       cgd 				/*
    327   1.1       cgd 				 * Add any permissions that we haven't already
    328   1.1       cgd 				 * done.
    329   1.1       cgd 				 */
    330   1.7       cgd 				if (perm || (op == '=' && !equalopdone)) {
    331   1.8       cgd 					if (op == '=')
    332   1.8       cgd 						equalopdone = 1;
    333   1.1       cgd 					ADDCMD(op, who, perm, mask);
    334   1.1       cgd 					perm = 0;
    335   1.1       cgd 				}
    336   1.1       cgd 				if (permXbits) {
    337   1.1       cgd 					ADDCMD('X', who, permXbits, mask);
    338   1.1       cgd 					permXbits = 0;
    339   1.1       cgd 				}
    340   1.1       cgd 				goto apply;
    341   1.1       cgd 			}
    342   1.1       cgd 		}
    343   1.1       cgd 
    344   1.1       cgd apply:		if (!*p)
    345   1.1       cgd 			break;
    346   1.1       cgd 		if (*p != ',')
    347   1.1       cgd 			goto getop;
    348   1.1       cgd 		++p;
    349   1.1       cgd 	}
    350   1.1       cgd 	set->cmd = 0;
    351   1.1       cgd #ifdef SETMODE_DEBUG
    352   1.1       cgd 	(void)printf("Before compress_mode()\n");
    353   1.1       cgd 	dumpmode(saveset);
    354   1.1       cgd #endif
    355   1.1       cgd 	compress_mode(saveset);
    356   1.1       cgd #ifdef SETMODE_DEBUG
    357   1.1       cgd 	(void)printf("After compress_mode()\n");
    358   1.1       cgd 	dumpmode(saveset);
    359   1.1       cgd #endif
    360   1.6       cgd 	return (saveset);
    361  1.31  christos out:
    362  1.31  christos 	serrno = errno;
    363  1.31  christos 	free(saveset);
    364  1.31  christos 	errno = serrno;
    365  1.31  christos 	return NULL;
    366   1.6       cgd }
    367   1.6       cgd 
    368   1.6       cgd static BITCMD *
    369  1.33  christos addcmd(BITCMD *set, mode_t op, mode_t who, mode_t oparg, mode_t mask)
    370   1.6       cgd {
    371  1.22     lukem 
    372  1.22     lukem 	_DIAGASSERT(set != NULL);
    373  1.22     lukem 
    374   1.6       cgd 	switch (op) {
    375   1.9       cgd 	case '=':
    376   1.9       cgd 		set->cmd = '-';
    377   1.9       cgd 		set->bits = who ? who : STANDARD_BITS;
    378   1.9       cgd 		set++;
    379   1.9       cgd 
    380   1.9       cgd 		op = '+';
    381   1.9       cgd 		/* FALLTHROUGH */
    382   1.6       cgd 	case '+':
    383   1.9       cgd 	case '-':
    384   1.6       cgd 	case 'X':
    385   1.6       cgd 		set->cmd = op;
    386   1.6       cgd 		set->bits = (who ? who : mask) & oparg;
    387   1.6       cgd 		break;
    388   1.6       cgd 
    389   1.6       cgd 	case 'u':
    390   1.6       cgd 	case 'g':
    391   1.6       cgd 	case 'o':
    392   1.6       cgd 		set->cmd = op;
    393   1.6       cgd 		if (who) {
    394   1.6       cgd 			set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) |
    395   1.6       cgd 				    ((who & S_IRGRP) ? CMD2_GBITS : 0) |
    396   1.6       cgd 				    ((who & S_IROTH) ? CMD2_OBITS : 0);
    397  1.20     perry 			set->bits = (mode_t)~0;
    398   1.6       cgd 		} else {
    399   1.6       cgd 			set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS;
    400   1.9       cgd 			set->bits = mask;
    401   1.6       cgd 		}
    402   1.6       cgd 
    403   1.6       cgd 		if (oparg == '+')
    404   1.6       cgd 			set->cmd2 |= CMD2_SET;
    405   1.6       cgd 		else if (oparg == '-')
    406   1.6       cgd 			set->cmd2 |= CMD2_CLR;
    407   1.6       cgd 		else if (oparg == '=')
    408   1.6       cgd 			set->cmd2 |= CMD2_SET|CMD2_CLR;
    409   1.6       cgd 		break;
    410   1.6       cgd 	}
    411   1.6       cgd 	return (set + 1);
    412   1.1       cgd }
    413   1.1       cgd 
    414   1.1       cgd #ifdef SETMODE_DEBUG
    415   1.6       cgd static void
    416  1.32      matt dumpmode(BITCMD *set)
    417   1.1       cgd {
    418  1.22     lukem 
    419  1.22     lukem 	_DIAGASSERT(set != NULL);
    420  1.22     lukem 
    421   1.1       cgd 	for (; set->cmd; ++set)
    422   1.1       cgd 		(void)printf("cmd: '%c' bits %04o%s%s%s%s%s%s\n",
    423   1.1       cgd 		    set->cmd, set->bits, set->cmd2 ? " cmd2:" : "",
    424   1.1       cgd 		    set->cmd2 & CMD2_CLR ? " CLR" : "",
    425   1.1       cgd 		    set->cmd2 & CMD2_SET ? " SET" : "",
    426   1.1       cgd 		    set->cmd2 & CMD2_UBITS ? " UBITS" : "",
    427   1.1       cgd 		    set->cmd2 & CMD2_GBITS ? " GBITS" : "",
    428   1.1       cgd 		    set->cmd2 & CMD2_OBITS ? " OBITS" : "");
    429   1.1       cgd }
    430   1.1       cgd #endif
    431   1.1       cgd 
    432   1.1       cgd /*
    433   1.1       cgd  * Given an array of bitcmd structures, compress by compacting consecutive
    434   1.1       cgd  * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
    435   1.1       cgd  * 'g' and 'o' commands continue to be separate.  They could probably be
    436   1.1       cgd  * compacted, but it's not worth the effort.
    437   1.1       cgd  */
    438  1.13       jtc static void
    439  1.32      matt compress_mode(BITCMD *set)
    440   1.1       cgd {
    441  1.19     perry 	BITCMD *nset;
    442  1.19     perry 	int setbits, clrbits, Xbits, op;
    443  1.22     lukem 
    444  1.22     lukem 	_DIAGASSERT(set != NULL);
    445   1.1       cgd 
    446   1.1       cgd 	for (nset = set;;) {
    447   1.1       cgd 		/* Copy over any 'u', 'g' and 'o' commands. */
    448   1.1       cgd 		while ((op = nset->cmd) != '+' && op != '-' && op != 'X') {
    449   1.1       cgd 			*set++ = *nset++;
    450   1.1       cgd 			if (!op)
    451   1.1       cgd 				return;
    452   1.1       cgd 		}
    453   1.1       cgd 
    454   1.1       cgd 		for (setbits = clrbits = Xbits = 0;; nset++) {
    455   1.1       cgd 			if ((op = nset->cmd) == '-') {
    456   1.1       cgd 				clrbits |= nset->bits;
    457   1.1       cgd 				setbits &= ~nset->bits;
    458   1.1       cgd 				Xbits &= ~nset->bits;
    459   1.1       cgd 			} else if (op == '+') {
    460   1.1       cgd 				setbits |= nset->bits;
    461   1.1       cgd 				clrbits &= ~nset->bits;
    462   1.1       cgd 				Xbits &= ~nset->bits;
    463   1.1       cgd 			} else if (op == 'X')
    464   1.1       cgd 				Xbits |= nset->bits & ~setbits;
    465   1.1       cgd 			else
    466   1.1       cgd 				break;
    467   1.1       cgd 		}
    468   1.1       cgd 		if (clrbits) {
    469   1.1       cgd 			set->cmd = '-';
    470   1.1       cgd 			set->cmd2 = 0;
    471   1.1       cgd 			set->bits = clrbits;
    472   1.1       cgd 			set++;
    473   1.1       cgd 		}
    474   1.1       cgd 		if (setbits) {
    475   1.1       cgd 			set->cmd = '+';
    476   1.1       cgd 			set->cmd2 = 0;
    477   1.1       cgd 			set->bits = setbits;
    478   1.1       cgd 			set++;
    479   1.1       cgd 		}
    480   1.1       cgd 		if (Xbits) {
    481   1.1       cgd 			set->cmd = 'X';
    482   1.1       cgd 			set->cmd2 = 0;
    483   1.1       cgd 			set->bits = Xbits;
    484   1.1       cgd 			set++;
    485   1.1       cgd 		}
    486   1.1       cgd 	}
    487   1.1       cgd }
    488