home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff331.lzh / Csh / src / main.c < prev    next >
C/C++ Source or Header  |  1990-03-21  |  5KB  |  204 lines

  1. /*
  2.  * MAIN.C
  3.  *
  4.  * Matthew Dillon, 24 Feb 1986
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  *
  9.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  10.  *
  11.  */
  12.  
  13. char shellname[]="Shell V4.01A";
  14. char shellctr []="CshCounter";
  15.  
  16. int aux; /* for use with aux: driver */
  17. char *oldtitle;
  18. char trueprompt[100];
  19. char Inline[260];
  20. struct IntuitionBase *IntuitionBase;
  21. struct Window *w;
  22. struct ArpBase *ArpBase;
  23.  
  24. main(argc, argv)
  25. register char *argv[];
  26. {
  27. #if RAW_CONSOLE
  28.     char *rawgets();
  29. #endif
  30.  
  31. register unsigned int i;
  32. extern int Enable_Abort;
  33. char buf[10];
  34. static char pipe1[32], pipe2[32];
  35. struct Window *getwindow();
  36.  
  37. ArpBase=(struct ArpBase *)OpenLibrary("arp.library",34L);
  38. if (ArpBase==NULL) { printf("No arp library\n"); exit(0); }
  39.  
  40. Forbid();
  41. i=0;
  42. if (Getenv(shellctr,buf,10L)) {
  43.     i=(int)(long)Atol(buf);
  44.     if (IoErr()) i=0;
  45.     }
  46. sprintf(buf,"%d",i+1);
  47. Setenv(shellctr,buf);
  48. Permit();
  49.  
  50. IntuitionBase=(struct IntuitionBase *)ArpBase->IntuiBase;
  51.  
  52. stdin->_flags    |= 0x80;    /* make sure we're set as a tty */
  53. stdout->_flags    |= 0x80;    /* in case of redirection in .login */
  54. Close(_devtab[2].fd);
  55. _devtab[2].mode |= O_STDIO;
  56. _devtab[2].fd = _devtab[1].fd;    /* set stderr to Output() otherwise */
  57.                 /* don't work with aux driver */
  58. Myprocess = (struct Process *)FindTask(0L);
  59. Mycli=(struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);
  60. w=getwindow();
  61.  
  62. oldtitle=(char *)(w->Title);
  63.  
  64. Pipe1 = pipe1;
  65. Pipe2 = pipe2;
  66. sprintf(pipe1, "ram:pipe1_%ld", Myprocess);
  67. sprintf(pipe2, "ram:pipe2_%ld", Myprocess);
  68.  
  69. sprintf(buf,"%ld",Myprocess->pr_TaskNum);
  70. set_var(LEVEL_SET, "_clinumber", buf);
  71.  
  72. set_var(LEVEL_SET, v_titlebar, shellname);
  73. set_var(LEVEL_SET, v_prompt,
  74.     (IsInteractive(Input())) ? "\23337m%p> \2330m" : "");
  75. set_var(LEVEL_SET, v_hist, "20");
  76. set_var(LEVEL_SET, v_lasterr, "0");
  77. set_var(LEVEL_SET, v_stat, "0");
  78. set_var(LEVEL_SET, v_path, "RAM:,RAM:c/,df0:c/,df1:c/,sys:system/");
  79. set_var(LEVEL_SET, "_insert", "1");
  80. set_var(LEVEL_SET, "f1", "cdir df0:\15");
  81. set_var(LEVEL_SET, "F1", "cdir df1:\15");
  82. set_var(LEVEL_SET, "f3", "cdir RAM:\15");
  83. set_var(LEVEL_SET, "F3", "cdir vd0:\15");
  84. set_var(LEVEL_SET, "f4", "cd df0:\15");
  85. set_var(LEVEL_SET, "F4", "cd df1:\15");
  86. set_var(LEVEL_SET, "f5", "cls; ls\15");
  87. set_var(LEVEL_SET, "F5", "cdir ");
  88. set_var(LEVEL_SET, "f6", "lc\15");
  89. set_var(LEVEL_SET, "f7", "info\15");
  90. set_var(LEVEL_SET, "F7", "assign \15");
  91. set_var(LEVEL_SET, "f8", "window -lf\15");
  92. set_var(LEVEL_SET, "F8", "window -sb\15");
  93. set_var(LEVEL_SET, "f10", "cls\15");
  94. set_var(LEVEL_SET, "F10", "exit\15");
  95. set_var(LEVEL_ALIAS, "cls", "echo -n ^l");
  96. set_var(LEVEL_ALIAS, "lc", "ls -s");
  97. set_var(LEVEL_ALIAS, "kr", "rm -r RAM:* >NIL:");
  98. set_var(LEVEL_ALIAS, "cdir", "%q cd $q; cls; dir");
  99. set_var(LEVEL_ALIAS, "exit", "endcli;quit");
  100. set_var(LEVEL_ALIAS, "lp", "cat >PRT:");
  101. seterr();
  102. if (Myprocess->pr_CurrentDir == NULL)
  103.     do_cd("x :");
  104. else do_pwd(NULL);
  105. Enable_Abort = 0;
  106.  
  107. if (exists(av[1] = "S:.login")) do_source("x S:.login");
  108.  
  109. for (i = 1; i < argc; ++i) {
  110.     if (!strcmp(argv[i],"-c")) {
  111.         Inline[0] = ' ';
  112.         Inline[1] = '\0';
  113.         while (++i < argc)
  114.             { strcat(Inline,argv[i]); strcat(Inline," "); }
  115.         exec_command(Inline);
  116.         main_exit(Lastresult);
  117.         }
  118.     if (!strcmp(argv[i],"-a")) { aux = 1; continue; }
  119.     sprintf (Inline, "source %s",argv[i]);
  120.     av[1] = argv[i];
  121.     do_source (Inline);
  122.     }
  123. for (;;) {
  124.    if (breakcheck())
  125.     while (WaitForChar(Input(), 100L) || stdin->_bp < stdin->_bend)
  126.         gets(Inline);
  127.    clearerr(stdin);  /* prevent acidental quit */
  128. #if RAW_CONSOLE
  129.    if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
  130. #else
  131.    printf("%s", disable ? "_ " : trueprompt);
  132.    fflush(stdout);
  133.    if (Quit || !gets(Inline)) main_exit(0);
  134. #endif
  135.    breakreset();
  136.    if (*Inline) exec_command(Inline);
  137.    }
  138. }
  139.  
  140. main_exit(n)
  141. {
  142. register unsigned short i;
  143. char buf[10];
  144.  
  145. Getenv(shellctr,buf,10L);
  146. i=(int)Atol(buf);
  147. sprintf(buf,"%d",i-1);
  148. Setenv(shellctr,buf);
  149. SetWindowTitles(w,oldtitle,-1L);
  150. for (i=1; i<MAXMYFILES; i++) myclose((int)i);
  151. ArpExit(0L,0L);            /* Intuition need not to be closed */
  152. }
  153.  
  154. breakcheck()
  155. {
  156. return (int)(SetSignal(0L,0L) & SIGBREAKF_CTRL_C);
  157. }
  158.  
  159. breakreset()
  160. {
  161. SetSignal(0L, SIGBREAKF_CTRL_C);
  162. }
  163.  
  164. dobreak()
  165. {
  166. if (breakcheck()) { printf("^C\n"); return(1); }
  167. return(0);
  168. }
  169.  
  170. /* this routine causes manx to use this Chk_Abort() rather than it's own */
  171. /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
  172. /* is zero).  Since we want to check for our own ^C's             */
  173.  
  174. Chk_Abort()
  175. {
  176. return(0);
  177. }
  178.  
  179. _wb_parse()
  180. {
  181. }
  182.  
  183. do_howmany()
  184. {
  185. char buf[10];
  186.  
  187. Getenv(shellctr, buf, 10L);
  188. printf("Shell(s) running: %s\n",buf);
  189. }
  190.  
  191. struct Window *getwindow()
  192. {
  193. struct InfoData *infodata;
  194. struct Window *win;
  195. long args[8];
  196.  
  197. infodata=AllocMem((long)sizeof(struct InfoData),MEMF_CLEAR | MEMF_PUBLIC);
  198. args[0]=(long)infodata >> 2;
  199. SendPacket(ACTION_DISK_INFO,args,Myprocess->pr_ConsoleTask);
  200. win=(struct Window *)infodata->id_VolumeNode;
  201. FreeMem(infodata,(long)sizeof(struct InfoData));
  202. return win;
  203. }
  204.