xref: /original-bsd/usr.bin/pascal/pdx/tree/misc.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * Miscellaneous commands "edit" and "help".
14  * Also, output redirection routine "setout" and "unsetout".
15  */
16 
17 #include "defs.h"
18 #include "tree.h"
19 #include "command.h"
20 #include "object.h"
21 #include "mappings.h"
22 #include "sym.h"
23 #include "symtab.h"
24 
25 extern char *getenv();
26 
27 #define DEF_EDITOR	"vi"
28 
29 /*
30  * Invoke an editor on the given file.  Which editor to use might change
31  * installation to installation.  For now, we use "vi".  In any event,
32  * the environment variable "EDITOR" overrides any default.
33  */
34 
35 edit(filename)
36 char *filename;
37 {
38 	char *ed;
39 	FILE *fp;
40 	SYM *s;
41 	ADDRESS addr;
42 	char buff[10];
43 
44 	if ((ed = getenv("EDITOR")) == NIL) {
45 		ed = DEF_EDITOR;
46 	}
47 	fp = fopen(filename, "r");
48 	if (fp == NIL) {
49 		s = st_lookup(symtab, filename);
50 		if (s == NIL) {
51 			error("can't read \"%s\"", filename);
52 		}
53 		s = which(s);
54 		if (!isblock(s)) {
55 			error("can't read \"%s\"", filename);
56 		}
57 		addr = firstline(s);
58 		filename = srcfilename(addr);
59 		sprintf(buff, "+%d", srcline(addr));
60 		call(ed, stdin, stdout, buff, filename, NIL);
61 	} else {
62 		fclose(fp);
63 		call(ed, stdin, stdout, filename, NIL);
64 	}
65 }
66 
67 /*
68  * Send some nasty mail to the current pdx support person.
69  */
70 
71 gripe()
72 {
73 	char *maintainer = "4bsd-bugs@Berkeley.EDU";
74 
75 	puts("Type control-D to end your message.  Be sure to include");
76 	puts("your name and the name of the file you are debugging.");
77 	putchar('\n');
78 	call("Mail", stdin, stdout, maintainer, NIL);
79 	puts("Thank you.");
80 }
81 
82 /*
83  * Give the user some help.
84  */
85 
86 help()
87 {
88 	puts("pdx command subset summary:");
89 	putchar('\n');
90 	puts("run                    - begin execution of the program");
91 	puts("cont                   - continue execution");
92 	puts("step                   - single step one line");
93 	puts("next                   - step to next line (skip over calls)");
94 	puts("trace <line#>          - trace execution of the line");
95 	puts("trace <proc>           - trace calls to the procedure");
96 	puts("trace <var>            - trace changes to the variable");
97 	puts("trace <exp> at <line#> - print <exp> when <line> is reached");
98 	puts("stop at <line>         - suspend execution at the line");
99 	puts("stop in <proc>         - suspend execution when <proc> is called");
100 	puts("status                 - print trace/stop's in effect");
101 	puts("delete <number>        - remove trace or stop of given number");
102 	puts("call <proc>            - call the procedure");
103 	puts("where                  - print currently active procedures");
104 	puts("print <exp>            - print the value of the expression");
105 	puts("whatis <name>          - print the declaration of the name");
106 	puts("list <line>, <line>    - list source lines");
107 	puts("edit <proc>            - edit file containing <proc>");
108 	puts("gripe                  - send mail to the person in charge of pdx");
109 	puts("quit                   - exit pdx");
110 }
111 
112 /*
113  * Divert output to the given file name.
114  * Cannot redirect to an existing file.
115  */
116 
117 LOCAL int so_fd;
118 LOCAL BOOLEAN notstdout;
119 
120 setout(filename)
121 char *filename;
122 {
123 	FILE *fp;
124 
125 	if ((fp = fopen(filename, "r")) != NIL) {
126 		fclose(fp);
127 		error("%s: file already exists", filename);
128 	} else {
129 		so_fd = dup(1);
130 		close(1);
131 		if (creat(filename, 0666) == NIL) {
132 			unsetout();
133 			error("can't create %s", filename);
134 		}
135 		notstdout = TRUE;
136 	}
137 }
138 
139 /*
140  * Revert output to standard output.
141  */
142 
143 unsetout()
144 {
145 	fflush(stdout);
146 	close(1);
147 	if (dup(so_fd) != 1) {
148 		panic("standard out dup failed");
149 	}
150 	close(so_fd);
151 	notstdout = FALSE;
152 }
153 
154 BOOLEAN isredirected()
155 {
156 	return(notstdout);
157 }
158