run.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. *
  4. * This product is part of the Amsterdam Compiler Kit.
  5. *
  6. * Permission to use, sell, duplicate or disclose this software must be
  7. * obtained in writing. Requests for such permissions may be sent to
  8. *
  9. * Dr. Andrew S. Tanenbaum
  10. * Wiskundig Seminarium
  11. * Vrije Universiteit
  12. * Postbox 7161
  13. * 1007 MC Amsterdam
  14. * The Netherlands
  15. *
  16. */
  17. #include "ack.h"
  18. #include "list.h"
  19. #include "trans.h"
  20. #include "data.h"
  21. #include <signal.h>
  22. #ifndef NORCSID
  23. static char rcs_id[] = "$Header$" ;
  24. #endif
  25. #define ARG_MORE 40 /* The size of args chunks to allocate */
  26. static char **arglist ; /* The first argument */
  27. static unsigned argcount ; /* The current number of arguments */
  28. static unsigned argmax; /* The maximum number of arguments so far */
  29. int do_run() {
  30. fatal("-g flag not implemeted") ;
  31. /*NOTREACHED*/
  32. return 0 ;
  33. }
  34. int runphase(phase) register trf *phase ; {
  35. register list_elem *elem ;
  36. if ( v_flag || debug ) {
  37. if ( v_flag==1 && !debug ) {
  38. vprint("%s",phase->t_name) ;
  39. if ( !phase->t_combine ) {
  40. vprint(" %s%s\n",p_basename,
  41. rindex(in.p_path,SUFCHAR) ) ;
  42. } else {
  43. scanlist(l_first(phase->t_inputs), elem) {
  44. vprint(" %s",p_cont(*elem)->p_path);
  45. }
  46. vprint("\n") ;
  47. }
  48. } else {
  49. /* list all args */
  50. vprint("%s",phase->t_prog) ;
  51. scanlist(l_first(phase->t_flags), elem) {
  52. vprint(" %s",l_content(*elem)) ;
  53. }
  54. scanlist(l_first(phase->t_args), elem) {
  55. vprint(" %s",l_content(*elem)) ;
  56. }
  57. vprint("\n") ;
  58. }
  59. }
  60. argcount=0 ;
  61. x_arg(phase->t_name) ;
  62. scanlist(l_first(phase->t_flags), elem) {
  63. x_arg(l_content(*elem)) ;
  64. }
  65. scanlist(l_first(phase->t_args), elem) {
  66. x_arg(l_content(*elem)) ;
  67. }
  68. x_arg( (char *)0 ) ;
  69. return run_exec(phase) ;
  70. }
  71. int run_exec(phase) trf *phase ; {
  72. int status, child, waitchild ;
  73. do_flush();
  74. while ( (child=fork())== -1 ) ;
  75. if ( child ) {
  76. /* The parent */
  77. do {
  78. waitchild= wait(&status) ;
  79. if ( waitchild== -1 ) {
  80. fatal("missing child") ;
  81. }
  82. } while ( waitchild!=child) ;
  83. if ( status ) {
  84. if ( status&0200 && (status&0177)!=SIGQUIT &&
  85. t_flag<=1 ) unlink("core") ;
  86. switch ( status&0177 ) {
  87. case 0 :
  88. break ;
  89. case SIGHUP:
  90. case SIGINT:
  91. case SIGQUIT:
  92. case SIGTERM:
  93. quit(-5) ;
  94. default:
  95. error("%s died with signal %d",
  96. phase->t_prog,status&0177) ;
  97. }
  98. /* The assumption is that processes voluntarely
  99. dying with a non-zero status already produced
  100. some sort of error message to the outside world.
  101. */
  102. n_error++ ;
  103. return 0 ;
  104. }
  105. return 1 ; /* From the parent */
  106. }
  107. /* The child */
  108. if ( phase->t_stdin ) {
  109. if ( !in.p_path ) {
  110. fatal("no input file for %s",phase->t_name) ;
  111. }
  112. close(0) ;
  113. if ( open(in.p_path,0)!=0 ) {
  114. error("cannot open %s",in.p_path) ;
  115. exit(1) ;
  116. }
  117. }
  118. if ( phase->t_stdout ) {
  119. if ( !out.p_path ) {
  120. fatal("no output file for %s",phase->t_name) ;
  121. }
  122. close(1) ;
  123. if ( creat(out.p_path,0666)!=1 ) {
  124. close(1); dup(2);
  125. error("cannot create %s",out.p_path) ;
  126. exit(1) ;
  127. }
  128. }
  129. execv(phase->t_prog,arglist) ;
  130. if ( phase->t_stdout ) { close(1) ; dup(2) ; }
  131. error("Cannot execute %s",phase->t_prog) ;
  132. exit(1) ;
  133. /*NOTREACHED*/
  134. }
  135. x_arg(string) char *string ; {
  136. /* Add one execute argument to the argument vector */
  137. if ( argcount==argmax ) {
  138. if ( argmax==0 ) {
  139. argmax= 2*ARG_MORE ;
  140. arglist= (char **)getcore(argmax*sizeof (char *)) ;
  141. } else {
  142. argmax += ARG_MORE ;
  143. arglist= (char **)changecore((char *)arglist,
  144. argmax*sizeof (char *)) ;
  145. }
  146. }
  147. *(arglist+argcount++) = string ;
  148. }