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
#[macro_export] macro_rules! set_logcb { ( $handle:tt, $f:tt ) => {{ use ::std::ffi::{c_void, CStr}; use ::std::os::raw::{c_char, c_int}; use ::std::ptr; use $crate::alpm_sys::*; use $crate::LogLevel; extern "C" { fn vasprintf( str: *const *mut c_char, fmt: *const c_char, args: *mut __va_list_tag, ) -> c_int; fn free(ptr: *mut c_void); } unsafe extern "C" fn c_logcb( level: alpm_loglevel_t, fmt: *const c_char, args: *mut __va_list_tag, ) { let buff = ptr::null_mut(); let n = vasprintf(&buff, fmt, args); if n != -1 { let s = CStr::from_ptr(buff); let level = LogLevel::from_bits(level).unwrap(); $f(level, &s.to_string_lossy()); free(buff as *mut c_void); } } unsafe { alpm_option_set_logcb($handle.as_alpm_handle_t(), Some(c_logcb)) }; }}; } #[macro_export] macro_rules! set_dlcb { ( $handle:tt, $f:tt ) => {{ use $crate::alpm_sys::*; use ::std::ffi::CStr; use ::std::os::raw::c_char; unsafe extern "C" fn c_dlcb( filename: *const c_char, xfered: off_t, total: off_t, ) { let filename = CStr::from_ptr(filename); let filename = filename.to_str().unwrap(); $f(&filename, xfered as u64, total as u64); } unsafe { alpm_option_set_dlcb($handle.as_alpm_handle_t(), Some(c_dlcb)) }; }}; } #[macro_export] macro_rules! set_fetchcb { ( $handle:tt, $f:tt ) => {{ use ::std::ffi::CStr; use ::std::os::raw::{c_char, c_int}; use $crate::alpm_sys::*; use $crate::FetchCbReturn; unsafe extern "C" fn c_fetchcb( url: *const c_char, localpath: *const c_char, force: c_int, ) -> c_int { let url = CStr::from_ptr(url).to_str().unwrap(); let localpath = CStr::from_ptr(localpath).to_str().unwrap(); let ret = $f(url, localpath, force != 0); match ret { FetchCbReturn::Ok => 0, FetchCbReturn::Err => -1, FetchCbReturn::FileExists => 1, } } unsafe { alpm_option_set_fetchcb($handle.as_alpm_handle_t(), Some(c_fetchcb)) }; }}; } #[macro_export] macro_rules! set_totaldlcb { ( $handle:tt, $f:tt ) => {{ use $crate::alpm_sys::*; unsafe extern "C" fn c_totaldlcb(total: off_t) { $f(total as u64); } unsafe { alpm_option_set_totaldlcb($handle.as_alpm_handle_t(), Some(c_totaldlcb)) }; }}; } #[macro_export] macro_rules! set_eventcb { ( $handle:tt, $f:tt ) => {{ use ::std::ptr; use $crate::alpm_sys::*; use $crate::Event; static mut C_ALPM_HANDLE: *mut alpm_handle_t = ptr::null_mut(); unsafe { C_ALPM_HANDLE = $handle.as_alpm_handle_t(); } unsafe extern "C" fn c_eventcb(event: *mut alpm_event_t) { let event = Event::new(C_ALPM_HANDLE, event); $f(event); } unsafe { alpm_option_set_eventcb($handle.as_alpm_handle_t(), Some(c_eventcb)) }; }}; } #[macro_export] macro_rules! set_questioncb { ( $handle:tt, $f:tt ) => {{ use ::std::ptr; use $crate::alpm_sys::*; use $crate::Question; static mut C_ALPM_HANDLE: *mut alpm_handle_t = ptr::null_mut(); unsafe { C_ALPM_HANDLE = $handle.as_alpm_handle_t(); } unsafe extern "C" fn c_questioncb(question: *mut alpm_question_t) { let question = Question::new(C_ALPM_HANDLE, question); $f(question); } unsafe { alpm_option_set_questioncb($handle.as_alpm_handle_t(), Some(c_questioncb)) }; }}; } #[macro_export] macro_rules! set_progresscb { ( $handle:tt, $f:tt ) => {{ use ::std::ffi::CStr; use ::std::mem::transmute; use ::std::os::raw::{c_char, c_int}; use $crate::alpm_sys::*; unsafe extern "C" fn c_progresscb( progress: alpm_progress_t, pkgname: *const c_char, percent: c_int, howmany: usize, current: usize, ) { let pkgname = CStr::from_ptr(pkgname); let pkgname = pkgname.to_str().unwrap(); let progress = transmute::<alpm_progress_t, Progress>(progress); $f(progress, &pkgname, percent as i32, howmany, current); } unsafe { alpm_option_set_progresscb($handle.as_alpm_handle_t(), Some(c_progresscb)) }; }}; } #[macro_export] macro_rules! log_action { ($handle:tt, $prefix:tt, $($arg:tt)*) => ({ use $crate::alpm_sys::*; use ::std::ffi::CString; let mut s = format!($($arg)*); s.push('\n'); let s = CString::new(s).unwrap(); let p = CString::new($prefix).unwrap(); let ret = unsafe { alpm_logaction($handle.as_alpm_handle_t(), p.as_ptr(), s.as_ptr()) }; if ret != 0 { Err($handle.last_error()) } else { Ok(()) } }) }