#include "config.h"
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "../vi/vi.h"
void
msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
{
static int reenter;
GS *gp;
size_t blen, len, mlen, nlen;
const char *p;
char *bp, *mp;
va_list ap;
if (sp == NULL) {
gp = NULL;
if (mt == M_BERR)
mt = M_ERR;
else if (mt == M_VINFO)
mt = M_INFO;
} else {
gp = sp->gp;
switch (mt) {
case M_BERR:
if (F_ISSET(sp, SC_VI) && !O_ISSET(sp, O_VERBOSE)) {
F_SET(gp, G_BELLSCHED);
return;
}
mt = M_ERR;
break;
case M_VINFO:
if (!O_ISSET(sp, O_VERBOSE))
return;
mt = M_INFO;
case M_INFO:
if (F_ISSET(sp, SC_EX_SILENT))
return;
break;
case M_ERR:
case M_SYSERR:
break;
default:
abort();
}
}
if (reenter++)
return;
nlen = 1024;
if (0) {
retry: FREE_SPACE(sp, bp, blen);
nlen *= 2;
}
bp = NULL;
blen = 0;
GET_SPACE_GOTO(sp, bp, blen, nlen);
#define REM (blen - mlen)
mp = bp;
mlen = 0;
if (mt == M_SYSERR) {
p = "Error: ";
len = strlen(p);
if (REM < len)
goto retry;
memcpy(mp, p, len);
mp += len;
mlen += len;
}
if ((mt == M_ERR || mt == M_SYSERR) &&
sp != NULL && gp != NULL && gp->if_name != NULL) {
for (p = gp->if_name; *p != '\0'; ++p) {
len = snprintf(mp, REM, "%s", KEY_NAME(sp, *p));
mp += len;
if ((mlen += len) > blen)
goto retry;
}
len = snprintf(mp, REM, ", %d: ", gp->if_lno);
mp += len;
if ((mlen += len) > blen)
goto retry;
}
if (fmt == NULL) {
len = 0;
goto nofmt;
}
va_start(ap, fmt);
len = vsnprintf(mp, REM, fmt, ap);
va_end(ap);
if (len >= nlen)
goto retry;
nofmt: mp += len;
if ((mlen += len) > blen)
goto retry;
if (mt == M_SYSERR) {
len = snprintf(mp, REM, ": %s", strerror(errno));
mp += len;
if ((mlen += len) > blen)
goto retry;
mt = M_ERR;
}
if ((mlen += 1) > blen)
goto retry;
*mp = '\n';
if (sp != NULL)
(void)ex_fflush(sp);
if (gp != NULL)
gp->scr_msg(sp, mt, bp, mlen);
else
(void)fprintf(stderr, "%.*s", (int)mlen, bp);
FREE_SPACE(sp, bp, blen);
alloc_err:
reenter = 0;
}
void
msgq_str(SCR *sp, mtype_t mtype, char *str, char *fmt)
{
int nf, sv_errno;
char *p;
if (str == NULL) {
msgq(sp, mtype, fmt);
return;
}
sv_errno = errno;
p = msg_print(sp, str, &nf);
errno = sv_errno;
msgq(sp, mtype, fmt, p);
if (nf)
FREE_SPACE(sp, p, 0);
}
void
mod_rpt(SCR *sp)
{
static char * const action[] = {
"added",
"changed",
"deleted",
"joined",
"moved",
"shifted",
"yanked",
};
static char * const lines[] = {
"line",
"lines",
};
recno_t total;
u_long rptval;
int first, cnt;
size_t blen, len, tlen;
const char *t;
char * const *ap;
char *bp, *p;
if (F_ISSET(sp, SC_EX_SILENT))
return;
sp->rptlchange = OOBLNO;
#define ARSIZE(a) sizeof(a) / sizeof (*a)
#define MAXNUM 25
rptval = O_VAL(sp, O_REPORT);
for (cnt = 0, total = 0; cnt < ARSIZE(action); ++cnt)
total += sp->rptlines[cnt];
if (total == 0)
return;
if (total <= rptval && sp->rptlines[L_YANKED] < rptval) {
for (cnt = 0; cnt < ARSIZE(action); ++cnt)
sp->rptlines[cnt] = 0;
return;
}
GET_SPACE_GOTO(sp, bp, blen, sizeof(action) * MAXNUM + 1);
for (p = bp, first = 1, tlen = 0,
ap = action, cnt = 0; cnt < ARSIZE(action); ++ap, ++cnt)
if (sp->rptlines[cnt] != 0) {
if (first)
first = 0;
else {
*p++ = ';';
*p++ = ' ';
tlen += 2;
}
len = snprintf(p, MAXNUM, "%u ", sp->rptlines[cnt]);
p += len;
tlen += len;
t = lines[sp->rptlines[cnt] == 1 ? 0 : 1];
len = strlen(t);
memcpy(p, t, len);
p += len;
tlen += len;
*p++ = ' ';
++tlen;
len = strlen(*ap);
memcpy(p, *ap, len);
p += len;
tlen += len;
sp->rptlines[cnt] = 0;
}
*p = '\n';
++tlen;
(void)ex_fflush(sp);
sp->gp->scr_msg(sp, M_INFO, bp, tlen);
FREE_SPACE(sp, bp, blen);
alloc_err:
return;
#undef ARSIZE
#undef MAXNUM
}
void
msgq_status(SCR *sp, recno_t lno, u_int flags)
{
recno_t last;
size_t blen, len;
int cnt, needsep;
const char *t;
char **ap, *bp, *np, *p, *s, *ep;
len = strlen(sp->frp->name);
GET_SPACE_GOTO(sp, bp, blen, len * MAX_CHARACTER_COLUMNS + 128);
p = bp;
ep = bp + blen;
for (t = sp->frp->name; *t != '\0'; ++t) {
len = KEY_LEN(sp, *t);
memcpy(p, KEY_NAME(sp, *t), len);
p += len;
}
np = p;
*p++ = ':';
*p++ = ' ';
if (F_ISSET(sp, SC_STATUS_CNT) && sp->argv != NULL) {
for (cnt = 0, ap = sp->argv; *ap != NULL; ++ap, ++cnt);
if (cnt > 1) {
(void)snprintf(p, ep - p, "%d files to edit", cnt);
p += strlen(p);
*p++ = ':';
*p++ = ' ';
}
F_CLR(sp, SC_STATUS_CNT);
}
needsep = 0;
if (F_ISSET(sp->frp, FR_NEWFILE)) {
F_CLR(sp->frp, FR_NEWFILE);
len = strlen("new file");
memcpy(p, "new file", len);
p += len;
needsep = 1;
} else {
if (F_ISSET(sp->frp, FR_NAMECHANGE)) {
len = strlen("name changed");
memcpy(p, "name changed", len);
p += len;
needsep = 1;
}
if (needsep) {
*p++ = ',';
*p++ = ' ';
}
t = (F_ISSET(sp->ep, F_MODIFIED)) ? "modified" : "unmodified";
len = strlen(t);
memcpy(p, t, len);
p += len;
needsep = 1;
}
if (F_ISSET(sp->frp, FR_UNLOCKED)) {
if (needsep) {
*p++ = ',';
*p++ = ' ';
}
len = strlen("UNLOCKED");
memcpy(p, "UNLOCKED", len);
p += len;
needsep = 1;
}
if (O_ISSET(sp, O_READONLY)) {
if (needsep) {
*p++ = ',';
*p++ = ' ';
}
len = strlen("readonly");
memcpy(p, "readonly", len);
p += len;
needsep = 1;
}
if (needsep) {
*p++ = ':';
*p++ = ' ';
}
if (LF_ISSET(MSTAT_SHOWLAST)) {
if (db_last(sp, &last))
return;
if (last == 0) {
len = strlen("empty file");
memcpy(p, "empty file", len);
p += len;
} else {
(void)snprintf(p, ep - p, "line %lu of %lu [%lu%%]",
(unsigned long)lno, (unsigned long)last,
(unsigned long)(lno * 100) / last);
p += strlen(p);
}
} else {
(void)snprintf(p, ep - p, "line %lu", (unsigned long)lno);
p += strlen(p);
}
#ifdef DEBUG
(void)snprintf(p, ep - p, " (pid %ld)", (long)getpid());
p += strlen(p);
#endif
*p++ = '\n';
len = p - bp;
s = bp;
if (LF_ISSET(MSTAT_TRUNCATE) && len > sp->cols) {
for (; s < np && (*s != '/' || (p - s) > sp->cols - 3); ++s);
if (s == np) {
s = p - (sp->cols - 5);
*--s = ' ';
}
*--s = '.';
*--s = '.';
*--s = '.';
len = p - s;
}
(void)ex_fflush(sp);
sp->gp->scr_msg(sp, M_INFO, s, len);
FREE_SPACE(sp, bp, blen);
alloc_err:
return;
}
const char *
msg_cmsg(SCR *sp, cmsg_t which, size_t *lenp)
{
const char *s;
switch (which) {
case CMSG_CONF:
s = "confirm? [ynq]";
break;
case CMSG_CONT:
s = "Press any key to continue: ";
break;
case CMSG_CONT_EX:
s = "Press any key to continue [: to enter more ex commands]: ";
break;
case CMSG_CONT_R:
s = "Press Enter to continue: ";
break;
case CMSG_CONT_S:
s = " cont?";
break;
case CMSG_CONT_Q:
s = "Press any key to continue [q to quit]: ";
break;
default:
abort();
}
*lenp = strlen(s);
return s;
}
char *
msg_print(SCR *sp, const char *s, int *needfree)
{
size_t blen, nlen;
const char *cp;
char *bp, *ep, *p, *t;
*needfree = 0;
for (cp = s; *cp != '\0'; ++cp)
if (!isprint(*cp))
break;
if (*cp == '\0')
return ((char *)s);
nlen = 0;
if (0) {
retry: if (sp == NULL)
free(bp);
else
FREE_SPACE(sp, bp, blen);
*needfree = 0;
}
nlen += 256;
if (sp == NULL) {
if ((bp = malloc(nlen)) == NULL)
goto alloc_err;
blen = 0;
} else
GET_SPACE_GOTO(sp, bp, blen, nlen);
if (0) {
alloc_err: return ("");
}
*needfree = 1;
for (p = bp, ep = (bp + blen) - 1, cp = s; *cp != '\0' && p < ep; ++cp)
for (t = KEY_NAME(sp, *cp); *t != '\0' && p < ep; *p++ = *t++);
if (p == ep)
goto retry;
*p = '\0';
return (bp);
}