summaryrefslogtreecommitdiff
path: root/lib/io_contr.cc
blob: 3175e88dbc66bfc1313487d112f0ac60bb401171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*$Id: io_contr.cc,v 26.137 2010/04/10 02:37:33 al Exp $ -*- C++ -*-
 * Copyright (C) 2001 Albert Davis
 * Author: Albert Davis <[email protected]>
 *
 * This file is part of "Gnucap", the Gnu Circuit Analysis Package
 *
 * 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 3, 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.
 *------------------------------------------------------------------
 * Sets up output direction and format for most commands
 * updates pointers into the string
 * outreset starts it all over
 */
//testing=script,sparse 2006.07.17
#include "io_.h"
#include "ap.h"
/*--------------------------------------------------------------------------*/
	void	initio(OMSTREAM&);
//static	void	decipher(char*);
	void	outreset(void);
	OMSTREAM* outset(CS&,OMSTREAM*);
static	FILE*	file_open(CS&,const char*,const char*,FILE*);
/*--------------------------------------------------------------------------*/
static FILE* fn;		/* write file				    */
static FILE* to_pipe;
/*--------------------------------------------------------------------------*/
/* initio: initialize file encryption, etc
 */
void initio(OMSTREAM& Where)
{
  const char* tag = "''''";
  if (Where.cipher()) {		/* if writing an encrypted file,    */
    untested();
    Where << tag << '\n';	/* mark it as encrypted		    */
  }else{
  }
#if 0
  if (Whence) {
    char buf[BUFLEN];
    if (!fgets(buf, BUFLEN, Whence))	/* if the first line deciphers to   */
      return;				/* the cipher tag, it is encrypted  */
    IO::incipher = true;		/* otherwise,			    */
    decipher(buf);			/*	 rewind and read normally   */
    if (strcmp(buf,tag) != 0) {		/* mismatch */
      IO::incipher = false;
      fseek(Whence,0L,SEEK_SET);
    }
  }
#endif
}
/*--------------------------------------------------------------------------*/
#if 0
/* decipher: un-encrypt a line of text in place
 */
void decipher(char *buf)
{
  untested();
  if (IO::incipher) {
    for ( ; isprint(buf[1]); buf++ ) {
      int fixed = static_cast<int>(buf[1]) - static_cast<int>(buf[0]);
      while (!isascii(fixed) || !isprint(fixed))
	fixed += (0x7f-0x20);
      buf[0] = static_cast<char>(fixed);
    }
    buf[0] = '\0';
  }else{
  }
  
}
#endif
/*--------------------------------------------------------------------------*/
/* outreset: close files and set i/o flags to standard values
 */
void outreset(void)
{
  if (to_pipe) {
    untested();
    pclose(to_pipe);
    to_pipe = NULL;
  }else{
  }
  xclose(&fn);
  IO::formaat = 0;
  IO::incipher = false;
  IO::mstdout.reset();
}
/*--------------------------------------------------------------------------*/
/* outset: set up i/o for a command
 * return whether or not it did anything
 */
OMSTREAM* outset(CS& cmd, OMSTREAM* out)
{
  bool echo = false;
  for (;;) {
    if (cmd.umatch("basic ")) {
      IO::formaat = ftos_EXP;
      (*out).setformat(ftos_EXP);
    }else if (cmd.umatch("cipher ")) {untested();
      (*out).setcipher().setpack();
    }else if (cmd.umatch("pack ")) {itested();
      (*out).setpack();
    }else if (cmd.umatch("quiet ")) {itested();
      echo = false;
      (*out).detach(IO::mstdout);
    }else if (cmd.umatch("echo ") || cmd.umatch("list ")) {itested();
      echo = true;
      (*out).attach(IO::mstdout);
    }else if (cmd.umatch("save ")) {itested();
      fn = file_open(cmd,"","w",fn);
      (*out).attach(fn);
    }else if (cmd.umatch("\\|")) {untested();
      // open a pipe
      std::string command;
      cmd >> command;
      to_pipe = popen(command.c_str(), "w");
      assert(to_pipe);
      (*out).attach(to_pipe);

      IO::formaat = ftos_EXP;
      (*out).setformat(ftos_EXP);
      if (!echo) {untested();
	(*out).detach(IO::mstdout);
      }else{untested();
      }
    }else if (cmd.umatch(">")) {itested();
      // open a file for write or append
      const char *rwaflag;
      rwaflag = (cmd.umatch(">")) ? "a" : "w";
      fn = file_open(cmd,"",rwaflag,fn);
      (*out).attach(fn);
      IO::formaat = ftos_EXP;
      (*out).setformat(ftos_EXP);
      if (!echo) {itested();
	(*out).detach(IO::mstdout);
      }else{untested();
      }
    }else{
      break;
    }
  }
  return out;
}
/*--------------------------------------------------------------------------*/
/* file_open: a different interface for xopen
 */
static FILE *file_open(
	CS& cmd,
	const char *ext,
	const char *rwaflag,
	FILE *fileptr)
{itested();
  xclose(&fileptr);
  fileptr = xopen(cmd,ext,rwaflag);
  if (!fileptr) {itested();
    throw Exception_File_Open("");
  }else{itested();
  }
  return fileptr;
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// vim:ts=8:sw=2:noet: