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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
const z = @import( "std" );
const folders = @import( "known_folders" );
var gpa = z.heap.GeneralPurposeAllocator( .{ .thread_safe = true } ){};
const alloc = gpa.allocator();
const bufprint = z.fmt.bufPrint;
const allocprint = z.fmt.allocPrint;
const ArrayList = z.ArrayList;
var paths = [_][]const u8{
"~/.local/share/applications",
"/usr/share/applications",
"/usr/local/share/applications",
};
const FsEntry = struct {
fn deinit( s: *const FsEntry ) void {
alloc.free( s.path );
alloc.free( s.name );
}
path: []const u8,
name: []const u8,
is_dir: bool
};
const XdgEntry = struct {
fn deinit( s: *const XdgEntry ) void {
alloc.free( s.path );
alloc.free( s.name );
alloc.free( s.desc );
alloc.free( s.exec );
}
path: []const u8,
name: []const u8,
desc: []const u8,
exec: []const u8,
};
pub fn checkSymlink( path: []const u8 ) !void {
const link_path_buf = try alloc.alloc( u8, z.fs.max_path_bytes );
var link_path = try z.fs.readLinkAbsolute( path, link_path_buf[0..z.fs.max_path_bytes] );
const is_abs = z.fs.path.isAbsolute( link_path );
if( !is_abs )
link_path = try z.fs.path.resolve( alloc, &[_][]const u8{ path, "..", link_path } );
defer if( !is_abs ) alloc.free( link_path );
const f = try z.fs.openFileAbsolute( link_path, .{} );
defer f.close();
const meta = try f.metadata();
if( meta.kind() != .file )
return error.NotAFile;
}
pub fn openDir( path: []const u8 ) !ArrayList( FsEntry ) {
var handle = try z.fs.openDirAbsolute( path, .{ .iterate = true, .no_follow = false } );
defer handle.close();
var list = ArrayList( FsEntry ).init( alloc );
var iter = handle.iterate();
while( try iter.next() ) |entry| {
if( !z.mem.endsWith( u8, entry.name, ".desktop" ) )
continue;
const path_buf = try alloc.alloc( u8, entry.name.len + path.len + 1 );
const name = try alloc.dupe( u8, entry.name );
const entry_path = try bufprint( path_buf, "{s}/{s}", .{path, entry.name} );
if( entry.kind == .sym_link ) {
checkSymlink( entry_path ) catch continue;
}
const fs_entry = FsEntry{
.name = name,
.path = entry_path,
.is_dir = entry.kind == .directory
};
list.append( fs_entry ) catch {};
}
return list;
}
pub fn parseXDGEntry( contents: []const u8, path: []const u8 ) !XdgEntry {
var i: u32 = 0;
var s: u32 = 0;
var name: ?[]const u8 = null;
var exec: ?[]const u8 = null;
var desc: ?[]const u8 = null;
for( contents ) |c| {
if( c == '\n' ) {
const line = contents[s..i];
if( name == null ) {
if( z.mem.startsWith( u8, line, "Name=" ) )
name = try alloc.dupe( u8, line[5..line.len] );
}
if( exec == null ) {
if( z.mem.startsWith( u8, line, "Exec=" ) )
exec = try alloc.dupe( u8, line[5..line.len] );
}
if( desc == null ) {
if( z.mem.startsWith( u8, line, "Comment=" ) )
desc = try alloc.dupe( u8, line[8..i-s] );
}
s = i + 1;
}
i += 1;
}
errdefer {
if( name != null ) alloc.free( name.? );
if( exec != null ) alloc.free( exec.? );
if( desc != null ) alloc.free( desc.? );
}
if( name == null or exec == null or name.?.len == 0 or exec.?.len == 0 )
return error.InvalidEntry;
if( desc == null )
desc = "";
return XdgEntry {
.name = name.?,
.exec = exec.?,
.desc = desc.?,
.path = try alloc.dupe( u8, path )
};
}
pub fn parseXDGEntries( entries: ArrayList( FsEntry ) ) !ArrayList( XdgEntry ) {
var list = ArrayList( XdgEntry ).init( alloc );
for( entries.items ) |entry| {
if( entry.is_dir )
continue;
var handle = try z.fs.openFileAbsolute( entry.path, .{} );
defer handle.close();
var reader = handle.reader();
const buf = try reader.readAllAlloc(alloc, 999999);
defer alloc.free( buf );
const xdg = parseXDGEntry( buf, entry.path ) catch continue;
try list.append( xdg );
}
return list;
}
pub fn main() !void {
var all_entries = ArrayList( XdgEntry ).init( alloc );
for( paths ) |path| {
var line = path;
var free_path = false;
const idx = z.mem.indexOf( u8, path, "~" );
if( idx ) |i| {
const home = try folders.getPath( alloc, .home );
if( home == null )
return error.NoHome;
line = try allocprint( alloc, "{s}{s}", .{ home.?, line[i+1..] } );
alloc.free( home.? );
free_path = true;
}
defer { if( free_path ) alloc.free( line ); }
const entries = try openDir( line );
defer {
for( entries.items ) |e|
e.deinit();
entries.deinit();
}
const list = try parseXDGEntries( entries );
try all_entries.appendSlice( list.items );
list.deinit();
}
for( all_entries.items ) |e| {
if( e.desc.len > 0 ) {
try z.io.getStdOut().writer().print( "{s} - {s} | {s}\n", .{ e.name, e.desc, e.path } );
} else
try z.io.getStdOut().writer().print( "{s} | {s}\n", .{ e.name, e.path } );
e.deinit();
}
all_entries.deinit();
_ = gpa.deinit();
}
|