2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
35 * PUBLIC: void msgq(SCR *, mtype_t, const char *, ...);
38 msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
41 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
43 const char *str; /* String pointer. */
44 size_t arg; /* Argument number. */
45 size_t prefix; /* Prefix string length. */
46 size_t skip; /* Skipped string length. */
47 size_t suffix; /* Suffix string length. */
50 static int reenter; /* STATIC: Re-entrancy check. */
52 size_t blen, len, mlen, nlen;
60 size_t cnt1, cnt2, soff;
65 * It's possible to enter msg when there's no screen to hold the
66 * message. If sp is NULL, ignore the special cases and put the
67 * message out to stderr.
73 else if (mt == M_VINFO)
79 if (F_ISSET(sp, SC_VI) && !O_ISSET(sp, O_VERBOSE)) {
80 F_SET(gp, G_BELLSCHED);
86 if (!O_ISSET(sp, O_VERBOSE))
91 if (F_ISSET(sp, SC_EX_SILENT))
103 * It's possible to reenter msg when it allocates space. We're
104 * probably dead anyway, but there's no reason to drop core.
107 * Yes, there's a race, but it should only be two instructions.
112 /* Get space for the message. */
115 retry: FREE_SPACE(sp, bp, blen);
120 GET_SPACE_GOTOC(sp, bp, blen, nlen);
125 * mp: pointer to the current next character to be written
126 * mlen: length of the already written characters
127 * blen: total length of the buffer
129 #define REM (blen - mlen)
132 if (mt == M_SYSERR) {
133 p = msg_cat(sp, "020|Error: ", &len);
142 * If we're running an ex command that the user didn't enter, display
143 * the file name and line number prefix.
145 if ((mt == M_ERR || mt == M_SYSERR) &&
146 sp != NULL && gp != NULL && gp->if_name != NULL) {
150 CHAR2INT(sp, gp->if_name, strlen(gp->if_name) + 1, wp, wlen);
151 for (; *wp != '\0'; ++wp) {
152 len = snprintf(mp, REM, "%s", KEY_NAME(sp, *wp));
154 if ((mlen += len) > blen)
157 len = snprintf(mp, REM, ", %d: ", gp->if_lno);
159 if ((mlen += len) > blen)
163 /* If nothing to format, we're done. */
166 fmt = msg_cat(sp, fmt, NULL);
170 * Nvi should run on machines that don't support the numbered argument
171 * specifications (%[digit]*$). We do this by reformatting the string
172 * so that we can hand it to vsprintf(3) and it will use the arguments
173 * in the right order. When vsprintf returns, we put the string back
174 * into the right order. It's undefined, according to SVID III, to mix
175 * numbered argument specifications with the standard style arguments,
176 * so this should be safe.
178 * In addition, we also need a character that is known to not occur in
179 * any vi message, for separating the parts of the string. As callers
180 * of msgq are responsible for making sure that all the non-printable
181 * characters are formatted for printing before calling msgq, we use a
182 * random non-printable character selected at terminal initialization
183 * time. This code isn't fast by any means, but as messages should be
184 * relatively short and normally have only a few arguments, it won't be
185 * too bad. Regardless, nobody has come up with any other solution.
187 * The result of this loop is an array of pointers into the message
188 * string, with associated lengths and argument numbers. The array
189 * is in the "correct" order, and the arg field contains the argument
192 for (p = fmt, soff = 0; soff < __NL_ARGMAX;) {
193 for (t = p; *p != '\0' && *p != '%'; ++p);
197 if (!isdigit((u_char)*p)) {
202 for (u = p; *++p != '\0' && isdigit((u_char)*p););
206 /* Up to, and including the % character. */
208 str[soff].prefix = u - t;
210 /* Up to, and including the $ character. */
211 str[soff].arg = atoi(u);
212 str[soff].skip = (p - u) + 1;
213 if (str[soff].arg >= __NL_ARGMAX)
216 /* Up to, and including the conversion character. */
217 for (u = p; (ch = *++p) != '\0';)
219 strchr("diouxXfeEgGcspn", ch) != NULL)
221 str[soff].suffix = p - u;
227 /* If no magic strings, we're done. */
231 /* Get space for the reordered strings. */
232 if ((rbp = malloc(nlen)) == NULL)
237 * Reorder the strings into the message string based on argument
241 * We ignore arguments that are out of order, i.e. if we don't find
242 * an argument, we continue. Assume (almost certainly incorrectly)
243 * that whoever created the string knew what they were doing.
246 * Brute force "sort", but since we don't expect more than one or two
247 * arguments in a string, the setup cost of a fast sort will be more
248 * expensive than the loop.
250 for (cnt1 = 1; cnt1 <= soff; ++cnt1)
251 for (cnt2 = 0; cnt2 < soff; ++cnt2)
252 if (cnt1 == str[cnt2].arg) {
253 memmove(s_rbp, str[cnt2].str, str[cnt2].prefix);
254 memmove(s_rbp + str[cnt2].prefix,
255 str[cnt2].str + str[cnt2].prefix +
256 str[cnt2].skip, str[cnt2].suffix);
257 s_rbp += str[cnt2].prefix + str[cnt2].suffix;
259 gp == NULL ? DEFAULT_NOPRINT : gp->noprint;
267 format: /* Format the arguments into the string. */
270 len = vsnprintf(mp, REM, fmt, ap);
280 * Go through the resulting string, and, for each separator character
281 * separated string, enter its new starting position and length in the
284 for (p = t = mp, cnt1 = 1,
285 ch = gp == NULL ? DEFAULT_NOPRINT : gp->noprint; *p != '\0'; ++p)
287 for (cnt2 = 0; cnt2 < soff; ++cnt2)
288 if (str[cnt2].arg == cnt1)
291 str[cnt2].prefix = p - t;
297 * Reorder the strings once again, putting them back into the
301 * Note, the length of the message gets decremented once for
302 * each substring, when we discard the separator character.
304 for (s_rbp = rbp, cnt1 = 0; cnt1 < soff; ++cnt1) {
305 memmove(rbp, str[cnt1].str, str[cnt1].prefix);
306 rbp += str[cnt1].prefix;
309 memmove(mp, s_rbp, rbp - s_rbp);
311 /* Free the reordered string memory. */
316 if ((mlen += len) > blen)
318 if (mt == M_SYSERR) {
319 len = snprintf(mp, REM, ": %s", strerror(errno));
321 if ((mlen += len) > blen)
326 /* Add trailing newline. */
327 if ((mlen += 1) > blen)
334 gp->scr_msg(sp, mt, bp, mlen);
336 (void)fprintf(stderr, "%.*s", (int)mlen, bp);
342 FREE_SPACE(sp, bp, blen);
349 * Display a message with an embedded string.
351 * PUBLIC: void msgq_wstr(SCR *, mtype_t, const CHAR_T *, const char *);
354 msgq_wstr(SCR *sp, mtype_t mtype, const CHAR_T *str, const char *fmt)
360 msgq(sp, mtype, "%s", fmt);
363 INT2CHAR(sp, str, STRLEN(str) + 1, nstr, nlen);
364 msgq_str(sp, mtype, nstr, fmt);
369 * Display a message with an embedded string.
371 * PUBLIC: void msgq_str(SCR *, mtype_t, const char *, const char *);
374 msgq_str(SCR *sp, mtype_t mtype, const char *str, const char *fmt)
380 msgq(sp, mtype, "%s", fmt);
385 p = msg_print(sp, str, &nf);
387 msgq(sp, mtype, fmt, p);
389 FREE_SPACE(sp, p, 0);
394 * Report on the lines that changed.
397 * Historic vi documentation (USD:15-8) claimed that "The editor will also
398 * always tell you when a change you make affects text which you cannot see."
399 * This wasn't true -- edit a large file and do "100d|1". We don't implement
400 * this semantic since it requires tracking each line that changes during a
401 * command instead of just keeping count.
403 * Line counts weren't right in historic vi, either. For example, given the
407 * the command 2d}, from the 'b' would report that two lines were deleted,
410 * PUBLIC: void mod_rpt(SCR *);
415 static char * const action[] = {
424 static char * const lines[] = {
431 size_t blen, len, tlen;
436 /* Change reports are turned off in batch mode. */
437 if (F_ISSET(sp, SC_EX_SILENT))
440 /* Reset changing line number. */
441 sp->rptlchange = OOBLNO;
444 * Don't build a message if not enough changed.
447 * And now, a vi clone test. Historically, vi reported if the number
448 * of changed lines was > than the value, not >=, unless it was a yank
449 * command, which used >=. No lie. Furthermore, an action was never
450 * reported for a single line action. This is consistent for actions
451 * other than yank, but yank didn't report single line actions even if
452 * the report edit option was set to 1. In addition, setting report to
453 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
454 * unknown reason (this bug was fixed in System III/V at some point).
455 * I got complaints, so nvi conforms to System III/V historic practice
456 * except that we report a yank of 1 line if report is set to 1.
458 #define ARSIZE(a) sizeof(a) / sizeof (*a)
460 rptval = O_VAL(sp, O_REPORT);
461 for (cnt = 0, total = 0; cnt < ARSIZE(action); ++cnt)
462 total += sp->rptlines[cnt];
465 if (total <= rptval && sp->rptlines[L_YANKED] < rptval) {
466 for (cnt = 0; cnt < ARSIZE(action); ++cnt)
467 sp->rptlines[cnt] = 0;
471 /* Build and display the message. */
472 GET_SPACE_GOTOC(sp, bp, blen, sizeof(action) * MAXNUM + 1);
473 for (p = bp, first = 1, tlen = 0,
474 ap = action, cnt = 0; cnt < ARSIZE(action); ++ap, ++cnt)
475 if (sp->rptlines[cnt] != 0) {
483 len = snprintf(p, MAXNUM, "%lu ",
484 (u_long)sp->rptlines[cnt]);
488 lines[sp->rptlines[cnt] == 1 ? 0 : 1], &len);
494 t = msg_cat(sp, *ap, &len);
498 sp->rptlines[cnt] = 0;
501 /* Add trailing newline. */
506 sp->gp->scr_msg(sp, M_INFO, bp, tlen);
508 FREE_SPACE(sp, bp, blen);
518 * Report on the file's status.
520 * PUBLIC: void msgq_status(SCR *, recno_t, u_int);
523 msgq_status(SCR *sp, recno_t lno, u_int flags)
529 char **ap, *bp, *np, *p, *s, *ep;
533 /* Get sufficient memory. */
534 len = strlen(sp->frp->name);
535 GET_SPACE_GOTOC(sp, bp, blen, len * MAX_CHARACTER_COLUMNS + 128);
539 /* Convert the filename. */
540 CHAR2INT(sp, sp->frp->name, len + 1, wp, wlen);
542 /* Copy in the filename. */
543 for (; *wp != '\0'; ++wp) {
544 len = KEY_LEN(sp, *wp);
545 memcpy(p, KEY_NAME(sp, *wp), len);
552 /* Copy in the argument count. */
553 if (F_ISSET(sp, SC_STATUS_CNT) && sp->argv != NULL) {
554 for (cnt = 0, ap = sp->argv; *ap != NULL; ++ap, ++cnt);
556 (void)snprintf(p, ep - p,
557 msg_cat(sp, "317|%d files to edit", NULL), cnt);
562 F_CLR(sp, SC_STATUS_CNT);
566 * See nvi/exf.c:file_init() for a description of how and when the
567 * read-only bit is set.
570 * The historic display for "name changed" was "[Not edited]".
573 if (F_ISSET(sp->frp, FR_NEWFILE)) {
574 F_CLR(sp->frp, FR_NEWFILE);
575 t = msg_cat(sp, "021|new file", &len);
580 if (F_ISSET(sp->frp, FR_NAMECHANGE)) {
581 t = msg_cat(sp, "022|name changed", &len);
590 if (F_ISSET(sp->ep, F_MODIFIED))
591 t = msg_cat(sp, "023|modified", &len);
593 t = msg_cat(sp, "024|unmodified", &len);
598 if (F_ISSET(sp->frp, FR_UNLOCKED)) {
603 t = msg_cat(sp, "025|UNLOCKED", &len);
608 if (O_ISSET(sp, O_READONLY)) {
613 t = msg_cat(sp, "026|readonly", &len);
622 if (LF_ISSET(MSTAT_SHOWLAST)) {
623 if (db_last(sp, &last))
626 t = msg_cat(sp, "028|empty file", &len);
630 t = msg_cat(sp, "027|line %lu of %lu [%ld%%]", &len);
631 (void)snprintf(p, ep - p, t, (u_long)lno, (u_long)last,
632 ((u_long)lno * 100) / last);
636 t = msg_cat(sp, "029|line %lu", &len);
637 (void)snprintf(p, ep - p, t, (u_long)lno);
641 (void)snprintf(p, ep - p, " (pid %lu)", (u_long)getpid());
648 * There's a nasty problem with long path names. Cscope and tags files
649 * can result in long paths and vi will request a continuation key from
650 * the user as soon as it starts the screen. Unfortunately, the user
651 * has already typed ahead, and chaos results. If we assume that the
652 * characters in the filenames and informational messages only take a
653 * single screen column each, we can trim the filename.
656 * Status lines get put up at fairly awkward times. For example, when
657 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
658 * split screen, we have to repaint the status lines for all the screens
659 * below the top screen. We don't want users having to enter continue
660 * characters for those screens. Make it really hard to screw this up.
663 if (LF_ISSET(MSTAT_TRUNCATE) && len > sp->cols) {
664 for (; s < np && (*s != '/' || (p - s) > sp->cols - 3); ++s);
666 s = p - (sp->cols - 5);
675 /* Flush any waiting ex messages. */
678 sp->gp->scr_msg(sp, M_INFO, s, len);
680 FREE_SPACE(sp, bp, blen);
687 * Open the message catalogs.
689 * PUBLIC: int msg_open(SCR *, char *);
692 msg_open(SCR *sp, char *file)
696 * Assume that the first file opened is the system default, and that
697 * all subsequent ones user defined. Only display error messages
698 * if we can't open the user defined ones -- it's useful to know if
699 * the system one wasn't there, but if nvi is being shipped with an
700 * installed system, the file will be there, if it's not, then the
701 * message will be repeated every time nvi is started up.
703 static int first = 1;
708 if ((p = strrchr(file, '/')) != NULL && p[1] == '\0') {
709 /* Confirms to XPG4. */
710 if ((p = join(file, setlocale(LC_MESSAGES, NULL))) == NULL) {
711 msgq(sp, M_SYSERR, NULL);
715 /* Make sure it's recognized as a path by catopen(3). */
716 if ((p = join(".", file)) == NULL) {
717 msgq(sp, M_SYSERR, NULL);
722 if ((catd = catopen(p, NL_CAT_LOCALE)) == (nl_catd)-1) {
730 * POSIX.1-2008 gives no instruction on how to report a
731 * corrupt catalog file. Errno == 0 is not rare; add
732 * EFTYPE, which is seen on FreeBSD, for a good measure.
735 if (errno == 0 || errno == EFTYPE)
739 msgq_str(sp, M_ERR, p,
740 "030|The file %s is not a message catalog");
742 msgq_str(sp, M_SYSERR, p, "%s");
756 * Close the message catalogs.
758 * PUBLIC: void msg_close(GS *);
763 if (gp->catd != (nl_catd)-1)
764 (void)catclose(gp->catd);
769 * Return common continuation messages.
771 * PUBLIC: const char *msg_cmsg(SCR *, cmsg_t, size_t *);
774 msg_cmsg(SCR *sp, cmsg_t which, size_t *lenp)
778 return (msg_cat(sp, "268|confirm? [ynq]", lenp));
780 return (msg_cat(sp, "269|Press any key to continue: ", lenp));
783 "270|Press any key to continue [: to enter more ex commands]: ",
786 return (msg_cat(sp, "161|Press Enter to continue: ", lenp));
788 return (msg_cat(sp, "275| cont?", lenp));
791 "271|Press any key to continue [q to quit]: ", lenp));
800 * Return a single message from the catalog, plus its length.
803 * Only a single catalog message can be accessed at a time, if multiple
804 * ones are needed, they must be copied into local memory.
806 * PUBLIC: const char *msg_cat(SCR *, const char *, size_t *);
809 msg_cat(SCR *sp, const char *str, size_t *lenp)
816 * If it's not a catalog message, i.e. has doesn't have a leading
817 * number and '|' symbol, we're done.
819 if (isdigit((u_char)str[0]) &&
820 isdigit((u_char)str[1]) &&
821 isdigit((u_char)str[2]) && str[3] == '|') {
825 gp = sp == NULL ? NULL : sp->gp;
826 if (gp != NULL && gp->catd != (nl_catd)-1 &&
827 (p = catgets(gp->catd, 1, msgno, str)) != NULL) {
840 * Return a printable version of a string, in allocated memory.
842 * PUBLIC: char *msg_print(SCR *, const char *, int *);
845 msg_print(SCR *sp, const char *s, int *needfree)
848 char *bp, *ep, *p, *t;
854 /* XXX Not good for debugging ex_read & ex_filter.*/
855 CHAR2INT5(sp, EXP(sp)->ibcw, (char *)s, strlen(s) + 1, wp, wlen);
856 for (cp = wp; *cp != '\0'; ++cp)
860 return ((char *)s); /* SAFE: needfree set to 0. */
864 retry: if (sp == NULL)
867 FREE_SPACE(sp, bp, blen);
872 if ((bp = malloc(nlen)) == NULL)
875 GET_SPACE_GOTOC(sp, bp, blen, nlen);
877 alloc_err: return ("");
881 for (p = bp, ep = (bp + blen) - 1; *wp != '\0' && p < ep; ++wp)
882 for (t = KEY_NAME(sp, *wp); *t != '\0' && p < ep; *p++ = *t++);