/* Reads the file stream and creates an order according to * its input. All names of items will be in all caps. No item * should have a zero or negative count associated with it. * Returns: pointer to first element of order list. If file is * null or empty, will return null. Will throw error if file * is formatted incorrectly. */ list_elt *makeOrder(FILE *stream) { char *name; int count; list_elt *first_elt; // MIGHT WANT TO THROW SOME SORT OF ERROR if(stream==NULL) { return NULL; } // Initialize first element to NULL first_elt = NULL; while(feof(stream) != 1) { fscanf(stream, "%s %d", name, count); allCaps(name); first_elt = update(first_elt, name, count); } return first_elt; }
/* * Read in the information about files used in making the system. * Store it in the ftab linked list. */ void read_files(void) { FILE *fp; register struct file_list *tp, *pf; register struct device *dp; register struct opt *op; const char *wd; char *this, *needs; const char *devorprof; int options; int not_option; char pname[BUFSIZ]; char fname[1024]; char *rest = (char *) 0; int nreqs, first = 1, isdup; ftab = 0; (void) sprintf(fname, "%s/files", config_directory); openit: fp = fopenp(VPATH, fname, pname, "r"); if (fp == 0) { perror(fname); exit(1); } next: options = 0; rest = (char *) 0; /* * filename [ standard | optional ] * [ dev* | profiling-routine ] [ device-driver] */ wd = get_word(fp); if (wd == (char *)EOF) { (void) fclose(fp); if (first == 1) { (void) sprintf(fname, "%s/files.%s", config_directory, machinename); first++; goto openit; } return; } if (wd == 0) goto next; /* * Allow comment lines beginning witha '#' character. */ if (*wd == '#') { while ((wd=get_word(fp)) && wd != (char *)EOF) ; goto next; } this = ns(wd); next_word(fp, wd); if (wd == 0) { printf("%s: No type for %s.\n", fname, this); exit(1); } if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags)) isdup = 1; else isdup = 0; tp = 0; nreqs = 0; devorprof = ""; needs = 0; if (eq(wd, "standard")) goto checkdev; if (!eq(wd, "optional")) { printf("%s: %s must be optional or standard\n", fname, this); exit(1); } if (strncmp(this, "OPTIONS/", 8) == 0) options++; not_option = 0; nextopt: next_word(fp, wd); if (wd == 0) goto doneopt; if (eq(wd, "not")) { not_option = !not_option; goto nextopt; } devorprof = wd; if (eq(wd, "device-driver") || eq(wd, "profiling-routine")) { next_word(fp, wd); goto save; } nreqs++; if (needs == 0 && nreqs == 1) needs = ns(wd); if (isdup) goto invis; if (options) { struct opt *lop = 0; struct device tdev; /* * Allocate a pseudo-device entry which we will insert into * the device list below. The flags field is set non-zero to * indicate an internal entry rather than one generated from * the configuration file. The slave field is set to define * the corresponding symbol as 0 should we fail to find the * option in the option list. */ init_dev(&tdev); tdev.d_name = ns(wd); tdev.d_type = PSEUDO_DEVICE; tdev.d_flags++; tdev.d_slave = 0; for (op=opt; op; lop=op, op=op->op_next) { char *od = allCaps(ns(wd)); /* * Found an option which matches the current device * dependency identifier. Set the slave field to * define the option in the header file. */ if (strcmp(op->op_name, od) == 0) { tdev.d_slave = 1; if (lop == 0) opt = op->op_next; else lop->op_next = op->op_next; free(op); op = 0; } free(od); if (op == 0) break; } newdev(&tdev); } for (dp = dtab; dp != 0; dp = dp->d_next) { if (eq(dp->d_name, wd) && (dp->d_type != PSEUDO_DEVICE || dp->d_slave)) { if (not_option) goto invis; /* dont want file if option present */ else goto nextopt; } } if (not_option) goto nextopt; /* want file if option missing */ for (op = opt; op != 0; op = op->op_next) if (op->op_value == 0 && opteq(op->op_name, wd)) { if (nreqs == 1) { free(needs); needs = 0; } goto nextopt; } invis: while ((wd = get_word(fp)) != 0) ; if (tp == 0) tp = new_fent(); tp->f_fn = this; tp->f_type = INVISIBLE; tp->f_needs = needs; tp->f_flags = isdup; goto next; doneopt: if (nreqs == 0) { printf("%s: what is %s optional on?\n", fname, this); exit(1); } checkdev: if (wd) { if (*wd == '|') goto getrest; next_word(fp, wd); if (wd) { devorprof = wd; next_word(fp, wd); } } save: getrest: if (wd) { if (*wd == '|') { rest = ns(get_rest(fp)); } else { printf("%s: syntax error describing %s\n", fname, this); exit(1); } } if (eq(devorprof, "profiling-routine") && profiling == 0) goto next; if (tp == 0) tp = new_fent(); tp->f_fn = this; tp->f_extra = rest; if (options) tp->f_type = INVISIBLE; else if (eq(devorprof, "device-driver")) tp->f_type = DRIVER; else if (eq(devorprof, "profiling-routine")) tp->f_type = PROFILING; else tp->f_type = NORMAL; tp->f_flags = 0; tp->f_needs = needs; if (pf && pf->f_type == INVISIBLE) pf->f_flags = 1; /* mark as duplicate */ goto next; }
// input/ouput handler function to be called on thread void *io_handler(void *param, int socket_fd) { char command[MAXCOMMANDSIZE]; // For receiving commands from user int exitsignal; // If user wants to end the application (Command: EXIT, value: 8) // Main process loop for client while(fgets(command, sizeof command, stdin)) { // Manual removal of newline character int len = strlen(command); if (len > 0 && command[len-1] == '\n') { command[len-1] = '\0'; } allCaps(command); // parse the command, check if either single or double arg command int token_count = 0; int idx = 1; char* pch; char s[2][MAXCOMMANDSIZE]; memset(s[0], "", MAXCOMMANDSIZE); memset(s[1], "", MAXCOMMANDSIZE); int isnul = strlen(command); if (isnul == 0) continue; pch = strtok(command, " "); strcpy(s[0], pch); while (pch != NULL) { pch = strtok(NULL, " "); if (pch != NULL) { strcpy(s[1], pch); idx++; if (idx > 1) break; } } if (idx == 1) { char* c1 = &s[0]; if(!strcmp(c1, "EXIT")) { /* clean up */ printf("Terminating server...\n"); close(socket_fd); exit(0); } else if(!strcmp(c1, "START")){ ready = 1; enable_stats = 1; if (abs_start == 0) abs_start = 1; else printf("Server ready: waiting for connections...\n"); } else if(!strcmp(c1, "STATS")){ if (enable_stats == 0) printf ("Server still inactive\n"); else stats(); } else if (!strcmp(c1, "END")){ printf ("Server terminating...\n"); end(); printf ("...done\n"); } else { fprintf(stderr, "Unknown command: %s...\n", command); } } else if (idx == 2) { char* c1 = &s[0]; char* c2 = &s[1]; if(!strcmp(c1, "BLOCK")){ int socket = atoi(c2); printf("Blocking socket %d\n", socket); block(socket); } else if(!strcmp(c1, "UNBLOCK")){ int socket = atoi(c2); printf("Unblocking socket %d\n", socket); unblock(socket); } else if(!strcmp(c1, "THROWOUT")) { int socket = atoi(c2); printf("Throwing out socket %d\n", socket); throwout(socket); } else { fprintf(stderr, "Unknown command: %s...\n", command); } } else { fprintf(stderr, "Unknown command: %s...\n", command); } } return NULL; }
/* * Build the makefile from the skeleton */ void makefile(void) { FILE *ifp, *ofp; FILE *dfp; char pname[BUFSIZ]; char line[BUFSIZ]; struct opt *op; struct users *up; read_files(); (void) sprintf(line, "%s/Makefile.template", config_directory); ifp = fopenp(VPATH, line, pname, "r"); if (ifp == 0) { perror(line); exit(1); } dfp = fopen(path("Makefile"), "r"); rename(path("Makefile"), path("Makefile.old")); unlink(path("Makefile.old")); unlink(path("M.d")); if ((ofp = fopen(path("M.d"), "w")) == NULL) { perror(path("M.d")); /* We'll let this error go */ } else fclose(ofp); ofp = fopen(path("Makefile"), "w"); if (ofp == 0) { perror(path("Makefile")); exit(1); } fprintf(ofp, "SOURCE_DIR=%s\n", source_directory); if (machine == MACHINE_SUN || machine == MACHINE_SUN2 || machine == MACHINE_SUN3 || machine == MACHINE_SUN4) fprintf(ofp, "IDENT=-D%s -D%s", machinename, allCaps(ident)); else fprintf(ofp, "IDENT=-D%s", allCaps(ident)); if (profiling) fprintf(ofp, " -DGPROF"); if (cputype == 0) { printf("cpu type must be specified\n"); exit(1); } do_build("cputypes.h", build_cputypes); for (op = opt; op; op = op->op_next) if (op->op_value) fprintf(ofp, " -D%s=\"%s\"", op->op_name, op->op_value); else fprintf(ofp, " -D%s", op->op_name); fprintf(ofp, "\n"); if ((unsigned)machine > NUSERS) { printf("maxusers config info isn't present, using vax\n"); up = &users[MACHINE_VAX-1]; } else up = &users[machine-1]; if (maxusers < up->u_min) { maxusers = up->u_min; } else if (maxusers > up->u_max) printf("warning: maxusers > %d (%d)\n", up->u_max, maxusers); if (maxusers) { do_build("confdep.h", build_confdep); } for (op = mkopt; op; op = op->op_next) if (op->op_value) fprintf(ofp, "%s=%s\n", op->op_name, op->op_value); else fprintf(ofp, "%s\n", op->op_name); while (fgets(line, BUFSIZ, ifp) != 0) { if (*line == '%') goto percent; if (profiling && strncmp(line, "COPTS=", 6) == 0) { register char *cp; if (machine != MACHINE_MMAX) fprintf(ofp, "GPROF.EX=$(SOURCE_DIR)/machdep/%s/gmon.ex\n", machinename); cp = index(line, '\n'); if (cp) *cp = 0; cp = line + 6; while (*cp && (*cp == ' ' || *cp == '\t')) cp++; COPTS = malloc((unsigned)(strlen(cp) + 1)); if (COPTS == 0) { printf("config: out of memory\n"); exit(1); } strcpy(COPTS, cp); if (machine == MACHINE_MIPSY || machine == MACHINE_MIPS) { fprintf(ofp, "%s ${CCPROFOPT}\n", line); fprintf(ofp, "PCOPTS=%s\n", cp); } else if (machine == MACHINE_MMAX) fprintf(ofp, "%s -p\n",line); else fprintf(ofp, "%s -pg\n", line); continue; } fprintf(ofp, "%s", line); continue; percent: if (eq(line, "%OBJS\n")) { do_objs(ofp, "OBJS=", -1); } else if (eq(line, "%CFILES\n")) { do_files(ofp, "CFILES=", 'c'); do_objs(ofp, "COBJS=", 'c'); } else if (eq(line, "%MFILES\n")) { do_files(ofp, "MFILES=", 'm'); do_objs(ofp, "MOBJS=", 'm'); } else if (eq(line, "%SFILES\n")) { do_files(ofp, "SFILES=", 's'); do_objs(ofp, "SOBJS=", 's'); } else if (eq(line, "%BFILES\n")) do_files(ofp, "BFILES=", 'b'); else if (eq(line, "%MACHDEP\n")) { /* * Move do_machdep() after the mkopt stuff. */ for (op = mkopt; op; op = op->op_next) fprintf(ofp, "%s=%s\n", op->op_name, op->op_value); do_machdep(ofp); } else if (eq(line, "%ORDERED\n")) do_ordered(ofp); else if (eq(line, "%RULES\n")) do_rules(ofp); else if (eq(line, "%LOAD\n")) do_load(ofp); else fprintf(stderr, "Unknown %% construct in generic makefile: %s", line); } if (dfp != NULL) { copy_dependencies(dfp, ofp); (void) fclose(dfp); } (void) fclose(ifp); (void) fclose(ofp); }