iB2JR

            nano (O) optimist vs nano (P)essimist.

            O: Accidentally found nice shortcut:
               Alt + # (Alt + Shift + 3). {1}
            P: usually Ctrl + _ means "go to the line.." 

            O: My version of nano has an option to switch 
               to regex search with the meta character + R. {2}
            P: Never say that again..

            ------
            {1} https://askubuntu.com/questions/73444
                /how-to-show-line-numbering-in-nano-when-opening-a-file
            {2} https://code.i-harness.com/en/q/762fff

## PHP. ОБЩАЕМСЯ С ОПЕРАЦИОННОЙ СИСТЕМОЙ.

Повторюсь снова..

  $ sudo sed -i 's/^display_errors = Off/display_errors = On/' \
    /etc/php/7.2/apache2/php.ini

  $ sudo sed -i 's/g = E_ALL & ~E_DEPRECATED & ~E_STRICT/g = E_ALL/' \
    /etc/php/7.2/apache2/php.ini

  $ cat /etc/php/7.2/apache2/php.ini | grep '^display_err\|^err'

  error_reporting = E_ALL
  display_errors = On

  $ sudo systemctl restart apache2

  $ cat /var/www/html/index.php

  <!DOCTYPE html>
  <html>
    <head>
      <title>index.php</title>
      <meta charset="utf-8">
    </head>
    <body>
      <?php
        $s= ( isset($_POST['submit']) ? $_POST['mydata'] : "");
      ?>
      <form method="post" action="index.php">
          <input name="mydata" type="text" class="form-control"
              value='<?php echo "$s";?>'
              style="width: 150px; display: inline" autofocus />
          <input name="submit" type="submit" value="Run"/>
      </form>
      <?php require_once('/home/virtu/exercise.php');?>
  </html>

  $ cat ~/exercise.php

  <?php
  $n = (int) $s;
  $ss = "";
  $summa = 0;
  for ($i = 1; $i <= $n; $i++) {
      $summa = $summa + $i;
      $ss=$ss.($i==1?"":"+").(string)$i;
  }
  echo $ss."=".(string)$summa.PHP_EOL;
  ?>

Замечу, что exercise.php создан автоматическим конвертированием из блок-схемы, нарисованной в Flowgorithm:

  function Main
    declare String s
    declare Integer n
    input s
    assign n = ToInteger(s)
    declare String ss
    assign ss = ""
    declare Integer summa
    assign summa = 0
    declare Integer i
    for i = 1 to n
        assign summa = summa + i
        assign ss = ss & ToString(i)
        if i != n
            assign ss = ss & " + "
        end if
    end for
    output ss & " = " & ToString( summa)
  end function

Koнвертируем в PHP

  Tools -> Source Code Viewer -> PHP

  <?php
  $n = (int) $s;
  $ss = "";
  $summa = 0;
  for ($i = 1; $i <= $n; $i++) {
      $summa = $summa + $i;
      $ss = $ss . (string) $i;
      if ($i != $n) {
          $ss = $ss . " + ";
      }
  }
  echo $ss . " = " . (string) $summa . PHP_EOL;
  ?>

(Код в начале этой заметки подправлен вручную, каюсь)

Сохраним наше первое упражнение на память (сокращение arp от слов арифметическая прогрессия):

  $ mv exercise.php ex_arp.php

# Упражнение 2. A кто там у нас странички-то выдает?

  $ cat exercise.php
  <?php
    function esystem( $as) {
      echo "$as:\n"; system( $as);
    }

    echo '<pre>';
    esystem('whoami');
    esystem('pwd');
    esystem('tree');
    echo '</pre>';
  ?>

Выводит:

  whoami:
  www-data
  pwd:
  /var/www/html
  tree:
  .
  |-- apache2.html
  |-- index.php
  |-- phpinfo.php
  |-- upload
  |-- upload.php
  `-- upload_ac.php

  1 directory, 5 files

# Упражнение 3. Команды

  $ cat exercise.php
  <?php
    function esystem( $as) {
      $trans = array("<" => "&lt;", ">" => "&gt;");
      echo strtr( $as, $trans).":\n";
      system( $as);
    }
    echo '<pre>';
    esystem($s);
    echo '</pre>';
  ?>

Спросим, например:

  echo "<?php echo \"Hiii\n\";?>" | php:

Нам ответят:

  echo "<?php echo \"Hiii\n\";?>" | php:
  Hiii

# ПРО FLOWGORITHM

I. Листья: comment, declare, assign, input, output.

Header:

  <?xml version="1.0"?>
  <flowgorithm fileversion="2.11">
    <attributes>
      <attribute name="name" value=""/>
      <attribute name="authors" value=""/>
      <attribute name="about" value=""/>
      <attribute name="saved" value=""/>
      <attribute name="created" value=""/>
      <attribute name="edited" value=""/>
    </attributes>

If you see "function Main" generate:

    <function name="Main" type="None" variable="">
        <parameters/>
        <body>

After line "function Main" comes it's body:

        <comment text="LEAF: DECLARE"/>
        <declare name="intV" type="Integer" array="False" size=""/>
        <declare name="intaV" type="Integer" array="True" size="5"/>
        <declare name="reV" type="Real" array="False" size=""/>
        <declare name="reaV" type="Real" array="True" size="6"/>
        <declare name="strV" type="String" array="False" size=""/>
        <declare name="straV" type="String" array="True" size="7"/>
        <declare name="booV" type="Boolean" array="False" size=""/>
        <declare name="booaV" type="Boolean" array="True" size="8"/>
        <comment text="LEAF: ASSIGN"/>
        <assign variable="intV" expression="256"/>
        <assign variable="intaV[0]" expression="0"/>
        <assign variable="reV" expression="53.17"/>
        <assign variable="reaV[0]" expression="0.1"/>
        <assign variable="strV" expression="&quot;Hi&quot;"/>
        <assign variable="straV[0]" expression="&quot;zero&quot;"/>
        <assign variable="booV" expression="true"/>
        <assign variable="booaV[0]" expression="false"/>
        <comment text="LEAF: INPUT"/>
        <input variable="intV"/>
        <comment text="LEAF: OUTPUT"/>
        <output expression="intV" newline="True"/>
        <output expression="strV" newline="True"/>
        <output expression="&quot;Bye&quot;" newline="True"/>

If you see "end function" generate:

        </body>
    </function>

Footer:

  </flowgorithm>

II. Еще есть листок LEAF: CALL , с которым все не так просто, так как он сработает только если есть соответствующий блок в XML файле. Наш конвертер будет вести себя так (тут есть что пропарсить):

  ..
  <function name="Main" type="None" variable="">
    <parameters/>
    <body>
      <comment text="LEAF: CALL"/>
      <call expression="callMe( &quot;Hello&quot; )"/>
    </body>
  </function>

If you see "call callMe( "Hello"). Generate:

  <function name="callMe" type="None" variable="">
    <parameters>
      <parameter name="strS" type="String" array="False"/>
    </parameters>
    <body>

If you see end function, generate:

    </body>
  </function>

If you see "call callMeAndReturn( "Hello"). Generate:

  <function name="giveMe" type="Integer" variable="intRet">
    <parameters>
      <parameter name="strS" type="String" array="False"/>
    </parameters>
    <body>
      <declare name="intRet" type="Integer" array="False" size=""/>
      <output expression="strS" newline="True"/>
      ..

III. Наконец, есть три исправных контейнера: "if", "while", "for" и один неисправный "do". Неисправным -- не пользоваться. Вот программа, где есть все девять строительных блоков и четыре типа данных:

    # comment,     boolean
      declare,     integer
      assign,      real
      input,       string
      output,
      call,
      if,
      while,
      for

Сохраняемый файл "all.fprg" имеет следующий вид:

  <?xml version="1.0"?>
  <flowgorithm fileversion="2.11">
    <attributes>
      <attribute name="name" value=""/>
      <attribute name="authors" value=""/>
      <attribute name="about" value=""/>
      <attribute name="saved" value=""/>
      <attribute name="created" value=""/>
      <attribute name="edited" value=""/>
    </attributes>

    <function name="Main" type="None" variable="">
      <parameters/>
      <body>

        <comment text="LEAF: DECLARE"/>

        <declare name="intV" type="Integer" array="False" size=""/>
        <declare name="intaV" type="Integer" array="True" size="5"/>
        <declare name="reV" type="Real" array="False" size=""/>
        <declare name="reaV" type="Real" array="True" size="6"/>
        <declare name="strV" type="String" array="False" size=""/>
        <declare name="straV" type="String" array="True" size="7"/>
        <declare name="booV" type="Boolean" array="False" size=""/>
        <declare name="booaV" type="Boolean" array="True" size="8"/>

        <comment text="LEAF: ASSIGN"/>

        <assign variable="intV" expression="256"/>
        <assign variable="intaV[0]" expression="0"/>
        <assign variable="reV" expression="53.17"/>
        <assign variable="reaV[0]" expression="0.1"/>
        <assign variable="strV" expression="&quot;Hi&quot;"/>
        <assign variable="straV[0]" expression="&quot;zero&quot;"/>
        <assign variable="booV" expression="true"/>
        <assign variable="booaV[0]" expression="false"/>

        <comment text="LEAF: INPUT"/>

        <input variable="intV"/>

        <comment text="LEAF: OUTPUT"/>

        <output expression="intV" newline="True"/>
        <output expression="strV" newline="True"/>
        <output expression="&quot;Bye&quot;" newline="True"/>

        <comment text="LEAF: CALL"/>

        <call expression="callMe( &quot;Hello&quot; )"/>
        <assign variable="intaV[0]"
            expression="callMeAndReturn(&quot;Bye&quot;)"/>

        <comment text="CONTAINER: IF"/>

        <if expression="intV &gt; 0"> <then>
            <output expression="&quot;True&quot;"
              newline="True"/>
          </then> <else>
            <output expression="&quot;False&quot;" newline="True"/>
        </else> </if>

        <comment text="CONTAINER: WHILE"/>

        <while expression="intV &gt; 0">
          <output expression=
            "&quot;Still In While &quot; &amp; ToString( intV)"
            newline="True"/>
          <assign variable="intV" expression="intV - 1"/>
        </while>

        <comment text="CONTAINER: FOR"/>

        <for variable="intV" start="0" end="2" direction="inc" step="1">
          <output expression=
            "ToString( intV) &amp; &quot; times in For&quot;"
            newline="True"/>
        </for>

      </body>
    </function>

    <function name="callMe" type="None" variable="">
      <parameters>
        <parameter name="strS" type="String" array="False"/>
      </parameters>
      <body>
        <output expression="strS" newline="True"/>
    </body> </function>

    <function name="callMeAndReturn" type="Integer" variable="intX">
      <parameters>
        <parameter name="strS" type="String" array="False"/>
      </parameters>
      <body>
        <declare name="intX" type="Integer" array="False" size=""/>
        <output expression="strS" newline="True"/>
    </body> </function>

  </flowgorithm>

На языке "Flo" это будет выглядеть так:

  function Main
  begin

    # LEAF: DECLARE

    declare int intV
    declare int intaV[5]
    declare real reV
    declare real reaV[6]
    declare string strV
    declare string straV[7]
    declare boolean booV
    declare boolean booaV[8]

    # LEAF: ASSIGN

    assign intV = 256
    assign intaV[0] = 0
    assign reV = 53.17
    assign reaV[0] = 0.1
    assign strV = "Hi"
    assign straV[0] = "zero"
    assign booV = true
    assign booaV[0] = false

    # LEAF: INPUT

    input intV

    # LEAF: OUTPUT

    output intV
    output strV
    output "Bye"

    # LEAF: CALL

    call callMe("Hello")
    assign intaV[0] = callMeAndReturn("Bye")

    # CONTAINER: IF

    if intV > 0
      output "TRUE"
    else
      output "FALSE"
    end if

    # CONTAINER: WHILE

    while intV > 0
      output "Still in While" & ToString( intV)
      assign intV = intV - 1
    end while

    # CONTAINER: FOR

    for intV = 0 to 2
      output ToString( intV) & " times in For"
    end for;

  end function

  function callMe
  var
    string strS
  end var
  begin
    output strS
  end function

  function callMeAndReturn
  var
    string strS
  end var
  begin
    declare int intX
    output strS
  end function


Таблица конвертаций

  HEADER                      <?xml version="1.0"?>
                                <flowgorithm fileversion="2.11">
                                <attributes>
                                  <attribute name="name" value=""/>
                                  <attribute name="authors" value=""/>
                                  <attribute name="about" value=""/>
                                  <attribute name="saved" value=""/>
                                  <attribute name="created" value=""/>
                                  <attribute name="edited" value=""/>
                                </attributes>

    function Main               <function name="Main" type="None" variable="">
    begin                       <parameters/> <body> <!-- last tag function -->

    # LEAF: DECLARE             <comment text="LEAF: DECLARE"/>

    declare int intV            <declare name="intV" type="Integer" array="False" size=""/>
    declare int intaV[5]        <declare name="intaV" type="Integer" array="True" size="5"/>
    declare real reV            <declare name="reV" type="Real" array="False" size=""/>
    declare real reaV[6]        <declare name="reaV" type="Real" array="True" size="6"/>
    declare string strV         <declare name="strV" type="String" array="False" size=""/>
    declare string straV[7]     <declare name="straV" type="String" array="True" size="7"/>
    declare boolean booV        <declare name="booV" type="Boolean" array="False" size=""/>
    declare boolean booaV[8]    <declare name="booaV" type="Boolean" array="True" size="8"/>

    # LEAF: ASSIGN              <comment text="LEAF: ASSIGN"/>

    assign intV = 256           <assign variable="intV" expression="256"/>
    assign intaV[0] = 0         <assign variable="intaV[0]" expression="0"/>
    assign reV = 53.17          <assign variable="reV" expression="53.17"/>
    assign reaV[0] = 0.1        <assign variable="reaV[0]" expression="0.1"/>
    assign strV = "Hi"          <assign variable="strV" expression="&quot;Hi&quot;"/>
    assign straV[0] = "zero"    <assign variable="straV[0]" expression="&quot;zero&quot;"/>
    assign booV = true          <assign variable="booV" expression="true"/>
    assign booaV[0] = false     <assign variable="booaV[0]" expression="false"/>

    # LEAF: INPUT               <comment text="LEAF: INPUT"/>

    input intV                  <input variable="intV"/>

    # LEAF: OUTPUT              <comment text="LEAF: OUTPUT"/>

    output intV                 <output expression="intV" newline="True"/>
    output strV                 <output expression="strV" newline="True"/>
    output "Bye"                <output expression="&quot;Bye&quot;" newline="True"/>

    # LEAF: CALL                <comment text="LEAF: CALL"/>

    call callMe("Hello")        <call expression="callMe( &quot;Hello&quot; )"/>
    assign intaV[0] = \         <assign variable="intaV[0]"
        callMeAndReturn("Bye")      expression="callMeAndReturn(&quot;Bye&quot;)"/>

    # CONTAINER: IF             <comment text="CONTAINER: IF"/>

    if intV > 0                 <if expression="intV &gt; 0"> <then>
      output "TRUE"                 <output expression="&quot;True&quot;" newline="True"/>
    else                        </then> <else>
      output"FALSE"                 <output expression="&quot;False&quot;" newline="True"/>
    end if                      </else> </if>

    # CONTAINER: WHILE          <comment text="CONTAINER: WHILE"/>

    while intV > 0              <while expression="intV &gt; 0">
      out ": "                    <output expression="&quot;: &quot;" newline = "False"/>
      outln "Still in While" \    <output expression=
          & ToString( intV)           "&quot;Still In While &quot; &amp; ToString( intV)" newline = "True"/>
      assign intV = intV - 1;      <assign variable="intV" expression="intV - 1"/>
    end while                    </while>

    # CONTAINER: FOR            <comment text="CONTAINER: FOR"/>

    for intV = 0 to 2           <for variable="intV" start="0" end="2" direction="inc" step="1">
      outln ToString( intV) \     <output expression="ToString( intV) &amp; &quot; times in For&quot;"
      & " times in For&quot;"     newline="True"/>
    end for                     </for>

  end function                  </body> </function>

  function callMe             <function name="callMe" type="None" variable="">
  var                             <parameters>
    string strS                   <parameter name="strS" type="String" array="False"/>
  end var                         </parameters>
  begin                           <body>  <!-- last tag: end var -->
    output strS                      <output expression="strS" newline="True"/>
  end function                 </body> </function>

  function callMeAndReturn     <function name="callMeAndReturn" type="Integer" variable="intX">
  var                              <parameters>
    string strS;                   <parameter name="strS" type="String" array="False"/>
  end var                              </parameters>
  begin                            <body>  <!-- last tag: end var -->
    declare int intX               <declare name="intX" type="Integer" array="False" size=""/>
    output strS                    <output expression="strS" newline="True"/>
  end function                 </body> </function>

  FOOTER                     </flowgorithm>


