home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / csh4.zip / CMDS.C < prev    next >
C/C++ Source or Header  |  1985-09-04  |  3KB  |  163 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. typedef struct
  4. {
  5.     char *cmdname;
  6.     int (*func)();
  7. } builtin;
  8.  
  9. char *str_lower();
  10.  
  11. extern int result;
  12.  
  13. extern int cmds(),ls(), cp(), rm(), do_prog(),pushd(),popd(),drive(), ver(),
  14.         more(),fgrep(),scr_clear(),set(),ch_mod(),cat(),echo(), y(),t(),
  15.         last(),invalid(),mv(),md(),touch(),cd(),pwd(),rd(),hist(),my_exit();
  16.  
  17. my_exit(argc,argv)
  18.     char *argv[];
  19. {
  20.     exit(result);
  21. }
  22.  
  23. ver()
  24. {
  25.     extern char *version;
  26.     write(2,version,strlen(version));
  27.     write(2,"\r\n",2);
  28. }
  29.  
  30. builtin commands[] =
  31. {
  32.     "a:",drive,
  33.     "b:",drive,
  34.     "c:",drive,
  35.     "cat",cat,
  36.     "cd",cd,
  37.     "chdir",cd,
  38.     "chmod",ch_mod,
  39.     "cls",scr_clear,
  40.     "commands",cmds,
  41.     "copy",cp,
  42.     "cp",cp,
  43.     "copy",cp,
  44.     "d:",drive,
  45.     "del",rm,
  46.     "dir",ls,
  47.     "e:",drive,
  48.     "echo",echo,
  49.     "era",rm,
  50.     "erase",rm,
  51.     "error",last,
  52.     "exit",my_exit,
  53.     "f:",drive,
  54.     "fgrep",fgrep,
  55.     "g:",drive,
  56.     "h:",drive,
  57.     "hist",hist,
  58.     "history",hist,
  59.     "i:",drive,
  60.     "j:",drive,
  61.     "ls",ls,
  62.     "md",md,
  63.     "mkdir",md,
  64.     "more",more,
  65.     "mv",mv,
  66.     "no history",invalid,
  67.     "popd",popd,
  68.     "pushd",pushd,
  69.     "pwd",pwd,
  70.     "rd",rd,
  71.     "rm",rm,
  72.     "rmdir",rd,
  73.     "set",set,
  74.     "tee",t,
  75.     "touch",touch,
  76.     "version",ver,
  77.     "y",y
  78. };
  79.  
  80. char *histerr = "no history";
  81. int j,hiscount;
  82. char *history[] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  83.                      NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
  84.  
  85. int histsize = (sizeof(history)/sizeof(char *));
  86. int numcmds =  (sizeof(commands)/sizeof(builtin));
  87.  
  88. cmds()
  89. {
  90.     char *current;
  91.     register int i,j,col;
  92.     col = 1;
  93.     for (i = 0; i < numcmds; i++)
  94.     {
  95.         current = commands[i].cmdname;
  96.         write(1,current,j = strlen(current));
  97.         for (;j < 16;j++)
  98.             write(1," ",1);
  99.         if (col == 4)
  100.         {
  101.             col = 1;
  102.             crlf();
  103.         }
  104.         else
  105.             ++col;
  106.     }
  107.     crlf();
  108. }
  109.  
  110. findcmd(cmdbuf)
  111.     char *cmdbuf;
  112. {
  113.     register int low,high,mid;
  114.     char localbuf[256];
  115.     int cond;
  116.     strcpy(localbuf,cmdbuf);
  117.     cmdbuf = str_lower(localbuf);
  118.     low = 0;
  119.     high = numcmds - 1;
  120.     while (low <= high)
  121.     {
  122.         mid = (low+high) / 2;
  123.         if    ( ( cond =  strncmp( cmdbuf,
  124.                                  commands[mid].cmdname,
  125.                                  strlen(commands[mid].cmdname) ) ) < 0 )
  126.                 high = mid - 1;
  127.         else if (cond > 0)
  128.                 low = mid + 1;
  129.         else
  130.         {
  131.             /* kludge to allow for program invocations like d:command */
  132.             if (cmdbuf[1] == ':')
  133.                 if (cmdbuf[2] == '\0')
  134.                     return mid;
  135.                 else
  136.                     return -1;
  137.             return mid;
  138.         }
  139.     }
  140.     return -1;
  141. }
  142.  
  143. hist()
  144. {
  145.     register int i;
  146.     char localbuf[256];
  147.     if (j < histsize)
  148.         i = 0;
  149.     else
  150.         i = j - histsize + 1;
  151.     for (;i <= j; i++)
  152.     {
  153.         sprintf(localbuf,"%d : %s\r\n",i,history[i % histsize]);
  154.         write(1,localbuf,strlen(localbuf));
  155.     }
  156. }
  157.  
  158. last()
  159. {
  160.     printf("return code of last command %d\n",result);
  161.     return result;
  162. }
  163.