Skip to content

Commit 93398d4

Browse files
committed
improve C functions
1 parent 74cfffe commit 93398d4

File tree

7 files changed

+23
-231
lines changed

7 files changed

+23
-231
lines changed

cutil/lm_nocompare.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
* modify it under the terms of the GNU Lesser General Public
1515
* License as published by the Free Software Foundation,
1616
* version 2.1 of the License.
17-
*
17+
*
1818
* This library is distributed in the hope that it will be useful,
1919
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2020
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2121
* Lesser General Public License for more details.
22-
*
22+
*
2323
* You should have received a copy of the GNU Lesser General Public
2424
* License along with this library; if not, write to the Free Software
2525
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26-
*
26+
*
2727
* Additional permission is given to link this library with the
2828
* OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
2929
* and you may distribute the linked executables. See the file

cutil/lm_printf.c

Lines changed: 0 additions & 201 deletions
This file was deleted.

cutil/lm_rformat.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static int s_wcswidth(const wchar_t *pwcs, size_t n)
6767
*/
6868
value display_width_of_string(value v_string)
6969
{
70+
CAMLparam1(v_string);
7071
const char *str = String_val(v_string);
7172

7273
wchar_t pwcs[BUFSIZE];
@@ -79,5 +80,5 @@ value display_width_of_string(value v_string)
7980
i += s_wcswidth(pwcs, BUFSIZE);
8081
}
8182

82-
return Val_int(i);
83+
CAMLreturn(Val_int(i));
8384
}

cutil/lm_terminfo.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,9 @@ static int load_terminfo() {
4444
}
4545

4646
/* Converts termcap data type to capname */
47-
static char *capname(int i) {
48-
49-
char *names[] = {"bold", "snrmq", "ssubm", "ssupm", "smul", "sitm", "sgr0"};
50-
51-
return names[i];
52-
}
47+
static char *capnames[] = {
48+
"bold", "snrmq", "ssubm", "ssupm", "smul", "sitm", "sgr0"
49+
};
5350

5451
#endif /* NCURSES support? */
5552

@@ -77,16 +74,17 @@ value caml_tgetstr(value id) {
7774
in. */
7875
#ifdef NCURSES
7976
if(load_terminfo() == 0) {
80-
char *name = capname(Int_val(id));
77+
char *name = capnames[Int_val(id)];
8178
termdata = tigetstr(name);
8279
}
8380
#endif /* NCURSES */
8481

8582
/* Note that tigetstr will return either 0 or -1 on error. */
8683
if(termdata == NULL || termdata == (char *)(-1)) {
87-
result = caml_copy_string("");
84+
result = Val_int(0); /* None */
8885
} else {
89-
result = caml_copy_string(termdata);
86+
result = caml_alloc_small(1, 0); /* Some string */
87+
Field(result, 0) = caml_copy_string(termdata);
9088
/* apparently we're not supposed to free termdata here */
9189
/* TEMP: I cannot find specs on this! */
9290
//free(termdata);

cutil/lm_termsize.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ value caml_term_size(value arg)
6666

6767
if(ioctl(fd, TIOCGWINSZ, &ws) < 0)
6868
caml_failwith("lm_termsize.c: caml_term_size: not a terminal");
69-
69+
7070
/* Return the pair of numbers */
7171
Field(buf, 0) = Val_int(ws.ws_row);
7272
Field(buf, 1) = Val_int(ws.ws_col);
7373
}
7474
#else /* TIOCGWINSZ */
75-
/* Assume that the terminal is 80 by 25 */
76-
Field(buf, 0) = Val_int( 25 );
77-
Field(buf, 1) = Val_int( 80 );
75+
/* Assume that the terminal is 80 by 25 */
76+
Field(buf, 0) = Val_int( 25 );
77+
Field(buf, 1) = Val_int( 80 );
7878
#endif /* TIOCGWINSZ */
7979
#endif /* WIN32 */
8080

cutil/lm_uname_ext.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
* modify it under the terms of the GNU Lesser General Public
1111
* License as published by the Free Software Foundation,
1212
* version 2.1 of the License.
13-
*
13+
*
1414
* This library is distributed in the hope that it will be useful,
1515
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1616
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1717
* Lesser General Public License for more details.
18-
*
18+
*
1919
* You should have received a copy of the GNU Lesser General Public
2020
* License along with this library; if not, write to the Free Software
2121
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22-
*
22+
*
2323
* Additional permission is given to link this library with the
2424
* OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
2525
* and you may distribute the linked executables. See the file

util/lm_terminfo.ml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type termcap = Bold | Normal | Subscript | Superscript
2929
(* The C function takes a integer, and returns the escape sequence
3030
(or an empty string if the ID is not defined for this terminal). *)
3131
external caml_tgetstr_enabled : unit -> bool = "caml_tgetstr_enabled"
32-
external caml_tgetstr : termcap -> string = "caml_tgetstr"
32+
external caml_tgetstr : termcap -> string option = "caml_tgetstr"
3333

3434

3535
(* Tgetstr is enabled only if the terminal is defined *)
@@ -39,15 +39,9 @@ let tgetstr_enabled = caml_tgetstr_enabled ()
3939
Lookup the terminal capability with indicated id. This assumes the
4040
terminfo to lookup is given in the TERM environment variable. This
4141
function returns None if the terminal capability is not defined. *)
42-
let tgetstr id =
43-
if tgetstr_enabled then
44-
let result = caml_tgetstr id in
45-
if result = "" then
46-
None
47-
else
48-
Some result
49-
else
50-
None
42+
let tgetstr = if tgetstr_enabled then
43+
caml_tgetstr
44+
else (fun _ -> None)
5145

5246

5347
(* Various terminfo identifier names for use with tgetstr *)

0 commit comments

Comments
 (0)