File: main.c

package info (click to toggle)
lsh 0.70-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 488 kB
  • ctags: 303
  • sloc: ansic: 6,713; makefile: 73; sh: 15
file content (313 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download | duplicates (3)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* This is copyrighted software but you may use/distribute it     *
 * in accordance with the conditions set out in the file COPYING  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <unistd.h>
#include "defs.h"

int
main (int argc, char **argv)
{
  int i, j;
  char cin[INBUF];
  char *ep;

/* Do essential initialization         */
  always ();
  pathinit ();
  syminit ();

  cin[0] = '\n';
  cin[1] = '\0';

/* Always have SOME drive mapped       */
  if (getcwd (cmdv, cmdvc) != NULL)
    {
      if (map (2, "/"))
	{
	  plerr (PLINK);
	  quitstat = 1;
	}
      else
	{
	  if (!chdir (cmdv))
	    {
	      if (setlo (2, cmdv))
		{
		  plerr (PLPNF);
		}
	    }
	  else
	    {
	      plerr (PLPNF);
	    }
	}
    }
  else
    {
      plerr (PLPNF);
      if (map (2, "/"))
	{
	  plerr (PLINK);
	  quitstat = 1;
	}
    }

/* If we are a login shell, set up shell level and trap TERM */
  if (argv[0][0] == '-' || getppid () == 1)
    {
      setenv (STRSHLVL, "1", 1);

      signal (SIGTERM, SIG_IGN);

/* check for mail                      */
      mailbytes = countmail ();
      if (mailbytes && isatty (STDOUT_FILENO))
	{
	  printf ("You have Mail [%d bytes]\n", mailbytes);
	}

    }

/* We should initialize if we start with a '-', are a child     */
/* of init or have been told by the variable in the environment */
  if (argv[0][0] == '-' || (getenv (STRNOUNINIT) != NULL) || getppid () == 1)
    {

#ifdef DEBUG
      fprintf (stderr, "We want to be initialized\n");
#endif

      sprintf (cmdv, "%d", getuid ());
      setenv ("UID", cmdv, 1);

      sprintf (cmdv, "%d", getgid ());
      setenv ("GID", cmdv, 1);

/* run the global autoexec first       */
      cmdrun ("/etc/autoexec", stdout);

/* run the autoexec for that group (redundant)   */
/*    sprintf(cmdv,"/etc/autoexec.%d",getgid()); */
/*    cmdrun(cmdv,stdout);                       */

/* check out any environment variables */
      introspect ();

/* Trap ^C                             */
      signal (SIGINT, SIG_IGN);

/* run the users autoexec here -       */
      ep = getenv ("HOME");
      if (ep != NULL)
	{

/* use an absolute path, relying on previous progs to set up HOME variable */
#ifdef DEBUG
	  fprintf (stderr, "Home dir is %s\n", ep);
#endif
	  strcpy (cmdv, ep);
#ifdef DEBUG
	  fprintf (stderr, "last character is <%c> -- is that a /?\n", cmdv[strlen (cmdv) - 1]);
#endif
	  if (cmdv[strlen (cmdv) - 1] == '/')
	    {
	      strcat (cmdv, "autoexec");
	    }
	  else
	    {
	      strcat (cmdv, "/autoexec");
	    }
#ifdef DEBUG
	  fprintf (stderr, "About to run %s\n", cmdv);
#endif
	  cmdrun (cmdv, stdout);

/* Now run the .autoexec file */
	  strcpy (cmdv, ep);
#ifdef DEBUG
	  fprintf (stderr, "last character is <%c> -- is that a /?\n", cmdv[strlen (cmdv) - 1]);
#endif
	  if (cmdv[strlen (cmdv) - 1] == '/')
	    {
	      strcat (cmdv, ".autoexec");
	    }
	  else
	    {
	      strcat (cmdv, "/.autoexec");
	    }
	  cmdrun (cmdv, stdout);

	}
      else
	{
/* if no HOME variable exits, then run the file in the current directory */
/* Could this somehow be a risk along the lines of .exrc ?               */
	  if (argv[0][0] == '-')
	    {
	      cmdrun ("autoexec", stdout);
	      cmdrun (".autoexec", stdout);
	    }
	}
    }
  else
    {
/* we are not a login shell, nor do we want to be initialized */
/* just check out any environment variables                   */
      introspect ();
    }

#ifdef DEBUG
  fprintf (stderr, "Got %d arguments\n", argc);
#endif

/* Note : Above initialization independant   */
/* of cmdline arguments                      */
  if (argc > 1)
    {
      ep = argv[1];
      if (ep[0] == '-')
	{
	  switch (ep[1])
	    {
	    case 'C':
	    case 'c':
	      running = 0;
	    case 'R':
	    case 'r':
	      if (argc > 2)
		{
		  j = 0;
		  for (i = 2; i < argc; i++)
		    {
		      if (strlen (argv[i]) < INBUF - j)
			{
			  strcpy (cin + j, argv[i]);
			  j += strlen (argv[i]);
			  cin[j] = ' ';
			  j++;
			}
		      else
			{
			  i = argc;
			  fputc ('\a', stdout);
			}
		    }
		  cin[j] = '\0';
#ifdef DEBUG
		  fprintf (stderr, "Will be running <%s> from the commandline\n", cin);
#endif
		  lex (cin, 0);
		  stmts (0, tokencount, tokenstream, stdin, stdout);
		}
	      break;
	    case 'K':
	    case 'k':
	      if (argc > 2)
		{
		  cmdrun (argv[2], stdout);
		}
	      else
		{
		  if (!(argv[0][0] == '-' || (getenv (STRNOUNINIT) != NULL) || getppid () == 1))
		    {
		      cmdrun ("/etc/autoexec", stdout);
		      ep = getenv ("HOME");
		      if (ep != NULL)
			{
			  strcpy (cmdv, ep);
			  if (cmdv[strlen (cmdv) - 1] == '/')
			    {
			      strcat (cmdv, "autoexec");
			    }
			  else
			    {
			      strcat (cmdv, "/autoexec");
			    }
			  cmdrun (cmdv, stdout);
			  strcpy (cmdv, ep);
			  if (cmdv[strlen (cmdv) - 1] == '/')
			    {
			      strcat (cmdv, ".autoexec");
			    }
			  else
			    {
			      strcat (cmdv, "/.autoexec");
			    }
			  cmdrun (cmdv, stdout);
			}
		    }
		}
	      break;
	    case 'H':
	    case 'h':
	    case '-':
	    case '?':
	      running = 0;
	      fprintf (stderr, "Usage: lsh [filename] [-c commands] [-h] [-k [filename]] [-r commands]\n");
	      break;
	    default:
	      plerr (PLPFN);
	      break;
	    }
	}
      else
	{
#ifdef DEBUG
	  fprintf (stderr, "Running file <%s>\n", ep);
#endif
	  cmdrun (ep, stdout);
	  running = 0;
	}
    }

  while (running)
    {

#ifdef DEBUG
      fprintf (stderr, "[%d] At top of main loop\n", getpid ());
#endif

      if (tokencount)
	fprintf (stdout, "\n");
      printprompt (drivecur, proz, stdout);

      if (timeout)
	{
	  alarm (timeout);
	}

      if (readl (cin, INBUF))
	{
	  running = 0;
	  fprintf (stdout, "\n");
	}
      else
	{
	  if (timeout)
	    {
	      alarm (0);
	    }
	  lex (cin, 0);
	  stmts (0, tokencount, tokenstream, stdin, stdout);
	}

#ifdef DEBUG
      fprintf (stderr, "[%d] At end of main loop... running [%d]\n", getpid (), running);
#endif
    }

#ifdef DIAGNOSE
  pathdump (stdout);
  symdump ();
  tokendump ();
#endif

  onquit ();

  return quitstat;
}