Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / system / blaze / term_redirect.cc
CommitLineData
920dae64
AT
1// ========== Copyright Header Begin ==========================================
2//
3// OpenSPARC T2 Processor File: term_redirect.cc
4// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
6//
7// The above named program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public
9// License version 2 as published by the Free Software Foundation.
10//
11// The above named program is distributed in the hope that it will be
12// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public
17// License along with this work; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19//
20// ========== Copyright Header End ============================================
21/*
22 * Copyright (C) 2001 Sun Microsystems, Inc.
23 * All rights reserved.
24 */
25#pragma ident "@(#)1.6 07/11/19 term_redirect.cc"
26
27//
28// This file contains an implementation of console I/O
29// redirection
30//
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <assert.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <strings.h>
40#include <stropts.h>
41#include <unistd.h>
42#include <sys/wait.h>
43#include <signal.h>
44#include <thread.h>
45
46#include "types.h"
47#include "blaze_globals.h"
48#include "term.h"
49#include "ui.h"
50#include "fileutil.h"
51
52#define MAXCURSTR 256
53#define MAXCTXT 128
54
55////////////////////////////////////////////////
56
57typedef struct term_redirect_t
58{
59 char * script_name;
60 char current_string [MAXCURSTR];
61 char str [MAXCTXT];
62 char *pstr;
63 char pattern [MAXCTXT];
64 char *ptail;
65 char unix_command [MAXCURSTR];
66 uint32_t idx;
67 FILE *fp;
68 cond_t trd_cond;
69 mutex_t trd_lock;
70
71 mutex_t uc_lock;
72 volatile bool_t unix_command_sent;
73 bool_t unix_command_nready;
74
75} TermRedirect;
76
77static TermRedirect *ptrd = NULL;
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82 void * console_input_start (void *);
83#ifdef __cplusplus
84}
85#endif
86
87////////////////////////////////////////////////
88
89char *term_redirect_get_file ()
90{
91 if (ptrd) {
92 return ptrd->script_name;
93 }
94 else {
95 return NULL;
96 }
97}
98
99////////////////////////////////////////////////
100
101void * console_input_start (void *arg)
102{
103 int idx = 0;
104
105 ui->verbose("console input interceptor started ... \n");
106
107 for (;;) {
108 mutex_lock (&ptrd->trd_lock);
109 cond_wait(&ptrd->trd_cond, &ptrd->trd_lock);
110 ptrd->unix_command [idx = strlen(ptrd->unix_command)] = 0xD;
111 ptrd->unix_command [idx + 1] = 0x0;
112 sleep (1);
113 term_fake_input ((uint8_t *)ptrd->unix_command, idx + 1, 0);
114 mutex_unlock (&ptrd->trd_lock);
115
116 mutex_lock (&ptrd->uc_lock);
117 ptrd->unix_command_sent = TRUE;
118 mutex_unlock (&ptrd->uc_lock);
119
120 }
121 return NULL;
122}
123
124////////////////////////////////////////////////
125
126void init_term_redirect (char *fname)
127{
128 FILE *fp = fopen (fname , "r");
129 thread_t tid;
130
131 if (fp == NULL) {
132 ui->error("file not found <%s> \n", fname);
133 return;
134 }
135 ptrd = (TermRedirect*) calloc (1, sizeof(TermRedirect));
136 ptrd->fp = fp;
137 ptrd->script_name = (char*)strdup(fname);
138
139 while (ptrd->pstr = fgets(ptrd->str, MAXCTXT - 2, ptrd->fp)) {
140 if ((ptrd->ptail = get_substr_until (ptrd->str, ptrd->pattern, ':')) != NULL) {
141 mutex_init(&ptrd->trd_lock, USYNC_THREAD, (void*)NULL);
142 mutex_init(&ptrd->uc_lock, USYNC_THREAD, (void*)NULL);
143 cond_init(&ptrd->trd_cond, USYNC_THREAD, (void *)NULL);
144 thr_create(NULL, NULL, console_input_start, (void*)ptrd, THR_BOUND, &tid);
145 return;
146 }
147 }
148
149 free ((char*)ptrd);
150 ptrd = NULL;
151 return;
152}
153
154////////////////////////////////////////////////
155
156void term_redirect_add (uint8_t c)
157{
158 char *ps;
159
160 if (ptrd) {
161
162 if ((ptrd->pstr == NULL)) {
163 free ((char*)ptrd);
164 ptrd = NULL;
165 ui->verbose("Console script is processed; return to terminal \n");
166 return;
167 }
168
169 if ((c == 0xD) || (c == 0xA) || (c == 0) || (ptrd->idx >= MAXCURSTR - 2)) {
170 ptrd->idx = 0;
171 ptrd->current_string[0] = '\0';
172 }
173 else {
174 ptrd->current_string[ptrd->idx++] = c;
175 ptrd->current_string[ptrd->idx] = '\0';
176
177 if ( strstr ((const char *)ptrd->current_string, (const char *)ptrd->pattern)) {
178
179 if (!ptrd->unix_command_nready) {
180 if (get_quoted_substr (ptrd->ptail, ptrd->unix_command)) {
181 mutex_lock (&ptrd->trd_lock);
182 cond_signal(&ptrd->trd_cond);
183 mutex_unlock (&ptrd->trd_lock);
184 }
185 ptrd->unix_command_nready = TRUE;
186 }
187
188 if (!ptrd->unix_command_sent) {
189 return;
190 }
191
192 ptrd->unix_command_nready = FALSE;
193 ptrd->unix_command_sent = FALSE;
194
195 while (ptrd->pstr = fgets (ptrd->str, MAXCTXT - 2, ptrd->fp)) {
196 if ((ptrd->ptail = get_substr_until (ptrd->str, ptrd->pattern, ':')) != NULL) {
197 return;
198 }
199 }
200 }
201 else {
202 if (ptrd->unix_command_nready) {
203 ptrd->unix_command_nready = FALSE;
204 ptrd->unix_command_sent = FALSE;
205 while (ptrd->pstr = fgets (ptrd->str, MAXCTXT - 2, ptrd->fp)) {
206 if ((ptrd->ptail = get_substr_until (ptrd->str, ptrd->pattern, ':')) != NULL) {
207 return;
208 }
209 }
210 }
211 }
212 }
213 }
214}
215
216////////////////////////////////////////////////
217
218
219
220
221
222