apt: Handle Description-en replacing Description in apt-cache output
[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)))
6f1dbd13
CE
47 else if size line >= 16 andalso String.substring (line, 0, 16) = "Description-en: " then
48 loop (section, SOME (String.substring (line, 16, size line - 17)))
f296c496
CE
49 else
50 loop (section, descr)
51 in
52 (case loop (NONE, NONE) of
53 (SOME section, SOME descr) => SOME {section = section, description = descr}
54 | _ => NONE)
55 before ignore (Unix.reap proc)
56 end
75585a67
AC
57
58fun installed name =
59 validName name
a378e688
AC
60 andalso let
61 val proc = Unix.execute ("/usr/bin/apt-cache", ["policy", name])
62 val inf = Unix.textInstreamOf proc
63
64 val _ = TextIO.inputLine inf
65 in
66 (case TextIO.inputLine inf of
67 NONE => false
68 | SOME line =>
69 case String.tokens Char.isSpace line of
70 [_, "(none)"] => false
71 | [_, _] => true
72 | _ => false)
73 before ignore (Unix.reap proc)
74 end
75585a67
AC
75
76end