Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / system / blaze / term_redirect.cc
// ========== Copyright Header Begin ==========================================
//
// OpenSPARC T2 Processor File: term_redirect.cc
// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
//
// The above named program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License version 2 as published by the Free Software Foundation.
//
// The above named 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 work; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// ========== Copyright Header End ============================================
/*
* Copyright (C) 2001 Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "@(#)1.6 07/11/19 term_redirect.cc"
//
// This file contains an implementation of console I/O
// redirection
//
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <stropts.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <thread.h>
#include "types.h"
#include "blaze_globals.h"
#include "term.h"
#include "ui.h"
#include "fileutil.h"
#define MAXCURSTR 256
#define MAXCTXT 128
////////////////////////////////////////////////
typedef struct term_redirect_t
{
char * script_name;
char current_string [MAXCURSTR];
char str [MAXCTXT];
char *pstr;
char pattern [MAXCTXT];
char *ptail;
char unix_command [MAXCURSTR];
uint32_t idx;
FILE *fp;
cond_t trd_cond;
mutex_t trd_lock;
mutex_t uc_lock;
volatile bool_t unix_command_sent;
bool_t unix_command_nready;
} TermRedirect;
static TermRedirect *ptrd = NULL;
#ifdef __cplusplus
extern "C" {
#endif
void * console_input_start (void *);
#ifdef __cplusplus
}
#endif
////////////////////////////////////////////////
char *term_redirect_get_file ()
{
if (ptrd) {
return ptrd->script_name;
}
else {
return NULL;
}
}
////////////////////////////////////////////////
void * console_input_start (void *arg)
{
int idx = 0;
ui->verbose("console input interceptor started ... \n");
for (;;) {
mutex_lock (&ptrd->trd_lock);
cond_wait(&ptrd->trd_cond, &ptrd->trd_lock);
ptrd->unix_command [idx = strlen(ptrd->unix_command)] = 0xD;
ptrd->unix_command [idx + 1] = 0x0;
sleep (1);
term_fake_input ((uint8_t *)ptrd->unix_command, idx + 1, 0);
mutex_unlock (&ptrd->trd_lock);
mutex_lock (&ptrd->uc_lock);
ptrd->unix_command_sent = TRUE;
mutex_unlock (&ptrd->uc_lock);
}
return NULL;
}
////////////////////////////////////////////////
void init_term_redirect (char *fname)
{
FILE *fp = fopen (fname , "r");
thread_t tid;
if (fp == NULL) {
ui->error("file not found <%s> \n", fname);
return;
}
ptrd = (TermRedirect*) calloc (1, sizeof(TermRedirect));
ptrd->fp = fp;
ptrd->script_name = (char*)strdup(fname);
while (ptrd->pstr = fgets(ptrd->str, MAXCTXT - 2, ptrd->fp)) {
if ((ptrd->ptail = get_substr_until (ptrd->str, ptrd->pattern, ':')) != NULL) {
mutex_init(&ptrd->trd_lock, USYNC_THREAD, (void*)NULL);
mutex_init(&ptrd->uc_lock, USYNC_THREAD, (void*)NULL);
cond_init(&ptrd->trd_cond, USYNC_THREAD, (void *)NULL);
thr_create(NULL, NULL, console_input_start, (void*)ptrd, THR_BOUND, &tid);
return;
}
}
free ((char*)ptrd);
ptrd = NULL;
return;
}
////////////////////////////////////////////////
void term_redirect_add (uint8_t c)
{
char *ps;
if (ptrd) {
if ((ptrd->pstr == NULL)) {
free ((char*)ptrd);
ptrd = NULL;
ui->verbose("Console script is processed; return to terminal \n");
return;
}
if ((c == 0xD) || (c == 0xA) || (c == 0) || (ptrd->idx >= MAXCURSTR - 2)) {
ptrd->idx = 0;
ptrd->current_string[0] = '\0';
}
else {
ptrd->current_string[ptrd->idx++] = c;
ptrd->current_string[ptrd->idx] = '\0';
if ( strstr ((const char *)ptrd->current_string, (const char *)ptrd->pattern)) {
if (!ptrd->unix_command_nready) {
if (get_quoted_substr (ptrd->ptail, ptrd->unix_command)) {
mutex_lock (&ptrd->trd_lock);
cond_signal(&ptrd->trd_cond);
mutex_unlock (&ptrd->trd_lock);
}
ptrd->unix_command_nready = TRUE;
}
if (!ptrd->unix_command_sent) {
return;
}
ptrd->unix_command_nready = FALSE;
ptrd->unix_command_sent = FALSE;
while (ptrd->pstr = fgets (ptrd->str, MAXCTXT - 2, ptrd->fp)) {
if ((ptrd->ptail = get_substr_until (ptrd->str, ptrd->pattern, ':')) != NULL) {
return;
}
}
}
else {
if (ptrd->unix_command_nready) {
ptrd->unix_command_nready = FALSE;
ptrd->unix_command_sent = FALSE;
while (ptrd->pstr = fgets (ptrd->str, MAXCTXT - 2, ptrd->fp)) {
if ((ptrd->ptail = get_substr_until (ptrd->str, ptrd->pattern, ':')) != NULL) {
return;
}
}
}
}
}
}
}
////////////////////////////////////////////////