Beispiel #1
0
/*
 *	look for a process beginning on this line
 */
static bool
isproc(char *s)
{
    pname[0] = '\0';
    if (!l_toplex || blklevel == 0)
	if (expmatch(s, l_prcbeg, pname) != NULL) {
	    return (true);
	}
    return (false);
}
Beispiel #2
0
/*
 *	look for a process beginning on this line
 */
static boolean
isproc(char *s)
{
    pname[0] = '\0';
    if (!l_toplex || blklevel == 0)
	if (expmatch (s, l_prcbeg, pname) != NIL) {
	    return (TRUE);
	}
    return (FALSE);
}
Beispiel #3
0
main()
{
    char reg[132];
    char *ireg;
    char str[132];
    char *match;
    char matstr[132];
    char c;

    while (1) {
	printf ("\nexpr: ");
	scanf ("%s", reg);
	ireg = convexp(reg);
	match = ireg;
	while(*match) {
	    switch (*match) {

	    case '\\':
	    case '(':
	    case ')':
	    case '|':
		printf ("%c", *match);
		break;

	    default:
		if (isalnum(*match))
		    printf("%c", *match);
		else
		    printf ("<%03o>", *match);
		break;
	    }
	    match++;
	}
	printf("\n");
	getchar();
	while(1) {
	    printf ("string: ");
	    match = str;
	    while ((c = getchar()) != '\n')
		*match++ = c;
	    *match = 0;
	    if (str[0] == '#')
		break;
	    matstr[0] = 0;
	    _start = str;
	    _escaped = 0;
	    match = expmatch (str, ireg, matstr);
	    if (match == 0)
		printf ("FAILED\n");
	    else
		printf ("match\nmatstr = %s\n", matstr);
	}

    }
}
Beispiel #4
0
static void
putScp(char *os)
{
    register char *s = os;		/* pointer to unmatched string */
    char dummy[BUFSIZ];			/* dummy to be used by expmatch */
    char *comptr;			/* end of a comment delimiter */
    char *acmptr;			/* end of a comment delimiter */
    char *strptr;			/* end of a string delimiter */
    char *chrptr;			/* end of a character const delimiter */
    char *blksptr;			/* end of a lexical block start */
    char *blkeptr;			/* end of a lexical block end */
    char *nocomptr;			/* end of a non-comment delimiter */

    s_start = os;			/* remember the start for expmatch */
    _escaped = false;
    if (nokeyw || incomm || instr)
	goto skip;
    if (isproc(s)) {
	ps("'FN ");
	ps(pname);
        ps("\n");
	if (psptr < PSMAX) {
	    ++psptr;
	    strncpy (pstack[psptr], pname, PNAMELEN);
	    pstack[psptr][PNAMELEN] = '\0';
	    plstack[psptr] = blklevel;
	}
    }
skip:
    do {
	/* check for string, comment, blockstart, etc */
	if (!incomm && !instr && !inchr) {

	    blkeptr = expmatch(s, l_blkend, dummy);
	    blksptr = expmatch(s, l_blkbeg, dummy);
	    comptr = expmatch(s, l_combeg, dummy);
	    acmptr = expmatch(s, l_acmbeg, dummy);
	    strptr = expmatch(s, l_strbeg, dummy);
	    chrptr = expmatch(s, l_chrbeg, dummy);
	    nocomptr = expmatch (s, l_nocom, dummy);

	    /* start of non-comment? */
	    if (nocomptr != NULL)
		if ((nocomptr <= comptr || comptr == NULL)
		  && (nocomptr <= acmptr || acmptr == NULL)) {
		    /* continue after non-comment */
		    putKcp (s, nocomptr-1, false);
		    s = nocomptr;
		    continue;
		}

	    /* start of a comment? */
	    if (comptr != NULL)
		if ((comptr < strptr || strptr == NULL)
		  && (comptr < acmptr || acmptr == NULL)
		  && (comptr < chrptr || chrptr == NULL)
		  && (comptr < blksptr || blksptr == NULL)
		  && (comptr < blkeptr || blkeptr == NULL)) {
		    putKcp(s, comptr-1, false);
		    s = comptr;
		    incomm = true;
		    comtype = STANDARD;
		    if (s != os)
			ps("\\c");
		    ps("\\c\n'+C\n");
		    continue;
		}

	    /* start of a comment? */
	    if (acmptr != NULL)
		if ((acmptr < strptr || strptr == NULL)
		  && (acmptr < chrptr || chrptr == NULL)
		  && (acmptr < blksptr || blksptr == NULL)
		  && (acmptr < blkeptr || blkeptr == NULL)) {
		    putKcp(s, acmptr-1, false);
		    s = acmptr;
		    incomm = true;
		    comtype = ALTERNATE;
		    if (s != os)
			ps("\\c");
		    ps("\\c\n'+C\n");
		    continue;
		}

	    /* start of a string? */
	    if (strptr != NULL)
		if ((strptr < chrptr || chrptr == NULL)
		  && (strptr < blksptr || blksptr == NULL)
		  && (strptr < blkeptr || blkeptr == NULL)) {
		    putKcp(s, strptr-1, false);
		    s = strptr;
		    instr = true;
		    continue;
		}

	    /* start of a character string? */
	    if (chrptr != NULL)
		if ((chrptr < blksptr || blksptr == NULL)
		  && (chrptr < blkeptr || blkeptr == NULL)) {
		    putKcp(s, chrptr-1, false);
		    s = chrptr;
		    inchr = true;
		    continue;
		}

	    /* end of a lexical block */
	    if (blkeptr != NULL) {
		if (blkeptr < blksptr || blksptr == NULL) {
		    putKcp(s, blkeptr - 1, false);
		    s = blkeptr;
		    if (blklevel > 0 /* sanity */)
			    blklevel--;
		    if (psptr >= 0 && plstack[psptr] >= blklevel) {

			/* end of current procedure */
			if (s != os)
			    ps("\\c");
			ps("\\c\n'-F\n");
			blklevel = plstack[psptr];

			/* see if we should print the last proc name */
			if (--psptr >= 0)
			    prccont = true;
			else
			    psptr = -1;
		    }
		    continue;
		}
	    }

	    /* start of a lexical block */
	    if (blksptr != NULL) {
		putKcp(s, blksptr - 1, false);
		s = blksptr;
		blklevel++;
		continue;
	    }

	/* check for end of comment */
	} else if (incomm) {
	    comptr = expmatch(s, l_comend, dummy);
	    acmptr = expmatch(s, l_acmend, dummy);
	    if (((comtype == STANDARD) && (comptr != NULL)) ||
	        ((comtype == ALTERNATE) && (acmptr != NULL))) {
		if (comtype == STANDARD) {
		    putKcp(s, comptr-1, true);
		    s = comptr;
		} else {
		    putKcp(s, acmptr-1, true);
		    s = acmptr;
		}
		incomm = false;
		ps("\\c\n'-C\n");
		continue;
	    } else {
		putKcp(s, s + strlen(s) -1, true);
		s = s + strlen(s);
		continue;
	    }

	/* check for end of string */
	} else if (instr) {
	    if ((strptr = expmatch(s, l_strend, dummy)) != NULL) {
		putKcp(s, strptr-1, true);
		s = strptr;
		instr = false;
		continue;
	    } else {
		putKcp(s, s+strlen(s)-1, true);
		s = s + strlen(s);
		continue;
	    }

	/* check for end of character string */
	} else if (inchr) {
	    if ((chrptr = expmatch(s, l_chrend, dummy)) != NULL) {
		putKcp(s, chrptr-1, true);
		s = chrptr;
		inchr = false;
		continue;
	    } else {
		putKcp(s, s+strlen(s)-1, true);
		s = s + strlen(s);
		continue;
	    }
	}

	/* print out the line */
	putKcp(s, s + strlen(s) -1, false);
	s = s + strlen(s);
    } while (*s);
}
Beispiel #5
0
char *
expmatch(char *s, char *re, char *mstring)
	/* s - string to check for a match in */
	/* re - a converted irregular expression */
	/* mstring - where to put whatever matches a \p */
{
    char *cs;		/* the current symbol */
    char *ptr, *s1;	/* temporary pointer */
    boolean matched;	/* a temporary boolean */

    /* initial conditions */
    if (re == NIL)
	return (NIL);
    cs = re;
    matched = FALSE;

    /* loop till expression string is exhausted (or at least pretty tired) */
    while (*cs) {
	switch (*cs & (OPER | STR | META)) {

	/* try to match a string */
	case STR:
	    matched = !STRNCMP (s, SSTR(cs), SCNT(cs));
	    if (matched) {

		/* hoorah it matches */
		s += SCNT(cs);
		cs = SNEXT(cs);
	    } else if (*cs & ALT) {

		/* alternation, skip to next expression */
		cs = SNEXT(cs);
	    } else if (*cs & OPT) {

		/* the match is optional */
		cs = SNEXT(cs);
		matched = 1;		/* indicate a successful match */
	    } else {

		/* no match, error return */
		return (NIL);
	    }
	    break;

	/* an operator, do something fancy */
	case OPER:
	    switch (OSYM(cs)) {

	    /* this is an alternation */
	    case '|':
		if (matched)

		    /* last thing in the alternation was a match, skip ahead */
		    cs = OPTR(cs);
		else

		    /* no match, keep trying */
		    cs = ONEXT(cs);
		break;

	    /* this is a grouping, recurse */
	    case '(':
		ptr = expmatch (s, ONEXT(cs), mstring);
		if (ptr != NIL) {

		    /* the subexpression matched */
		    matched = 1;
		    s = ptr;
		} else if (*cs & ALT) {

		    /* alternation, skip to next expression */
		    matched = 0;
		} else if (*cs & OPT) {

		    /* the match is optional */
		    matched = 1;	/* indicate a successful match */
		} else {

		    /* no match, error return */
		    return (NIL);
		}
		cs = OPTR(cs);
		break;
	    }
	    break;

	/* try to match a metasymbol */
	case META:
	    switch (MSYM(cs)) {

	    /* try to match anything and remember what was matched */
	    case 'p':
		/*
		 *  This is really the same as trying the match the
		 *  remaining parts of the expression to any subset
		 *  of the string.
		 */
		s1 = s;
		do {
		    ptr = expmatch (s1, MNEXT(cs), mstring);
		    if (ptr != NIL && s1 != s) {

			/* we have a match, remember the match */
			strncpy (mstring, s, s1 - s);
			mstring[s1 - s] = '\0';
			return (ptr);
		    } else if (ptr != NIL && (*cs & OPT)) {

			/* it was aoptional so no match is ok */
			return (ptr);
		    } else if (ptr != NIL) {

			/* not optional and we still matched */
			return (NIL);
		    }
		    if (!isidchr(*s1))
			return (NIL);
		    if (*s1 == '\\')
			_escaped = _escaped ? FALSE : TRUE;
		    else
			_escaped = FALSE;
		} while (*s1++);
		return (NIL);

	    /* try to match anything */
	    case 'a':
		/*
		 *  This is really the same as trying the match the
		 *  remaining parts of the expression to any subset
		 *  of the string.
		 */
		s1 = s;
		do {
		    ptr = expmatch (s1, MNEXT(cs), mstring);
		    if (ptr != NIL && s1 != s) {

			/* we have a match */
			return (ptr);
		    } else if (ptr != NIL && (*cs & OPT)) {

			/* it was aoptional so no match is ok */
			return (ptr);
		    } else if (ptr != NIL) {

			/* not optional and we still matched */
			return (NIL);
		    }
		    if (*s1 == '\\')
			_escaped = _escaped ? FALSE : TRUE;
		    else
			_escaped = FALSE;
		} while (*s1++);
		return (NIL);

	    /* fail if we are currently _escaped */
	    case 'e':
		if (_escaped)
		    return(NIL);
		cs = MNEXT(cs); 
		break;

	    /* match any number of tabs and spaces */
	    case 'd':
		ptr = s;
		while (*s == ' ' || *s == '\t')
		    s++;
		if (s != ptr || s == Start) {

		    /* match, be happy */
		    matched = 1;
		    cs = MNEXT(cs); 
		} else if (*s == '\n' || *s == '\0') {

		    /* match, be happy */
		    matched = 1;
		    cs = MNEXT(cs); 
		} else if (*cs & ALT) {

		    /* try the next part */
		    matched = 0;
		    cs = MNEXT(cs);
		} else if (*cs & OPT) {

		    /* doesn't matter */
		    matched = 1;
		    cs = MNEXT(cs);
		} else

		    /* no match, error return */
		    return (NIL);
		break;

	    /* check for end of line */
	    case '$':
		if (*s == '\0' || *s == '\n') {

		    /* match, be happy */
		    s++;
		    matched = 1;
		    cs = MNEXT(cs);
		} else if (*cs & ALT) {

		    /* try the next part */
		    matched = 0;
		    cs = MNEXT(cs);
		} else if (*cs & OPT) {

		    /* doesn't matter */
		    matched = 1;
		    cs = MNEXT(cs);
		} else

		    /* no match, error return */
		    return (NIL);
		break;

	    /* check for start of line */
	    case '^':
		if (s == Start) {

		    /* match, be happy */
		    matched = 1;
		    cs = MNEXT(cs);
		} else if (*cs & ALT) {

		    /* try the next part */
		    matched = 0;
		    cs = MNEXT(cs);
		} else if (*cs & OPT) {

		    /* doesn't matter */
		    matched = 1;
		    cs = MNEXT(cs);
		} else

		    /* no match, error return */
		    return (NIL);
		break;

	    /* end of a subexpression, return success */
	    case ')':
		return (s);
	    }
	    break;
	}
    }
    return (s);
}