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
|
#include <string>
#include <math.h>
#include <stdio.h>
#include "gr.hh"
#include "extern.hh"
#include "private.hh"
#include "superus.hh"
bool insertCmd(void);
extern char _grTempString[];
bool
insertCmd()
{
int unsigned i = 0;
char *filename;
char *longfilename;
FILE *fp;
GET_STORAGE(filename, char, LineLength);
GET_STORAGE(longfilename, char, LineLength);
switch (_nword) {
case 2:
while (*(_cmdLine + i) != ' ' && *(_cmdLine + i) != '\0')
i++;
while (*(_cmdLine + i) == ' ' || *(_cmdLine + i) == '\t')
i++;
filename = tilde_expand(_cmdLine + i);
break;
default:
demonstrate_command_usage();
NUMBER_WORDS_ERROR;
break;
}
// Search directory only if not specified as local/fullpath
if (filename[0] != '/' && filename[0] != '.')
longfilename = file_in_list(filename, false, false);
else
strcpy(longfilename, filename);
if (NULL == (fp = fopen(longfilename, "r"))) {
sprintf(_grTempString, "Sorry, I can't open file `%s'", longfilename);
err(_grTempString);
free(filename);
return false;
}
#if 1
if (((unsigned) superuser()) & FLAG_AUT1) {
CmdFile cf; // as in file.cc
cf.set(longfilename, fp, false, 0);
_cmdFILE.push_back(cf);
}
#endif
/*
* Scan through the file, doing lines.
*/
while (!feof(fp)) {
/*
* See if hit EOF on a line with no text.
*/
if (NULL == fgets(_cmdLine, LineLength, fp))
break;
if (feof(fp)) {
warning("Missing newline at end of inserted file `\\",
filename,
"'",
"\\");
strcat(_cmdLine, "\n");
}
_cmdFILE.back().increment_line(); // BUG line numbers wrong BUG
if (((unsigned) superuser()) & FLAG_AUT1) {
void insert_source_indicator(char *cl); // in doline.cc
insert_source_indicator(_cmdLine);
}
massage_command_line(_cmdLine);
if (_nword > 0 && !strcmp(_word[0], "return") && !skipping_through_if())
break;
if (!perform_command_line(fp, false)) {
return false;
}
}
_done = 0;
free(filename);
fclose(fp);
if (((unsigned) superuser()) & FLAG_AUT1) {
_cmdFILE.pop_back();
}
return true;
}
|