Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / msg.c
blobff29eecd0e48d0cca7c4205c8471d89845019863
1 /* $NetBSD: msg.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/param.h>
19 #include <sys/types.h> /* XXX: param.h may not have included types.h */
20 #include <sys/queue.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
24 #include <bitstring.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #ifdef __STDC__
35 #include <stdarg.h>
36 #else
37 #include <varargs.h>
38 #endif
40 #include "common.h"
41 #include "dbinternal.h"
42 #include "../vi/vi.h"
45 * msgq --
46 * Display a message.
48 * PUBLIC: void msgq __P((SCR *, mtype_t, const char *, ...));
50 void
51 #ifdef __STDC__
52 msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
53 #else
54 msgq(sp, mt, fmt, va_alist)
55 SCR *sp;
56 mtype_t mt;
57 const char *fmt;
58 va_dcl
59 #endif
61 #ifndef NL_ARGMAX
62 #define __NL_ARGMAX 20 /* Set to 9 by System V. */
63 struct {
64 const char *str; /* String pointer. */
65 size_t arg; /* Argument number. */
66 size_t prefix; /* Prefix string length. */
67 size_t skip; /* Skipped string length. */
68 size_t suffix; /* Suffix string length. */
69 } str[__NL_ARGMAX];
70 #endif
71 static int reenter; /* STATIC: Re-entrancy check. */
72 GS *gp;
73 WIN *wp = NULL;
74 size_t blen, len, mlen, nlen;
75 const char *p;
76 char *bp, *mp;
77 va_list ap;
78 #ifndef NL_ARGMAX
79 CHAR_T ch;
80 char *rbp, *s_rbp;
81 const char *t, *u;
82 size_t cnt1, cnt2, soff;
83 #endif
86 * !!!
87 * It's possible to enter msg when there's no screen to hold the
88 * message. If sp is NULL, ignore the special cases and put the
89 * message out to stderr.
91 if (sp == NULL) {
92 gp = NULL;
93 if (mt == M_BERR)
94 mt = M_ERR;
95 else if (mt == M_VINFO)
96 mt = M_INFO;
97 } else {
98 gp = sp->gp;
99 wp = sp->wp;
100 switch (mt) {
101 case M_BERR:
102 if (F_ISSET(sp, SC_VI) && !O_ISSET(sp, O_VERBOSE)) {
103 F_SET(gp, G_BELLSCHED);
104 return;
106 mt = M_ERR;
107 break;
108 case M_VINFO:
109 if (!O_ISSET(sp, O_VERBOSE))
110 return;
111 mt = M_INFO;
112 /* FALLTHROUGH */
113 case M_INFO:
114 if (F_ISSET(sp, SC_EX_SILENT))
115 return;
116 break;
117 case M_ERR:
118 case M_SYSERR:
119 case M_DBERR:
120 break;
121 default:
122 abort();
127 * It's possible to reenter msg when it allocates space. We're
128 * probably dead anyway, but there's no reason to drop core.
130 * XXX
131 * Yes, there's a race, but it should only be two instructions.
133 if (reenter++)
134 return;
136 /* Get space for the message. */
137 nlen = 1024;
138 if (0) {
139 retry: FREE_SPACE(sp, bp, blen);
140 nlen *= 2;
142 bp = NULL;
143 blen = 0;
144 GET_SPACE_GOTOC(sp, bp, blen, nlen);
147 * Error prefix.
149 * mp: pointer to the current next character to be written
150 * mlen: length of the already written characters
151 * blen: total length of the buffer
153 #define REM (blen - mlen)
154 mp = bp;
155 mlen = 0;
156 if (mt == M_SYSERR || mt == M_DBERR) {
157 p = msg_cat(sp, "020|Error: ", &len);
158 if (REM < len)
159 goto retry;
160 memcpy(mp, p, len);
161 mp += len;
162 mlen += len;
166 * If we're running an ex command that the user didn't enter, display
167 * the file name and line number prefix.
169 if ((mt == M_ERR || mt == M_SYSERR) &&
170 sp != NULL && wp != NULL && wp->if_name != NULL) {
171 for (p = wp->if_name; *p != '\0'; ++p) {
172 len = snprintf(mp, REM, "%s", KEY_NAME(sp, *p));
173 mp += len;
174 if ((mlen += len) > blen)
175 goto retry;
177 len = snprintf(mp, REM, ", %d: ", wp->if_lno);
178 mp += len;
179 if ((mlen += len) > blen)
180 goto retry;
183 /* If nothing to format, we're done. */
184 if (fmt == NULL)
185 goto nofmt;
186 fmt = msg_cat(sp, fmt, NULL);
188 #ifndef NL_ARGMAX
190 * Nvi should run on machines that don't support the numbered argument
191 * specifications (%[digit]*$). We do this by reformatting the string
192 * so that we can hand it to vsprintf(3) and it will use the arguments
193 * in the right order. When vsprintf returns, we put the string back
194 * into the right order. It's undefined, according to SVID III, to mix
195 * numbered argument specifications with the standard style arguments,
196 * so this should be safe.
198 * In addition, we also need a character that is known to not occur in
199 * any vi message, for separating the parts of the string. As callers
200 * of msgq are responsible for making sure that all the non-printable
201 * characters are formatted for printing before calling msgq, we use a
202 * random non-printable character selected at terminal initialization
203 * time. This code isn't fast by any means, but as messages should be
204 * relatively short and normally have only a few arguments, it won't be
205 * too bad. Regardless, nobody has come up with any other solution.
207 * The result of this loop is an array of pointers into the message
208 * string, with associated lengths and argument numbers. The array
209 * is in the "correct" order, and the arg field contains the argument
210 * order.
212 for (p = fmt, soff = 0; soff < __NL_ARGMAX;) {
213 for (t = p; *p != '\0' && *p != '%'; ++p);
214 if (*p == '\0')
215 break;
216 ++p;
217 if (!isdigit(*p)) {
218 if (*p == '%')
219 ++p;
220 continue;
222 for (u = p; *++p != '\0' && isdigit(*p););
223 if (*p != '$')
224 continue;
226 /* Up to, and including the % character. */
227 str[soff].str = t;
228 str[soff].prefix = u - t;
230 /* Up to, and including the $ character. */
231 str[soff].arg = atoi(u);
232 str[soff].skip = (p - u) + 1;
233 if (str[soff].arg >= __NL_ARGMAX)
234 goto ret;
236 /* Up to, and including the conversion character. */
237 for (u = p; (ch = *++p) != '\0';)
238 if (isalpha(ch) &&
239 strchr("diouxXfeEgGcspn", ch) != NULL)
240 break;
241 str[soff].suffix = p - u;
242 if (ch != '\0')
243 ++p;
244 ++soff;
247 /* If no magic strings, we're done. */
248 if (soff == 0)
249 goto format;
251 /* Get space for the reordered strings. */
252 if ((rbp = malloc(nlen)) == NULL)
253 goto ret;
254 s_rbp = rbp;
257 * Reorder the strings into the message string based on argument
258 * order.
260 * !!!
261 * We ignore arguments that are out of order, i.e. if we don't find
262 * an argument, we continue. Assume (almost certainly incorrectly)
263 * that whoever created the string knew what they were doing.
265 * !!!
266 * Brute force "sort", but since we don't expect more than one or two
267 * arguments in a string, the setup cost of a fast sort will be more
268 * expensive than the loop.
270 for (cnt1 = 1; cnt1 <= soff; ++cnt1)
271 for (cnt2 = 0; cnt2 < soff; ++cnt2)
272 if (cnt1 == str[cnt2].arg) {
273 memmove(s_rbp, str[cnt2].str, str[cnt2].prefix);
274 memmove(s_rbp + str[cnt2].prefix,
275 str[cnt2].str + str[cnt2].prefix +
276 str[cnt2].skip, str[cnt2].suffix);
277 s_rbp += str[cnt2].prefix + str[cnt2].suffix;
278 *s_rbp++ =
279 gp == NULL ? DEFAULT_NOPRINT : gp->noprint;
280 break;
282 *s_rbp = '\0';
283 fmt = rbp;
284 #endif
286 #ifndef NL_ARGMAX
287 format: /* Format the arguments into the string. */
288 #endif
289 #ifdef __STDC__
290 va_start(ap, fmt);
291 #else
292 va_start(ap);
293 #endif
294 len = vsnprintf(mp, REM, fmt, ap);
295 va_end(ap);
296 if (len >= nlen)
297 goto retry;
299 #ifndef NL_ARGMAX
300 if (soff == 0)
301 goto nofmt;
304 * Go through the resulting string, and, for each separator character
305 * separated string, enter its new starting position and length in the
306 * array.
308 for (p = t = mp, cnt1 = 1,
309 ch = gp == NULL ? DEFAULT_NOPRINT : gp->noprint; *p != '\0'; ++p)
310 if (*p == ch) {
311 for (cnt2 = 0; cnt2 < soff; ++cnt2)
312 if (str[cnt2].arg == cnt1)
313 break;
314 str[cnt2].str = t;
315 str[cnt2].prefix = p - t;
316 t = p + 1;
317 ++cnt1;
321 * Reorder the strings once again, putting them back into the
322 * message buffer.
324 * !!!
325 * Note, the length of the message gets decremented once for
326 * each substring, when we discard the separator character.
328 for (s_rbp = rbp, cnt1 = 0; cnt1 < soff; ++cnt1) {
329 memmove(rbp, str[cnt1].str, str[cnt1].prefix);
330 rbp += str[cnt1].prefix;
331 --len;
333 memmove(mp, s_rbp, rbp - s_rbp);
335 /* Free the reordered string memory. */
336 free(s_rbp);
337 #endif
339 nofmt: mp += len;
340 if ((mlen += len) > blen)
341 goto retry;
342 if (mt == M_SYSERR) {
343 len = snprintf(mp, REM, ": %s", strerror(errno));
344 mp += len;
345 if ((mlen += len) > blen)
346 goto retry;
347 mt = M_ERR;
349 if (mt == M_DBERR) {
350 len = snprintf(mp, REM, ": %s", db_strerror(sp->db_error));
351 mp += len;
352 if ((mlen += len) > blen)
353 goto retry;
354 mt = M_ERR;
357 /* Add trailing newline. */
358 if ((mlen += 1) > blen)
359 goto retry;
360 *mp = '\n';
362 if (sp != NULL && sp->ep != NULL)
363 (void)ex_fflush(sp);
364 if (wp != NULL)
365 wp->scr_msg(sp, mt, bp, mlen);
366 else
367 (void)fprintf(stderr, "%.*s", (int)mlen, bp);
369 /* Cleanup. */
370 #ifndef NL_ARGMAX
371 ret:
372 #endif
373 FREE_SPACE(sp, bp, blen);
374 alloc_err:
375 reenter = 0;
379 * msgq_str --
380 * Display a message with an embedded string.
382 * PUBLIC: void msgq_wstr __P((SCR *, mtype_t, const CHAR_T *, const char *));
384 void
385 msgq_wstr(SCR *sp, mtype_t mtype, const CHAR_T *str, const char *fmt)
387 size_t nlen;
388 const char *nstr;
390 if (str == NULL) {
391 msgq(sp, mtype, fmt);
392 return;
394 INT2CHAR(sp, str, STRLEN(str) + 1, nstr, nlen);
395 msgq_str(sp, mtype, nstr, fmt);
399 * msgq_str --
400 * Display a message with an embedded string.
402 * PUBLIC: void msgq_str __P((SCR *, mtype_t, char *, char *));
404 void
405 msgq_str(SCR *sp, mtype_t mtype, const char *str, const char *fmt)
407 int nf, sv_errno;
408 char *p;
410 if (str == NULL) {
411 msgq(sp, mtype, fmt);
412 return;
415 sv_errno = errno;
416 p = msg_print(sp, str, &nf);
417 errno = sv_errno;
418 msgq(sp, mtype, fmt, p);
419 if (nf)
420 FREE_SPACE(sp, p, 0);
424 * mod_rpt --
425 * Report on the lines that changed.
427 * !!!
428 * Historic vi documentation (USD:15-8) claimed that "The editor will also
429 * always tell you when a change you make affects text which you cannot see."
430 * This wasn't true -- edit a large file and do "100d|1". We don't implement
431 * this semantic since it requires tracking each line that changes during a
432 * command instead of just keeping count.
434 * Line counts weren't right in historic vi, either. For example, given the
435 * file:
436 * abc
437 * def
438 * the command 2d}, from the 'b' would report that two lines were deleted,
439 * not one.
441 * PUBLIC: void mod_rpt __P((SCR *));
443 void
444 mod_rpt(SCR *sp)
446 static const char * const action[] = {
447 "293|added",
448 "294|changed",
449 "295|deleted",
450 "296|joined",
451 "297|moved",
452 "298|shifted",
453 "299|yanked",
455 static const char * const lines[] = {
456 "300|line",
457 "301|lines",
459 db_recno_t total;
460 u_long rptval;
461 int first;
462 size_t cnt, blen, len, tlen;
463 const char *t;
464 const char * const *ap;
465 char *bp, *p;
467 /* Change reports are turned off in batch mode. */
468 if (F_ISSET(sp, SC_EX_SILENT))
469 return;
471 /* Reset changing line number. */
472 sp->rptlchange = OOBLNO;
475 * Don't build a message if not enough changed.
477 * !!!
478 * And now, a vi clone test. Historically, vi reported if the number
479 * of changed lines was > than the value, not >=, unless it was a yank
480 * command, which used >=. No lie. Furthermore, an action was never
481 * reported for a single line action. This is consistent for actions
482 * other than yank, but yank didn't report single line actions even if
483 * the report edit option was set to 1. In addition, setting report to
484 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
485 * unknown reason (this bug was fixed in System III/V at some point).
486 * I got complaints, so nvi conforms to System III/V historic practice
487 * except that we report a yank of 1 line if report is set to 1.
489 #define ARSIZE(a) sizeof(a) / sizeof (*a)
490 #define MAXNUM 25
491 rptval = O_VAL(sp, O_REPORT);
492 for (cnt = 0, total = 0; cnt < ARSIZE(action); ++cnt)
493 total += sp->rptlines[cnt];
494 if (total == 0)
495 return;
496 if (total <= rptval && sp->rptlines[L_YANKED] < rptval) {
497 for (cnt = 0; cnt < ARSIZE(action); ++cnt)
498 sp->rptlines[cnt] = 0;
499 return;
502 /* Build and display the message. */
503 GET_SPACE_GOTOC(sp, bp, blen, sizeof(action) * MAXNUM + 1);
504 for (p = bp, first = 1, tlen = 0,
505 ap = action, cnt = 0; cnt < ARSIZE(action); ++ap, ++cnt)
506 if (sp->rptlines[cnt] != 0) {
507 if (first)
508 first = 0;
509 else {
510 *p++ = ';';
511 *p++ = ' ';
512 tlen += 2;
514 len = snprintf(p, MAXNUM, "%lu ",
515 (unsigned long) sp->rptlines[cnt]);
516 p += len;
517 tlen += len;
518 t = msg_cat(sp,
519 lines[sp->rptlines[cnt] == 1 ? 0 : 1], &len);
520 memcpy(p, t, len);
521 p += len;
522 tlen += len;
523 *p++ = ' ';
524 ++tlen;
525 t = msg_cat(sp, *ap, &len);
526 memcpy(p, t, len);
527 p += len;
528 tlen += len;
529 sp->rptlines[cnt] = 0;
532 /* Add trailing newline. */
533 *p = '\n';
534 ++tlen;
536 (void)ex_fflush(sp);
537 sp->wp->scr_msg(sp, M_INFO, bp, tlen);
539 FREE_SPACE(sp, bp, blen);
540 alloc_err:
541 return;
543 #undef ARSIZE
544 #undef MAXNUM
548 * msgq_status --
549 * Report on the file's status.
551 * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
553 void
554 msgq_status(SCR *sp, db_recno_t lno, u_int flags)
556 db_recno_t last;
557 size_t blen, len;
558 int cnt, needsep;
559 const char *t;
560 char **ap, *bp, *np, *p, *s;
562 /* Get sufficient memory. */
563 len = strlen(sp->frp->name);
564 GET_SPACE_GOTOC(sp, bp, blen, len * MAX_CHARACTER_COLUMNS + 128);
565 p = bp;
567 /* Copy in the filename. */
568 for (p = bp, t = sp->frp->name; *t != '\0'; ++t) {
569 len = KEY_LEN(sp, *t);
570 memcpy(p, KEY_NAME(sp, *t), len);
571 p += len;
573 np = p;
574 *p++ = ':';
575 *p++ = ' ';
577 /* Copy in the argument count. */
578 if (F_ISSET(sp, SC_STATUS_CNT) && sp->argv != NULL) {
579 for (cnt = 0, ap = sp->argv; *ap != NULL; ++ap, ++cnt);
580 if (cnt > 1) {
581 (void)sprintf(p,
582 msg_cat(sp, "317|%d files to edit", NULL), cnt);
583 p += strlen(p);
584 *p++ = ':';
585 *p++ = ' ';
587 F_CLR(sp, SC_STATUS_CNT);
591 * See nvi/exf.c:file_init() for a description of how and when the
592 * read-only bit is set.
594 * !!!
595 * The historic display for "name changed" was "[Not edited]".
597 needsep = 0;
598 if (F_ISSET(sp->frp, FR_NEWFILE)) {
599 F_CLR(sp->frp, FR_NEWFILE);
600 t = msg_cat(sp, "021|new file", &len);
601 memcpy(p, t, len);
602 p += len;
603 needsep = 1;
604 } else {
605 if (F_ISSET(sp->frp, FR_NAMECHANGE)) {
606 t = msg_cat(sp, "022|name changed", &len);
607 memcpy(p, t, len);
608 p += len;
609 needsep = 1;
611 if (needsep) {
612 *p++ = ',';
613 *p++ = ' ';
615 if (F_ISSET(sp->ep, F_MODIFIED))
616 t = msg_cat(sp, "023|modified", &len);
617 else
618 t = msg_cat(sp, "024|unmodified", &len);
619 memcpy(p, t, len);
620 p += len;
621 needsep = 1;
623 if (F_ISSET(sp->frp, FR_UNLOCKED)) {
624 if (needsep) {
625 *p++ = ',';
626 *p++ = ' ';
628 t = msg_cat(sp, "025|UNLOCKED", &len);
629 memcpy(p, t, len);
630 p += len;
631 needsep = 1;
633 if (O_ISSET(sp, O_READONLY)) {
634 if (needsep) {
635 *p++ = ',';
636 *p++ = ' ';
638 t = msg_cat(sp, "026|readonly", &len);
639 memcpy(p, t, len);
640 p += len;
641 needsep = 1;
643 if (needsep) {
644 *p++ = ':';
645 *p++ = ' ';
647 if (LF_ISSET(MSTAT_SHOWLAST)) {
648 if (db_last(sp, &last))
649 return;
650 if (last == 0) {
651 t = msg_cat(sp, "028|empty file", &len);
652 memcpy(p, t, len);
653 p += len;
654 } else {
655 t = msg_cat(sp, "027|line %lu of %lu [%ld%%]", &len);
656 (void)sprintf(p, t, lno, last, (lno * 100) / last);
657 p += strlen(p);
659 } else {
660 t = msg_cat(sp, "029|line %lu", &len);
661 (void)sprintf(p, t, lno);
662 p += strlen(p);
664 #ifdef DEBUG
665 (void)sprintf(p, " (pid %lu)", (u_long)getpid());
666 p += strlen(p);
667 #endif
668 *p++ = '\n';
669 len = p - bp;
672 * There's a nasty problem with long path names. Cscope and tags files
673 * can result in long paths and vi will request a continuation key from
674 * the user as soon as it starts the screen. Unfortunately, the user
675 * has already typed ahead, and chaos results. If we assume that the
676 * characters in the filenames and informational messages only take a
677 * single screen column each, we can trim the filename.
679 * XXX
680 * Status lines get put up at fairly awkward times. For example, when
681 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
682 * split screen, we have to repaint the status lines for all the screens
683 * below the top screen. We don't want users having to enter continue
684 * characters for those screens. Make it really hard to screw this up.
686 s = bp;
687 if (LF_ISSET(MSTAT_TRUNCATE) && len > sp->cols) {
688 for (; s < np && (*s != '/' || (size_t)(p - s) > sp->cols - 3); ++s);
689 if (s == np) {
690 s = p - (sp->cols - 5);
691 *--s = ' ';
693 *--s = '.';
694 *--s = '.';
695 *--s = '.';
696 len = p - s;
699 /* Flush any waiting ex messages. */
700 (void)ex_fflush(sp);
702 sp->wp->scr_msg(sp, M_INFO, s, len);
704 FREE_SPACE(sp, bp, blen);
705 alloc_err:
706 return;
710 * msg_open --
711 * Open the message catalogs.
713 * PUBLIC: int msg_open __P((SCR *, const char *));
716 msg_open(SCR *sp, const char *file)
719 * !!!
720 * Assume that the first file opened is the system default, and that
721 * all subsequent ones user defined. Only display error messages
722 * if we can't open the user defined ones -- it's useful to know if
723 * the system one wasn't there, but if nvi is being shipped with an
724 * installed system, the file will be there, if it's not, then the
725 * message will be repeated every time nvi is started up.
727 static int first = 1;
728 DB *db;
729 DBT data, key;
730 db_recno_t msgno;
731 char buf[MAXPATHLEN];
732 const char *p, *t;
734 if ((p = strrchr(file, '/')) != NULL && p[1] == '\0' &&
735 (((t = getenv("LC_MESSAGES")) != NULL && t[0] != '\0') ||
736 ((t = getenv("LANG")) != NULL && t[0] != '\0'))) {
737 (void)snprintf(buf, sizeof(buf), "%s%s", file, t);
738 p = buf;
739 } else
740 p = file;
741 if ((sp->db_error = db_create(&db, 0, 0)) != 0 ||
742 (sp->db_error = db->set_re_source(db, p)) != 0 ||
743 (sp->db_error = db_open(db, NULL, DB_RECNO, 0, 0)) != 0) {
744 if (first) {
745 first = 0;
746 return (1);
748 msgq_str(sp, M_DBERR, p, "%s");
749 return (1);
753 * Test record 1 for the magic string. The msgq call is here so
754 * the message catalog build finds it.
756 #define VMC "VI_MESSAGE_CATALOG"
757 memset(&key, 0, sizeof(key));
758 key.data = &msgno;
759 key.size = sizeof(db_recno_t);
760 memset(&data, 0, sizeof(data));
761 msgno = 1;
762 if ((sp->db_error = db->get(db, NULL, &key, &data, 0)) != 0 ||
763 data.size != sizeof(VMC) - 1 ||
764 memcmp(data.data, VMC, sizeof(VMC) - 1)) {
765 (void)db->close(db, DB_NOSYNC);
766 if (first) {
767 first = 0;
768 return (1);
770 msgq_str(sp, M_DBERR, p,
771 "030|The file %s is not a message catalog");
772 return (1);
774 first = 0;
776 if (sp->gp->msg != NULL)
777 (void)sp->gp->msg->close(sp->gp->msg, DB_NOSYNC);
778 sp->gp->msg = db;
779 return (0);
783 * msg_close --
784 * Close the message catalogs.
786 * PUBLIC: void msg_close __P((GS *));
788 void
789 msg_close(GS *gp)
791 if (gp->msg != NULL) {
792 (void)gp->msg->close(gp->msg, 0);
793 gp->msg = NULL;
798 * msg_cont --
799 * Return common continuation messages.
801 * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
803 const char *
804 msg_cmsg(SCR *sp, cmsg_t which, size_t *lenp)
806 switch (which) {
807 case CMSG_CONF:
808 return (msg_cat(sp, "268|confirm? [ynq]", lenp));
809 case CMSG_CONT:
810 return (msg_cat(sp, "269|Press any key to continue: ", lenp));
811 case CMSG_CONT_EX:
812 return (msg_cat(sp,
813 "270|Press any key to continue [: to enter more ex commands]: ",
814 lenp));
815 case CMSG_CONT_R:
816 return (msg_cat(sp, "161|Press Enter to continue: ", lenp));
817 case CMSG_CONT_S:
818 return (msg_cat(sp, "275| cont?", lenp));
819 case CMSG_CONT_Q:
820 return (msg_cat(sp,
821 "271|Press any key to continue [q to quit]: ", lenp));
822 default:
823 abort();
825 /* NOTREACHED */
829 * msg_cat --
830 * Return a single message from the catalog, plus its length.
832 * !!!
833 * Only a single catalog message can be accessed at a time, if multiple
834 * ones are needed, they must be copied into local memory.
836 * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
838 const char *
839 msg_cat(SCR *sp, const char *str, size_t *lenp)
841 GS *gp;
842 DBT data, key;
843 db_recno_t msgno;
846 * If it's not a catalog message, i.e. has doesn't have a leading
847 * number and '|' symbol, we're done.
849 if (isdigit((unsigned char)str[0]) &&
850 isdigit((unsigned char)str[1]) && isdigit((unsigned char)str[2]) &&
851 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(SCR *sp, const char *s, int *needfree)
891 size_t blen, nlen;
892 const char *cp;
893 char *bp, *ep, *p;
894 unsigned char *t;
896 *needfree = 0;
898 for (cp = s; *cp != '\0'; ++cp)
899 if (!isprint((unsigned char)*cp))
900 break;
901 if (*cp == '\0')
902 return ((char *)__UNCONST(s)); /* SAFE: needfree set to 0. */
904 nlen = 0;
905 if (0) {
906 retry: if (sp == NULL)
907 free(bp);
908 else
909 FREE_SPACE(sp, bp, blen);
910 *needfree = 0;
912 nlen += 256;
913 if (sp == NULL) {
914 if ((bp = malloc(nlen)) == NULL)
915 goto alloc_err;
916 } else
917 GET_SPACE_GOTOC(sp, bp, blen, nlen);
918 if (0) {
919 alloc_err: return __UNCONST("");
921 *needfree = 1;
923 for (p = bp, ep = (bp + blen) - 1, cp = s; *cp != '\0' && p < ep; ++cp)
924 for (t = KEY_NAME(sp, *cp); *t != '\0' && p < ep; *p++ = *t++);
925 if (p == ep)
926 goto retry;
927 *p = '\0';
928 return (bp);