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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include "extern.hh"
#include "files.hh"
#include "superus.hh"
extern char *egetenv(const char *item);
bool lsCmd(void);
bool open_file(DataFile::type type);
bool openCmd(void);
bool
openCmd()
{
if (((unsigned) superuser()) & FLAG_AUT1)printf("\nDEBUG: %s:%d opening file named '%s'. Before doing that, datafile stack_len= %d\n",__FILE__,__LINE__, _word[1], int(_dataFILE.size()));
switch (_nword) {
case 2:
open_file(DataFile::ascii);
break;
case 3:
if (!strcmp(_word[2], "netCDF") || !strcmp(_word[2], "netcdf")) {
#if defined(HAVE_LIBNETCDF)
open_file(DataFile::bin_netcdf);
#else
err("`open ... netCDF' impossible since Gri not compiled with netCDF library");
return false;
#endif
} else if (!strcmp(_word[2], "binary")) {
open_file(DataFile::bin_unknown);
} else {
err("\
`open filename' can't understand final word `\\",
_word[_nword - 1],
"'.",
"\\"
);
return false;
}
break;
case 4: // open filename binary type
if (strcmp(_word[2], "binary")) {
err("\
Third word of `open filename' must be \"binary\", not `\\",
_word[2],
"' as given.",
"\\"
);
return false;
}
if (!strcmp(_word[3], "uchar"))
open_file(DataFile::bin_uchar);
else if (!strcmp(_word[3], "16bit"))
open_file(DataFile::bin_16bit);
else if (!strcmp(_word[3], "int"))
open_file(DataFile::bin_int);
else if (!strcmp(_word[3], "float"))
open_file(DataFile::bin_float);
else if (!strcmp(_word[3], "double"))
open_file(DataFile::bin_double);
else {
err("\
`open filename' can't understand final word `\\",
_word[_nword - 1],
"'.",
"\\"
);
return false;
}
break;
default:
err("`open' needs a file name");
return false;
}
clear_eof_flag_on_data_file();
return true;
}
bool
open_file(DataFile::type type)
{
put_syn("\\.return_value.", "", true);
// Must decode filename, which may have "/" in it which got expanded to "
// / " by expand_blanks().
// If filename is quoted, either read from a pipe (if last nonblank
// character is '|') or just ignore the quotes
if (*_word[1] == '"') {
// Quoted filename. First check to see endquote exists.
remove_esc_quotes(_word[1]);
int len = strlen(_word[1]);
if (len < 2) {
err("`open' needs a proper filename");
return false;
}
if (*(_word[1] + len - 1) != '"') {
err("Missing quote on end of file/pipe name.");
return false;
}
// Now check for a pipe
for (int i = len - 2; i > -1; i--) {
if (*(_word[1] + i) == '|') {
// It's a pipe
std::string pipecmd;
if (type == DataFile::bin_netcdf) {
err("`open' cannot have a pipe that is netCDF type");
return false;
}
// Copy string for system command, and then
// erase regular expression \s*|\s*"$
pipecmd.assign(_word[1] + 1);// skip the initial quote
if (pipecmd[pipecmd.size() - 1] == '"')
pipecmd.STRINGERASE(pipecmd.size() - 1);
while (isspace(pipecmd[pipecmd.size() - 1]))
pipecmd.STRINGERASE(pipecmd.size() - 1);
if (pipecmd[pipecmd.size() - 1] == '|')
pipecmd.STRINGERASE(pipecmd.size() - 1);
while (isspace(pipecmd[pipecmd.size() - 1]))
pipecmd.STRINGERASE(pipecmd.size() - 1);
pipecmd.append(" > ");
std::string tmpfile_name(tmp_file_name());
pipecmd.append(tmpfile_name.c_str());
call_the_OS(pipecmd.c_str(), __FILE__, __LINE__);
if (!push_data_file(tmpfile_name.c_str(), type, "r", true)) {
err("`open' can't find file `\\", tmpfile_name.c_str(), "'", "\\");
return false;
}
break;
} else if (!isspace(*(_word[1] + i))) {
// Presume quoted string ... but check to be sure!
std::string filename;
if (*_word[1] == '"' && *_word[1] != '\0')
filename.assign(1 + _word[1]);
else
filename.assign(_word[1]);
if (filename.size() < 1) {
err("`open' needs a filename; \"\" won't do!");
return false;
}
if (filename[filename.size() - 1] == '"')
filename.STRINGERASE(filename.size() - 1, filename.size());
// Determine actual filename (substituting for ~ etc).
std::string completefilename(filename);
resolve_filename(completefilename, true, 'd');
if (!push_data_file(completefilename.c_str(), type, "r", false)) {
err("`open' can't find file `\\", completefilename.c_str(), "'", "\\");
return false;
}
break;
}
}
} else {
// Normal filename
if (!strlen(_word[1])) {
err("`open' needs a filename");
return false;
}
std::string completefilename(_word[1]);
resolve_filename(completefilename, true, 'd');
if (!push_data_file(completefilename.c_str(), type, "r", false)) {
err("`open' can't find (or successfully open) file `\\", completefilename.c_str(), "' due to system error `", strerror(errno), "'.", "\\");
return false;
}
put_syn("\\.return_value.", completefilename.c_str(), true);
}
return true;
}
bool
cdCmd()
{
#if defined(HAVE_GETENV)
char * home = egetenv("HOME");
switch (_nword) {
case 1:
// `cd' means move to home directory
_current_directory.assign(home);
break;
case 2:
if (!strcmp(_word[1], ".")) {
// "."
_current_directory.assign(".");
} else if (!strncmp(_word[1], "..", 2)) {
// "../LOCATION"
_current_directory.append("/");
_current_directory.append(_word[1]);
} else if (!strcmp(_word[1], "~")) {
// "~"
_current_directory.assign(home);
} else if (!strcmp(_word[1], "$HOME")) {
// "$HOME"
_current_directory.assign(home);
} else if (*_word[1] == '~') {
// "~/LOCATION"
char tmp[1000];
sprintf(tmp, "%s%s", home, _word[1] + 1);
_current_directory.assign(tmp);
} else if (!strncmp(_word[1], "$HOME", 5)) {
// "$HOME/LOCATION"
char tmp[1000];
sprintf(tmp, "%s%s", home, _word[1] + 5);
_current_directory.assign(tmp);
} else if (!strncmp(_word[1], "/", 1)) {
// "/LOCATION"
_current_directory.assign(_word[1]);
} else {
// "LOCATION"
_current_directory.append("/");
_current_directory.append(_word[1]);
}
break;
default:
demonstrate_command_usage();
NUMBER_WORDS_ERROR;
return false;
}
return true;
#else
err("`cd' requires \"environment variables\", which this computer lacks.");
return false;
#endif
}
bool
pwdCmd()
{
// `cd' means move to home directory
gr_textput(_current_directory.c_str());
gr_textput("\n");
return true;
}
bool
lsCmd()
{
#if defined(VMS)
warning("`ls' not implemented in VMS version.");
return true;
#else
// `cd' means move to home directory
char command[100];
switch (_nword) {
case 1:
sprintf(command, "ls -CF %s/", _current_directory.c_str());
call_the_OS(command, __FILE__, __LINE__);
break;
case 2:
sprintf(command, "ls -CF %s/%s", _current_directory.c_str(), _word[1]);
call_the_OS(command, __FILE__, __LINE__);
break;
default:
demonstrate_command_usage();
NUMBER_WORDS_ERROR;
return false;
}
return true;
#endif
}
|