guard against Solaris preprocessor polution
[nvi.git] / common / api.c
blob78263a0d8c79da08f2aad093d5f3a0c04292c919
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
6 * Copyright (c) 1995
7 * George V. Neville-Neil. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "$Id: api.c,v 8.35 2001/04/25 21:27:32 skimo Exp $ (Berkeley) $Date: 2001/04/25 21:27:32 $";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <termios.h>
28 #include <unistd.h>
30 #include "../common/common.h"
31 #include "../ex/tag.h"
33 extern GS *__global_list; /* XXX */
36 * api_fscreen --
37 * Return a pointer to the screen specified by the screen id
38 * or a file name.
40 * PUBLIC: SCR *api_fscreen __P((int, char *));
42 SCR *
43 api_fscreen(id, name)
44 int id;
45 char *name;
47 GS *gp;
48 SCR *tsp;
49 WIN *wp;
51 gp = __global_list;
53 /* Search the displayed lists. */
54 for (wp = gp->dq.cqh_first;
55 wp != (void *)&gp->dq; wp = wp->q.cqe_next)
56 for (tsp = wp->scrq.cqh_first;
57 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
58 if (name == NULL) {
59 if (id == tsp->id)
60 return (tsp);
61 } else if (!strcmp(name, tsp->frp->name))
62 return (tsp);
64 /* Search the hidden list. */
65 for (tsp = gp->hq.cqh_first;
66 tsp != (void *)&gp->hq; tsp = tsp->q.cqe_next)
67 if (name == NULL) {
68 if (id == tsp->id)
69 return (tsp);
70 } else if (!strcmp(name, tsp->frp->name))
71 return (tsp);
72 return (NULL);
76 * api_aline --
77 * Append a line.
79 * PUBLIC: int api_aline __P((SCR *, db_recno_t, char *, size_t));
81 int
82 api_aline(sp, lno, line, len)
83 SCR *sp;
84 db_recno_t lno;
85 char *line;
86 size_t len;
88 size_t wblen;
89 CHAR_T *wbp;
91 CHAR2INT(sp, line, len, wbp, wblen);
93 return (db_append(sp, 1, lno, wbp, wblen));
97 * api_extend --
98 * Extend file.
100 * PUBLIC: int api_extend __P((SCR *, db_recno_t));
102 int
103 api_extend(sp, lno)
104 SCR *sp;
105 db_recno_t lno;
107 db_recno_t lastlno;
108 if (db_last(sp, &lastlno))
109 return 1;
110 while(lastlno < lno)
111 if (db_append(sp, 1, lastlno++, NULL, 0))
112 return 1;
113 return 0;
117 * api_dline --
118 * Delete a line.
120 * PUBLIC: int api_dline __P((SCR *, db_recno_t));
123 api_dline(sp, lno)
124 SCR *sp;
125 db_recno_t lno;
127 if (db_delete(sp, lno))
128 return 1;
129 /* change current line if deleted line is that one
130 * or one berfore that
132 if (sp->lno >= lno && sp->lno > 1)
133 sp->lno--;
134 return 0;
138 * api_gline --
139 * Get a line.
141 * PUBLIC: int api_gline __P((SCR *, db_recno_t, CHAR_T **, size_t *));
144 api_gline(sp, lno, linepp, lenp)
145 SCR *sp;
146 db_recno_t lno;
147 CHAR_T **linepp;
148 size_t *lenp;
150 int isempty;
152 if (db_eget(sp, lno, linepp, lenp, &isempty)) {
153 if (isempty)
154 msgq(sp, M_ERR, "209|The file is empty");
155 return (1);
157 return (0);
161 * api_iline --
162 * Insert a line.
164 * PUBLIC: int api_iline __P((SCR *, db_recno_t, CHAR_T *, size_t));
167 api_iline(sp, lno, line, len)
168 SCR *sp;
169 db_recno_t lno;
170 CHAR_T *line;
171 size_t len;
173 return (db_insert(sp, lno, line, len));
177 * api_lline --
178 * Return the line number of the last line in the file.
180 * PUBLIC: int api_lline __P((SCR *, db_recno_t *));
183 api_lline(sp, lnop)
184 SCR *sp;
185 db_recno_t *lnop;
187 return (db_last(sp, lnop));
191 * api_sline --
192 * Set a line.
194 * PUBLIC: int api_sline __P((SCR *, db_recno_t, CHAR_T *, size_t));
197 api_sline(sp, lno, line, len)
198 SCR *sp;
199 db_recno_t lno;
200 CHAR_T *line;
201 size_t len;
203 return (db_set(sp, lno, line, len));
207 * api_getmark --
208 * Get the mark.
210 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
213 api_getmark(sp, markname, mp)
214 SCR *sp;
215 int markname;
216 MARK *mp;
218 return (mark_get(sp, (ARG_CHAR_T)markname, mp, M_ERR));
222 * api_setmark --
223 * Set the mark.
225 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
228 api_setmark(sp, markname, mp)
229 SCR *sp;
230 int markname;
231 MARK *mp;
233 return (mark_set(sp, (ARG_CHAR_T)markname, mp, 1));
237 * api_nextmark --
238 * Return the first mark if next not set, otherwise return the
239 * subsequent mark.
241 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
244 api_nextmark(sp, next, namep)
245 SCR *sp;
246 int next;
247 char *namep;
249 LMARK *mp;
251 mp = sp->ep->marks.lh_first;
252 if (next)
253 for (; mp != NULL; mp = mp->q.le_next)
254 if (mp->name == *namep) {
255 mp = mp->q.le_next;
256 break;
258 if (mp == NULL)
259 return (1);
260 *namep = mp->name;
261 return (0);
265 * api_getcursor --
266 * Get the cursor.
268 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
271 api_getcursor(sp, mp)
272 SCR *sp;
273 MARK *mp;
275 mp->lno = sp->lno;
276 mp->cno = sp->cno;
277 return (0);
281 * api_setcursor --
282 * Set the cursor.
284 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
287 api_setcursor(sp, mp)
288 SCR *sp;
289 MARK *mp;
291 size_t len;
293 if (db_get(sp, mp->lno, DBG_FATAL, NULL, &len))
294 return (1);
295 if (mp->cno < 0 || mp->cno > len) {
296 msgq(sp, M_ERR, "Cursor set to nonexistent column");
297 return (1);
300 /* Set the cursor. */
301 sp->lno = mp->lno;
302 sp->cno = mp->cno;
303 return (0);
307 * api_emessage --
308 * Print an error message.
310 * PUBLIC: void api_emessage __P((SCR *, char *));
312 void
313 api_emessage(sp, text)
314 SCR *sp;
315 char *text;
317 msgq(sp, M_ERR, "%s", text);
321 * api_imessage --
322 * Print an informational message.
324 * PUBLIC: void api_imessage __P((SCR *, char *));
326 void
327 api_imessage(sp, text)
328 SCR *sp;
329 char *text;
331 msgq(sp, M_INFO, "%s", text);
335 * api_edit
336 * Create a new screen and return its id
337 * or edit a new file in the current screen.
339 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
342 api_edit(sp, file, spp, newscreen)
343 SCR *sp;
344 char *file;
345 SCR **spp;
346 int newscreen;
348 EXCMD cmd;
349 size_t wlen;
350 CHAR_T *wp;
352 if (file) {
353 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
354 CHAR2INT(sp, file, strlen(file) + 1, wp, wlen);
355 argv_exp0(sp, &cmd, wp, wlen - 1 /* terminating 0 */);
356 } else
357 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
358 if (newscreen)
359 cmd.flags |= E_NEWSCREEN; /* XXX */
360 if (cmd.cmd->fn(sp, &cmd))
361 return (1);
362 *spp = sp->nextdisp;
363 return (0);
367 * api_escreen
368 * End a screen.
370 * PUBLIC: int api_escreen __P((SCR *));
373 api_escreen(sp)
374 SCR *sp;
376 EXCMD cmd;
379 * XXX
380 * If the interpreter exits anything other than the current
381 * screen, vi isn't going to update everything correctly.
383 ex_cinit(sp, &cmd, C_QUIT, 0, OOBLNO, OOBLNO, 0);
384 return (cmd.cmd->fn(sp, &cmd));
388 * api_swscreen --
389 * Switch to a new screen.
391 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
394 api_swscreen(sp, new)
395 SCR *sp, *new;
398 * XXX
399 * If the interpreter switches from anything other than the
400 * current screen, vi isn't going to update everything correctly.
402 sp->nextdisp = new;
403 F_SET(sp, SC_SSWITCH);
405 return (0);
409 * api_map --
410 * Map a key.
412 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
415 api_map(sp, name, map, len)
416 SCR *sp;
417 char *name, *map;
418 size_t len;
420 EXCMD cmd;
421 size_t wlen;
422 CHAR_T *wp;
424 ex_cinit(sp, &cmd, C_MAP, 0, OOBLNO, OOBLNO, 0);
425 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
426 argv_exp0(sp, &cmd, wp, wlen - 1);
427 CHAR2INT(sp, map, len, wp, wlen);
428 argv_exp0(sp, &cmd, wp, wlen);
429 return (cmd.cmd->fn(sp, &cmd));
433 * api_unmap --
434 * Unmap a key.
436 * PUBLIC: int api_unmap __P((SCR *, char *));
438 int
439 api_unmap(sp, name)
440 SCR *sp;
441 char *name;
443 EXCMD cmd;
444 size_t wlen;
445 CHAR_T *wp;
447 ex_cinit(sp, &cmd, C_UNMAP, 0, OOBLNO, OOBLNO, 0);
448 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
449 argv_exp0(sp, &cmd, wp, wlen - 1);
450 return (cmd.cmd->fn(sp, &cmd));
454 * api_opts_get --
455 * Return a option value as a string, in allocated memory.
456 * If the option is of type boolean, boolvalue is (un)set
457 * according to the value; otherwise boolvalue is -1.
459 * PUBLIC: int api_opts_get __P((SCR *, char *, char **, int *));
462 api_opts_get(sp, name, value, boolvalue)
463 SCR *sp;
464 char *name, **value;
465 int *boolvalue;
467 OPTLIST const *op;
468 int offset;
470 if ((op = opts_search(name)) == NULL) {
471 opts_nomatch(sp, name);
472 return (1);
475 offset = op - optlist;
476 if (boolvalue != NULL)
477 *boolvalue = -1;
478 switch (op->type) {
479 case OPT_0BOOL:
480 case OPT_1BOOL:
481 MALLOC_RET(sp, *value, char *, strlen(op->name) + 2 + 1);
482 (void)sprintf(*value,
483 "%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
484 if (boolvalue != NULL)
485 *boolvalue = O_ISSET(sp, offset);
486 break;
487 case OPT_NUM:
488 MALLOC_RET(sp, *value, char *, 20);
489 (void)sprintf(*value, "%lu", (u_long)O_VAL(sp, offset));
490 break;
491 case OPT_STR:
492 if (O_STR(sp, offset) == NULL) {
493 MALLOC_RET(sp, *value, char *, 2);
494 value[0] = '\0';
495 } else {
496 MALLOC_RET(sp,
497 *value, char *, strlen(O_STR(sp, offset)) + 1);
498 (void)sprintf(*value, "%s", O_STR(sp, offset));
500 break;
502 return (0);
506 * api_opts_set --
507 * Set options.
509 * PUBLIC: int api_opts_set __P((SCR *, char *, char *, u_long, int));
512 api_opts_set(sp, name, str_value, num_value, bool_value)
513 SCR *sp;
514 char *name, *str_value;
515 u_long num_value;
516 int bool_value;
518 ARGS *ap[2], a, b;
519 OPTLIST const *op;
520 int rval;
521 size_t blen;
522 char *bp;
523 size_t wblen;
524 CHAR_T *wbp;
526 if ((op = opts_search(name)) == NULL) {
527 opts_nomatch(sp, name);
528 return (1);
531 switch (op->type) {
532 case OPT_0BOOL:
533 case OPT_1BOOL:
534 GET_SPACE_RET(sp, bp, blen, 64);
535 a.len = snprintf(bp, 64, "%s%s", bool_value ? "" : "no", name);
536 break;
537 case OPT_NUM:
538 GET_SPACE_RET(sp, bp, blen, 64);
539 a.len = snprintf(bp, 64, "%s=%lu", name, num_value);
540 break;
541 case OPT_STR:
542 GET_SPACE_RET(sp, bp, blen, 1024);
543 a.len = snprintf(bp, 1024, "%s=%s", name, str_value);
544 break;
547 CHAR2INT(sp, bp, a.len, wbp, wblen);
548 a.len = wblen;
549 wbp = v_wstrdup(sp, wbp, wblen);
551 a.bp = wbp;
552 b.len = 0;
553 b.bp = NULL;
554 ap[0] = &a;
555 ap[1] = &b;
556 rval = opts_set(sp, ap, NULL);
558 free(wbp);
559 FREE_SPACE(sp, bp, blen);
561 return (rval);
565 * api_run_str --
566 * Execute a string as an ex command.
568 * PUBLIC: int api_run_str __P((SCR *, char *));
570 int
571 api_run_str(sp, cmd)
572 SCR *sp;
573 char *cmd;
575 size_t wlen;
576 CHAR_T *wp;
578 CHAR2INT(sp, cmd, strlen(cmd)+1, wp, wlen);
579 return (ex_run_str(sp, NULL, wp, wlen - 1, 0, 0));
583 * PUBLIC: TAGQ * api_tagq_new __P((SCR*, char*));
585 TAGQ *
586 api_tagq_new(sp, tag)
587 SCR *sp;
588 char *tag;
590 TAGQ *tqp;
591 size_t len;
593 /* Allocate and initialize the tag queue structure. */
594 len = strlen(tag);
595 CALLOC_GOTO(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + len + 1);
596 CIRCLEQ_INIT(&tqp->tagq);
597 tqp->tag = tqp->buf;
598 memcpy(tqp->tag, tag, (tqp->tlen = len) + 1);
600 return tqp;
602 alloc_err:
603 return (NULL);
607 * PUBLIC: void api_tagq_add __P((SCR*, TAGQ*, char*, char *, char *));
609 void
610 api_tagq_add(sp, tqp, filename, search, msg)
611 SCR *sp;
612 TAGQ *tqp;
613 char *filename, *search, *msg;
615 TAG *tp;
616 CHAR_T *wp;
617 size_t wlen;
618 size_t flen = strlen(filename);
619 size_t slen = strlen(search);
620 size_t mlen = strlen(msg);
622 CALLOC_GOTO(sp, tp, TAG *, 1,
623 sizeof(TAG) - 1 + flen + 1 +
624 (slen + 1 + mlen + 1) * sizeof(CHAR_T));
625 tp->fname = (char *)tp->buf;
626 memcpy(tp->fname, filename, flen + 1);
627 tp->fnlen = flen;
628 tp->search = (CHAR_T *)((char *)tp->fname + flen + 1);
629 CHAR2INT(sp, search, slen + 1, wp, wlen);
630 MEMCPYW(tp->search, wp, wlen);
631 tp->slen = slen;
632 tp->msg = tp->search + slen + 1;
633 CHAR2INT(sp, msg, mlen + 1, wp, wlen);
634 MEMCPYW(tp->msg, wp, wlen);
635 tp->mlen = mlen;
636 CIRCLEQ_INSERT_TAIL(&tqp->tagq, tp, q);
638 alloc_err:
639 return;
643 * PUBLIC: int api_tagq_push __P((SCR*, TAGQ**));
646 api_tagq_push(sp, tqpp)
647 SCR *sp;
648 TAGQ **tqpp;
650 FREF *frp;
651 db_recno_t lno;
652 size_t cno;
653 int istmp;
654 TAGQ *tqp;
656 tqp = *tqpp;
658 *tqpp = 0;
660 /* Check to see if we found anything. */
661 if (tqp->tagq.cqh_first == (void *)&tqp->tagq) {
662 free(tqp);
663 return 0;
666 tqp->current = tqp->tagq.cqh_first;
668 if (tagq_push(sp, tqp, 0, 0))
669 return 1;
671 return (0);
675 * PUBLIC: void api_tagq_free __P((SCR*, TAGQ*));
677 void
678 api_tagq_free(sp, tqp)
679 SCR *sp;
680 TAGQ *tqp;
682 if (tqp)
683 tagq_free(sp, tqp);