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 11.1 2015/02/09 11:12:44 marc Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
39 * PUBLIC: void msgq(SCR *, mtype_t, const char *, ...);
49 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
51 const char *str
; /* String pointer. */
52 size_t arg
; /* Argument number. */
53 size_t prefix
; /* Prefix string length. */
54 size_t skip
; /* Skipped string length. */
55 size_t suffix
; /* Suffix string length. */
58 static int reenter
; /* STATIC: Re-entrancy check. */
60 size_t blen
, len
, mlen
, nlen
;
68 size_t cnt1
, cnt2
, soff
;
73 * It's possible to enter msg when there's no screen to hold the
74 * message. If sp is NULL, ignore the special cases and put the
75 * message out to stderr.
81 else if (mt
== M_VINFO
)
87 if (F_ISSET(sp
, SC_VI
) && !O_ISSET(sp
, O_VERBOSE
)) {
88 F_SET(gp
, G_BELLSCHED
);
94 if (!O_ISSET(sp
, O_VERBOSE
))
99 if (F_ISSET(sp
, SC_EX_SILENT
))
111 * It's possible to reenter msg when it allocates space. We're
112 * probably dead anyway, but there's no reason to drop core.
115 * Yes, there's a race, but it should only be two instructions.
120 /* Get space for the message. */
123 retry
: FREE_SPACE(sp
, bp
, blen
);
128 GET_SPACE_GOTOC(sp
, bp
, blen
, nlen
);
133 * mp: pointer to the current next character to be written
134 * mlen: length of the already written characters
135 * blen: total length of the buffer
137 #define REM (blen - mlen)
140 if (mt
== M_SYSERR
) {
141 p
= msg_cat(sp
, "020|Error: ", &len
);
150 * If we're running an ex command that the user didn't enter, display
151 * the file name and line number prefix.
153 if ((mt
== M_ERR
|| mt
== M_SYSERR
) &&
154 sp
!= NULL
&& gp
!= NULL
&& gp
->if_name
!= NULL
) {
158 CHAR2INT(sp
, gp
->if_name
, strlen(gp
->if_name
) + 1, wp
, wlen
);
159 for (; *wp
!= '\0'; ++wp
) {
160 len
= snprintf(mp
, REM
, "%s", KEY_NAME(sp
, *wp
));
162 if ((mlen
+= len
) > blen
)
165 len
= snprintf(mp
, REM
, ", %d: ", gp
->if_lno
);
167 if ((mlen
+= len
) > blen
)
171 /* If nothing to format, we're done. */
174 fmt
= msg_cat(sp
, fmt
, NULL
);
178 * Nvi should run on machines that don't support the numbered argument
179 * specifications (%[digit]*$). We do this by reformatting the string
180 * so that we can hand it to vsprintf(3) and it will use the arguments
181 * in the right order. When vsprintf returns, we put the string back
182 * into the right order. It's undefined, according to SVID III, to mix
183 * numbered argument specifications with the standard style arguments,
184 * so this should be safe.
186 * In addition, we also need a character that is known to not occur in
187 * any vi message, for separating the parts of the string. As callers
188 * of msgq are responsible for making sure that all the non-printable
189 * characters are formatted for printing before calling msgq, we use a
190 * random non-printable character selected at terminal initialization
191 * time. This code isn't fast by any means, but as messages should be
192 * relatively short and normally have only a few arguments, it won't be
193 * too bad. Regardless, nobody has come up with any other solution.
195 * The result of this loop is an array of pointers into the message
196 * string, with associated lengths and argument numbers. The array
197 * is in the "correct" order, and the arg field contains the argument
200 for (p
= fmt
, soff
= 0; soff
< __NL_ARGMAX
;) {
201 for (t
= p
; *p
!= '\0' && *p
!= '%'; ++p
);
210 for (u
= p
; *++p
!= '\0' && isdigit(*p
););
214 /* Up to, and including the % character. */
216 str
[soff
].prefix
= u
- t
;
218 /* Up to, and including the $ character. */
219 str
[soff
].arg
= atoi(u
);
220 str
[soff
].skip
= (p
- u
) + 1;
221 if (str
[soff
].arg
>= __NL_ARGMAX
)
224 /* Up to, and including the conversion character. */
225 for (u
= p
; (ch
= *++p
) != '\0';)
227 strchr("diouxXfeEgGcspn", ch
) != NULL
)
229 str
[soff
].suffix
= p
- u
;
235 /* If no magic strings, we're done. */
239 /* Get space for the reordered strings. */
240 if ((rbp
= malloc(nlen
)) == NULL
)
245 * Reorder the strings into the message string based on argument
249 * We ignore arguments that are out of order, i.e. if we don't find
250 * an argument, we continue. Assume (almost certainly incorrectly)
251 * that whoever created the string knew what they were doing.
254 * Brute force "sort", but since we don't expect more than one or two
255 * arguments in a string, the setup cost of a fast sort will be more
256 * expensive than the loop.
258 for (cnt1
= 1; cnt1
<= soff
; ++cnt1
)
259 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
260 if (cnt1
== str
[cnt2
].arg
) {
261 memmove(s_rbp
, str
[cnt2
].str
, str
[cnt2
].prefix
);
262 memmove(s_rbp
+ str
[cnt2
].prefix
,
263 str
[cnt2
].str
+ str
[cnt2
].prefix
+
264 str
[cnt2
].skip
, str
[cnt2
].suffix
);
265 s_rbp
+= str
[cnt2
].prefix
+ str
[cnt2
].suffix
;
267 gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
;
275 format
: /* Format the arguments into the string. */
278 len
= vsnprintf(mp
, REM
, fmt
, ap
);
288 * Go through the resulting string, and, for each separator character
289 * separated string, enter its new starting position and length in the
292 for (p
= t
= mp
, cnt1
= 1,
293 ch
= gp
== NULL
? DEFAULT_NOPRINT
: gp
->noprint
; *p
!= '\0'; ++p
)
295 for (cnt2
= 0; cnt2
< soff
; ++cnt2
)
296 if (str
[cnt2
].arg
== cnt1
)
299 str
[cnt2
].prefix
= p
- t
;
305 * Reorder the strings once again, putting them back into the
309 * Note, the length of the message gets decremented once for
310 * each substring, when we discard the separator character.
312 for (s_rbp
= rbp
, cnt1
= 0; cnt1
< soff
; ++cnt1
) {
313 memmove(rbp
, str
[cnt1
].str
, str
[cnt1
].prefix
);
314 rbp
+= str
[cnt1
].prefix
;
317 memmove(mp
, s_rbp
, rbp
- s_rbp
);
319 /* Free the reordered string memory. */
324 if ((mlen
+= len
) > blen
)
326 if (mt
== M_SYSERR
) {
327 len
= snprintf(mp
, REM
, ": %s", strerror(errno
));
329 if ((mlen
+= len
) > blen
)
334 /* Add trailing newline. */
335 if ((mlen
+= 1) > blen
)
342 gp
->scr_msg(sp
, mt
, bp
, mlen
);
344 (void)fprintf(stderr
, "%.*s", (int)mlen
, bp
);
350 FREE_SPACE(sp
, bp
, blen
);
357 * Display a message with an embedded string.
359 * PUBLIC: void msgq_wstr(SCR *, mtype_t, const CHAR_T *, const char *);
372 msgq(sp
, mtype
, "%s", fmt
);
375 INT2CHAR(sp
, str
, STRLEN(str
) + 1, nstr
, nlen
);
376 msgq_str(sp
, mtype
, nstr
, fmt
);
381 * Display a message with an embedded string.
383 * PUBLIC: void msgq_str(SCR *, mtype_t, const char *, const char *);
396 msgq(sp
, mtype
, "%s", fmt
);
401 p
= msg_print(sp
, str
, &nf
);
403 msgq(sp
, mtype
, fmt
, p
);
405 FREE_SPACE(sp
, p
, 0);
410 * Report on the lines that changed.
413 * Historic vi documentation (USD:15-8) claimed that "The editor will also
414 * always tell you when a change you make affects text which you cannot see."
415 * This wasn't true -- edit a large file and do "100d|1". We don't implement
416 * this semantic since it requires tracking each line that changes during a
417 * command instead of just keeping count.
419 * Line counts weren't right in historic vi, either. For example, given the
423 * the command 2d}, from the 'b' would report that two lines were deleted,
426 * PUBLIC: void mod_rpt(SCR *);
431 static char * const action
[] = {
440 static char * const lines
[] = {
447 size_t blen
, len
, tlen
;
452 /* Change reports are turned off in batch mode. */
453 if (F_ISSET(sp
, SC_EX_SILENT
))
456 /* Reset changing line number. */
457 sp
->rptlchange
= OOBLNO
;
460 * Don't build a message if not enough changed.
463 * And now, a vi clone test. Historically, vi reported if the number
464 * of changed lines was > than the value, not >=, unless it was a yank
465 * command, which used >=. No lie. Furthermore, an action was never
466 * reported for a single line action. This is consistent for actions
467 * other than yank, but yank didn't report single line actions even if
468 * the report edit option was set to 1. In addition, setting report to
469 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
470 * unknown reason (this bug was fixed in System III/V at some point).
471 * I got complaints, so nvi conforms to System III/V historic practice
472 * except that we report a yank of 1 line if report is set to 1.
474 #define ARSIZE(a) sizeof(a) / sizeof (*a)
476 rptval
= O_VAL(sp
, O_REPORT
);
477 for (cnt
= 0, total
= 0; cnt
< ARSIZE(action
); ++cnt
)
478 total
+= sp
->rptlines
[cnt
];
481 if (total
<= rptval
&& sp
->rptlines
[L_YANKED
] < rptval
) {
482 for (cnt
= 0; cnt
< ARSIZE(action
); ++cnt
)
483 sp
->rptlines
[cnt
] = 0;
487 /* Build and display the message. */
488 GET_SPACE_GOTOC(sp
, bp
, blen
, sizeof(action
) * MAXNUM
+ 1);
489 for (p
= bp
, first
= 1, tlen
= 0,
490 ap
= action
, cnt
= 0; cnt
< ARSIZE(action
); ++ap
, ++cnt
)
491 if (sp
->rptlines
[cnt
] != 0) {
499 len
= snprintf(p
, MAXNUM
, "%lu ",
500 (u_long
)sp
->rptlines
[cnt
]);
504 lines
[sp
->rptlines
[cnt
] == 1 ? 0 : 1], &len
);
510 t
= msg_cat(sp
, *ap
, &len
);
514 sp
->rptlines
[cnt
] = 0;
517 /* Add trailing newline. */
522 sp
->gp
->scr_msg(sp
, M_INFO
, bp
, tlen
);
524 FREE_SPACE(sp
, bp
, blen
);
534 * Report on the file's status.
536 * PUBLIC: void msgq_status(SCR *, recno_t, u_int);
548 char **ap
, *bp
, *np
, *p
, *s
, *ep
;
552 /* Get sufficient memory. */
553 len
= strlen(sp
->frp
->name
);
554 GET_SPACE_GOTOC(sp
, bp
, blen
, len
* MAX_CHARACTER_COLUMNS
+ 128);
558 /* Convert the filename. */
559 CHAR2INT(sp
, sp
->frp
->name
, len
+ 1, wp
, wlen
);
561 /* Copy in the filename. */
562 for (; *wp
!= '\0'; ++wp
) {
563 len
= KEY_LEN(sp
, *wp
);
564 memcpy(p
, KEY_NAME(sp
, *wp
), len
);
571 /* Copy in the argument count. */
572 if (F_ISSET(sp
, SC_STATUS_CNT
) && sp
->argv
!= NULL
) {
573 for (cnt
= 0, ap
= sp
->argv
; *ap
!= NULL
; ++ap
, ++cnt
);
575 (void)snprintf(p
, ep
- p
,
576 msg_cat(sp
, "317|%d files to edit", NULL
), cnt
);
581 F_CLR(sp
, SC_STATUS_CNT
);
585 * See nvi/exf.c:file_init() for a description of how and when the
586 * read-only bit is set.
589 * The historic display for "name changed" was "[Not edited]".
592 if (F_ISSET(sp
->frp
, FR_NEWFILE
)) {
593 F_CLR(sp
->frp
, FR_NEWFILE
);
594 t
= msg_cat(sp
, "021|new file", &len
);
599 if (F_ISSET(sp
->frp
, FR_NAMECHANGE
)) {
600 t
= msg_cat(sp
, "022|name changed", &len
);
609 if (F_ISSET(sp
->ep
, F_MODIFIED
))
610 t
= msg_cat(sp
, "023|modified", &len
);
612 t
= msg_cat(sp
, "024|unmodified", &len
);
617 if (F_ISSET(sp
->frp
, FR_UNLOCKED
)) {
622 t
= msg_cat(sp
, "025|UNLOCKED", &len
);
627 if (O_ISSET(sp
, O_READONLY
)) {
632 t
= msg_cat(sp
, "026|readonly", &len
);
641 if (LF_ISSET(MSTAT_SHOWLAST
)) {
642 if (db_last(sp
, &last
))
645 t
= msg_cat(sp
, "028|empty file", &len
);
649 t
= msg_cat(sp
, "027|line %lu of %lu [%ld%%]", &len
);
650 (void)snprintf(p
, ep
- p
, t
, (u_long
)lno
, (u_long
)last
,
651 ((u_long
)lno
* 100) / last
);
655 t
= msg_cat(sp
, "029|line %lu", &len
);
656 (void)snprintf(p
, ep
- p
, t
, (u_long
)lno
);
660 (void)snprintf(p
, ep
- p
, " (pid %lu)", (u_long
)getpid());
667 * There's a nasty problem with long path names. Cscope and tags files
668 * can result in long paths and vi will request a continuation key from
669 * the user as soon as it starts the screen. Unfortunately, the user
670 * has already typed ahead, and chaos results. If we assume that the
671 * characters in the filenames and informational messages only take a
672 * single screen column each, we can trim the filename.
675 * Status lines get put up at fairly awkward times. For example, when
676 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
677 * split screen, we have to repaint the status lines for all the screens
678 * below the top screen. We don't want users having to enter continue
679 * characters for those screens. Make it really hard to screw this up.
682 if (LF_ISSET(MSTAT_TRUNCATE
) && len
> sp
->cols
) {
683 for (; s
< np
&& (*s
!= '/' || (p
- s
) > sp
->cols
- 3); ++s
);
685 s
= p
- (sp
->cols
- 5);
694 /* Flush any waiting ex messages. */
697 sp
->gp
->scr_msg(sp
, M_INFO
, s
, len
);
699 FREE_SPACE(sp
, bp
, blen
);
706 * Open the message catalogs.
708 * PUBLIC: int msg_open(SCR *, char *);
717 * Assume that the first file opened is the system default, and that
718 * all subsequent ones user defined. Only display error messages
719 * if we can't open the user defined ones -- it's useful to know if
720 * the system one wasn't there, but if nvi is being shipped with an
721 * installed system, the file will be there, if it's not, then the
722 * message will be repeated every time nvi is started up.
724 static int first
= 1;
729 if ((p
= strrchr(file
, '/')) != NULL
&& p
[1] == '\0') {
730 /* Confirms to XPG4. */
731 if ((p
= join(file
, setlocale(LC_MESSAGES
, NULL
))) == NULL
) {
732 msgq(sp
, M_SYSERR
, NULL
);
736 /* Make sure it's recognized as a path by catopen(3). */
737 if ((p
= join(".", file
)) == NULL
) {
738 msgq(sp
, M_SYSERR
, NULL
);
743 if ((catd
= catopen(p
, NL_CAT_LOCALE
)) == (nl_catd
)-1) {
751 * POSIX.1-2008 gives no instruction on how to report a
752 * corrupt catalog file. Errno == 0 is not rare; add
753 * EFTYPE, which is seen on FreeBSD, for a good measure.
755 if (errno
== 0 || errno
== EFTYPE
)
756 msgq_str(sp
, M_ERR
, p
,
757 "030|The file %s is not a message catalog");
759 msgq_str(sp
, M_SYSERR
, p
, "%s");
773 * Close the message catalogs.
775 * PUBLIC: void msg_close(GS *);
780 if (gp
->catd
!= (nl_catd
)-1)
781 (void)catclose(gp
->catd
);
786 * Return common continuation messages.
788 * PUBLIC: const char *msg_cmsg(SCR *, cmsg_t, size_t *);
798 return (msg_cat(sp
, "268|confirm? [ynq]", lenp
));
800 return (msg_cat(sp
, "269|Press any key to continue: ", lenp
));
803 "270|Press any key to continue [: to enter more ex commands]: ",
806 return (msg_cat(sp
, "161|Press Enter to continue: ", lenp
));
808 return (msg_cat(sp
, "275| cont?", lenp
));
811 "271|Press any key to continue [q to quit]: ", lenp
));
820 * Return a single message from the catalog, plus its length.
823 * Only a single catalog message can be accessed at a time, if multiple
824 * ones are needed, they must be copied into local memory.
826 * PUBLIC: const char *msg_cat(SCR *, const char *, size_t *);
839 * If it's not a catalog message, i.e. has doesn't have a leading
840 * number and '|' symbol, we're done.
842 if (isdigit(str
[0]) &&
843 isdigit(str
[1]) && isdigit(str
[2]) && str
[3] == '|') {
847 gp
= sp
== NULL
? NULL
: sp
->gp
;
848 if (gp
!= NULL
&& gp
->catd
!= (nl_catd
)-1 &&
849 (p
= catgets(gp
->catd
, 1, msgno
, str
)) != NULL
) {
862 * Return a printable version of a string, in allocated memory.
864 * PUBLIC: char *msg_print(SCR *, const char *, int *);
873 char *bp
, *ep
, *p
, *t
;
879 /* XXX Not good for debugging ex_read & ex_filter.*/
880 CHAR2INT5(sp
, EXP(sp
)->ibcw
, (char *)s
, strlen(s
) + 1, wp
, wlen
);
881 for (cp
= wp
; *cp
!= '\0'; ++cp
)
885 return ((char *)s
); /* SAFE: needfree set to 0. */
889 retry
: if (sp
== NULL
)
892 FREE_SPACE(sp
, bp
, blen
);
897 if ((bp
= malloc(nlen
)) == NULL
)
900 GET_SPACE_GOTOC(sp
, bp
, blen
, nlen
);
902 alloc_err
: return ("");
906 for (p
= bp
, ep
= (bp
+ blen
) - 1; *wp
!= '\0' && p
< ep
; ++wp
)
907 for (t
= KEY_NAME(sp
, *wp
); *t
!= '\0' && p
< ep
; *p
++ = *t
++);