1 /* $NetBSD: api.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * George V. Neville-Neil. All rights reserved.
10 * See the LICENSE file for redistribution information.
15 #include <sys/cdefs.h>
18 static const char sccsid
[] = "Id: api.c,v 8.40 2002/06/08 19:30:33 skimo Exp (Berkeley) Date: 2002/06/08 19:30:33 ";
21 __RCSID("$NetBSD: api.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
24 #include <sys/types.h>
25 #include <sys/queue.h>
28 #include <bitstring.h>
36 #include "../common/common.h"
37 #include "../ex/tag.h"
39 extern GS
*__global_list
; /* XXX */
43 * Return a pointer to the screen specified by the screen id
46 * PUBLIC: SCR *api_fscreen __P((int, char *));
49 api_fscreen(int id
, char *name
)
57 /* Search the displayed lists. */
58 TAILQ_FOREACH(wp
, &gp
->dq
, q
)
59 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
63 } else if (!strcmp(name
, tsp
->frp
->name
))
66 /* Search the hidden list. */
67 TAILQ_FOREACH (tsp
, &gp
->hq
, q
)
71 } else if (!strcmp(name
, tsp
->frp
->name
))
80 * PUBLIC: int api_aline __P((SCR *, db_recno_t, char *, size_t));
83 api_aline(SCR
*sp
, db_recno_t lno
, char *line
, size_t len
)
88 CHAR2INT(sp
, line
, len
, wbp
, wblen
);
90 return (db_append(sp
, 1, lno
, wbp
, wblen
));
97 * PUBLIC: int api_extend __P((SCR *, db_recno_t));
100 api_extend(SCR
*sp
, db_recno_t lno
)
103 if (db_last(sp
, &lastlno
))
106 if (db_append(sp
, 1, lastlno
++, NULL
, 0))
115 * PUBLIC: int api_dline __P((SCR *, db_recno_t));
118 api_dline(SCR
*sp
, db_recno_t lno
)
120 if (db_delete(sp
, lno
))
122 /* change current line if deleted line is that one
123 * or one berfore that
125 if (sp
->lno
>= lno
&& sp
->lno
> 1)
134 * PUBLIC: int api_gline __P((SCR *, db_recno_t, CHAR_T **, size_t *));
137 api_gline(SCR
*sp
, db_recno_t lno
, CHAR_T
**linepp
, size_t *lenp
)
141 if (db_eget(sp
, lno
, linepp
, lenp
, &isempty
)) {
143 msgq(sp
, M_ERR
, "209|The file is empty");
153 * PUBLIC: int api_iline __P((SCR *, db_recno_t, CHAR_T *, size_t));
156 api_iline(SCR
*sp
, db_recno_t lno
, CHAR_T
*line
, size_t len
)
158 return (db_insert(sp
, lno
, line
, len
));
163 * Return the line number of the last line in the file.
165 * PUBLIC: int api_lline __P((SCR *, db_recno_t *));
168 api_lline(SCR
*sp
, db_recno_t
*lnop
)
170 return (db_last(sp
, lnop
));
177 * PUBLIC: int api_sline __P((SCR *, db_recno_t, CHAR_T *, size_t));
180 api_sline(SCR
*sp
, db_recno_t lno
, CHAR_T
*line
, size_t len
)
182 return (db_set(sp
, lno
, line
, len
));
189 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
192 api_getmark(SCR
*sp
, int markname
, MARK
*mp
)
194 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
201 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
204 api_setmark(SCR
*sp
, int markname
, MARK
*mp
)
206 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
211 * Return the first mark if next not set, otherwise return the
214 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
217 api_nextmark(SCR
*sp
, int next
, char *namep
)
221 mp
= LIST_FIRST(&sp
->ep
->marks
);
223 for (; mp
!= NULL
; mp
= LIST_NEXT(mp
, q
))
224 if (mp
->name
== *namep
) {
225 mp
= LIST_NEXT(mp
, q
);
238 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
241 api_getcursor(SCR
*sp
, MARK
*mp
)
252 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
255 api_setcursor(SCR
*sp
, MARK
*mp
)
259 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
262 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
266 /* Set the cursor. */
274 * Print an error message.
276 * PUBLIC: void api_emessage __P((SCR *, char *));
279 api_emessage(SCR
*sp
, char *text
)
281 msgq(sp
, M_ERR
, "%s", text
);
286 * Print an informational message.
288 * PUBLIC: void api_imessage __P((SCR *, char *));
291 api_imessage(SCR
*sp
, char *text
)
293 msgq(sp
, M_INFO
, "%s", text
);
298 * Create a new screen and return its id
299 * or edit a new file in the current screen.
301 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
304 api_edit(SCR
*sp
, char *file
, SCR
**spp
, int newscreen
)
311 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
312 CHAR2INT(sp
, file
, strlen(file
) + 1, wp
, wlen
);
313 argv_exp0(sp
, &cmd
, wp
, wlen
- 1 /* terminating 0 */);
315 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
317 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
318 if (cmd
.cmd
->fn(sp
, &cmd
))
328 * PUBLIC: int api_escreen __P((SCR *));
337 * If the interpreter exits anything other than the current
338 * screen, vi isn't going to update everything correctly.
340 ex_cinit(sp
, &cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0);
341 return (cmd
.cmd
->fn(sp
, &cmd
));
346 * Switch to a new screen.
348 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
351 api_swscreen(SCR
*sp
, SCR
*new)
355 * If the interpreter switches from anything other than the
356 * current screen, vi isn't going to update everything correctly.
359 F_SET(sp
, SC_SSWITCH
);
368 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
371 api_map(SCR
*sp
, char *name
, char *map
, size_t len
)
377 ex_cinit(sp
, &cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0);
378 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
379 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
380 CHAR2INT(sp
, map
, len
, wp
, wlen
);
381 argv_exp0(sp
, &cmd
, wp
, wlen
);
382 return (cmd
.cmd
->fn(sp
, &cmd
));
389 * PUBLIC: int api_unmap __P((SCR *, char *));
392 api_unmap(SCR
*sp
, char *name
)
398 ex_cinit(sp
, &cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0);
399 CHAR2INT(sp
, name
, strlen(name
) + 1, wp
, wlen
);
400 argv_exp0(sp
, &cmd
, wp
, wlen
- 1);
401 return (cmd
.cmd
->fn(sp
, &cmd
));
406 * Return a option value as a string, in allocated memory.
407 * If the option is of type boolean, boolvalue is (un)set
408 * according to the value; otherwise boolvalue is -1.
410 * PUBLIC: int api_opts_get __P((SCR *, const CHAR_T *, char **, int *));
413 api_opts_get(SCR
*sp
, const CHAR_T
*name
, char **value
, int *boolvalue
)
418 if ((op
= opts_search(name
)) == NULL
) {
419 opts_nomatch(sp
, name
);
423 offset
= op
- optlist
;
424 if (boolvalue
!= NULL
)
429 MALLOC_RET(sp
, *value
, char *, STRLEN(op
->name
) + 2 + 1);
430 (void)sprintf(*value
,
431 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
432 if (boolvalue
!= NULL
)
433 *boolvalue
= O_ISSET(sp
, offset
);
436 MALLOC_RET(sp
, *value
, char *, 20);
437 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
440 if (O_STR(sp
, offset
) == NULL
) {
441 MALLOC_RET(sp
, *value
, char *, 2);
445 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
446 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
457 * PUBLIC: int api_opts_set __P((SCR *, const CHAR_T *, const char *, u_long, int));
460 api_opts_set(SCR
*sp
, const CHAR_T
*name
,
461 const char *str_value
, u_long num_value
, int bool_value
)
469 if ((op
= opts_search(name
)) == NULL
) {
470 opts_nomatch(sp
, name
);
477 GET_SPACE_RETW(sp
, bp
, blen
, 64);
478 a
.len
= SPRINTF(bp
, 64, L("%s"WS
), bool_value
? "" : "no", name
);
481 GET_SPACE_RETW(sp
, bp
, blen
, 64);
482 a
.len
= SPRINTF(bp
, 64, L(""WS
"=%lu"), name
, num_value
);
485 GET_SPACE_RETW(sp
, bp
, blen
, 1024);
486 a
.len
= SPRINTF(bp
, 1024, L(""WS
"=%s"), name
, str_value
);
498 rval
= opts_set(sp
, ap
, NULL
);
500 FREE_SPACEW(sp
, bp
, blen
);
507 * Execute a string as an ex command.
509 * PUBLIC: int api_run_str __P((SCR *, char *));
512 api_run_str(SCR
*sp
, char *cmd
)
517 CHAR2INT(sp
, cmd
, strlen(cmd
)+1, wp
, wlen
);
518 return (ex_run_str(sp
, NULL
, wp
, wlen
- 1, 0, 0));
522 * PUBLIC: TAGQ * api_tagq_new __P((SCR*, char*));
525 api_tagq_new(SCR
*sp
, char *tag
)
530 /* Allocate and initialize the tag queue structure. */
532 CALLOC_GOTO(sp
, tqp
, TAGQ
*, 1, sizeof(TAGQ
) + len
+ 1);
533 TAILQ_INIT(&tqp
->tagq
);
535 memcpy(tqp
->tag
, tag
, (tqp
->tlen
= len
) + 1);
544 * PUBLIC: void api_tagq_add __P((SCR*, TAGQ*, char*, char *, char *));
547 api_tagq_add(SCR
*sp
, TAGQ
*tqp
, char *filename
, char *search
, char *msg
)
552 size_t flen
= strlen(filename
);
553 size_t slen
= strlen(search
);
554 size_t mlen
= strlen(msg
);
556 CALLOC_GOTO(sp
, tp
, TAG
*, 1,
557 sizeof(TAG
) - 1 + flen
+ 1 +
558 (slen
+ 1 + mlen
+ 1) * sizeof(CHAR_T
));
559 tp
->fname
= (char *)tp
->buf
;
560 memcpy(tp
->fname
, filename
, flen
+ 1);
562 tp
->search
= (CHAR_T
*)((char *)tp
->fname
+ flen
+ 1);
563 CHAR2INT(sp
, search
, slen
+ 1, wp
, wlen
);
564 MEMCPYW(tp
->search
, wp
, wlen
);
566 tp
->msg
= tp
->search
+ slen
+ 1;
567 CHAR2INT(sp
, msg
, mlen
+ 1, wp
, wlen
);
568 MEMCPYW(tp
->msg
, wp
, wlen
);
570 TAILQ_INSERT_TAIL(&tqp
->tagq
, tp
, q
);
577 * PUBLIC: int api_tagq_push __P((SCR*, TAGQ**));
580 api_tagq_push(SCR
*sp
, TAGQ
**tqpp
)
588 /* Check to see if we found anything. */
589 if (TAILQ_EMPTY(&tqp
->tagq
)) {
594 tqp
->current
= TAILQ_FIRST(&tqp
->tagq
);
596 if (tagq_push(sp
, tqp
, 0, 0))
603 * PUBLIC: void api_tagq_free __P((SCR*, TAGQ*));
606 api_tagq_free(SCR
*sp
, TAGQ
*tqp
)