/* * Print the termcap string out with variable substitution */ void EchoTC(Char **v) { Char **globbed; char cv[BUFSIZE];/*FIXBUF*/ int verbose = 0, silent = 0; static char *fmts = "%s\n", *fmtd = "%d\n"; int li,co; setname("echotc"); v = glob_all_or_error(v); globbed = v; cleanup_push(globbed, blk_cleanup); if (!v || !*v || *v[0] == '\0') goto end; if (v[0][0] == '-') { switch (v[0][1]) { case 'v': verbose = 1; break; case 's': silent = 1; break; default: stderror(ERR_NAME | ERR_TCUSAGE); break; } v++; } if (!*v || *v[0] == '\0') goto end; (void) StringCbCopy(cv,sizeof(cv), short2str(*v)); GetSize(&li,&co); if(!lstrcmp(cv,"rows") || !lstrcmp(cv,"lines") ) { xprintf(fmtd,T_Lines); goto end; } else if(!lstrcmp(cv,"cols") ) { xprintf(fmtd,T_ActualWindowSize); goto end; } else if(!lstrcmp(cv,"buffer") ) { xprintf(fmtd,T_Cols); goto end; } else stderror(ERR_SYSTEM, "EchoTC","Sorry, this function is not supported"); end: cleanup_until(globbed); }
/*ARGSUSED*/ void doinlib(Char **v, struct command *c) { Char **globbed; setname(short2str(*v++)); v = glob_all_or_error(v); globbed = v; cleanup_push(globbed, blk_cleanup); while (v && *v) llib(*v++); cleanup_until(globbed); }
void dotitle(Char **vc, struct command * c) { int k; char titlebuf[512]; char errbuf[128],err2[128]; char **v; Char **nvc; UNREFERENCED_PARAMETER(c); vc++; nvc = glob_all_or_error(vc); if (nvc == NULL) return; if (nvc != vc) cleanup_push(nvc, blk_cleanup); if ((k = GetConsoleTitle(titlebuf, sizeof(titlebuf))) != 0) { setcopy(STRoldtitle,str2short(titlebuf), VAR_READWRITE|VAR_NOGLOB); } titlebuf[0] = '\0'; v = short2blk(nvc); if (nvc != vc) cleanup_until(nvc); cleanup_push((Char **)v, blk_cleanup); for (k = 0; v[k] != NULL ; k++){ __try { StringCbCat(titlebuf,sizeof(titlebuf),v[k]); StringCbCat(titlebuf,sizeof(titlebuf)," "); } __except(GetExceptionCode()) { stderror(ERR_TOOMANY); } } if (!SetConsoleTitle(titlebuf) ) { make_err_str(GetLastError(),errbuf,128); (void)StringCbPrintf(err2,sizeof(err2),"%s",v[k]); stderror(ERR_SYSTEM,err2,errbuf); } cleanup_until((Char **)v); return; }
/*ARGSUSED*/ void dolist(Char **v, struct command *c) { Char **globbed; int i, k, ret = 0; struct stat st; USE(c); if (*++v == NULL) { struct Strbuf word = Strbuf_INIT; Strbuf_terminate(&word); cleanup_push(&word, Strbuf_cleanup); (void) t_search(&word, LIST, TW_ZERO, 0, STRNULL, 0); cleanup_until(&word); return; } v = glob_all_or_error(v); globbed = v; cleanup_push(globbed, blk_cleanup); for (k = 0; v[k] != NULL && v[k][0] != '-'; k++) continue; if (v[k]) { /* * We cannot process a flag therefore we let ls do it right. */ Char *lspath; struct command *t; struct wordent cmd, *nextword, *lastword; Char *cp; struct varent *vp; if (setintr) { pintr_disabled++; cleanup_push(&pintr_disabled, disabled_cleanup); } if (seterr) { xfree(seterr); seterr = NULL; } lspath = STRls; STRmCF[1] = 'C'; STRmCF[3] = '\0'; /* Look at listflags, to add -A to the flags, to get a path of ls if necessary */ if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL && vp->vec[0] != STRNULL) { if (vp->vec[1] != NULL && vp->vec[1][0] != '\0') lspath = vp->vec[1]; for (cp = vp->vec[0]; *cp; cp++) switch (*cp) { case 'x': STRmCF[1] = 'x'; break; case 'a': STRmCF[3] = 'a'; break; case 'A': STRmCF[3] = 'A'; break; default: break; } } cmd.word = STRNULL; lastword = &cmd; nextword = xcalloc(1, sizeof cmd); nextword->word = Strsave(lspath); lastword->next = nextword; nextword->prev = lastword; lastword = nextword; nextword = xcalloc(1, sizeof cmd); nextword->word = Strsave(STRmCF); lastword->next = nextword; nextword->prev = lastword; #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE) if (dspmbyte_ls) { lastword = nextword; nextword = xcalloc(1, sizeof cmd); nextword->word = Strsave(STRmmliteral); lastword->next = nextword; nextword->prev = lastword; } #endif #ifdef COLOR_LS_F if (color_context_ls) { lastword = nextword; nextword = xcalloc(1, sizeof cmd); nextword->word = Strsave(STRmmcolormauto); lastword->next = nextword; nextword->prev = lastword; } #endif /* COLOR_LS_F */ lastword = nextword; for (cp = *v; cp; cp = *++v) { nextword = xcalloc(1, sizeof cmd); nextword->word = quote(Strsave(cp)); lastword->next = nextword; nextword->prev = lastword; lastword = nextword; } lastword->next = &cmd; cmd.prev = lastword; cleanup_push(&cmd, lex_cleanup); /* build a syntax tree for the command. */ t = syntax(cmd.next, &cmd, 0); cleanup_push(t, syntax_cleanup); if (seterr) stderror(ERR_OLD); /* expand aliases like process() does */ /* alias(&cmd); */ /* execute the parse tree. */ execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE); /* done. free the lex list and parse tree. */ cleanup_until(&cmd); if (setintr) cleanup_until(&pintr_disabled); } else { Char *dp, *tmp; struct Strbuf buf = Strbuf_INIT; cleanup_push(&buf, Strbuf_cleanup); for (k = 0, i = 0; v[k] != NULL; k++) { tmp = dnormalize(v[k], symlinks == SYM_IGNORE); cleanup_push(tmp, xfree); dp = Strend(tmp) - 1; if (*dp == '/' && dp != tmp) #ifdef apollo if (dp != &tmp[1]) #endif /* apollo */ *dp = '\0'; if (stat(short2str(tmp), &st) == -1) { int err; err = errno; if (k != i) { if (i != 0) xputchar('\n'); print_by_column(STRNULL, &v[i], k - i, FALSE); } haderr = 1; xprintf("%S: %s.\n", tmp, strerror(err)); haderr = 0; i = k + 1; ret = 1; } else if (S_ISDIR(st.st_mode)) { Char *cp; if (k != i) { if (i != 0) xputchar('\n'); print_by_column(STRNULL, &v[i], k - i, FALSE); } if (k != 0 && v[1] != NULL) xputchar('\n'); xprintf("%S:\n", tmp); buf.len = 0; for (cp = tmp; *cp; cp++) Strbuf_append1(&buf, (*cp | QUOTE)); Strbuf_terminate(&buf); dp = &buf.s[buf.len - 1]; if ( #ifdef WINNT_NATIVE (*dp != (Char) (':' | QUOTE)) && #endif /* WINNT_NATIVE */ (*dp != (Char) ('/' | QUOTE))) { Strbuf_append1(&buf, '/'); Strbuf_terminate(&buf); } else *dp &= TRIM; (void) t_search(&buf, LIST, TW_ZERO, 0, STRNULL, 0); i = k + 1; } cleanup_until(tmp); } cleanup_until(&buf); if (k != i) { if (i != 0) xputchar('\n'); print_by_column(STRNULL, &v[i], k - i, FALSE); } if (ret) stderror(ERR_SILENT); } cleanup_until(globbed); }
void dostart(Char ** vc, struct command *c) { char *cmdstr,*cmdend,*ptr; char argv0[256];/*FIXBUF*/ DWORD cmdsize; char *currdir=NULL; char *savepath; char **v = NULL; STARTUPINFO si; PROCESS_INFORMATION pi; DWORD dwCreationFlags=CREATE_NEW_CONSOLE; DWORD k,cmdlen,j,jj,ret; UNREFERENCED_PARAMETER(c); vc++; cmdsize = 512; cmdstr = heap_alloc(cmdsize); cmdend = cmdstr; cmdlen = 0; memset(&si,0,sizeof(si)); si.cb = sizeof(si); vc = glob_all_or_error(vc); v = short2blk(vc); if(v == NULL) { stderror(ERR_NOMEM); return; } blkfree(vc); for (k = 0; v[k] != NULL ; k++){ if ( v[k][0] == '-' ) { /* various options */ if( (v[k][1] == 'T') || (v[k][1] == 't')) si.lpTitle =&( v[k][2]); else if ( (v[k][1] == 'D') || (v[k][1] == 'd')) currdir =&( v[k][2]); else if (!_stricmp(&v[k][1],"MIN") ) si.wShowWindow = SW_SHOWMINIMIZED; else if (!_stricmp(&v[k][1],"MAX") ) si.wShowWindow = SW_SHOWMAXIMIZED; else if (!_stricmp(&v[k][1],"SEPARATE") ) dwCreationFlags |= CREATE_SEPARATE_WOW_VDM; else if (!_stricmp(&v[k][1],"SHARED") ) dwCreationFlags |= CREATE_SHARED_WOW_VDM; else if (!_stricmp(&v[k][1],"LOW") ) dwCreationFlags |= IDLE_PRIORITY_CLASS; else if (!_stricmp(&v[k][1],"NORMAL") ) dwCreationFlags |= NORMAL_PRIORITY_CLASS; else if (!_stricmp(&v[k][1],"HIGH") ) dwCreationFlags |= HIGH_PRIORITY_CLASS; else if (!_stricmp(&v[k][1],"REALTIME") ) dwCreationFlags |= REALTIME_PRIORITY_CLASS; else{ blkfree((Char **)v); stderror(ERR_SYSTEM,start_usage,"See CMD.EXE for more info");/*FIXRESET*/ } } else{ // non-option arg break; } } /* * Stop the insanity of requiring start "tcsh -l" * Option processing now stops at first non-option arg * -amol 5/30/96 */ for (jj=k;v[jj] != NULL; jj++) { j=(lstrlen(v[jj]) + 2); if (j + cmdlen > cmdsize) { ptr = cmdstr; cmdstr = heap_realloc(cmdstr, max(cmdsize << 1, j+cmdlen) ); if(!cmdstr) { heap_free(ptr); stderror(ERR_NOMEM,"start");/*FIXRESET*/ } cmdend = cmdstr + (cmdend - ptr); cmdsize <<= 1; } ptr = v[jj]; while (*ptr) { *cmdend++ = *ptr++; cmdlen++; } *cmdend++ = ' '; cmdlen++; } if (jj == k) { blkfree((Char **)v); stderror(ERR_SYSTEM,start_usage,"See CMD.EXE for more info");/*FIXRESET*/ return; } *cmdend = 0; StringCbCopy(argv0,sizeof(argv0),v[k]); /* * strictly speaking, it should do no harm to set the path * back to '\'-delimited even in the parent, but in the * interest of consistency, we save the old value and restore it * later */ savepath = fix_path_for_child(); if (! CreateProcess(NULL, cmdstr, NULL, NULL, FALSE, dwCreationFlags, NULL, currdir, &si, &pi) ) { restore_path(savepath); ret = GetLastError(); if (ret == ERROR_BAD_EXE_FORMAT || ret == ERROR_ACCESS_DENIED || (ret == ERROR_FILE_NOT_FOUND && (is_url(v[k]) || is_directory(v[k])) ) ) { char erbuf[MAX_PATH]; errno = ENOEXEC; try_shell_ex(&v[k],0,FALSE); heap_free(cmdstr); /* free !! */ if (errno) { strerror_s(erbuf,sizeof(erbuf),errno); stderror(ERR_ARCH,argv0,erbuf);/*FIXRESET*/ } } else if (ret == ERROR_INVALID_PARAMETER) { errno = ENAMETOOLONG; heap_free(cmdstr); /* free !! */ stderror(ERR_TOOLARGE,argv0);/*FIXRESET*/ } else { errno = ENOENT; if ( ( (v[k][0] == '\\') ||(v[k][0] == '/') ) && ( (v[k][1] == '\\') ||(v[k][1] == '/') ) && (!v[k+1]) ) try_shell_ex(&v[k],0,FALSE); heap_free(cmdstr); /* free !! */ if (errno) { stderror(ERR_NOTFOUND,argv0);/*FIXRESET*/ } } } else { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); heap_free(cmdstr); restore_path(savepath); } blkfree((Char **)v); return; }
/*ARGSUSED*/ void dobs2cmd(Char **v, struct command *c) { Char *cp, **globbed; int i = 0, len = 0; char *cmd = NULL; int pvec[2]; struct command faket; Char *fakecom[2]; char tibuf[BUFSIZE]; int icnt, old_pintr_disabled; static const Char STRbs2cmd[] = { 'b','s','2','c','m','d','\0' }; v++; if (setintr) pintr_push_enable(&old_pintr_disabled); v = glob_all_or_error(v); if (setintr) cleanup_until(&old_pintr_disabled); globbed = v; cleanup_push(globbed, blk_cleanup); /* First round: count the string lengths */ for (i=0; v[i]; ++i) { len += Strlen(v[i]) + (v[i+1] != NULL); } cmd = xmalloc(len+1); /* 1 for the final '\0' *//* FIXME: memory leak? */ /* 2nd round: fill cmd buffer */ i = 0; while ((cp = *v++) != 0) { int c; while (c = *cp++) cmd[i++] = (char)c; if (*v) cmd[i++] = ' '; } cmd[i] = '\0'; /* Make upper case */ bs2upcase(cmd); faket.t_dtyp = NODE_COMMAND; faket.t_dflg = F_BACKQ|F_STDERR; faket.t_dlef = 0; faket.t_drit = 0; faket.t_dspr = 0; faket.t_dcom = fakecom; fakecom[0] = (Char *)STRbs2cmd; fakecom[1] = 0; mypipe(pvec); cleanup_push(&pvec[0], open_cleanup); cleanup_push(&pvec[1], open_cleanup); if (pfork(&faket, -1) == 0) { sigset_t set; /* child */ xclose(pvec[0]); (void) dmove(pvec[1], 1); (void) dmove(SHDIAG, 2); initdesc(); sigemptyset(&set); sigaddset(&set, SIGINT); (void)sigprocmask(SIG_UNBLOCK, &set, NULL); #ifdef SIGTSTP signal(SIGTSTP, SIG_IGN); #endif #ifdef SIGTTIN signal(SIGTTIN, SIG_IGN); #endif #ifdef SIGTTOU signal(SIGTTOU, SIG_IGN); #endif xexit(bs2cmdlist(cmd)); } cleanup_until(&pvec[1]); for(;;) { int old_pintr_disabled; if (setintr) pintr_push_enable(&old_pintr_disabled); icnt = xread(pvec[0], tibuf, sizeof(tibuf)); if (setintr) cleanup_until(&old_pintr_disabled); if (icnt <= 0) break; for (i = 0; i < icnt; i++) xputchar((unsigned char) tibuf[i]); } cleanup_until(&pvec[0]); pwait(); flush(); cleanup_until(globbed); }
/*ARGSUSED*/ void domigrate(Char **v, struct command *c) { struct sf *st; char *s; Char *cp; struct process *pp; int err1 = 0; int pid = 0; siteno_t new_site = 0; pchild_disabled++; cleanup_push(&pchild_disabled, disabled_cleanup); if (setintr) { pintr_disabled++; cleanup_push(&pintr_disabled, disabled_cleanup); } ++v; if (*v[0] == '-') { /* * Do the -site. */ s = short2str(&v[0][1]); /* * see comment in setspath() */ dont_free = 1; if ((st = sfname(s)) == NULL) { dont_free = 0; setname(s); stderror(ERR_NAME | ERR_STRING, CGETS(23, 7, "Site not found")); } dont_free = 0; new_site = st->sf_id; ++v; } if (!*v || *v[0] == '\0') { if (migratepid(0, new_site) == -1) err1++; } else { Char **globbed; v = glob_all_or_error(v); globbed = v; cleanup_push(globbed, blk_cleanup); while (v && (cp = *v)) { if (*cp == '%') { pp = pfind(cp); if (kill3(- pp->p_jobid, SIGMIGRATE, new_site) < 0) { xprintf("%S: %s\n", cp, strerror(errno)); err1++; } } else if (!(Isdigit(*cp) || *cp == '-')) stderror(ERR_NAME | ERR_JOBARGS); else { pid = atoi(short2str(cp)); if (migratepid(pid, new_site) == -1) err1++; } v++; } cleanup_until(globbed); } done: cleanup_until(&pchild_disabled); if (err1) stderror(ERR_SILENT); }