(********************************************************************)
(*                                                                  *)
(*  pcs_act.s7i   Generate code for actions of process commands.    *)
(*  Copyright (C) 1990 - 1994, 2004 - 2016  Thomas Mertes           *)
(*                                                                  *)
(*  This file is part of the Seed7 compiler.                        *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const ACTION: PCS_CHILD_STDERR is action "PCS_CHILD_STDERR";
const ACTION: PCS_CHILD_STDIN  is action "PCS_CHILD_STDIN";
const ACTION: PCS_CHILD_STDOUT is action "PCS_CHILD_STDOUT";
const ACTION: PCS_CMP          is action "PCS_CMP";
const ACTION: PCS_CPY          is action "PCS_CPY";
const ACTION: PCS_CREATE       is action "PCS_CREATE";
const ACTION: PCS_DESTR        is action "PCS_DESTR";
const ACTION: PCS_EMPTY        is action "PCS_EMPTY";
const ACTION: PCS_EQ           is action "PCS_EQ";
const ACTION: PCS_EXIT_VALUE   is action "PCS_EQ";
const ACTION: PCS_HASHCODE     is action "PCS_HASHCODE";
const ACTION: PCS_IS_ALIVE     is action "PCS_IS_ALIVE";
const ACTION: PCS_KILL         is action "PCS_KILL";
const ACTION: PCS_NE           is action "PCS_NE";
const ACTION: PCS_PIPE2        is action "PCS_PIPE2";
const ACTION: PCS_PTY          is action "PCS_PTY";
const ACTION: PCS_START        is action "PCS_START";
const ACTION: PCS_STR          is action "PCS_STR";
const ACTION: PCS_VALUE        is action "PCS_VALUE";
const ACTION: PCS_WAIT_FOR     is action "PCS_WAIT_FOR";


const proc: pcs_prototypes (inout file: c_prog) is func

  begin
    declareExtern(c_prog, "fileType    pcsChildStdErr (const const_processType);");
    declareExtern(c_prog, "fileType    pcsChildStdIn (const const_processType);");
    declareExtern(c_prog, "fileType    pcsChildStdOut (const const_processType);");
    declareExtern(c_prog, "intType     pcsCmp (const const_processType, const const_processType);");
    declareExtern(c_prog, "intType     pcsCmpGeneric (const genericType, const genericType);");
    declareExtern(c_prog, "void        pcsCpy (processType *const, const processType);");
    declareExtern(c_prog, "void        pcsCpyGeneric (genericType *const, const genericType);");
    declareExtern(c_prog, "processType pcsCreate (const processType);");
    declareExtern(c_prog, "genericType pcsCreateGeneric (const genericType);");
    declareExtern(c_prog, "void        pcsDestr (const processType);");
    declareExtern(c_prog, "void        pcsDestrGeneric (const genericType);");
    declareExtern(c_prog, "boolType    pcsEq (const const_processType, const const_processType);");
    declareExtern(c_prog, "intType     pcsExitValue (const const_processType);");
    declareExtern(c_prog, "intType     pcsHashCode (const const_bigIntType);");
    declareExtern(c_prog, "intType     pcsHashCodeGeneric (const genericType);");
    declareExtern(c_prog, "boolType    pcsIsAlive (const processType);");
    declareExtern(c_prog, "void        pcsKill (const processType);");
    declareExtern(c_prog, "void        pcsPipe2 (const const_striType, const const_arrayType, fileType *, fileType *);");
    declareExtern(c_prog, "void        pcsPty (const const_striType, const const_arrayType, fileType *, fileType *);");
    declareExtern(c_prog, "processType pcsStart (const const_striType, const const_arrayType, fileType, fileType, fileType);");
    declareExtern(c_prog, "striType    pcsStr (const const_processType);");
    declareExtern(c_prog, "processType pcsValue (const const_objRefType);");
    declareExtern(c_prog, "void        pcsWaitFor (const processType);");
  end func;


const proc: process (PCS_CHILD_STDERR, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_file_result(c_expr);
    c_expr.result_expr &:= "pcsChildStdErr(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_CHILD_STDIN, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_file_result(c_expr);
    c_expr.result_expr &:= "pcsChildStdIn(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_CHILD_STDOUT, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_file_result(c_expr);
    c_expr.result_expr &:= "pcsChildStdOut(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_CMP, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsCmp(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_CPY, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: statement is expr_type.value;
    var expr_type: c_param1 is expr_type.value;
    var expr_type: c_param3 is expr_type.value;
  begin
    statement.temp_num := c_expr.temp_num;
    prepareAnyParamTemporarys(params[1], c_param1, statement);
    c_param3.demand := ASSIGN_RESULT;
    prepareAnyParamTemporarys(params[3], c_param3, statement);
    if c_param3.result_expr <> "" then
      statement.temp_decls &:= "processType new_process;\n";
      statement.expr &:= "new_process=";
      statement.expr &:= c_param3.result_expr;
      statement.expr &:= ";\n";
      if isNormalVariable(params[1]) then
        statement.expr &:= "pcsDestr(";
        statement.expr &:= c_param1.expr;
        statement.expr &:= ");\n";
        statement.expr &:= c_param1.expr;
        statement.expr &:= "=new_process;\n";
      else
        statement.temp_decls &:= "processType *process_ptr=&(";
        statement.temp_decls &:= c_param1.expr;
        statement.temp_decls &:= ");\n";
        statement.expr &:= "pcsDestr(*process_ptr);\n";
        statement.expr &:= "*process_ptr=new_process;\n";
      end if;
    else
      statement.expr &:= "pcsCpy(&(";
      statement.expr &:= c_param1.expr;
      statement.expr &:= "), ";
      statement.expr &:= c_param3.expr;
      statement.expr &:= ");\n";
    end if;
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


const proc: process (PCS_EQ, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsEq(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_EXIT_VALUE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsExitValue(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_HASHCODE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsHashCode(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_IS_ALIVE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsIsAlive(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_KILL, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "pcsKill(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (PCS_NE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "!pcsEq(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_PIPE2, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsPipe2(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[4], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (PCS_PTY, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsPty(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[4], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (PCS_START, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_process_result(c_expr);
    c_expr.result_expr := "pcsStart(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getAnyParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[3], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[4], c_expr);
    c_expr.result_expr &:= ", ";
    getStdParamToResultExpr(params[5], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_STR, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "pcsStr(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_VALUE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsValue(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;



const proc: process (PCS_WAIT_FOR, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "pcsWaitFor(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;