Changeset 874 for other/sbts/trunk
- Timestamp:
- 02/18/08 23:45:32 (11 months ago)
- Files:
-
- other/sbts/trunk/dsss (added)
- other/sbts/trunk/dsss.conf (modified) (1 diff)
- other/sbts/trunk/dsss/base.sbts (added)
- other/sbts/trunk/dsss/digen.sbts (added)
- other/sbts/trunk/dsss/stubshlib.sbts (added)
- other/sbts/trunk/sbts/Makefile (modified) (1 diff)
- other/sbts/trunk/sbts/filetests.c (added)
- other/sbts/trunk/sbts/glob.c (modified) (1 diff)
- other/sbts/trunk/sbts/main.d (modified) (1 diff)
- other/sbts/trunk/sbts/sbts.d (modified) (18 diffs)
- other/sbts/trunk/scripts (added)
- other/sbts/trunk/scripts/base.sbts (added)
- other/sbts/trunk/scripts/find.sbts (added)
- other/sbts/trunk/test.sbts (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
other/sbts/trunk/dsss.conf
r871 r874 6 6 prebuild = cd sbts ; make 7 7 preclean = cd sbts ; make clean 8 buildflags = sbts/glob.o 8 buildflags = sbts/glob.o sbts/filetests.o 9 9 version (DigitalMars) { 10 10 version (Windows) { other/sbts/trunk/sbts/Makefile
r871 r874 3 3 RM=rm -f 4 4 5 all: glob.o 5 all: glob.o filetests.o 6 6 7 glob.o: glob.c 8 $(CC) $(CFLAGS) -c glob.c -o glob.o7 .c.o: 8 $(CC) $(CFLAGS) -c $< -o $@ 9 9 10 10 clean: other/sbts/trunk/sbts/glob.c
r871 r874 89 89 #define RANGE '-' 90 90 #define RBRACKET ']' 91 #ifdef _WIN32 92 #define SEP '\\' 93 #else 91 94 #define SEP '/' 95 #endif 92 96 #define STAR '*' 93 97 #define TILDE '~' other/sbts/trunk/sbts/main.d
r872 r874 82 82 83 83 } else { 84 sbtsSearchPath = ["scripts"]; 85 84 86 // put in the arguments 85 87 argsToEnv(args[1..$], &getsetenv); other/sbts/trunk/sbts/sbts.d
r873 r874 72 72 const int O_APPEND = 1024; 73 73 int close(int); 74 ptrdiff_t read(int, void*, size_t); 75 ptrdiff_t write(int, void*, size_t); 76 77 // these are in filetests.c 78 int pathExists(char*); 79 int isFile(char*); 80 int isDir(char*); 74 81 } 75 82 … … 82 89 // child, run the command 83 90 execvp(file, args); 84 exit(1 );91 exit(127); 85 92 86 93 } else if (pid < 0) { … … 102 109 /// The return code of the last thing to run 103 110 int lastRet = 0; 111 112 /// The search path for . and source 113 char[][] sbtsSearchPath; 104 114 105 115 /** … … 136 146 // check for redirection specials 137 147 char[][] args = toks; 138 for (int i = 0; i < toks.length - 1; i++) {148 for (int i = 0; i < toks.length; i++) { 139 149 char[] tok = toks[i]; 140 150 … … 153 163 int redirfd = 1; 154 164 int mode = O_WRONLY|O_CREAT; 155 156 // check the modes 157 if (tok == "\x01>>") { 158 mode |= O_APPEND; 159 } else if (tok == "\x012>") { 160 redirfd = 2; 161 } else if (tok == "\x012>>") { 162 redirfd = 2; 163 mode |= O_APPEND; 165 int fd = -1; 166 167 // work through it 168 for (int j = 1; j < tok.length; j++) { 169 if (tok[j] >= '0' && tok[j] <= '9') { 170 redirfd = tok[j] - '0'; 171 172 } else if (tok[j] == '>') { 173 // check for append 174 j++; if (j >= tok.length) break; 175 if (tok[j] == '>') { 176 mode |= O_APPEND; 177 } else { 178 j--; 179 } 180 181 } else if (tok[j] == '&') { 182 // the fd to copy from 183 fd = datoi(tok[j+1..$]); 184 j = tok.length; 185 } 164 186 } 165 187 … … 169 191 // support /dev/null on Windows 170 192 version (Windows) { 171 if ( toks[i] == "/dev/null") {193 if (i < toks.length && toks[i] == "/dev/null") { 172 194 toks[i] = "NUL"; 173 195 } 174 196 } 175 197 176 int fd = open((toks[i]~'\0').ptr, mode, 0644); 177 dup2(fd, redirfd); 178 close(fd); 198 if (fd == -1 && i < toks.length) { 199 fd = open((toks[i]~'\0').ptr, mode, 0644); 200 dup2(fd, redirfd); 201 close(fd); 202 } else { 203 dup2(fd, redirfd); 204 } 179 205 180 206 } … … 366 392 if (!tokQuotes) { 367 393 // check for redirection 368 if (tok == "<" || 369 tok == ">" || 370 tok == ">>" || 371 tok == "2>" || 372 tok == "2>>") { 394 if (regex.find(tok, "[<>]") != -1 && 395 regex.find(tok, "[ \\t\\r\\na-zA-Z]") == -1) { 373 396 toks ~= ("\x01" ~ tok); 374 397 375 398 } else { 399 // glob. On Windows, /->\ 400 version (Windows) { 401 foreach (toki, tokc; tok) { 402 if (tokc == '/') { 403 tok[toki] = '\\'; 404 } 405 } 406 } 407 376 408 char* ctok = (tok ~ '\0').ptr; 377 409 glob_t tokglob; … … 616 648 } 617 649 618 char[] fcont = cast(char[]) read(cmd[1]); 650 char[] fname = cmd[1]; 651 652 // first try in . 653 if (!isFile((fname~'\0').ptr)) { 654 // OK, go through the search path 655 foreach (path; sbtsSearchPath) { 656 fname = path ~ "/" ~ cmd[1]; 657 if (isFile((fname~'\0').ptr)) break; 658 fname = null; 659 } 660 } 661 662 char[] fcont = cast(char[]) read(fname); 619 663 *ret = rerun(fcont); 620 664 break; … … 689 733 write(1, arg.ptr, arg.length); 690 734 } 691 692 // finish it all with a newline693 write(1, cast(char*) "\n", 1);694 735 break; 695 736 } … … 807 848 } 808 849 850 case "return": 851 { 852 if (cmd.length < 2) { 853 throw new SBTSReturn(0); 854 } else { 855 throw new SBTSReturn(datoi(cmd[1])); 856 } 857 break; 858 } 859 809 860 case "set": 810 861 { … … 900 951 901 952 // run the function 902 *ret = rerun(*fun); 953 try { 954 *ret = rerun(*fun); 955 } catch(SBTSReturn sr) { 956 *ret = sr.rval; 957 } 903 958 904 959 // and replace the args … … 912 967 913 968 return true; 969 } 970 971 /// A return value from a function 972 private class SBTSReturn : Exception { 973 this(int srval) { 974 super("SBTSReturn"); 975 rval = srval; 976 } 977 978 int rval; 914 979 } 915 980 … … 927 992 int cmdCalculator(char[][] args) 928 993 { 929 int calcAddExp( char[][] args,inout int i)994 int calcAddExp(inout int i) 930 995 { 931 996 int val = datoi(args[i]); … … 945 1010 } 946 1011 947 int calcMulExp( char[][] args,inout int i)1012 int calcMulExp(inout int i) 948 1013 { 949 int val = calcAddExp( args,i);1014 int val = calcAddExp(i); 950 1015 for (i++; i < args.length; i++) { 951 1016 char[] arg = args[i]; 952 1017 if (arg == "*") { 953 1018 i++; 954 val *= calcAddExp( args,i);1019 val *= calcAddExp(i); 955 1020 i--; 956 1021 } else if (arg == "/") { 957 1022 i++; 958 val /= calcAddExp( args,i);1023 val /= calcAddExp(i); 959 1024 i--; 960 1025 } else { … … 966 1031 967 1032 int i = 0; 968 return calcMulExp( args,i);1033 return calcMulExp(i); 969 1034 } 970 1035 … … 995 1060 int rd; 996 1061 997 while ((rd = fread(buf.ptr + rdtotal, 1, buf.length - rdtotal, stdin)) > 0) {1062 while ((rd = read(0, buf.ptr + rdtotal, buf.length - rdtotal)) > 0) { 998 1063 if (rd == buf.length - rdtotal) { 999 1064 // read it all, expand … … 1007 1072 1008 1073 // then replace 1009 char[] outp = regex.sub(buf[0..rdtotal], args[0], args[1] );1074 char[] outp = regex.sub(buf[0..rdtotal], args[0], args[1], "g"); 1010 1075 write(1, outp.ptr, outp.length); 1011 1076 return 0; … … 1017 1082 int cmdTest(char[][] args) 1018 1083 { 1019 // expects three arguments 1020 if (args.length < 3) { 1021 return 1; 1022 } 1023 1024 // the second is the test 1025 switch (args[1]) { 1026 case "-lt": 1027 case "-le": 1028 case "-eq": 1029 case "-ne": 1030 case "-gt": 1031 case "-ge": 1032 { 1033 // integral checks 1034 int left = datoi(args[0]); 1035 int right = datoi(args[2]); 1036 1084 int parseTest(inout int i) 1085 { 1086 if (args[i].length > 1 && args[i][0] == '-') { 1087 // unary file test 1088 i++; 1089 char* f = (args[i]~'\0').ptr; 1090 i++; 1091 1092 switch (args[i-2]) { 1093 case "-e": 1094 return !pathExists(f); 1095 1096 case "-f": 1097 return !isFile(f); 1098 1099 case "-d": 1100 return !isDir(f); 1101 } 1102 1103 } else { 1104 // binary test 1105 char[] sleft = args[i]; 1106 char[] op = args[i+1]; 1107 char[] sright = args[i+2]; 1108 i += 3; 1109 1110 switch (op) { 1111 case "-lt": 1112 case "-le": 1113 case "-eq": 1114 case "-ne": 1115 case "-gt": 1116 case "-ge": 1117 { 1118 // integral checks 1119 int left = datoi(sleft); 1120 int right = datoi(sright); 1037 1121 1038 switch (args[1]) { 1039 case "-lt": 1040 return !(left < right); 1122 switch (op) { 1123 case "-lt": 1124 return !(left < right); 1125 1126 case "-le": 1127 return !(left <= right); 1128 1129 case "-eq": 1130 return !(left == right); 1131 1132 case "-ne": 1133 return !(left != right); 1134 1135 case "-gt": 1136 return !(left > right); 1137 1138 case "-ge": 1139 return !(left >= right); 1140 } 1141 1142 break; 1143 } 1144 1041 1145 1042 case "-le": 1043 return !(left <= right); 1146 case "=": 1147 { 1148 // string equals 1149 return strcmp((sleft~'\0').ptr, (sright~'\0').ptr); 1150 } 1151 1044 1152 1045 case "-eq": 1046 return !(left == right); 1047 1048 case "-ne": 1049 return !(left != right); 1050 1051 case "-gt": 1052 return !(left > right); 1053 1054 case "-ge": 1055 return !(left >= right); 1056 } 1057 1058 1059 break; 1060 1061 } 1062 1063 1064 case "=": 1065 { 1066 // string equals 1067 return strcmp((args[0]~'\0').ptr, (args[2]~'\0').ptr); 1068 } 1069 1070 1071 case "!=": 1072 { 1073 // string not-equals 1074 return !strcmp((args[0]~'\0').ptr, (args[2]~'\0').ptr); 1075 } 1076 } 1077 } 1153 case "!=": 1154 { 1155 // string not-equals 1156 return !strcmp((sleft~'\0').ptr, (sright~'\0').ptr); 1157 } 1158 } 1159 } 1160 } 1161 1162 // parse and expressions 1163 int parseAndExp(inout int i) 1164 { 1165 int val = parseTest(i); 1166 for (; i < args.length; i++) { 1167 if (args[i] == "-a") { 1168 // this is an and expression 1169 i++; 1170 val = !(!val && !parseTest(i)); 1171 i--; 1172 1173 } else { 1174 break; 1175 1176 } 1177 } 1178 return val; 1179 } 1180 1181 // parse or expressions 1182 int parseOrExp(inout int i) 1183 { 1184 int val = parseAndExp(i); 1185 for (; i < args.length; i++) { 1186 if (args[i] == "-o") { 1187 // an or exp 1188 i++; 1189 val = !(!val || !parseTest(i)); 1190 i--; 1191 1192 } else { 1193 break; 1194 1195 } 1196 } 1197 return val; 1198 } 1199 1200 int i = 0; 1201 return parseOrExp(i); 1202 } other/sbts/trunk/test.sbts
r872 r874 1 foreach i sbts/* { 2 capture cont {cat $i} 3 with "$cont" {grep ango} 4 } 1 echo hi >&2
