1 /* exp_clib.c - top-level functions in the expect C library, libexpect.a
3 Written by: Don Libes, libes@cme.nist.gov, NIST, 12/3/90
5 Design and implementation of this program was paid for by U.S. tax
6 dollars. Therefore it is public domain. However, the author and NIST
7 would appreciate credit if this program or parts of it are used.
10 #include "expect_cf.h"
14 #ifdef HAVE_INTTYPES_H
15 # include <inttypes.h>
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
20 #ifdef TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
33 # if defined(HAVE_TERMIOS)
41 #ifdef HAVE_SYS_FCNTL_H
42 # include <sys/fcntl.h>
47 #ifdef HAVE_STRREDIR_H
48 #include <sys/strredir.h>
55 /*#include <memory.h> - deprecated - ANSI C moves them into string.h */
63 * Tcl's compat/stdlib.h
69 * Declares facilities exported by the "stdlib" portion of
70 * the C library. This file isn't complete in the ANSI-C
71 * sense; it only declares things that are needed by Tcl.
72 * This file is needed even on many systems with their own
73 * stdlib.h (e.g. SunOS) because not all stdlib.h files
74 * declare all the procedures needed here (such as strtod).
76 * Copyright (c) 1991 The Regents of the University of California.
77 * Copyright (c) 1994 Sun Microsystems, Inc.
79 * See the file "license.terms" for information on usage and redistribution
80 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
82 * RCS: @(#) $Id: exp_clib.c,v 5.39 2010/08/31 22:20:27 andreas_kupries Exp $
88 extern void abort _ANSI_ARGS_((void));
89 extern double atof _ANSI_ARGS_((CONST char *string));
90 extern int atoi _ANSI_ARGS_((CONST char *string));
91 extern long atol _ANSI_ARGS_((CONST char *string));
92 extern char * calloc _ANSI_ARGS_((unsigned int numElements,
94 extern void exit _ANSI_ARGS_((int status));
95 extern int free _ANSI_ARGS_((char *blockPtr));
96 extern char * getenv _ANSI_ARGS_((CONST char *name));
97 extern char * malloc _ANSI_ARGS_((unsigned int numBytes));
98 extern void qsort _ANSI_ARGS_((VOID *base, int n, int size,
99 int (*compar)(CONST VOID *element1, CONST VOID
101 extern char * realloc _ANSI_ARGS_((char *ptr, unsigned int numBytes));
102 extern double strtod _ANSI_ARGS_((CONST char *string, char **endPtr));
103 extern long strtol _ANSI_ARGS_((CONST char *string, char **endPtr,
105 extern unsigned long strtoul _ANSI_ARGS_((CONST char *string,
106 char **endPtr, int base));
111 * end of Tcl's compat/stdlib.h
115 #include <stdlib.h> /* for malloc */
120 #define TclRegError exp_TclRegError
123 * regexp code - from tcl8.0.4/generic/regexp.c
127 * TclRegComp and TclRegExec -- TclRegSub is elsewhere
129 * Copyright (c) 1986 by University of Toronto.
130 * Written by Henry Spencer. Not derived from licensed software.
132 * Permission is granted to anyone to use this software for any
133 * purpose on any computer system, and to redistribute it freely,
134 * subject to the following restrictions:
136 * 1. The author is not responsible for the consequences of use of
137 * this software, no matter how awful, even if they arise
138 * from defects in it.
140 * 2. The origin of this software must not be misrepresented, either
141 * by explicit claim or by omission.
143 * 3. Altered versions must be plainly marked as such, and must not
144 * be misrepresented as being the original software.
146 * Beware that some of this code is subtly aware of the way operator
147 * precedence is structured in regular expressions. Serious changes in
148 * regular-expression syntax might require a total rethink.
150 * *** NOTE: this code has been altered slightly for use in Tcl: ***
151 * *** 1. Use ckalloc and ckfree instead of malloc and free. ***
152 * *** 2. Add extra argument to regexp to specify the real ***
153 * *** start of the string separately from the start of the ***
154 * *** current search. This is needed to search for multiple ***
155 * *** matches within a string. ***
156 * *** 3. Names have been changed, e.g. from regcomp to ***
157 * *** TclRegComp, to avoid clashes with other ***
158 * *** regexp implementations used by applications. ***
159 * *** 4. Added errMsg declaration and TclRegError procedure ***
160 * *** 5. Various lint-like things, such as casting arguments ***
161 * *** in procedure calls. ***
163 * *** NOTE: This code has been altered for use in MT-Sturdy Tcl ***
164 * *** 1. All use of static variables has been changed to access ***
165 * *** fields of a structure. ***
166 * *** 2. This in addition to changes to TclRegError makes the ***
167 * *** code multi-thread safe. ***
169 * RCS: @(#) $Id: exp_clib.c,v 5.39 2010/08/31 22:20:27 andreas_kupries Exp $
178 * The variable below is set to NULL before invoking regexp functions
179 * and checked after those functions. If an error occurred then TclRegError
180 * will set the variable to point to a (static) error message. This
181 * mechanism unfortunately does not support multi-threading, but the
182 * procedures TclRegError and TclGetRegError can be modified to use
183 * thread-specific storage for the variable and thereby make the code
187 static char *errMsg = NULL;
190 * The "internal use only" fields in regexp.h are present to pass info from
191 * compile to execute that permits the execute phase to run lots faster on
192 * simple cases. They are:
194 * regstart char that must begin a match; '\0' if none obvious
195 * reganch is the match anchored (at beginning-of-line only)?
196 * regmust string (pointer into program) that match must include, or NULL
197 * regmlen length of regmust string
199 * Regstart and reganch permit very fast decisions on suitable starting points
200 * for a match, cutting down the work a lot. Regmust permits fast rejection
201 * of lines that cannot possibly match. The regmust tests are costly enough
202 * that TclRegComp() supplies a regmust only if the r.e. contains something
203 * potentially expensive (at present, the only such thing detected is * or +
204 * at the start of the r.e., which can involve a lot of backup). Regmlen is
205 * supplied because the test in TclRegExec() needs it and TclRegComp() is
206 * computing it anyway.
210 * Structure for regexp "program". This is essentially a linear encoding
211 * of a nondeterministic finite-state machine (aka syntax charts or
212 * "railroad normal form" in parsing technology). Each node is an opcode
213 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
214 * all nodes except BRANCH implement concatenation; a "next" pointer with
215 * a BRANCH on both ends of it is connecting two alternatives. (Here we
216 * have one of the subtle syntax dependencies: an individual BRANCH (as
217 * opposed to a collection of them) is never concatenated with anything
218 * because of operator precedence.) The operand of some types of node is
219 * a literal string; for others, it is a node leading into a sub-FSM. In
220 * particular, the operand of a BRANCH node is the first node of the branch.
221 * (NB this is *not* a tree structure: the tail of the branch connects
222 * to the thing following the set of BRANCHes.) The opcodes are:
225 /* definition number opnd? meaning */
226 #define END 0 /* no End of program. */
227 #define BOL 1 /* no Match "" at beginning of line. */
228 #define EOL 2 /* no Match "" at end of line. */
229 #define ANY 3 /* no Match any one character. */
230 #define ANYOF 4 /* str Match any character in this string. */
231 #define ANYBUT 5 /* str Match any character not in this string. */
232 #define BRANCH 6 /* node Match this alternative, or the next... */
233 #define BACK 7 /* no Match "", "next" ptr points backward. */
234 #define EXACTLY 8 /* str Match this string. */
235 #define NOTHING 9 /* no Match empty string. */
236 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
237 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
238 #define OPEN 20 /* no Mark this point in input as start of #n. */
239 /* OPEN+1 is number 1, etc. */
240 #define CLOSE (OPEN+NSUBEXP) /* no Analogous to OPEN. */
245 * BRANCH The set of branches constituting a single choice are hooked
246 * together with their "next" pointers, since precedence prevents
247 * anything being concatenated to any individual branch. The
248 * "next" pointer of the last BRANCH in a choice points to the
249 * thing following the whole choice. This is also where the
250 * final "next" pointer of each individual branch points; each
251 * branch starts with the operand node of a BRANCH node.
253 * BACK Normal "next" pointers all implicitly point forward; BACK
254 * exists to make loop structures possible.
256 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
257 * BRANCH structures using BACK. Simple cases (one character
258 * per match) are implemented with STAR and PLUS for speed
259 * and to minimize recursive plunges.
261 * OPEN,CLOSE ...are numbered at compile time.
265 * A node is one char of opcode followed by two chars of "next" pointer.
266 * "Next" pointers are stored as two 8-bit pieces, high order first. The
267 * value is a positive offset from the opcode of the node containing it.
268 * An operand, if any, simply follows the node. (Note that much of the
269 * code generation knows about this implicit relationship.)
271 * Using two bytes for the "next" pointer is vast overkill for most things,
272 * but allows patterns to get big without disasters.
275 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
276 #define OPERAND(p) ((p) + 3)
279 * See regmagic.h for one further detail of program structure.
284 * Utility definitions.
287 #define UCHARAT(p) ((int)*(unsigned char *)(p))
289 #define UCHARAT(p) ((int)*(p)&CHARBITS)
292 #define FAIL(m) { TclRegError(m); return(NULL); }
293 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
294 #define META "^$.[()|?+*\\"
297 * Flags to be passed up and down.
299 #define HASWIDTH 01 /* Known never to match null string. */
300 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
301 #define SPSTART 04 /* Starts with * or +. */
302 #define WORST 0 /* Worst case. */
305 * Global work variables for TclRegComp().
307 struct regcomp_state {
308 char *regparse; /* Input-scan pointer. */
309 int regnpar; /* () count. */
310 char *regcode; /* Code-emit pointer; ®dummy = don't. */
311 long regsize; /* Code size. */
314 static char regdummy;
317 * The first byte of the regexp internal "program" is actually this magic
318 * number; the start node begins in the second byte.
324 * Forward declarations for TclRegComp()'s friends.
327 static char * reg _ANSI_ARGS_((int paren, int *flagp,
328 struct regcomp_state *rcstate));
329 static char * regatom _ANSI_ARGS_((int *flagp,
330 struct regcomp_state *rcstate));
331 static char * regbranch _ANSI_ARGS_((int *flagp,
332 struct regcomp_state *rcstate));
333 static void regc _ANSI_ARGS_((int b,
334 struct regcomp_state *rcstate));
335 static void reginsert _ANSI_ARGS_((int op, char *opnd,
336 struct regcomp_state *rcstate));
337 static char * regnext _ANSI_ARGS_((char *p));
338 static char * regnode _ANSI_ARGS_((int op,
339 struct regcomp_state *rcstate));
340 static void regoptail _ANSI_ARGS_((char *p, char *val));
341 static char * regpiece _ANSI_ARGS_((int *flagp,
342 struct regcomp_state *rcstate));
343 static void regtail _ANSI_ARGS_((char *p, char *val));
346 static int strcspn _ANSI_ARGS_((char *s1, char *s2));
350 - TclRegComp - compile a regular expression into internal code
352 * We can't allocate space until we know how big the compiled form will be,
353 * but we can't compile it (and thus know how big it is) until we've got a
354 * place to put the code. So we cheat: we compile it twice, once with code
355 * generation turned off and size counting turned on, and once "for real".
356 * This also means that we don't allocate space until we are sure that the
357 * thing really will compile successfully, and we never have to move the
358 * code and thus invalidate pointers into it. (Note that it has to be in
359 * one piece because free() must be able to free it all.)
361 * Beware that the optimization-preparation code in here knows about some
362 * of the structure of the compiled regexp.
370 register char *longest;
373 struct regcomp_state state;
374 struct regcomp_state *rcstate= &state;
377 FAIL("NULL argument");
379 /* First pass: determine size, legality. */
380 rcstate->regparse = exp;
381 rcstate->regnpar = 1;
382 rcstate->regsize = 0L;
383 rcstate->regcode = ®dummy;
384 regc(MAGIC, rcstate);
385 if (reg(0, &flags, rcstate) == NULL)
388 /* Small enough for pointer-storage convention? */
389 if (rcstate->regsize >= 32767L) /* Probably could be 65535L. */
390 FAIL("regexp too big");
392 /* Allocate space. */
393 r = (regexp *)ckalloc(sizeof(regexp) + (unsigned)rcstate->regsize);
395 FAIL("out of space");
397 /* Second pass: emit code. */
398 rcstate->regparse = exp;
399 rcstate->regnpar = 1;
400 rcstate->regcode = r->program;
401 regc(MAGIC, rcstate);
402 if (reg(0, &flags, rcstate) == NULL) {
407 /* Dig out information for optimizations. */
408 r->regstart = '\0'; /* Worst-case defaults. */
412 scan = r->program+1; /* First BRANCH. */
413 if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
414 scan = OPERAND(scan);
416 /* Starting-point info. */
417 if (OP(scan) == EXACTLY)
418 r->regstart = *OPERAND(scan);
419 else if (OP(scan) == BOL)
423 * If there's something expensive in the r.e., find the
424 * longest literal string that must appear and make it the
425 * regmust. Resolve ties in favor of later strings, since
426 * the regstart check works with the beginning of the r.e.
427 * and avoiding duplication strengthens checking. Not a
428 * strong reason, but sufficient in the absence of others.
433 for (; scan != NULL; scan = regnext(scan))
434 if (OP(scan) == EXACTLY && ((int) strlen(OPERAND(scan))) >= len) {
435 longest = OPERAND(scan);
436 len = strlen(OPERAND(scan));
438 r->regmust = longest;
447 - reg - regular expression, i.e. main body or parenthesized thing
449 * Caller must absorb opening parenthesis.
451 * Combining parenthesis handling with the base level of regular expression
452 * is a trifle forced, but the need to tie the tails of the branches to what
453 * follows makes it hard to avoid.
456 reg(paren, flagp, rcstate)
457 int paren; /* Parenthesized? */
459 struct regcomp_state *rcstate;
463 register char *ender;
464 register int parno = 0;
467 *flagp = HASWIDTH; /* Tentatively. */
469 /* Make an OPEN node, if parenthesized. */
471 if (rcstate->regnpar >= NSUBEXP)
473 parno = rcstate->regnpar;
475 ret = regnode(OPEN+parno,rcstate);
479 /* Pick up the branches, linking them together. */
480 br = regbranch(&flags,rcstate);
484 regtail(ret, br); /* OPEN -> first. */
487 if (!(flags&HASWIDTH))
489 *flagp |= flags&SPSTART;
490 while (*rcstate->regparse == '|') {
492 br = regbranch(&flags,rcstate);
495 regtail(ret, br); /* BRANCH -> BRANCH. */
496 if (!(flags&HASWIDTH))
498 *flagp |= flags&SPSTART;
501 /* Make a closing node, and hook it on the end. */
502 ender = regnode((paren) ? CLOSE+parno : END,rcstate);
505 /* Hook the tails of the branches to the closing node. */
506 for (br = ret; br != NULL; br = regnext(br))
507 regoptail(br, ender);
509 /* Check for proper termination. */
510 if (paren && *rcstate->regparse++ != ')') {
511 FAIL("unmatched ()");
512 } else if (!paren && *rcstate->regparse != '\0') {
513 if (*rcstate->regparse == ')') {
514 FAIL("unmatched ()");
516 FAIL("junk on end"); /* "Can't happen". */
524 - regbranch - one alternative of an | operator
526 * Implements the concatenation operator.
529 regbranch(flagp, rcstate)
531 struct regcomp_state *rcstate;
534 register char *chain;
535 register char *latest;
538 *flagp = WORST; /* Tentatively. */
540 ret = regnode(BRANCH,rcstate);
542 while (*rcstate->regparse != '\0' && *rcstate->regparse != '|' &&
543 *rcstate->regparse != ')') {
544 latest = regpiece(&flags, rcstate);
547 *flagp |= flags&HASWIDTH;
548 if (chain == NULL) /* First piece. */
549 *flagp |= flags&SPSTART;
551 regtail(chain, latest);
554 if (chain == NULL) /* Loop ran zero times. */
555 (void) regnode(NOTHING,rcstate);
561 - regpiece - something followed by possible [*+?]
563 * Note that the branching code sequences used for ? and the general cases
564 * of * and + are somewhat optimized: they use the same NOTHING node as
565 * both the endmarker for their branch list and the body of the last branch.
566 * It might seem that this node could be dispensed with entirely, but the
567 * endmarker role is not redundant.
570 regpiece(flagp, rcstate)
572 struct regcomp_state *rcstate;
579 ret = regatom(&flags,rcstate);
583 op = *rcstate->regparse;
589 if (!(flags&HASWIDTH) && op != '?')
590 FAIL("*+ operand could be empty");
591 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
593 if (op == '*' && (flags&SIMPLE))
594 reginsert(STAR, ret, rcstate);
595 else if (op == '*') {
596 /* Emit x* as (x&|), where & means "self". */
597 reginsert(BRANCH, ret, rcstate); /* Either x */
598 regoptail(ret, regnode(BACK,rcstate)); /* and loop */
599 regoptail(ret, ret); /* back */
600 regtail(ret, regnode(BRANCH,rcstate)); /* or */
601 regtail(ret, regnode(NOTHING,rcstate)); /* null. */
602 } else if (op == '+' && (flags&SIMPLE))
603 reginsert(PLUS, ret, rcstate);
604 else if (op == '+') {
605 /* Emit x+ as x(&|), where & means "self". */
606 next = regnode(BRANCH,rcstate); /* Either */
608 regtail(regnode(BACK,rcstate), ret); /* loop back */
609 regtail(next, regnode(BRANCH,rcstate)); /* or */
610 regtail(ret, regnode(NOTHING,rcstate)); /* null. */
611 } else if (op == '?') {
612 /* Emit x? as (x|) */
613 reginsert(BRANCH, ret, rcstate); /* Either x */
614 regtail(ret, regnode(BRANCH,rcstate)); /* or */
615 next = regnode(NOTHING,rcstate); /* null. */
617 regoptail(ret, next);
620 if (ISMULT(*rcstate->regparse))
627 - regatom - the lowest level
629 * Optimization: gobbles an entire sequence of ordinary characters so that
630 * it can turn them into a single node, which is smaller to store and
631 * faster to run. Backslashed characters are exceptions, each becoming a
632 * separate node; the code is simpler that way and it's not worth fixing.
635 regatom(flagp, rcstate)
637 struct regcomp_state *rcstate;
642 *flagp = WORST; /* Tentatively. */
644 switch (*rcstate->regparse++) {
646 ret = regnode(BOL,rcstate);
649 ret = regnode(EOL,rcstate);
652 ret = regnode(ANY,rcstate);
653 *flagp |= HASWIDTH|SIMPLE;
657 register int classend;
659 if (*rcstate->regparse == '^') { /* Complement of range. */
660 ret = regnode(ANYBUT,rcstate);
663 ret = regnode(ANYOF,rcstate);
664 if (*rcstate->regparse == ']' || *rcstate->regparse == '-')
665 regc(*rcstate->regparse++,rcstate);
666 while (*rcstate->regparse != '\0' && *rcstate->regparse != ']') {
667 if (*rcstate->regparse == '-') {
669 if (*rcstate->regparse == ']' || *rcstate->regparse == '\0')
672 clss = UCHARAT(rcstate->regparse-2)+1;
673 classend = UCHARAT(rcstate->regparse);
674 if (clss > classend+1)
675 FAIL("invalid [] range");
676 for (; clss <= classend; clss++)
677 regc((char)clss,rcstate);
681 regc(*rcstate->regparse++,rcstate);
684 if (*rcstate->regparse != ']')
685 FAIL("unmatched []");
687 *flagp |= HASWIDTH|SIMPLE;
691 ret = reg(1, &flags, rcstate);
694 *flagp |= flags&(HASWIDTH|SPSTART);
699 FAIL("internal urp"); /* Supposed to be caught earlier. */
704 FAIL("?+* follows nothing");
707 if (*rcstate->regparse == '\0')
709 ret = regnode(EXACTLY,rcstate);
710 regc(*rcstate->regparse++,rcstate);
712 *flagp |= HASWIDTH|SIMPLE;
719 len = strcspn(rcstate->regparse, META);
721 FAIL("internal disaster");
722 ender = *(rcstate->regparse+len);
723 if (len > 1 && ISMULT(ender))
724 len--; /* Back off clear of ?+* operand. */
728 ret = regnode(EXACTLY,rcstate);
730 regc(*rcstate->regparse++,rcstate);
742 - regnode - emit a node
744 static char * /* Location. */
747 struct regcomp_state *rcstate;
752 ret = rcstate->regcode;
753 if (ret == ®dummy) {
754 rcstate->regsize += 3;
760 *ptr++ = '\0'; /* Null "next" pointer. */
762 rcstate->regcode = ptr;
768 - regc - emit (if appropriate) a byte of code
773 struct regcomp_state *rcstate;
775 if (rcstate->regcode != ®dummy)
776 *rcstate->regcode++ = (char)b;
782 - reginsert - insert an operator in front of already-emitted operand
784 * Means relocating the operand.
787 reginsert(op, opnd, rcstate)
790 struct regcomp_state *rcstate;
794 register char *place;
796 if (rcstate->regcode == ®dummy) {
797 rcstate->regsize += 3;
801 src = rcstate->regcode;
802 rcstate->regcode += 3;
803 dst = rcstate->regcode;
807 place = opnd; /* Op node, where operand used to be. */
814 - regtail - set the next-pointer at the end of a node chain
828 /* Find last node. */
831 temp = regnext(scan);
837 if (OP(scan) == BACK)
841 *(scan+1) = (char)((offset>>8)&0377);
842 *(scan+2) = (char)(offset&0377);
846 - regoptail - regtail on operand of first argument; nop if operandless
853 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
854 if (p == NULL || p == ®dummy || OP(p) != BRANCH)
856 regtail(OPERAND(p), val);
860 * TclRegExec and friends
864 * Global work variables for TclRegExec().
866 struct regexec_state {
867 char *reginput; /* String-input pointer. */
868 char *regbol; /* Beginning of input, for ^ check. */
869 char **regstartp; /* Pointer to startp array. */
870 char **regendp; /* Ditto for endp. */
876 static int regtry _ANSI_ARGS_((regexp *prog, char *string,
877 struct regexec_state *restate));
878 static int regmatch _ANSI_ARGS_((char *prog,
879 struct regexec_state *restate));
880 static int regrepeat _ANSI_ARGS_((char *p,
881 struct regexec_state *restate));
885 void regdump _ANSI_ARGS_((regexp *r));
886 static char *regprop _ANSI_ARGS_((char *op));
890 - TclRegExec - match a regexp against a string
893 TclRegExec(prog, string, start)
894 register regexp *prog;
895 register char *string;
899 struct regexec_state state;
900 struct regexec_state *restate= &state;
903 if (prog == NULL || string == NULL) {
904 TclRegError("NULL parameter");
908 /* Check validity of program. */
909 if (UCHARAT(prog->program) != MAGIC) {
910 TclRegError("corrupted program");
914 /* If there is a "must appear" string, look for it. */
915 if (prog->regmust != NULL) {
917 while ((s = strchr(s, prog->regmust[0])) != NULL) {
918 if (strncmp(s, prog->regmust, (size_t) prog->regmlen)
920 break; /* Found it. */
923 if (s == NULL) /* Not present. */
927 /* Mark beginning of line for ^ . */
928 restate->regbol = start;
930 /* Simplest case: anchored match need be tried only once. */
932 return(regtry(prog, string, restate));
934 /* Messy cases: unanchored match. */
936 if (prog->regstart != '\0')
937 /* We know what char it must start with. */
938 while ((s = strchr(s, prog->regstart)) != NULL) {
939 if (regtry(prog, s, restate))
944 /* We don't -- general case. */
946 if (regtry(prog, s, restate))
948 } while (*s++ != '\0');
955 - regtry - try match at specific point
957 static int /* 0 failure, 1 success */
958 regtry(prog, string, restate)
961 struct regexec_state *restate;
967 restate->reginput = string;
968 restate->regstartp = prog->startp;
969 restate->regendp = prog->endp;
973 for (i = NSUBEXP; i > 0; i--) {
977 if (regmatch(prog->program + 1,restate)) {
978 prog->startp[0] = string;
979 prog->endp[0] = restate->reginput;
986 - regmatch - main matching routine
988 * Conceptually the strategy is simple: check to see whether the current
989 * node matches, call self recursively to see whether the rest matches,
990 * and then act accordingly. In practice we make some effort to avoid
991 * recursion, in particular by going through "ordinary" nodes (that don't
992 * need to know whether the rest of the match failed) by a loop instead of
995 static int /* 0 failure, 1 success */
996 regmatch(prog, restate)
998 struct regexec_state *restate;
1000 register char *scan; /* Current node. */
1001 char *next; /* Next node. */
1005 if (scan != NULL && regnarrate)
1006 fprintf(stderr, "%s(\n", regprop(scan));
1008 while (scan != NULL) {
1011 fprintf(stderr, "%s...\n", regprop(scan));
1013 next = regnext(scan);
1017 if (restate->reginput != restate->regbol) {
1022 if (*restate->reginput != '\0') {
1027 if (*restate->reginput == '\0') {
1030 restate->reginput++;
1034 register char *opnd;
1036 opnd = OPERAND(scan);
1037 /* Inline the first character, for speed. */
1038 if (*opnd != *restate->reginput) {
1042 if (len > 1 && strncmp(opnd, restate->reginput, (size_t) len)
1046 restate->reginput += len;
1050 if (*restate->reginput == '\0'
1051 || strchr(OPERAND(scan), *restate->reginput) == NULL) {
1054 restate->reginput++;
1057 if (*restate->reginput == '\0'
1058 || strchr(OPERAND(scan), *restate->reginput) != NULL) {
1061 restate->reginput++;
1077 register char *save;
1080 no = OP(scan) - OPEN;
1081 save = restate->reginput;
1083 if (regmatch(next,restate)) {
1085 * Don't set startp if some later invocation of the
1086 * same parentheses already has.
1088 if (restate->regstartp[no] == NULL) {
1089 restate->regstartp[no] = save;
1106 register char *save;
1109 no = OP(scan) - CLOSE;
1110 save = restate->reginput;
1112 if (regmatch(next,restate)) {
1114 * Don't set endp if some later
1115 * invocation of the same parentheses
1118 if (restate->regendp[no] == NULL)
1119 restate->regendp[no] = save;
1126 register char *save;
1128 if (OP(next) != BRANCH) { /* No choice. */
1129 next = OPERAND(scan); /* Avoid recursion. */
1132 save = restate->reginput;
1133 if (regmatch(OPERAND(scan),restate))
1135 restate->reginput = save;
1136 scan = regnext(scan);
1137 } while (scan != NULL && OP(scan) == BRANCH);
1144 register char nextch;
1146 register char *save;
1150 * Lookahead to avoid useless match attempts
1151 * when we know what character comes next.
1154 if (OP(next) == EXACTLY)
1155 nextch = *OPERAND(next);
1156 min = (OP(scan) == STAR) ? 0 : 1;
1157 save = restate->reginput;
1158 no = regrepeat(OPERAND(scan),restate);
1160 /* If it could work, try it. */
1161 if (nextch == '\0' || *restate->reginput == nextch)
1162 if (regmatch(next,restate))
1164 /* Couldn't or didn't -- back up. */
1166 restate->reginput = save + no;
1171 return(1); /* Success! */
1173 if (OP(scan) > OPEN && OP(scan) < OPEN+NSUBEXP) {
1175 } else if (OP(scan) > CLOSE && OP(scan) < CLOSE+NSUBEXP) {
1178 TclRegError("memory corruption");
1186 * We get here only if there's trouble -- normally "case END" is
1187 * the terminating point.
1189 TclRegError("corrupted pointers");
1194 - regrepeat - repeatedly match something simple, report how many
1197 regrepeat(p, restate)
1199 struct regexec_state *restate;
1201 register int count = 0;
1202 register char *scan;
1203 register char *opnd;
1205 scan = restate->reginput;
1209 count = strlen(scan);
1213 while (*opnd == *scan) {
1219 while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1225 while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1230 default: /* Oh dear. Called inappropriately. */
1231 TclRegError("internal foulup");
1232 count = 0; /* Best compromise. */
1235 restate->reginput = scan;
1241 - regnext - dig the "next" pointer out of a node
1247 register int offset;
1264 static char *regprop();
1267 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1274 register char op = EXACTLY; /* Arbitrary non-END op. */
1275 register char *next;
1279 while (op != END) { /* While that wasn't END last time... */
1281 printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1283 if (next == NULL) /* Next ptr. */
1286 printf("(%d)", (s-r->program)+(next-s));
1288 if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1289 /* Literal string, where present. */
1290 while (*s != '\0') {
1299 /* Header fields of interest. */
1300 if (r->regstart != '\0')
1301 printf("start `%c' ", r->regstart);
1303 printf("anchored ");
1304 if (r->regmust != NULL)
1305 printf("must have \"%s\"", r->regmust);
1310 - regprop - printable representation of opcode
1317 static char buf[50];
1319 (void) strcpy(buf, ":");
1361 sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
1373 sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1383 if (OP(op) > OPEN && OP(op) < OPEN+NSUBEXP) {
1384 sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
1387 } else if (OP(op) > CLOSE && OP(op) < CLOSE+NSUBEXP) {
1388 sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1391 TclRegError("corrupted opcode");
1396 (void) strcat(buf, p);
1402 * The following is provided for those people who do not have strcspn() in
1403 * their C libraries. They should get off their butts and do something
1404 * about it; at least one public-domain implementation of those (highly
1405 * useful) string routines has been published on Usenet.
1409 * strcspn - find length of initial segment of s1 consisting entirely
1410 * of characters not from s2
1418 register char *scan1;
1419 register char *scan2;
1423 for (scan1 = s1; *scan1 != '\0'; scan1++) {
1424 for (scan2 = s2; *scan2 != '\0';) /* ++ moved down. */
1425 if (*scan1 == *scan2++)
1434 *----------------------------------------------------------------------
1438 * This procedure is invoked by the regexp code when an error
1439 * occurs. It saves the error message so it can be seen by the
1440 * code that called Spencer's code.
1446 * The value of "string" is saved in "errMsg".
1448 *----------------------------------------------------------------------
1452 exp_TclRegError(string)
1453 char *string; /* Error message. */
1465 * end of regexp definitions and code
1469 * stolen from exp_log.c - this function is called from the Expect library
1470 * but the one that the library supplies calls Tcl functions. So we supply
1479 if (exp_is_debugging) {
1480 fprintf(stderr,str);
1481 if (exp_logfile) fprintf(exp_logfile,str);
1486 * expect-specific definitions and code
1490 #include "exp_int.h"
1492 /* exp_glob.c - expect functions for doing glob
1494 * Based on Tcl's glob functions but modified to support anchors and to
1495 * return information about the possibility of future matches
1497 * Modifications by: Don Libes, NIST, 2/6/90
1500 /* The following functions implement expect's glob-style string
1501 * matching Exp_StringMatch allow's implements the unanchored front
1502 * (or conversely the '^') feature. Exp_StringMatch2 does the rest of
1506 /* Exp_StringMatch2 --
1508 * Like Tcl_StringMatch except that
1509 * 1) returns number of characters matched, -1 if failed.
1510 * (Can return 0 on patterns like "" or "$")
1511 * 2) does not require pattern to match to end of string
1512 * 3) much of code is stolen from Tcl_StringMatch
1513 * 4) front-anchor is assumed (Tcl_StringMatch retries for non-front-anchor)
1517 Exp_StringMatch2(string,pattern)
1518 register char *string; /* String. */
1519 register char *pattern; /* Pattern, which may contain
1520 * special characters. */
1523 int match = 0; /* # of chars matched */
1526 /* If at end of pattern, success! */
1527 if (*pattern == 0) {
1531 /* If last pattern character is '$', verify that entire
1532 * string has been matched.
1534 if ((*pattern == '$') && (pattern[1] == 0)) {
1535 if (*string == 0) return(match);
1539 /* Check for a "*" as the next pattern character. It matches
1540 * any substring. We handle this by calling ourselves
1541 * recursively for each postfix of string, until either we
1542 * match or we reach the end of the string.
1545 if (*pattern == '*') {
1549 if (*pattern == 0) {
1550 return(strlen(string)+match); /* DEL */
1552 /* find longest match - switched to this on 12/31/93 */
1553 head_len = strlen(string); /* length before tail */
1554 tail = string + head_len;
1555 while (head_len >= 0) {
1558 if (-1 != (rc = Exp_StringMatch2(tail, pattern))) {
1559 return rc + match + head_len; /* DEL */
1564 return -1; /* DEL */
1568 * after this point, all patterns must match at least one
1569 * character, so check this
1572 if (*string == 0) return -1;
1574 /* Check for a "?" as the next pattern character. It matches
1575 * any single character.
1578 if (*pattern == '?') {
1582 /* Check for a "[" as the next pattern character. It is followed
1583 * by a list of characters that are acceptable, or by a range
1584 * (two characters separated by "-").
1587 if (*pattern == '[') {
1590 if ((*pattern == ']') || (*pattern == 0)) {
1591 return -1; /* was 0; DEL */
1593 if (*pattern == *string) {
1596 if (pattern[1] == '-') {
1599 return -1; /* DEL */
1601 if ((*pattern <= *string) && (c2 >= *string)) {
1604 if ((*pattern >= *string) && (c2 <= *string)) {
1612 while (*pattern != ']') {
1613 if (*pattern == 0) {
1622 /* If the next pattern character is backslash, strip it off
1623 * so we do exact matching on the character that follows.
1626 if (*pattern == '\\') {
1628 if (*pattern == 0) {
1633 /* There's no special character. Just make sure that the next
1634 * characters of each string match.
1637 if (*pattern != *string) {
1641 thisCharOK: pattern += 1;
1649 int /* returns # of chars that matched */
1650 Exp_StringMatch(string, pattern,offset)
1653 int *offset; /* offset from beginning of string where pattern matches */
1656 int sm; /* count of chars matched or -1 */
1662 if (pattern[0] == '^') {
1665 } else if (pattern[0] == '*') {
1670 * test if pattern matches in initial position.
1671 * This handles front-anchor and 1st iteration of non-front-anchor.
1672 * Note that 1st iteration must be tried even if string is empty.
1675 sm = Exp_StringMatch2(string,pattern);
1676 if (sm >= 0) return(sm);
1678 if (caret) return -1;
1679 if (star) return -1;
1681 if (*string == '\0') return -1;
1683 for (s = string+1;*s;s++) {
1684 sm = Exp_StringMatch2(s,pattern);
1694 #define EXP_MATCH_MAX 2000
1696 char *exp_buffer = 0;
1697 char *exp_buffer_end = 0;
1698 char *exp_match = 0;
1699 char *exp_match_end = 0;
1700 int exp_match_max = EXP_MATCH_MAX; /* bytes */
1701 int exp_full_buffer = FALSE; /* don't return on full buffer */
1702 int exp_remove_nulls = TRUE;
1703 int exp_timeout = 10; /* seconds */
1704 int exp_pty_timeout = 5; /* seconds - see CRAY below */
1705 int exp_autoallocpty = TRUE; /* if TRUE, we do allocation */
1706 int exp_pty[2]; /* master is [0], slave is [1] */
1708 char *exp_stty_init = 0; /* initial stty args */
1709 int exp_ttycopy = TRUE; /* copy tty parms from /dev/tty */
1710 int exp_ttyinit = TRUE; /* set tty parms to sane state */
1711 int exp_console = FALSE; /* redirect console */
1712 void (*exp_child_exec_prelude)() = 0;
1713 void (*exp_close_in_child)() = 0;
1715 #ifdef HAVE_SIGLONGJMP
1716 sigjmp_buf exp_readenv; /* for interruptable read() */
1718 jmp_buf exp_readenv; /* for interruptable read() */
1719 #endif /* HAVE_SIGLONGJMP */
1721 int exp_reading = FALSE; /* whether we can longjmp or not */
1723 int exp_is_debugging = FALSE;
1724 FILE *exp_debugfile = 0;
1726 FILE *exp_logfile = 0;
1727 int exp_logfile_all = FALSE; /* if TRUE, write log of all interactions */
1728 int exp_loguser = TRUE; /* if TRUE, user sees interactions on stdout */
1731 char *exp_printify();
1732 int exp_getptymaster();
1733 int exp_getptyslave();
1735 #define sysreturn(x) return(errno = x, -1)
1737 void exp_init_pty();
1740 The following functions are linked from the Tcl library. They
1741 don't cause anything else in the library to be dragged in, so it
1742 shouldn't cause any problems (e.g., bloat).
1744 The functions are relatively small but painful enough that I don't care
1745 to recode them. You may, if you absolutely want to get rid of any
1749 static unsigned int bufsiz = 2*EXP_MATCH_MAX;
1754 char *buffer; /* buffer of matchable chars */
1755 char *buffer_end; /* one beyond end of matchable chars */
1756 char *match_end; /* one beyond end of matched string */
1757 int msize; /* size of allocate space */
1758 /* actual size is one larger for null */
1761 static int fd_alloc_max = -1; /* max fd allocated */
1763 /* translate fd or fp to fd */
1769 if (fd == -1) return(fs + fileno(fp));
1770 else return(fs + fd);
1779 struct f *newfs; /* temporary, so we don't lose old fs */
1781 if (fd > fd_alloc_max) {
1782 if (!fs) { /* no fd's yet allocated */
1783 newfs = (struct f *)malloc(sizeof(struct f)*(fd+1));
1785 } else { /* enlarge fd table */
1786 newfs = (struct f *)realloc((char *)fs,sizeof(struct f)*(fd+1));
1787 low = fd_alloc_max+1;
1791 for (i = low; i <= fd_alloc_max; i++) { /* init new entries */
1792 fs[i].valid = FALSE;
1800 fp->buffer = malloc((unsigned)(bufsiz+1));
1801 if (!fp->buffer) return 0;
1805 fp->buffer_end = fp->buffer;
1806 fp->match_end = fp->buffer;
1816 /* required on BSD side of MIPS OS <jmsellen@watdragon.waterloo.edu> */
1817 # include <sysv/sys.s>
1818 syscall(SYS_setpgrp);
1824 (void) setpgrp(0,0);
1828 /* returns fd of master side of pty */
1830 exp_spawnv(file,argv)
1832 char *argv[]; /* some compiler complains about **argv? */
1835 int errorfd; /* place to stash fileno(stderr) in child */
1836 /* while we're setting up new stderr */
1844 int slave_write_ioctls = 1;
1845 /* by default, slave will be write-ioctled this many times */
1848 static int first_time = TRUE;
1854 expDiagLogPtrSet(expDiagLogU);
1857 * TIP 27; It is unclear why this code produces a
1858 * warning. The equivalent code in exp_main_sub.c
1859 * (line 512) does not generate a warning !
1862 expErrnoMsgSet(Tcl_ErrnoMsg);
1865 if (!file || !argv) sysreturn(EINVAL);
1866 if (!argv[0] || strcmp(file,argv[0])) {
1867 exp_debuglog("expect: warning: file (%s) != argv[0] (%s)\n",
1869 argv[0]?argv[0]:"");
1873 /* any extraneous ioctl's that occur in slave must be accounted for
1874 when trapping, see below in child half of fork */
1875 #if defined(TIOCSCTTY) && !defined(CIBAUD) && !defined(sun) && !defined(hp9000s300)
1876 slave_write_ioctls++;
1878 #endif /*PTYTRAP_DIES*/
1880 if (exp_autoallocpty) {
1881 if (0 > (exp_pty[0] = exp_getptymaster())) sysreturn(ENODEV);
1883 fcntl(exp_pty[0],F_SETFD,1); /* close on exec */
1885 exp_slave_control(exp_pty[0],1);*/
1888 if (!fd_new(exp_pty[0])) {
1893 if (-1 == (pipe(sync_fds))) {
1896 if (-1 == (pipe(sync2_fds))) {
1902 if (-1 == pipe(status_pipe)) {
1905 close(sync2_fds[0]);
1906 close(sync2_fds[1]);
1910 if ((exp_pid = fork()) == -1) return(-1);
1914 close(sync2_fds[0]);
1915 close(status_pipe[1]);
1917 if (!exp_autoallocpty) close(exp_pty[1]);
1921 if (exp_autoallocpty) {
1922 /* trap initial ioctls in a feeble attempt to not */
1923 /* block the initially. If the process itself */
1924 /* ioctls /dev/tty, such blocks will be trapped */
1925 /* later during normal event processing */
1927 while (slave_write_ioctls) {
1930 cc = exp_wait_for_slave_open(exp_pty[0]);
1931 #if defined(TIOCSCTTY) && !defined(CIBAUD) && !defined(sun) && !defined(hp9000s300)
1932 if (cc == TIOCSCTTY) slave_write_ioctls = 0;
1934 if (cc & IOC_IN) slave_write_ioctls--;
1935 else if (cc == -1) {
1936 printf("failed to trap slave pty");
1942 #endif /*PTYTRAP_DIES*/
1945 * wait for slave to initialize pty before allowing
1946 * user to send to it
1949 exp_debuglog("parent: waiting for sync byte\r\n");
1950 cc = read(sync_fds[0],&sync_byte,1);
1952 exp_errorlog("parent sync byte read: %s\r\n",Tcl_ErrnoMsg(errno));
1956 /* turn on detection of eof */
1957 exp_slave_control(exp_pty[0],1);
1960 * tell slave to go on now now that we have initialized pty
1963 exp_debuglog("parent: telling child to go ahead\r\n");
1964 cc = write(sync2_fds[1]," ",1);
1966 exp_errorlog("parent sync byte write: %s\r\n",Tcl_ErrnoMsg(errno));
1970 exp_debuglog("parent: now unsynchronized from child\r\n");
1973 close(sync2_fds[1]);
1975 /* see if child's exec worked */
1978 switch (read(status_pipe[0],&child_errno,sizeof child_errno)) {
1980 if (errno == EINTR) goto retry;
1981 /* well it's not really the child's errno */
1982 /* but it can be treated that way */
1983 child_errno = errno;
1986 /* child's exec succeeded */
1990 /* child's exec failed; err contains exec's errno */
1991 waitpid(exp_pid, NULL, 0);
1992 errno = child_errno;
1995 close(status_pipe[0]);
2000 * child process - do not return from here! all errors must exit()
2004 close(sync2_fds[1]);
2005 close(status_pipe[0]);
2006 fcntl(status_pipe[1],F_SETFD,1); /* close on exec */
2009 (void) close(exp_pty[0]);
2012 /* ultrix (at least 4.1-2) fails to obtain controlling tty if setsid */
2013 /* is called. setpgrp works though. */
2014 #if defined(POSIX) && !defined(ultrix)
2032 ttyfd = open("/dev/tty", O_RDWR);
2034 (void) ioctl(ttyfd, TIOCNOTTY, (char *)0);
2035 (void) close(ttyfd);
2037 #endif /* TIOCNOTTY */
2040 #endif /* DO_SETSID */
2042 /* save error fd while we're setting up new one */
2043 errorfd = fcntl(2,F_DUPFD,3);
2044 /* and here is the macro to restore it */
2045 #define restore_error_fd {close(2);fcntl(errorfd,F_DUPFD,2);}
2047 if (exp_autoallocpty) {
2053 /* since we closed fd 0, open of pty slave must return fd 0 */
2055 if (0 > (exp_pty[1] = exp_getptyslave(exp_ttycopy,exp_ttyinit,
2058 fprintf(stderr,"open(slave pty): %s\n",Tcl_ErrnoMsg(errno));
2062 if (exp_pty[1] != 0) {
2064 fprintf(stderr,"exp_getptyslave: slave = %d but expected 0\n",
2069 if (exp_pty[1] != 0) {
2070 close(0); fcntl(exp_pty[1],F_DUPFD,0);
2072 close(1); fcntl(0,F_DUPFD,1);
2073 close(2); fcntl(0,F_DUPFD,1);
2079 /* The test for hpux may have to be more specific. In particular, the */
2080 /* code should be skipped on the hp9000s300 and hp9000s720 (but there */
2081 /* is no documented define for the 720!) */
2083 #if defined(TIOCSCTTY) && !defined(sun) && !defined(hpux)
2084 /* 4.3+BSD way to acquire controlling terminal */
2085 /* according to Stevens - Adv. Prog..., p 642 */
2086 #ifdef __QNX__ /* posix in general */
2087 if (tcsetct(0, getpid()) == -1) {
2089 expErrorLog("failed to get controlling terminal using TIOCSCTTY");
2093 (void) ioctl(0,TIOCSCTTY,(char *)0);
2094 /* ignore return value - on some systems, it is defined but it
2095 * fails and it doesn't seem to cause any problems. Or maybe
2096 * it works but returns a bogus code. Noone seems to be able
2097 * to explain this to me. The systems are an assortment of
2098 * different linux systems (and FreeBSD 2.5), RedHat 5.2 and
2106 (void) ioctl(0,TCSETCTTY,0);
2108 if (open("/dev/tty", O_RDWR) < 0) {
2110 fprintf(stderr,"open(/dev/tty): %s\r\n",Tcl_ErrnoMsg(errno));
2117 setptyutmp(); /* create a utmp entry */
2119 /* _CRAY2 code from Hal Peterson <hrp@cray.com>, Cray Research, Inc. */
2122 * Interpose a process between expect and the spawned child to
2123 * keep the slave side of the pty open to allow time for expect
2124 * to read the last output. This is a workaround for an apparent
2125 * bug in the Unicos pty driver on Cray-2's under Unicos 6.0 (at
2128 if ((pid = fork()) == -1) {
2130 fprintf(stderr,"second fork: %s\r\n",Tcl_ErrnoMsg(errno));
2135 /* Intermediate process. */
2140 /* How long should we wait? */
2141 timeout = exp_pty_timeout;
2143 /* Let the spawned process run to completion. */
2144 while (wait(&status) < 0 && errno == EINTR)
2147 /* Wait for the pty to clear. */
2150 /* Duplicate the spawned process's status. */
2151 if (WIFSIGNALED(status))
2152 kill(getpid(), WTERMSIG(status));
2154 /* The kill may not have worked, but this will. */
2155 exit(WEXITSTATUS(status));
2164 if ((fd = open("/dev/console", O_RDONLY)) == -1) {
2166 fprintf(stderr, "spawn %s: cannot open console, check permissions of /dev/console\n",argv[0]);
2169 if (ioctl(fd, SRIOCSREDIR, 0) == -1) {
2171 fprintf(stderr, "spawn %s: cannot redirect console, check permissions of /dev/console\n",argv[0]);
2178 if (ioctl(0,TIOCCONS,(char *)&on) == -1) {
2180 fprintf(stderr, "spawn %s: cannot open console, check permissions of /dev/console\n",argv[0]);
2183 #endif /* TIOCCONS */
2186 /* tell parent that we are done setting up pty */
2187 /* The actual char sent back is irrelevant. */
2189 /* exp_debuglog("child: telling parent that pty is initialized\r\n");*/
2190 cc = write(sync_fds[1]," ",1);
2193 fprintf(stderr,"child: sync byte write: %s\r\n",Tcl_ErrnoMsg(errno));
2198 /* wait for master to let us go on */
2199 cc = read(sync2_fds[0],&sync_byte,1);
2202 exp_errorlog("child: sync byte read: %s\r\n",Tcl_ErrnoMsg(errno));
2205 close(sync2_fds[0]);
2207 /* exp_debuglog("child: now unsynchronized from parent\r\n"); */
2209 /* (possibly multiple) masters are closed automatically due to */
2210 /* earlier fcntl(,,CLOSE_ON_EXEC); */
2212 /* just in case, allow user to explicitly close other files */
2213 if (exp_close_in_child) (*exp_close_in_child)();
2215 /* allow user to do anything else to child */
2216 if (exp_child_exec_prelude) (*exp_child_exec_prelude)();
2218 (void) execvp(file,argv);
2220 /* Unfortunately, by now we've closed fd's to stderr, logfile
2221 * and debugfile. The only reasonable thing to do is to send
2222 * *back the error as part of the program output. This will
2223 * be *picked up in an expect or interact command.
2226 write(status_pipe[1], &errno, sizeof errno);
2231 /* returns fd of master side of pty */
2234 exp_spawnl TCL_VARARGS_DEF(char *,arg1)
2235 /*exp_spawnl(va_alist)*/
2238 va_list args; /* problematic line here */
2242 arg = TCL_VARARGS_START(char *,arg1,args);
2245 arg = va_arg(args,char *);
2249 if (i == 0) sysreturn(EINVAL);
2250 if (!(argv = (char **)malloc((i+1)*sizeof(char *)))) sysreturn(ENOMEM);
2251 argv[0] = TCL_VARARGS_START(char *,arg1,args);
2254 argv[i] = va_arg(args,char *);
2255 if (!argv[i]) break;
2257 i = exp_spawnv(argv[0],argv+1);
2262 /* allow user-provided fd to be passed to expect funcs */
2274 /* remove nulls from s. Initially, the number of chars in s is c, */
2275 /* not strlen(s). This count does not include the trailing null. */
2276 /* returns number of nulls removed. */
2282 char *s2 = s; /* points to place in original string to put */
2283 /* next non-null character */
2287 for (i=0;i<c;i++,s++) {
2292 if (count) *s2 = *s;
2298 static int i_read_errno;/* place to save errno, if i_read() == -1, so it
2299 doesn't get overwritten before we get to read it */
2304 int n; /* signal number, unused by us */
2307 signal(SIGALRM,sigalarm_handler);
2310 #ifdef HAVE_SIGLONGJMP
2311 siglongjmp(exp_readenv,1);
2313 longjmp(exp_readenv,1);
2314 #endif /* HAVE_SIGLONGJMP */
2317 /* interruptable read */
2319 i_read(fd,fp,buffer,length,timeout)
2328 /* since setjmp insists on returning 1 upon longjmp(,0), */
2329 /* longjmp(,2 (EXP_RESTART)) instead. */
2331 /* no need to set alarm if -1 (infinite) or 0 (poll with */
2332 /* guaranteed data) */
2334 if (timeout > 0) alarm(timeout);
2336 /* restart read if setjmp returns 0 (first time) or 2 (EXP_RESTART). */
2337 /* abort if setjmp returns 1 (EXP_ABORT). */
2338 #ifdef HAVE_SIGLONGJMP
2339 if (EXP_ABORT != sigsetjmp(exp_readenv,1)) {
2341 if (EXP_ABORT != setjmp(exp_readenv)) {
2342 #endif /* HAVE_SIGLONGJMP */
2348 /*fprintf(stderr,"<<EOF>>",c);fflush(stderr);*/
2349 if (feof(fp)) cc = 0;
2352 /*fprintf(stderr,"<<%c>>",c);fflush(stderr);*/
2357 #ifndef HAVE_PTYTRAP
2358 cc = read(fd,buffer,length);
2360 # include <sys/ptyio.h>
2370 if (-1 == (cc = select(fd+1,
2371 (SELECT_MASK_TYPE *)&rdrs,
2372 (SELECT_MASK_TYPE *)0,
2373 (SELECT_MASK_TYPE *)&excep,
2374 (struct timeval *)0))) {
2375 /* window refreshes trigger EINTR, ignore */
2376 if (errno == EINTR) goto restart;
2378 if (FD_ISSET(fd,&rdrs)) {
2379 cc = read(fd,buffer,length);
2380 } else if (FD_ISSET(fd,&excep)) {
2381 struct request_info ioctl_info;
2382 ioctl(fd,TIOCREQCHECK,&ioctl_info);
2383 if (ioctl_info.request == TIOCCLOSE) {
2384 cc = 0; /* indicate eof */
2386 ioctl(fd, TIOCREQSET, &ioctl_info);
2387 /* presumably, we trapped an open here */
2391 #endif /* HAVE_PTYTRAP */
2394 /* can't get fread to return early! */
2396 if (!(cc = fread(buffer,1,length,fp))) {
2397 if (ferror(fp)) cc = -1;
2401 i_read_errno = errno; /* errno can be overwritten by the */
2402 /* time we return */
2404 exp_reading = FALSE;
2406 if (timeout > 0) alarm(0);
2410 /* I tried really hard to make the following two functions share the code */
2411 /* that makes the ecase array, but I kept running into a brick wall when */
2412 /* passing var args into the funcs and then again into a make_cases func */
2413 /* I would very much appreciate it if someone showed me how to do it right */
2415 /* takes triplets of args, with a final "exp_last" arg */
2416 /* triplets are type, pattern, and then int to return */
2417 /* returns negative value if error (or EOF/timeout) occurs */
2418 /* some negative values can also have an associated errno */
2420 /* the key internal variables that this function depends on are:
2426 expectv(fd,fp,ecases)
2429 struct exp_case *ecases;
2431 int cc = 0; /* number of chars returned in a single read */
2432 int buf_length; /* numbers of chars in exp_buffer */
2433 int old_length; /* old buf_length */
2434 int first_time = TRUE; /* force old buffer to be tested before */
2435 /* additional reads */
2436 int polled = 0; /* true if poll has caused read() to occur */
2438 struct exp_case *ec; /* points to current ecase */
2440 time_t current_time; /* current time (when we last looked)*/
2441 time_t end_time; /* future time at which to give up */
2442 int remtime; /* remaining time in timeout */
2447 #define return_normally(x) {return_val = x; goto cleanup;}
2448 #define return_errno(x) {sys_error = x; goto cleanup;}
2451 if (!f) return_errno(ENOMEM);
2453 exp_buffer = f->buffer;
2454 exp_buffer_end = f->buffer_end;
2455 exp_match_end = f->match_end;
2457 buf_length = exp_buffer_end - exp_match_end;
2460 * take end of previous match to end of buffer
2461 * and copy to beginning of buffer
2463 memmove(exp_buffer,exp_match_end,buf_length);
2465 exp_buffer_end = exp_buffer + buf_length;
2466 *exp_buffer_end = '\0';
2468 if (!ecases) return_errno(EINVAL);
2470 /* compile if necessary */
2471 for (ec=ecases;ec->type != exp_end;ec++) {
2472 if ((ec->type == exp_regexp) && !ec->re) {
2473 TclRegError((char *)0);
2474 if (!(ec->re = TclRegComp(ec->pattern))) {
2475 fprintf(stderr,"regular expression %s is bad: %s",ec->pattern,TclGetRegError());
2476 return_errno(EINVAL);
2481 /* get the latest buffer size. Double the user input for two */
2482 /* reasons. 1) Need twice the space in case the match */
2483 /* straddles two bufferfuls, 2) easier to hack the division by */
2484 /* two when shifting the buffers later on */
2486 bufsiz = 2*exp_match_max;
2487 if (f->msize != bufsiz) {
2488 /* if truncated, forget about some data */
2489 if (buf_length > bufsiz) {
2490 /* copy end of buffer down */
2492 /* copy one less than what buffer can hold to avoid */
2493 /* triggering buffer-full handling code below */
2494 /* which will immediately dump the first half */
2496 memmove(exp_buffer,exp_buffer+(buf_length - bufsiz)+1,
2498 buf_length = bufsiz-1;
2500 exp_buffer = realloc(exp_buffer,bufsiz+1);
2501 if (!exp_buffer) return_errno(ENOMEM);
2502 exp_buffer[buf_length] = '\0';
2503 exp_buffer_end = exp_buffer + buf_length;
2507 /* some systems (i.e., Solaris) require fp be flushed when switching */
2508 /* directions - do this again afterwards */
2509 if (fd == -1) fflush(fp);
2511 if (exp_timeout != -1) signal(SIGALRM,sigalarm_handler);
2513 /* remtime and current_time updated at bottom of loop */
2514 remtime = exp_timeout;
2516 time(¤t_time);
2517 end_time = current_time + remtime;
2520 /* when buffer fills, copy second half over first and */
2521 /* continue, so we can do matches over multiple buffers */
2522 if (buf_length == bufsiz) {
2523 int first_half, second_half;
2525 if (exp_full_buffer) {
2526 exp_debuglog("expect: full buffer\r\n");
2527 exp_match = exp_buffer;
2528 exp_match_end = exp_buffer + buf_length;
2529 exp_buffer_end = exp_match_end;
2530 return_normally(EXP_FULLBUFFER);
2532 first_half = bufsiz/2;
2533 second_half = bufsiz - first_half;
2535 memcpy(exp_buffer,exp_buffer+first_half,second_half);
2536 buf_length = second_half;
2537 exp_buffer_end = exp_buffer + second_half;
2541 * always check first if pattern is already in buffer
2550 * we should timeout if either
2551 * 1) exp_timeout > remtime <= 0 (normal)
2552 * 2) exp_timeout == 0 and we have polled at least once
2555 if (((exp_timeout > remtime) && (remtime <= 0)) ||
2556 ((exp_timeout == 0) && polled)) {
2557 exp_debuglog("expect: timeout\r\n");
2558 exp_match_end = exp_buffer;
2559 return_normally(EXP_TIMEOUT);
2562 /* remember that we have actually checked at least once */
2567 bufsiz - buf_length,
2571 exp_debuglog("expect: eof\r\n");
2572 return_normally(EXP_EOF); /* normal EOF */
2573 } else if (cc == -1) { /* abnormal EOF */
2574 /* ptys produce EIO upon EOF - sigh */
2575 if (i_read_errno == EIO) {
2576 /* convert to EOF indication */
2577 exp_debuglog("expect: eof\r\n");
2578 return_normally(EXP_EOF);
2580 exp_debuglog("expect: error (errno = %d)\r\n",i_read_errno);
2581 return_errno(i_read_errno);
2582 } else if (cc == -2) {
2583 exp_debuglog("expect: timeout\r\n");
2584 exp_match_end = exp_buffer;
2585 return_normally(EXP_TIMEOUT);
2588 old_length = buf_length;
2590 exp_buffer_end += buf_length;
2592 if (exp_logfile_all || (exp_loguser && exp_logfile)) {
2593 fwrite(exp_buffer + old_length,1,cc,exp_logfile);
2595 if (exp_loguser) fwrite(exp_buffer + old_length,1,cc,stdout);
2596 if (exp_debugfile) fwrite(exp_buffer + old_length,1,cc,exp_debugfile);
2598 /* if we wrote to any logs, flush them */
2599 if (exp_debugfile) fflush(exp_debugfile);
2602 if (exp_logfile) fflush(exp_logfile);
2605 /* remove nulls from input, so we can use C-style strings */
2606 /* doing it here lets them be sent to the screen, just */
2607 /* in case they are involved in formatting operations */
2608 if (exp_remove_nulls) {
2609 buf_length -= rm_nulls(exp_buffer + old_length, cc);
2611 /* cc should be decremented as well, but since it will not */
2612 /* be used before being set again, there is no need */
2613 exp_buffer_end = exp_buffer + buf_length;
2614 *exp_buffer_end = '\0';
2615 exp_match_end = exp_buffer;
2618 exp_debuglog("expect: does {%s} match ",exp_printify(exp_buffer));
2619 /* pattern supplied */
2620 for (ec=ecases;ec->type != exp_end;ec++) {
2623 exp_debuglog("{%s}? ",exp_printify(ec->pattern));
2624 if (ec->type == exp_glob) {
2626 matched = Exp_StringMatch(exp_buffer,ec->pattern,&offset);
2628 exp_match = exp_buffer + offset;
2629 exp_match_end = exp_match + matched;
2631 } else if (ec->type == exp_exact) {
2632 char *p = strstr(exp_buffer,ec->pattern);
2636 exp_match_end = p + strlen(ec->pattern);
2638 } else if (ec->type == exp_null) {
2641 for (p=exp_buffer;p<exp_buffer_end;p++) {
2645 exp_match_end = p+1;
2649 TclRegError((char *)0);
2650 if (TclRegExec(ec->re,exp_buffer,exp_buffer)) {
2652 exp_match = ec->re->startp[0];
2653 exp_match_end = ec->re->endp[0];
2654 } else if (TclGetRegError()) {
2655 fprintf(stderr,"r.e. match (pattern %s) failed: %s",ec->pattern,TclGetRegError());
2659 if (matched != -1) {
2660 exp_debuglog("yes\nexp_buffer is {%s}\n",
2661 exp_printify(exp_buffer));
2662 return_normally(ec->value);
2663 } else exp_debuglog("no\n");
2667 * Update current time and remaining time.
2668 * Don't bother if we are waiting forever or polling.
2670 if (exp_timeout > 0) {
2671 time(¤t_time);
2672 remtime = end_time - current_time;
2676 f->buffer = exp_buffer;
2677 f->buffer_end = exp_buffer_end;
2678 f->match_end = exp_match_end;
2680 /* some systems (i.e., Solaris) require fp be flushed when switching */
2681 /* directions - do this before as well */
2682 if (fd == -1) fflush(fp);
2692 exp_fexpectv(fp,ecases)
2694 struct exp_case *ecases;
2696 return(expectv(-1,fp,ecases));
2700 exp_expectv(fd,ecases)
2702 struct exp_case *ecases;
2704 return(expectv(fd,(FILE *)0,ecases));
2709 exp_expectl TCL_VARARGS_DEF(int,arg1)
2710 /*exp_expectl(va_alist)*/
2715 struct exp_case *ec, *ecases;
2719 fd = TCL_VARARGS_START(int,arg1,args);
2720 /* va_start(args);*/
2721 /* fd = va_arg(args,int);*/
2722 /* first just count the arg sets */
2724 type = va_arg(args,enum exp_type);
2725 if (type == exp_end) break;
2727 /* Ultrix 4.2 compiler refuses enumerations comparison!? */
2728 if ((int)type < 0 || (int)type >= (int)exp_bogus) {
2729 fprintf(stderr,"bad type (set %d) in exp_expectl\n",i);
2733 va_arg(args,char *); /* COMPUTED BUT NOT USED */
2734 if (type == exp_compiled) {
2735 va_arg(args,regexp *); /* COMPUTED BUT NOT USED */
2737 va_arg(args,int); /* COMPUTED BUT NOT USED*/
2741 if (!(ecases = (struct exp_case *)
2742 malloc((1+i)*sizeof(struct exp_case))))
2745 /* now set up the actual cases */
2746 fd = TCL_VARARGS_START(int,arg1,args);
2748 /*va_arg(args,int);*/ /*COMPUTED BUT NOT USED*/
2749 for (ec=ecases;;ec++) {
2750 ec->type = va_arg(args,enum exp_type);
2751 if (ec->type == exp_end) break;
2752 ec->pattern = va_arg(args,char *);
2753 if (ec->type == exp_compiled) {
2754 ec->re = va_arg(args,regexp *);
2758 ec->value = va_arg(args,int);
2761 i = expectv(fd,(FILE *)0,ecases);
2763 for (ec=ecases;ec->type != exp_end;ec++) {
2764 /* free only if regexp and we compiled it for user */
2765 if (ec->type == exp_regexp) {
2766 free((char *)ec->re);
2769 free((char *)ecases);
2774 exp_fexpectl TCL_VARARGS_DEF(FILE *,arg1)
2775 /*exp_fexpectl(va_alist)*/
2780 struct exp_case *ec, *ecases;
2784 fp = TCL_VARARGS_START(FILE *,arg1,args);
2786 /*fp = va_arg(args,FILE *);*/
2787 /* first just count the arg-pairs */
2789 type = va_arg(args,enum exp_type);
2790 if (type == exp_end) break;
2792 /* Ultrix 4.2 compiler refuses enumerations comparison!? */
2793 if ((int)type < 0 || (int)type >= (int)exp_bogus) {
2794 fprintf(stderr,"bad type (set %d) in exp_expectl\n",i);
2798 va_arg(args,char *); /* COMPUTED BUT NOT USED */
2799 if (type == exp_compiled) {
2800 va_arg(args,regexp *); /* COMPUTED BUT NOT USED */
2802 va_arg(args,int); /* COMPUTED BUT NOT USED*/
2806 if (!(ecases = (struct exp_case *)
2807 malloc((1+i)*sizeof(struct exp_case))))
2812 va_arg(args,FILE *); /*COMPUTED, BUT NOT USED*/
2814 (void) TCL_VARARGS_START(FILE *,arg1,args);
2816 for (ec=ecases;;ec++) {
2817 ec->type = va_arg(args,enum exp_type);
2818 if (ec->type == exp_end) break;
2819 ec->pattern = va_arg(args,char *);
2820 if (ec->type == exp_compiled) {
2821 ec->re = va_arg(args,regexp *);
2825 ec->value = va_arg(args,int);
2828 i = expectv(-1,fp,ecases);
2830 for (ec=ecases;ec->type != exp_end;ec++) {
2831 /* free only if regexp and we compiled it for user */
2832 if (ec->type == exp_regexp) {
2833 free((char *)ec->re);
2836 free((char *)ecases);
2840 /* like popen(3) but works in both directions */
2848 if (0 > (ec = exp_spawnl("sh","sh","-c",program,(char *)0))) return(0);
2849 if (!(fp = fdopen(ec,"r+"))) return(0);
2850 setbuf(fp,(char *)0);
2863 /* presumably, no stderr, so don't bother with error message */
2864 if (exp_disconnected) sysreturn(EALREADY);
2865 exp_disconnected = TRUE;
2867 freopen("/dev/null","r",stdin);
2868 freopen("/dev/null","w",stdout);
2869 freopen("/dev/null","w",stderr);
2875 /* put process in our own pgrp, and lose controlling terminal */
2877 signal(SIGHUP,SIG_IGN);
2878 if (fork()) exit(0); /* first child exits (as per Stevens, */
2879 /* UNIX Network Programming, p. 79-80) */
2880 /* second child process continues as daemon */
2883 /* Pyramid lacks this defn */
2885 ttyfd = open("/dev/tty", O_RDWR);
2887 /* zap controlling terminal if we had one */
2888 (void) ioctl(ttyfd, TIOCNOTTY, (char *)0);
2889 (void) close(ttyfd);
2891 #endif /* TIOCNOTTY */
2897 /* send to log if open and debugging enabled */
2898 /* send to stderr if debugging enabled */
2899 /* use this function for recording unusual things in the log */
2902 exp_debuglog TCL_VARARGS_DEF(char *,arg1)
2907 fmt = TCL_VARARGS_START(char *,arg1,args);
2908 if (exp_debugfile) vfprintf(exp_debugfile,fmt,args);
2909 if (exp_is_debugging) {
2910 vfprintf(stderr,fmt,args);
2911 if (exp_logfile) vfprintf(exp_logfile,fmt,args);
2918 /* send to log if open */
2919 /* send to stderr */
2920 /* use this function for error conditions */
2923 exp_errorlog TCL_VARARGS_DEF(char *,arg1)
2928 fmt = TCL_VARARGS_START(char *,arg1,args);
2929 vfprintf(stderr,fmt,args);
2930 if (exp_debugfile) vfprintf(exp_debugfile,fmt,args);
2931 if (exp_logfile) vfprintf(exp_logfile,fmt,args);
2941 static int destlen = 0;
2942 static char *dest = 0;
2943 char *d; /* ptr into dest */
2946 if (s == 0) return("<null>");
2948 /* worst case is every character takes 4 to printify */
2949 need = strlen(s)*4 + 1;
2950 if (need > destlen) {
2951 if (dest) ckfree(dest);
2952 dest = ckalloc(need);
2956 for (d = dest;*s;s++) {
2958 strcpy(d,"\\r"); d += 2;
2959 } else if (*s == '\n') {
2960 strcpy(d,"\\n"); d += 2;
2961 } else if (*s == '\t') {
2962 strcpy(d,"\\t"); d += 2;
2963 } else if (isascii(*s) && isprint(*s)) {
2966 sprintf(d,"\\x%02x",*s & 0xff); d += 4;