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.
7 * George V. Neville-Neil. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "$Id: api.c,v 8.27 1996/12/05 12:27:30 bostic Exp $ (Berkeley) $Date: 1996/12/05 12:27:30 $";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
32 extern GS
*__global_list
; /* XXX */
36 * Return a pointer to the screen specified by the screen id
39 * PUBLIC: SCR *api_fscreen __P((int, char *));
51 /* Search the displayed list. */
52 for (tsp
= gp
->dq
.cqh_first
;
53 tsp
!= (void *)&gp
->dq
; tsp
= tsp
->q
.cqe_next
)
57 } else if (!strcmp(name
, tsp
->frp
->name
))
60 /* Search the hidden list. */
61 for (tsp
= gp
->hq
.cqh_first
;
62 tsp
!= (void *)&gp
->hq
; tsp
= tsp
->q
.cqe_next
)
66 } else if (!strcmp(name
, tsp
->frp
->name
))
75 * PUBLIC: int api_aline __P((SCR *, recno_t, char *, size_t));
78 api_aline(sp
, lno
, line
, len
)
84 return (db_append(sp
, 1, lno
, line
, len
));
91 * PUBLIC: int api_dline __P((SCR *, recno_t));
98 return (db_delete(sp
, lno
));
105 * PUBLIC: int api_gline __P((SCR *, recno_t, char **, size_t *));
108 api_gline(sp
, lno
, linepp
, lenp
)
116 if (db_eget(sp
, lno
, linepp
, lenp
, &isempty
)) {
118 msgq(sp
, M_ERR
, "209|The file is empty");
128 * PUBLIC: int api_iline __P((SCR *, recno_t, char *, size_t));
131 api_iline(sp
, lno
, line
, len
)
137 return (db_insert(sp
, lno
, line
, len
));
142 * Return the line number of the last line in the file.
144 * PUBLIC: int api_lline __P((SCR *, recno_t *));
151 return (db_last(sp
, lnop
));
158 * PUBLIC: int api_sline __P((SCR *, recno_t, char *, size_t));
161 api_sline(sp
, lno
, line
, len
)
167 return (db_set(sp
, lno
, line
, len
));
174 * PUBLIC: int api_getmark __P((SCR *, int, MARK *));
177 api_getmark(sp
, markname
, mp
)
182 return (mark_get(sp
, (ARG_CHAR_T
)markname
, mp
, M_ERR
));
189 * PUBLIC: int api_setmark __P((SCR *, int, MARK *));
192 api_setmark(sp
, markname
, mp
)
197 return (mark_set(sp
, (ARG_CHAR_T
)markname
, mp
, 1));
202 * Return the first mark if next not set, otherwise return the
205 * PUBLIC: int api_nextmark __P((SCR *, int, char *));
208 api_nextmark(sp
, next
, namep
)
215 mp
= sp
->ep
->marks
.lh_first
;
217 for (; mp
!= NULL
; mp
= mp
->q
.le_next
)
218 if (mp
->name
== *namep
) {
232 * PUBLIC: int api_getcursor __P((SCR *, MARK *));
235 api_getcursor(sp
, mp
)
248 * PUBLIC: int api_setcursor __P((SCR *, MARK *));
251 api_setcursor(sp
, mp
)
257 if (db_get(sp
, mp
->lno
, DBG_FATAL
, NULL
, &len
))
259 if (mp
->cno
< 0 || mp
->cno
> len
) {
260 msgq(sp
, M_ERR
, "Cursor set to nonexistent column");
264 /* Set the cursor. */
272 * Print an error message.
274 * PUBLIC: void api_emessage __P((SCR *, char *));
277 api_emessage(sp
, text
)
281 msgq(sp
, M_ERR
, "%s", text
);
286 * Print an informational message.
288 * PUBLIC: void api_imessage __P((SCR *, char *));
291 api_imessage(sp
, text
)
295 msgq(sp
, M_INFO
, "%s", text
);
300 * Create a new screen and return its id
301 * or edit a new file in the current screen.
303 * PUBLIC: int api_edit __P((SCR *, char *, SCR **, int));
306 api_edit(sp
, file
, spp
, newscreen
)
315 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
316 argv_exp0(sp
, &cmd
, file
, strlen(file
));
318 ex_cinit(sp
, &cmd
, C_EDIT
, 0, OOBLNO
, OOBLNO
, 0);
320 cmd
.flags
|= E_NEWSCREEN
; /* XXX */
321 if (cmd
.cmd
->fn(sp
, &cmd
))
331 * PUBLIC: int api_escreen __P((SCR *));
341 * If the interpreter exits anything other than the current
342 * screen, vi isn't going to update everything correctly.
344 ex_cinit(sp
, &cmd
, C_QUIT
, 0, OOBLNO
, OOBLNO
, 0);
345 return (cmd
.cmd
->fn(sp
, &cmd
));
350 * Switch to a new screen.
352 * PUBLIC: int api_swscreen __P((SCR *, SCR *));
355 api_swscreen(sp
, new)
360 * If the interpreter switches from anything other than the
361 * current screen, vi isn't going to update everything correctly.
364 F_SET(sp
, SC_SSWITCH
);
373 * PUBLIC: int api_map __P((SCR *, char *, char *, size_t));
376 api_map(sp
, name
, map
, len
)
383 ex_cinit(sp
, &cmd
, C_MAP
, 0, OOBLNO
, OOBLNO
, 0);
384 argv_exp0(sp
, &cmd
, name
, strlen(name
));
385 argv_exp0(sp
, &cmd
, map
, len
);
386 return (cmd
.cmd
->fn(sp
, &cmd
));
393 * PUBLIC: int api_unmap __P((SCR *, char *));
402 ex_cinit(sp
, &cmd
, C_UNMAP
, 0, OOBLNO
, OOBLNO
, 0);
403 argv_exp0(sp
, &cmd
, name
, strlen(name
));
404 return (cmd
.cmd
->fn(sp
, &cmd
));
409 * Return a option value as a string, in allocated memory.
410 * If the option is of type boolean, boolvalue is (un)set
411 * according to the value; otherwise boolvalue is -1.
413 * PUBLIC: int api_opts_get __P((SCR *, char *, char **, int *));
416 api_opts_get(sp
, name
, value
, boolvalue
)
424 if ((op
= opts_search(name
)) == NULL
) {
425 opts_nomatch(sp
, name
);
429 offset
= op
- optlist
;
430 if (boolvalue
!= NULL
)
435 MALLOC_RET(sp
, *value
, char *, strlen(op
->name
) + 2 + 1);
436 (void)sprintf(*value
,
437 "%s%s", O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
438 if (boolvalue
!= NULL
)
439 *boolvalue
= O_ISSET(sp
, offset
);
442 MALLOC_RET(sp
, *value
, char *, 20);
443 (void)sprintf(*value
, "%lu", (u_long
)O_VAL(sp
, offset
));
446 if (O_STR(sp
, offset
) == NULL
) {
447 MALLOC_RET(sp
, *value
, char *, 2);
451 *value
, char *, strlen(O_STR(sp
, offset
)) + 1);
452 (void)sprintf(*value
, "%s", O_STR(sp
, offset
));
463 * PUBLIC: int api_opts_set __P((SCR *, char *, char *, u_long, int));
466 api_opts_set(sp
, name
, str_value
, num_value
, bool_value
)
468 char *name
, *str_value
;
478 if ((op
= opts_search(name
)) == NULL
) {
479 opts_nomatch(sp
, name
);
486 GET_SPACE_RET(sp
, bp
, blen
, 64);
487 a
.len
= snprintf(bp
, 64, "%s%s", bool_value
? "" : "no", name
);
490 GET_SPACE_RET(sp
, bp
, blen
, 64);
491 a
.len
= snprintf(bp
, 64, "%s=%lu", name
, num_value
);
494 GET_SPACE_RET(sp
, bp
, blen
, 1024);
495 a
.len
= snprintf(bp
, 1024, "%s=%s", name
, str_value
);
503 rval
= opts_set(sp
, ap
, NULL
);
505 FREE_SPACE(sp
, bp
, blen
);
512 * Execute a string as an ex command.
514 * PUBLIC: int api_run_str __P((SCR *, char *));
521 return (ex_run_str(sp
, NULL
, cmd
, strlen(cmd
), 0, 0));