| 1 | package Sidef::Sys::Sys; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | use parent qw( | ||
| 6 | Sidef::Object::Object | ||
| 7 | ); | ||
| 8 | |||
| 9 | use Sidef::Types::Bool::Bool; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | CORE::bless {}, __PACKAGE__; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub exit { | ||
| 16 | my ($self, $code) = @_; | ||
| 17 | CORE::exit($code // 0); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub kill { | ||
| 21 | my ($self, $signal, @list) = @_; | ||
| 22 | Sidef::Types::Number::Number->new(CORE::kill("$signal", map { CORE::int($_) } @list)); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub wait { | ||
| 26 | my ($self) = @_; | ||
| 27 | Sidef::Types::Number::Number->new(CORE::wait); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub fork { | ||
| 31 | my ($self) = @_; | ||
| 32 | Sidef::Types::Number::Number->new(CORE::fork() // return undef); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub alarm { | ||
| 36 | my ($self, $sec) = @_; | ||
| 37 | |||
| 38 | state $x = require Time::HiRes; | ||
| 39 | (Time::HiRes::alarm($sec)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub ualarm { | ||
| 43 | my ($self, $sec) = @_; | ||
| 44 | |||
| 45 | state $x = require Time::HiRes; | ||
| 46 | (Time::HiRes::ualarm($sec)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 47 | } | ||
| 48 | |||
| 49 | *micro_alarm = \&ualarm; | ||
| 50 | |||
| 51 | sub sleep { | ||
| 52 | my ($self, $sec) = @_; | ||
| 53 | |||
| 54 | state $x = require Time::HiRes; | ||
| 55 | (Time::HiRes::sleep($sec)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub nanosleep { | ||
| 59 | my ($self, $sec) = @_; | ||
| 60 | |||
| 61 | state $x = require Time::HiRes; | ||
| 62 | (Time::HiRes::nanosleep($sec)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 63 | } | ||
| 64 | |||
| 65 | *nano_sleep = \&nanosleep; | ||
| 66 | |||
| 67 | sub usleep { | ||
| 68 | my ($self, $sec) = @_; | ||
| 69 | |||
| 70 | state $x = require Time::HiRes; | ||
| 71 | (Time::HiRes::usleep($sec)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 72 | } | ||
| 73 | |||
| 74 | *micro_sleep = \&usleep; | ||
| 75 | |||
| 76 | sub osname { | ||
| 77 | my ($self) = @_; | ||
| 78 | Sidef::Types::String::String->new($^O); | ||
| 79 | } | ||
| 80 | |||
| 81 | *os = \&osname; | ||
| 82 | |||
| 83 | sub user { | ||
| 84 | my ($self) = @_; | ||
| 85 | Sidef::Types::String::String->new(CORE::getlogin); | ||
| 86 | } | ||
| 87 | |||
| 88 | *getlogin = \&user; | ||
| 89 | |||
| 90 | sub getenv { | ||
| 91 | my ($self, $name) = @_; | ||
| 92 | defined($ENV{"$name"}) ? Sidef::Types::String::String->new($ENV{"$name"}) : undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub setenv { | ||
| 96 | my ($self, $name, $value) = @_; | ||
| 97 | if (defined($value)) { $ENV{"$name"} = "$value"; } | ||
| 98 | else { CORE::delete($ENV{"$name"}); } | ||
| 99 | Sidef::Types::Bool::Bool::TRUE; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub env { | ||
| 103 | my ($self) = @_; | ||
| 104 | my $hash = Sidef::Types::Hash::Hash->new; | ||
| 105 | foreach my $key (CORE::keys(%ENV)) { | ||
| 106 | $hash->{$key} = Sidef::Types::String::String->new($ENV{$key}); | ||
| 107 | } | ||
| 108 | $hash; | ||
| 109 | } | ||
| 110 | |||
| 111 | *environ = \&env; | ||
| 112 | |||
| 113 | sub umask { | ||
| 114 | my ($self, $mode) = @_; | ||
| 115 | |||
| 116 | if (defined($mode)) { | ||
| 117 | return Sidef::Types::Number::Number->new(CORE::umask($mode)); | ||
| 118 | } | ||
| 119 | |||
| 120 | Sidef::Types::Number::Number->new(CORE::umask); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub ref { | ||
| 124 | my ($self, $obj) = @_; | ||
| 125 | Sidef::Types::String::String->new(CORE::ref $obj); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub defined { | ||
| 129 | my ($self, $obj) = @_; | ||
| 130 | (defined $obj) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub class_name { | ||
| 134 | my ($self, $obj) = @_; | ||
| 135 | my $ref = CORE::ref($obj); | ||
| 136 | |||
| 137 | my $rindex = rindex($ref, '::'); | ||
| 138 | Sidef::Types::String::String->new($rindex == -1 ? $ref : substr($ref, $rindex + 2)); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub sidef { | ||
| 142 | my ($self) = @_; | ||
| 143 | |||
| 144 | state $x = require File::Spec; | ||
| 145 | Sidef::Types::String::String->new(File::Spec->rel2abs($0)); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub die { | ||
| 149 | my ($self, @args) = @_; | ||
| 150 | CORE::die(@args, "\n"); | ||
| 151 | } | ||
| 152 | |||
| 153 | *raise = \¨ | ||
| 154 | |||
| 155 | sub warn { | ||
| 156 | my ($self, @args) = @_; | ||
| 157 | CORE::warn(@args, "\n"); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub print { | ||
| 161 | my ($self, @args) = @_; | ||
| 162 | (CORE::print(@args)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub printf { | ||
| 166 | my ($self, @args) = @_; | ||
| 167 | (CORE::printf(@args)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub printh { | ||
| 171 | my ($self, $fh, @args) = @_; | ||
| 172 | |||
| 173 | if (CORE::ref($fh) eq 'GLOB') { | ||
| 174 | return (CORE::print {$fh} @args) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 175 | } | ||
| 176 | |||
| 177 | $fh->print(@args); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub println { | ||
| 181 | my ($self, @args) = @_; | ||
| 182 | (CORE::say @args) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 183 | } | ||
| 184 | |||
| 185 | *say = \&println; | ||
| 186 | |||
| 187 | sub scanln { | ||
| 188 | my ($self, $text) = @_; | ||
| 189 | $self->read($text, 'Sidef::Types::String::String'); | ||
| 190 | } | ||
| 191 | |||
| 192 | *readln = \&scanln; | ||
| 193 | *readline = \&scanln; | ||
| 194 | |||
| 195 | sub read { | ||
| 196 | my ($self, $type, $opt_arg) = @_; | ||
| 197 | |||
| 198 | my $message = ''; | ||
| 199 | |||
| 200 | if (defined($opt_arg)) { | ||
| 201 | $message = "$type"; | ||
| 202 | $type = $opt_arg; | ||
| 203 | } | ||
| 204 | |||
| 205 | state $term = do { | ||
| 206 | require Term::ReadLine; | ||
| 207 | $Sidef::Term::ReadLine //= Term::ReadLine->new("$0"); | ||
| 208 | }; | ||
| 209 | |||
| 210 | my $input = $term->readline($message) // return undef; | ||
| 211 | |||
| 212 | defined($type) | ||
| 213 | ? $type->new($input) | ||
| 214 | : Sidef::Types::String::String->new($input); | ||
| 215 | } | ||
| 216 | |||
| 217 | sub open { | ||
| 218 | my ($self, $var, $mode, $filename) = @_; | ||
| 219 | $filename->to_file->open($mode, $var); | ||
| 220 | } | ||
| 221 | |||
| 222 | sub stdin { | ||
| 223 | Sidef::Types::Glob::FileHandle->stdin; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub stdout { | ||
| 227 | Sidef::Types::Glob::FileHandle->stdout; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub stderr { | ||
| 231 | Sidef::Types::Glob::FileHandle->stderr; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub opendir { | ||
| 235 | my ($self, $var, $dirname) = @_; | ||
| 236 | $dirname->to_dir->open($var); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub eval { | ||
| 240 | my ($self, $code) = @_; | ||
| 241 | Sidef::Types::Perl::Perl->new($code)->eval; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub bless { | ||
| 245 | my ($self, $obj, $class) = @_; | ||
| 246 | CORE::bless $obj, $class; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub refaddr { | ||
| 250 | my ($self, $obj) = @_; | ||
| 251 | Sidef::Types::Number::Number::_set_int(Scalar::Util::refaddr($obj)); | ||
| 252 | } | ||
| 253 | |||
| 254 | sub reftype { | ||
| 255 | my ($self, $obj) = @_; | ||
| 256 | Sidef::Types::String::String->new(Scalar::Util::reftype($obj)); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub weaken { | ||
| 260 | my ($self, $obj) = @_; | ||
| 261 | (Scalar::Util::weaken($obj)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub isweak { | ||
| 265 | my ($self, $obj) = @_; | ||
| 266 | (Scalar::Util::isweak($obj)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub unweaken { | ||
| 270 | my ($self, $obj) = @_; | ||
| 271 | (Scalar::Util::unweaken($obj)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub select { | ||
| 275 | my ($self, $fh) = @_; | ||
| 276 | Sidef::Types::Glob::FileHandle->new(scalar CORE::select(CORE::ref($fh) eq 'GLOB' ? $fh : $fh->get_value)); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub system { | ||
| 280 | my ($self, @args) = @_; | ||
| 281 | Sidef::Types::Number::Number::_set_int(scalar CORE::system(@args)); | ||
| 282 | } | ||
| 283 | |||
| 284 | *run = \&system; | ||
| 285 | |||
| 286 | sub exec { | ||
| 287 | my ($self, @args) = @_; | ||
| 288 | CORE::exec(@args); | ||
| 289 | } | ||
| 290 | |||
| 291 | sub __GETPW__ { | ||
| 292 | my ($self, $name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = @_; | ||
| 293 | $name // return undef; | ||
| 294 | Sidef::Types::Array::Array->new( | ||
| 295 | Sidef::Types::String::String->new($name), Sidef::Types::String::String->new($passwd), | ||
| 296 | Sidef::Types::Number::Number->new($uid), Sidef::Types::Number::Number->new($gid), | ||
| 297 | Sidef::Types::String::String->new($quota), Sidef::Types::String::String->new($comment), | ||
| 298 | Sidef::Types::String::String->new($gcos), Sidef::Types::String::String->new($shell), | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | |||
| 302 | sub setpwent { | ||
| 303 | my ($self) = @_; | ||
| 304 | (CORE::setpwent) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub getpwuid { | ||
| 308 | my ($self, $uid) = @_; | ||
| 309 | $self->__GETPW__(CORE::getpwuid($uid)); | ||
| 310 | } | ||
| 311 | |||
| 312 | sub getpwnam { | ||
| 313 | my ($self, $name) = @_; | ||
| 314 | $self->__GETPW__(CORE::getpwnam($name)); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub getpwent { | ||
| 318 | my ($self) = @_; | ||
| 319 | $self->__GETPW__(CORE::getpwent); | ||
| 320 | } | ||
| 321 | |||
| 322 | sub __GETGR__ { | ||
| 323 | my ($self, $name, $passwd, $gid, $members) = @_; | ||
| 324 | $name // return | ||
| 325 | Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($name), | ||
| 326 | Sidef::Types::String::String->new($passwd), | ||
| 327 | Sidef::Types::Number::Number->new($gid), | ||
| 328 | Sidef::Types::Array::Array->new(map { Sidef::Types::String::String->new($_) } split(' ', $members)), | ||
| 329 | ); | ||
| 330 | } | ||
| 331 | |||
| 332 | sub setgrent { | ||
| 333 | my ($self) = @_; | ||
| 334 | (CORE::setgrent) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 335 | } | ||
| 336 | |||
| 337 | sub getgrent { | ||
| 338 | my ($self) = @_; | ||
| 339 | $self->__GETGR__(CORE::getgrent); | ||
| 340 | } | ||
| 341 | |||
| 342 | sub getgrgid { | ||
| 343 | my ($self, $gid) = @_; | ||
| 344 | $self->__GETGR__(CORE::getgrgid($gid)); | ||
| 345 | } | ||
| 346 | |||
| 347 | sub getgrnam { | ||
| 348 | my ($self, $name) = @_; | ||
| 349 | $self->__GETGR__(CORE::getgrnam($name)); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub __GETHOST__ { | ||
| 353 | my ($self, $name, $aliases, $addrtype, $length, @addrs) = @_; | ||
| 354 | $name // return undef; | ||
| 355 | Sidef::Types::Array::Array->new( | ||
| 356 | Sidef::Types::String::String->new($name), | ||
| 357 | Sidef::Types::String::String->new($aliases), | ||
| 358 | Sidef::Types::String::String->new($addrtype), | ||
| 359 | Sidef::Types::Number::Number->new($length), | ||
| 360 | Sidef::Types::Array::Array->new(map { Sidef::Types::String::String->new($_) } @addrs), | ||
| 361 | ); | ||
| 362 | } | ||
| 363 | |||
| 364 | sub sethostent { | ||
| 365 | my ($self, $stayopen) = @_; | ||
| 366 | (CORE::sethostent($stayopen)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 367 | } | ||
| 368 | |||
| 369 | sub gethostbyaddr { | ||
| 370 | my ($self, $addr, $addrtype) = @_; | ||
| 371 | $self->__GETHOST__(CORE::gethostbyaddr($addr, $addrtype)); | ||
| 372 | } | ||
| 373 | |||
| 374 | sub gethostbyname { | ||
| 375 | my ($self, $name) = @_; | ||
| 376 | $self->__GETHOST__(CORE::gethostbyname($name)); | ||
| 377 | } | ||
| 378 | |||
| 379 | sub gethostent { | ||
| 380 | my ($self) = @_; | ||
| 381 | $self->__GETHOST__(CORE::gethostent); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub __GETNET__ { | ||
| 385 | my ($self, $name, $aliases, $addrtype, $net) = @_; | ||
| 386 | $name // return undef; | ||
| 387 | Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($name), Sidef::Types::String::String->new($aliases), | ||
| 388 | Sidef::Types::String::String->new($addrtype), Sidef::Types::String::String->new($net),); | ||
| 389 | } | ||
| 390 | |||
| 391 | sub setnetent { | ||
| 392 | my ($self, $stayopen) = @_; | ||
| 393 | (CORE::setnetent($stayopen)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 394 | } | ||
| 395 | |||
| 396 | sub getnetbyaddr { | ||
| 397 | my ($self, $addr, $addrtype) = @_; | ||
| 398 | $self->__GETNET__(CORE::getnetbyaddr($addr, $addrtype)); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub getnetbyname { | ||
| 402 | my ($self, $name) = @_; | ||
| 403 | $self->__GETNET__(CORE::getnetbyname($name)); | ||
| 404 | } | ||
| 405 | |||
| 406 | sub getnetent { | ||
| 407 | my ($self) = @_; | ||
| 408 | $self->__GETNET__(CORE::getnetent); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub __GETPROTO__ { | ||
| 412 | my ($self, $name, $aliases, $proto) = @_; | ||
| 413 | $name // return undef; | ||
| 414 | Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($name), | ||
| 415 | Sidef::Types::String::String->new($aliases), | ||
| 416 | Sidef::Types::String::String->new($proto), | ||
| 417 | ); | ||
| 418 | } | ||
| 419 | |||
| 420 | sub setprotoent { | ||
| 421 | my ($self, $stayopen) = @_; | ||
| 422 | (CORE::setprotoent($stayopen)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 423 | } | ||
| 424 | |||
| 425 | sub getprotobyname { | ||
| 426 | my ($self, $name) = @_; | ||
| 427 | $self->__GETPROTO__(CORE::getprotobyname($name)); | ||
| 428 | } | ||
| 429 | |||
| 430 | sub getprotobynumber { | ||
| 431 | my ($self, $num) = @_; | ||
| 432 | $self->__GETPROTO__(CORE::getprotobynumber($num)); | ||
| 433 | } | ||
| 434 | |||
| 435 | sub getprotoent { | ||
| 436 | my ($self) = @_; | ||
| 437 | $self->__GETPROTO__(CORE::getprotoent); | ||
| 438 | } | ||
| 439 | |||
| 440 | sub __GETSERV__ { | ||
| 441 | my ($self, $name, $aliases, $port, $proto) = @_; | ||
| 442 | $name // return undef; | ||
| 443 | Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($name), Sidef::Types::String::String->new($aliases), | ||
| 444 | Sidef::Types::Number::Number->new($port), Sidef::Types::String::String->new($proto),); | ||
| 445 | } | ||
| 446 | |||
| 447 | sub setservent { | ||
| 448 | my ($self, $stayopen) = @_; | ||
| 449 | (CORE::setservent($stayopen)) ? (Sidef::Types::Bool::Bool::TRUE) : (Sidef::Types::Bool::Bool::FALSE); | ||
| 450 | } | ||
| 451 | |||
| 452 | sub getservbyname { | ||
| 453 | my ($self, $name, $proto) = @_; | ||
| 454 | $self->__GETSERV__(CORE::getservbyname($name, $proto)); | ||
| 455 | } | ||
| 456 | |||
| 457 | sub getservbyport { | ||
| 458 | my ($self, $port, $proto) = @_; | ||
| 459 | $self->__GETSERV__(CORE::getservbyport($port, $proto)); | ||
| 460 | } | ||
| 461 | |||
| 462 | sub getservent { | ||
| 463 | my ($self) = @_; | ||
| 464 | $self->__GETSERV__(CORE::getservent); | ||
| 465 | } | ||
| 466 | |||
| 467 | # | ||
| 468 | ## get/set priority | ||
| 469 | # | ||
| 470 | sub getpriority { | ||
| 471 | my ($self, $which, $who) = @_; | ||
| 472 | Sidef::Types::Number::Number->new(CORE::getpriority($which, $who)); | ||
| 473 | } | ||
| 474 | |||
| 475 | sub setpriority { | ||
| 476 | my ($self, $which, $who, $priority) = @_; | ||
| 477 | Sidef::Types::Number::Number->new(CORE::setpriority($which, $who, $priority)); | ||
| 478 | } | ||
| 479 | |||
| 480 | sub getppid { | ||
| 481 | my ($self) = @_; | ||
| 482 | Sidef::Types::Number::Number->new(CORE::getppid); | ||
| 483 | } | ||
| 484 | |||
| 485 | # | ||
| 486 | ## get/set the process group of a process | ||
| 487 | # | ||
| 488 | sub getpgrp { | ||
| 489 | my ($self, $pid) = @_; | ||
| 490 | Sidef::Types::Number::Number->new(CORE::getpgrp(defined($pid) ? $pid : ())); | ||
| 491 | } | ||
| 492 | |||
| 493 | sub setpgrp { | ||
| 494 | my ($self, $pid, $pgrp) = @_; | ||
| 495 | $pid //= 0; | ||
| 496 | $pgrp //= 0; | ||
| 497 | Sidef::Types::Number::Number->new(CORE::setpgrp($pid, $pgrp)); | ||
| 498 | } | ||
| 499 | |||
| 500 | 1 |