eSystem
[ class tree: eSystem ] [ index: eSystem ] [ all elements ]

Source for file man.php

Documentation is available at man.php

  1. <?php
  2. /**
  3.  * Project: eSystem:: The Extended file system<br>
  4.  * File:    eSystem/system.php
  5.  *
  6.  * Sub pcakge of eSystem package. This package includes extended system
  7.  * methods.
  8.  *
  9.  * @category   System
  10.  * @package    eSystem
  11.  * @subpackage eSystem_man
  12.  * @author     JoungKyun.Kim <http://oops.org>
  13.  * @copyright  (c) 2018, OOPS.org
  14.  * @license    BSD
  15.  * @link       http://pear.oops.org/package/KSC5601
  16.  * @filesource
  17.  */
  18.  
  19.  
  20. /**
  21.  * include eSystem_system class
  22.  */
  23. require_once 'eSystem/system.php';
  24.  
  25. /**
  26.  * Man contorol class api
  27.  *
  28.  * @package eSystem
  29.  */
  30. class eSystem_man extends eSystem_system
  31. {
  32.     // {{{ properties
  33.     public $tmpdir = "/tmp";
  34.     // }}}
  35.  
  36.     // {{{ function so_man ($_file, $_base, $_int = '') {
  37.     /**
  38.      * Valid real man file
  39.      *
  40.      * @access public
  41.      * @return string path
  42.      * @param  string path of man file
  43.      * @param  string base path of man page
  44.      * @param  string  (optional) L10n code for international man pages
  45.      */
  46.     function so_man ($_file$_base$_int ''{
  47.         $_dotso array ();
  48.  
  49.         if preg_match ('/\.gz$/'$_file) ) {
  50.             $_func 'gzfile';
  51.             $_ext  '.gz';
  52.         else {
  53.             $_func 'file';
  54.             $_ext  '';
  55.         }
  56.  
  57.         if preg_match ('!/man[0-9]+$!'$_base) )
  58.             $_base dirname ($_base);
  59.  
  60.         if file_exists ($_file) )
  61.             return $_file;
  62.  
  63.         $_dotso $_func($_file);
  64.  
  65.         foreach ($_dotso as $_v)
  66.             $dotso .= $_v;
  67.  
  68.         if preg_match ("/\.so (.+)/m"$dotso$_match) )
  69.             $_file "{$_base}/{$_int}{$_match[1]}{$_ext}";
  70.  
  71.         return $_file;
  72.     }
  73.     // }}}
  74.  
  75.     /*
  76.      * User level function
  77.      */
  78.  
  79.     // {{{ function manPath ($_name, $_path = '/usr/share/man', $_sec = 0)
  80.     /**
  81.      * Return man page file path with man page section and name
  82.      *
  83.      * The exmaple:
  84.      * {@example eSystem/test.php 170 2}
  85.      *
  86.      * @access public
  87.      * @return string Returns man page file path
  88.      * @param  string Name of man page for searching
  89.      * @param  string (optional) Base man page base path
  90.      * @param  integer (optional) Section of man page
  91.      */
  92.     function manPath ($_name$_path '/usr/share/man'$_sec 0{
  93.         $_path $_path '/usr/share/man/' $_path;
  94.  
  95.         if $_sec {
  96.             $_f   "{$_path}/man{$_sec}/{$_name}.{$_sec}";
  97.             $_fgz "{$_path}/man{$_sec}/{$_name}.{$_sec}.gz";
  98.  
  99.             if file_exists ($_f) )
  100.                 return $_f;
  101.             elseif file_exists ($_fgz) )
  102.                 return $_fgz;
  103.         else {
  104.             $_fa array();
  105.             $_name preg_quote ($_name);
  106.             $_fa eFilesystem::find ($_path"!/{$_name}\.[0-9](\.gz)*$!");
  107.             $_fac count ($_fa);
  108.  
  109.             if $_fac )
  110.                 return ($_fac $_fa $_fa[0];
  111.         }
  112.  
  113.         return '';
  114.     }
  115.     // }}}
  116.  
  117.     // {{{ function man ($_name, $_no, $_int = NULL, $__base = null, $_s = false)
  118.     /**
  119.      * Return man page contents for human readable
  120.      *
  121.      * @access public
  122.      * @return string Returns man page file path
  123.      * @param  string  name of man page
  124.      * @param  int     Section of man page
  125.      * @param  string  (optional) L10n code for international man pages
  126.      * @param  string  (optional) Base man page base path
  127.      * @param  boolean (optional) Defaults to 0. Set true, even if result
  128.      *                  is array, force convert plain text strings.
  129.      */
  130.     function man ($_name$_no$_int NULL$__base null$_s false{
  131.         if extension_loaded ("zlib")) {
  132.             echo "Error: man function requires zlib extension!\n";
  133.             exit (1);
  134.         }
  135.  
  136.         $__base  $__base $__base '/usr/share/man';
  137.         $_mdir   "man{$_no}";
  138.         $_man    "{$_name}.{$_no}";
  139.         $_int    $_int "{$_int}/'';
  140.  
  141.         $_base   "{$__base}/{$_int}{$_mdir}";
  142.         $_file   "{$_base}/{$_man}";
  143.         $_gzfile "{$_base}/{$_man}.gz";
  144.  
  145.         if file_exists ('/usr/bin/nroff') )
  146.             $mancmd '/usr/bin/nroff -man';
  147.         else
  148.             $mancmd '/usr/bin/groff -S -Wall -mtty-char -Tascii -man';
  149.  
  150.         if file_exists ($_gzfile) ) {
  151.             $_gzfile $this->so_man ($_gzfile$__base$_int);
  152.             $_gz array ();
  153.  
  154.             if file_exists ($_gzfile) )
  155.                 return '';
  156.  
  157.             $_gz gzfile ($_gzfile);
  158.  
  159.             foreach ($_gz as $_v)
  160.                 $_gztmp .= $_v;
  161.  
  162.             $tmpfile tempnam ($this->tmpdir"man-");
  163.             if @file_put_contents ($tmpfile$_gztmp=== false {
  164.                 unlink ($tmpfile);
  165.                 echo "Error: Can't write $tmpfile\n";
  166.                 exit (1);
  167.             }
  168.  
  169.             $this->_system ("$mancmd $tmpfile");
  170.             $_r $this->_stdout;
  171.             unlink ($tmpfile);
  172.         elseif file_exists ($_file) ) {
  173.             $_file $this->so_man ($_file$__base$_int);
  174.             $this->_system ("$mancmd $_file");
  175.             $_r $this->_stdout;
  176.         else
  177.             return '';
  178.  
  179.         if $_s {
  180.             if is_array ($_r) ) {
  181.                 foreach ($_r as $_v )
  182.                     $v .= $_v ."\n";
  183.             }
  184.             return $v;
  185.         }
  186.  
  187.         return $_r;
  188.     }
  189.     // }}}
  190. }
  191.  
  192. /*
  193.  * Local variables:
  194.  * tab-width: 4
  195.  * c-basic-offset: 4
  196.  * End:
  197.  * vim600: noet sw=4 ts=4 fdm=marker
  198.  * vim<600: noet sw=4 ts=4
  199.  */
  200. ?>

Documentation generated on Tue, 14 May 2019 02:00:31 +0900 by phpDocumentor 1.4.4