// (bind 'sym|lst . prg) -> any any doBind(any ex) { any x, y; x = cdr(ex); if (isNum(y = EVAL(car(x)))) argError(ex, y); if (isNil(y)) return prog(cdr(x)); if (isSym(y)) { bindFrame f; Bind(y,f); x = prog(cdr(x)); Unbind(f); return x; } { struct { // bindFrame struct bindFrame *link; int i, cnt; struct {any sym; any val;} bnd[length(y)]; } f; f.link = Env.bind, Env.bind = (bindFrame*)&f; f.i = f.cnt = 0; do { if (isNum(car(y))) argError(ex, car(y)); if (isSym(car(y))) { f.bnd[f.cnt].sym = car(y); f.bnd[f.cnt].val = val(car(y)); } else { f.bnd[f.cnt].sym = caar(y); f.bnd[f.cnt].val = val(caar(y)); val(caar(y)) = cdar(y); } ++f.cnt; } while (isCell(y = cdr(y))); x = prog(cdr(x)); while (--f.cnt >= 0) val(f.bnd[f.cnt].sym) = f.bnd[f.cnt].val; Env.bind = f.link; return x; } }
/** * @ingroup shell * * Shell command (nc). * @param nargs number of arguments in args array * @param args array of arguments * @return 0 for success, 1 for error */ shellcmd xsh_nc(int nargs, char *args[]) { int a; ushort port; bool listen = FALSE; struct netaddr dst; struct netaddr src; tid_typ recvthr; tid_typ sendthr; int msg = 0; int dev; /* Output help, if '--help' argument was supplied */ if (nargs == 2 && 0 == strncmp(args[1], "--help", 7)) { usage(args[0]); return 0; } port = 0; dst.type = NULL; src.type = NULL; /* Parse arguments */ for (a = 1; a < nargs; a++) { if (args[a][0] != '-') { if (!listen && NULL == dst.type) { if (!isdigit(args[a][0])) { return argError(args[a]); } dot2ipv4(args[a], &dst); } else if (listen && NULL == src.type) { if (!isdigit(args[a][0])) { return argError(args[a]); } dot2ipv4(args[a], &src); } else if (NULL == port) { if (!isdigit(args[a][0])) { return argError(args[a]); } port = atoi(args[a]); } } else { switch (args[a][1]) { /* Listen on port */ case 'l': listen = TRUE; break; /* Source IP */ case 's': a++; if (a >= nargs || !isdigit(args[a][0])) { return argError(args[a - 1]); } dot2ipv4(args[a], &src); break; default: return argError(args[a]); } } } /* Verify arguments */ if (NULL == src.type) { fprintf(stderr, "Source/listen IP address required\n"); return 1; } if (NULL == port) { fprintf(stderr, "Invalid port\n"); return 1; } if (!listen && NULL == dst.type) { fprintf(stderr, "Remote IP address required\n"); return 1; } /* Allocate a TCP device */ #ifdef NTCP dev = tcpAlloc(); #else dev = SYSERR; #endif /* NTCP */ if (SYSERR == dev) { return connError(); } if (listen) { if (SYSERR == open(dev, &src, NULL, port, NULL, TCP_PASSIVE)) { close(dev); return connError(); } } else { if (SYSERR == open(dev, &src, &dst, NULL, port, TCP_ACTIVE)) { close(dev); return connError(); } } recvthr = create(ncRecv, SHELL_CMDSTK, SHELL_CMDPRIO, "nc_recv", 1, dev); sendthr = create(ncSend, SHELL_CMDSTK, SHELL_CMDPRIO, "nc_send", 1, dev); if ((SYSERR == recvthr) || (SYSERR == sendthr)) { kill(recvthr); kill(sendthr); close(dev); fprintf(stderr, "Failed to spawn threads"); return 1; } thrtab[recvthr].fdesc[0] = stdin; thrtab[recvthr].fdesc[1] = stdout; thrtab[recvthr].fdesc[2] = stderr; thrtab[sendthr].fdesc[0] = stdin; thrtab[sendthr].fdesc[1] = stdout; thrtab[sendthr].fdesc[2] = stderr; /* Start both threads */ while (recvclr() != NOMSG); ready(recvthr, RESCHED_YES); ready(sendthr, RESCHED_NO); /* Wait for one thread to die */ while ((msg != recvthr) && (msg != sendthr)) { msg = receive(); } sleep(10); /* Kill both threads */ kill(recvthr); kill(sendthr); close(dev); return 0; }
bool GetSetupCommand(struct cmd *setup, const int argc, char **argv) { if(argc == 1) { printf("%s\n\n",PROGRAM_NAME_STRING " v" VERSION_STRING ); printf( "Usege: mSplit [-sb] [SIZE suffix] [in-file_name] [out-directory]\n\n" "Commands:\n" " -s: write SIZE bytes per output file\n" " -b: make Bat-file\n" "\n" "Suffixes:\n" " b: byte\n" " k: kilobyte\n" " m: megabyte\n" " g: gigabyte\n" " t: terabyte\n" "\n" "Examples: mSplit -sb 50m filename\n" ); return true; } if(argc < 4) { printf("incorrect command"); return true; } try { if(strlen(argv[1]) <=1 || strlen(argv[1]) > 3) { throw argv[1]; } std::auto_ptr<char> command(new char[strlen(argv[1])+2]); strcpy(command.get(), argv[1]); StrToLower(command.get()); if(command.get()[0] != '-') { throw argv[1]; } else { // char *p = strchr(command.get(), 'b'); // if(p == 0) // if( *( ) != 'b' ){ // throw argv[1]; // }else{ // setup->makeBatFile = true; // } if( command.get()[1] != 's' ) { throw argv[1]; } else { if( command.get()[2] != 'b' && command.get()[2] != 0 ) { throw argv[1]; } else { if(command.get()[2] == 'b') { setup->makeBatFile = true; } } setup->splitSize = true; unsigned int nsize = atoi(argv[2]); setup->outFileSize = nsize; if(nsize == 0) { throw argv[2]; } else { std::auto_ptr<char> suffixCommand(new char[strlen(argv[2])+1]); strcpy(suffixCommand.get(), argv[2]); const char *pStr = GetFirstABC(suffixCommand.get()); const int len = strlen(pStr); if(pStr == NULL || len >1) { throw argv[2]; } else { StrToLower(suffixCommand.get()); const char opt = *pStr; switch (opt) { case 'b': break; case 'k': setup->outFileSize <<= 10; if( setup->outFileSize <= 0) {throw 1;} break; case 'm': setup->outFileSize <<= 20; if( setup->outFileSize <= 0) {throw 1;} break; case 'g': setup->outFileSize <<= 30; if( setup->outFileSize <= 0) {throw 1;} break; case 't': setup->outFileSize <<= 40; if( setup->outFileSize <= 0) {throw 1;} break; default: throw argv[2]; break; } } } } } } catch (const char *str) { argError(str); return true; } catch (const int OverValue) { printf("%s\n", "overflow value-> size outFile"); return true; } std::auto_ptr<FileIO> inFile( new FileIO ); if( inFile->FileOpen(argv[3],GENERIC_READ) == false ) { printf("can not open input file: "); printf(argv[3]); printf("\n"); return true; } else { setup->inFile = argv[3]; setup->inFileSize = inFile->GetSize(); char *tempNameFile = new char[strlen(argv[3])+1]; GetFileFromFullPath(argv[3], tempNameFile); setup->outFile = tempNameFile; delete[] tempNameFile; } if(argc != 4) { if( ::GetFileAttributes(argv[4]) == INVALID_FILE_ATTRIBUTES) { printf("can not found directory: "); printf(argv[4]); printf("\n"); return 1; } else { setup->outDir = argv[4]; return false; } } else { char *tempPath = new char[strlen(argv[3])+1]; GetDirFromFullPath(argv[3], tempPath); setup->outDir = tempPath; delete[] tempPath; } return false; }