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