void read_motd(char *filename) { int fd, len; register aMotd *temp, *last; struct stat Sb; char line[80]; register char *tmp; if ((fd = open(filename, O_RDONLY)) == -1) return; if (fstat(fd, &Sb) == -1) { close(fd); return; } if (Sb.st_mtime <= motd_mtime) { close(fd); return; } motd_mtime = Sb.st_mtime; for(;motd != NULL;motd=last) { last = motd->next; MyFree(motd->line); MyFree(motd); } (void)dgets(-1, NULL, 0); /* make sure buffer is at empty pos */ last = NULL; while ((len=dgets(fd, line, sizeof(line)-1)) > 0) { if ((tmp = strchr(line, '\n')) != NULL) *tmp = (char) 0; else if ((tmp = strchr(line, '\r')) != NULL) *tmp = (char) 0; else line[len] = '\0'; temp = (aMotd *)MyMalloc(sizeof(aMotd)); temp->line = mystrdup(line); temp->next = NULL; if (!motd) motd = temp; else last->next = temp; last = temp; } (void)dgets(-1, NULL, 0); /* make sure buffer is at empty pos */ close(fd); }
/* * This is the back end to do_processes, saves some repeated code */ static void handle_filedesc (Process *proc, int *fd, int hook_nonl, int hook_nl) { char exec_buffer[IO_BUFFER_SIZE + 1]; ssize_t len; int ofs; const char *callback = NULL; int hook = -1; int l; char logical_name[1024]; const char *utf8_text; char *extra = NULL; /* No buffering! */ switch ((len = dgets(*fd, exec_buffer, IO_BUFFER_SIZE, 0))) { case -1: /* Something died */ { *fd = new_close(*fd); if (proc->p_stdout == -1 && proc->p_stderr == -1) proc->dumb = 1; return; /* PUNT! */ } case 0: /* We didnt get a full line */ { /* * XXX This is a hack. dgets() can return 0 for a line * containing solely a newline, as well as a line that didn't * have a newline. So we have to check to see if the line * contains only a newline! */ if (exec_buffer[0] != '\n') { if (hook_nl == EXEC_LIST) { if (proc->stdoutpc && *proc->stdoutpc) callback = proc->stdoutpc; } else if (hook_nl == EXEC_ERRORS_LIST) { if (proc->stderrpc && *proc->stderrpc) callback = proc->stderrpc; } hook = hook_nonl; break; } /* XXX HACK -- Line contains only a newline. */ *exec_buffer = 0; /* FALLTHROUGH */ } default: /* We got a full line */ { if (hook_nl == EXEC_LIST) { if (proc->stdoutc && *proc->stdoutc) callback = proc->stdoutc; } else if (hook_nl == EXEC_ERRORS_LIST) { if (proc->stderrc && *proc->stderrc) callback = proc->stderrc; } hook = hook_nl; break; } } ofs = from_server; from_server = proc->server; if (proc->refnum) l = message_setall(proc->refnum, NULL, LEVEL_OTHER); else l = message_from(NULL, LEVEL_OTHER); proc->counter++; while (len > 0 && (exec_buffer[len - 1] == '\n' || exec_buffer[len - 1] == '\r')) exec_buffer[--len] = 0; index_to_target(proc->index, logical_name, sizeof(logical_name)); utf8_text = inbound_recode(logical_name, proc->server, empty_string, exec_buffer, &extra); if (proc->redirect) redirect_text(proc->server, proc->who, utf8_text, proc->redirect, 1); if (callback) call_lambda_command("EXEC", callback, utf8_text); else if (proc->logical) { if ((do_hook(hook, "%s %s", proc->logical, utf8_text))) if (!proc->redirect) put_it("%s", utf8_text); } else { if ((do_hook(hook, "%d %s", proc->index, utf8_text))) if (!proc->redirect) put_it("%s", utf8_text); } new_free(&extra); pop_message_from(l); from_server = ofs; }
/* * do_processes: given a set of read-descriptors (as returned by a call to * select()), this will determine which of the process has produced output * and will hadle it appropriately */ void do_processes(fd_set * rd) { int i, flag; char exec_buffer[INPUT_BUFFER_SIZE + 1]; char *ptr; Process *proc; int old_timeout; if (process_list == NULL) return; old_timeout = dgets_timeout(1); for (i = 0; i < process_list_size; i++) { if ((proc = process_list[i]) && proc->p_stdout != -1) { if (FD_ISSET(proc->p_stdout, rd)) { /* * This switch case indented back to allow for 80 columns, * phone, jan 1993. */ switch (dgets(exec_buffer, INPUT_BUFFER_SIZE, proc->p_stdout, NULL)) { case 0: if (proc->p_stderr == -1) { proc->p_stdin = exec_close(proc->p_stdin); proc->p_stdout = exec_close(proc->p_stdout); if (proc->exited) delete_process(i); } else proc->p_stdout = exec_close(proc->p_stdout); break; case -1: if (proc->logical) flag = do_hook(EXEC_PROMPT_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_PROMPT_LIST, "%d %s", i, exec_buffer); set_prompt_by_refnum(proc->refnum, exec_buffer); update_input(UPDATE_ALL); /* if (flag == 0) */ break; default: message_to(proc->refnum); proc->counter++; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) { *ptr = (char) 0; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) *ptr = (char) 0; } if (proc->logical) flag = do_hook(EXEC_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_LIST, "%d %s", i, exec_buffer); if (flag) { if (proc->redirect) { int server; server = from_server; from_server = proc->server; send_text(proc->who, stripansicodes(exec_buffer), proc->redirect, 1, 1); from_server = server; } else put_it("%s", stripansicodes(exec_buffer)); } message_to(0); break; } /* end of special intendation */ } } if (process_list && i < process_list_size && (proc = process_list[i]) && proc->p_stderr != -1) { if (FD_ISSET(proc->p_stderr, rd)) { /* Do the intendation on this switch as well */ switch (dgets(exec_buffer, INPUT_BUFFER_SIZE, proc->p_stderr, NULL)) { case 0: if (proc->p_stdout == -1) { proc->p_stderr = exec_close(proc->p_stderr); proc->p_stdin = exec_close(proc->p_stdin); if (proc->exited) delete_process(i); } else proc->p_stderr = exec_close(proc->p_stderr); break; case -1: if (proc->logical) flag = do_hook(EXEC_PROMPT_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_PROMPT_LIST, "%d %s", i, exec_buffer); set_prompt_by_refnum(proc->refnum, exec_buffer); update_input(UPDATE_ALL); if (flag == 0) break; default: message_to(proc->refnum); (proc->counter)++; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) { *ptr = (char) 0; ptr = exec_buffer + strlen(exec_buffer) - 1; if ((*ptr == '\n') || (*ptr == '\r')) *ptr = (char) 0; } if (proc->logical) flag = do_hook(EXEC_ERRORS_LIST, "%s %s", proc->logical, exec_buffer); else flag = do_hook(EXEC_ERRORS_LIST, "%d %s", i, exec_buffer); if (flag) { if (proc->redirect) { int server; server = from_server; from_server = proc->server; send_text(proc->who, stripansicodes(exec_buffer), process_list[i]->redirect, 1, 1); from_server = server; } else put_it("%s", stripansicodes(exec_buffer)); } message_to(0); break; } /* End of indentation for 80 columns */ } } } check_process_limits(); (void) dgets_timeout(old_timeout); }
/* * This is the back end to do_processes, saves some repeated code */ static void handle_filedesc (Process *proc, int *fd, int hook_nonl, int hook_nl) { char exec_buffer[IO_BUFFER_SIZE + 1]; ssize_t len; switch ((len = dgets(*fd, exec_buffer, IO_BUFFER_SIZE, 0, NULL))) /* No buffering! */ { case -1: /* Something died */ { *fd = new_close(*fd); if (proc->p_stdout == -1 && proc->p_stderr == -1) proc->dumb = 1; break; } case 0: /* We didnt get a full line */ { /* * XXX XXX Major, world class hack here XXX XXX * Since this problem has been addressed already * by newio2.c, it doesn't matter if we just fix the * symptom here since this code is all obsolete. */ if (exec_buffer[0] == '\n') { exec_buffer[0] = 0; goto this_sucks; } if (hook_nl == EXEC_LIST && proc->stdoutpc && *proc->stdoutpc) parse_line("EXEC", proc->stdoutpc, exec_buffer, 0, 0); else if (hook_nl == EXEC_ERRORS_LIST && proc->stderrpc && *proc->stderrpc) parse_line("EXEC", proc->stderrpc, exec_buffer, 0, 0); else if (proc->logical) do_hook(hook_nonl, "%s %s", proc->logical, exec_buffer); else do_hook(hook_nonl, "%d %s", proc->index, exec_buffer); set_prompt_by_refnum(proc->refnum, exec_buffer); break; } default: /* We got a full line */ this_sucks: { int ofs; ofs = from_server; from_server = proc->server; if (proc->refnum) message_to(proc->refnum); proc->counter++; while (len > 0 && (exec_buffer[len] == '\n' || exec_buffer[len] == '\r')) { exec_buffer[len--] = 0; } if (proc->redirect) redirect_text(proc->server, proc->who, exec_buffer, proc->redirect, 1); if (hook_nl == EXEC_LIST && proc->stdoutc && *proc->stdoutc) parse_line("EXEC", proc->stdoutc, exec_buffer, 0, 0); else if (hook_nl == EXEC_ERRORS_LIST && proc->stderrc && *proc->stderrc) parse_line("EXEC", proc->stderrc, exec_buffer, 0, 0); else if (proc->logical) { if ((do_hook(hook_nl, "%s %s", proc->logical, exec_buffer))) if (!proc->redirect) put_it("%s", exec_buffer); } else { if ((do_hook(hook_nl, "%d %s", proc->index, exec_buffer))) if (!proc->redirect) put_it("%s", exec_buffer); } message_to(-1); from_server = ofs; } } }