robin/cdl
cute fetch client written in zig
git clone https://git.robinwobin.dev/robin/cdl
3353e24
main
1const std =@import ( "std" ); 2 3const cdl =@import ( "root.zig" ); 4 5/// owned 6pub fn hostname () ! [] const u8 { 7var buf : [ std . posix . HOST_NAME_MAX ] u8 =undefined ; 8const hostname_buf =try std . posix . gethostname ( & buf ); 9return try cdl . gpa . dupe ( u8 , hostname_buf ); 10} 11 12pub fn user () ! ? [] const u8 { 13const pwinfo =try PWInfo . get (); 14if ( pwinfo ) | pw | return pw . user ; 15 16if ( cdl . environ_map ) | envmap | return envmap . get ( "USER" ); 17if ( cdl . environ_map ) | envmap | return envmap . get ( "LOGNAME" ); 18 19return null ; 20} 21 22pub fn shell () ! ? [] const u8 { 23const shell_str =blk : { 24const pwinfo =try PWInfo . get (); 25if ( pwinfo ) | pw | break : blk pw . shell ; 26 27break : blk if ( cdl . environ_map ) | envmap | 28envmap . get ( "SHELL" ) 29else 30null ; 31} orelse return null ; 32 33if ( std . mem . cutScalarLast ( u8 , shell_str , '/' )) | parts | 34return parts [ 1 ]; 35 36return shell_str ; 37} 38 39pub fn osname () ! ? [] const u8 { 40const f =try std . Io . Dir . openFileAbsolute ( cdl . io , "/etc/os-release" , .{}); 41 42var buf : [ 0x1000 ] u8 =undefined ; 43var file_reader =f . reader ( cdl . io , & buf ); 44var reader =& file_reader . interface ; 45 46while ( try reader . takeDelimiter ( '\n' )) | line | 47if ( std . mem . cutPrefix ( u8 , line , "NAME=" )) | name | 48return try cdl . allocator . dupe ( u8 , trimquotes ( name )); 49return null ; 50} 51 52pub fn desktop () ? [] const u8 { 53if ( cdl . environ_map ) | envmap | return envmap . get ( "XDG_CURRENT_DESKTOP" ); 54return null ; 55} 56 57pub fn session () ? [] const u8 { 58if ( cdl . environ_map ) | envmap | return envmap . get ( "XDG_SESSION_TYPE" ); 59return null ; 60} 61 62pub fn terminal () ? [] const u8 { 63if ( cdl . environ_map ) | envmap | return envmap . get ( "TERMINAL" ); 64const nterm : ? [] const u8 =blk : { 65const termenv =if ( cdl . environ_map ) | envmap | envmap . get ( "TERM" ) else null ; 66if ( termenv ) | term | if ( term . len > 0 ) if ( std . mem . findScalar ( u8 , term , '-' )) | i | 67break : blk term [ 0 .. i ] 68else 69break : blk term ; 70break : blk null ; 71}; 72if ( nterm ) | term | if ( ! std . mem . eql ( u8 , term , "xterm" ) and ! std . mem . eql ( u8 , term , "linux" )) 73return term ; 74 75return null ; 76} 77 78pub fn editor () ? [] const u8 { 79if ( cdl . environ_map ) | envmap | return envmap . get ( "EDITOR" ); 80return null ; 81} 82 83const PWInfo =struct { 84user : ? [] const u8 , 85shell : ? [] const u8 , 86dir : ? [] const u8 , 87 88var cached : ? @This () =null ; 89 90pub fn get () ! ? PWInfo { 91if ( PWInfo . cached ) | c | return c ; 92 93const f =try std . Io . Dir . openFileAbsolute ( cdl . io , "/etc/passwd" , .{}); 94 95var buf : [ 0x1000 ] u8 =undefined ; 96var file_reader =f . reader ( cdl . io , & buf ); 97var reader =& file_reader . interface ; 98 99const uid =std . os . linux . getuid (); 100w : while ( try reader . takeDelimiter ( '\n' )) | line | { 101var parts =std . mem . splitScalar ( u8 , line , ':' ); 102var pw_user : ? [] const u8 =null ; 103var pw_shell : ? [] const u8 =null ; 104var pw_dir : ? [] const u8 =null ; 105 106var i : usize =0 ; 107while ( parts . next ()) | part | : ( i = i + 1 ) switch ( i ) { 1080 =>pw_user = part , 1092 =>if ( try std . fmt . parseInt ( u32 , part , 10 ) != uid ) continue : w , 1105 =>pw_dir = part , 1116 =>pw_shell = part , 112else =>{}, 113}; 114 115return .{ 116. user =if ( pw_user ) | user_ | try cdl . allocator . dupe ( u8 , user_ ) else null , 117. shell =if ( pw_shell ) | shell_ | try cdl . allocator . dupe ( u8 , shell_ ) else null , 118. dir =if ( pw_dir ) | dir_ | try cdl . allocator . dupe ( u8 , dir_ ) else null , 119}; 120} 121 122return null ; 123} 124}; 125 126// helpers ==================================================================== 127 128fn trimquotes ( s : [] const u8 ) [] const u8 { 129if ( s . len < 2 ) return s ; 130const open =switch ( s [ 0 ]) { 131'"' =>s [ 0 ], 132'\'' =>s [ 0 ], 133else =>return s , 134}; 135// assume malformed 136if ( s [ s . len - 1 ] != open ) return s [ 1 ..]; 137return s [ 1 .. s . len - 1 ]; 138} 139 140test "trim quotes" { 141try std . testing . expectEqualStrings ( "meow" , trimquotes ( "meow" )); 142try std . testing . expectEqualStrings ( "meow" , trimquotes ( 143\\"meow" 144)); 145try std . testing . expectEqualStrings ( "meow" , trimquotes ( 146\\'meow' 147)); 148try std . testing . expectEqualStrings ( "meow" , trimquotes ( 149\\'meow 150)); 151try std . testing . expectEqualStrings ( "meow" , trimquotes ( 152\\"meow 153)); 154}