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.
13 static const char sccsid
[] = "$Id: msg.c,v 10.50 2000/04/21 19:00:33 skimo Exp $ (Berkeley) $Date: 2000/04/21 19:00:33 $";
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
22 #include <bitstring.h>
45 * PUBLIC: void msgq __P((SCR *, mtype_t, const char *, ...));
49 msgq(SCR
*sp
, mtype_t mt
, const char *fmt
, ...)
51 msgq(sp
, mt
, fmt
, va_alist
)
59 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
61 const char *str
; /* String pointer. */
62 size_t arg
; /* Argument number. */
63 size_t prefix
; /* Prefix string length. */
64 size_t skip
; /* Skipped string length. */
65 size_t suffix
; /* Suffix string length. */
68 static int reenter
; /* STATIC: Re-entrancy check. */
71 size_t blen
, cnt1
, cnt2
, len
, mlen
, nlen
, soff
;
72 const char *p
, *t
, *u
;
73 char *bp
, *mp
, *rbp
, *s_rbp
;
78 * It's possible to enter msg when there's no screen to hold the
79 * message. If sp is NULL, ignore the special cases and put the
80 * message out to stderr.
86 else if (mt
== M_VINFO
)
92 if (F_ISSET(sp
, SC_VI
) && !O_ISSET(sp
, O_VERBOSE
)) {
93 F_SET(gp
, G_BELLSCHED
);
99 if (!O_ISSET(sp
, O_VERBOSE
))
104 if (F_ISSET(sp
, SC_EX_SILENT
))
116 * It's possible to reenter msg when it allocates space. We're
117 * probably dead anyway, but there's no reason to drop core.
120 * Yes, there's a race, but it should only be two instructions.
125 /* Get space for the message. */
128 retry
: FREE_SPACE(sp
, bp
, blen
);
133 GET_SPACE_GOTO(sp
, bp
, blen
, nlen
);
138 * mp: pointer to the current next character to be written
139 * mlen: length of the already written characters
140 * blen: total length of the buffer
142 #define REM (blen - mlen)
145 if (mt
== M_SYSERR
) {
146 p
= msg_cat(sp
, "020|Error: ", &len
);
155 * If we're running an ex command that the user didn't enter, display
156 * the file name and line number prefix.
158 if ((mt
== M_ERR
|| mt
== M_SYSERR
) &&
159 sp
!= NULL
&& gp
!= NULL
&& gp
->if_name
!= NULL
) {
160 for (p
= gp
->if_name
; *p
!= '\0'; ++p
) {
161 len
= snprintf(mp
, REM
, "%s", KEY_NAME(sp
, *p
));
163 if ((mlen
+= len
) > blen
)
166 len
= snprintf(mp
, REM
, ", %d: ", gp
->if_lno
);
168 if ((mlen
+= len
) > blen
)
172 /* If nothing to format, we're done. */
175 fmt
= msg_cat(sp
, fmt
, NULL
);
179 * Nvi should run on machines that don't support the numbered argument
180 * specifications (%[digit]*$). We do this by reformatting the string
181 * so that we can hand it to vsprintf(3) and it will use the arguments
182 * in the right order. When vsprintf returns, we put the string back
183 * into the right order. It's undefined, according to SVID III, to mix
184 * numbered argument specifications with the standard style arguments,
185 * so this should be safe.
187 * In addition, we also need a character that is known to not occur in
188 * any vi message, for separating the parts of the string. As callers
189 * of msgq are responsible for making sure that all the non-printable
190 * characters are formatted for printing before calling msgq, we use a
191 * random non-printable character selected at terminal initialization
192 * time. This code isn't fast by any means, but as messages should be
193 * relatively short and normally have only a few arguments, it won't be
194 * too bad. Regardless, nobody has come up with any other solution.
196 * The result of this loop is an array of pointers into the message
197 * string, with associated lengths and argument numbers. The array
198 * is in the "correct" order, and the arg field contains the argument
201 for (p
= fmt
, soff
= 0; soff
< __NL_ARGMAX
;) {
202 for (t
= p
; *p
!= '\0' && *p
!= '%'; ++p
);
211 for (u
= p
; *++p
!= '\0' && isdigit(*p
););
215 /* Up to, and including the % character. */
217 str
[soff
].prefix
= u
- t
;
219 /* Up to, and including the $ character. */
220 str
[soff
].arg
= atoi(u
);
221 str
[soff
].skip
= (p
- u
) + 1;
222 if (str
[soff
].arg
>= __NL_ARGMAX
)
225 /* Up to, and including the conversion character. */
226 for (u
= p
; (ch
= *++p
) != '\0';)
228 strchr("diouxXfeEgGcspn", ch
) != NULL
)
230 str
[soff
].suffix
= p
- u
;
236 /* If no magic strings, we're done. */
240 /* Get space for the reordered strings. */
241 if ((rbp
= malloc(nlen
)) == NULL
)
246 * Reorder the strings into the message string based on argument
250 * We ignore arguments that are out of order, i.e. if we don't find
251 * an argument, we continue. Assume (almost certainly incorrectly)
252 * that whoever created the string knew what they were doing.
255 * Brute force "sort", but since we don't expect more than one or two
256 * arguments in a string, the setup cost of a fast sort will be more
257 * expensive than the loop.
259 for (cnt1
= 1; cnt1
<= soff
; ++cnt1
)
260 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
261 if (cnt1
== str
[cnt2
].arg
) {
262 memmove(s_rbp
, str
[cnt2
].str
, str
[cnt2
].prefix
);
263 memmove(s_rbp
+ str
[cnt2
].prefix
,
264 str
[cnt2
].str
+ str
[cnt2
].prefix
+
265 str
[cnt2
].skip
, str
[cnt2
].suffix
);
266 s_rbp
+= str
[cnt2
].prefix
+ str
[cnt2
].suffix
;
268 gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
;
275 format
: /* Format the arguments into the string. */
281 len
= vsnprintf(mp
, REM
, fmt
, ap
);
291 * Go through the resulting string, and, for each separator character
292 * separated string, enter its new starting position and length in the
295 for (p
= t
= mp
, cnt1
= 1,
296 ch
= gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
; *p
!= '\0'; ++p
)
298 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
299 if (str
[cnt2
].arg
== cnt1
)
302 str
[cnt2
].prefix
= p
- t
;
308 * Reorder the strings once again, putting them back into the
312 * Note, the length of the message gets decremented once for
313 * each substring, when we discard the separator character.
315 for (s_rbp
= rbp
, cnt1
= 0; cnt1
< soff
; ++cnt1
) {
316 memmove(rbp
, str
[cnt1
].str
, str
[cnt1
].prefix
);
317 rbp
+= str
[cnt1
].prefix
;
320 memmove(mp
, s_rbp
, rbp
- s_rbp
);
322 /* Free the reordered string memory. */
327 if ((mlen
+= len
) > blen
)
329 if (mt
== M_SYSERR
) {
330 len
= snprintf(mp
, REM
, ": %s", strerror(errno
));
332 if ((mlen
+= len
) > blen
)
337 /* Add trailing newline. */
338 if ((mlen
+= 1) > blen
)
345 gp
->scr_msg(sp
, mt
, bp
, mlen
);
347 (void)fprintf(stderr
, "%.*s", (int)mlen
, bp
);
350 ret
: FREE_SPACE(sp
, bp
, blen
);
357 * Display a message with an embedded string.
359 * PUBLIC: void msgq_str __P((SCR *, mtype_t, char *, char *));
362 msgq_str(sp
, mtype
, str
, fmt
)
371 msgq(sp
, mtype
, fmt
);
376 p
= msg_print(sp
, str
, &nf
);
378 msgq(sp
, mtype
, fmt
, p
);
380 FREE_SPACE(sp
, p
, 0);
385 * Report on the lines that changed.
388 * Historic vi documentation (USD:15-8) claimed that "The editor will also
389 * always tell you when a change you make affects text which you cannot see."
390 * This wasn't true -- edit a large file and do "100d|1". We don't implement
391 * this semantic since it requires tracking each line that changes during a
392 * command instead of just keeping count.
394 * Line counts weren't right in historic vi, either. For example, given the
398 * the command 2d}, from the 'b' would report that two lines were deleted,
401 * PUBLIC: void mod_rpt __P((SCR *));
407 static char * const action
[] = {
416 static char * const lines
[] = {
423 size_t blen
, len
, tlen
;
428 /* Change reports are turned off in batch mode. */
429 if (F_ISSET(sp
, SC_EX_SILENT
))
432 /* Reset changing line number. */
433 sp
->rptlchange
= OOBLNO
;
436 * Don't build a message if not enough changed.
439 * And now, a vi clone test. Historically, vi reported if the number
440 * of changed lines was > than the value, not >=, unless it was a yank
441 * command, which used >=. No lie. Furthermore, an action was never
442 * reported for a single line action. This is consistent for actions
443 * other than yank, but yank didn't report single line actions even if
444 * the report edit option was set to 1. In addition, setting report to
445 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
446 * unknown reason (this bug was fixed in System III/V at some point).
447 * I got complaints, so nvi conforms to System III/V historic practice
448 * except that we report a yank of 1 line if report is set to 1.
450 #define ARSIZE(a) sizeof(a) / sizeof (*a)
452 rptval
= O_VAL(sp
, O_REPORT
);
453 for (cnt
= 0, total
= 0; cnt
< ARSIZE(action
); ++cnt
)
454 total
+= sp
->rptlines
[cnt
];
457 if (total
<= rptval
&& sp
->rptlines
[L_YANKED
] < rptval
) {
458 for (cnt
= 0; cnt
< ARSIZE(action
); ++cnt
)
459 sp
->rptlines
[cnt
] = 0;
463 /* Build and display the message. */
464 GET_SPACE_GOTO(sp
, bp
, blen
, sizeof(action
) * MAXNUM
+ 1);
465 for (p
= bp
, first
= 1, tlen
= 0,
466 ap
= action
, cnt
= 0; cnt
< ARSIZE(action
); ++ap
, ++cnt
)
467 if (sp
->rptlines
[cnt
] != 0) {
475 len
= snprintf(p
, MAXNUM
, "%lu ", sp
->rptlines
[cnt
]);
479 lines
[sp
->rptlines
[cnt
] == 1 ? 0 : 1], &len
);
485 t
= msg_cat(sp
, *ap
, &len
);
489 sp
->rptlines
[cnt
] = 0;
492 /* Add trailing newline. */
497 sp
->gp
->scr_msg(sp
, M_INFO
, bp
, tlen
);
499 FREE_SPACE(sp
, bp
, blen
);
509 * Report on the file's status.
511 * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
514 msgq_status(sp
, lno
, flags
)
523 char **ap
, *bp
, *np
, *p
, *s
;
525 /* Get sufficient memory. */
526 len
= strlen(sp
->frp
->name
);
527 GET_SPACE_GOTO(sp
, bp
, blen
, len
* MAX_CHARACTER_COLUMNS
+ 128);
530 /* Copy in the filename. */
531 for (p
= bp
, t
= sp
->frp
->name
; *t
!= '\0'; ++t
) {
532 len
= KEY_LEN(sp
, *t
);
533 memcpy(p
, KEY_NAME(sp
, *t
), len
);
540 /* Copy in the argument count. */
541 if (F_ISSET(sp
, SC_STATUS_CNT
) && sp
->argv
!= NULL
) {
542 for (cnt
= 0, ap
= sp
->argv
; *ap
!= NULL
; ++ap
, ++cnt
);
545 msg_cat(sp
, "317|%d files to edit", NULL
), cnt
);
550 F_CLR(sp
, SC_STATUS_CNT
);
554 * See nvi/exf.c:file_init() for a description of how and when the
555 * read-only bit is set.
558 * The historic display for "name changed" was "[Not edited]".
561 if (F_ISSET(sp
->frp
, FR_NEWFILE
)) {
562 F_CLR(sp
->frp
, FR_NEWFILE
);
563 t
= msg_cat(sp
, "021|new file", &len
);
568 if (F_ISSET(sp
->frp
, FR_NAMECHANGE
)) {
569 t
= msg_cat(sp
, "022|name changed", &len
);
578 if (F_ISSET(sp
->ep
, F_MODIFIED
))
579 t
= msg_cat(sp
, "023|modified", &len
);
581 t
= msg_cat(sp
, "024|unmodified", &len
);
586 if (F_ISSET(sp
->frp
, FR_UNLOCKED
)) {
591 t
= msg_cat(sp
, "025|UNLOCKED", &len
);
596 if (O_ISSET(sp
, O_READONLY
)) {
601 t
= msg_cat(sp
, "026|readonly", &len
);
610 if (LF_ISSET(MSTAT_SHOWLAST
)) {
611 if (db_last(sp
, &last
))
614 t
= msg_cat(sp
, "028|empty file", &len
);
618 t
= msg_cat(sp
, "027|line %lu of %lu [%ld%%]", &len
);
619 (void)sprintf(p
, t
, lno
, last
, (lno
* 100) / last
);
623 t
= msg_cat(sp
, "029|line %lu", &len
);
624 (void)sprintf(p
, t
, lno
);
628 (void)sprintf(p
, " (pid %lu)", (u_long
)getpid());
635 * There's a nasty problem with long path names. Cscope and tags files
636 * can result in long paths and vi will request a continuation key from
637 * the user as soon as it starts the screen. Unfortunately, the user
638 * has already typed ahead, and chaos results. If we assume that the
639 * characters in the filenames and informational messages only take a
640 * single screen column each, we can trim the filename.
643 * Status lines get put up at fairly awkward times. For example, when
644 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
645 * split screen, we have to repaint the status lines for all the screens
646 * below the top screen. We don't want users having to enter continue
647 * characters for those screens. Make it really hard to screw this up.
650 if (LF_ISSET(MSTAT_TRUNCATE
) && len
> sp
->cols
) {
651 for (; s
< np
&& (*s
!= '/' || (p
- s
) > sp
->cols
- 3); ++s
);
653 s
= p
- (sp
->cols
- 5);
662 /* Flush any waiting ex messages. */
665 sp
->gp
->scr_msg(sp
, M_INFO
, s
, len
);
667 FREE_SPACE(sp
, bp
, blen
);
674 * Open the message catalogs.
676 * PUBLIC: int msg_open __P((SCR *, char *));
685 * Assume that the first file opened is the system default, and that
686 * all subsequent ones user defined. Only display error messages
687 * if we can't open the user defined ones -- it's useful to know if
688 * the system one wasn't there, but if nvi is being shipped with an
689 * installed system, the file will be there, if it's not, then the
690 * message will be repeated every time nvi is started up.
692 static int first
= 1;
696 char *p
, *t
, buf
[MAXPATHLEN
];
698 if ((p
= strrchr(file
, '/')) != NULL
&& p
[1] == '\0' &&
699 ((t
= getenv("LC_MESSAGES")) != NULL
&& t
[0] != '\0' ||
700 (t
= getenv("LANG")) != NULL
&& t
[0] != '\0')) {
701 (void)snprintf(buf
, sizeof(buf
), "%s%s", file
, t
);
706 O_NONBLOCK
| O_RDONLY
, 0, DB_RECNO
, NULL
)) == NULL
) {
711 msgq_str(sp
, M_SYSERR
, p
, "%s");
716 * Test record 1 for the magic string. The msgq call is here so
717 * the message catalog build finds it.
719 #define VMC "VI_MESSAGE_CATALOG"
721 key
.size
= sizeof(db_recno_t
);
723 if (db
->get(db
, &key
, &data
, 0) != 0 ||
724 data
.size
!= sizeof(VMC
) - 1 ||
725 memcmp(data
.data
, VMC
, sizeof(VMC
) - 1)) {
731 msgq_str(sp
, M_ERR
, p
,
732 "030|The file %s is not a message catalog");
737 if (sp
->gp
->msg
!= NULL
)
738 (void)sp
->gp
->msg
->close(sp
->gp
->msg
);
745 * Close the message catalogs.
747 * PUBLIC: void msg_close __P((GS *));
754 (void)gp
->msg
->close(gp
->msg
);
759 * Return common continuation messages.
761 * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
764 msg_cmsg(sp
, which
, lenp
)
771 return (msg_cat(sp
, "268|confirm? [ynq]", lenp
));
773 return (msg_cat(sp
, "269|Press any key to continue: ", lenp
));
776 "270|Press any key to continue [: to enter more ex commands]: ",
779 return (msg_cat(sp
, "161|Press Enter to continue: ", lenp
));
781 return (msg_cat(sp
, "275| cont?", lenp
));
784 "271|Press any key to continue [q to quit]: ", lenp
));
793 * Return a single message from the catalog, plus its length.
796 * Only a single catalog message can be accessed at a time, if multiple
797 * ones are needed, they must be copied into local memory.
799 * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
802 msg_cat(sp
, str
, lenp
)
812 * If it's not a catalog message, i.e. has doesn't have a leading
813 * number and '|' symbol, we're done.
815 if (isdigit(str
[0]) &&
816 isdigit(str
[1]) && isdigit(str
[2]) && str
[3] == '|') {
818 key
.size
= sizeof(db_recno_t
);
823 * Really sleazy hack -- we put an extra character on the
824 * end of the format string, and then we change it to be
825 * the nul termination of the string. There ought to be
826 * a better way. Once we can allocate multiple temporary
827 * memory buffers, maybe we can use one of them instead.
829 gp
= sp
== NULL
? NULL
: sp
->gp
;
830 if (gp
!= NULL
&& gp
->msg
!= NULL
&&
831 gp
->msg
->get(gp
->msg
, &key
, &data
, 0) == 0 &&
834 *lenp
= data
.size
- 1;
835 ((char *)data
.data
)[data
.size
- 1] = '\0';
847 * Return a printable version of a string, in allocated memory.
849 * PUBLIC: char *msg_print __P((SCR *, const char *, int *));
852 msg_print(sp
, s
, needfree
)
859 char *bp
, *ep
, *p
, *t
;
863 for (cp
= s
; *cp
!= '\0'; ++cp
)
867 return ((char *)s
); /* SAFE: needfree set to 0. */
871 retry
: if (sp
== NULL
)
874 FREE_SPACE(sp
, bp
, blen
);
879 if ((bp
= malloc(nlen
)) == NULL
)
882 GET_SPACE_GOTO(sp
, bp
, blen
, nlen
);
884 alloc_err
: return ("");
888 for (p
= bp
, ep
= (bp
+ blen
) - 1, cp
= s
; *cp
!= '\0' && p
< ep
; ++cp
)
889 for (t
= KEY_NAME(sp
, *cp
); *t
!= '\0' && p
< ep
; *p
++ = *t
++);