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.60 2002/03/27 10:06:56 skimo Exp $ (Berkeley) $Date: 2002/03/27 10:06:56 $";
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. */
72 size_t blen
, cnt1
, cnt2
, len
, mlen
, nlen
, soff
;
73 const char *p
, *t
, *u
;
74 char *bp
, *mp
, *rbp
, *s_rbp
;
79 * It's possible to enter msg when there's no screen to hold the
80 * message. If sp is NULL, ignore the special cases and put the
81 * message out to stderr.
87 else if (mt
== M_VINFO
)
94 if (F_ISSET(sp
, SC_VI
) && !O_ISSET(sp
, O_VERBOSE
)) {
95 F_SET(gp
, G_BELLSCHED
);
101 if (!O_ISSET(sp
, O_VERBOSE
))
106 if (F_ISSET(sp
, SC_EX_SILENT
))
119 * It's possible to reenter msg when it allocates space. We're
120 * probably dead anyway, but there's no reason to drop core.
123 * Yes, there's a race, but it should only be two instructions.
128 /* Get space for the message. */
131 retry
: FREE_SPACE(sp
, bp
, blen
);
136 GET_SPACE_GOTO(sp
, bp
, blen
, nlen
);
141 * mp: pointer to the current next character to be written
142 * mlen: length of the already written characters
143 * blen: total length of the buffer
145 #define REM (blen - mlen)
148 if (mt
== M_SYSERR
|| mt
== M_DBERR
) {
149 p
= msg_cat(sp
, "020|Error: ", &len
);
158 * If we're running an ex command that the user didn't enter, display
159 * the file name and line number prefix.
161 if ((mt
== M_ERR
|| mt
== M_SYSERR
) &&
162 sp
!= NULL
&& wp
!= NULL
&& wp
->if_name
!= NULL
) {
163 for (p
= wp
->if_name
; *p
!= '\0'; ++p
) {
164 len
= snprintf(mp
, REM
, "%s", KEY_NAME(sp
, *p
));
166 if ((mlen
+= len
) > blen
)
169 len
= snprintf(mp
, REM
, ", %d: ", wp
->if_lno
);
171 if ((mlen
+= len
) > blen
)
175 /* If nothing to format, we're done. */
178 fmt
= msg_cat(sp
, fmt
, NULL
);
182 * Nvi should run on machines that don't support the numbered argument
183 * specifications (%[digit]*$). We do this by reformatting the string
184 * so that we can hand it to vsprintf(3) and it will use the arguments
185 * in the right order. When vsprintf returns, we put the string back
186 * into the right order. It's undefined, according to SVID III, to mix
187 * numbered argument specifications with the standard style arguments,
188 * so this should be safe.
190 * In addition, we also need a character that is known to not occur in
191 * any vi message, for separating the parts of the string. As callers
192 * of msgq are responsible for making sure that all the non-printable
193 * characters are formatted for printing before calling msgq, we use a
194 * random non-printable character selected at terminal initialization
195 * time. This code isn't fast by any means, but as messages should be
196 * relatively short and normally have only a few arguments, it won't be
197 * too bad. Regardless, nobody has come up with any other solution.
199 * The result of this loop is an array of pointers into the message
200 * string, with associated lengths and argument numbers. The array
201 * is in the "correct" order, and the arg field contains the argument
204 for (p
= fmt
, soff
= 0; soff
< __NL_ARGMAX
;) {
205 for (t
= p
; *p
!= '\0' && *p
!= '%'; ++p
);
214 for (u
= p
; *++p
!= '\0' && isdigit(*p
););
218 /* Up to, and including the % character. */
220 str
[soff
].prefix
= u
- t
;
222 /* Up to, and including the $ character. */
223 str
[soff
].arg
= atoi(u
);
224 str
[soff
].skip
= (p
- u
) + 1;
225 if (str
[soff
].arg
>= __NL_ARGMAX
)
228 /* Up to, and including the conversion character. */
229 for (u
= p
; (ch
= *++p
) != '\0';)
231 strchr("diouxXfeEgGcspn", ch
) != NULL
)
233 str
[soff
].suffix
= p
- u
;
239 /* If no magic strings, we're done. */
243 /* Get space for the reordered strings. */
244 if ((rbp
= malloc(nlen
)) == NULL
)
249 * Reorder the strings into the message string based on argument
253 * We ignore arguments that are out of order, i.e. if we don't find
254 * an argument, we continue. Assume (almost certainly incorrectly)
255 * that whoever created the string knew what they were doing.
258 * Brute force "sort", but since we don't expect more than one or two
259 * arguments in a string, the setup cost of a fast sort will be more
260 * expensive than the loop.
262 for (cnt1
= 1; cnt1
<= soff
; ++cnt1
)
263 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
264 if (cnt1
== str
[cnt2
].arg
) {
265 memmove(s_rbp
, str
[cnt2
].str
, str
[cnt2
].prefix
);
266 memmove(s_rbp
+ str
[cnt2
].prefix
,
267 str
[cnt2
].str
+ str
[cnt2
].prefix
+
268 str
[cnt2
].skip
, str
[cnt2
].suffix
);
269 s_rbp
+= str
[cnt2
].prefix
+ str
[cnt2
].suffix
;
271 gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
;
278 format
: /* Format the arguments into the string. */
284 len
= vsnprintf(mp
, REM
, fmt
, ap
);
294 * Go through the resulting string, and, for each separator character
295 * separated string, enter its new starting position and length in the
298 for (p
= t
= mp
, cnt1
= 1,
299 ch
= gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
; *p
!= '\0'; ++p
)
301 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
302 if (str
[cnt2
].arg
== cnt1
)
305 str
[cnt2
].prefix
= p
- t
;
311 * Reorder the strings once again, putting them back into the
315 * Note, the length of the message gets decremented once for
316 * each substring, when we discard the separator character.
318 for (s_rbp
= rbp
, cnt1
= 0; cnt1
< soff
; ++cnt1
) {
319 memmove(rbp
, str
[cnt1
].str
, str
[cnt1
].prefix
);
320 rbp
+= str
[cnt1
].prefix
;
323 memmove(mp
, s_rbp
, rbp
- s_rbp
);
325 /* Free the reordered string memory. */
330 if ((mlen
+= len
) > blen
)
332 if (mt
== M_SYSERR
) {
333 len
= snprintf(mp
, REM
, ": %s", strerror(errno
));
335 if ((mlen
+= len
) > blen
)
340 len
= snprintf(mp
, REM
, ": %s", db_strerror(sp
->db_error
));
342 if ((mlen
+= len
) > blen
)
347 /* Add trailing newline. */
348 if ((mlen
+= 1) > blen
)
355 wp
->scr_msg(sp
, mt
, bp
, mlen
);
357 (void)fprintf(stderr
, "%.*s", (int)mlen
, bp
);
360 ret
: FREE_SPACE(sp
, bp
, blen
);
367 * Display a message with an embedded string.
369 * PUBLIC: void msgq_wstr __P((SCR *, mtype_t, CHAR_T *, char *));
372 msgq_wstr(SCR
*sp
, mtype_t mtype
, CHAR_T
*str
, char *fmt
)
378 msgq(sp
, mtype
, fmt
);
381 INT2CHAR(sp
, str
, STRLEN(str
) + 1, nstr
, nlen
);
382 msgq_str(sp
, mtype
, nstr
, fmt
);
387 * Display a message with an embedded string.
389 * PUBLIC: void msgq_str __P((SCR *, mtype_t, char *, char *));
392 msgq_str(SCR
*sp
, mtype_t mtype
, char *str
, char *fmt
)
398 msgq(sp
, mtype
, fmt
);
403 p
= msg_print(sp
, str
, &nf
);
405 msgq(sp
, mtype
, fmt
, p
);
407 FREE_SPACE(sp
, p
, 0);
412 * Report on the lines that changed.
415 * Historic vi documentation (USD:15-8) claimed that "The editor will also
416 * always tell you when a change you make affects text which you cannot see."
417 * This wasn't true -- edit a large file and do "100d|1". We don't implement
418 * this semantic since it requires tracking each line that changes during a
419 * command instead of just keeping count.
421 * Line counts weren't right in historic vi, either. For example, given the
425 * the command 2d}, from the 'b' would report that two lines were deleted,
428 * PUBLIC: void mod_rpt __P((SCR *));
433 static char * const action
[] = {
442 static char * const lines
[] = {
449 size_t blen
, len
, tlen
;
454 /* Change reports are turned off in batch mode. */
455 if (F_ISSET(sp
, SC_EX_SILENT
))
458 /* Reset changing line number. */
459 sp
->rptlchange
= OOBLNO
;
462 * Don't build a message if not enough changed.
465 * And now, a vi clone test. Historically, vi reported if the number
466 * of changed lines was > than the value, not >=, unless it was a yank
467 * command, which used >=. No lie. Furthermore, an action was never
468 * reported for a single line action. This is consistent for actions
469 * other than yank, but yank didn't report single line actions even if
470 * the report edit option was set to 1. In addition, setting report to
471 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
472 * unknown reason (this bug was fixed in System III/V at some point).
473 * I got complaints, so nvi conforms to System III/V historic practice
474 * except that we report a yank of 1 line if report is set to 1.
476 #define ARSIZE(a) sizeof(a) / sizeof (*a)
478 rptval
= O_VAL(sp
, O_REPORT
);
479 for (cnt
= 0, total
= 0; cnt
< ARSIZE(action
); ++cnt
)
480 total
+= sp
->rptlines
[cnt
];
483 if (total
<= rptval
&& sp
->rptlines
[L_YANKED
] < rptval
) {
484 for (cnt
= 0; cnt
< ARSIZE(action
); ++cnt
)
485 sp
->rptlines
[cnt
] = 0;
489 /* Build and display the message. */
490 GET_SPACE_GOTO(sp
, bp
, blen
, sizeof(action
) * MAXNUM
+ 1);
491 for (p
= bp
, first
= 1, tlen
= 0,
492 ap
= action
, cnt
= 0; cnt
< ARSIZE(action
); ++ap
, ++cnt
)
493 if (sp
->rptlines
[cnt
] != 0) {
501 len
= snprintf(p
, MAXNUM
, "%lu ",
502 (unsigned long) sp
->rptlines
[cnt
]);
506 lines
[sp
->rptlines
[cnt
] == 1 ? 0 : 1], &len
);
512 t
= msg_cat(sp
, *ap
, &len
);
516 sp
->rptlines
[cnt
] = 0;
519 /* Add trailing newline. */
524 sp
->wp
->scr_msg(sp
, M_INFO
, bp
, tlen
);
526 FREE_SPACE(sp
, bp
, blen
);
536 * Report on the file's status.
538 * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
541 msgq_status(SCR
*sp
, db_recno_t lno
, u_int flags
)
547 char **ap
, *bp
, *np
, *p
, *s
;
549 /* Get sufficient memory. */
550 len
= strlen(sp
->frp
->name
);
551 GET_SPACE_GOTO(sp
, bp
, blen
, len
* MAX_CHARACTER_COLUMNS
+ 128);
554 /* Copy in the filename. */
555 for (p
= bp
, t
= sp
->frp
->name
; *t
!= '\0'; ++t
) {
556 len
= KEY_LEN(sp
, *t
);
557 memcpy(p
, KEY_NAME(sp
, *t
), len
);
564 /* Copy in the argument count. */
565 if (F_ISSET(sp
, SC_STATUS_CNT
) && sp
->argv
!= NULL
) {
566 for (cnt
= 0, ap
= sp
->argv
; *ap
!= NULL
; ++ap
, ++cnt
);
569 msg_cat(sp
, "317|%d files to edit", NULL
), cnt
);
574 F_CLR(sp
, SC_STATUS_CNT
);
578 * See nvi/exf.c:file_init() for a description of how and when the
579 * read-only bit is set.
582 * The historic display for "name changed" was "[Not edited]".
585 if (F_ISSET(sp
->frp
, FR_NEWFILE
)) {
586 F_CLR(sp
->frp
, FR_NEWFILE
);
587 t
= msg_cat(sp
, "021|new file", &len
);
592 if (F_ISSET(sp
->frp
, FR_NAMECHANGE
)) {
593 t
= msg_cat(sp
, "022|name changed", &len
);
602 if (F_ISSET(sp
->ep
, F_MODIFIED
))
603 t
= msg_cat(sp
, "023|modified", &len
);
605 t
= msg_cat(sp
, "024|unmodified", &len
);
610 if (F_ISSET(sp
->frp
, FR_UNLOCKED
)) {
615 t
= msg_cat(sp
, "025|UNLOCKED", &len
);
620 if (O_ISSET(sp
, O_READONLY
)) {
625 t
= msg_cat(sp
, "026|readonly", &len
);
634 if (LF_ISSET(MSTAT_SHOWLAST
)) {
635 if (db_last(sp
, &last
))
638 t
= msg_cat(sp
, "028|empty file", &len
);
642 t
= msg_cat(sp
, "027|line %lu of %lu [%ld%%]", &len
);
643 (void)sprintf(p
, t
, lno
, last
, (lno
* 100) / last
);
647 t
= msg_cat(sp
, "029|line %lu", &len
);
648 (void)sprintf(p
, t
, lno
);
652 (void)sprintf(p
, " (pid %lu)", (u_long
)getpid());
659 * There's a nasty problem with long path names. Cscope and tags files
660 * can result in long paths and vi will request a continuation key from
661 * the user as soon as it starts the screen. Unfortunately, the user
662 * has already typed ahead, and chaos results. If we assume that the
663 * characters in the filenames and informational messages only take a
664 * single screen column each, we can trim the filename.
667 * Status lines get put up at fairly awkward times. For example, when
668 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
669 * split screen, we have to repaint the status lines for all the screens
670 * below the top screen. We don't want users having to enter continue
671 * characters for those screens. Make it really hard to screw this up.
674 if (LF_ISSET(MSTAT_TRUNCATE
) && len
> sp
->cols
) {
675 for (; s
< np
&& (*s
!= '/' || (p
- s
) > sp
->cols
- 3); ++s
);
677 s
= p
- (sp
->cols
- 5);
686 /* Flush any waiting ex messages. */
689 sp
->wp
->scr_msg(sp
, M_INFO
, s
, len
);
691 FREE_SPACE(sp
, bp
, blen
);
698 * Open the message catalogs.
700 * PUBLIC: int msg_open __P((SCR *, char *));
703 msg_open(SCR
*sp
, char *file
)
707 * Assume that the first file opened is the system default, and that
708 * all subsequent ones user defined. Only display error messages
709 * if we can't open the user defined ones -- it's useful to know if
710 * the system one wasn't there, but if nvi is being shipped with an
711 * installed system, the file will be there, if it's not, then the
712 * message will be repeated every time nvi is started up.
714 static int first
= 1;
718 char *p
, *t
, buf
[MAXPATHLEN
];
720 if ((p
= strrchr(file
, '/')) != NULL
&& p
[1] == '\0' &&
721 (((t
= getenv("LC_MESSAGES")) != NULL
&& t
[0] != '\0') ||
722 ((t
= getenv("LANG")) != NULL
&& t
[0] != '\0'))) {
723 (void)snprintf(buf
, sizeof(buf
), "%s%s", file
, t
);
727 if ((sp
->db_error
= db_create(&db
, 0, 0)) != 0 ||
728 (sp
->db_error
= db
->set_re_source(db
, p
)) != 0 ||
729 (sp
->db_error
= db
->open(db
, NULL
, NULL
, DB_RECNO
, 0, 0)) != 0) {
734 msgq_str(sp
, M_DBERR
, p
, "%s");
739 * Test record 1 for the magic string. The msgq call is here so
740 * the message catalog build finds it.
742 #define VMC "VI_MESSAGE_CATALOG"
743 memset(&key
, 0, sizeof(key
));
745 key
.size
= sizeof(db_recno_t
);
746 memset(&data
, 0, sizeof(data
));
748 if ((sp
->db_error
= db
->get(db
, NULL
, &key
, &data
, 0)) != 0 ||
749 data
.size
!= sizeof(VMC
) - 1 ||
750 memcmp(data
.data
, VMC
, sizeof(VMC
) - 1)) {
751 (void)db
->close(db
, DB_NOSYNC
);
756 msgq_str(sp
, M_DBERR
, p
,
757 "030|The file %s is not a message catalog");
762 if (sp
->gp
->msg
!= NULL
)
763 (void)sp
->gp
->msg
->close(sp
->gp
->msg
, DB_NOSYNC
);
770 * Close the message catalogs.
772 * PUBLIC: void msg_close __P((GS *));
777 if (gp
->msg
!= NULL
) {
778 (void)gp
->msg
->close(gp
->msg
, 0);
785 * Return common continuation messages.
787 * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
790 msg_cmsg(SCR
*sp
, cmsg_t which
, size_t *lenp
)
794 return (msg_cat(sp
, "268|confirm? [ynq]", lenp
));
796 return (msg_cat(sp
, "269|Press any key to continue: ", lenp
));
799 "270|Press any key to continue [: to enter more ex commands]: ",
802 return (msg_cat(sp
, "161|Press Enter to continue: ", lenp
));
804 return (msg_cat(sp
, "275| cont?", lenp
));
807 "271|Press any key to continue [q to quit]: ", lenp
));
816 * Return a single message from the catalog, plus its length.
819 * Only a single catalog message can be accessed at a time, if multiple
820 * ones are needed, they must be copied into local memory.
822 * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
825 msg_cat(SCR
*sp
, const char *str
, size_t *lenp
)
832 * If it's not a catalog message, i.e. has doesn't have a leading
833 * number and '|' symbol, we're done.
835 if (isdigit(str
[0]) &&
836 isdigit(str
[1]) && isdigit(str
[2]) && str
[3] == '|') {
837 memset(&key
, 0, sizeof(key
));
839 key
.size
= sizeof(db_recno_t
);
840 memset(&data
, 0, sizeof(data
));
845 * Really sleazy hack -- we put an extra character on the
846 * end of the format string, and then we change it to be
847 * the nul termination of the string. There ought to be
848 * a better way. Once we can allocate multiple temporary
849 * memory buffers, maybe we can use one of them instead.
851 gp
= sp
== NULL
? NULL
: sp
->gp
;
852 if (gp
!= NULL
&& gp
->msg
!= NULL
&&
853 gp
->msg
->get(gp
->msg
, NULL
, &key
, &data
, 0) == 0 &&
856 *lenp
= data
.size
- 1;
857 ((char *)data
.data
)[data
.size
- 1] = '\0';
869 * Return a printable version of a string, in allocated memory.
871 * PUBLIC: char *msg_print __P((SCR *, const char *, int *));
874 msg_print(SCR
*sp
, const char *s
, int *needfree
)
878 char *bp
, *ep
, *p
, *t
;
882 for (cp
= s
; *cp
!= '\0'; ++cp
)
886 return ((char *)s
); /* SAFE: needfree set to 0. */
890 retry
: if (sp
== NULL
)
893 FREE_SPACE(sp
, bp
, blen
);
898 if ((bp
= malloc(nlen
)) == NULL
)
901 GET_SPACE_GOTO(sp
, bp
, blen
, nlen
);
903 alloc_err
: return ("");
907 for (p
= bp
, ep
= (bp
+ blen
) - 1, cp
= s
; *cp
!= '\0' && p
< ep
; ++cp
)
908 for (t
= KEY_NAME(sp
, *cp
); *t
!= '\0' && p
< ep
; *p
++ = *t
++);