/* select() said activity; check it out */ static void read_and_process(struct client *p) { char msg[MAXHANDLE + 2 + MAXMESSAGE + 2 + 1]; char *s = myreadline(p); if (!s) return; if (p->name[0]) { sprintf(msg, "%s: %s\r\n", p->name, s); broadcast(msg, strlen(msg)); } else { strncpy(p->name, s, MAXHANDLE); p->name[MAXHANDLE] = '\0'; cleanupstr(p->name); if (p->name[0]) { printf("fd %d is using handle '%s'\n", p->fd, p->name); sprintf(msg, "chatsvr: Welcome to our new participant, %s\r\n", p->name); broadcast(msg, strlen(msg)); } else { static char botchmsg[] = "chatsvr: protocol botch\r\n"; write(p->fd, botchmsg, sizeof botchmsg - 1); printf("Disconnecting fd %d because of protocol botch in handle registration\n", p->fd); fflush(stdout); close(p->fd); removeclient(p); } } }
static void shell_readline(int argc, char **args) { int file_capacity = INIT_FILE_SIZE; char *tmpstr = (char*)malloc(file_capacity); char *leftover = NULL; while (1) { init_opline(); if (leftover != NULL) { char *tmptmpstr = myreadline(" "); tmpstr = str_join(tmptmpstr, leftover); free(leftover); leftover = NULL; free(tmptmpstr); tmptmpstr = NULL; } else { tmpstr = myreadline(">>> "); } if (tmpstr == NULL || strcmp(tmpstr, "exit") == 0) { break; } leftover = split_and_exec(argc, args, tmpstr); } FREE(tmpstr); }
int main(int argc, char *argv[]) { int i, j, doubledash = 0; char *mem, *line, *last = NULL; struct stat instat; (void)fstat(0,&instat); /* XXX */ if (S_ISCHR(instat.st_mode)) chardev = 1; if (S_ISSOCK(instat.st_mode)) sockdev = 1; for (i=1; i<argc; i++) if (argv[i][0] == '-') { if (argv[i][1] == '-' && !argv[i][2]) break; for (j=1; argv[i][j]; j++) switch (argv[i][j]) { case 'b': banner=0; break; case 'i': input=0; break; case 'c': garbage=1; break; case 'h': help=1; break; case 's': standalone=1; break; default: sprintf(sprintfbuf, "Unknown command line option: %s\n", argv[i]); show(sprintfbuf,ON_NEW_LINE); cleanup_exit(EXIT_FAILURE); } } if ((mem = (char *)malloc(MEMSIZE)) == NULL) { sprintf(sprintfbuf, "FATAL ERROR: Cannot allocate %d bytes for the Virtual Machine\n", MEMSIZE); show(sprintfbuf, ON_NEW_LINE); exit(EXIT_FAILURE); } if (banner) show("Welcome to TextPico 1.0\n\n", ON_OLD_LINE); if (help) { show ("-b: show no opening banner\n" "-c: print a message during garbage collection\n" "-h: help\n" "-i: no interactive session: only process file arguments\n" "-s: ignore #!-line to allow stand-alone executables\n", ON_OLD_LINE); exit(EXIT_SUCCESS); } (void)_PICO_INIT_(mem, MEMSIZE); assert(!_PICO_SESSION_(1)); (void)signal(SIGINT, abrt); (void)signal(SIGSEGV, segv); (void)signal(SIGILL, ill); (void)signal(SIGFPE, fpe); for (i=1; i< argc; i++) { if (argv[i][0] == '-' && !argv[i][1]) { show("ERROR: Tpico does not support \"-\" syntax", ON_NEW_LINE); continue; } if (argv[i][0] == '-' && argv[i][1] == '-' && !argv[i][2]) { doubledash = 1; continue; } if (argv[i][0] == '-' && !doubledash) continue; if (read_file(argv[i]), globalfilebuf != NULL) { msgbuf[0] = '\0'; error_loop(_PICO_DO_(1, globalfilebuf)); free(globalfilebuf); globalfilebuf = NULL; } } if (input) { if (chardev) { //rl_inhibit_completion = 1; rl_readline_name="tpico"; rl_completion_entry_function=identifiergenerator; rl_completion_append_character=0; rl_completer_quote_characters="\"'"; rl_basic_word_break_characters=" !#$%&()*+,-./:;<=>?@[\\]^`{|}~"; /* including quote characters causes [""dis] + <tab> -> [""display"] */ #ifdef RL_STATE_NONE // only defined by GNU readline _rl_enable_paren_matching(1); //rl_set_paren_blink_timeout(250000); #endif /* tilde_expand() does not work on stupid OS/X readline */ strncpy(historyfilename, getenv("HOME"), NAMELEN); historyfilename[NAMELEN-1] = '\0'; if (strlen(historyfilename)+strlen("/.tpico_history") < NAMELEN) strcpy(historyfilename+strlen(historyfilename), "/.tpico_history"); else strcpy(historyfilename, ".tpico_history"); (void)read_history(historyfilename); stifle_history(500); /* 500 history entries should be enough... */ using_history(); history_active = 1; _PICO_DO_(1,""); /* identifiergenerator will fail before first eval */ } while (!feof(stdin)) { at_prompt = 1; show("", ON_NEW_LINE); last_shown = '\0'; if ((line = myreadline(">")) == NULL) break; last_shown = LF; if (!line[0] || line[0] == CR || line[0] == LF) { free(line); continue; } msgbuf[0] = '\0'; if ((last == NULL || strcmp(line, last)) && chardev) add_history(line); at_prompt = 0; error_loop(_PICO_DO_(1, line)); if (last != NULL) free(last); last = line; } } cleanup_exit(EXIT_SUCCESS); }