static void do_cite(WIN *w, int start, int end) { wchar_t tmp_line[MAXCOLS]; ELM *tmp_e; int x, y; for (y=start; y<=end; y++) { vt_send('>'); vt_send(' '); tmp_e = mc_getline(w, y); mc_wdrawelm_var(w, tmp_e, tmp_line); tmp_line[w->xs] = 0; for (x = w->xs-1; x >= 0; x--) { if (tmp_line[x] <= ' ') tmp_line[x]=0; else break; } for (x = 0; tmp_line[x]; x++) { char buf[MB_LEN_MAX]; size_t i, len; len = one_wctomb(buf, tmp_line[x]); for (i = 0; i < len; i++) vt_send(buf[i]); } vt_send(13); } }
/* * Paste text file to console/serial line. Avoid ascii-xfer problem of * swallowing up status messages returned via the serial line. * This is especially useful for Embedded Microprocessor Development Kits * that use raw file transfer mode (no protocols) to download text encoded * executable files (eg., in S-Record or Intel Hex formats) * * TC Wan <*****@*****.**> 2003-10-18 */ int paste_file(void) { FILE *fp; char line[1024]; char *s; const int dotrans = 0; const int ldelay = 1; /* hardcoded 1 ms */ char buf[128] = ""; char *ptr; int bytes_read; unsigned long bdone = 0; int x; if ((s = filedir(1, 0)) == NULL) return 0; if ((fp = fopen(s, "r")) == NULL) { perror(s); return -1; } while (fgets(line, sizeof(line), fp)) { /* Check for I/O or timer. */ x = check_io(portfd_connected, 0, 1000, buf, sizeof(buf), &bytes_read); /* Send data from the modem to the screen. */ if ((x & 1)) { ptr = buf; while (bytes_read-- > 0) { if (P_PARITY[0] == 'M' || P_PARITY[0] == 'S') *ptr &= 0x7f; vt_out(*ptr++); } mc_wflush(); } if (dotrans && (s = strrchr(line, '\n')) != NULL) { if (s > line && *(s - 1) == '\r') s--; *s = 0; s = line; for (s = line; *s; s++) vt_send(*s); vt_send('\r'); vt_send('\n'); bdone += strlen(line) + 2; } else { for (s = line; *s; s++) vt_send(*s); bdone += strlen(s); } if (ldelay) { #ifdef HAVE_USLEEP usleep(ldelay * 1000); #endif } } fclose(fp); return 0; }
/* * Run an external script. * ask = 1 if first ask for confirmation. * s = scriptname, l=loginname, p=password. */ void runscript(int ask, const char *s, const char *l, const char *p) { int status; int n, i; int pipefd[2]; char buf[81]; char scr_lines[5]; char cmdline[128]; struct pollfd fds[2]; char *translated_cmdline; char *ptr; WIN *w; int done = 0; char *msg = _("Same as last"); char *username = _(" A - Username :"******" B - Password :"******" C - Name of script :"), *question = _("Change which setting? (Return to run, ESC to stop)"); if (ask) { w = mc_wopen(10, 5, 70, 10, BDOUBLE, stdattr, mfcolor, mbcolor, 0, 0, 1); mc_wtitle(w, TMID, _("Run a script")); mc_wputs(w, "\n"); mc_wprintf(w, "%s %s\n", username, scr_user[0] ? msg : ""); mc_wprintf(w, "%s %s\n", password, scr_passwd[0] ? msg : ""); mc_wprintf(w, "%s %s\n", name_of_script, scr_name); mc_wlocate(w, 4, 5); mc_wputs(w, question); mc_wredraw(w, 1); while (!done) { mc_wlocate(w, mbslen (question) + 5, 5); n = wxgetch(); if (islower(n)) n = toupper(n); switch (n) { case '\r': case '\n': if (scr_name[0] == '\0') { mc_wbell(); break; } mc_wclose(w, 1); done = 1; break; case 27: /* ESC */ mc_wclose(w, 1); return; case 'A': mc_wlocate(w, mbslen (username) + 1, 1); mc_wclreol(w); scr_user[0] = 0; mc_wgets(w, scr_user, 32, 32); break; case 'B': mc_wlocate(w, mbslen (password) + 1, 2); mc_wclreol(w); scr_passwd[0] = 0; mc_wgets(w, scr_passwd, 32, 32); break; case 'C': mc_wlocate(w, mbslen (name_of_script) + 1, 3); mc_wgets(w, scr_name, 32, 32); break; default: break; } } } else { strncpy(scr_user, l, sizeof(scr_user)); strncpy(scr_name, s, sizeof(scr_name)); strncpy(scr_passwd, p, sizeof(scr_passwd)); } sprintf(scr_lines, "%d", (int) lines); /* jl 13.09.97 */ /* Throw away status line if temporary */ if (tempst) { mc_wclose(st, 1); tempst = 0; st = NULL; } scriptname(scr_name); pipe(pipefd); if (mcd(P_SCRIPTDIR) < 0) return; snprintf(cmdline, sizeof(cmdline), "%s %s %s %s", P_SCRIPTPROG, scr_name, logfname, logfname[0]==0? "": homedir); switch (udpid = fork()) { case -1: werror(_("Out of memory: could not fork()")); close(pipefd[0]); close(pipefd[1]); mcd(""); return; case 0: /* Child */ dup2(portfd, 0); dup2(portfd, 1); dup2(pipefd[1], 2); close(pipefd[0]); close(pipefd[1]); for (n = 1; n < _NSIG; n++) signal(n, SIG_DFL); mc_setenv("LOGIN", scr_user); mc_setenv("PASS", scr_passwd); mc_setenv("TERMLIN", scr_lines); /* jl 13.09.97 */ translated_cmdline = translate(cmdline); if (translated_cmdline != NULL) { fastexec(translated_cmdline); free(translated_cmdline); } exit(1); default: /* Parent */ break; } setcbreak(1); /* Cbreak, no echo */ enab_sig(1, 0); /* But enable SIGINT */ signal(SIGINT, udcatch); close(pipefd[1]); /* pipe output from "runscript" program to terminal emulator */ fds[0].fd = pipefd[0]; /* runscript */ fds[0].events = POLLIN; fds[1].fd = STDIN_FILENO; /* stdin */ fds[1].events = POLLIN; script_running = 1; while (script_running && poll(fds, 2, -1) > 0) for (i = 0; i < 2; i++) { if (fds[i].revents & (POLLERR | POLLHUP | POLLNVAL)) script_running = 0; else if ((fds[i].revents & POLLIN) && (n = read(fds[i].fd, buf, sizeof(buf)-1)) > 0) { ptr = buf; while (n--) if (i) vt_send(*ptr++); else vt_out(*ptr++); timer_update(); mc_wflush(); } } /* Collect status, and clean up. */ m_wait(&status); enab_sig(0, 0); signal(SIGINT, SIG_IGN); setcbreak(2); /* Raw, no echo */ close(pipefd[0]); scriptname(""); mcd(""); }