AmendHub

Download:

jcs

/

subtext

/

amendments

/

34

session: Switch to session_{printf,output,output_string}

Also finish session_bar to concatenate both sides of the bar

jcs made amendment 34 over 2 years ago
--- console.c Wed Dec 15 14:34:35 2021 +++ console.c Wed Dec 15 15:21:23 2021 @@ -694,10 +694,10 @@ console_parse_csi(struct console *console) case 'n': /* DSR - device status report */ switch (param1) { case 5: /* terminal is ready */ - session_output(console->session, "\33[0n"); + session_output(console->session, "\33[0n", 4); break; case 6: /* CPR - report cursor position */ - session_output(console->session, "\33[%d;%dR", + session_printf(console->session, "\33[%d;%dR", console->cursor_line + 1, console->cursor_column + 1); break; } --- session.c Wed Dec 15 14:36:25 2021 +++ session.c Wed Dec 15 15:41:30 2021 @@ -85,10 +85,10 @@ session_run(struct uthread *uthread, void *arg) len = session_load_view(s, DB_TEXT_ISSUE_ID, &view); if (len) { - session_output_formatted(s, view, len); + session_output(s, view, len); free(view); } else { - session_output(s, "\r\n" + session_printf(s, "\r\n" "Welcome to %s (%s)\r\n" "\r\n", db->config.name, s->node); } @@ -96,7 +96,7 @@ session_run(struct uthread *uthread, void *arg) if (s->autologin_username) { s->user = user_find(db, s->autologin_username); if (!s->user) { - session_output(s, "Failed to find autologin user\r\n"); + session_output_string(s, "Failed to find autologin user\r\n"); session_close(&s); return; } @@ -119,26 +119,26 @@ session_run(struct uthread *uthread, void *arg) } sessions_tally++; - session_output(s, "Welcome, %s%s%s, you are the %d%s caller today.\r\n", + session_printf(s, "Welcome, %s%s%s, you are the %d%s caller today.\r\n", ansi(s, ANSI_BOLD, NULL), s->user ? s->user->username : GUEST_USERNAME, ansi(s, ANSI_RESET, NULL), sessions_tally, ordinal(sessions_tally)); - session_output(s, "\r\n[ menu goes here ]\r\n\r\n"); - + session_output_string(s, "\r\n[ menu goes here ]\r\n\r\n"); + while (!done) { - session_output(s, "Main Menu> "); + session_output_string(s, "Main Menu> "); c = session_input_char(s); - session_output(s, "%c\r\n", c, 2); + session_printf(s, "%c\r\n", c); /* TODO: make letter->command dynamic from a resource */ switch (c) { case 'g': case 'G': /* goodbye */ - session_output(s, "Goodbye!\r\n"); + session_output_string(s, "Goodbye!\r\n"); done = 1; break; case 'w': @@ -148,7 +148,7 @@ session_run(struct uthread *uthread, void *arg) case '\r': break; default: - session_output(s, "Invalid option\r\n"); + session_output_string(s, "Invalid option\r\n"); break; } } @@ -192,7 +192,7 @@ session_close(struct session **session) } size_t -session_output(struct session *session, const char *format, ...) +session_printf(struct session *session, const char *format, ...) { va_list ap; size_t len; @@ -203,12 +203,11 @@ session_output(struct session *session, const char *fo if (len >= sizeof(session_tbuf)) panic("sprintf overflow in session_output!"); - return session_output_formatted(session, session_tbuf, len); + return session_output(session, session_tbuf, len); } size_t -session_output_formatted(struct session *session, const char *str, - size_t len) +session_output(struct session *session, const char *str, size_t len) { size_t chunk, olen = len, stroff = 0; @@ -235,6 +234,13 @@ session_output_formatted(struct session *session, cons return olen; } +size_t +session_output_string(struct session *session, const char *str) +{ + size_t len = strlen(str); + return session_output(session, str, len); +} + char session_input_char(struct session *session) { @@ -297,7 +303,7 @@ session_field_input(struct session *session, unsigned /* TODO */ } else /* back one, space, back one (\33 oct = \e) */ - session_output_formatted(session, "\33[D \33[D", 7); + session_output(session, "\33[D \33[D", 7); break; case '\r': @@ -316,7 +322,7 @@ session_field_input(struct session *session, unsigned ipos++; ilen++; field[ipos] = '\0'; - session_output_formatted(session, mask ? &mask : (char *)&c, 1); + session_output(session, mask ? &mask : (char *)&c, 1); } } @@ -334,9 +340,9 @@ session_login(struct session *s) short n; for (n = 1; n <= 3; n++) { - session_output(s, "login: "); + session_output_string(s, "login: "); username = session_field_input(s, 32, 0); - session_output(s, "\r\n"); + session_output(s, "\r\n", 2); if (username[0] == '\0') { n--; @@ -353,9 +359,9 @@ session_login(struct session *s) user = user_find(db, username); } - session_output(s, "Password: "); + session_output_string(s, "Password: "); password = session_field_input(s, 64, '*'); - session_output(s, "\r\n"); + session_output(s, "\r\n", 2); if (user) { if (user_authenticate(db, user, password) == AUTH_USER_OK) @@ -376,10 +382,10 @@ session_login(struct session *s) return AUTH_USER_OK; uthread_msleep(60); - session_output(s, "Login incorrect\r\n"); + session_output_string(s, "Login incorrect\r\n"); } - session_output(s, "Thanks for playing\r\n"); + session_output_string(s, "Thanks for playing\r\n"); return AUTH_USER_FAILED; } @@ -393,43 +399,68 @@ session_login(struct session *s) char * session_bar(struct session *s, char *left_str, char *right_str) { - static char bar[128]; - char *strip; - short len, bold, n, llen, rlen, pad; + static char sides[2][128]; + static char bar[256]; + char *str, *side; + short sstrlen, sidelen, bold, n, nside, barlen, pad; - len = sprintf(bar, "\r%s", ansi(s, ANSI_REVERSE, NULL)); + memset(bar, 0, sizeof(bar)); + barlen = sprintf(bar, "\r%s", ansi(s, ANSI_REVERSE, NULL)); - bold = 0; - llen = strlen(left_str); - rlen = (right_str == NULL ? 0 : strlen(right_str)); - for (n = 0; n < llen; n++) { - if (left_str[n] == '\b') { - if (bold) - len = strlcat(bar, ansi(s, ANSI_RESET, ANSI_REVERSE, NULL), - sizeof(bar)); - else - len = strlcat(bar, ansi(s, ANSI_BOLD, NULL), sizeof(bar)); + for (nside = 0; nside < 2; nside++) { + if (nside == 0) + str = left_str; + else + str = right_str; - bold = !bold; - } else { - bar[len] = left_str[n]; - len++; + side = (char *)&sides[nside]; + side[0] = '\0'; + + if (str == NULL) + continue; + + bold = 0; + sstrlen = strlen(str); + sidelen = 0; + + for (n = 0; n < sstrlen; n++) { + if (str[n] == '\b') { + if (bold) + sidelen = strlcat(side, + ansi(s, ANSI_RESET, ANSI_REVERSE, NULL), + sizeof(sides[0])); + else + sidelen = strlcat(side, ansi(s, ANSI_BOLD, NULL), + sizeof(sides[0])); + + bold = !bold; + } else { + side[sidelen++] = str[n]; + } + + if (sidelen >= sizeof(sides[0])) + panic("session_bar: side %d overflow!", nside); } - if (len >= sizeof(bar)) - panic("session_bar: overflow!"); + side[sidelen] = '\0'; } - bar[len] = '\0'; - /* TODO: right_str */ + pad = s->terminal_cols; + if (left_str) + pad -= ansi_strip(sides[0], NULL); + if (right_str) + pad -= ansi_strip(sides[1], NULL); + + barlen = strlcat(bar, sides[0], sizeof(bar)); + + for (; pad > 0 && barlen < sizeof(bar); pad--) + bar[barlen++] = ' '; - pad = s->terminal_cols - ansi_strip(bar, NULL); - for (; pad > 0 && len < sizeof(bar); pad--) - bar[len++] = ' '; - bar[len] = '\0'; - len = strlcat(bar, ansi(s, ANSI_RESET, NULL), sizeof(bar)); - if (len > sizeof(bar)) - panic("session_bar: overflow!"); + barlen = strlcat(bar, sides[1], sizeof(bar)); + barlen = strlcat(bar, ansi(s, ANSI_RESET, NULL), sizeof(bar)); + + if (barlen > sizeof(bar)) + panic("session_bar: bar overflow!"); return bar; } @@ -492,20 +523,20 @@ session_pause_return(struct session *s, short enforce, { unsigned char c; - session_output(s, "%sPress %s<Enter>%s ", + session_printf(s, "%sPress %s<Enter>%s ", ansi(s, ANSI_RESET, NULL), ansi(s, ANSI_BOLD, NULL), ansi(s, ANSI_RESET, NULL)); if (msg) - session_output_formatted(s, msg, strlen(msg)); + session_output_string(s, msg); else - session_output(s, "to return to the main menu..."); + session_output_string(s, "to return to the main menu..."); for (;;) { c = session_input_char(s); if (!enforce || c == '\r') break; } - session_output_formatted(s, "\r\n", 2); + session_output(s, "\r\n", 2); } void @@ -515,10 +546,12 @@ session_who(struct session *s) unsigned long idle; short n; - session_output(s, "%sWho's Online%s\r\n", + session_printf(s, + "%sWho's Online%s\r\n", ansi(s, ANSI_BOLD, NULL), ansi(s, ANSI_RESET, NULL)); - session_output(s, "%sNode User Via Speed Idle%s\r\n", + session_printf(s, + "%sNode User Via Speed Idle%s\r\n", ansi(s, ANSI_BOLD, NULL), ansi(s, ANSI_RESET, NULL)); for (n = 0; n < nsessions; n++) { @@ -532,7 +565,7 @@ session_who(struct session *s) else sprintf(idle_s, "%ldd", idle / (60 * 60 * 24)); - session_output(s, "%-7s %-20s %-7s %-6d %-6s\r\n", + session_printf(s, "%-7s %-20s %-7s %-6d %-6s\r\n", sessions[n]->node, sessions[n]->user ? sessions[n]->user->username : "guest", sessions[n]->via, @@ -540,6 +573,6 @@ session_who(struct session *s) idle_s); } - session_output_formatted(s, "\r\n", 2); + session_output(s, "\r\n", 2); session_pause_return(s, 0, NULL); } --- session.h Wed Dec 15 14:33:53 2021 +++ session.h Wed Dec 15 15:14:32 2021 @@ -84,9 +84,9 @@ struct session *session_create(char *node, char *via, void session_close(struct session **session); void session_idle(struct session *session); char session_input_char(struct session *session); -size_t session_output(struct session *session, const char *format, ...); -size_t session_output_formatted(struct session *session, const char *str, - size_t len); +size_t session_printf(struct session *session, const char *format, ...); +size_t session_output(struct session *session, const char *str, size_t len); +size_t session_output_string(struct session *session, const char *str); char *session_field_input(struct session *session, unsigned short len, char mask); char *session_bar(struct session *s, char *left_str, char *right_str); --- telnet.c Mon Dec 13 09:13:45 2021 +++ telnet.c Wed Dec 15 15:22:52 2021 @@ -527,7 +527,7 @@ telnet_output_iac(struct session *session, char *iacs, node->sending_iac = 1; - session_output_formatted(session, iacs, len); + session_output(session, iacs, len); while (session->obuflen) telnet_output(session);