/* * Update the online time. */ static void update_status_time(void) { time_t now; time(&now); if (status_message_showing) { if (now > status_display_msg_until) { /* time over for status message, restore standard status line */ status_message_showing = 0; show_status(); } else show_status_fmt("%$"); } if (old_online == online || online <= (old_online + 59)) return; if (P_LOGCONN[0] == 'Y' && old_online >= 0 && online < 0) do_log(_("Gone offline (%ld:%02ld:%02ld)"), old_online / 3600, (old_online / 60) % 60, old_online % 60); old_online = online; if (!status_message_showing) show_status(); mc_wflush(); }
/* * Print the directory. Only draw from "cur" to bottom. */ static void prdir(WIN *dirw, int top, int cur, GETSDIR_ENTRY *dirdat, int longest) { int f, start; char f_str[BUFSIZ]; char t_str[BUFSIZ]; start = cur - top; dirflush = 0; sprintf(f_str, " %%-%ds", longest + 2); mc_wlocate(dirw, 0, start + FILE_MWTR); for (f = start; f < dirw->ys - (1 + FILE_MWTR); f++) { GETSDIR_ENTRY *d; if (!(d = getno(f + top, dirdat))) break; if (d->cflags & FL_TAG) mc_wsetattr(dirw, XA_REVERSE | stdattr); if (S_ISDIR(d->mode)) { snprintf(t_str, sizeof(t_str), "[%s]", d->fname); mc_wprintf(dirw, f_str, t_str); } else mc_wprintf(dirw, f_str, d->fname); mc_wsetattr(dirw, XA_NORMAL | stdattr); mc_wputc(dirw, '\n'); } dirflush = 1; mc_wflush(); }
/* * 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; }
/* * fmg 8/20/97 * Search history - main function that started the C-code blasphemy :-) * This function doesn't care about case/case-less status... */ void searchhist(WIN *w_hist, wchar_t *str) { int y; WIN *w_new; const char *hline; size_t i; /* Find out how big a window we must open. */ y = w_hist->y2; if (st == (WIN *)0 || (st && tempst)) y--; /* Open a Search line window. */ w_new = mc_wopen(0, y+1, w_hist->x2, y+1, 0, st_attr, sfcolor, sbcolor, 0, 0, 1); w_new->doscroll = 0; w_new->wrap = 0; hline = _("SEARCH FOR (ESC=Exit)"); mc_wprintf(w_new, "%s(%d):",hline,MAX_SEARCH); mc_wredraw(w_new, 1); mc_wflush(); mc_wlocate(w_new, mbslen(hline)+6, 0); for (i = 0; str[i] != 0; i++) mc_wputc(w_new, str[i]); mc_wlocate(w_new, mbslen(hline)+6, 0); mc_wgetwcs(w_new, str, MAX_SEARCH, MAX_SEARCH); #if 0 if (!str[0]) { /* then unchanged... must have pressed ESC... get out */ mc_wflush(); mc_wclose(w_new, 0); return; } #endif mc_wredraw(w_hist, 1); mc_wclose(w_new, 1); mc_wflush(); return; }
/* Output a string to the modem. */ static void v_termout(const char *s, int len) { const char *p; if (vt_echo) { for (p = s; *p; p++) { vt_out(*p); if (!vt_addlf && *p == '\r') vt_out('\n'); } mc_wflush(); } (*termout)(s, len); }
/* * Print one directory entry. */ static void prone(WIN *dirw, GETSDIR_ENTRY *dirdat, int longest, int inverse) { char f_str[BUFSIZ]; char t_str[BUFSIZ]; dirflush = 0; sprintf(f_str, " %%-%ds", longest + 2); /* if (dirdat->cflags & FL_TAG) mc_wsetattr(dirw, XA_REVERSE | stdattr); */ if (inverse) mc_wsetattr(dirw, XA_REVERSE | stdattr); if (S_ISDIR(dirdat->mode)) { snprintf(t_str, sizeof(t_str), "[%s]", dirdat->fname); mc_wprintf(dirw, f_str, t_str); } else mc_wprintf(dirw, f_str, dirdat->fname); mc_wsetattr(dirw, XA_NORMAL | stdattr); dirflush = 1; mc_wflush(); }
/* * The main terminal loop: * - If there are characters received send them * to the screen via the appropriate translate function. */ int do_terminal(void) { char buf[128]; int buf_offset = 0; int c; int x; int blen; int zauto = 0; static const char zsig[] = "**\030B00"; int zpos = 0; const char *s; dirflush = 0; WIN *error_on_open_window = NULL; dirty_goto: /* Show off or online time */ update_status_time(); /* If the status line was shown temporarily, delete it again. */ if (tempst) { tempst = 0; mc_wclose(st, 1); st = NULL; } /* Auto Zmodem? */ if (P_PAUTO[0] >= 'A' && P_PAUTO[0] <= 'Z') zauto = P_PAUTO[0]; /* Set the terminal modes */ setcbreak(2); /* Raw, no echo */ keyboard(KSTART, 0); /* Main loop */ while (1) { /* See if window size changed */ if (size_changed) { size_changed = 0; wrapln = us->wrap; /* I got the resize code going again! Yeah! */ mc_wclose(us, 0); us = NULL; if (st) mc_wclose(st, 0); st = NULL; mc_wclose(stdwin, 0); if (win_init(tfcolor, tbcolor, XA_NORMAL) < 0) leave(_("Could not re-initialize window system.")); /* Set the terminal modes */ setcbreak(2); /* Raw, no echo */ init_emul(terminal, 0); } /* Update the timer. */ timer_update(); /* check if device is ok, if not, try to open it */ if (!get_device_status(portfd_connected)) { /* Ok, it's gone, most probably someone unplugged the USB-serial, we * need to free the FD so that a replug can get the same device * filename, open it again and be back */ int reopen = portfd == -1; close(portfd); lockfile_remove(); portfd = -1; if (open_term(reopen, reopen, 1) < 0) { if (!error_on_open_window) error_on_open_window = mc_tell(_("Cannot open %s!"), dial_tty); } else { if (error_on_open_window) { mc_wclose(error_on_open_window, 1); error_on_open_window = NULL; } } } /* Check for I/O or timer. */ x = check_io(portfd_connected, 0, 1000, buf + buf_offset, sizeof(buf) - buf_offset, &blen); blen += buf_offset; buf_offset = 0; /* Data from the modem to the screen. */ if ((x & 1) == 1) { char obuf[sizeof(buf)]; char *ptr; if (using_iconv()) { char *otmp = obuf; size_t output_len = sizeof(obuf); size_t input_len = blen; ptr = buf; do_iconv(&ptr, &input_len, &otmp, &output_len); // something happened at all? if (output_len < sizeof(obuf)) { if (input_len) { // something remained, we need to adapt buf accordingly memmove(buf, ptr, input_len); buf_offset = input_len; } blen = sizeof(obuf) - output_len; ptr = obuf; } else ptr = buf; } else { ptr = buf; } while (blen-- > 0) { /* Auto zmodem detect */ if (zauto) { if (zsig[zpos] == *ptr) zpos++; else zpos = 0; } if (P_PARITY[0] == 'M' || P_PARITY[0] == 'S') *ptr &= 0x7f; if (display_hex) { unsigned char c = *ptr++; unsigned char u = c >> 4; c &= 0xf; vt_out(u > 9 ? 'a' + (u - 10) : '0' + u); vt_out(c > 9 ? 'a' + (c - 10) : '0' + c); vt_out(' '); } else vt_out(*ptr++); if (zauto && zsig[zpos] == 0) { dirflush = 1; keyboard(KSTOP, 0); updown('D', zauto - 'A'); dirflush = 0; zpos = 0; blen = 0; goto dirty_goto; } } mc_wflush(); }
/* * Locate the cursor at the correct position in * the user screen. */ static void ret_csr(void) { mc_wlocate(us, us->curx, us->cury); mc_wflush(); }
/* Scroll back */ static void scrollback(void) { int y,c; WIN *b_us, *b_st; ELM *tmp_e; int case_matters=0; /* fmg: case-importance, needed for 'N' */ static wchar_t look_for[MAX_SEARCH]; /* fmg: last used search pattern */ wchar_t tmp_line[MAXCOLS]; int citemode = 0; int cite_ystart = 1000000, cite_yend = -1, cite_y = 0; int inverse; int loop = 1; char hline0[128], hline1[128], *hline; static int hit=0; /* Find out how big a window we must open. */ y = us->y2; if (st == (WIN *)0 || (st && tempst)) y--; /* Open a window. */ b_us = mc_wopen(0, 0, us->x2, y, 0, us->attr, COLFG(us->color), COLBG(us->color), 0, 0, 0); mc_wcursor(b_us, CNONE); /* Open a help line window. */ b_st = mc_wopen(0, y+1, us->x2, y+1, 0, st_attr, sfcolor, sbcolor, 0, 0, 1); b_st->doscroll = 0; b_st->wrap = 0; /* Make sure help line is as wide as window. */ /* * fmg 8/20/97 * added /=Srch, \=CaseLess, and N=Next and changed rest of line... * Hope you like it :-) */ strcpy(hline0, _("HISTORY: U=Up D=Down F=PgDn B=PgUp s=Srch S=CaseLess N=Next C=Cite ESC=Exit ")); if (b_st->xs < 127) hline0[b_st->xs] = 0; hline = hline0; mc_wprintf(b_st, "%s", hline); mc_wredraw(b_st, 1); mc_wflush(); /* And do the job. */ y = us->histlines; /* fmg 8/20/97 * Needed for N)extSearch, keeps track of line on which current "hit" * is... we advance it to 'N'ext hit in find_next(). We start at "top" * of history stack */ hit = 0; drawhist(b_us, y, 0); while (loop) { c = wxgetch(); switch (c) { /* * fmg 8/22/97 * Take care of the search key: Caseless */ case '\\': case 'S': case_matters = 0; /* case-importance, ie. none :-) */ /* * fmg 8/22/97 * Take care of the search key: Exact Match */ case '/': case 's': if (!us->histlines) { mc_wbell(); werror(_("History buffer Disabled!")); break; } if (!us->histline) { mc_wbell(); werror(_("History buffer empty!")); break; } if (citemode) break; /* we need this for the case-importance-toggle to work.. */ if (c == '/' || c == 's') case_matters=1; /* case-importance, ie. DOES */ /* open up new search window... */ searchhist(b_us, look_for); /* must redraw status line... */ mc_wlocate(b_st, 0, 0); /* move back to column 0! */ mc_wprintf(b_st, "%s", hline); /* and show the above-defined hline */ mc_wredraw(b_st, 1); /* again... */ /* highlight any matches */ if (wcslen(look_for) > 1) { hit = find_next(us, b_us, y, look_for, case_matters); if (hit == -1) { mc_wbell(); mc_wflush(); hit = 0; break; } drawhist_look(b_us, hit, 1, look_for, case_matters); y = hit; } else { mc_wbell(); break; } mc_wflush(); break; /* * fmg 8/22/97 * Take care of the Next Hit key... * Popup an error window if no previous... why not start a new * search? How do we know which case-importance they wanted? */ case 'n': case 'N': /* highlight NEXT match */ if (citemode) break; if (wcslen(look_for) > 1) { hit = find_next(us, b_us, y, look_for, case_matters); if (hit == -1) { mc_wbell(); mc_wflush(); hit = 0; break; } drawhist_look(b_us, hit, 1, look_for, case_matters); y = hit; } else { /* no search pattern... */ mc_wbell(); werror(_("No previous search!\n Please 's' or 'S' first!")); } mc_wflush(); break; case 'u': case 'U': case K_UP: if (citemode && cite_y) { cite_y--; if (cite_ystart != 1000000) { cite_yend = y + cite_y; drawcite(b_us, cite_y+1, y, cite_ystart, cite_yend); drawcite(b_us, cite_y, y, cite_ystart, cite_yend); } mc_wlocate(b_us, 0, cite_y); break; } if (y <= 0) break; y--; if (cite_ystart != 1000000) cite_yend = y + cite_y; mc_wscroll(b_us, S_DOWN); /* * fmg 8/20/97 * This is needed so that the movement in window will HIGHLIGHT * the lines that have the pattern we wanted... it's just nice. * This almost beggs for a function :-) */ if (citemode) { inverse = (y+cite_y >= cite_ystart && y+cite_y <= cite_yend); } else { tmp_e = mc_getline(b_us, y); if (wcslen(look_for) > 1) { /* quick scan for pattern match */ mc_wdrawelm_var(b_us, tmp_e, tmp_line); inverse = (wcslen(tmp_line)>1 && StrStr(tmp_line, look_for, case_matters)); } else inverse = 0; } if (inverse) mc_wdrawelm_inverse(b_us, 0, mc_getline(b_us, y)); else mc_wdrawelm(b_us, 0, mc_getline(b_us, y)); if (citemode) mc_wlocate(b_us, 0, cite_y); mc_wflush(); break; case 'd': case 'D': case K_DN: if (citemode && cite_y < b_us->ys-1) { cite_y++; if (cite_ystart != 1000000) { cite_yend = y + cite_y; drawcite(b_us, cite_y-1, y, cite_ystart, cite_yend); drawcite(b_us, cite_y, y, cite_ystart, cite_yend); } mc_wlocate(b_us, 0, cite_y); break; } if (y >= us->histlines) break; y++; if (cite_ystart != 1000000) cite_yend = y + cite_y; mc_wscroll(b_us, S_UP); /* * fmg 8/20/97 * This is needed so that the movement in window will HIGHLIGHT * the lines that have the pattern we wanted... it's just nice. * This almost beggs for a function :-) */ if (citemode) { inverse = (y+cite_y >= cite_ystart && y+cite_y <= cite_yend); } else { tmp_e = mc_getline(b_us, y + b_us->ys - 1); if (wcslen(look_for) > 1) { /* quick scan for pattern match */ mc_wdrawelm_var(b_us, tmp_e, tmp_line); inverse = (wcslen(tmp_line)>1 && StrStr(tmp_line, look_for, case_matters)); } else inverse = 0; } if (inverse) mc_wdrawelm_inverse(b_us, b_us->ys - 1, mc_getline(b_us, y + b_us->ys - 1)); else mc_wdrawelm(b_us, b_us->ys - 1, mc_getline(b_us, y + b_us->ys - 1)); if (citemode) mc_wlocate(b_us, 0, cite_y); mc_wflush(); break; case 'b': case 'B': case K_PGUP: if (y <= 0) break; y -= b_us->ys; if (y < 0) y = 0; if (cite_ystart != 1000000) cite_yend = y + cite_y; /* * fmg 8/20/97 * This is needed so that the movement in window will HIGHLIGHT * the lines that have the pattern we wanted... it's just nice. * Highlight any matches */ if (wcslen(look_for) > 1 && us->histline) drawhist_look(b_us, y, 1, look_for, case_matters); else drawhist(b_us, y, 1); if (citemode) mc_wlocate(b_us, 0, cite_y); break; case 'f': case 'F': case ' ': /* filipg: space bar will go page-down... pager-like */ case K_PGDN: if (y >= us->histlines) break; y += b_us->ys; if (y > us->histlines) y=us->histlines; if (cite_ystart != 1000000) cite_yend = y + cite_y; /* * fmg 8/20/97 * This is needed so that the movement in window will HIGHLIGHT * the lines that have the pattern we wanted... it's just nice. * Highlight any matches */ if (wcslen(look_for) > 1 && us->histline) drawhist_look(b_us, y, 1, look_for, case_matters); else drawhist(b_us, y, 1); if (citemode) mc_wlocate(b_us, 0, cite_y); break; case 'C': case 'c': /* start citation mode */ if (citemode ^= 1) { cite_y = 0; cite_ystart = 1000000; cite_yend = -1; strcpy(hline1, _(" CITATION: ENTER=select start line ESC=exit ")); if (b_st->xs < 127) hline1[b_st->xs]=0; hline = hline1; } else { hline = hline0; } mc_wlocate(b_st, 0, 0); mc_wprintf(b_st, "%s", hline); mc_wredraw(b_st, 1); if (citemode) mc_wlocate(b_us, 0, cite_y); break; case 10: case 13: if (!citemode) break; if (cite_ystart == 1000000) { cite_yend = cite_ystart = y + cite_y; strcpy(hline1, _(" CITATION: ENTER=select end line ESC=exit ")); if (b_st->xs < 127) hline1[b_st->xs]=0; } else { if (cite_ystart > cite_yend) break; drawcite_whole(b_us, y, 1000000, -1); loop = 0; break; } mc_wlocate(b_st, 0, 0); mc_wprintf(b_st, "%s", hline); mc_wredraw(b_st, 1); mc_wdrawelm_inverse(b_us, cite_y, mc_getline(b_us, cite_ystart)); mc_wlocate(b_us, 0, cite_y); break; case K_ESC: if (!citemode) { loop = 0; break; } if (cite_ystart == 1000000) { citemode = 0; hline = hline0; } else { cite_ystart = 1000000; strcpy(hline1, _(" CITATION: ENTER=select start line ESC=exit ")); } drawcite_whole(b_us, y, cite_ystart, cite_yend); mc_wlocate(b_st, 0, 0); mc_wprintf(b_st, "%s", hline); mc_wredraw(b_st, 1); if (citemode) mc_wlocate(b_us, 0, cite_y); break; } } /* Cleanup. */ if (citemode) do_cite(b_us, cite_ystart, cite_yend); mc_wclose(b_us, y == us->histlines ? 0 : 1); mc_wclose(b_st, 1); mc_wlocate(us, us->curx, us->cury); mc_wflush(); mc_wredraw(us, 1); }
/* * 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(""); }