zsh.h 90.5 KB
Newer Older
Tanaka Akira's avatar
Tanaka Akira committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * zsh.h - standard header file
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1992-1997 Paul Falstad
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Paul Falstad or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Paul Falstad and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Paul Falstad and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Paul Falstad and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

/* A few typical macros */
#define minimum(a,b)  ((a) < (b) ? (a) : (b))

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * Our longest integer type:  will be a 64 bit either if long already is,
 * or if we found some alternative such as long long.
 */
#ifdef ZSH_64_BIT_TYPE
typedef ZSH_64_BIT_TYPE zlong;
#ifdef ZSH_64_BIT_UTYPE
typedef ZSH_64_BIT_UTYPE zulong;
#else
typedef unsigned zlong zulong;
#endif
#else
typedef long zlong;
typedef unsigned long zulong;
#endif

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*
 * Work out how to define large integer constants that will fit
 * in a zlong.
 */
#if defined(ZSH_64_BIT_TYPE) || defined(LONG_IS_64_BIT)
/* We have some 64-bit type */
#ifdef LONG_IS_64_BIT
/* It's long */
#define ZLONG_CONST(x)  x ## l
#else
/* It's long long */
#ifdef ZLONG_IS_LONG_LONG
#define ZLONG_CONST(x)  x ## ll
#else
/*
 * There's some 64-bit type, but we don't know what it is.
 * We'll just cast it and hope the compiler does the right thing.
 */
#define ZLONG_CONST(x) ((zlong)x)
#endif
#endif
#else
/* We're stuck with long */
#define ZLONG_CONST(x) (x ## l)
#endif

75 76 77 78 79 80 81 82
/*
 * Double float support requires 64-bit alignment, so if longs and
 * pointers are less we need to pad out.
 */
#ifndef LONG_IS_64_BIT
# define PAD_64_BIT 1
#endif

Tanaka Akira's avatar
Tanaka Akira committed
83
/* math.c */
84 85 86 87 88 89 90 91 92 93
typedef struct {
    union {
	zlong l;
	double d;
    } u;
    int type;
} mnumber;

#define MN_INTEGER 1		/* mnumber is integer */
#define MN_FLOAT   2		/* mnumber is floating point */
94
#define MN_UNSET   4		/* mnumber not yet retrieved */
95 96 97 98 99 100 101 102

typedef struct mathfunc *MathFunc;
typedef mnumber (*NumMathFunc)(char *, int, mnumber *, int);
typedef mnumber (*StrMathFunc)(char *, char *, int);

struct mathfunc {
    MathFunc next;
    char *name;
103
    int flags;			/* MFF_* flags defined below */
104 105 106 107 108 109 110 111
    NumMathFunc nfunc;
    StrMathFunc sfunc;
    char *module;
    int minargs;
    int maxargs;
    int funcid;
};

112
/* Math function takes a string argument */
113
#define MFF_STR      1
114
/* Math function has been loaded from library */
115
#define MFF_ADDED    2
116 117
/* Math function is implemented by a shell function */
#define MFF_USERFUNC 4
118 119
/* When autoloading, enable all features in module */
#define MFF_AUTOALL  8
120

121

122 123 124 125
#define NUMMATHFUNC(name, func, min, max, id) \
    { NULL, name, 0, func, NULL, NULL, min, max, id }
#define STRMATHFUNC(name, func, id) \
    { NULL, name, MFF_STR, NULL, func, NULL, 0, 0, id }
Tanaka Akira's avatar
Tanaka Akira committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

/* Character tokens are sometimes casted to (unsigned char)'s.         * 
 * Unfortunately, some compilers don't correctly cast signed to        * 
 * unsigned promotions; i.e. (int)(unsigned char)((char) -1) evaluates * 
 * to -1, instead of 255 like it should.  We circumvent the troubles   * 
 * of such shameful delinquency by casting to a larger unsigned type   * 
 * then back down to unsigned char.                                    */

#ifdef BROKEN_SIGNED_TO_UNSIGNED_CASTING
# define STOUC(X)	((unsigned char)(unsigned short)(X))
#else
# define STOUC(X)	((unsigned char)(X))
#endif

/* Meta together with the character following Meta denotes the character *
 * which is the exclusive or of 32 and the character following Meta.     *
 * This is used to represent characters which otherwise has special      *
 * meaning for zsh.  These are the characters for which the imeta() test *
 * is true: the null character, and the characters from Meta to Marker.  */

#define Meta		((char) 0x83)

/* Note that the fourth character in DEFAULT_IFS is Meta *
 * followed by a space which denotes the null character. */

#define DEFAULT_IFS	" \t\n\203 "

153 154 155 156
/* As specified in the standard (POSIX 2008) */

#define DEFAULT_IFS_SH	" \t\n"

157 158 159 160
/*
 * Character tokens.
 * These should match the characters in ztokens, defined in lex.c
 */
Tanaka Akira's avatar
Tanaka Akira committed
161 162 163 164 165
#define Pound		((char) 0x84)
#define String		((char) 0x85)
#define Hat		((char) 0x86)
#define Star		((char) 0x87)
#define Inpar		((char) 0x88)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
#define Inparmath	((char) 0x89)
#define Outpar		((char) 0x8a)
#define Outparmath	((char) 0x8b)
#define Qstring	        ((char) 0x8c)
#define Equals		((char) 0x8d)
#define Bar	      	((char) 0x8e)
#define Inbrace	        ((char) 0x8f)
#define Outbrace	((char) 0x90)
#define Inbrack	        ((char) 0x91)
#define Outbrack	((char) 0x92)
#define Tick		((char) 0x93)
#define Inang		((char) 0x94)
#define Outang		((char) 0x95)
#define OutangProc	((char) 0x96)
#define Quest		((char) 0x97)
#define Tilde		((char) 0x98)
#define Qtick		((char) 0x99)
#define Comma		((char) 0x9a)
184 185 186 187
/*
 * Null arguments: placeholders for single and double quotes
 * and backslashes.
 */
188 189 190
#define Snull		((char) 0x9b)
#define Dnull		((char) 0x9c)
#define Bnull		((char) 0x9d)
191 192 193 194
/*
 * Backslash which will be returned to "\" instead of being stripped
 * when we turn the string into a printable format.
 */
195
#define Bnullkeep       ((char) 0x9e)
196 197 198 199 200
/*
 * Null argument that does not correspond to any character.
 * This should be last as it does not appear in ztokens and
 * is used to initialise the IMETA type in inittyptab().
 */
201
#define Nularg		((char) 0x9f)
Tanaka Akira's avatar
Tanaka Akira committed
202

203 204 205 206
/*
 * Take care to update the use of IMETA appropriately when adding
 * tokens here.
 */
207 208 209 210 211
/*
 * Marker used in paramsubst for rc_expand_param.
 * Also used in pattern character arrays as guaranteed not to
 * mark a character in a string.
 */
212
#define Marker		((char) 0xa0)
Tanaka Akira's avatar
Tanaka Akira committed
213 214 215 216 217

/* chars that need to be quoted if meant literally */

#define SPECCHARS "#$^*()=|{}[]`<>?~;&\n\t \\\'\""

218 219
/* chars that need to be quoted for pattern matching */

220
#define PATCHARS "#^*()|[]<>?~\\"
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
/*
 * Types of quote.  This is used in various places, so care needs
 * to be taken when changing them.  (Oooh, don't you look surprised.)
 * - Passed to quotestring() to indicate style.  This is the ultimate
 *   destiny of most of the other uses of members of the enum.
 * - In paramsubst(), to count q's in parameter substitution.
 * - In the completion code, where we maintain a stack of quotation types.
 */
enum {
    /*
     * No quote.  Not a valid quote, but useful in the substitution
     * and completion code to indicate we're not doing any quoting.
     */
    QT_NONE,
    /* Backslash: \ */
    QT_BACKSLASH,
    /* Single quote: ' */
    QT_SINGLE,
    /* Double quote: " */
    QT_DOUBLE,
    /* Print-style quote: $' */
    QT_DOLLARS,
    /*
     * Backtick: `
     * Not understood by many parts of the code; here for a convenience
     * in those cases where we need to represent a complete set.
     */
    QT_BACKTICK,
250 251 252 253
    /*
     * Single quotes, but the default is not to quote unless necessary.
     * This is only useful as an argument to quotestring().
     */
254
    QT_SINGLE_OPTIONAL,
255 256 257 258 259 260
    /*
     * Only quote pattern characters.
     * ${(b)foo} guarantees that ${~foo} matches the string
     * contained in foo.
     */
    QT_BACKSLASH_PATTERN,
261 262 263 264
    /*
     * As QT_BACKSLASH, but a NULL string is shown as ''.
     */
    QT_BACKSLASH_SHOWNULL
265 266
};

267 268
#define QT_IS_SINGLE(x)	((x) == QT_SINGLE || (x) == QT_SINGLE_OPTIONAL)

269 270 271 272 273
/*
 * Lexical tokens: unlike the character tokens above, these never
 * appear in strings and don't necessarily represent a single character.
 */

274
enum lextok {
Tanaka Akira's avatar
Tanaka Akira committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    NULLTOK,		/* 0  */
    SEPER,
    NEWLIN,
    SEMI,
    DSEMI,
    AMPER,		/* 5  */
    INPAR,
    OUTPAR,
    DBAR,
    DAMPER,
    OUTANG,		/* 10 */
    OUTANGBANG,
    DOUTANG,
    DOUTANGBANG,
    INANG,
    INOUTANG,		/* 15 */
    DINANG,
    DINANGDASH,
    INANGAMP,
    OUTANGAMP,
    AMPOUTANG,		/* 20 */
    OUTANGAMPBANG,
    DOUTANGAMP,
    DOUTANGAMPBANG,
    TRINANG,
    BAR,		/* 25 */
    BARAMP,
    INOUTPAR,
    DINPAR,
    DOUTPAR,
    AMPERBANG,		/* 30 */
    SEMIAMP,
307
    SEMIBAR,
Tanaka Akira's avatar
Tanaka Akira committed
308 309
    DOUTBRACK,
    STRING,
310 311
    ENVSTRING,		/* 35 */
    ENVARRAY,
Tanaka Akira's avatar
Tanaka Akira committed
312 313 314 315 316
    ENDINPUT,
    LEXERR,

    /* Tokens for reserved words */
    BANG,	/* !         */
317 318
    DINBRACK,	/* [[        */	/* 40 */
    INBRACE,    /* {         */
Tanaka Akira's avatar
Tanaka Akira committed
319 320 321
    OUTBRACE,   /* }         */
    CASE,	/* case      */
    COPROC,	/* coproc    */
322 323
    DOLOOP,	/* do        */ /* 45 */
    DONE,	/* done      */
Tanaka Akira's avatar
Tanaka Akira committed
324 325 326
    ELIF,	/* elif      */
    ELSE,	/* else      */
    ZEND,	/* end       */
327 328
    ESAC,	/* esac      */ /* 50 */
    FI,		/* fi        */
Tanaka Akira's avatar
Tanaka Akira committed
329 330 331
    FOR,	/* for       */
    FOREACH,	/* foreach   */
    FUNC,	/* function  */
332 333
    IF,		/* if        */ /* 55 */
    NOCORRECT,	/* nocorrect */
Tanaka Akira's avatar
Tanaka Akira committed
334 335 336
    REPEAT,	/* repeat    */
    SELECT,	/* select    */
    THEN,	/* then      */
337 338
    TIME,	/* time      */ /* 60 */
    UNTIL,	/* until     */
339 340
    WHILE,	/* while     */
    TYPESET     /* typeset or similar */
Tanaka Akira's avatar
Tanaka Akira committed
341 342 343 344 345 346 347
};

/* Redirection types.  If you modify this, you may also have to modify *
 * redirtab in parse.c and getredirs() in text.c and the IS_* macros   *
 * below.                                                              */

enum {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    REDIR_WRITE,		/* > */
    REDIR_WRITENOW,		/* >| */
    REDIR_APP,		/* >> */
    REDIR_APPNOW,		/* >>| */
    REDIR_ERRWRITE,		/* &>, >& */
    REDIR_ERRWRITENOW,	/* >&| */
    REDIR_ERRAPP,		/* >>& */
    REDIR_ERRAPPNOW,		/* >>&| */
    REDIR_READWRITE,		/* <> */
    REDIR_READ,		/* < */
    REDIR_HEREDOC,		/* << */
    REDIR_HEREDOCDASH,	/* <<- */
    REDIR_HERESTR,		/* <<< */
    REDIR_MERGEIN,		/* <&n */
    REDIR_MERGEOUT,		/* >&n */
    REDIR_CLOSE,		/* >&-, <&- */
    REDIR_INPIPE,		/* < <(...) */
    REDIR_OUTPIPE		/* > >(...) */
Tanaka Akira's avatar
Tanaka Akira committed
366
};
367
#define REDIR_TYPE_MASK	(0x1f)
368
/* Redir using {var} syntax */
369
#define REDIR_VARID_MASK (0x20)
370 371
/* Mark here-string that came from a here-document */
#define REDIR_FROM_HEREDOC_MASK (0x40)
Tanaka Akira's avatar
Tanaka Akira committed
372

373
#define IS_WRITE_FILE(X)      ((X)>=REDIR_WRITE && (X)<=REDIR_READWRITE)
Tanaka Akira's avatar
Tanaka Akira committed
374 375
#define IS_APPEND_REDIR(X)    (IS_WRITE_FILE(X) && ((X) & 2))
#define IS_CLOBBER_REDIR(X)   (IS_WRITE_FILE(X) && ((X) & 1))
376 377
#define IS_ERROR_REDIR(X)     ((X)>=REDIR_ERRWRITE && (X)<=REDIR_ERRAPPNOW)
#define IS_READFD(X)          (((X)>=REDIR_READWRITE && (X)<=REDIR_MERGEIN) || (X)==REDIR_INPIPE)
Tanaka Akira's avatar
Tanaka Akira committed
378 379
#define IS_REDIROP(X)         ((X)>=OUTANG && (X)<=TRINANG)

380 381 382 383 384 385 386 387 388 389 390 391
/*
 * Values for the fdtable array.  They say under what circumstances
 * the fd will be close.  The fdtable is an unsigned char, so these are
 * #define's rather than an enum.
 */
/* Entry not used. */
#define FDT_UNUSED		0
/*
 * Entry used internally by the shell, should not be visible to other
 * processes.
 */
#define FDT_INTERNAL		1
392 393 394 395 396
/*
 * Entry visible to other processes, for example created using
 * the {varid}> file syntax.
 */
#define FDT_EXTERNAL		2
397 398 399
/*
 * Entry used by output from the XTRACE option.
 */
400
#define FDT_XTRACE		3
401 402 403 404 405 406 407 408 409
/*
 * Entry used for file locking.
 */
#define FDT_FLOCK		4
/*
 * As above, but the fd is not marked for closing on exec,
 * so the shell can still exec the last process.
 */
#define FDT_FLOCK_EXEC		5
410 411 412
#ifdef PATH_DEV_FD
/*
 * Entry used by a process substition.
413 414
 * This marker is not tested internally as we associated the file
 * descriptor with a job for closing.
415
 */
416
#define FDT_PROC_SUBST		6
417 418
#endif

Tanaka Akira's avatar
Tanaka Akira committed
419 420 421 422 423 424
/* Flags for input stack */
#define INP_FREE      (1<<0)	/* current buffer can be free'd            */
#define INP_ALIAS     (1<<1)	/* expanding alias or history              */
#define INP_HIST      (1<<2)	/* expanding history                       */
#define INP_CONT      (1<<3)	/* continue onto previously stacked input  */
#define INP_ALCONT    (1<<4)	/* stack is continued from alias expn.     */
425 426 427
#define INP_HISTCONT  (1<<5)	/* stack is continued from history expn.   */
#define INP_LINENO    (1<<6)    /* update line number                      */
#define INP_APPEND    (1<<7)    /* Append new lines to allow backup        */
428
#define INP_RAW_KEEP  (1<<8)    /* Input needed in raw mode even if alias  */
Tanaka Akira's avatar
Tanaka Akira committed
429 430 431 432 433 434 435 436 437 438 439

/* Flags for metafy */
#define META_REALLOC	0
#define META_USEHEAP	1
#define META_STATIC	2
#define META_DUP	3
#define META_ALLOC	4
#define META_NOALLOC	5
#define META_HEAPDUP	6
#define META_HREALLOC	7

440 441 442 443 444 445 446 447 448
/* Context to save and restore (bit fields) */
enum {
    /* History mechanism */
    ZCONTEXT_HIST       = (1<<0),
    /* Lexical analyser */
    ZCONTEXT_LEX        = (1<<1),
    /* Parser */
    ZCONTEXT_PARSE      = (1<<2)
};
Tanaka Akira's avatar
Tanaka Akira committed
449 450 451 452 453 454

/**************************/
/* Abstract types for zsh */
/**************************/

typedef struct alias     *Alias;
455 456
typedef struct asgment   *Asgment;
typedef struct builtin   *Builtin;
Tanaka Akira's avatar
Tanaka Akira committed
457
typedef struct cmdnam    *Cmdnam;
458 459
typedef struct complist  *Complist;
typedef struct conddef   *Conddef;
460
typedef struct dirsav    *Dirsav;
461
typedef struct emulation_options *Emulation_options;
462
typedef struct features  *Features;
Peter Stephenson's avatar
Peter Stephenson committed
463
typedef struct feature_enables  *Feature_enables;
464 465
typedef struct funcstack *Funcstack;
typedef struct funcwrap  *FuncWrap;
466 467
typedef struct hashnode  *HashNode;
typedef struct hashtable *HashTable;
Tanaka Akira's avatar
Tanaka Akira committed
468 469 470
typedef struct heap      *Heap;
typedef struct heapstack *Heapstack;
typedef struct histent   *Histent;
471
typedef struct hookdef   *Hookdef;
472
typedef struct jobfile   *Jobfile;
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
typedef struct job       *Job;
typedef struct linkedmod *Linkedmod;
typedef struct linknode  *LinkNode;
typedef union  linkroot  *LinkList;
typedef struct module    *Module;
typedef struct nameddir  *Nameddir;
typedef struct options	 *Options;
typedef struct optname   *Optname;
typedef struct param     *Param;
typedef struct paramdef  *Paramdef;
typedef struct patprog   *Patprog;
typedef struct prepromptfn *Prepromptfn;
typedef struct process   *Process;
typedef struct redir     *Redir;
typedef struct reswd     *Reswd;
typedef struct shfunc    *Shfunc;
typedef struct timedfn   *Timedfn;
typedef struct value     *Value;
Tanaka Akira's avatar
Tanaka Akira committed
491 492 493 494 495 496 497 498 499

/********************************/
/* Definitions for linked lists */
/********************************/

/* linked list abstract data type */

struct linknode {
    LinkNode next;
500
    LinkNode prev;
Tanaka Akira's avatar
Tanaka Akira committed
501 502 503 504 505 506
    void *dat;
};

struct linklist {
    LinkNode first;
    LinkNode last;
507 508 509 510 511 512
    int flags;
};

union linkroot {
    struct linklist list;
    struct linknode node;
Tanaka Akira's avatar
Tanaka Akira committed
513 514 515 516
};

/* Macros for manipulating link lists */

517 518 519
#define firstnode(X)        ((X)->list.first)
#define lastnode(X)         ((X)->list.last)
#define peekfirst(X)        (firstnode(X)->dat)
520
#define peeklast(X)         (lastnode(X)->dat)
521 522 523 524 525
#define addlinknode(X,Y)    insertlinknode(X,lastnode(X),Y)
#define zaddlinknode(X,Y)   zinsertlinknode(X,lastnode(X),Y)
#define uaddlinknode(X,Y)   uinsertlinknode(X,lastnode(X),Y)
#define empty(X)            (firstnode(X) == NULL)
#define nonempty(X)         (firstnode(X) != NULL)
526 527 528 529
#define getaddrdata(X)      (&((X)->dat))
#define getdata(X)          ((X)->dat)
#define setdata(X,Y)        ((X)->dat = (Y))
#define nextnode(X)         ((X)->next)
530
#define prevnode(X)         ((X)->prev)
531 532
#define pushnode(X,Y)       insertlinknode(X,&(X)->node,Y)
#define zpushnode(X,Y)      zinsertlinknode(X,&(X)->node,Y)
533
#define incnode(X)          (X = nextnode(X))
534
#define decnode(X)          (X = prevnode(X))
535
#define firsthist()         (hist_ring? hist_ring->down->histnum : curhist)
536
#define setsizednode(X,Y,Z) (firstnode(X)[(Y)].dat = (void *) (Z))
537 538 539

/* stack allocated linked lists */

540
#define local_list0(N) union linkroot N
541 542
#define init_list0(N) \
    do { \
543 544
        (N).list.first = NULL; \
        (N).list.last = &(N).node; \
545
        (N).list.flags = 0; \
546
    } while (0)
547
#define local_list1(N) union linkroot N; struct linknode __n0
548 549
#define init_list1(N,V0) \
    do { \
550 551
        (N).list.first = &__n0; \
        (N).list.last = &__n0; \
552
        (N).list.flags = 0; \
553
        __n0.next = NULL; \
554
        __n0.prev = &(N).node; \
555 556
        __n0.dat = (void *) (V0); \
    } while (0)
Tanaka Akira's avatar
Tanaka Akira committed
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
/*************************************/
/* Specific elements of linked lists */
/*************************************/

typedef void (*voidvoidfnptr_t) _((void));

/*
 * Element of the prepromptfns list.
 */
struct prepromptfn {
    voidvoidfnptr_t func;
};


/*
 * Element of the timedfns list.
 */
struct timedfn {
    voidvoidfnptr_t func;
    time_t when;
};

Tanaka Akira's avatar
Tanaka Akira committed
580 581 582 583 584 585
/********************************/
/* Definitions for syntax trees */
/********************************/

/* These are control flags that are passed *
 * down the execution pipeline.            */
586 587 588
#define Z_TIMED	 (1<<0)	/* pipeline is being timed                   */
#define Z_SYNC	 (1<<1)	/* run this sublist synchronously       (;)  */
#define Z_ASYNC  (1<<2)	/* run this sublist asynchronously      (&)  */
Tanaka Akira's avatar
Tanaka Akira committed
589
#define Z_DISOWN (1<<3)	/* run this sublist without job control (&|) */
590 591
/* (1<<4) is used for Z_END, see the wordcode definitions */
/* (1<<5) is used for Z_SIMPLE, see the wordcode definitions */
Tanaka Akira's avatar
Tanaka Akira committed
592

593
/* Condition types. */
Tanaka Akira's avatar
Tanaka Akira committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

#define COND_NOT    0
#define COND_AND    1
#define COND_OR     2
#define COND_STREQ  3
#define COND_STRNEQ 4
#define COND_STRLT  5
#define COND_STRGTR 6
#define COND_NT     7
#define COND_OT     8
#define COND_EF     9
#define COND_EQ    10
#define COND_NE    11
#define COND_LT    12
#define COND_GT    13
#define COND_LE    14
#define COND_GE    15
611 612 613
#define COND_REGEX 16
#define COND_MOD   17
#define COND_MODI  18
614 615 616 617 618 619 620 621 622 623 624 625

typedef int (*CondHandler) _((char **, int));

struct conddef {
    Conddef next;		/* next in list                       */
    char *name;			/* the condition name                 */
    int flags;			/* see CONDF_* below                  */
    CondHandler handler;	/* handler function                   */
    int min;			/* minimum number of strings          */
    int max;			/* maximum number of strings          */
    int condid;			/* for overloading handler functions  */
    char *module;		/* module to autoload                 */
Tanaka Akira's avatar
Tanaka Akira committed
626 627
};

628 629 630 631 632 633
/* Condition is an infix */
#define CONDF_INFIX   1
/* Condition has been loaded from library */
#define CONDF_ADDED   2
/* When autoloading, enable all features in library */
#define CONDF_AUTOALL 4
Tanaka Akira's avatar
Tanaka Akira committed
634

635 636
#define CONDDEF(name, flags, handler, min, max, condid) \
    { NULL, name, flags, handler, min, max, condid, NULL }
Tanaka Akira's avatar
Tanaka Akira committed
637

638 639 640 641 642 643 644
/* Flags for redirections */

enum {
    /* Mark a here-string that came from a here-document */
    REDIRF_FROM_HEREDOC = 1
};

645
/* tree element for redirection lists */
Tanaka Akira's avatar
Tanaka Akira committed
646

647 648
struct redir {
    int type;
649
    int flags;
650 651
    int fd1, fd2;
    char *name;
652
    char *varid;
653 654
    char *here_terminator;
    char *munged_here_terminator;
Tanaka Akira's avatar
Tanaka Akira committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
};

/* The number of fds space is allocated for  *
 * each time a multio must increase in size. */
#define MULTIOUNIT 8

/* A multio is a list of fds associated with a certain fd.       *
 * Thus if you do "foo >bar >ble", the multio for fd 1 will have *
 * two fds, the result of open("bar",...), and the result of     *
 * open("ble",....).                                             */

/* structure used for multiple i/o redirection */
/* one for each fd open                        */

struct multio {
    int ct;			/* # of redirections on this fd                 */
    int rflag;			/* 0 if open for reading, 1 if open for writing */
    int pipe;			/* fd of pipe if ct > 1                         */
    int fds[MULTIOUNIT];	/* list of src/dests redirected to/from this fd */
};

/* lvalue for variable assignment/expansion */

struct value {
    int isarr;
    Param pm;		/* parameter node                      */
681
    int flags;		/* flags defined below                 */
682
    int start;		/* first element of array slice, or -1 */
683
    int end;		/* 1-rel last element of array slice, or -1 */
684
    char **arr;		/* cache for hash turned into array */
Tanaka Akira's avatar
Tanaka Akira committed
685 686
};

687 688
enum {
    VALFLAG_INV =	0x0001,	/* We are performing inverse subscripting */
689 690
    VALFLAG_EMPTY =	0x0002,	/* Subscripted range is empty */
    VALFLAG_SUBST =     0x0004  /* Substitution, so apply padding, case flags */
691 692
};

693
#define MAX_ARRLEN    262144
Tanaka Akira's avatar
Tanaka Akira committed
694

695
/********************************************/
696
/* Definitions for word code                 */
697 698 699 700 701 702 703 704 705 706
/********************************************/

typedef unsigned int wordcode;
typedef wordcode *Wordcode;

typedef struct funcdump *FuncDump;
typedef struct eprog *Eprog;

struct funcdump {
    FuncDump next;		/* next in list */
707 708
    dev_t dev;			/* device */
    ino_t ino;			/* indoe number */
709 710 711 712 713
    int fd;			/* file descriptor */
    Wordcode map;		/* pointer to header */
    Wordcode addr;		/* mapped region */
    int len;			/* length */
    int count;			/* reference count */
714
    char *filename;
Tanaka Akira's avatar
Tanaka Akira committed
715 716
};

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
/*
 * A note on the use of reference counts in Eprogs.
 *
 * When an Eprog is created, nref is set to -1 if the Eprog is on the
 * heap; then no attempt is ever made to free it.  (This information is
 * already present in EF_HEAP; we use the redundancy for debugging
 * checks.)
 *
 * Otherwise, nref is initialised to 1.  Calling freeprog() decrements
 * nref and frees the Eprog if the count is now zero.  When the Eprog
 * is in use, we call useeprog() at the start and freeprog() at the
 * end to increment and decrement the reference counts.  If an attempt
 * is made to free the Eprog from within, this will then take place
 * when execution is finished, typically in the call to freeeprog()
 * in execode().  If the Eprog was on the heap, neither useeprog()
 * nor freeeprog() has any effect.
 */
734 735 736 737
struct eprog {
    int flags;			/* EF_* below */
    int len;			/* total block length */
    int npats;			/* Patprog cache size */
738
    int nref;			/* number of references: delete when zero */
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
    Patprog *pats;		/* the memory block, the patterns */
    Wordcode prog;		/* memory block ctd, the code */
    char *strs;			/* memory block ctd, the strings */
    Shfunc shf;			/* shell function for autoload */
    FuncDump dump;		/* dump file this is in */
};

#define EF_REAL 1
#define EF_HEAP 2
#define EF_MAP  4
#define EF_RUN  8

typedef struct estate *Estate;

struct estate {
    Eprog prog;			/* the eprog executed */
    Wordcode pc;		/* program counter, current pos */
    char *strs;			/* strings from prog */
};

typedef struct eccstr *Eccstr;

struct eccstr {
Sven Wischnowsky's avatar
Sven Wischnowsky committed
762
    Eccstr left, right;
763
    char *str;
Sven Wischnowsky's avatar
Sven Wischnowsky committed
764
    wordcode offs, aoffs;
765 766
    int nfunc;
};
Tanaka Akira's avatar
Tanaka Akira committed
767

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
#define EC_NODUP  0
#define EC_DUP    1
#define EC_DUPTOK 2

#define WC_CODEBITS 5

#define wc_code(C)   ((C) & ((wordcode) ((1 << WC_CODEBITS) - 1)))
#define wc_data(C)   ((C) >> WC_CODEBITS)
#define wc_bdata(D)  ((D) << WC_CODEBITS)
#define wc_bld(C,D)  (((wordcode) (C)) | (((wordcode) (D)) << WC_CODEBITS))

#define WC_END      0
#define WC_LIST     1
#define WC_SUBLIST  2
#define WC_PIPE     3
#define WC_REDIR    4
#define WC_ASSIGN   5
#define WC_SIMPLE   6
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
#define WC_TYPESET  7
#define WC_SUBSH    8
#define WC_CURSH    9
#define WC_TIMED   10
#define WC_FUNCDEF 11
#define WC_FOR     12
#define WC_SELECT  13
#define WC_WHILE   14
#define WC_REPEAT  15
#define WC_CASE    16
#define WC_IF      17
#define WC_COND    18
#define WC_ARITH   19
#define WC_AUTOFN  20
#define WC_TRY     21
801 802

/* increment as necessary */
803
#define WC_COUNT   22
804 805 806 807 808 809

#define WCB_END()           wc_bld(WC_END, 0)

#define WC_LIST_TYPE(C)     wc_data(C)
#define Z_END               (1<<4) 
#define Z_SIMPLE            (1<<5)
810 811 812
#define WC_LIST_FREE        (6)	/* Next bit available in integer */
#define WC_LIST_SKIP(C)     (wc_data(C) >> WC_LIST_FREE)
#define WCB_LIST(T,O)       wc_bld(WC_LIST, ((T) | ((O) << WC_LIST_FREE)))
813 814 815 816 817 818 819 820 821

#define WC_SUBLIST_TYPE(C)  (wc_data(C) & ((wordcode) 3))
#define WC_SUBLIST_END      0
#define WC_SUBLIST_AND      1
#define WC_SUBLIST_OR       2
#define WC_SUBLIST_FLAGS(C) (wc_data(C) & ((wordcode) 0x1c))
#define WC_SUBLIST_COPROC   4
#define WC_SUBLIST_NOT      8
#define WC_SUBLIST_SIMPLE  16
822 823 824 825
#define WC_SUBLIST_FREE    (5)	/* Next bit available in integer */
#define WC_SUBLIST_SKIP(C)  (wc_data(C) >> WC_SUBLIST_FREE)
#define WCB_SUBLIST(T,F,O)  wc_bld(WC_SUBLIST, \
				   ((T) | (F) | ((O) << WC_SUBLIST_FREE)))
826 827 828 829 830 831 832

#define WC_PIPE_TYPE(C)     (wc_data(C) & ((wordcode) 1))
#define WC_PIPE_END         0
#define WC_PIPE_MID         1
#define WC_PIPE_LINENO(C)   (wc_data(C) >> 1)
#define WCB_PIPE(T,L)       wc_bld(WC_PIPE, ((T) | ((L) << 1)))

833 834
#define WC_REDIR_TYPE(C)    ((int)(wc_data(C) & REDIR_TYPE_MASK))
#define WC_REDIR_VARID(C)   ((int)(wc_data(C) & REDIR_VARID_MASK))
835
#define WC_REDIR_FROM_HEREDOC(C) ((int)(wc_data(C) & REDIR_FROM_HEREDOC_MASK))
836
#define WCB_REDIR(T)        wc_bld(WC_REDIR, (T))
837
/* Size of redir is 4 words if REDIR_VARID_MASK is set, else 3 */
838 839 840
#define WC_REDIR_WORDS(C)			\
    ((WC_REDIR_VARID(C) ? 4 : 3) +		\
     (WC_REDIR_FROM_HEREDOC(C) ? 2 : 0))
841 842

#define WC_ASSIGN_TYPE(C)   (wc_data(C) & ((wordcode) 1))
843
#define WC_ASSIGN_TYPE2(C)  ((wc_data(C) & ((wordcode) 2)) >> 1)
844 845
#define WC_ASSIGN_SCALAR    0
#define WC_ASSIGN_ARRAY     1
846
#define WC_ASSIGN_NEW       0
847 848 849 850 851 852
/*
 * In normal assignment, this indicate += to append.
 * In assignment following a typeset, where that's not allowed,
 * we overload this to indicate a variable without an
 * assignment.
 */
853 854 855
#define WC_ASSIGN_INC       1
#define WC_ASSIGN_NUM(C)    (wc_data(C) >> 2)
#define WCB_ASSIGN(T,A,N)   wc_bld(WC_ASSIGN, ((T) | ((A) << 1) | ((N) << 2)))
856 857 858 859

#define WC_SIMPLE_ARGC(C)   wc_data(C)
#define WCB_SIMPLE(N)       wc_bld(WC_SIMPLE, (N))

860 861 862
#define WC_TYPESET_ARGC(C)  wc_data(C)
#define WCB_TYPESET(N)      wc_bld(WC_TYPESET, (N))

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
#define WC_SUBSH_SKIP(C)    wc_data(C)
#define WCB_SUBSH(O)        wc_bld(WC_SUBSH, (O))

#define WC_CURSH_SKIP(C)    wc_data(C)
#define WCB_CURSH(O)        wc_bld(WC_CURSH, (O))

#define WC_TIMED_TYPE(C)    wc_data(C)
#define WC_TIMED_EMPTY      0
#define WC_TIMED_PIPE       1
#define WCB_TIMED(T)        wc_bld(WC_TIMED, (T))

#define WC_FUNCDEF_SKIP(C)  wc_data(C)
#define WCB_FUNCDEF(O)      wc_bld(WC_FUNCDEF, (O))

#define WC_FOR_TYPE(C)      (wc_data(C) & 3)
#define WC_FOR_PPARAM       0
#define WC_FOR_LIST         1
#define WC_FOR_COND         2
#define WC_FOR_SKIP(C)      (wc_data(C) >> 2)
#define WCB_FOR(T,O)        wc_bld(WC_FOR, ((T) | ((O) << 2)))

#define WC_SELECT_TYPE(C)   (wc_data(C) & 1)
#define WC_SELECT_PPARAM    0
#define WC_SELECT_LIST      1
#define WC_SELECT_SKIP(C)   (wc_data(C) >> 1)
#define WCB_SELECT(T,O)     wc_bld(WC_SELECT, ((T) | ((O) << 1)))

#define WC_WHILE_TYPE(C)    (wc_data(C) & 1)
#define WC_WHILE_WHILE      0
#define WC_WHILE_UNTIL      1
#define WC_WHILE_SKIP(C)    (wc_data(C) >> 1)
#define WCB_WHILE(T,O)      wc_bld(WC_WHILE, ((T) | ((O) << 1)))

#define WC_REPEAT_SKIP(C)   wc_data(C)
#define WCB_REPEAT(O)       wc_bld(WC_REPEAT, (O))

899 900 901
#define WC_TRY_SKIP(C)	    wc_data(C)
#define WCB_TRY(O)	    wc_bld(WC_TRY, (O))

902
#define WC_CASE_TYPE(C)     (wc_data(C) & 7)
903 904 905
#define WC_CASE_HEAD        0
#define WC_CASE_OR          1
#define WC_CASE_AND         2
906 907 908 909
#define WC_CASE_TESTAND     3
#define WC_CASE_FREE	    (3) /* Next bit available in integer */
#define WC_CASE_SKIP(C)     (wc_data(C) >> WC_CASE_FREE)
#define WCB_CASE(T,O)       wc_bld(WC_CASE, ((T) | ((O) << WC_CASE_FREE)))
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

#define WC_IF_TYPE(C)       (wc_data(C) & 3)
#define WC_IF_HEAD          0
#define WC_IF_IF            1
#define WC_IF_ELIF          2
#define WC_IF_ELSE          3
#define WC_IF_SKIP(C)       (wc_data(C) >> 2)
#define WCB_IF(T,O)         wc_bld(WC_IF, ((T) | ((O) << 2)))

#define WC_COND_TYPE(C)     (wc_data(C) & 127)
#define WC_COND_SKIP(C)     (wc_data(C) >> 7)
#define WCB_COND(T,O)       wc_bld(WC_COND, ((T) | ((O) << 7)))

#define WCB_ARITH()         wc_bld(WC_ARITH, 0)

#define WCB_AUTOFN()        wc_bld(WC_AUTOFN, 0)
Tanaka Akira's avatar
Tanaka Akira committed
926 927

/********************************************/
928
/* Definitions for job table and job control */
Tanaka Akira's avatar
Tanaka Akira committed
929 930
/********************************************/

931 932 933 934 935 936 937 938 939 940 941 942
/* Entry in filelist linked list in job table */

struct jobfile {
    /* Record to be deleted or closed */
    union {
	char *name; /* Name of file to delete */
	int fd;	    /* File descriptor to close */
    } u;
    /* Discriminant */
    int is_fd;
};

Tanaka Akira's avatar
Tanaka Akira committed
943 944 945 946 947 948 949 950 951
/* entry in the job table */

struct job {
    pid_t gleader;		/* process group leader of this job  */
    pid_t other;		/* subjob id or subshell pid         */
    int stat;                   /* see STATs below                   */
    char *pwd;			/* current working dir of shell when *
				 * this job was spawned              */
    struct process *procs;	/* list of processes                 */
952
    struct process *auxprocs;	/* auxiliary processes e.g multios   */
Tanaka Akira's avatar
Tanaka Akira committed
953
    LinkList filelist;		/* list of files to delete when done */
954
				/* elements are struct jobfile */
Tanaka Akira's avatar
Tanaka Akira committed
955 956 957 958
    int stty_in_env;		/* if STTY=... is present            */
    struct ttyinfo *ty;		/* the modes specified by STTY       */
};

959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
#define STAT_CHANGED	(0x0001) /* status changed and not reported      */
#define STAT_STOPPED	(0x0002) /* all procs stopped or exited          */
#define STAT_TIMED	(0x0004) /* job is being timed                   */
#define STAT_DONE	(0x0008) /* job is done                          */
#define STAT_LOCKED	(0x0010) /* shell is finished creating this job, */
                                 /*   may be deleted from job table      */
#define STAT_NOPRINT	(0x0020) /* job was killed internally,           */
                                 /*   we don't want to show that         */
#define STAT_INUSE	(0x0040) /* this job entry is in use             */
#define STAT_SUPERJOB	(0x0080) /* job has a subjob                     */
#define STAT_SUBJOB	(0x0100) /* job is a subjob                      */
#define STAT_WASSUPER   (0x0200) /* was a super-job, sub-job needs to be */
				 /* deleted */
#define STAT_CURSH	(0x0400) /* last command is in current shell     */
#define STAT_NOSTTY	(0x0800) /* the tty settings are not inherited   */
				 /* from this job when it exits.         */
#define STAT_ATTACH	(0x1000) /* delay reattaching shell to tty       */
#define STAT_SUBLEADER  (0x2000) /* is super-job, but leader is sub-shell */
Tanaka Akira's avatar
Tanaka Akira committed
977

978 979
#define STAT_BUILTIN    (0x4000) /* job at tail of pipeline is a builtin */

Tanaka Akira's avatar
Tanaka Akira committed
980 981 982 983 984 985 986 987 988
#define SP_RUNNING -1		/* fake status for jobs currently running */

struct timeinfo {
    long ut;                    /* user space time   */
    long st;                    /* system space time */
};

#define JOBTEXTSIZE 80

989 990 991
/* Size to initialise the job table to, and to increment it by when needed. */
#define MAXJOBS_ALLOC	(50)

Tanaka Akira's avatar
Tanaka Akira committed
992 993
/* node in job process lists */

994
#ifdef HAVE_GETRUSAGE
995
typedef struct rusage child_times_t;
996 997 998 999
#else
typedef struct timeinfo child_times_t;
#endif

Tanaka Akira's avatar
Tanaka Akira committed
1000 1001 1002 1003 1004
struct process {
    struct process *next;
    pid_t pid;                  /* process id                       */
    char text[JOBTEXTSIZE];	/* text to print when 'jobs' is run */
    int status;			/* return code from waitpid/wait3() */
1005
    child_times_t ti;
Tanaka Akira's avatar
Tanaka Akira committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    struct timeval bgtime;	/* time job was spawned             */
    struct timeval endtime;	/* time job exited                  */
};

struct execstack {
    struct execstack *next;

    pid_t list_pipe_pid;
    int nowait;
    int pline_level;
    int list_pipe_child;
    int list_pipe_job;
    char list_pipe_text[JOBTEXTSIZE];
    int lastval;
    int noeval;
    int badcshglob;
    pid_t cmdoutpid;
    int cmdoutval;
1024
    int use_cmdoutval;
1025 1026 1027
    int trap_return;
    int trap_state;
    int trapisfunc;
1028
    int traplocallevel;
Tanaka Akira's avatar
Tanaka Akira committed
1029 1030 1031 1032 1033 1034
    int noerrs;
    char *underscore;
};

struct heredocs {
    struct heredocs *next;
1035 1036 1037
    int type;
    int pc;
    char *str;
Tanaka Akira's avatar
Tanaka Akira committed
1038 1039 1040 1041 1042 1043 1044 1045 1046
};

struct dirsav {
    int dirfd, level;
    char *dirname;
    dev_t dev;
    ino_t ino;
};

1047 1048
#define MAX_PIPESTATS 256

Tanaka Akira's avatar
Tanaka Akira committed
1049 1050 1051 1052 1053 1054 1055
/*******************************/
/* Definitions for Hash Tables */
/*******************************/

typedef void *(*VFunc) _((void *));
typedef void (*FreeFunc) _((void *));

1056
typedef unsigned (*HashFunc)       _((const char *));
Tanaka Akira's avatar
Tanaka Akira committed
1057
typedef void     (*TableFunc)      _((HashTable));
1058 1059 1060 1061 1062
/*
 * Note that this is deliberately "char *", not "const char *",
 * since the AddNodeFunc is passed a pointer to a string that
 * will be stored and later freed.
 */
Tanaka Akira's avatar
Tanaka Akira committed
1063
typedef void     (*AddNodeFunc)    _((HashTable, char *, void *));
1064 1065
typedef HashNode (*GetNodeFunc)    _((HashTable, const char *));
typedef HashNode (*RemoveNodeFunc) _((HashTable, const char *));
Tanaka Akira's avatar
Tanaka Akira committed
1066
typedef void     (*FreeNodeFunc)   _((HashNode));
1067
typedef int      (*CompareFunc)    _((const char *, const char *));
Tanaka Akira's avatar
Tanaka Akira committed
1068 1069 1070 1071

/* type of function that is passed to *
 * scanhashtable or scanmatchtable    */
typedef void     (*ScanFunc)       _((HashNode, int));
1072
typedef void     (*ScanTabFunc)    _((HashTable, ScanFunc, int));
Tanaka Akira's avatar
Tanaka Akira committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

typedef void (*PrintTableStats) _((HashTable));

/* hash table for standard open hashing */

struct hashtable {
    /* HASHTABLE DATA */
    int hsize;			/* size of nodes[]  (number of hash values)   */
    int ct;			/* number of elements                         */
    HashNode *nodes;		/* array of size hsize                        */
1083
    void *tmpdata;
Tanaka Akira's avatar
Tanaka Akira committed
1084 1085 1086 1087 1088

    /* HASHTABLE METHODS */
    HashFunc hash;		/* pointer to hash function for this table    */
    TableFunc emptytable;	/* pointer to function to empty table         */
    TableFunc filltable;	/* pointer to function to fill table          */
1089
    CompareFunc cmpnodes;	/* pointer to function to compare two nodes     */
Tanaka Akira's avatar
Tanaka Akira committed
1090 1091 1092 1093 1094 1095 1096 1097 1098
    AddNodeFunc addnode;	/* pointer to function to add new node        */
    GetNodeFunc getnode;	/* pointer to function to get an enabled node */
    GetNodeFunc getnode2;	/* pointer to function to get node            */
				/* (getnode2 will ignore DISABLED flag)       */
    RemoveNodeFunc removenode;	/* pointer to function to delete a node       */
    ScanFunc disablenode;	/* pointer to function to disable a node      */
    ScanFunc enablenode;	/* pointer to function to enable a node       */
    FreeNodeFunc freenode;	/* pointer to function to free a node         */
    ScanFunc printnode;		/* pointer to function to print a node        */
1099
    ScanTabFunc scantab;	/* pointer to function to scan table          */
Tanaka Akira's avatar
Tanaka Akira committed
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118

#ifdef HASHTABLE_INTERNAL_MEMBERS
    HASHTABLE_INTERNAL_MEMBERS	/* internal use in hashtable.c                */
#endif
};

/* generic hash table node */

struct hashnode {
    HashNode next;		/* next in hash chain */
    char *nam;			/* hash key           */
    int flags;			/* various flags      */
};

/* The flag to disable nodes in a hash table.  Currently  *
 * you can disable builtins, shell functions, aliases and *
 * reserved words.                                        */
#define DISABLED	(1<<0)

1119 1120 1121
/* node in shell option table */

struct optname {
1122
    struct hashnode node;
1123 1124 1125
    int optno;			/* option number */
};

Tanaka Akira's avatar
Tanaka Akira committed
1126 1127 1128
/* node in shell reserved word hash table (reswdtab) */

struct reswd {
1129
    struct hashnode node;
Tanaka Akira's avatar
Tanaka Akira committed
1130 1131 1132 1133 1134 1135
    int token;			/* corresponding lexer token */
};

/* node in alias hash table (aliastab) */

struct alias {
1136
    struct hashnode node;
Tanaka Akira's avatar
Tanaka Akira committed
1137 1138 1139 1140
    char *text;			/* expansion of alias       */
    int inuse;			/* alias is being expanded  */
};

1141 1142
/* bit 0 of flags is the DISABLED flag */
/* is this alias global? */
Tanaka Akira's avatar
Tanaka Akira committed
1143
#define ALIAS_GLOBAL	(1<<1)
1144 1145
/* is this an alias for suffix handling? */
#define ALIAS_SUFFIX	(1<<2)
Tanaka Akira's avatar
Tanaka Akira committed
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
/* structure for foo=bar assignments */

struct asgment {
    struct linknode node;
    char *name;
    int is_array;
    union {
	char *scalar;
	LinkList array;
    } value;
};

/*
 * Assignment is array?
 */
#define ASG_ARRAYP(asg) ((asg)->is_array)

/*
 * Assignment has value?
1166 1167
 * If the assignment is an arrray, then it certainly has a value --- we
 * can only tell if there's an expicit assignment.
1168 1169
 */

1170
#define ASG_VALUEP(asg) (ASG_ARRAYP(asg) ||			\
1171 1172
			 ((asg)->value.scalar != (char *)0))

Tanaka Akira's avatar
Tanaka Akira committed
1173 1174 1175
/* node in command path hash table (cmdnamtab) */

struct cmdnam {
1176
    struct hashnode node;
Tanaka Akira's avatar
Tanaka Akira committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    union {
	char **name;		/* full pathname for external commands */
	char *cmd;		/* file name for hashed commands       */
    }
    u;
};

/* flag for nodes explicitly added to *
 * cmdnamtab with hash builtin        */
#define HASHED		(1<<1)

/* node in shell function hash table (shfunctab) */

struct shfunc {
1191
    struct hashnode node;
1192
    char *filename;             /* Name of file located in */
1193
    zlong lineno;		/* line number in above file */
1194
    Eprog funcdef;		/* function definition    */
1195
    Eprog redir;                /* redirections to apply */
1196
    Emulation_options sticky;   /* sticky emulation definitions, if any */
Tanaka Akira's avatar
Tanaka Akira committed
1197 1198
};

1199 1200 1201 1202 1203 1204 1205 1206 1207
/* Shell function context types. */

#define SFC_NONE     0		/* no function running */
#define SFC_DIRECT   1		/* called directly from the user */
#define SFC_SIGNAL   2		/* signal handler */
#define SFC_HOOK     3		/* one of the special functions */
#define SFC_WIDGET   4		/* user defined widget */
#define SFC_COMPLETE 5		/* called from completion code */
#define SFC_CWIDGET  6		/* new style completion widget */
1208
#define SFC_SUBST    7          /* used to perform substitution task */
1209

1210 1211 1212 1213 1214 1215 1216 1217
/* tp in funcstack */

enum {
    FS_SOURCE,
    FS_FUNC,
    FS_EVAL
};

1218 1219 1220 1221
/* node in function stack */

struct funcstack {
    Funcstack prev;		/* previous in stack */
1222
    char *name;			/* name of function/sourced file called */
1223
    char *filename;		/* file function resides in */
1224
    char *caller;		/* name of caller */
1225 1226
    zlong flineno;		/* line number in file */
    zlong lineno;		/* line offset from beginning of function */
1227
    int tp;     		/* type of entry: sourced file, func, eval */
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
};

/* node in list of function call wrappers */

typedef int (*WrapFunc) _((Eprog, FuncWrap, char *));

struct funcwrap {
    FuncWrap next;
    int flags;
    WrapFunc handler;
    Module module;
};

#define WRAPF_ADDED 1

#define WRAPDEF(func) \
    { NULL, 0, func, NULL }

1246 1247 1248 1249 1250 1251 1252 1253 1254
/*
 * User-defined hook arrays
 */

/* Name appended to function name to get hook array */
#define HOOK_SUFFIX	"_functions"
/* Length of that including NUL byte */
#define HOOK_SUFFIX_LEN	11

Tanaka Akira's avatar
Tanaka Akira committed
1255 1256
/* node in builtin command hash table (builtintab) */

1257 1258 1259 1260
/*
 * Handling of options.
 *
 * Option strings are standard in that a trailing `:' indicates
1261
 * a mandatory argument.  In addition, `::' indicates an optional
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
 * argument which must immediately follow the option letter if it is present.
 * `:%' indicates an optional numeric argument which may follow
 * the option letter or be in the next word; the only test is
 * that the next character is a digit, and no actual conversion is done.
 */

#define MAX_OPS 128

/* Macros taking struct option * and char argument */
/* Option was set as -X */
#define OPT_MINUS(ops,c)	((ops)->ind[c] & 1)
/* Option was set as +X */
#define OPT_PLUS(ops,c)		((ops)->ind[c] & 2)
/*
1276 1277 1278
 * Option was set any old how, maybe including an argument
 * (cheap test when we don't care).  Some bits of code
 * expect this to be 1 or 0.
1279
 */
1280
#define OPT_ISSET(ops,c)	((ops)->ind[c] != 0)
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
/* Option has an argument */
#define OPT_HASARG(ops,c)	((ops)->ind[c] > 3)
/* The argument for the option; not safe if it doesn't have one */
#define OPT_ARG(ops,c)		((ops)->args[((ops)->ind[c] >> 2) - 1])
/* Ditto, but safely returns NULL if there is no argument. */
#define OPT_ARG_SAFE(ops,c)	(OPT_HASARG(ops,c) ? OPT_ARG(ops,c) : NULL)

struct options {
    unsigned char ind[MAX_OPS];
    char **args;
    int argscount, argsalloc;
};

/*
 * Handler arguments are: builtin name, null-terminated argument
 * list excluding command name, option structure, the funcid element from the
 * builtin structure.
 */

typedef int (*HandlerFunc) _((char *, char **, Options, int));
1301
typedef int (*HandlerFuncAssign) _((char *, char **, LinkList, Options, int));
Tanaka Akira's avatar
Tanaka Akira committed
1302 1303 1304
#define NULLBINCMD ((HandlerFunc) 0)

struct builtin {
1305
    struct hashnode node;
Tanaka Akira's avatar
Tanaka Akira committed
1306 1307 1308 1309 1310 1311 1312 1313 1314
    HandlerFunc handlerfunc;	/* pointer to function that executes this builtin     */
    int minargs;		/* minimum number of arguments                        */
    int maxargs;		/* maximum number of arguments, or -1 for no limit    */
    int funcid;			/* xbins (see above) for overloaded handlerfuncs      */
    char *optstr;		/* string of legal options                            */
    char *defopts;		/* options set by default for overloaded handlerfuncs */
};

#define BUILTIN(name, flags, handler, min, max, funcid, optstr, defopts) \
1315
    { { NULL, name, flags }, handler, min, max, funcid, optstr, defopts }
Tanaka Akira's avatar
Tanaka Akira committed
1316 1317 1318 1319 1320 1321
#define BIN_PREFIX(name, flags) \
    BUILTIN(name, flags | BINF_PREFIX, NULLBINCMD, 0, 0, 0, NULL, NULL)

/* builtin flags */
/* DISABLE IS DEFINED AS (1<<0) */
#define BINF_PLUSOPTS		(1<<1)	/* +xyz legal */
1322 1323
#define BINF_PRINTOPTS		(1<<2)
#define BINF_ADDED		(1<<3)	/* is in the builtins hash table */
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
#define BINF_MAGICEQUALS	(1<<4)  /* needs automatic MAGIC_EQUAL_SUBST substitution */
#define BINF_PREFIX		(1<<5)
#define BINF_DASH		(1<<6)
#define BINF_BUILTIN		(1<<7)
#define BINF_COMMAND		(1<<8)
#define BINF_EXEC		(1<<9)
#define BINF_NOGLOB		(1<<10)
#define BINF_PSPECIAL		(1<<11)
/* Builtin option handling */
#define BINF_SKIPINVALID	(1<<12)	/* Treat invalid option as argument */
#define BINF_KEEPNUM		(1<<13) /* `[-+]NUM' can be an option */
#define BINF_SKIPDASH		(1<<14) /* Treat `-' as argument (maybe `+') */
1336
#define BINF_DASHDASHVALID	(1<<15) /* Handle `--' even if SKIPINVALD */
1337
#define BINF_CLEARENV		(1<<16) /* new process started with cleared env */
1338
#define BINF_AUTOALL		(1<<17) /* autoload all features at once */
1339 1340 1341 1342 1343 1344
 /*
  * Handles options itself.  This is only useful if the option string for a
  * builtin with an empty option string.  It is used to indicate that "--"
  * does not terminate options.
  */
#define BINF_HANDLES_OPTS	(1<<18)
1345 1346 1347 1348 1349 1350
/*
 * Handles the assignement interface.  The argv list actually contains
 * two nested litsts, the first of normal arguments, and the second of
 * assignment structures.
 */
#define BINF_ASSIGN		(1<<19)
Tanaka Akira's avatar
Tanaka Akira committed
1351 1352

struct module {
1353
    struct hashnode node;
1354 1355 1356
    union {
	void *handle;
	Linkedmod linked;
1357
	char *alias;
1358
    } u;
1359
    LinkList autoloads;
Tanaka Akira's avatar
Tanaka Akira committed
1360
    LinkList deps;
1361
    int wrapper;
Tanaka Akira's avatar
Tanaka Akira committed
1362 1363
};

1364
/* We are in the process of loading the module */
Tanaka Akira's avatar
Tanaka Akira committed
1365
#define MOD_BUSY    (1<<0)
1366 1367 1368 1369 1370
/*
 * We are in the process of unloading the module.
 * Note this is not needed to indicate a module is actually
 * unloaded: for that, the handle (or linked pointer) is set to NULL.
 */
1371
#define MOD_UNLOAD  (1<<1)
1372
/* We are in the process of setting up the module */
1373
#define MOD_SETUP   (1<<2)
1374
/* Module is statically linked into the main binary */
1375
#define MOD_LINKED  (1<<3)
1376
/* Module setup has been carried out (and module has not been finished) */
1377
#define MOD_INIT_S  (1<<4)
1378
/* Module boot has been carried out (and module has not been finished) */
1379
#define MOD_INIT_B  (1<<5)
1380
/* Module record is an alias */
1381
#define MOD_ALIAS   (1<<6)
1382

1383 1384 1385 1386
typedef int (*Module_generic_func) _((void));
typedef int (*Module_void_func) _((Module));
typedef int (*Module_features_func) _((Module, char ***));
typedef int (*Module_enables_func) _((Module, int **));
1387 1388 1389

struct linkedmod {
    char *name;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
    Module_void_func setup;
    Module_features_func features;
    Module_enables_func enables;
    Module_void_func boot;
    Module_void_func cleanup;
    Module_void_func finish;
};

/*
 * Structure combining all the concrete features available in
 * a module and with space for information about abstract features.
 */
struct features {
    /* List of builtins provided by the module and the size thereof */
    Builtin bn_list;
    int bn_size;
    /* List of conditions provided by the module and the size thereof */
    Conddef cd_list;
    int cd_size;
    /* List of math functions provided by the module and the size thereof */
    MathFunc mf_list;
    int mf_size;
1412 1413 1414
    /* List of parameters provided by the module and the size thereof */
    Paramdef pd_list;
    int pd_size;
1415 1416
    /* Number of abstract features */
    int n_abstract;
1417 1418
};

Peter Stephenson's avatar
Peter Stephenson committed
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
/*
 * Structure describing enables for one feature.
 */
struct feature_enables {
    /* String feature to enable (N.B. no leading +/- allowed) */
    char *str;
    /* Optional compiled pattern for str sans +/-, NULL for string match */
    Patprog pat;
};

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
/* C-function hooks */

typedef int (*Hookfn) _((Hookdef, void *));

struct hookdef {
    Hookdef next;
    char *name;
    Hookfn def;
    int flags;
    LinkList funcs;
};

#define HOOKF_ALL 1

#define HOOKDEF(name, func, flags) { NULL, name, (Hookfn) func, flags, NULL }

/*
 * Types used in pattern matching.  Most of these longs could probably
 * happily be ints.
 */

struct patprog {
    long		startoff;  /* length before start of programme */
    long		size;	   /* total size from start of struct */
    long		mustoff;   /* offset to string that must be present */
1454
    long		patmlen;   /* length of pure string or longest match */
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
    int			globflags; /* globbing flags to set at start */
    int			globend;   /* globbing flags set after finish */
    int			flags;	   /* PAT_* flags */
    int			patnpar;   /* number of active parentheses */
    char		patstartch;
};

/* Flags used in pattern matchers (Patprog) and passed down to patcompile */

#define PAT_FILE	0x0001	/* Pattern is a file name */
#define PAT_FILET	0x0002	/* Pattern is top level file, affects ~ */
#define PAT_ANY		0x0004	/* Match anything (cheap "*") */
#define PAT_NOANCH	0x0008	/* Not anchored at end */
#define PAT_NOGLD	0x0010	/* Don't glob dots */
#define PAT_PURES	0x0020	/* Pattern is a pure string: set internally */
#define PAT_STATIC	0x0040	/* Don't copy pattern to heap as per default */
#define PAT_SCAN	0x0080	/* Scanning, so don't try must-match test */
#define PAT_ZDUP        0x0100  /* Copy pattern in real memory */
1473 1474
#define PAT_NOTSTART	0x0200	/* Start of string is not real start */
#define PAT_NOTEND	0x0400	/* End of string is not real end */
1475
#define PAT_HAS_EXCLUDP	0x0800	/* (internal): top-level path1~path2. */
1476
#define PAT_LCMATCHUC   0x1000  /* equivalent to setting (#l) */
1477

1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
/**
 * Indexes into the array of active pattern characters.
 * This must match the array zpc_chars in pattern.c.
 */
enum zpc_chars {
    /*
     * These characters both terminate a pattern segment and
     * a pure string segment.
     */
    ZPC_SLASH,			/* / active as file separator */
    ZPC_NULL,			/* \0 as string terminator */
    ZPC_BAR,			/* | for "or" */
    ZPC_OUTPAR,			/* ) for grouping */
    ZPC_TILDE,			/* ~ for exclusion (extended glob) */
    ZPC_SEG_COUNT,              /* No. of the above characters */
    /*
     * These characters terminate a pure string segment.
     */
    ZPC_INPAR = ZPC_SEG_COUNT,  /* ( for grouping */
    ZPC_QUEST,			/* ? as wildcard */
    ZPC_STAR,			/* * as wildcard */
    ZPC_INBRACK,		/* [ for character class */
    ZPC_INANG,			/* < for numeric glob */
    ZPC_HAT,			/* ^ for exclusion (extended glob) */
    ZPC_HASH,			/* # for repetition (extended glob) */
    ZPC_BNULLKEEP,		/* Special backslashed null not removed */
1504 1505 1506 1507 1508
    /*
     * These characters are only valid before a parenthesis
     */
    ZPC_NO_KSH_GLOB,
    ZPC_KSH_QUEST = ZPC_NO_KSH_GLOB, /* ? for ?(...) in KSH_GLOB */
1509 1510 1511 1512 1513 1514 1515
    ZPC_KSH_STAR,               /* * for *(...) in KSH_GLOB */
    ZPC_KSH_PLUS,               /* + for +(...) in KSH_GLOB */
    ZPC_KSH_BANG,               /* ! for !(...) in KSH_GLOB */
    ZPC_KSH_AT,                 /* @ for @(...) in KSH_GLOB */
    ZPC_COUNT			/* Number of special chararacters */
};

1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
/*
 * Structure to save disables special characters for function scope.
 */
struct zpc_disables_save {
    struct zpc_disables_save *next;
    /*
     * Bit vector of ZPC_COUNT disabled characters.
     * We'll live dangerously and assumed ZPC_COUNT is no greater
     * than the number of bits an an unsigned int.
     */
    unsigned int disables;
};

typedef struct zpc_disables_save *Zpc_disables_save;

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
/*
 * Special match types used in character classes.  These
 * are represented as tokens, with Meta added.  The character
 * class is represented as a metafied string, with only these
 * tokens special.  Note that an active leading "!" or "^" for
 * negation is not part of the string but is flagged in the
 * surrounding context.
 *
 * These types are also used in character and equivalence classes
 * in completion matching.
 *
 * This must be kept ordered by the array colon_stuffs in pattern.c.
 */
/* Special value for first definition */
#define PP_FIRST  1
/* POSIX-defined types:  [:alpha:] etc. */
#define PP_ALPHA  1
#define PP_ALNUM  2
#define PP_ASCII  3
#define PP_BLANK  4
#define PP_CNTRL  5
#define PP_DIGIT  6
#define PP_GRAPH  7
#define PP_LOWER  8
#define PP_PRINT  9
#define PP_PUNCT  10
#define PP_SPACE  11
#define PP_UPPER  12
#define PP_XDIGIT 13
/* Zsh additions:  [:IDENT:] etc. */
#define PP_IDENT  14
#define PP_IFS    15
#define PP_IFSSPACE   16
#define PP_WORD   17
/* Special value for last definition */
#define PP_LAST   17

/* Unknown type.  Not used in a valid token. */
#define PP_UNKWN  18
/* Range: token followed by the (possibly multibyte) start and end */
#define PP_RANGE  19

1573 1574 1575 1576 1577
/* Globbing flags: lower 8 bits gives approx count */
#define GF_LCMATCHUC	0x0100
#define GF_IGNCASE	0x0200
#define GF_BACKREF	0x0400
#define GF_MATCHREF	0x0800
1578
#define GF_MULTIBYTE	0x1000	/* Use multibyte if supported by build */
1579 1580 1581 1582 1583 1584

/* Dummy Patprog pointers. Used mainly in executable code, but the
 * pattern code needs to know about it, too. */

#define dummy_patprog1 ((Patprog) 1)
#define dummy_patprog2 ((Patprog) 2)
Tanaka Akira's avatar
Tanaka Akira committed
1585

1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
/* standard node types for get/set/unset union in parameter */

/*
 * note non-standard const in pointer declaration: structures are
 * assumed to be read-only.
 */
typedef const struct gsu_scalar *GsuScalar;
typedef const struct gsu_integer *GsuInteger;
typedef const struct gsu_float *GsuFloat;
typedef const struct gsu_array *GsuArray;
typedef const struct gsu_hash *GsuHash;

struct gsu_scalar {
    char *(*getfn) _((Param));
    void (*setfn) _((Param, char  *));
    void (*unsetfn) _((Param, int));
};

struct gsu_integer {
    zlong (*getfn) _((Param));
    void (*setfn) _((Param, zlong));
    void (*unsetfn) _((Param, int));
};

struct gsu_float {
    double (*getfn) _((Param));
    void (*setfn) _((Param, double));
    void (*unsetfn) _((Param, int));
};

struct gsu_array {
    char **(*getfn) _((Param));
    void (*setfn) _((Param, char **));
    void (*unsetfn) _((Param, int));
};

struct gsu_hash {
    HashTable (*getfn) _((Param));
    void (*setfn) _((Param, HashTable));
    void (*unsetfn) _((Param, int));
};


Tanaka Akira's avatar
Tanaka Akira committed
1629 1630 1631
/* node used in parameter hash table (paramtab) */

struct param {
1632
    struct hashnode node;
Tanaka Akira's avatar
Tanaka Akira committed
1633 1634 1635 1636 1637 1638

    /* the value of this parameter */
    union {
	void *data;		/* used by special parameter functions    */
	char **arr;		/* value if declared array   (PM_ARRAY)   */
	char *str;		/* value if declared string  (PM_SCALAR)  */
1639
	zlong val;		/* value if declared integer (PM_INTEGER) */
1640
	zlong *valptr;		/* value if special pointer to integer */
1641 1642 1643
	double dval;		/* value if declared float
				                    (PM_EFLOAT|PM_FFLOAT) */
        HashTable hash;		/* value if declared assoc   (PM_HASHED)  */
Tanaka Akira's avatar
Tanaka Akira committed
1644 1645
    } u;

1646 1647 1648 1649 1650 1651 1652 1653
    /*
     * get/set/unset methods.
     *
     * Unlike the data union, this points to a single instance
     * for every type (although there are special types, e.g.
     * tied arrays have a different gsu_scalar struct from the
     * normal one).  It's really a poor man's vtable.
     */
Tanaka Akira's avatar
Tanaka Akira committed
1654
    union {
1655 1656 1657 1658 1659 1660 1661 1662 1663
	GsuScalar s;
	GsuInteger i;
	GsuFloat f;
	GsuArray a;
	GsuHash h;
    } gsu;

    int base;			/* output base or floating point prec    */
    int width;			/* field width                           */
Tanaka Akira's avatar
Tanaka Akira committed
1664 1665 1666 1667 1668 1669
    char *env;			/* location in environment, if exported  */
    char *ename;		/* name of corresponding environment var */
    Param old;			/* old struct for use with local         */
    int level;			/* if (old != NULL), level of localness  */
};

1670 1671 1672 1673 1674 1675
/* structure stored in struct param's u.data by tied arrays */
struct tieddata {
    char ***arrptr;		/* pointer to corresponding array */
    int joinchar;		/* character used to join arrays */
};

Tanaka Akira's avatar
Tanaka Akira committed
1676 1677 1678
/* flags for parameters */

/* parameter types */
1679 1680 1681 1682 1683 1684
#define PM_SCALAR	0	/* scalar                                   */
#define PM_ARRAY	(1<<0)	/* array                                    */
#define PM_INTEGER	(1<<1)	/* integer                                  */
#define PM_EFLOAT	(1<<2)	/* double with %e output		    */
#define PM_FFLOAT	(1<<3)	/* double with %f output		    */
#define PM_HASHED	(1<<4)	/* association                              */
Tanaka Akira's avatar
Tanaka Akira committed
1685

1686 1687
#define PM_TYPE(X) \
  (X & (PM_SCALAR|PM_INTEGER|PM_EFLOAT|PM_FFLOAT|PM_ARRAY|PM_HASHED))
Tanaka Akira's avatar
Tanaka Akira committed
1688

1689 1690 1691 1692
#define PM_LEFT		(1<<5)	/* left justify, remove leading blanks      */
#define PM_RIGHT_B	(1<<6)	/* right justify, fill with leading blanks  */
#define PM_RIGHT_Z	(1<<7)	/* right justify, fill with leading zeros   */
#define PM_LOWER	(1<<8)	/* all lower case                           */
Tanaka Akira's avatar
Tanaka Akira committed
1693 1694 1695

/* The following are the same since they *
 * both represent -u option to typeset   */
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
#define PM_UPPER	(1<<9)	/* all upper case                           */
#define PM_UNDEFINED	(1<<9)	/* undefined (autoloaded) shell function    */

#define PM_READONLY	(1<<10)	/* readonly                                 */
#define PM_TAGGED	(1<<11)	/* tagged                                   */
#define PM_EXPORTED	(1<<12)	/* exported                                 */

/* The following are the same since they *
 * both represent -U option to typeset   */
#define PM_UNIQUE	(1<<13)	/* remove duplicates                        */
#define PM_UNALIASED	(1<<13)	/* do not expand aliases when autoloading   */

#define PM_HIDE		(1<<14)	/* Special behaviour hidden by local        */
Peter Stephenson's avatar
Peter Stephenson committed
1709 1710
#define PM_HIDEVAL	(1<<15)	/* Value not shown in `typeset' commands    */
#define PM_TIED 	(1<<16)	/* array tied to colon-path or v.v.         */
1711
#define PM_TAGGED_LOCAL (1<<16) /* (function): non-recursive PM_TAGGED      */
1712

1713 1714 1715
#define PM_KSHSTORED	(1<<17) /* function stored in ksh form              */
#define PM_ZSHSTORED	(1<<18) /* function stored in zsh form              */

1716
/* Remaining flags do not correspond directly to command line arguments */
1717 1718 1719 1720 1721 1722 1723 1724
#define PM_LOCAL	(1<<21) /* this parameter will be made local        */
#define PM_SPECIAL	(1<<22) /* special builtin parameter                */
#define PM_DONTIMPORT	(1<<23)	/* do not import this variable              */
#define PM_RESTRICTED	(1<<24) /* cannot be changed in restricted mode     */
#define PM_UNSET	(1<<25)	/* has null value                           */
#define PM_REMOVABLE	(1<<26)	/* special can be removed from paramtab     */
#define PM_AUTOLOAD	(1<<27) /* autoloaded from module                   */
#define PM_NORESTORE	(1<<28)	/* do not restore value of local special    */
1725 1726 1727 1728
#define PM_AUTOALL	(1<<28) /* autoload all features in module
				 * when loading: valid only if PM_AUTOLOAD
				 * is also present.
				 */
1729 1730
#define PM_HASHELEM     (1<<29) /* is a hash-element */
#define PM_NAMEDDIR     (1<<30) /* has a corresponding nameddirtab entry    */
1731 1732

/* The option string corresponds to the first of the variables above */
1733
#define TYPESET_OPTSTR "aiEFALRZlurtxUhHTkz"
1734 1735 1736 1737 1738

/* These typeset options take an optional numeric argument */
#define TYPESET_OPTNUM "LRZiEF"

/* Flags for extracting elements of arrays and associative arrays */
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
#define SCANPM_WANTVALS   (1<<0) /* Return value includes hash values */
#define SCANPM_WANTKEYS   (1<<1) /* Return value includes hash keys */
#define SCANPM_WANTINDEX  (1<<2) /* Return value includes array index */
#define SCANPM_MATCHKEY   (1<<3) /* Subscript matched against key */
#define SCANPM_MATCHVAL   (1<<4) /* Subscript matched against value */
#define SCANPM_MATCHMANY  (1<<5) /* Subscript matched repeatedly, return all */
#define SCANPM_ASSIGNING  (1<<6) /* Assigning whole array/hash */
#define SCANPM_KEYMATCH   (1<<7) /* keys of hash treated as patterns */
#define SCANPM_DQUOTED    (1<<8) /* substitution was double-quoted
				  * (only used for testing early end of
				  * subscript)
				  */
#define SCANPM_ARRONLY    (1<<9) /* value is array but we don't
				  * necessarily want to match multiple
				  * elements
				  */
1755 1756 1757 1758
/* "$foo[@]"-style substitution
 * Only sign bit is significant
 */
#define SCANPM_ISVAR_AT   ((int)(((unsigned int)-1)<<15))
1759 1760 1761 1762 1763 1764 1765

/*
 * Flags for doing matches inside parameter substitutions, i.e.
 * ${...#...} and friends.  This could be an enum, but so
 * could a lot of other things.
 */

1766
#define SUB_END		0x0001	/* match end instead of beginning, % or %%  */
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
#define SUB_LONG	0x0002	/* % or # doubled, get longest match */
#define SUB_SUBSTR	0x0004	/* match a substring */
#define SUB_MATCH	0x0008	/* include the matched portion */
#define SUB_REST	0x0010	/* include the unmatched portion */
#define SUB_BIND	0x0020	/* index of beginning of string */
#define SUB_EIND	0x0040	/* index of end of string */
#define SUB_LEN		0x0080	/* length of match */
#define SUB_ALL		0x0100	/* match complete string */
#define SUB_GLOBAL	0x0200	/* global substitution ${..//all/these} */
#define SUB_DOSUBST	0x0400	/* replacement string needs substituting */
1777 1778 1779
#define SUB_RETFAIL	0x0800  /* return status 0 if no match */
#define SUB_START	0x1000  /* force match at start with SUB_END
				 * and no SUB_SUBSTR */
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
#define SUB_LIST	0x2000  /* no substitution, return list of matches */

/*
 * Structure recording multiple matches inside a test string.
 * b and e are the beginning and end of the match.
 * replstr is the replacement string, if any.
 */
struct repldata {
    int b, e;			/* beginning and end of chunk to replace */
    char *replstr;		/* replacement string to use */
};
typedef struct repldata *Repldata;

1793 1794 1795 1796 1797 1798 1799 1800 1801
/*
 * Flags to zshtokenize.
 */
enum {
    /* Do glob substitution */
    ZSHTOK_SUBST = 0x0001,
    /* Use sh-style globbing */
    ZSHTOK_SHGLOB = 0x0002
};
1802 1803

/* Flags as the second argument to prefork */
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
/* argument handled like typeset foo=bar */
#define PREFORK_TYPESET	        0x01
/* argument handled like the RHS of foo=bar */
#define PREFORK_ASSIGN	        0x02
/* single word substitution */
#define PREFORK_SINGLE	        0x04
/* explicitly split nested substitution */
#define PREFORK_SPLIT           0x08
/* SHWORDSPLIT in parameter expn */
#define PREFORK_SHWORDSPLIT     0x10
/* SHWORDSPLIT forced off in nested subst */
#define PREFORK_NOSHWORDSPLIT   0x20
1816

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
/*
 * Structure for adding parameters in a module.
 * The flags should declare the type; note PM_SCALAR is zero.
 *
 * Special hashes are recognized by getnfn so the PM_HASHED
 * is optional.  These get slightly non-standard attention:
 * the function createspecialhash is used to create them.
 *
 * The get/set/unset attribute may be NULL; in that case the
 * parameter is assigned methods suitable for handling the
 * tie variable var, if that is not NULL, else standard methods.
 *
 * pm is set when the parameter is added to the parameter table
 * and serves as a flag that the parameter has been added.
 */
1832 1833 1834
struct paramdef {
    char *name;
    int flags;
1835 1836 1837 1838 1839
    void *var;			/* tied internal variable, if any */
    const void *gsu;		/* get/set/unset structure, if special */
    GetNodeFunc getnfn;		/* function to get node, if special hash */
    ScanTabFunc scantfn;	/* function to scan table, if special hash */
    Param pm;			/* structure inserted into param table */
1840 1841
};

1842 1843 1844 1845
/*
 * Shorthand for common uses of adding parameters, with no special
 * hash properties.
 */
1846
#define PARAMDEF(name, flags, var, gsu) \
1847 1848 1849
    { name, flags, (void *) var, (void *) gsu, \
	    NULL, NULL, NULL \
    }
1850 1851 1852 1853 1854
/*
 * Note that the following definitions are appropriate for defining
 * parameters that reference a variable (var).  Hence the get/set/unset
 * methods used will assume var needs dereferencing to get the value.
 */
1855
#define INTPARAMDEF(name, var) \
1856
    { name, PM_INTEGER, (void *) var, NULL,  NULL, NULL, NULL }
1857
#define STRPARAMDEF(name, var) \
1858
    { name, PM_SCALAR, (void *) var, NULL, NULL, NULL, NULL }
1859
#define ARRPARAMDEF(name, var) \
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
    { name, PM_ARRAY, (void *) var, NULL, NULL, NULL, NULL }
/*
 * The following is appropriate for a module function that behaves
 * in a special fashion.  Parameters used in a module that don't
 * have special behaviour shouldn't be declared in a table but
 * should just be added with the standard parameter functions.
 *
 * These parameters are not marked as removable, since they
 * shouldn't be loaded as local parameters, unlike the special
 * Zle parameters that are added and removed on each call to Zle.
 * We add the PM_REMOVABLE flag when removing the feature corresponding
 * to the parameter.
 */
#define SPECIALPMDEF(name, flags, gsufn, getfn, scanfn) \
    { name, flags | PM_SPECIAL | PM_HIDE | PM_HIDEVAL, \
	    NULL, gsufn, getfn, scanfn, NULL }
Tanaka Akira's avatar
Tanaka Akira committed
1876

1877 1878 1879
#define setsparam(S,V) assignsparam(S,V,0)
#define setaparam(S,V) assignaparam(S,V,0)

1880 1881 1882 1883 1884
/*
 * Flags for assignsparam and assignaparam.
 */
enum {
    ASSPM_AUGMENT = 1 << 0,
1885 1886
    ASSPM_WARN_CREATE = 1 << 1,
    ASSPM_ENV_IMPORT = 1 << 2
1887 1888
};

Tanaka Akira's avatar
Tanaka Akira committed
1889 1890 1891
/* node for named directory hash table (nameddirtab) */

struct nameddir {
1892
    struct hashnode node;
Tanaka Akira's avatar
Tanaka Akira committed
1893 1894 1895 1896 1897 1898 1899
    char *dir;			/* the directory in full            */
    int diff;			/* strlen(.dir) - strlen(.nam)      */
};

/* flags for named directories */
/* DISABLED is defined (1<<0) */
#define ND_USERNAME	(1<<1)	/* nam is actually a username       */
1900
#define ND_NOABBREV	(1<<2)	/* never print as abbrev (PWD or OLDPWD) */
Tanaka Akira's avatar
Tanaka Akira committed
1901

1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
/* Storage for single group/name mapping */
typedef struct {
    /* Name of group */
    char *name;
    /* Group identifier */
    gid_t gid;
} groupmap;
typedef groupmap *Groupmap;

/* Storage for a set of group/name mappings */
typedef struct {
    /* The set of name to gid mappings */
    Groupmap array;
    /* A count of the valid entries in groupmap. */
    int num;
} groupset;
typedef groupset *Groupset;