The R Project SVN R

Rev

Rev 4742 | Rev 5458 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4742 Rev 5214
Line 1... Line 1...
1
/*
1
/*
2
 *  R : A Computer Langage for Statistical Data Analysis
2
 *  R : A Computer Langage for Statistical Data Analysis
3
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
3
 *  Copyright (C) 1999  The R Core Development Team
4
 *  Copyright (C) 1998, 1999  Guido Masarotto
-
 
5
 *
4
 *
6
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
8
 *  (at your option) any later version.
Line 16... Line 15...
16
 *  You should have received a copy of the GNU General Public License
15
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
16
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
18
 */
20
 
19
 
21
#include "Defn.h"
-
 
22
#include "Print.h"
-
 
23
#include "Fileio.h"
20
#include "../unix/edit.c"
24
#include "IOStuff.h"
-
 
25
#include "Parse.h"
-
 
26
#include <stdio.h>
-
 
27
#include "run.h"
-
 
28
 
-
 
29
/*
-
 
30
 * ed, vi etc have 3 parameters. the data, a file and an editor
-
 
31
 *
-
 
32
 * if file is specified then the given file is used (and not removed on exit) if
-
 
33
 * file is not specified then a temporary file is used; since only one
-
 
34
 * temporary file is used for an entire session previous editing is lost
-
 
35
 *
-
 
36
 * if data is specified then it is passed out to be edited; if data is not
-
 
37
 * specified then either file (if specified) or the temporary file is used
-
 
38
 * (thus errors can be re-editied by calling edit a second time with no
-
 
39
 * arguments).
-
 
40
 *
-
 
41
 * if the editor is specified then the specified editor is invoked if possible
-
 
42
 * and an error message reported otherwise
-
 
43
 */
-
 
44
 
-
 
45
static char DefaultFileName[MAX_PATH];
-
 
46
 
-
 
47
void InitEd()
-
 
48
{
-
 
49
    char *tmp;
-
 
50
 
-
 
51
    tmp = getenv("TMP");
-
 
52
    if (!tmp) tmp = getenv("TEMP");
-
 
53
    if (!tmp) getenv("R_USER");
-
 
54
    sprintf(DefaultFileName,"%s/XXXXXX",tmp);
-
 
55
    mktemp(DefaultFileName);
-
 
56
}
-
 
57
 
-
 
58
 
-
 
59
SEXP do_edit(SEXP call, SEXP op, SEXP args, SEXP rho)
-
 
60
{
-
 
61
    int   i, rc, status;
-
 
62
    SEXP  x, fn, envir, ed;
-
 
63
    char *filename, *editcmd, *vmaxsave;
-
 
64
    FILE *fp;
-
 
65
 
-
 
66
    checkArity(op, args);
-
 
67
 
-
 
68
    vmaxsave = vmaxget();
-
 
69
 
-
 
70
    x = CAR(args);
-
 
71
    if (TYPEOF(x) == CLOSXP) envir = CLOENV(x);
-
 
72
    else envir = R_NilValue;
-
 
73
    PROTECT(envir);
-
 
74
 
-
 
75
    fn = CADR(args);
-
 
76
    if (!isString(fn))
-
 
77
	error("invalid argument to edit()\n");
-
 
78
 
-
 
79
    if (LENGTH(STRING(fn)[0]) > 0) {
-
 
80
	filename = R_alloc(strlen(CHAR(STRING(fn)[0])), sizeof(char));
-
 
81
	strcpy(filename, CHAR(STRING(fn)[0]));
-
 
82
    } else
-
 
83
	filename = DefaultFileName;
-
 
84
 
-
 
85
    if (x != R_NilValue) {
-
 
86
	if ((fp = R_fopen(filename, "w")) == NULL)
-
 
87
	    errorcall(call, "unable to open file\n");
-
 
88
	x = deparse1(x, 0);
-
 
89
	for (i = 0; i < LENGTH(x); i++)
-
 
90
	    fprintf(fp, "%s\n", CHAR(STRING(x)[i]));
-
 
91
	fclose(fp);
-
 
92
    }
-
 
93
    ed = CAR(CDDR(args));
-
 
94
    if (!isString(ed))
-
 
95
	error("editor type not valid\n");
-
 
96
    editcmd = R_alloc(strlen(CHAR(STRING(ed)[0])) + strlen(filename) + 2, sizeof(char));
-
 
97
    sprintf(editcmd, "%s %s", CHAR(STRING(ed)[0]), filename);
-
 
98
    rc = runcmd(editcmd, 1, 1, "");
-
 
99
    if (rc == NOLAUNCH)
-
 
100
	errorcall(call, "unable to run editor\n");
-
 
101
    if (rc != 0)
-
 
102
	warningcall(call, "editor ran but returned error status\n");
-
 
103
 
-
 
104
    if ((fp = R_fopen(filename, "r")) == NULL)
-
 
105
	errorcall(call, "unable to open file to read\n");
-
 
106
    R_ParseCnt = 0;
-
 
107
    x = PROTECT(R_ParseFile(fp, -1, &status));
-
 
108
    if (status != PARSE_OK)
-
 
109
	errorcall(call, "An error occurred on line %d\n use a command like\n x <- vi()\n to recover\n", R_ParseError);
-
 
110
    else
-
 
111
	fclose(fp);
-
 
112
    R_ResetConsole();
-
 
113
    {   /* can't just eval(x) here */
-
 
114
	int i, n;
-
 
115
	SEXP tmp;
-
 
116
 
-
 
117
	n = LENGTH(x);
-
 
118
	for (i = 0 ; i < n ; i++)
-
 
119
	    tmp = eval(VECTOR(x)[i], R_GlobalEnv);
-
 
120
	x = tmp;
-
 
121
    }
-
 
122
    if (TYPEOF(x) == CLOSXP && envir != R_NilValue)
-
 
123
	CLOENV(x) = envir;
-
 
124
    UNPROTECT(2);
-
 
125
    vmaxset(vmaxsave);
-
 
126
    return (x);
-
 
127
}
-