Jspice3
help.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1986 Wayne A. Christopher
5  1994 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * The main routine for the help system in stand-alone mode.
10  */
11 #include "spice.h"
12 #include "cpdefs.h"
13 #ifdef HAVE_GETPWUID
14 #include <pwd.h>
15 #endif
16 
17 #ifdef HAVE_X11
18 
19 #include <X11/Intrinsic.h>
20 #include <X11/StringDefs.h>
21 
22 Display *Xdisplay;
23 static XtAppContext app_con;
24 static Widget toplevel;
25 static char *hcopydev;
26 
27 static String fallback_resources[] = {
28 
29  "help.help.background: lightgrey",
30  "help.help.Box.background: lightblue",
31  "help.help.Box.Command.background: yellow",
32  "help.help.Box.Toggle.background: yellow",
33  "help.help.Box.Label.background: pink",
34  "help.help.Label.background: pink",
35 
36  "help.popup.form.background: lightblue",
37  "help.popup.form.Command.background: yellow",
38  "help.popup.form.Label.background: pink",
39 
40  "help.popup_m.form_m.background: pink",
41  "help.popup_m.form_m.Command.background: yellow",
42  NULL
43 };
44 
45 
46 static int
47 errorhandler(display, errorev)
48 
49 Display *display;
50 XErrorEvent *errorev;
51 {
52  char ErrorMessage[1024];
53 
54  XGetErrorText(display, errorev->error_code, ErrorMessage, 1024);
55  printf(ErrorMessage);
56  return (0);
57 }
58 
59 
60 static void
61 CRaction(caller, call_data, params, numparams)
62 
63 Widget caller;
64 XEvent *call_data;
65 String *params;
66 Cardinal *numparams;
67 {
68  /* simulate a button press when CR entered in text of popup */
69  Widget action = XtNameToWidget(XtParent(caller), "action");
70 
71  if (action)
72  XtCallCallbacks(action, XtNcallback, NULL);
73 }
74 
75 
76 static void
77 QUaction(caller, call_data, params, numparams)
78 
79 Widget caller;
80 XEvent *call_data;
81 String *params;
82 Cardinal *numparams;
83 {
84  /* simulate a Quit button press when DeleteWindow requested */
85  Widget quit;
86 
87  quit = XtNameToWidget(caller, "help.buttonbox.delete");
88  if (quit)
89  XtCallCallbacks(quit, XtNcallback, NULL);
90 }
91 
92 static XtActionsRec actions[] = {
93  {"cr_action", CRaction},
94  {"quit_action", QUaction}
95 };
96 
97 #endif
98 
99 /* dummy declaration so CP.a doesn't pull in lexical.o and other objects */
100 bool cp_interactive = true;
101 FILE *cp_in, *cp_out, *cp_err;
102 
103 extern bool hlp_usex, hlp_killx;
104 char *Help_Path;
105 char *kw_hcopydev = "hcopydev";
106 
107 #ifdef __STDC__
108 static char *tilde_expand(char*);
109 #else
110 static char *tilde_expand();
111 #endif
112 
113 
114 /* ARGSUSED */
115 int
116 main(argc, argv)
117 
118 int argc;
119 char **argv;
120 {
121  wordlist *wl = NULL;
122  char *s;
123 
124  /* SPICE_HELP defined with -D cmdline option */
125  Help_Path = copy(SPICE_HELP);
126  if ((s = getenv("SPICE_HELP")) != NULL)
127  Help_Path = copy(s);
128  else if ((s = getenv("SPICE_LIB_DIR")) != NULL) {
129  Help_Path = tmalloc(strlen(s) + 10);
130  sprintf(Help_Path,"%s/helpdir",s);
131  }
132  if ((s = getenv("PRINTER")) != NULL)
133  hcopydev = copy(s);
134  else
135  hcopydev = "lp";
136  cp_in = stdin;
137  cp_out = stdout;
138  cp_err = stderr;
139 
140 #ifdef HAVE_X11
141  hlp_usex = 1;
142  hlp_killx = 1;
143 
144  /* initialize X toolkit */
145  XtToolkitInitialize();
146  app_con = XtCreateApplicationContext();
147  XtAppSetFallbackResources(app_con, fallback_resources);
148  Xdisplay = XtOpenDisplay(app_con, NULL, NULL, "Help", NULL, 0,
149  &argc, argv);
150  if (!Xdisplay) {
151  fprintf(stderr, "Error: Can't open X display");
152  return (1);
153  }
154  XSetErrorHandler(errorhandler);
155  XtAppAddActions(app_con, actions, XtNumber(actions));
156 #endif
157 
158 out:
159  if (argc > 1)
160  wl = wl_build(argv + 1);
161  hlp_main(Help_Path, wl);
162 
163 #ifdef HAVE_X11
164  if (hlp_usex) {
165  XtAppMainLoop(app_con);
166  }
167 #endif
168 
169  return (EXIT_NORMAL);
170 }
171 
172 
173 void
175 
176 {
177  fprintf(stderr, "fatal error\n");
178  exit(1);
179 }
180 
181 
182 /* ARGSUSED */
183 bool
185 
186 char *n, *r;
187 int t;
188 {
189  if (!strcmp(n, kw_hcopydev)) {
190  strcpy(r, hcopydev);
191  return (True);
192  }
193  return (false);
194 }
195 
196 
197 void
199 
200 char *buf;
201 {
202 #ifdef MSDOS
203  char *s, *t;
204  int bcnt = 0, ecnt = 0;
205 
206  s = t = buf;
207  while (*t != '\0') {
208  if (*t == '/' || *t == '\\') {
209  *s++ = '\\';
210  t++;
211  bcnt = 0;
212  ecnt = 0;
213  }
214  else if (*t == '.') {
215  *s++ = *t++;
216  ecnt = 1;
217  }
218  else if (!ecnt) {
219  if (bcnt++ < 8) *s++ = *t++;
220  else t++;
221  }
222  else {
223  if (ecnt++ < 4) *s++ = *t++;
224  else t++;
225  }
226  }
227  *s = '\0';
228 
229 #else
230  char *s;
231 
232  if (index(buf,'~')) {
233  s = tilde_expand(buf);
234  if (s) {
235  strcpy(buf,s);
236  txfree(s);
237  }
238  }
239 #endif
240 }
241 
242 
243 static char *
245 
246 char *string;
247 {
248 #ifdef HAVE_GETPWUID
249  struct passwd *pw;
250  char *tail;
251  char buf[BSIZE_SP];
252  char *k, c;
253 
254  if (!string)
255  return (NULL);
256 
257  while (*string && isspace(*string))
258  string++;
259 
260  if (*string != '~')
261  return copy(string);
262 
263  string += 1;
264 
265  if (!*string || *string == '/') {
266  pw = getpwuid(getuid());
267  *buf = 0;
268  }
269  else {
270  k = buf;
271  while ((c = *string) && c != '/')
272  *k++ = c, string++;
273  *k = 0;
274  pw = getpwnam(buf);
275  }
276 
277  if (pw) {
278  strcpy(buf, pw->pw_dir);
279  if (*string)
280  strcat(buf, string);
281  }
282  else
283  return (NULL);
284 
285  return (copy(buf));
286 
287 #else
288  return (copy(string));
289 #endif
290 }
291 
292 #ifdef HAVE_X11
293 
294 /* ARGSUSED */
295 void
296 ScedESC()
297 { return; }
298 
299 void
300 ToTopOfSced()
301 { return; }
302 
303 #endif
void cp_pathfix(char *buf)
Definition: help.c:198
static char buf[MAXPROMPT]
Definition: arg.c:18
#define BSIZE_SP
Definition: misc.h:19
bool cp_getvar(char *n, int t, char *r)
Definition: help.c:184
char * strcpy()
Definition: cddefs.h:119
char * Help_Path
Definition: help.c:104
bool hlp_killx
#define EXIT_NORMAL
Definition: misc.h:25
static void CRaction()
Definition: library.c:18
void hlp_main()
char * copy()
char * getenv(char *c)
Definition: libfuncs.c:106
FILE * cp_err
Definition: help.c:101
char * tmalloc()
bool cp_interactive
Definition: help.c:100
void txfree()
#define NULL
Definition: spdefs.h:121
FILE * cp_out
Definition: help.c:101
#define True
Definition: scedstub.c:16
wordlist * wl_build()
static double c
Definition: vectors.c:16
Definition: cpstd.h:21
char * kw_hcopydev
Definition: help.c:105
static int errorhandler()
Definition: cddefs.h:162
void ScedESC()
Definition: x11.c:2222
char * index(char *s, char c) const
Definition: string.c:294
static char * tilde_expand()
void fatal()
Definition: help.c:174
FILE * cp_in
Definition: help.c:101
int main(int argc, char **argv)
Definition: help.c:116
bool hlp_usex
Definition: provide.c:15
char ErrorMessage[]
Definition: error.c:20
Definition: cddefs.h:192
Display * Xdisplay
#define XtNumber(arr)
Definition: display.c:114
static void QUaction()