Subversion Repositories SvarDOS

Rev

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

Rev 438 Rev 443
Line 76... Line 76...
76
#include "env.h"
76
#include "env.h"
77
#include "helpers.h"
77
#include "helpers.h"
78
#include "redir.h"
78
#include "redir.h"
79
#include "rmodinit.h"
79
#include "rmodinit.h"
80
 
80
 
-
 
81
#define FLAG_EXEC_AND_QUIT 1
-
 
82
 
81
struct config {
83
struct config {
82
  int locate;
84
  unsigned char flags;
83
  int install;
85
  char *execcmd;
84
  unsigned short envsiz;
86
  unsigned short envsiz;
85
} cfg;
87
};
-
 
88
 
86
 
89
 
-
 
90
/* parses command line the hard way (directly from PSP) */
-
 
91
static void parse_argv(struct config *cfg) {
-
 
92
  unsigned short i;
-
 
93
  const unsigned char *cmdlinelen = (unsigned char *)0x80;
-
 
94
  char *cmdline = (char *)0x81;
87
 
95
 
88
static void parse_argv(struct config *cfg, int argc, char **argv) {
-
 
89
  int i;
-
 
90
  memset(cfg, 0, sizeof(*cfg));
96
  memset(cfg, 0, sizeof(*cfg));
91
 
97
 
-
 
98
  /* set a NULL terminator on cmdline */
-
 
99
  cmdline[*cmdlinelen] = 0;
-
 
100
 
92
  for (i = 1; i < argc; i++) {
101
  for (i = 0;;) {
-
 
102
 
-
 
103
    /* skip over any leading spaces */
-
 
104
    for (;; i++) {
93
    if (strcmp(argv[i], "/locate") == 0) {
105
      if (cmdline[i] == 0) return;
94
      cfg->locate = 1;
106
      if (cmdline[i] != ' ') break;
95
    }
107
    }
-
 
108
 
96
    if (strstartswith(argv[i], "/e:") == 0) {
109
    if (cmdline[i] != '/') {
-
 
110
      output("Invalid parameter: ");
-
 
111
      outputnl(cmdline + i);
-
 
112
      /* exit(1); */
-
 
113
    } else {
-
 
114
      i++;        /* skip the slash */
-
 
115
      switch (cmdline[i]) {
-
 
116
        case 'c': /* /C = execute command and quit */
-
 
117
        case 'C':
-
 
118
          cfg->flags |= FLAG_EXEC_AND_QUIT;
-
 
119
          /* FALLTHRU */
-
 
120
        case 'k': /* /K = execute command and keep running */
-
 
121
        case 'K':
-
 
122
          cfg->execcmd = cmdline + i + 1;
-
 
123
          return;
-
 
124
 
-
 
125
        case 'e': /* preset the initial size of the environment block */
-
 
126
        case 'E':
-
 
127
          i++;
-
 
128
          if (cmdline[i] == ':') i++; /* could be /E:size */
-
 
129
          atous(&(cfg->envsiz), cmdline + i);
97
      if ((atous(&(cfg->envsiz), argv[i] + 3) != 0) || (cfg->envsiz < 64)) {
130
          if (cfg->envsiz < 64) cfg->envsiz = 0;
-
 
131
          break;
-
 
132
 
-
 
133
        default:
-
 
134
          output("Invalid switch:");
98
        cfg->envsiz = 0;
135
          output(" ");
-
 
136
          outputnl(cmdline + i);
-
 
137
          exit(1);
-
 
138
          break;
99
      }
139
      }
100
    }
140
    }
-
 
141
 
-
 
142
    /* move to next argument or quit processing if end of cmdline */
-
 
143
    for (i++; (cmdline[i] != 0) && (cmdline[i] != ' ') && (cmdline[i] != '/'); i++);
-
 
144
 
101
  }
145
  }
102
}
146
}
103
 
147
 
104
 
148
 
105
static void buildprompt(char *s, unsigned short envseg) {
149
static void buildprompt(char *s, unsigned short envseg) {
Line 260... Line 304...
260
  /* printf("EXEPATH: '%s'\r\n", buff); */
304
  /* printf("EXEPATH: '%s'\r\n", buff); */
261
  env_setvar(envseg, buff);
305
  env_setvar(envseg, buff);
262
}
306
}
263
 
307
 
264
 
308
 
265
int main(int argc, char **argv) {
309
int main(void) {
266
  static struct config cfg;
310
  static struct config cfg;
267
  static unsigned short rmod_seg;
311
  static unsigned short rmod_seg;
268
  static unsigned short far *rmod_envseg;
312
  static unsigned short far *rmod_envseg;
269
  static unsigned short far *lastexitcode;
313
  static unsigned short far *lastexitcode;
270
  static unsigned char BUFFER[4096];
314
  static unsigned char BUFFER[4096];
271
 
315
 
272
  parse_argv(&cfg, argc, argv);
316
  parse_argv(&cfg);
273
 
317
 
274
  rmod_seg = rmod_find();
318
  rmod_seg = rmod_find();
275
  if (rmod_seg == 0xffff) {
319
  if (rmod_seg == 0xffff) {
276
    rmod_seg = rmod_install(cfg.envsiz);
320
    rmod_seg = rmod_install(cfg.envsiz);
277
    if (rmod_seg == 0xffff) {
321
    if (rmod_seg == 0xffff) {
Line 295... Line 339...
295
    envsiz = *sizptr;
339
    envsiz = *sizptr;
296
    envsiz *= 16;
340
    envsiz *= 16;
297
    printf("rmod_inpbuff at %04X:%04X, env_seg at %04X:0000 (env_size = %u bytes)\r\n", rmod_seg, RMOD_OFFSET_INPBUFF, *rmod_envseg, envsiz);
341
    printf("rmod_inpbuff at %04X:%04X, env_seg at %04X:0000 (env_size = %u bytes)\r\n", rmod_seg, RMOD_OFFSET_INPBUFF, *rmod_envseg, envsiz);
298
  }*/
342
  }*/
299
 
343
 
300
  for (;;) {
344
  do {
301
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
345
    char far *cmdline = MK_FP(rmod_seg, RMOD_OFFSET_INPBUFF + 2);
302
    unsigned char far *echostatus = MK_FP(rmod_seg, RMOD_OFFSET_ECHOFLAG);
346
    unsigned char far *echostatus = MK_FP(rmod_seg, RMOD_OFFSET_ECHOFLAG);
303
 
347
 
304
    if (*echostatus != 0) outputnl(""); /* terminate the previous command with a CR/LF */
-
 
305
 
-
 
306
    /* (re)load translation strings if needed */
348
    /* (re)load translation strings if needed */
307
    nls_langreload(BUFFER, *rmod_envseg);
349
    nls_langreload(BUFFER, *rmod_envseg);
308
 
350
 
-
 
351
    /* skip user input if I have a command to exec (/C or /K) */
-
 
352
    if (cfg.execcmd != NULL) {
-
 
353
      cmdline = cfg.execcmd;
-
 
354
      cfg.execcmd = NULL;
-
 
355
      goto EXEC_CMDLINE;
-
 
356
    }
-
 
357
 
-
 
358
    if (*echostatus != 0) outputnl(""); /* terminate the previous command with a CR/LF */
-
 
359
 
309
    SKIP_NEWLINE:
360
    SKIP_NEWLINE:
310
 
361
 
311
    /* print shell prompt (only if ECHO is enabled) */
362
    /* print shell prompt (only if ECHO is enabled) */
312
    if (*echostatus != 0) {
363
    if (*echostatus != 0) {
313
      char *promptptr = BUFFER;
364
      char *promptptr = BUFFER;
Line 378... Line 429...
378
    if (cmdline[-1] == 0) goto SKIP_NEWLINE;
429
    if (cmdline[-1] == 0) goto SKIP_NEWLINE;
379
 
430
 
380
    /* replace \r by a zero terminator */
431
    /* replace \r by a zero terminator */
381
    cmdline[(unsigned char)(cmdline[-1])] = 0;
432
    cmdline[(unsigned char)(cmdline[-1])] = 0;
382
 
433
 
-
 
434
    /* I jump here when I need to exec an initial command (/C or /K) */
-
 
435
    EXEC_CMDLINE:
-
 
436
 
383
    /* move pointer forward to skip over any leading spaces */
437
    /* move pointer forward to skip over any leading spaces */
384
    while (*cmdline == ' ') cmdline++;
438
    while (*cmdline == ' ') cmdline++;
385
 
439
 
386
    /* update rmod's ptr to COMPSPEC so it is always up to date */
440
    /* update rmod's ptr to COMPSPEC so it is always up to date */
387
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
441
    rmod_updatecomspecptr(rmod_seg, *rmod_envseg);
Line 391... Line 445...
391
      outputnl("");
445
      outputnl("");
392
      continue;
446
      continue;
393
    }
447
    }
394
 
448
 
395
    /* try matching (and executing) an internal command */
449
    /* try matching (and executing) an internal command */
396
    {
-
 
397
      int ecode = cmd_process(rmod_seg, *rmod_envseg, cmdline, BUFFER);
450
    if (cmd_process(rmod_seg, *rmod_envseg, cmdline, BUFFER) >= -1) {
398
      if (ecode >= 0) *lastexitcode = ecode;
-
 
399
      if (ecode >= -1) { /* internal command executed */
451
      /* internal command executed */
400
        redir_revert(); /* revert stdout (in case it was redirected) */
452
      redir_revert(); /* revert stdout (in case it was redirected) */
401
        continue;
453
      continue;
402
      }
-
 
403
    }
454
    }
404
 
455
 
405
    /* if here, then this was not an internal command */
456
    /* if here, then this was not an internal command */
406
    run_as_external(cmdline);
457
    run_as_external(cmdline);
407
 
458
 
Line 410... Line 461...
410
 
461
 
411
    /* execvp() replaces the current process by the new one
462
    /* execvp() replaces the current process by the new one
412
    if I am still alive then external command failed to execute */
463
    if I am still alive then external command failed to execute */
413
    outputnl("Bad command or file name");
464
    outputnl("Bad command or file name");
414
 
465
 
415
  }
-
 
-
 
466
  } while ((cfg.flags & FLAG_EXEC_AND_QUIT) == 0);
416
 
467
 
417
  return(0);
468
  return(0);
418
}
469
}