-
Notifications
You must be signed in to change notification settings - Fork 0
/
getfileopts.ml
153 lines (134 loc) · 4.53 KB
/
getfileopts.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(***********************************************************************)
(* getfileopts.ml - Loads settings from settings file. *)
(* *)
(* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, *)
(* 2011, 2012 Yaron Minsky and Contributors *)
(* *)
(* This file is part of SKS. SKS is free software; you can *)
(* redistribute it and/or modify it under the terms of the GNU General *)
(* Public License as published by the Free Software Foundation; either *)
(* version 2 of the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
(* General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public License *)
(* along with this program; if not, write to the Free Software *)
(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *)
(* USA or see <http://www.gnu.org/licenses/>. *)
(***********************************************************************)
open StdLabels
open MoreLabels
open Printf
open Settings
open Pstyle
let protect ~f ~(finally: unit -> unit) =
let result = ref None in
try
result := Some (f ());
raise Exit
with
Exit as e ->
finally ();
(match !result with Some x -> x | None -> raise e)
| e ->
finally (); raise e
let whitespace c = c = '\t' || c = ' ' || c = '\n'
let strip s =
let lower = ref 0 in
while !lower < String.length s && whitespace s.[!lower] do
incr lower
done;
let upper = ref (String.length s - 1) in
while !upper >= 0 && whitespace s.[!upper] do
decr upper
done;
if !upper < !lower then ""
else
String.sub s ~pos:!lower ~len:(!upper - !lower + 1)
let csplit c s =
let i = String.index s c in
(strip (String.sub ~pos:0 ~len:i s),
strip (String.sub ~pos:(i+1) ~len:(String.length s - i - 1) s)
)
let decomment l =
let l =
try
let pos = String.index l '#' in
String.sub l ~pos:0 ~len:pos
with
Not_found -> l
in
strip l
(** convert a line of the config line to command-line format *)
let line_convert l =
let l = decomment l in
if String.length l = 0 then None
else
let (command,arg) = csplit ':' l in
Some [ "-" ^ command ; arg ]
(** read in file and convert it to command-line format *)
let file_convert f =
let rec loop accum =
match (try Some (input_line f) with End_of_file -> None)
with
| Some l -> (
match line_convert l with
None -> loop accum
| Some l -> loop (l :: accum)
)
| None -> "" :: List.concat (List.rev accum)
in
Array.of_list (loop [])
let fname_convert fname =
if Sys.file_exists fname then
try
let f = open_in fname in
protect ~f:(fun () -> file_convert f)
~finally:(fun () -> close_in f)
with
Sys_error _ as e -> failwith
(sprintf "Sys error while parsing config file: %s"
(Printexc.to_string e) )
else
[||]
(**************************************************************)
(**************************************************************)
(**************************************************************)
let config_fname = "sksconf"
let parse args =
Arg.current := 0;
Arg.parse_argv args parse_spec anon_options usage_string
let () =
try
let pos = ref 0 in
while !pos < Array.length Sys.argv && Sys.argv.(!pos) <>
"-read_config_file"
do incr pos done;
if !pos = Array.length Sys.argv
then (
parse Sys.argv;
let from_file_commandline =
fname_convert (Filename.concat !Settings.confdir config_fname)
in
parse from_file_commandline
)
else (
parse (Sys.argv <|> (0,!pos));
let from_file_commandline =
fname_convert (Filename.concat !Settings.confdir config_fname)
in
parse from_file_commandline;
parse (Array.append [|""|] (Sys.argv <|> (!pos + 1,0)))
);
anonlist := List.rev !anonlist;
anonlist := List.filter ~f:(( <> ) "") !anonlist
with
| Arg.Bad s ->
print_string s;
exit (-1)
| Arg.Help s ->
print_string s;
exit 0