source: trunk/IPs/systemC/hierarchy_memory/tty/tty.h @ 2

Last change on this file since 2 was 2, checked in by kane, 17 years ago

Import Morpheo

File size: 2.9 KB
Line 
1#ifndef TTY_H
2#define TTY_H
3#include <fcntl.h>
4#include <stdarg.h>
5#include <stdio.h>
6
7#include <unistd.h>
8#include <signal.h>
9#include <stdint.h>
10#include <iostream>
11#include "xtty.h"
12
13using namespace std;
14
15namespace hierarchy_memory {
16namespace tty          {
17
18  class param_t
19  {
20  public : char   * name;
21  public : uint32_t nb_tty;
22  public : char  ** name_tty;
23  public : bool     with_xtty;
24
25  public : param_t () {};
26
27  public : param_t (char   * name    ,
28                    uint32_t nb_tty  ,
29                    char  ** name_tty,
30                    bool     with_xtty)
31    {
32      this->name     = name;
33      this->nb_tty   = nb_tty;
34      this->name_tty = name_tty;
35      this->with_xtty = with_xtty;
36    }
37  };
38
39  class Tty
40  {
41  private   : char           * name;
42  protected : const uint32_t   nb_tty;
43  private   : const bool       with_xtty;
44  private   : xtty_t         * xtty;
45
46  public : Tty (param_t param):
47    nb_tty    (param.nb_tty),
48    with_xtty  (param.with_xtty)
49    {
50      uint32_t size_name = strlen(param.name)+1;
51      name = new char [size_name];
52      strncpy(name,param.name,size_name);
53
54      xtty = new xtty_t [nb_tty];
55
56      for (uint32_t i = 0; i < nb_tty; i++)
57        {
58          // ***** Create a log_file *****
59          xtty [i].log_file = fopen(param.name_tty[i],"w");
60
61          if (xtty [i].log_file == NULL)
62            {
63              // Error
64              cerr << "<Tty> Can't open the file : \"" << param.name_tty[i] << "\"" << endl;
65              exit(1);
66            }
67
68          if (with_xtty == true)
69            {
70              // ***** TTY and pipe init *****
71              canal canal_output;
72             
73              if (pipe (canal_output.CanalPipe) != 0)
74                {
75                  cerr << "<Tty> Error in pipe creation" << endl;
76                  exit(1);
77                }
78             
79              if ((xtty[i].pid = fork()) == 0)
80                {
81                  // Childen -> transform in a xtty
82                  close(0);
83                  dup (canal_output.CanalPipe [0]);
84                  close(1);
85                  execlp("xtty","xtty", param.name_tty [i] ,NULL); // devient un tty
86                 
87                  cerr << "<Tty> Error in the creation of tty" << endl;
88                  exit (1);
89                }
90              else
91                {
92                  // father
93                  xtty[i].pipe_output = canal_output.CanalPipe [1];
94                }
95            }
96        }//end for
97    }
98
99  public : ~Tty ()
100    {
101      // Kill all tty
102      for (unsigned int i=0; i<nb_tty; i++)
103      {
104        if (with_xtty == true)
105          if (xtty[i].pid)
106            kill(xtty[i].pid,SIGTERM);
107        fclose(xtty[i].log_file);
108      }
109
110    }
111  public : void reset ()
112    {
113    }
114
115    //*****[ write ]*****
116  public : bool write (uint32_t num_tty, char data)
117    {
118      if (num_tty >= nb_tty)
119        return false;
120      char write_data = data;
121
122      if (with_xtty == true)
123        ::write (xtty[num_tty].pipe_output, &write_data, 1);
124      fputc (data, xtty[num_tty].log_file);
125
126      return true;
127    }
128
129    public    : friend ostream& operator<< (ostream& output_stream, const Tty & x)
130      {
131        output_stream << "<" << x.name << ">" << endl;
132        output_stream << " * nb_tty        : " << x.nb_tty      << endl;
133
134        return output_stream;
135      };
136  };
137
138};}; //end namespace
139#endif //!TTY_H
Note: See TracBrowser for help on using the repository browser.