-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsx_if.c
159 lines (116 loc) · 2.79 KB
/
sx_if.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
153
154
155
156
157
158
159
/* sx_if.c
IF for Samarux.
Conditional execution.
Copyright (c) 2007 - 2015 Miguel I. Garcia Lopez.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Usage:
if arg1 [not] eq arg2 command
if arg1 [not] exist command
if arg1 [not] in_env command
Options:
None.
Examples:
if $USER eq Mike echo Hello Mike!
if profile.sx not exist echo No profile found!
if USER not in_env echo Environment variable USER is not defined!
Changes:
15 Feb 2015 : 1.00 : 1st version.
23 Feb 2015 : 1.01 : Now 'equal' is 'eq'.
21 Mar 2015 : 1.02 : Added in_env test.
*/
#define SX_IF
#define IF_EQ 1
#define IF_EXIST 2
#define IF_IN_ENV 3
IfMain(argc, argv)
int argc, argv[];
{
char *arg1, *arg2, *wrd, *q;
int not, cmd, result, index;
/* Set-up some variables */
index = 1;
not = cmd = result = 0;
/* Get 1st argument */
if((arg1 = IfGetArg(index++, argc, argv)) == NULL)
return 1;
/* Get 1st reserved word */
if((wrd = IfGetArg(index++, argc, argv)) == NULL)
return 1;
/* NOT? */
if(!strcmp("not", wrd))
{
++not;
if((wrd = IfGetArg(index++, argc, argv)) == NULL)
return 1;
}
/* EQ, EXIST, IN_ENV? */
if(!strcmp("eq", wrd))
cmd = IF_EQ;
else if(!strcmp("exist", wrd))
cmd = IF_EXIST;
else if(!strcmp("in_env", wrd))
cmd = IF_IN_ENV;
else
return Error("Bad syntax");
/* Test specified condition */
switch(cmd)
{
case IF_EQ :
if((arg2 = IfGetArg(index++, argc, argv)) == NULL)
return 1;
result = strcmp(arg1, arg2) ? 0 : 1;
break;
case IF_EXIST :
result = IfExist(arg1);
break;
case IF_IN_ENV :
result = EnvGet(arg1) != NULL ? 1 : 0;
break;
}
/* Check for command */
if((q = IfGetArg(index, argc, argv)) == NULL)
return 1;
/* Check for NOT */
if(not)
result = 1 - result;
/* Execute command if condition is TRUE */
if(result)
{
if((q = UnParse(argc - index, argv + index)) == NULL)
return 1;
Execute(q);
free(q);
}
/* Return success */
return 0;
}
IfExist(fn)
char *fn;
{
FILE *fp;
if((fp = fopen(fn, "r")) == NULL)
return 0;
fclose(fp);
return 1;
}
IfGetArg(index, argc, argv)
int index, argc, argv[];
{
if(index < argc)
return argv[index];
ErrorPars();
return NULL;
}
#undef IF_EQ
#undef IF_EXIST
#undef IF_IN_ENV