home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / util / csh-5.39.lha / Csh / src / execom.c < prev    next >
C/C++ Source or Header  |  1994-07-30  |  49KB  |  1,969 lines

  1. /*
  2.  * EXECOM.C
  3.  *
  4.  * Matthew Dillon, 10 August 1986
  5.  * Version 2.07M by Steve Drew 10-Sep-87
  6.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  7.  * Version 5.00L by Urban Mueller 17-Feb-91
  8.  * Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
  9.  *
  10.  */
  11.  
  12. #include "shell.h"
  13.  
  14. #define DEFAULTFRAME 1024
  15. #define Strlen(x)    strlen((char*)x)
  16. #define Strcpy(x,y)  strcpy((char*)x,(char*)y)
  17. #define Strcat(x,y)  strcat((char*)x,(char*)y)
  18. #define Index(x,y)   ((UBYTE*)index((char*)x,y))
  19. #define CHECKAV(num) { if( ac+num+10>max_ac ) checkav( frameptr, num ); }
  20.  
  21. typedef struct StackFrame {
  22.     struct StackFrame *next;
  23.     int  bytesleft;
  24.     char *ptr;
  25.     char mem[1];
  26. } FRAME;
  27.  
  28. /* execom.c */
  29. int   fcomm(UBYTE *str,FRAME **frame,char *from );
  30. int   hasspace( char *s );
  31. int   myfgets( char *buf, FILE *in );
  32.  
  33. static char  *compile_avf(FRAME **frame,char **av,int start,int end,char delim,int quote);
  34. static int   preformat(char *s, UBYTE *d);
  35. static void  backtrans(UBYTE *str);
  36. static UBYTE *exarg(UBYTE **ptr, UBYTE *elast);
  37. static UBYTE *format_insert_string(FRAME **frameptr, UBYTE *str, char *from);
  38. static int   find_command(char *str);
  39. static int   checkav( FRAME **frame,int n );
  40. static char  *tempname( int which );
  41. static UBYTE *skipword( UBYTE *strskip );
  42.  
  43. static void  newframe( FRAME **frame, int bytes);
  44. static void  *falloc( FRAME **frame, int bytes );
  45. static void  *frealloc( FRAME **frame, char *oldstring, int morebytes );
  46. static void  funalloc( FRAME **frame, int bytes );
  47. static char  *push_cpy( FRAME **frame, void *s);
  48. static void  deleteframe( FRAME *frame);
  49.  
  50. void  exec_every(void);
  51.  
  52. int do_nothing(void);
  53.  
  54. struct COMMAND {
  55.     int (*func)();
  56.     short minargs;
  57.     short stat;
  58.     int val;
  59.     char *name;
  60.     char *options;
  61.     char *usage;
  62. };
  63.  
  64. #define ST_COND   0x01 /* this is an 'if' type command, no redirection */
  65. #define ST_NORED  0x02 /* this command can't be redirected             */
  66. #define ST_NOEXP  0x04 /* no wild card expansion for this command      */
  67. #define ST_A0     0x08 /* delimit args with 0xA0 instead of ' '        */
  68. #define ST_FUNC   0x10 /* can be used as a function                    */
  69. #define ST_STORED 0x20 /* stores redirection instead of execuing it    */
  70. #define ST_AVLINE 0x40 /* needs avline                                 */
  71.  
  72. #define COND  ST_COND
  73. #define NORED ST_NORED
  74. #define STRED ST_STORED
  75. #define NOEXP ST_NOEXP
  76. #define A0   (ST_A0|ST_AVLINE)
  77. #define AV    ST_AVLINE
  78. #define FUNC  ST_FUNC
  79.  
  80. #define ALIAS LEVEL_ALIAS
  81. #define SET   LEVEL_SET
  82.  
  83. #define FIRSTCOMMAND 4
  84. #define COM_EXEC 3
  85.  
  86. static struct COMMAND Command[] = {
  87.  do_run,       0,   A0,     0, "\001",       NULL, NULL, /* may call do_source, do_cd */
  88.  do_nothing,   0,    0,     0, "\002",       NULL, NULL, /* dummy for aliases   */
  89.  do_nothing,   0,    0,     0, "\003",       NULL, NULL, /* dummy for aliases with args */
  90.  do_run,       0,NORED|A0,  1, "\004",       NULL, NULL, /* external that needs to be Execute()d */
  91.  do_abortline, 0,    0,     0, "abortline",  NULL, "",
  92.  do_action,    2,   AV,     9, "action",     "av", "action file [args]",
  93.  do_addbuffers,1,    0,     0, "addbuffers", NULL, "{drive [bufs]}",
  94.  do_tackon,    3, FUNC,     0, "addpart",    NULL, "var pathname filename",
  95.  do_set_var,   0,    0, ALIAS, "alias",      NULL, "[name [string] ]",
  96.  do_ascii,     0,    0,     0, "ascii",      "oh", "-oh [string]",
  97.  do_assign,    0,    0,     0, "assign",     "adpln",",logical,-adp {logical physical}",
  98.  do_basename,  2, FUNC,     0, "basename",   NULL, "var path",
  99.  do_cat,       0,    0,     0, "cat",        "n",  "-n [file file...]",
  100.  do_cd,        0,    0,     0, "cd",         "g",  "[path],-g path...path",
  101.  do_chgrp,     2,    0,     0, "chgrp",      NULL, "group file...file",
  102.  do_chmod,     2,    0,     0, "chmod",      NULL, "flags file...file",
  103.  do_chown,     2,    0,     0, "chown",      NULL, "owner file...file",
  104.  do_class,     0,   A0,     0, "class",      "n",  "-n name {type=param} \"actions\" {action=command}",
  105.  do_close,     0,    0,     0, "close",      NULL, "filenumber",
  106.  do_copy,      1,    0,     0, "copy",       "rudpfmqoa","-adfmopqru file file,-ud file...file dir,-ud dir...dir dir",
  107.  do_copy,      1,    0,     0, "cp",         "rudpfmqoa","-adfmopqru file file,-ud file...file dir,-ud dir...dir dir",
  108.  do_date,      0,    0,     0, "date",       "srb","-bsr [date/time]",
  109.  do_inc,       1,    0,    -1, "dec",        NULL, "varname [step]",
  110.  do_rm,        0,    0,     0, "delete",     "rfpqv","-fpqrv file...file",
  111.  do_dir,       0,NOEXP,     0, "dir",        "sfdcnhltbuikqavopzegL","-abcdefghiklnopqstuvL [-z lformat] [path...path]",
  112.  do_diskchange,1,    0,     0, "diskchange", NULL, "drive...drive",
  113.  do_echo,      0,    0,     0, "echo",       "ne", "-ne string",
  114.  do_if,        0, COND,     1, "else",       NULL, "",
  115.  do_if,        0, COND,     2, "endif",      NULL, "",
  116.  do_error,     1,    0,     0, "error",      NULL, "num",
  117.  do_exec,      1,   AV,     0, "exec",       "i",  "-i command",
  118.  do_fault,     1,    0,     0, "fault",      NULL, "error",
  119.  do_filenote,  1,    0,     0, "filenote",   "s",  "file...file note,-s file...file",
  120.  do_fileslist, 0,    0,     0, "flist",      NULL, "",
  121.  do_fltlower,  0,    0,     0, "fltlower",   NULL, "<in >out",
  122.  do_fltupper,  0,    0,     0, "fltupper",   NULL, "<in >out",
  123.  do_foreach,   3,    0,     0, "foreach",    "v",  "-v varname ( string ) command",
  124.  do_forever,   1,   AV,     0, "forever",    NULL, "command",
  125.  do_forline,   3,    0,     0, "forline",    NULL, "var filename command",
  126.  do_fornum,    4,    0,     0, "fornum",     "vs", "-vs var n1 n2 command",
  127.  do_getenv,    1, FUNC,     0, "getenv",     NULL, "shellvar envvar",
  128.  do_goto,      1,    0,     0, "goto",       NULL, "label",
  129.  do_head,      0,    0,     0, "head",       NULL, "[filename] [num]",
  130.  do_help,      0,    0,     0, "help",       "f",  "help -f",
  131.  do_history,   0,    0,     0, "history",    "nr", "-nr [partial_string]",
  132.  do_howmany,   0,    0,     0, "howmany",    NULL, "",
  133.  do_htype,     0,    0,     0, "htype",      "r",  "-r [file...file]",
  134.  do_if,        1,COND|NORED,0, "if",         "rftmdvone","-n arg cond arg,-n arg,-nf file,-nd dir,-nm,-nt file...file,-nr rpn_expr,-no opt args,-v varname",
  135.  do_inc,       1,    0,     1, "inc",        NULL, "varname [step]",
  136.  do_info,      0,    0,     0, "info",       "pt", "-pt [drive...drive]",
  137.  do_input,     1,    0,     0, "input",      "sr", "-rs var...var",
  138.  do_join,      2,    0,     1, "join",       "r",  "-r file...file",
  139.  do_keymap,    1,    0,     0, "keymap",     "n",  "-n number {key=function}",
  140.  do_label,     1, COND,     0, "label",      NULL, "name",
  141.  do_local,     0,    0,     0, "local",      NULL, "[var...var]",
  142.  do_linecnt,   0,    0,     0, "linecnt",    NULL, "<in >out",
  143.  do_ln,        1,    0,     0, "ln",         "s",  "-s original [link]",
  144.  do_dir,       0,NOEXP,     0, "ls",         "sfdcnhltbuikqavopzegL","-abcdefghiklnopqstuvL [-z format] [path...path]",
  145.  do_ln,        1,    0,     0, "makelink",   "s", "-s original [link]",
  146.  do_man,       0,    0,     0, "man",        NULL, "command...command",
  147.  do_mkdir,     1,    0,     0, "md",         "p",  "-p name...name",
  148.  do_mem,       0,    0,     0, "mem",        "cfqsrl","-cflqsr",
  149.  do_menu,      0,    0,     0, "menu",       "nm", "-nm [title item...item]",
  150.  do_mkdir,     1,    0,     0, "mkdir",      "p",  "-p name...name",
  151.  do_mv,        2,    0,     0, "mv",         "fv", "-fv from to,from...from todir",
  152.  do_open,      3,    0,     0, "open",       NULL, "file mode number",
  153.  do_path,      0,    0,     0, "path",       "rg", "-gr [dir...dir]",
  154.  do_pri,       2,    0,     0, "pri",        NULL, "clinumber pri,0 pri",
  155.  do_protect,   2,    0,     0, "protect",    NULL, "file...file flags",
  156.  do_ps,        0,    0,     0, "ps",         "les","-els [commandname...commandname]",
  157.  do_pwd,       0,    0,     0, "pwd",        NULL, "",
  158.  do_qsort,     0,    0,     0, "qsort",      "rc",  "-cr <in >out",
  159.  do_quit,      0,NORED,     0, "quit",       NULL, "",
  160.  do_truerun,   1,NORED|AV,  1, "rback",      NULL, "command",
  161.  do_mv,        2,    0,     0, "rename",     "fv", "-fv from to,from...from todir",
  162.  do_readfile,  1,    0,     0, "readf