Change package-exists to return section/description
[hcoop/domtool2.git] / src / plugins / apt.sml
CommitLineData
75585a67
AC
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006-2007, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19(* APT package database querying *)
20
21structure Apt :> APT = struct
22
23fun validName s = CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"_" orelse ch = #"-" orelse ch = #".") s
24 andalso (size s > 0 andalso String.sub (s, 0) <> #"-")
991d8e66
CE
25
26(* Copyed from the portal, doesn't exactly go out this in the most
27 direct way, or does it? *)
28
f296c496
CE
29fun info name =
30 if not (validName name) then
31 NONE
32 else
33 let
34 val proc = Unix.execute ("/usr/bin/apt-cache", ["show", name])
35 val inf = Unix.textInstreamOf proc
36
37 val _ = TextIO.inputLine inf (* in every let* lies an imperative program in disguise *)
38
39 fun loop (section, descr) =
40 case TextIO.inputLine inf of
41 NONE => (section, descr)
42 | SOME line =>
43 if size line >= 9 andalso String.substring (line, 0, 9) = "Section: " then
44 loop (SOME (String.substring (line, 9, size line - 10)), descr)
45 else if size line >= 13 andalso String.substring (line, 0, 13) = "Description: " then
46 loop (section, SOME (String.substring (line, 13, size line - 14)))
47 else
48 loop (section, descr)
49 in
50 (case loop (NONE, NONE) of
51 (SOME section, SOME descr) => SOME {section = section, description = descr}
52 | _ => NONE)
53 before ignore (Unix.reap proc)
54 end
75585a67
AC
55
56fun installed name =
57 validName name
a378e688
AC
58 andalso let
59 val proc = Unix.execute ("/usr/bin/apt-cache", ["policy", name])
60 val inf = Unix.textInstreamOf proc
61
62 val _ = TextIO.inputLine inf
63 in
64 (case TextIO.inputLine inf of
65 NONE => false
66 | SOME line =>
67 case String.tokens Char.isSpace line of
68 [_, "(none)"] => false
69 | [_, _] => true
70 | _ => false)
71 before ignore (Unix.reap proc)
72 end
75585a67
AC
73
74end