NULLify closed db and don't call db_last on a closed db in ip_refresh
[nvi.git] / common / msg.c
blobf0ce415d511df527a1e09e81b922a14fcab32345
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: msg.c,v 10.54 2000/07/19 18:31:52 skimo Exp $ (Berkeley) $Date: 2000/07/19 18:31:52 $";
14 #endif /* not lint */
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #ifdef __STDC__
33 #include <stdarg.h>
34 #else
35 #include <varargs.h>
36 #endif
38 #include "common.h"
39 #include "../vi/vi.h"
42 * msgq --
43 * Display a message.
45 * PUBLIC: void msgq __P((SCR *, mtype_t, const char *, ...));
47 void
48 #ifdef __STDC__
49 msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
50 #else
51 msgq(sp, mt, fmt, va_alist)
52 SCR *sp;
53 mtype_t mt;
54 const char *fmt;
55 va_dcl
56 #endif
58 #ifndef NL_ARGMAX
59 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
60 struct {
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. */
66 } str[__NL_ARGMAX];
67 #endif
68 static int reenter; /* STATIC: Re-entrancy check. */
69 CHAR_T ch;
70 GS *gp;
71 WIN *wp;
72 size_t blen, cnt1, cnt2, len, mlen, nlen, soff;
73 const char *p, *t, *u;
74 char *bp, *mp, *rbp, *s_rbp;
75 va_list ap;
78 * !!!
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.
83 if (sp == NULL) {
84 gp = NULL;
85 if (mt == M_BERR)
86 mt = M_ERR;
87 else if (mt == M_VINFO)
88 mt = M_INFO;
89 } else {
90 gp = sp->gp;
91 wp = sp->wp;
92 switch (mt) {
93 case M_BERR:
94 if (F_ISSET(sp, SC_VI) && !O_ISSET(sp, O_VERBOSE)) {
95 F_SET(gp, G_BELLSCHED);
96 return;
98 mt = M_ERR;
99 break;
100 case M_VINFO:
101 if (!O_ISSET(sp, O_VERBOSE))
102 return;
103 mt = M_INFO;
104 /* FALLTHROUGH */
105 case M_INFO:
106 if (F_ISSET(sp, SC_EX_SILENT))
107 return;
108 break;
109 case M_ERR:
110 case M_SYSERR:
111 case M_DBERR:
112 break;
113 default:
114 abort();
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.
122 * XXX
123 * Yes, there's a race, but it should only be two instructions.
125 if (reenter++)
126 return;
128 /* Get space for the message. */
129 nlen = 1024;
130 if (0) {
131 retry: FREE_SPACE(sp, bp, blen);
132 nlen *= 2;
134 bp = NULL;
135 blen = 0;
136 GET_SPACE_GOTO(sp, bp, blen, nlen);
139 * Error prefix.
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)
146 mp = bp;
147 mlen = 0;
148 if (mt == M_SYSERR || mt == M_DBERR) {
149 p = msg_cat(sp, "020|Error: ", &len);
150 if (REM < len)
151 goto retry;
152 memcpy(mp, p, len);
153 mp += len;
154 mlen += 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));
165 mp += len;
166 if ((mlen += len) > blen)
167 goto retry;
169 len = snprintf(mp, REM, ", %d: ", wp->if_lno);
170 mp += len;
171 if ((mlen += len) > blen)
172 goto retry;
175 /* If nothing to format, we're done. */
176 if (fmt == NULL)
177 goto nofmt;
178 fmt = msg_cat(sp, fmt, NULL);
180 #ifndef NL_ARGMAX
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
202 * order.
204 for (p = fmt, soff = 0; soff < __NL_ARGMAX;) {
205 for (t = p; *p != '\0' && *p != '%'; ++p);
206 if (*p == '\0')
207 break;
208 ++p;
209 if (!isdigit(*p)) {
210 if (*p == '%')
211 ++p;
212 continue;
214 for (u = p; *++p != '\0' && isdigit(*p););
215 if (*p != '$')
216 continue;
218 /* Up to, and including the % character. */
219 str[soff].str = t;
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)
226 goto ret;
228 /* Up to, and including the conversion character. */
229 for (u = p; (ch = *++p) != '\0';)
230 if (isalpha(ch) &&
231 strchr("diouxXfeEgGcspn", ch) != NULL)
232 break;
233 str[soff].suffix = p - u;
234 if (ch != '\0')
235 ++p;
236 ++soff;
239 /* If no magic strings, we're done. */
240 if (soff == 0)
241 goto format;
243 /* Get space for the reordered strings. */
244 if ((rbp = malloc(nlen)) == NULL)
245 goto ret;
246 s_rbp = rbp;
249 * Reorder the strings into the message string based on argument
250 * order.
252 * !!!
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.
257 * !!!
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;
270 *s_rbp++ =
271 gp == NULL ? DEFAULT_NOPRINT : gp->noprint;
272 break;
274 *s_rbp = '\0';
275 fmt = rbp;
276 #endif
278 format: /* Format the arguments into the string. */
279 #ifdef __STDC__
280 va_start(ap, fmt);
281 #else
282 va_start(ap);
283 #endif
284 len = vsnprintf(mp, REM, fmt, ap);
285 va_end(ap);
286 if (len >= nlen)
287 goto retry;
289 #ifndef NL_ARGMAX
290 if (soff == 0)
291 goto nofmt;
294 * Go through the resulting string, and, for each separator character
295 * separated string, enter its new starting position and length in the
296 * array.
298 for (p = t = mp, cnt1 = 1,
299 ch = gp == NULL ? DEFAULT_NOPRINT : gp->noprint; *p != '\0'; ++p)
300 if (*p == ch) {
301 for (cnt2 = 0; cnt2 < soff; ++cnt2)
302 if (str[cnt2].arg == cnt1)
303 break;
304 str[cnt2].str = t;
305 str[cnt2].prefix = p - t;
306 t = p + 1;
307 ++cnt1;
311 * Reorder the strings once again, putting them back into the
312 * message buffer.
314 * !!!
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;
321 --len;
323 memmove(mp, s_rbp, rbp - s_rbp);
325 /* Free the reordered string memory. */
326 free(s_rbp);
327 #endif
329 nofmt: mp += len;
330 if ((mlen += len) > blen)
331 goto retry;
332 if (mt == M_SYSERR) {
333 len = snprintf(mp, REM, ": %s", strerror(errno));
334 mp += len;
335 if ((mlen += len) > blen)
336 goto retry;
337 mt = M_ERR;
339 if (mt == M_DBERR) {
340 len = snprintf(mp, REM, ": %s", db_strerror(sp->db_error));
341 mp += len;
342 if ((mlen += len) > blen)
343 goto retry;
344 mt = M_ERR;
347 /* Add trailing newline. */
348 if ((mlen += 1) > blen)
349 goto retry;
350 *mp = '\n';
352 if (sp != NULL)
353 (void)ex_fflush(sp);
354 if (gp != NULL)
355 gp->scr_msg(sp, mt, bp, mlen);
356 else
357 (void)fprintf(stderr, "%.*s", (int)mlen, bp);
359 /* Cleanup. */
360 ret: FREE_SPACE(sp, bp, blen);
361 alloc_err:
362 reenter = 0;
366 * msgq_str --
367 * Display a message with an embedded string.
369 * PUBLIC: void msgq_wstr __P((SCR *, mtype_t, CHAR_T *, char *));
371 void
372 msgq_wstr(sp, mtype, str, fmt)
373 SCR *sp;
374 mtype_t mtype;
375 CHAR_T *str;
376 char *fmt;
378 size_t nlen;
379 char *nstr;
381 INT2CHAR(sp, str, v_strlen(str) + 1, nstr, nlen);
382 msgq_str(sp, mtype, nstr, fmt);
386 * msgq_str --
387 * Display a message with an embedded string.
389 * PUBLIC: void msgq_str __P((SCR *, mtype_t, char *, char *));
391 void
392 msgq_str(sp, mtype, str, fmt)
393 SCR *sp;
394 mtype_t mtype;
395 char *str, *fmt;
397 int nf, sv_errno;
398 char *p;
400 if (str == NULL) {
401 msgq(sp, mtype, fmt);
402 return;
405 sv_errno = errno;
406 p = msg_print(sp, str, &nf);
407 errno = sv_errno;
408 msgq(sp, mtype, fmt, p);
409 if (nf)
410 FREE_SPACE(sp, p, 0);
414 * mod_rpt --
415 * Report on the lines that changed.
417 * !!!
418 * Historic vi documentation (USD:15-8) claimed that "The editor will also
419 * always tell you when a change you make affects text which you cannot see."
420 * This wasn't true -- edit a large file and do "100d|1". We don't implement
421 * this semantic since it requires tracking each line that changes during a
422 * command instead of just keeping count.
424 * Line counts weren't right in historic vi, either. For example, given the
425 * file:
426 * abc
427 * def
428 * the command 2d}, from the 'b' would report that two lines were deleted,
429 * not one.
431 * PUBLIC: void mod_rpt __P((SCR *));
433 void
434 mod_rpt(sp)
435 SCR *sp;
437 static char * const action[] = {
438 "293|added",
439 "294|changed",
440 "295|deleted",
441 "296|joined",
442 "297|moved",
443 "298|shifted",
444 "299|yanked",
446 static char * const lines[] = {
447 "300|line",
448 "301|lines",
450 db_recno_t total;
451 u_long rptval;
452 int first, cnt;
453 size_t blen, len, tlen;
454 const char *t;
455 char * const *ap;
456 char *bp, *p;
458 /* Change reports are turned off in batch mode. */
459 if (F_ISSET(sp, SC_EX_SILENT))
460 return;
462 /* Reset changing line number. */
463 sp->rptlchange = OOBLNO;
466 * Don't build a message if not enough changed.
468 * !!!
469 * And now, a vi clone test. Historically, vi reported if the number
470 * of changed lines was > than the value, not >=, unless it was a yank
471 * command, which used >=. No lie. Furthermore, an action was never
472 * reported for a single line action. This is consistent for actions
473 * other than yank, but yank didn't report single line actions even if
474 * the report edit option was set to 1. In addition, setting report to
475 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
476 * unknown reason (this bug was fixed in System III/V at some point).
477 * I got complaints, so nvi conforms to System III/V historic practice
478 * except that we report a yank of 1 line if report is set to 1.
480 #define ARSIZE(a) sizeof(a) / sizeof (*a)
481 #define MAXNUM 25
482 rptval = O_VAL(sp, O_REPORT);
483 for (cnt = 0, total = 0; cnt < ARSIZE(action); ++cnt)
484 total += sp->rptlines[cnt];
485 if (total == 0)
486 return;
487 if (total <= rptval && sp->rptlines[L_YANKED] < rptval) {
488 for (cnt = 0; cnt < ARSIZE(action); ++cnt)
489 sp->rptlines[cnt] = 0;
490 return;
493 /* Build and display the message. */
494 GET_SPACE_GOTO(sp, bp, blen, sizeof(action) * MAXNUM + 1);
495 for (p = bp, first = 1, tlen = 0,
496 ap = action, cnt = 0; cnt < ARSIZE(action); ++ap, ++cnt)
497 if (sp->rptlines[cnt] != 0) {
498 if (first)
499 first = 0;
500 else {
501 *p++ = ';';
502 *p++ = ' ';
503 tlen += 2;
505 len = snprintf(p, MAXNUM, "%lu ", sp->rptlines[cnt]);
506 p += len;
507 tlen += len;
508 t = msg_cat(sp,
509 lines[sp->rptlines[cnt] == 1 ? 0 : 1], &len);
510 memcpy(p, t, len);
511 p += len;
512 tlen += len;
513 *p++ = ' ';
514 ++tlen;
515 t = msg_cat(sp, *ap, &len);
516 memcpy(p, t, len);
517 p += len;
518 tlen += len;
519 sp->rptlines[cnt] = 0;
522 /* Add trailing newline. */
523 *p = '\n';
524 ++tlen;
526 (void)ex_fflush(sp);
527 sp->gp->scr_msg(sp, M_INFO, bp, tlen);
529 FREE_SPACE(sp, bp, blen);
530 alloc_err:
531 return;
533 #undef ARSIZE
534 #undef MAXNUM
538 * msgq_status --
539 * Report on the file's status.
541 * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
543 void
544 msgq_status(sp, lno, flags)
545 SCR *sp;
546 db_recno_t lno;
547 u_int flags;
549 db_recno_t last;
550 size_t blen, len;
551 int cnt, needsep;
552 const char *t;
553 char **ap, *bp, *np, *p, *s;
555 /* Get sufficient memory. */
556 len = strlen(sp->frp->name);
557 GET_SPACE_GOTO(sp, bp, blen, len * MAX_CHARACTER_COLUMNS + 128);
558 p = bp;
560 /* Copy in the filename. */
561 for (p = bp, t = sp->frp->name; *t != '\0'; ++t) {
562 len = KEY_LEN(sp, *t);
563 memcpy(p, KEY_NAME(sp, *t), len);
564 p += len;
566 np = p;
567 *p++ = ':';
568 *p++ = ' ';
570 /* Copy in the argument count. */
571 if (F_ISSET(sp, SC_STATUS_CNT) && sp->argv != NULL) {
572 for (cnt = 0, ap = sp->argv; *ap != NULL; ++ap, ++cnt);
573 if (cnt > 1) {
574 (void)sprintf(p,
575 msg_cat(sp, "317|%d files to edit", NULL), cnt);
576 p += strlen(p);
577 *p++ = ':';
578 *p++ = ' ';
580 F_CLR(sp, SC_STATUS_CNT);
584 * See nvi/exf.c:file_init() for a description of how and when the
585 * read-only bit is set.
587 * !!!
588 * The historic display for "name changed" was "[Not edited]".
590 needsep = 0;
591 if (F_ISSET(sp->frp, FR_NEWFILE)) {
592 F_CLR(sp->frp, FR_NEWFILE);
593 t = msg_cat(sp, "021|new file", &len);
594 memcpy(p, t, len);
595 p += len;
596 needsep = 1;
597 } else {
598 if (F_ISSET(sp->frp, FR_NAMECHANGE)) {
599 t = msg_cat(sp, "022|name changed", &len);
600 memcpy(p, t, len);
601 p += len;
602 needsep = 1;
604 if (needsep) {
605 *p++ = ',';
606 *p++ = ' ';
608 if (F_ISSET(sp->ep, F_MODIFIED))
609 t = msg_cat(sp, "023|modified", &len);
610 else
611 t = msg_cat(sp, "024|unmodified", &len);
612 memcpy(p, t, len);
613 p += len;
614 needsep = 1;
616 if (F_ISSET(sp->frp, FR_UNLOCKED)) {
617 if (needsep) {
618 *p++ = ',';
619 *p++ = ' ';
621 t = msg_cat(sp, "025|UNLOCKED", &len);
622 memcpy(p, t, len);
623 p += len;
624 needsep = 1;
626 if (O_ISSET(sp, O_READONLY)) {
627 if (needsep) {
628 *p++ = ',';
629 *p++ = ' ';
631 t = msg_cat(sp, "026|readonly", &len);
632 memcpy(p, t, len);
633 p += len;
634 needsep = 1;
636 if (needsep) {
637 *p++ = ':';
638 *p++ = ' ';
640 if (LF_ISSET(MSTAT_SHOWLAST)) {
641 if (db_last(sp, &last))
642 return;
643 if (last == 0) {
644 t = msg_cat(sp, "028|empty file", &len);
645 memcpy(p, t, len);
646 p += len;
647 } else {
648 t = msg_cat(sp, "027|line %lu of %lu [%ld%%]", &len);
649 (void)sprintf(p, t, lno, last, (lno * 100) / last);
650 p += strlen(p);
652 } else {
653 t = msg_cat(sp, "029|line %lu", &len);
654 (void)sprintf(p, t, lno);
655 p += strlen(p);
657 #ifdef DEBUG
658 (void)sprintf(p, " (pid %lu)", (u_long)getpid());
659 p += strlen(p);
660 #endif
661 *p++ = '\n';
662 len = p - bp;
665 * There's a nasty problem with long path names. Cscope and tags files
666 * can result in long paths and vi will request a continuation key from
667 * the user as soon as it starts the screen. Unfortunately, the user
668 * has already typed ahead, and chaos results. If we assume that the
669 * characters in the filenames and informational messages only take a
670 * single screen column each, we can trim the filename.
672 * XXX
673 * Status lines get put up at fairly awkward times. For example, when
674 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
675 * split screen, we have to repaint the status lines for all the screens
676 * below the top screen. We don't want users having to enter continue
677 * characters for those screens. Make it really hard to screw this up.
679 s = bp;
680 if (LF_ISSET(MSTAT_TRUNCATE) && len > sp->cols) {
681 for (; s < np && (*s != '/' || (p - s) > sp->cols - 3); ++s);
682 if (s == np) {
683 s = p - (sp->cols - 5);
684 *--s = ' ';
686 *--s = '.';
687 *--s = '.';
688 *--s = '.';
689 len = p - s;
692 /* Flush any waiting ex messages. */
693 (void)ex_fflush(sp);
695 sp->gp->scr_msg(sp, M_INFO, s, len);
697 FREE_SPACE(sp, bp, blen);
698 alloc_err:
699 return;
703 * msg_open --
704 * Open the message catalogs.
706 * PUBLIC: int msg_open __P((SCR *, char *));
709 msg_open(sp, file)
710 SCR *sp;
711 char *file;
714 * !!!
715 * Assume that the first file opened is the system default, and that
716 * all subsequent ones user defined. Only display error messages
717 * if we can't open the user defined ones -- it's useful to know if
718 * the system one wasn't there, but if nvi is being shipped with an
719 * installed system, the file will be there, if it's not, then the
720 * message will be repeated every time nvi is started up.
722 static int first = 1;
723 DB *db;
724 DBT data, key;
725 db_recno_t msgno;
726 char *p, *t, buf[MAXPATHLEN];
728 if ((p = strrchr(file, '/')) != NULL && p[1] == '\0' &&
729 (((t = getenv("LC_MESSAGES")) != NULL && t[0] != '\0') ||
730 ((t = getenv("LANG")) != NULL && t[0] != '\0'))) {
731 (void)snprintf(buf, sizeof(buf), "%s%s", file, t);
732 p = buf;
733 } else
734 p = file;
735 if ((sp->db_error = db_create(&db, sp->gp->env, 0)) != 0 ||
736 (sp->db_error = db->set_re_source(db, p)) != 0 ||
737 (sp->db_error = db->open(db, NULL, NULL, DB_RECNO, 0, 0)) != 0) {
738 if (first) {
739 first = 0;
740 return (1);
742 msgq_str(sp, M_DBERR, p, "%s");
743 return (1);
747 * Test record 1 for the magic string. The msgq call is here so
748 * the message catalog build finds it.
750 #define VMC "VI_MESSAGE_CATALOG"
751 memset(&key, 0, sizeof(key));
752 key.data = &msgno;
753 key.size = sizeof(db_recno_t);
754 memset(&data, 0, sizeof(data));
755 msgno = 1;
756 if ((sp->db_error = db->get(db, NULL, &key, &data, 0)) != 0 ||
757 data.size != sizeof(VMC) - 1 ||
758 memcmp(data.data, VMC, sizeof(VMC) - 1)) {
759 (void)db->close(db, DB_NOSYNC);
760 if (first) {
761 first = 0;
762 return (1);
764 msgq_str(sp, M_DBERR, p,
765 "030|The file %s is not a message catalog");
766 return (1);
768 first = 0;
770 if (sp->gp->msg != NULL)
771 (void)sp->gp->msg->close(sp->gp->msg, DB_NOSYNC);
772 sp->gp->msg = db;
773 return (0);
777 * msg_close --
778 * Close the message catalogs.
780 * PUBLIC: void msg_close __P((GS *));
782 void
783 msg_close(gp)
784 GS *gp;
786 if (gp->msg != NULL) {
787 (void)gp->msg->close(gp->msg, 0);
788 gp->msg = NULL;
793 * msg_cont --
794 * Return common continuation messages.
796 * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
798 const char *
799 msg_cmsg(sp, which, lenp)
800 SCR *sp;
801 cmsg_t which;
802 size_t *lenp;
804 switch (which) {
805 case CMSG_CONF:
806 return (msg_cat(sp, "268|confirm? [ynq]", lenp));
807 case CMSG_CONT:
808 return (msg_cat(sp, "269|Press any key to continue: ", lenp));
809 case CMSG_CONT_EX:
810 return (msg_cat(sp,
811 "270|Press any key to continue [: to enter more ex commands]: ",
812 lenp));
813 case CMSG_CONT_R:
814 return (msg_cat(sp, "161|Press Enter to continue: ", lenp));
815 case CMSG_CONT_S:
816 return (msg_cat(sp, "275| cont?", lenp));
817 case CMSG_CONT_Q:
818 return (msg_cat(sp,
819 "271|Press any key to continue [q to quit]: ", lenp));
820 default:
821 abort();
823 /* NOTREACHED */
827 * msg_cat --
828 * Return a single message from the catalog, plus its length.
830 * !!!
831 * Only a single catalog message can be accessed at a time, if multiple
832 * ones are needed, they must be copied into local memory.
834 * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
836 const char *
837 msg_cat(sp, str, lenp)
838 SCR *sp;
839 const char *str;
840 size_t *lenp;
842 GS *gp;
843 DBT data, key;
844 db_recno_t msgno;
847 * If it's not a catalog message, i.e. has doesn't have a leading
848 * number and '|' symbol, we're done.
850 if (isdigit(str[0]) &&
851 isdigit(str[1]) && isdigit(str[2]) && str[3] == '|') {
852 memset(&key, 0, sizeof(key));
853 key.data = &msgno;
854 key.size = sizeof(db_recno_t);
855 memset(&data, 0, sizeof(data));
856 msgno = atoi(str);
859 * XXX
860 * Really sleazy hack -- we put an extra character on the
861 * end of the format string, and then we change it to be
862 * the nul termination of the string. There ought to be
863 * a better way. Once we can allocate multiple temporary
864 * memory buffers, maybe we can use one of them instead.
866 gp = sp == NULL ? NULL : sp->gp;
867 if (gp != NULL && gp->msg != NULL &&
868 gp->msg->get(gp->msg, NULL, &key, &data, 0) == 0 &&
869 data.size != 0) {
870 if (lenp != NULL)
871 *lenp = data.size - 1;
872 ((char *)data.data)[data.size - 1] = '\0';
873 return (data.data);
875 str = &str[4];
877 if (lenp != NULL)
878 *lenp = strlen(str);
879 return (str);
883 * msg_print --
884 * Return a printable version of a string, in allocated memory.
886 * PUBLIC: char *msg_print __P((SCR *, const char *, int *));
888 char *
889 msg_print(sp, s, needfree)
890 SCR *sp;
891 const char *s;
892 int *needfree;
894 size_t blen, nlen;
895 const char *cp;
896 char *bp, *ep, *p, *t;
898 *needfree = 0;
900 for (cp = s; *cp != '\0'; ++cp)
901 if (!isprint(*cp))
902 break;
903 if (*cp == '\0')
904 return ((char *)s); /* SAFE: needfree set to 0. */
906 nlen = 0;
907 if (0) {
908 retry: if (sp == NULL)
909 free(bp);
910 else
911 FREE_SPACE(sp, bp, blen);
912 needfree = 0;
914 nlen += 256;
915 if (sp == NULL) {
916 if ((bp = malloc(nlen)) == NULL)
917 goto alloc_err;
918 } else
919 GET_SPACE_GOTO(sp, bp, blen, nlen);
920 if (0) {
921 alloc_err: return ("");
923 *needfree = 1;
925 for (p = bp, ep = (bp + blen) - 1, cp = s; *cp != '\0' && p < ep; ++cp)
926 for (t = KEY_NAME(sp, *cp); *t != '\0' && p < ep; *p++ = *t++);
927 if (p == ep)
928 goto retry;
929 *p = '\0';
930 return (bp);