示例#1
0
文件: pr.c 项目: 00001/plan9port
/*
 * Defer message about failure to open file to prevent messing up
 * alignment of page with tear perforations or form markers.
 * Treat empty file as special case and report as diagnostic.
 */
Biobuf*
mustopen(char *s, Fils *f)
{
	char *tmp;

	if(*s == '\0') {
		f->f_name = STDINNAME();
		f->f_f = malloc(sizeof(Biobuf));
		if(f->f_f == 0)
			cerror("no memory");
		Binit(f->f_f, 0, OREAD);
	} else
	if((f->f_f = Bopen(f->f_name = s, OREAD)) == 0) {
		tmp = ffiler(f->f_name);
		s = strcpy((char*)getspace(strlen(tmp) + 1), tmp);
		free(tmp);
	}
	if(f->f_f != 0) {
		if((f->f_nextc = Bgetrune(f->f_f)) >= 0 || Multi == 'm')
			return f->f_f;
		sprint(s = (char*)getspace(strlen(f->f_name) + 1 + EMPTY),
			"%s -- empty file\n", f->f_name);
		Bterm(f->f_f);
	}
	error = 1;
	cerror(s);
	fprint(2, "\n");
	return 0;
}
示例#2
0
void exfree(void * p, size_t size)
{
	space_t *s = getspace();
	s->external_space_in_use -= size;
	s->num_exfrees ++;
	free(p);
}
示例#3
0
文件: mmu.c 项目: nivertech/vx32
/*
 * Called when scheduler has decided to run proc p.
 * Prepare to run proc p.
 */
void
mmuswitch(Proc *p)
{
    /*
     * Switch the address space, but only if it's not the
     * one we were just in.  Also, kprocs don't count --
     * only the guys on cpu0 do.
     */
    if(p->kp)
        return;

    if(tracemmu)
        iprint("mmuswitch %ld %s\n", p->pid, p->text);

    if(p->pmmu.us && p->pmmu.us->p == p) {
        if(tracemmu) iprint("---------- %ld %s [%d]\n",
                                p->pid, p->text, p->pmmu.us - uspace);
        usespace(p->pmmu.us);
        if(!p->newtlb && !m->flushmmu) {
            usespace(p->pmmu.us);
            return;
        }
        mmapflush(p->pmmu.us);
        p->newtlb = 0;
        return;
    }

    if(p->pmmu.us == nil)
        getspace(p);
    else
        takespace(p, p->pmmu.us);
    if(tracemmu) iprint("========== %ld %s [%d]\n",
                            p->pid, p->text, p->pmmu.us - uspace);
}
示例#4
0
void xfree(void * p, size_t size)
{
	space_t *s = getspace();
	s->space_in_use -= size;
	s->num_xfrees ++;

	free(p);
}
示例#5
0
文件: tcopy.c 项目: rlugojr/mhvtl
void verify(register int inp, register int outp, register char *outb)
{
    register int eot, inmaxblk, inn, outmaxblk, outn;
    register char *inb;

    inb = getspace(maxblk);
    inmaxblk = outmaxblk = maxblk;
    for (eot = 0;; guesslen = 0) {
        inn = read(inp, inb, inmaxblk);
        if (inn == -1) {
            if (guesslen)
                while (errno == EINVAL && (inmaxblk -= 1024)) {
                    inn = read(inp, inb, inmaxblk);
                    if (inn >= 0)
                        goto r1;
                }
            warn("read error");
            break;
        }
r1:
        outn = read(outp, outb, outmaxblk);
        if (outn == -1) {
            if (guesslen)
                while (errno == EINVAL && (outmaxblk -= 1024)) {
                    outn = read(outp, outb, outmaxblk);
                    if (outn >= 0)
                        goto r2;
                }
            warn("read error");
            break;
        }
r2:
        if (inn != outn) {
            fprintf(msg,
                    "%s: tapes have different block sizes; %d != %d.\n",
                    "tcopy", inn, outn);
            break;
        }
        if (!inn) {
            if (eot++) {
                fprintf(msg, "tcopy: tapes are identical.\n");
                return;
            }
        } else {
            if (bcmp(inb, outb, inn)) {
                fprintf(msg,
                        "tcopy: tapes have different data.\n");
                break;
            }
            eot = 0;
        }
    }
    exit(1);
}
示例#6
0
文件: ar.c 项目: tusharsoni/Inferno
/*
 *	allocate space for a control block or member buffer.  if the malloc
 *	fails we try to reclaim space by spilling previously allocated
 *	member buffers.
 */
char *
armalloc(int n)
{
    char *cp;

    do {
        cp = malloc(n);
        if (cp) {
            memset(cp, 0, n);
            return cp;
        }
    } while (getspace());
    fprint(2, "ar: out of memory\n");
    exits("malloc");
    return 0;
}
示例#7
0
// Note: returns an offset from the heap start, you'll have to add
// the heap bottom to this value to get a machine address.
linkval vardict::addvar(std::string name, type_t type)
{
    variable *var = new variable;
    var->type = type;
    var->offset = getspace(type.getstoragesize());
    if (vars.find(name) != vars.end())
        var->next = vars[name];  // push the stack down one...
    vars[name] = var;
    if (start_from_top)
        var->address = HEAP_TOP - var->offset - type.getstoragesize() + 1;
    else
        var->address = linkval("__program_end") + var->offset;
#ifdef EBUG
    std::cout << "adding var " << name << " 0x" << std::hex << var->offset << "\n";
#endif
    return var->address;
}
示例#8
0
void * exalloc(size_t size)
{
	void * p = malloc(size);
#ifdef TRACK_SPACE_USAGE
	space_t *s = getspace();
	s->external_space_in_use += size;
	if (s->max_external_space_used < s->external_space_in_use)
		s->max_external_space_used = s->external_space_in_use;
	s->num_exallocs ++;
	if (s->max_outstanding_exallocs < (s->num_exallocs - s->num_exfrees))
		s->max_outstanding_exallocs = (s->num_exallocs - s->num_exfrees);
#endif /* TRACK_SPACE_USAGE */

	if ((p == NULL) && (size != 0))
	{
		prt_error("Fatal Error: Ran out of space. (ext)");
		abort();
		exit(1);
	}
	return p;
}
示例#9
0
/**
 * maximum space used during the parse
 */
size_t get_max_space_used(void)
{
	return getspace()->max_space_used;
}
示例#10
0
/**
 * space used but not yet freed during parse
 */
size_t get_space_in_use(void)
{
	return getspace()->space_in_use;
}
示例#11
0
static	int
findopt(int argc, char **argv)
{
	int	eargc = 0;
	int	c;
	int	mflg = 0;
	int	aflg = 0;
	int	optnum;
	int	argv_ind;
	int	end_opt;
	int	i;
	int	isarg = 0;

	fixtty();

	/* Handle page number option */
	for (optnum = 1, end_opt = 0; optnum < argc && !end_opt; optnum++) {
		switch (*argv[optnum]) {
		case '+':
			/* check for all digits */
			if (strlen(&argv[optnum][1]) !=
			    strspn(&argv[optnum][1], "0123456789")) {
				(void) fprintf(stderr, gettext(
				    "pr: Badly formed number\n"));
				exit(1);
			}

			if ((Fpage = (int)strtol(&argv[optnum][1],
			    (char **)NULL, 10)) < 0) {
				(void) fprintf(stderr, gettext(
				    "pr: Badly formed number\n"));
				exit(1);
			}
			REMOVE_ARG(argc, &argv[optnum]);
			optnum--;
			break;

		case '-':
			/* Check for end of options */
			if (argv[optnum][1] == '-') {
				end_opt++;
				break;
			}

			if (argv[optnum][1] == 'h' || argv[optnum][1] == 'l' ||
			    argv[optnum][1] == 'o' || argv[optnum][1] == 'w')
				isarg = 1;
			else
				isarg = 0;

			break;

		default:
			if (isarg == 0)
				end_opt++;
			else
				isarg = 0;
			break;
		}
	}

	/*
	 * Handle options with optional arguments.
	 * If optional arguments are present they may not be separated
	 * from the option letter.
	 */

	for (optnum = 1; optnum < argc; optnum++) {
		if (argv[optnum][0] == '-' && argv[optnum][1] == '-')
			/* End of options */
			break;

		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0')
			/* stdin file name */
			continue;

		if (argv[optnum][0] != '-')
			/* not option */
			continue;

		for (argv_ind = 1; argv[optnum][argv_ind] != '\0'; argv_ind++) {
			switch (argv[optnum][argv_ind]) {
			case 'e':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -e option\n"));
						exit(1);
					}
					Etabc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Etabn = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				if (Etabn <= 0)
					Etabn = DEFTAB;
				argv_ind--;
				break;

			case 'i':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -i option\n"));
						exit(1);
					}
					Itabc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Itabn = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				if (Itabn <= 0)
					Itabn = DEFTAB;
				argv_ind--;
				break;


			case 'n':
				++Lnumb;
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -n option\n"));
						exit(1);
					}
					Nsepc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Numw = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				argv_ind--;
				if (!Numw)
					Numw = NUMW;
				break;

			case 's':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((Sepc = argv[optnum][argv_ind]) == '\0')
					Sepc = '\t';
				else {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -s option\n"));
						exit(1);
					}
					Sepc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				argv_ind--;
				break;

			default:
				break;
			}
		}
		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0') {
			REMOVE_ARG(argc, &argv[optnum]);
			optnum--;
		}
	}

	/* Now get the other options */
	while ((c = getopt(argc, argv, "0123456789adfFh:l:mo:prtw:"))
	    != EOF) {
		switch (c) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			Ncols *= 10;
			Ncols += c - '0';
			break;

		case 'a':
			aflg++;
			if (!Multi)
				Multi = c;
			break;

		case 'd':
			Dblspace = 2;
			break;

		case 'f':
			++Formfeed;
			++Pause;
			break;

		case 'h':
			Head = optarg;
			break;

		case 'l':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Length = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'm':
			mflg++;
			Multi = c;
			break;

		case 'o':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Offset = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'p':
			++Pause;
			break;

		case 'r':
			Report = 0;
			break;

		case 't':
			Margin = 0;
			break;

		case 'w':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Linew = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'F':
#ifdef	XPG4
			++Formfeed;
#else
			fold++;
#endif
			break;

		case '?':
			usage(2);
			break;

		default :
			usage(2);
		}
	}

	/* Count the file names and strip options */
	for (i = 1; i < argc; i++) {
		/* Check for explicit stdin */
		if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
			argv[eargc++][0] = '\0';
			REMOVE_ARG(argc, &argv[i]);
			if (i < optind)
				optind--;
		}
	}
	for (i = eargc; optind < argc; i++, optind++) {
		argv[i] = argv[optind];
		eargc++;
	}

	/* Check options */
	if (Ncols == 0)
		Ncols = 1;

	if (mflg && (Ncols > 1)) {
		(void) fprintf(stderr,
		    gettext("pr: only one of either -m or -column allowed\n"));
		usage(1);
	}

	if (Ncols == 1 && fold)
		Multi = 'm';

	if (Length <= 0)
		Length = LENGTH;

	if (Length <= Margin)
		Margin = 0;

	Plength = Length - Margin/2;

	if (Multi == 'm')
		Ncols = eargc;

	switch (Ncols) {
	case 0:
		Ncols = 1;
		break;

	case 1:
		break;

	default:
		if (Etabn == 0)	/* respect explicit tab specification */
			Etabn = DEFTAB;
		if (Itabn == 0)
			Itabn = DEFTAB;
	}

	if ((Fcol = (foldinf *) malloc(sizeof (foldinf) * Ncols)) == NULL) {
		(void) fprintf(stderr, gettext("pr: malloc failed\n"));
		exit(1);
	}
	for (i = 0; i < Ncols; i++)
		Fcol[i].fold = Fcol[i].skip = 0;

	if (Linew == 0)
		Linew = Ncols != 1 && Sepc == 0 ? LINEW : 512;

	if (Lnumb) {
		int numw;

		if (Nsepc == '\t') {
			if (Itabn == 0)
				numw = Numw + DEFTAB - (Numw % DEFTAB);
			else
				numw = Numw + Itabn - (Numw % Itabn);
		} else {
			numw = Numw + ((iswprint(Nsepc)) ? 1 : 0);
		}
		Linew -= (Multi == 'm') ? numw : numw * Ncols;
	}

	if ((Colw = (Linew - Ncols + 1)/Ncols) < 1)
		die("width too small");

	if (Ncols != 1 && Multi == 0) {
		/* Buflen should take the number of wide characters */
		/* Not the size for Buffer */
		Buflen = ((UNS) (Plength / Dblspace + 1)) *
		    2 * (Linew + 1);
		/* Should allocate Buflen * sizeof (wchar_t) */
		Buffer = (wchar_t *)getspace(Buflen * sizeof (wchar_t));
		Bufptr = Bufend = &Buffer[Buflen];
		Colpts = (COLP) getspace((UNS) ((Ncols + 1) *
		    sizeof (*Colpts)));
		Colpts[0].c_lno = 0;
	}

	/* is stdin not a tty? */
	if (Ttyout && (Pause || Formfeed) && !ttyname(fileno(stdin)))
		Ttyin = fopen("/dev/tty", "r");

	return (eargc);
}
示例#12
0
static	void
putspace(void)
{
	int nc = 0;

	for (; Nspace > 0; Outpos += nc, Nspace -= nc) {
#ifdef XPG4
		/* XPG4:  -i:  replace multiple SPACE chars with tab chars */
		if ((Nspace >= 2 && Itabn > 0 &&
			Nspace >= (nc = Itabn - Outpos % Itabn)) && !fold) {
#else
		/* Solaris:  -i:  replace white space with tab chars */
		if ((Itabn > 0 && Nspace >= (nc = Itabn - Outpos % Itabn)) &&
			!fold) {
#endif
			(void) fputwc(Itabc, stdout);
		} else {
			nc = 1;
			(void) putchar(' ');
		}
	}
}


static	void
unget(int colno)
{
	if (Buffer) {
		if (*(Colpts[colno].c_ptr-1) != '\t')
			--(Colpts[colno].c_ptr);
		if (Colpts[colno].c_lno)
			Colpts[colno].c_lno--;
	} else {
		if ((Multi == 'm' && colno == 0) || Multi != 'm')
			if (Lnumb && !foldcol)
				Lnumb--;
		colno = (Multi == 'a') ? 0 : colno;
		(void) ungetwc(Files[colno].f_nextc, Files[colno].f_f);
		Files[colno].f_nextc = C;
	}
}


/*
 * Defer message about failure to open file to prevent messing up
 * alignment of page with tear perforations or form markers.
 * Treat empty file as special case and report as diagnostic.
 */

static	FILE *
mustopen(char *s, FILS *f)
{
	char	*empty_file_msg = gettext("%s -- empty file");
	int	c;

	if (*s == '\0') {
		f->f_name = STDINNAME();
		f->f_f = stdin;
	} else if ((f->f_f = fopen(f->f_name = s, "r")) == NULL) {
		s = ffiler(f->f_name);
		s = strcpy((char *)getspace((UNS) strlen(s) + 1), s);
	}
	if (f->f_f != NULL) {
		errno = 0;
		f->f_nextc = _fgetwc_pr(f->f_f, &c);
		if (f->f_nextc != WEOF) {
			return (f->f_f);
		} else {	/* WEOF */
			if (errno == EILSEQ) {
				f->f_nextc = (wchar_t)c;
				return (f->f_f);
			}
			if (Multi == 'm')
				return (f->f_f);
		}
		(void) sprintf(s = (char *)getspace((UNS) strlen(f->f_name)
		    + 1 + (UNS) strlen(empty_file_msg)),
		    empty_file_msg, f->f_name);
		(void) fclose(f->f_f);
	}
	Error = 1;
	if (Report)
		if (Ttyout) {	/* accumulate error reports */
			Lasterr = Lasterr->e_nextp =
			    (ERR *) getspace((UNS) sizeof (ERR));
			Lasterr->e_nextp = NULL;
			Lasterr->e_mess = s;
		} else {	/* ok to print error report now */
			cerror(s);
			(void) putc('\n', stderr);
		}
	return ((FILE *)NULL);
}
示例#13
0
文件: tcopy.c 项目: 2asoft/freebsd
int
main(int argc, char *argv[])
{
	int lastnread, nread, nw, inp, outp;
	enum {READ, VERIFY, COPY, COPYVERIFY} op = READ;
	sig_t oldsig;
	int ch, needeof;
	char *buff;
	const char *inf;

	msg = stdout;
	guesslen = 1;
	outp = -1;
	while ((ch = getopt(argc, argv, "cs:vx")) != -1)
		switch((char)ch) {
		case 'c':
			op = COPYVERIFY;
			break;
		case 's':
			maxblk = atoi(optarg);
			if (maxblk <= 0) {
				warnx("illegal block size");
				usage();
			}
			guesslen = 0;
			break;
		case 'v':
			op = VERIFY;
			break;
		case 'x':
			msg = stderr;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	switch(argc) {
	case 0:
		if (op != READ)
			usage();
		inf = _PATH_DEFTAPE;
		break;
	case 1:
		if (op != READ)
			usage();
		inf = argv[0];
		break;
	case 2:
		if (op == READ)
			op = COPY;
		inf = argv[0];
		if ((outp = open(argv[1], op == VERIFY ? O_RDONLY :
		    op == COPY ? O_WRONLY : O_RDWR, DEFFILEMODE)) < 0)
			err(3, "%s", argv[1]);
		break;
	default:
		usage();
	}

	if ((inp = open(inf, O_RDONLY, 0)) < 0)
		err(1, "%s", inf);

	buff = getspace(maxblk);

	if (op == VERIFY) {
		verify(inp, outp, buff);
		exit(0);
	}

	if ((oldsig = signal(SIGINT, SIG_IGN)) != SIG_IGN)
		(void) signal(SIGINT, intr);

	needeof = 0;
	for (lastnread = NOCOUNT;;) {
		if ((nread = read(inp, buff, maxblk)) == -1) {
			while (errno == EINVAL && (maxblk -= 1024)) {
				nread = read(inp, buff, maxblk);
				if (nread >= 0)
					goto r1;
			}
			err(1, "read error, file %d, record %ju", filen, (intmax_t)record);
		} else if (nread != lastnread) {
			if (lastnread != 0 && lastnread != NOCOUNT) {
				if (lastrec == 0 && nread == 0)
					fprintf(msg, "%ju records\n", (intmax_t)record);
				else if (record - lastrec > 1)
					fprintf(msg, "records %ju to %ju\n",
					    (intmax_t)lastrec, (intmax_t)record);
				else
					fprintf(msg, "record %ju\n", (intmax_t)lastrec);
			}
			if (nread != 0)
				fprintf(msg, "file %d: block size %d: ",
				    filen, nread);
			(void) fflush(stdout);
			lastrec = record;
		}
r1:		guesslen = 0;
		if (nread > 0) {
			if (op == COPY || op == COPYVERIFY) {
				if (needeof) {
					writeop(outp, MTWEOF);
					needeof = 0;
				}
				nw = write(outp, buff, nread);
				if (nw != nread) {
					if (nw == -1) {
						warn("write error, file %d, record %ju", filen,
						    (intmax_t)record);
					} else {
						warnx("write error, file %d, record %ju", filen,
						    (intmax_t)record);
						warnx("write (%d) != read (%d)", nw, nread);
					}
					errx(5, "copy aborted");
				}
			}
			size += nread;
			record++;
		} else {
			if (lastnread <= 0 && lastnread != NOCOUNT) {
				fprintf(msg, "eot\n");
				break;
			}
			fprintf(msg,
			    "file %d: eof after %ju records: %ju bytes\n",
			    filen, (intmax_t)record, (intmax_t)size);
			needeof = 1;
			filen++;
			tsize += size;
			size = record = lastrec = 0;
			lastnread = 0;
		}
		lastnread = nread;
	}
	fprintf(msg, "total length: %ju bytes\n", (intmax_t)tsize);
	(void)signal(SIGINT, oldsig);
	if (op == COPY || op == COPYVERIFY) {
		writeop(outp, MTWEOF);
		writeop(outp, MTWEOF);
		if (op == COPYVERIFY) {
			rewind_tape(outp);
			rewind_tape(inp);
			verify(inp, outp, buff);
		}
	}
	exit(0);
}
示例#14
0
//Function for setting the command structure according to the input string
void setStructure(char **temp,int noOfArgumentsInTemp)
{
	int tempArrayIndex=0,argumentsInTemp,j=0,noOfCommands=0,commandArgument=0,flagCheckCommand=0,flagCheckRedirection=0;
	char *commandName=NULL;
	argumentsInTemp=noOfArgumentsInTemp;
	struct command *c=NULL,*end=NULL,*commandNext=NULL;
	head=NULL;
	while(noOfArgumentsInTemp!=0)
	{
		if(tempArrayIndex<argumentsInTemp && strcmp(temp[tempArrayIndex],"<")!=0 && strcmp(temp[tempArrayIndex],">")!=0 && strcmp(temp[tempArrayIndex],">>")!=0 && strcmp(temp[tempArrayIndex],"|")!=0)
		{
			if(j==0)
			{
				c=getspace();
				c=initialiseStructure(c);
				head=c;
				c->current_cmnd_args=(char **)malloc(sizeof(char *)*5);
				memset(c->current_cmnd_args,0,sizeof(char *)*5);
				commandName=(char*)malloc( (strlen(temp[tempArrayIndex])+1)  *  sizeof(char));
				strcpy(commandName,temp[tempArrayIndex]);
				c->cmnd=commandName;
				c->current_operation='\0';
				c->next=NULL;
				c->current_cmnd_args[commandArgument]=c->cmnd;
				tempArrayIndex++;
				noOfArgumentsInTemp--;
				j++;
				noOfCommands++;
				commandArgument++;
				c->current_cmnd_args[commandArgument]=NULL;
			}
			else
			{
				commandName=(char*)malloc( (strlen(temp[tempArrayIndex])+1)  *  sizeof(char));
				strcpy(commandName,temp[tempArrayIndex]);
				c->current_cmnd_args[commandArgument]=commandName;
				tempArrayIndex++;
				noOfArgumentsInTemp--;
				commandArgument++;
				c->current_cmnd_args[commandArgument]=NULL;

			}
		}
		if(tempArrayIndex<argumentsInTemp && strcmp(temp [tempArrayIndex],"<")==0 )
		{
			flagCheckCommand=1;
			c->next_operation='<';
			flagCheckRedirection=1;
			tempArrayIndex++;
			commandName=(char*)malloc(strlen(temp[tempArrayIndex])+1);
			memset(commandName, '\0', strlen(temp[tempArrayIndex])+1);
			strcpy(commandName,temp[tempArrayIndex]);
			c->infilename=commandName;
			tempArrayIndex++;
			noOfArgumentsInTemp=noOfArgumentsInTemp-2;
		}
		if(tempArrayIndex<argumentsInTemp && strcmp(temp[tempArrayIndex],">")==0 )
		{
			flagCheckCommand=1;
				if(flagCheckRedirection==0)
				{
					c->next_operation='>';
				}

					tempArrayIndex++;
					commandName=(char*)malloc(strlen(temp[tempArrayIndex])+1);
					memset(commandName, '\0', strlen(temp[tempArrayIndex])+1);
					strcpy(commandName,temp[tempArrayIndex]);
					c->outfilename=commandName;
					tempArrayIndex++;
					noOfArgumentsInTemp=noOfArgumentsInTemp-2;
		}
		if(tempArrayIndex<argumentsInTemp && strcmp(temp[tempArrayIndex],">>")==0 )
		{
					flagCheckCommand=1;
						if(flagCheckRedirection==0)
						{
							c->next_operation='>';
						}
							tempArrayIndex++;
							commandName=(char*)malloc(strlen(temp[tempArrayIndex])+1);
							memset(commandName, '\0', strlen(temp[tempArrayIndex])+1);
							strcpy(commandName,temp[tempArrayIndex]);
							c->appenoutFile=commandName;
							tempArrayIndex++;
							noOfArgumentsInTemp=noOfArgumentsInTemp-2;
		}
		if(tempArrayIndex<argumentsInTemp && strcmp(temp[tempArrayIndex],"|")==0)
		{
							flagCheckCommand=1;
							commandArgument=0;
							c->next_operation='|';
							commandNext=getspace();
							commandNext=initialiseStructure(commandNext);
							end=putAtEnd();
							end->next=commandNext;
							c=commandNext;
							c->next=NULL;
							c->current_operation='|';
							c->current_cmnd_args=(char **)malloc(sizeof(char *)*5);
							tempArrayIndex++;
							commandName=(char*)malloc(strlen(temp[tempArrayIndex])+1);
							memset(commandName, '\0', strlen(temp[tempArrayIndex])+1);
							strcpy(commandName,temp[tempArrayIndex]);
							c->cmnd=commandName;
							c->current_cmnd_args[commandArgument]=c->cmnd;
							tempArrayIndex++;
							noOfArgumentsInTemp=noOfArgumentsInTemp-2;
							commandArgument++;
							noOfCommands++;
							c->current_cmnd_args[commandArgument]=NULL;
		}
	}
	if(flagCheckCommand==0)
	{
		c->next_operation='\0';
	}
}
示例#15
0
文件: pr.c 项目: 00001/plan9port
int
findopt(int argc, char *argv[])
{
	char **eargv = argv;
	int eargc = 0, c;

	while(--argc > 0) {
		switch(c = **++argv) {
		case '-':
			if((c = *++*argv) == '\0')
				break;
		case '+':
			do {
				if(isdigit(c)) {
					--*argv;
					Ncols = atoix(argv);
				} else
				switch(c = TOLOWER(c)) {
				case '+':
					if((Fpage = atoix(argv)) < 1)
						Fpage = 1;
					continue;
				case 'd':
					Dblspace = 2;
					continue;
				case 'e':
					TABS(Etabn, Etabc);
					continue;
				case 'f':
					Formfeed++;
					continue;
				case 'h':
					if(--argc > 0)
						Head = argv[1];
					continue;
				case 'i':
					TABS(Itabn, Itabc);
					continue;
				case 'l':
					Len = atoix(argv);
					continue;
				case 'a':
				case 'm':
					Multi = c;
					continue;
				case 'o':
					Offset = atoix(argv);
					continue;
				case 's':
					if((Sepc = (*argv)[1]) != '\0')
						++*argv;
					else
						Sepc = '\t';
					continue;
				case 't':
					Margin = 0;
					continue;
				case 'w':
					Linew = atoix(argv);
					continue;
				case 'n':
					Lnumb++;
					if((Numw = intopt(argv, &Nsepc)) <= 0)
						Numw = NUMW;
				case 'b':
					Balance = 1;
					continue;
				case 'p':
					Padodd = 1;
					continue;
				default:
					die("bad option");
				}
			} while((c = *++*argv) != '\0');
			if(Head == argv[1])
				argv++;
			continue;
		}
		*eargv++ = *argv;
		eargc++;
	}
	if(Len == 0)
		Len = LENGTH;
	if(Len <= Margin)
		Margin = 0;
	Plength = Len - Margin/2;
	if(Multi == 'm')
		Ncols = eargc;
	switch(Ncols) {
	case 0:
		Ncols = 1;
	case 1:
		break;
	default:
		if(Etabn == 0)		/* respect explicit tab specification */
			Etabn = DEFTAB;
	}
	if(Linew == 0)
		Linew = Ncols != 1 && Sepc == 0? LINEW: 512;
	if(Lnumb)
		Linew -= Multi == 'm'? Numw: Numw*Ncols;
	if((Colw = (Linew - Ncols + 1)/Ncols) < 1)
		die("width too small");
	if(Ncols != 1 && Multi == 0) {
		ulong buflen = ((ulong)(Plength/Dblspace + 1))*(Linew+1)*sizeof(char);
		Buffer = getspace(buflen*sizeof(*Buffer));
		Bufend = &Buffer[buflen];
		Colpts = getspace((Ncols+1)*sizeof(*Colpts));
	}
	return eargc;
}