1 /* $NetBSD: msg.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
3 * Copyright (c) 1991, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1991, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: msg.c,v 10.61 2003/07/18 23:17:30 skimo Exp (Berkeley) Date: 2003/07/18 23:17:30 ";
17 #include <sys/param.h>
18 #include <sys/types.h> /* XXX: param.h may not have included types.h */
19 #include <sys/queue.h>
23 #include <bitstring.h>
40 #include "dbinternal.h"
47 * PUBLIC: void msgq __P((SCR *, mtype_t, const char *, ...));
51 msgq(SCR
*sp
, mtype_t mt
, const char *fmt
, ...)
53 msgq(sp
, mt
, fmt
, va_alist
)
61 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
63 const char *str
; /* String pointer. */
64 size_t arg
; /* Argument number. */
65 size_t prefix
; /* Prefix string length. */
66 size_t skip
; /* Skipped string length. */
67 size_t suffix
; /* Suffix string length. */
70 static int reenter
; /* STATIC: Re-entrancy check. */
73 size_t blen
, len
, mlen
, nlen
;
81 size_t cnt1
, cnt2
, soff
;
86 * It's possible to enter msg when there's no screen to hold the
87 * message. If sp is NULL, ignore the special cases and put the
88 * message out to stderr.
94 else if (mt
== M_VINFO
)
101 if (F_ISSET(sp
, SC_VI
) && !O_ISSET(sp
, O_VERBOSE
)) {
102 F_SET(gp
, G_BELLSCHED
);
108 if (!O_ISSET(sp
, O_VERBOSE
))
113 if (F_ISSET(sp
, SC_EX_SILENT
))
126 * It's possible to reenter msg when it allocates space. We're
127 * probably dead anyway, but there's no reason to drop core.
130 * Yes, there's a race, but it should only be two instructions.
135 /* Get space for the message. */
138 retry
: FREE_SPACE(sp
, bp
, blen
);
143 GET_SPACE_GOTOC(sp
, bp
, blen
, nlen
);
148 * mp: pointer to the current next character to be written
149 * mlen: length of the already written characters
150 * blen: total length of the buffer
152 #define REM (blen - mlen)
155 if (mt
== M_SYSERR
|| mt
== M_DBERR
) {
156 p
= msg_cat(sp
, "020|Error: ", &len
);
165 * If we're running an ex command that the user didn't enter, display
166 * the file name and line number prefix.
168 if ((mt
== M_ERR
|| mt
== M_SYSERR
) &&
169 sp
!= NULL
&& wp
!= NULL
&& wp
->if_name
!= NULL
) {
170 for (p
= wp
->if_name
; *p
!= '\0'; ++p
) {
171 len
= snprintf(mp
, REM
, "%s", KEY_NAME(sp
, *p
));
173 if ((mlen
+= len
) > blen
)
176 len
= snprintf(mp
, REM
, ", %d: ", wp
->if_lno
);
178 if ((mlen
+= len
) > blen
)
182 /* If nothing to format, we're done. */
185 fmt
= msg_cat(sp
, fmt
, NULL
);
189 * Nvi should run on machines that don't support the numbered argument
190 * specifications (%[digit]*$). We do this by reformatting the string
191 * so that we can hand it to vsprintf(3) and it will use the arguments
192 * in the right order. When vsprintf returns, we put the string back
193 * into the right order. It's undefined, according to SVID III, to mix
194 * numbered argument specifications with the standard style arguments,
195 * so this should be safe.
197 * In addition, we also need a character that is known to not occur in
198 * any vi message, for separating the parts of the string. As callers
199 * of msgq are responsible for making sure that all the non-printable
200 * characters are formatted for printing before calling msgq, we use a
201 * random non-printable character selected at terminal initialization
202 * time. This code isn't fast by any means, but as messages should be
203 * relatively short and normally have only a few arguments, it won't be
204 * too bad. Regardless, nobody has come up with any other solution.
206 * The result of this loop is an array of pointers into the message
207 * string, with associated lengths and argument numbers. The array
208 * is in the "correct" order, and the arg field contains the argument
211 for (p
= fmt
, soff
= 0; soff
< __NL_ARGMAX
;) {
212 for (t
= p
; *p
!= '\0' && *p
!= '%'; ++p
);
216 if (!isdigit((unsigned char)*p
)) {
221 for (u
= p
; *++p
!= '\0' && isdigit((unsigned char)*p
););
225 /* Up to, and including the % character. */
227 str
[soff
].prefix
= u
- t
;
229 /* Up to, and including the $ character. */
230 str
[soff
].arg
= atoi(u
);
231 str
[soff
].skip
= (p
- u
) + 1;
232 if (str
[soff
].arg
>= __NL_ARGMAX
)
235 /* Up to, and including the conversion character. */
236 for (u
= p
; (ch
= (unsigned char)*++p
) != '\0';)
238 strchr("diouxXfeEgGcspn", ch
) != NULL
)
240 str
[soff
].suffix
= p
- u
;
246 /* If no magic strings, we're done. */
250 /* Get space for the reordered strings. */
251 if ((rbp
= malloc(nlen
)) == NULL
)
256 * Reorder the strings into the message string based on argument
260 * We ignore arguments that are out of order, i.e. if we don't find
261 * an argument, we continue. Assume (almost certainly incorrectly)
262 * that whoever created the string knew what they were doing.
265 * Brute force "sort", but since we don't expect more than one or two
266 * arguments in a string, the setup cost of a fast sort will be more
267 * expensive than the loop.
269 for (cnt1
= 1; cnt1
<= soff
; ++cnt1
)
270 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
271 if (cnt1
== str
[cnt2
].arg
) {
272 memmove(s_rbp
, str
[cnt2
].str
, str
[cnt2
].prefix
);
273 memmove(s_rbp
+ str
[cnt2
].prefix
,
274 str
[cnt2
].str
+ str
[cnt2
].prefix
+
275 str
[cnt2
].skip
, str
[cnt2
].suffix
);
276 s_rbp
+= str
[cnt2
].prefix
+ str
[cnt2
].suffix
;
278 gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
;
286 format
: /* Format the arguments into the string. */
293 len
= vsnprintf(mp
, REM
, fmt
, ap
);
303 * Go through the resulting string, and, for each separator character
304 * separated string, enter its new starting position and length in the
307 for (p
= t
= mp
, cnt1
= 1,
308 ch
= gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
; *p
!= '\0'; ++p
)
310 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
311 if (str
[cnt2
].arg
== cnt1
)
314 str
[cnt2
].prefix
= p
- t
;
320 * Reorder the strings once again, putting them back into the
324 * Note, the length of the message gets decremented once for
325 * each substring, when we discard the separator character.
327 for (s_rbp
= rbp
, cnt1
= 0; cnt1
< soff
; ++cnt1
) {
328 memmove(rbp
, str
[cnt1
].str
, str
[cnt1
].prefix
);
329 rbp
+= str
[cnt1
].prefix
;
332 memmove(mp
, s_rbp
, rbp
- s_rbp
);
334 /* Free the reordered string memory. */
339 if ((mlen
+= len
) > blen
)
341 if (mt
== M_SYSERR
) {
342 len
= snprintf(mp
, REM
, ": %s", strerror(errno
));
344 if ((mlen
+= len
) > blen
)
349 len
= snprintf(mp
, REM
, ": %s", db_strerror(sp
->db_error
));
351 if ((mlen
+= len
) > blen
)
356 /* Add trailing newline. */
357 if ((mlen
+= 1) > blen
)
361 if (sp
!= NULL
&& sp
->ep
!= NULL
)
364 wp
->scr_msg(sp
, mt
, bp
, mlen
);
366 (void)fprintf(stderr
, "%.*s", (int)mlen
, bp
);
372 FREE_SPACE(sp
, bp
, blen
);
379 * Display a message with an embedded string.
381 * PUBLIC: void msgq_wstr __P((SCR *, mtype_t, const CHAR_T *, const char *));
384 msgq_wstr(SCR
*sp
, mtype_t mtype
, const CHAR_T
*str
, const char *fmt
)
390 msgq(sp
, mtype
, "%s", fmt
);
393 INT2CHAR(sp
, str
, STRLEN(str
) + 1, nstr
, nlen
);
394 msgq_str(sp
, mtype
, nstr
, fmt
);
399 * Display a message with an embedded string.
401 * PUBLIC: void msgq_str __P((SCR *, mtype_t, const char *, const char *));
404 msgq_str(SCR
*sp
, mtype_t mtype
, const char *str
, const char *fmt
)
410 msgq(sp
, mtype
, "%s", fmt
);
415 p
= msg_print(sp
, str
, &nf
);
417 msgq(sp
, mtype
, fmt
, p
);
419 FREE_SPACE(sp
, p
, 0);
424 * Report on the lines that changed.
427 * Historic vi documentation (USD:15-8) claimed that "The editor will also
428 * always tell you when a change you make affects text which you cannot see."
429 * This wasn't true -- edit a large file and do "100d|1". We don't implement
430 * this semantic since it requires tracking each line that changes during a
431 * command instead of just keeping count.
433 * Line counts weren't right in historic vi, either. For example, given the
437 * the command 2d}, from the 'b' would report that two lines were deleted,
440 * PUBLIC: void mod_rpt __P((SCR *));
445 static const char * const action
[] = {
454 static const char * const lines
[] = {
461 size_t cnt
, blen
, len
, tlen
;
463 const char * const *ap
;
466 /* Change reports are turned off in batch mode. */
467 if (F_ISSET(sp
, SC_EX_SILENT
))
470 /* Reset changing line number. */
471 sp
->rptlchange
= OOBLNO
;
474 * Don't build a message if not enough changed.
477 * And now, a vi clone test. Historically, vi reported if the number
478 * of changed lines was > than the value, not >=, unless it was a yank
479 * command, which used >=. No lie. Furthermore, an action was never
480 * reported for a single line action. This is consistent for actions
481 * other than yank, but yank didn't report single line actions even if
482 * the report edit option was set to 1. In addition, setting report to
483 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
484 * unknown reason (this bug was fixed in System III/V at some point).
485 * I got complaints, so nvi conforms to System III/V historic practice
486 * except that we report a yank of 1 line if report is set to 1.
488 #define ARSIZE(a) sizeof(a) / sizeof (*a)
490 rptval
= O_VAL(sp
, O_REPORT
);
491 for (cnt
= 0, total
= 0; cnt
< ARSIZE(action
); ++cnt
)
492 total
+= sp
->rptlines
[cnt
];
495 if (total
<= rptval
&& sp
->rptlines
[L_YANKED
] < rptval
) {
496 for (cnt
= 0; cnt
< ARSIZE(action
); ++cnt
)
497 sp
->rptlines
[cnt
] = 0;
501 /* Build and display the message. */
502 GET_SPACE_GOTOC(sp
, bp
, blen
, sizeof(action
) * MAXNUM
+ 1);
503 for (p
= bp
, first
= 1, tlen
= 0,
504 ap
= action
, cnt
= 0; cnt
< ARSIZE(action
); ++ap
, ++cnt
)
505 if (sp
->rptlines
[cnt
] != 0) {
513 len
= snprintf(p
, MAXNUM
, "%lu ",
514 (unsigned long) sp
->rptlines
[cnt
]);
518 lines
[sp
->rptlines
[cnt
] == 1 ? 0 : 1], &len
);
524 t
= msg_cat(sp
, *ap
, &len
);
528 sp
->rptlines
[cnt
] = 0;
531 /* Add trailing newline. */
536 sp
->wp
->scr_msg(sp
, M_INFO
, bp
, tlen
);
538 FREE_SPACE(sp
, bp
, blen
);
548 * Report on the file's status.
550 * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
553 msgq_status(SCR
*sp
, db_recno_t lno
, u_int flags
)
559 char **ap
, *bp
, *np
, *p
, *s
;
561 /* Get sufficient memory. */
562 len
= strlen(sp
->frp
->name
);
563 GET_SPACE_GOTOC(sp
, bp
, blen
, len
* MAX_CHARACTER_COLUMNS
+ 128);
566 /* Copy in the filename. */
567 for (p
= bp
, t
= sp
->frp
->name
; *t
!= '\0'; ++t
) {
568 len
= KEY_LEN(sp
, *t
);
569 memcpy(p
, KEY_NAME(sp
, *t
), len
);
576 /* Copy in the argument count. */
577 if (F_ISSET(sp
, SC_STATUS_CNT
) && sp
->argv
!= NULL
) {
578 for (cnt
= 0, ap
= sp
->argv
; *ap
!= NULL
; ++ap
, ++cnt
);
581 msg_cat(sp
, "317|%d files to edit", NULL
), cnt
);
586 F_CLR(sp
, SC_STATUS_CNT
);
590 * See nvi/exf.c:file_init() for a description of how and when the
591 * read-only bit is set.
594 * The historic display for "name changed" was "[Not edited]".
597 if (F_ISSET(sp
->frp
, FR_NEWFILE
)) {
598 F_CLR(sp
->frp
, FR_NEWFILE
);
599 t
= msg_cat(sp
, "021|new file", &len
);
604 if (F_ISSET(sp
->frp
, FR_NAMECHANGE
)) {
605 t
= msg_cat(sp
, "022|name changed", &len
);
614 if (F_ISSET(sp
->ep
, F_MODIFIED
))
615 t
= msg_cat(sp
, "023|modified", &len
);
617 t
= msg_cat(sp
, "024|unmodified", &len
);
622 if (F_ISSET(sp
->frp
, FR_UNLOCKED
)) {
627 t
= msg_cat(sp
, "025|UNLOCKED", &len
);
632 if (O_ISSET(sp
, O_READONLY
)) {
637 t
= msg_cat(sp
, "026|readonly", &len
);
646 if (LF_ISSET(MSTAT_SHOWLAST
)) {
647 if (db_last(sp
, &last
))
650 t
= msg_cat(sp
, "028|empty file", &len
);
654 t
= msg_cat(sp
, "027|line %lu of %lu [%ld%%]", &len
);
655 (void)sprintf(p
, t
, lno
, last
, (lno
* 100) / last
);
659 t
= msg_cat(sp
, "029|line %lu", &len
);
660 (void)sprintf(p
, t
, lno
);
664 (void)sprintf(p
, " (pid %lu)", (u_long
)getpid());
671 * There's a nasty problem with long path names. Cscope and tags files
672 * can result in long paths and vi will request a continuation key from
673 * the user as soon as it starts the screen. Unfortunately, the user
674 * has already typed ahead, and chaos results. If we assume that the
675 * characters in the filenames and informational messages only take a
676 * single screen column each, we can trim the filename.
679 * Status lines get put up at fairly awkward times. For example, when
680 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
681 * split screen, we have to repaint the status lines for all the screens
682 * below the top screen. We don't want users having to enter continue
683 * characters for those screens. Make it really hard to screw this up.
686 if (LF_ISSET(MSTAT_TRUNCATE
) && len
> sp
->cols
) {
687 for (; s
< np
&& (*s
!= '/' || (size_t)(p
- s
) > sp
->cols
- 3); ++s
);
689 s
= p
- (sp
->cols
- 5);
698 /* Flush any waiting ex messages. */
701 sp
->wp
->scr_msg(sp
, M_INFO
, s
, len
);
703 FREE_SPACE(sp
, bp
, blen
);
710 * Open the message catalogs.
712 * PUBLIC: int msg_open __P((SCR *, const char *));
715 msg_open(SCR
*sp
, const char *file
)
719 * Assume that the first file opened is the system default, and that
720 * all subsequent ones user defined. Only display error messages
721 * if we can't open the user defined ones -- it's useful to know if
722 * the system one wasn't there, but if nvi is being shipped with an
723 * installed system, the file will be there, if it's not, then the
724 * message will be repeated every time nvi is started up.
726 static int first
= 1;
730 char buf
[MAXPATHLEN
];
733 if ((p
= strrchr(file
, '/')) != NULL
&& p
[1] == '\0' &&
734 (((t
= getenv("LC_MESSAGES")) != NULL
&& t
[0] != '\0') ||
735 ((t
= getenv("LANG")) != NULL
&& t
[0] != '\0'))) {
736 (void)snprintf(buf
, sizeof(buf
), "%s%s", file
, t
);
740 if (db_msg_open(sp
, p
, &db
)) {
745 msgq_str(sp
, M_DBERR
, p
, "%s");
750 * Test record 1 for the magic string. The msgq call is here so
751 * the message catalog build finds it.
753 #define VMC "VI_MESSAGE_CATALOG"
754 memset(&key
, 0, sizeof(key
));
756 key
.size
= sizeof(db_recno_t
);
757 memset(&data
, 0, sizeof(data
));
759 if ((sp
->db_error
= db_get_low(db
, &key
, &data
, 0)) != 0 ||
760 data
.size
!= sizeof(VMC
) - 1 ||
761 memcmp(data
.data
, VMC
, sizeof(VMC
) - 1)) {
767 msgq_str(sp
, M_DBERR
, p
,
768 "030|The file %s is not a message catalog");
773 if (sp
->gp
->msg
!= NULL
)
774 (void)db_close(sp
->gp
->msg
);
781 * Close the message catalogs.
783 * PUBLIC: void msg_close __P((GS *));
788 if (gp
->msg
!= NULL
) {
789 (void)db_close(gp
->msg
);
796 * Return common continuation messages.
798 * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
801 msg_cmsg(SCR
*sp
, cmsg_t which
, size_t *lenp
)
805 return (msg_cat(sp
, "268|confirm? [ynq]", lenp
));
807 return (msg_cat(sp
, "269|Press any key to continue: ", lenp
));
810 "270|Press any key to continue [: to enter more ex commands]: ",
813 return (msg_cat(sp
, "161|Press Enter to continue: ", lenp
));
815 return (msg_cat(sp
, "275| cont?", lenp
));
818 "271|Press any key to continue [q to quit]: ", lenp
));
827 * Return a single message from the catalog, plus its length.
830 * Only a single catalog message can be accessed at a time, if multiple
831 * ones are needed, they must be copied into local memory.
833 * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
836 msg_cat(SCR
*sp
, const char *str
, size_t *lenp
)
843 * If it's not a catalog message, i.e. has doesn't have a leading
844 * number and '|' symbol, we're done.
846 if (isdigit((unsigned char)str
[0]) &&
847 isdigit((unsigned char)str
[1]) && isdigit((unsigned char)str
[2]) &&
849 memset(&key
, 0, sizeof(key
));
851 key
.size
= sizeof(db_recno_t
);
852 memset(&data
, 0, sizeof(data
));
857 * Really sleazy hack -- we put an extra character on the
858 * end of the format string, and then we change it to be
859 * the nul termination of the string. There ought to be
860 * a better way. Once we can allocate multiple temporary
861 * memory buffers, maybe we can use one of them instead.
863 gp
= sp
== NULL
? NULL
: sp
->gp
;
864 if (gp
!= NULL
&& gp
->msg
!= NULL
&&
865 db_get_low(gp
->msg
, &key
, &data
, 0) == 0 &&
868 *lenp
= data
.size
- 1;
869 ((char *)data
.data
)[data
.size
- 1] = '\0';
881 * Return a printable version of a string, in allocated memory.
883 * PUBLIC: char *msg_print __P((SCR *, const char *, int *));
886 msg_print(SCR
*sp
, const char *s
, int *needfree
)
895 for (cp
= s
; *cp
!= '\0'; ++cp
)
896 if (!isprint((unsigned char)*cp
))
899 return ((char *)__UNCONST(s
)); /* SAFE: needfree set to 0. */
903 retry
: if (sp
== NULL
)
906 FREE_SPACE(sp
, bp
, blen
);
911 if ((bp
= malloc(nlen
)) == NULL
)
914 GET_SPACE_GOTOC(sp
, bp
, blen
, nlen
);
916 alloc_err
: return __UNCONST("");
920 for (p
= bp
, ep
= (bp
+ blen
) - 1, cp
= s
; *cp
!= '\0' && p
< ep
; ++cp
)
921 for (t
= KEY_NAME(sp
, *cp
); *t
!= '\0' && p
< ep
; *p
++ = *t
++);