Index: cli.c =================================================================== RCS file: /usr/cvsroot/asterisk/cli.c,v retrieving revision 1.30 diff -u -r1.30 cli.c --- cli.c 16 Jan 2004 01:24:00 -0000 1.30 +++ cli.c 29 Jan 2004 03:08:23 -0000 @@ -34,14 +34,73 @@ #define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \ " on a " BUILD_MACHINE " running " BUILD_OS + +static char cliprefix[16384]; +static short int clinl[16384]; + +void ast_cli_set_prefix(int fd, char pref) { + if (cliprefix[0] != '\0') { + memset(cliprefix,0,sizeof(cliprefix)); + }; + if (clinl[0] != 0x1) { + memset(clinl,0x1,sizeof(clinl)); + }; + + if (fd >= 0 && fd < 16384) { + cliprefix[fd] = pref; + clinl[fd] = pref ? 0x1 : 0x0; + ast_log(LOG_DEBUG,"Set fd %d to %c",fd,pref); + } else { + ast_log(LOG_WARNING, "Attempted to assign prefix to fd that was too large\n"); + }; +}; void ast_cli(int fd, char *fmt, ...) { char *stuff; va_list ap; + + if (cliprefix[0] != '\0') { + memset(cliprefix,0,sizeof(cliprefix)); + }; + if (clinl[0] != 0x1) { + memset(clinl,0x1,sizeof(clinl)); + }; + va_start(ap, fmt); vasprintf(&stuff, fmt, ap); va_end(ap); - write(fd, stuff, strlen(stuff)); + if (cliprefix[fd]) { + char *cur; + char *next = stuff; + char delim = '\n'; + int nl = 0x0; + int len; + int first = 1; + if (clinl[fd]) { + clinl[fd] = 0x0; + nl = 0x1; + }; + + while ((cur = next)) { + next = strchr(next,delim); + if (next) { + next++; + len = next - cur; + nl = 1; + } else { + len = strlen(cur); + if (!first) nl = 0; + }; + if (len && nl) { + write(fd, &(cliprefix[fd]), 1); + }; + write(fd, cur, len); + first = 1; + }; + clinl[fd] = nl; + } else { + write(fd, stuff, strlen(stuff)); + }; free(stuff); } Index: manager.c =================================================================== RCS file: /usr/cvsroot/asterisk/manager.c,v retrieving revision 1.37 diff -u -r1.37 manager.c --- manager.c 29 Jan 2004 00:28:51 -0000 1.37 +++ manager.c 29 Jan 2004 03:08:24 -0000 @@ -407,9 +407,11 @@ s->blocking = 1; ast_mutex_unlock(&s->lock); ast_cli(s->fd, "Response: Follows\r\n"); - /* FIXME: Wedge a ActionID response in here, waiting for later changes */ + ast_cli_set_prefix(s->fd,':'); ast_cli_command(s->fd, cmd); - ast_cli(s->fd, "--END COMMAND--\r\n\r\n"); + ast_cli(s->fd, "--END COMMAND--"); + ast_cli_set_prefix(s->fd,'\0'); + ast_cli(s->fd, "\r\n\r\n"); ast_mutex_lock(&s->lock); s->blocking = 0; ast_mutex_unlock(&s->lock); Index: include/asterisk/cli.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/cli.h,v retrieving revision 1.5 diff -u -r1.5 cli.h --- include/asterisk/cli.h 10 Sep 2003 05:24:49 -0000 1.5 +++ include/asterisk/cli.h 29 Jan 2004 03:08:24 -0000 @@ -20,6 +20,7 @@ #include +extern void ast_cli_set_prefix(int fd, char pref); extern void ast_cli(int fd, char *fmt, ...) __attribute__ ((format (printf, 2, 3)));