static void trapmemref(int signo, siginfo_t *si, void *a) { USED(a); /* ucontext_t*, could fetch pc in machine-dependent way */ if(isnilref(si)) disfault(nil, exNilref); else if(signo == SIGBUS) sysfault("bad address addr=", si->si_addr); /* eg, misaligned */ else sysfault("segmentation violation addr=", si->si_addr); }
buffer_t *new_command_line(void) { buffer_t *command_line; command_line = malloc(sizeof(buffer_t)); sysfault(!command_line, NULL); command_line->size = BUFFER_STEP; command_line->buffer = malloc(BUFFER_STEP * sizeof(char)); if (command_line->buffer == NULL) { free(command_line); sysfault(1, NULL); } return command_line; }
static void trapILL(int signo, siginfo_t *si, void *a) { USED(signo); USED(a); sysfault("illegal instruction pc=", si->si_addr); }
list_node_t * append_node (list_t *list) { list_node_t * node; node = malloc (sizeof (list_node_t)); sysfault (!node, NULL); node->value = NULL; if (list->first == NULL) { list->first = node; node->previous = NULL; } else { list->last->next = node; node->previous = list->last; } list->last = node; node->next = NULL; list->size++; return node; }
list_t *new_list (void (*del)(void*)) { list_t *list; list = malloc (sizeof (list_t)); sysfault (!list, NULL); list->size = 0; list->first = NULL; list->last = NULL; list->del = del; return list; }
char * stringdup (const char *str) { char *p; int n; n = strlen (str)+1; p = malloc (n*sizeof(char)); sysfault (!p, NULL); strcpy (p, str); return p; }
int read_command_line(buffer_t * command_line) { int read_bytes, count, temp; int read_more = 1; char *offset, *p; int howmany; count = 0; offset = command_line->buffer; howmany = command_line->size; command_line->length = 0; while (read_more) { /* Read howmany bytes. */ read_bytes = read(1, offset, howmany); count += read_bytes; /* This happens also if the previous read left a trailing newline. */ if (offset[0] == '\n') { offset[0] = '\0'; return --count; } read_more = 0; if (read_bytes < howmany) offset[read_bytes - 1] = '\0'; /* We read less than howmany; done. */ else if (offset[read_bytes - 1] == '\n') offset[read_bytes - 1] = '\0'; /* We read exactly howmany; done. */ else { /* There is more to read. */ /* Enlarge buffer. */ temp = command_line->size; p = realloc(command_line->buffer, (temp + BUFFER_STEP) * sizeof(char)); sysfault(!p, -1); /* Offest is the end of the buffer. */ offset = command_line->buffer + count; command_line->size += BUFFER_STEP; howmany = BUFFER_STEP; read_more = 1; } } command_line->length = count; return count; }
pipeline_t *new_pipeline(void) { pipeline_t *pipeline; int i, j; pipeline = malloc(1 * sizeof(pipeline_t)); sysfault(!pipeline, NULL); /* Allocate a pipeline structure. */ pipeline->command = malloc((MAX_COMMANDS + 1) * sizeof(char **)); if (!pipeline->command) { sysdebug(1); free(pipeline); return NULL; } /* Allocate memory for commands. */ for (i = 0; i < MAX_COMMANDS + 1; i++) { pipeline->command[i] = malloc((MAX_ARGUMENTS + 1) * sizeof(char *)); if (!pipeline->command[i]) { sysdebug(1); for (j = 0; j < i; j++) free(pipeline->command[j]); free(pipeline->command); free(pipeline); return NULL; } } pipeline->ground = FOREGROUND; pipeline->file_in[0] = '\0'; pipeline->file_out[0] = '\0'; return pipeline; }