File: shellexp.c

package info (click to toggle)
cruft-ng 0.4.7
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 152 kB
  • sloc: cpp: 779; ansic: 92; makefile: 37; sh: 9
file content (97 lines) | stat: -rw-r--r-- 2,229 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>

#define FALSE false
#define TRUE  true

/* 0 on no match, non-zero on match */
int shellexp( char* string, char* pattern ) {
    /*  printf( "...matching( \"%s\", \"%s\" )\n", string, pattern ); */
    
    switch( pattern[0] ) {
    case '\0':
	return string[0] == '\0';
    case '?':
	switch( string[0] ) {
	case '\0':
	case '/':
	    return FALSE;
	default:
	    return shellexp( string+1, pattern+1 );
	}
    case '/':
	if ( pattern[1] == '*' && pattern[2] == '*' ) {
	    char* pch = string;
	    if ( pattern[3] != '/' && pattern[3] != '\0' ) {
		fprintf( stderr, "Bad expression.\n" );
		return -1;
	    }
	    if ( *pch != '/' ) return FALSE;                
	    if ( pattern[3] == '\0' ) return TRUE;
	    while ( *pch != '\0' ) {
		if ( *pch == '/' ) {
		    int ret = shellexp( pch, pattern + 3 );
		    if ( ret == TRUE || ret == -1 )
		    	return ret;
		}
		pch++;
	    }
	    return FALSE;
	} else if ( string[0] == '/' ) {
		return shellexp( string+1, pattern+1 );
	} else
		return FALSE;
    case '*':
	if ( string[0] == '/' ) return shellexp( string, pattern+1 );
	{
		int ret = shellexp( string, pattern+1 );
		if (ret == FALSE)
			return string[0] != '\0' ? shellexp( string + 1, pattern ) : FALSE;
		else
			return ret;
	}
    case '[': 
	if ( string[0] == '\0' ) return FALSE;
	{
	    int nott = FALSE;
	    int okay = FALSE;
	    pattern++;
	    if ( pattern[0] == '!' || pattern[0] == '^' ) {
		nott = TRUE;
		pattern++;
	    }

	    if ( pattern[0] == ']' ) {
		if ( string[0] == ']' ) { okay = TRUE; }
		pattern++;
	    }
	    
	    while( pattern[0] != ']' && pattern[0] != '\0' ) {
		if ( pattern[0] == string[0] ) {
		    okay = TRUE;
		} else if ( pattern[1] == '-' && pattern[2] != ']' ) {
		    if ( pattern[0] <= string[0] && string[0] <= pattern[2] ) {
			okay = TRUE;
		    }
		    pattern += 2;
		}
		pattern++;
	    }
	    
	    if ( pattern[0] == '\0' ) {
		fprintf( stderr, "Bad shell expression\n" );
		return -1;
	    }
	    
	    if (! (nott ? !okay : okay))
	    	return FALSE;
	    else
	    	return shellexp( string + 1, pattern + 1 );
	}
    default:
    	if (pattern[0] != string[0])
		return FALSE;
	else
		return shellexp( string + 1, pattern + 1 );
    }
}