Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / ip / ip_funcs.c
blobc8d7ee1ff401b573b03cc7708ca425835ac39bdf
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 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: ip_funcs.c,v 8.23 2001/06/25 15:19:23 skimo Exp (Berkeley) Date: 2001/06/25 15:19:23";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "../common/common.h"
26 #include "../vi/vi.h"
27 #include "../ipc/ip.h"
28 #include "extern.h"
31 * ip_addstr --
32 * Add len bytes from the string at the cursor, advancing the cursor.
34 * PUBLIC: int ip_waddstr __P((SCR *, const CHAR_T *, size_t));
36 int
37 ip_waddstr(SCR *sp, const CHAR_T *str, size_t len)
39 IP_BUF ipb;
40 IP_PRIVATE *ipp;
41 int iv, rval;
43 ipp = IPP(sp);
45 ipb.code = SI_WADDSTR;
46 ipb.len1 = len * sizeof(CHAR_T);
47 ipb.str1 = (char *)str;
48 rval = vi_send(ipp->o_fd, "a", &ipb);
49 /* XXXX */
50 ipp->col += len;
52 return (rval);
56 * ip_addstr --
57 * Add len bytes from the string at the cursor, advancing the cursor.
59 * PUBLIC: int ip_addstr __P((SCR *, const char *, size_t));
61 int
62 ip_addstr(SCR *sp, const char *str, size_t len)
64 IP_BUF ipb;
65 IP_PRIVATE *ipp;
66 int iv, rval;
68 ipp = IPP(sp);
71 * If ex isn't in control, it's the last line of the screen and
72 * it's a split screen, use inverse video.
74 iv = 0;
75 if (!F_ISSET(sp, SC_SCR_EXWROTE) &&
76 ipp->row == LASTLINE(sp) && IS_SPLIT(sp)) {
77 iv = 1;
78 ip_attr(sp, SA_INVERSE, 1);
80 ipb.code = SI_ADDSTR;
81 ipb.len1 = len;
82 ipb.str1 = str;
83 rval = vi_send(ipp->o_fd, "a", &ipb);
84 /* XXXX */
85 ipp->col += len;
87 if (iv)
88 ip_attr(sp, SA_INVERSE, 0);
89 return (rval);
93 * ip_attr --
94 * Toggle a screen attribute on/off.
96 * PUBLIC: int ip_attr __P((SCR *, scr_attr_t, int));
98 int
99 ip_attr(SCR *sp, scr_attr_t attribute, int on)
101 IP_BUF ipb;
102 IP_PRIVATE *ipp = IPP(sp);
104 if (attribute == SA_ALTERNATE) {
105 if (on) F_SET(ipp, IP_ON_ALTERNATE);
106 else F_CLR(ipp, IP_ON_ALTERNATE);
109 ipb.code = SI_ATTRIBUTE;
110 ipb.val1 = attribute;
111 ipb.val2 = on;
113 return (vi_send(ipp->o_fd, "12", &ipb));
117 * ip_baud --
118 * Return the baud rate.
120 * PUBLIC: int ip_baud __P((SCR *, u_long *));
123 ip_baud(SCR *sp, u_long *ratep)
125 *ratep = 9600; /* XXX: Translation: fast. */
126 return (0);
130 * ip_bell --
131 * Ring the bell/flash the screen.
133 * PUBLIC: int ip_bell __P((SCR *));
136 ip_bell(SCR *sp)
138 IP_BUF ipb;
139 IP_PRIVATE *ipp = IPP(sp);
141 ipb.code = SI_BELL;
143 return (vi_send(ipp->o_fd, NULL, &ipb));
147 * ip_busy --
148 * Display a busy message.
150 * PUBLIC: void ip_busy __P((SCR *, const char *, busy_t));
152 void
153 ip_busy(SCR *sp, const char *str, busy_t bval)
155 IP_BUF ipb;
156 IP_PRIVATE *ipp = IPP(sp);
158 switch (bval) {
159 case BUSY_ON:
160 ipb.code = SI_BUSY_ON;
161 ipb.str1 = str;
162 ipb.len1 = strlen(str);
163 (void)vi_send(ipp->o_fd, "a", &ipb);
164 break;
165 case BUSY_OFF:
166 ipb.code = SI_BUSY_OFF;
167 (void)vi_send(ipp->o_fd, NULL, &ipb);
168 break;
169 case BUSY_UPDATE:
170 break;
172 return;
176 * ip_child --
177 * Prepare child.
179 * PUBLIC: int ip_child __P((SCR *));
182 ip_child(SCR *sp)
184 IP_PRIVATE *ipp = IPP(sp);
186 if (ipp->t_fd != -1) {
187 dup2(ipp->t_fd, 0);
188 dup2(ipp->t_fd, 1);
189 dup2(ipp->t_fd, 2);
190 close(ipp->t_fd);
192 return 0;
196 * ip_clrtoeol --
197 * Clear from the current cursor to the end of the line.
199 * PUBLIC: int ip_clrtoeol __P((SCR *));
202 ip_clrtoeol(SCR *sp)
204 IP_BUF ipb;
205 IP_PRIVATE *ipp = IPP(sp);
207 /* Temporary hack until we can pass screen pointers
208 * or name screens
210 if (IS_VSPLIT(sp)) {
211 size_t x, y, spcnt;
212 IP_PRIVATE *ipp;
213 int error;
215 ipp = IPP(sp);
216 y = ipp->row;
217 x = ipp->col;
218 error = 0;
219 for (spcnt = sp->cols - x;
220 spcnt > 0 && ! error; --spcnt)
221 error = ip_addstr(sp, " ", 1);
222 if (sp->coff == 0)
223 error |= ip_addstr(sp, "|", 1);
224 error |= ip_move(sp, y, x);
225 return error;
228 ipb.code = SI_CLRTOEOL;
230 return (vi_send(ipp->o_fd, NULL, &ipb));
234 * ip_cursor --
235 * Return the current cursor position.
237 * PUBLIC: int ip_cursor __P((SCR *, size_t *, size_t *));
240 ip_cursor(SCR *sp, size_t *yp, size_t *xp)
242 IP_PRIVATE *ipp;
244 ipp = IPP(sp);
245 *yp = ipp->row;
246 *xp = ipp->col;
247 return (0);
251 * ip_deleteln --
252 * Delete the current line, scrolling all lines below it.
254 * PUBLIC: int ip_deleteln __P((SCR *));
257 ip_deleteln(SCR *sp)
259 IP_BUF ipb;
260 IP_PRIVATE *ipp = IPP(sp);
263 * This clause is required because the curses screen uses reverse
264 * video to delimit split screens. If the screen does not do this,
265 * this code won't be necessary.
267 * If the bottom line was in reverse video, rewrite it in normal
268 * video before it's scrolled.
270 if (!F_ISSET(sp, SC_SCR_EXWROTE) && IS_SPLIT(sp)) {
271 ipb.code = SI_REWRITE;
272 ipb.val1 = RLNO(sp, LASTLINE(sp));
273 if (vi_send(ipp->o_fd, "1", &ipb))
274 return (1);
278 * The bottom line is expected to be blank after this operation,
279 * and other screens must support that semantic.
281 ipb.code = SI_DELETELN;
282 return (vi_send(ipp->o_fd, NULL, &ipb));
286 * ip_discard --
287 * Discard a screen.
289 * PUBLIC: int ip_discard __P((SCR *, SCR **));
292 ip_discard(SCR *discardp, SCR **acquirep)
294 return (0);
298 * ip_ex_adjust --
299 * Adjust the screen for ex.
301 * PUBLIC: int ip_ex_adjust __P((SCR *, exadj_t));
304 ip_ex_adjust(SCR *sp, exadj_t action)
306 abort();
307 /* NOTREACHED */
311 * ip_insertln --
312 * Push down the current line, discarding the bottom line.
314 * PUBLIC: int ip_insertln __P((SCR *));
317 ip_insertln(SCR *sp)
319 IP_BUF ipb;
320 IP_PRIVATE *ipp = IPP(sp);
322 ipb.code = SI_INSERTLN;
324 return (vi_send(ipp->o_fd, NULL, &ipb));
328 * ip_keyval --
329 * Return the value for a special key.
331 * PUBLIC: int ip_keyval __P((SCR *, scr_keyval_t, CHAR_T *, int *));
334 ip_keyval(SCR *sp, scr_keyval_t val, CHAR_T *chp, int *dnep)
337 * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
338 * VWERASE is a 4BSD extension.
340 switch (val) {
341 case KEY_VEOF:
342 *dnep = '\004'; /* ^D */
343 break;
344 case KEY_VERASE:
345 *dnep = '\b'; /* ^H */
346 break;
347 case KEY_VKILL:
348 *dnep = '\025'; /* ^U */
349 break;
350 #ifdef VWERASE
351 case KEY_VWERASE:
352 *dnep = '\027'; /* ^W */
353 break;
354 #endif
355 default:
356 *dnep = 1;
357 break;
359 return (0);
363 * ip_move --
364 * Move the cursor.
366 * PUBLIC: int ip_move __P((SCR *, size_t, size_t));
369 ip_move(SCR *sp, size_t lno, size_t cno)
371 IP_PRIVATE *ipp;
372 IP_BUF ipb;
374 ipp = IPP(sp);
375 ipp->row = lno;
376 ipp->col = cno;
378 ipb.code = SI_MOVE;
379 ipb.val1 = RLNO(sp, lno);
380 ipb.val2 = RCNO(sp, cno);
381 return (vi_send(ipp->o_fd, "12", &ipb));
385 * PUBLIC: void ip_msg __P((SCR *, mtype_t, char *, size_t));
387 void
388 ip_msg(SCR *sp, mtype_t mtype, char *line, size_t len)
390 IP_PRIVATE *ipp = IPP(sp);
392 if (F_ISSET(ipp, IP_ON_ALTERNATE))
393 vs_msg(sp, mtype, line, len);
394 else {
395 write(ipp->t_fd, line, len);
396 F_CLR(sp, SC_EX_WAIT_NO);
401 * ip_refresh --
402 * Refresh the screen.
404 * PUBLIC: int ip_refresh __P((SCR *, int));
407 ip_refresh(SCR *sp, int repaint)
409 IP_BUF ipb;
410 IP_PRIVATE *ipp;
411 db_recno_t total;
413 ipp = IPP(sp);
416 * If the scroll bar information has changed since we last sent
417 * it, resend it. Currently, we send three values:
419 * top The line number of the first line in the screen.
420 * num The number of lines visible on the screen.
421 * total The number of lines in the file.
423 * XXX
424 * This is a gross violation of layering... we're looking at data
425 * structures at which we have absolutely no business whatsoever
426 * looking...
428 ipb.val1 = HMAP->lno;
429 ipb.val2 = TMAP->lno - HMAP->lno;
430 if (sp->ep != NULL && sp->ep->db != NULL)
431 (void)db_last(sp, &total);
432 ipb.val3 = total == 0 ? 1 : total;
433 if (ipb.val1 != ipp->sb_top ||
434 ipb.val2 != ipp->sb_num || ipb.val3 != ipp->sb_total) {
435 ipb.code = SI_SCROLLBAR;
436 (void)vi_send(ipp->o_fd, "123", &ipb);
437 ipp->sb_top = ipb.val1;
438 ipp->sb_num = ipb.val2;
439 ipp->sb_total = ipb.val3;
442 /* Refresh/repaint the screen. */
443 ipb.code = repaint ? SI_REDRAW : SI_REFRESH;
444 return (vi_send(ipp->o_fd, NULL, &ipb));
448 * ip_rename --
449 * Rename the file.
451 * PUBLIC: int ip_rename __P((SCR *, char *, int));
454 ip_rename(SCR *sp, char *name, int on)
456 IP_BUF ipb;
457 IP_PRIVATE *ipp = IPP(sp);
459 ipb.code = SI_RENAME;
460 ipb.str1 = name;
461 ipb.len1 = name ? strlen(name) : 0;
462 return (vi_send(ipp->o_fd, "a", &ipb));
466 * ip_reply --
467 * Reply to a message.
469 * PUBLIC: int ip_reply __P((SCR *, int, char *));
472 ip_reply(SCR *sp, int status, char *msg)
474 IP_BUF ipb;
475 IP_PRIVATE *ipp = IPP(sp);
477 ipb.code = SI_REPLY;
478 ipb.val1 = status;
479 ipb.str1 = msg == NULL ? "" : msg;
480 ipb.len1 = strlen(ipb.str1);
481 return (vi_send(ipp->o_fd, "1a", &ipb));
485 * ip_split --
486 * Split a screen.
488 * PUBLIC: int ip_split __P((SCR *, SCR *));
491 ip_split(SCR *origp, SCR *newp)
493 return (0);
497 * ip_suspend --
498 * Suspend a screen.
500 * PUBLIC: int ip_suspend __P((SCR *, int *));
503 ip_suspend(SCR *sp, int *allowedp)
505 *allowedp = 0;
506 return (0);
510 * ip_usage --
511 * Print out the ip usage messages.
513 * PUBLIC: void ip_usage __P((void));
515 void
516 ip_usage(void)
518 #define USAGE "\
519 usage: vi [-eFlRrSv] [-c command] [-I ifd.ofd] [-t tag] [-w size] [file ...]\n"
520 (void)fprintf(stderr, "%s", USAGE);
521 #undef USAGE