home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume20 / rc / part04 / status.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-22  |  2.3 KB  |  114 lines

  1. /* status.c: functions for printing fancy status messages in rc */
  2.  
  3. #include "rc.h"
  4. #include "utils.h"
  5. #include "status.h"
  6. #include "nalloc.h"
  7. #include "walk.h"
  8. #include "sigmsgs.h"
  9.  
  10. /* status == the wait() value of the last command in the pipeline, or the last command */
  11.  
  12. static int statuses[512];
  13. static int pipelength = 1;
  14.  
  15. /*
  16.    Test to see if rc's status is true. According to td, status is true if and only if every
  17.    pipe-member has an exit status of zero.
  18. */
  19.  
  20. int istrue(void) {
  21.     int i;
  22.  
  23.     for (i = 0; i < pipelength; i++)
  24.         if (statuses[i] != 0)
  25.             return FALSE;
  26.     return TRUE;
  27. }
  28.  
  29. /*
  30.    Return the status as an integer. A status which has low-bits set is a signal number,
  31.    whereas a status with high bits set is a value set from exit().
  32. */
  33.  
  34. int getstatus(void) {
  35.     int s = statuses[0];
  36.  
  37.     return s & 0xff ? s & 0x7f : (s >> 8) & 0xff;
  38. }
  39.  
  40. void set(boolean code) {
  41.     setstatus((!code) << 8); /* exit status 1 == 0x100 */
  42. }
  43.  
  44. /* take a pipeline and store the exit statuses. Check to see whether any of the children dumped core */
  45.  
  46. void setpipestatus(int stats[], int num) {
  47.     int i;
  48.  
  49.     for (i = 0; i < (pipelength = num); i++) {
  50.         statprint(stats[i]);
  51.         statuses[i] = stats[i];
  52.     }
  53. }
  54.  
  55. /* set a simple status, as opposed to a pipeline */
  56.  
  57. void setstatus(int i) {
  58.     pipelength = 1;
  59.     statuses[0] = i;
  60.     statprint(i);
  61. }
  62.  
  63. /* print a message if termination was with a signal, and if the child dumped core. exit on error if -e is set */
  64.  
  65. void statprint(int i) {
  66.     if (i & 0xff) {
  67.         char *msg = ((i & 0x7f) < NUMOFSIGNALS ? signals[i & 0x7f][1] : "");
  68.  
  69.         if (i & 0x80) {
  70.             if (*msg == '\0')
  71.                 fprint(2,"core dumped\n");
  72.             else
  73.                 fprint(2,"%s--core dumped\n",msg);
  74.         } else if (*msg != '\0')
  75.             fprint(2,"%s\n",msg);
  76.     }
  77.  
  78.     if (i != 0 && dashee && !cond)
  79.         rc_exit(getstatus());
  80. }
  81.  
  82. /* prepare a list to be passed back. Used whenever $status is dereferenced */
  83.  
  84. List *sgetstatus(void) {
  85.     List *r;
  86.     int i;
  87.  
  88.     for (r = NULL, i = 0; i < pipelength; i++) {
  89.         char buf[16];
  90.         List *q = nnew(List);
  91.         int s = statuses[i];
  92.         int t;
  93.  
  94.         q->n = r;
  95.         r = q;
  96.  
  97.         if ((t = s & 0x7f) != 0) {
  98.             if (t < NUMOFSIGNALS && *signals[t][0] != '\0')
  99.                 sprint(buf, "%s", signals[t][0]);
  100.             else
  101.                 sprint(buf,"-%d", t); /* unknown signals are negated */
  102.             if (s & 0x80)
  103.                 strcat(buf, "+core");
  104.         } else {
  105.             sprint(buf, "%d", (s >> 8) & 0xff);
  106.         }
  107.  
  108.         r->w = ncpy(buf);
  109.         r->m = NULL;
  110.     }
  111.  
  112.     return r;
  113. }
  114.