int main()
{
    int flag,n,g,q,t,l,r,i,count;
    int L[100010],R[100010];
    t=readnum();
    int a[100010];
    while(t--)
    {
        n=readnum();
        q=readnum();
        for(i=1;i<=n;i++)
            a[i]=readnum();
        L[1]=a[1];
        for(i=2;i<=n;i++)
            L[i]=gcd(L[i-1],a[i]);
        R[n]=a[n];
        for(i=n-1;i>=1;i--)
            R[i]=gcd(R[i+1],a[i]);
        while(q--)
        {
            l=readnum();
            r=readnum();
            if(r==1 || l==n)
            {
                if(r==1)
                    g=R[2];
                if(l==n)
                    g=L[n-1];
            }
            else
            {
                if(l-1<1)
                    g=R[r+1];
                else if(r+1>n)
                    g=L[l-1];
                else
                    g=gcd(L[l-1],R[r+1]);
            }
            printf("%d\n",g);
        }
    }
    return 0;
}
int main()
{
    int flag,n,q,t,l,r,i,count;
    t=readnum();
    int a[100010];
    while(t--)
    {
        int g=1;
        n=readnum();
        q=readnum();
        for(i=1;i<=n;i++)
            a[i]=readnum();
        
        while(q--)
        {
            l=readnum();
            r=readnum();
            printf("%d\n",g);
        }
    }
    return 0;
}
Exemple #3
0
static long
consread(Chan *c, void *buf, long n, vlong offset)
{
	int l;
	Osenv *o;
	int ch, eol, i;
	char *p, tmp[128];
	char *cbuf = buf;

	if(n <= 0)
		return n;
	o = up->env;
	switch((ulong)c->qid.path){
	case Qdir:
		return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
	case Qsysctl:
		return readstr(offset, buf, n, VERSION);
	case Qcons:
	case Qkeyboard:
		qlock(&kbd);
		if(waserror()) {
			qunlock(&kbd);
			nexterror();
		}
		if(kbd.raw || kbd.kbdr) {
			if(qcanread(lineq))
				n = qread(lineq, buf, n);
			else {
				/* read as much as possible */
				do {
					i = qread(kbdq, cbuf, n);
					cbuf += i;
					n -= i;
				} while(n>0 && qcanread(kbdq));
				n = cbuf - (char*)buf;
			}
		} else {
			while(!qcanread(lineq)) {
				qread(kbdq, &kbd.line[kbd.x], 1);
				ch = kbd.line[kbd.x];
				eol = 0;
				switch(ch){
				case '\b':
					if(kbd.x)
						kbd.x--;
					break;
				case 0x15:
					kbd.x = 0;
					break;
				case '\n':
				case 0x04:
					eol = 1;
				default:
					kbd.line[kbd.x++] = ch;
					break;
				}
				if(kbd.x == sizeof(kbd.line) || eol) {
					if(ch == 0x04)
						kbd.x--;
					qwrite(lineq, kbd.line, kbd.x);
					kbd.x = 0;
				}
			}
			n = qread(lineq, buf, n);
		}
		qunlock(&kbd);
		poperror();
		return n;

	case Qscancode:
		if(offset == 0)
			return readstr(0, buf, n, kscanid);
		else
			return qread(kscanq, buf, n);

	case Qtime:
		snprint(tmp, sizeof(tmp), "%.lld", (vlong)mseconds()*1000);
		return readstr(offset, buf, n, tmp);

	case Qhostowner:
		return readstr(offset, buf, n, eve);

	case Quser:
		return readstr(offset, buf, n, o->user);

	case Qjit:
		snprint(tmp, sizeof(tmp), "%d", cflag);
		return readstr(offset, buf, n, tmp);

	case Qnull:
		return 0;

	case Qmsec:
		return readnum(offset, buf, n, TK2MS(MACHP(0)->ticks), NUMSIZE);

	case Qsysname:
		if(sysname == nil)
			return 0;
		return readstr(offset, buf, n, sysname);

	case Qnotquiterandom:
		genrandom(buf, n);
		return n;

	case Qrandom:
		return randomread(buf, n);

	case Qmemory:
		return poolread(buf, n, offset);

	case Qdrivers:
		p = malloc(READSTR);
		if(p == nil)
			error(Enomem);
		l = 0;
		for(i = 0; devtab[i] != nil; i++)
			l += snprint(p+l, READSTR-l, "#%C %s\n", devtab[i]->dc,  devtab[i]->name);
		if(waserror()){
			free(p);
			nexterror();
		}
		n = readstr(offset, buf, n, p);
		free(p);
		poperror();
		return n;

	case Qklog:
		return qread(klogq, buf, n);

	case Qkprint:
		rlock(&kprintq);
		if(waserror()){
			runlock(&kprintq);
			nexterror();
		}
		n = qread(kprintq.q, buf, n);
		poperror();
		runlock(&kprintq);
		return n;

	default:
		print("consread %llud\n", c->qid.path);
		error(Egreg);
	}
	return -1;		/* never reached */
}
Exemple #4
0
/*
 *	******
 *	OALTAR
 *	******
 *
 */
void oaltar()
{
	long k;
	int  p;

start:
	lprcat("\nDo you (p) pray  (d) desecrate"); 
	iopts();
	while (1) {
		while (1) switch(getcharacter()) {
		case 'p':	
		    lprcat(" pray.\nDo you (m) give money or (j) just pray? ");
			while (1) switch(getcharacter()) {
			case 'j':
				p = rund(100);
				if      (p < 12) createmonster(makemonst(level+2));
				else if (p < 17) enchweapon(ENCH_ALTAR);
				else if (p < 22) enchantarmor(ENCH_ALTAR);
				else if (p < 27) ohear();
				else             lprcat("\nNothing happens.");

				return;

			case 'm':
				cursor(1,24);
				cltoeoln();
				cursor(1,23);
				cltoeoln();
				lprcat("how much do you donate? ");
				if ((k = readnum(c[GOLD])) < 0)
					goto start;
				if (c[GOLD] < k) {
					lprcat("You don't have that much!");
					nap(1001);
					goto start;
				}
				if (k < (c[GOLD]/10) && rnd(60)<30 && !wizard) {
					lprcat("Cheapskate! The Gods are insulted by such a "
						   "tiny offering!");
					forget();
					createmonster(DEMONPRINCE);
					c[AGGRAVATE] += 1500;
					return;
				}

				c[GOLD] -= k;
				if (k < (c[GOLD]+k)/10 || k < rnd(50) && !wizard) {
					createmonster(makemonst(level+2));
					c[AGGRAVATE] += 500;
					bottomline();
					return;
				}

				p = rund(16);
				if (p <  4) 
					lprcat("Thank you.");
				else if (p <  6) { 
					enchantarmor(ENCH_ALTAR);
					enchantarmor(ENCH_ALTAR);
				}
				else if (p < 8) {
					enchweapon(ENCH_ALTAR);
					enchweapon(ENCH_ALTAR);
				}
				else
					ohear();

				bottomline();
				return;
			} /* end while switch : case j or m */

		case 'd': 
			lprcat(" desecrate");
			if (rnd(100)<60) { 
				createmonster(makemonst(level+3)+8); 
				c[AGGRAVATE] += 2500; 
			}
			else if(rnd(100)<5) raiselevel();
			else if (rnd(101)<30) {
	lprcat("\nThe altar crumbles into a pile of dust before your eyes.");
				forget();    /*remember to destroy the altar*/
			}
			else lprcat("\nNothing happens.");
			return;

		case 'i':
		case ESC: 
			ignore();
			if (rnd(100)<30) { 
				createmonster(makemonst(level+2)); 
				c[AGGRAVATE] += rnd(450); 
			}
			else	
				lprcat("\nNothing happens.");
			return;
		} /* end while switch: pray, des, ignore */
	} /* NOTREACHED */
} /* end oaltar */
Exemple #5
0
static long
consread(Chan *c, void *buf, long n, vlong off)
{
	ulong l;
	Mach *mp;
	char *b, *bp, ch;
	char tmp[256];		/* must be >= 18*NUMSIZE (Qswap) */
	int i, k, id, send;
	vlong offset = off;

	if(n <= 0)
		return n;

	switch((ulong)c->qid.path){
	case Qdir:
		return devdirread(c, buf, n, consdir, nelem(consdir), devgen);

	case Qcons:
		qlock(&kbd.lk);
		if(waserror()) {
			qunlock(&kbd.lk);
			nexterror();
		}
		while(!qcanread(lineq)){
			if(qread(kbdq, &ch, 1) == 0)
				continue;
			send = 0;
			if(ch == 0){
				/* flush output on rawoff -> rawon */
				if(kbd.x > 0)
					send = !qcanread(kbdq);
			}else if(kbd.raw){
				kbd.line[kbd.x++] = ch;
				send = !qcanread(kbdq);
			}else{
				switch(ch){
				case '\b':
					if(kbd.x > 0)
						kbd.x--;
					break;
				case 0x15:	/* ^U */
					kbd.x = 0;
					break;
				case '\n':
				case 0x04:	/* ^D */
					send = 1;
				default:
					if(ch != 0x04)
						kbd.line[kbd.x++] = ch;
					break;
				}
			}
			if(send || kbd.x == sizeof kbd.line){
				qwrite(lineq, kbd.line, kbd.x);
				kbd.x = 0;
			}
		}
		n = qread(lineq, buf, n);
		qunlock(&kbd.lk);
		poperror();
		return n;

	case Qcputime:
		k = offset;
		if(k >= 6*NUMSIZE)
			return 0;
		if(k+n > 6*NUMSIZE)
			n = 6*NUMSIZE - k;
		/* easiest to format in a separate buffer and copy out */
		for(i=0; i<6 && NUMSIZE*i<k+n; i++){
			l = up->time[i];
			if(i == TReal)
				l = msec() - l;
			l = TK2MS(l);
			readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
		}
		memmove(buf, tmp+k, n);
		return n;

	case Qkmesg:
		/*
		 * This is unlocked to avoid tying up a process
		 * that's writing to the buffer.  kmesg.n never 
		 * gets smaller, so worst case the reader will
		 * see a slurred buffer.
		 */
		if(off >= kmesg.n)
			n = 0;
		else{
			if(off+n > kmesg.n)
				n = kmesg.n - off;
			memmove(buf, kmesg.buf+off, n);
		}
		return n;
		
	case Qkprint:
		return qread(kprintoq, buf, n);

	case Qpgrpid:
		return readnum((ulong)offset, buf, n, up->pgrp->pgrpid, NUMSIZE);

	case Qpid:
		return readnum((ulong)offset, buf, n, up->pid, NUMSIZE);

	case Qppid:
		return readnum((ulong)offset, buf, n, up->parentpid, NUMSIZE);

	case Qtime:
		return readtime((ulong)offset, buf, n);

	case Qbintime:
		return readbintime(buf, n);

	case Qhostowner:
		return readstr((ulong)offset, buf, n, eve);

	case Qhostdomain:
		return readstr((ulong)offset, buf, n, hostdomain);

	case Quser:
		return readstr((ulong)offset, buf, n, up->user);

	case Qnull:
		return 0;

	case Qsysstat:
		b = smalloc(conf.nmach*(NUMSIZE*11+1) + 1);	/* +1 for NUL */
		bp = b;
		for(id = 0; id < 32; id++) {
			if(active.machs & (1<<id)) {
				mp = MACHP(id);
				readnum(0, bp, NUMSIZE, id, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->load, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE,
					(mp->perf.avg_inidle*100)/mp->perf.period,
					NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE,
					(mp->perf.avg_inintr*100)/mp->perf.period,
					NUMSIZE);
				bp += NUMSIZE;
				*bp++ = '\n';
			}
		}
		if(waserror()){
			free(b);
			nexterror();
		}
		n = readstr((ulong)offset, buf, n, b);
		free(b);
		poperror();
		return n;

	case Qswap:
		tmp[0] = 0;

		return readstr((ulong)offset, buf, n, tmp);

	case Qsysname:
		if(sysname == nil)
			return 0;
		return readstr((ulong)offset, buf, n, sysname);

	case Qrandom:
		return randomread(buf, n);

	case Qdrivers:
		b = malloc(READSTR);
		if(b == nil)
			error(Enomem);
		n = 0;
		for(i = 0; devtab[i] != nil; i++)
			n += snprint(b+n, READSTR-n, "#%C %s\n", devtab[i]->dc,  devtab[i]->name);
		if(waserror()){
			free(b);
			nexterror();
		}
		n = readstr((ulong)offset, buf, n, b);
		free(b);
		poperror();
		return n;

	case Qzero:
		memset(buf, 0, n);
		return n;

	case Qosversion:
		snprint(tmp, sizeof tmp, "2000");
		n = readstr((ulong)offset, buf, n, tmp);
		return n;

	default:
		print("consread 0x%llux\n", c->qid.path);
		error(Egreg);
	}
	return -1;		/* never reached */
}
Exemple #6
0
void add_class(student_t *student)
{
    int done = FALSE;
    int choice = 0;
    char *answer, dept_name[8], *pdept_name;
    short course_num;
    int course_id;
    course_t *course;

    while(!done) {
        printf("Add Class to Schedule\n");
        printf("0 - Go Back\n");
        printf("1 - Add by Class Num (ex: MAT 150)\n");
        printf("2 - Add by Course ID (ex: 2600)\n");
        printf("Choice: ");
        choice = readnum(line, LINE_SIZE);
        if (choice == -1)
            break;

        switch(choice) {
        case 0:
            done = TRUE; break;
        case 1:
            answer = answer_question("Enter Class ID: ");
            pdept_name = strsep(&answer, " ");
            if(!pdept_name || !answer || cgc_strlen(pdept_name) > 7) {
                printf("Bad Input\n");
                break;
            }
            strcpy(dept_name, pdept_name);
            course_num = strtol(answer, NULL, 10);
            printf("0 - Go Back\n");
            list_courses_by_num(dept_name, course_num);
            printf("Choice: ");
            choice = readnum(line, LINE_SIZE);
            if (choice && choice != -1) {
                course = select_course_num(dept_name, course_num, --choice);
                if (course) {
                    if (student->add_course(student, course))
                        printf("Successfully added course!\n");
                    else
                        printf("Could not add course.\n");
                } else {
                    printf("Bad selection\n");
                }
            }
            break;
        case 2:
            answer = answer_question("Enter Course ID: ");
            course_id = strtol(answer, NULL, 10);
            printf("0 - Go Back\n");
            list_courses_by_id(course_id);
            printf("Choice: ");
            choice = readnum(line, LINE_SIZE);
            if (choice && choice != -1) {
                course = select_course_id(course_id, --choice);
                if (course) {
                    if (student->add_course(student, course))
                        printf("Successfully added course!\n");
                    else
                        printf("Could not add course.\n");
                } else {
                    printf("Bad selection\n");
                }
            }
            break;
        default:
            printf("Bad Input\n");
        }
    }

    printf("Returning to main menu\n");
}
Exemple #7
0
/*
 * segment registry
 */
static int32_t ieee_segment(char *name, int pass, int *bits)
{
    /*
     * We call the label manager here to define a name for the new
     * segment, and when our _own_ label-definition stub gets
     * called in return, it should register the new segment name
     * using the pointer it gets passed. That way we save memory,
     * by sponging off the label manager.
     */
    if (!name) {
        *bits = 16;
        if (!any_segs)
            return 0;
        return seghead->index;
    } else {
        struct ieeeSection *seg;
        int ieee_idx, attrs;
	bool rn_error;
        char *p;

        /*
         * Look for segment attributes.
         */
        attrs = 0;
        while (*name == '.')
            name++;             /* hack, but a documented one */
        p = name;
        while (*p && !nasm_isspace(*p))
            p++;
        if (*p) {
            *p++ = '\0';
            while (*p && nasm_isspace(*p))
                *p++ = '\0';
        }
        while (*p) {
            while (*p && !nasm_isspace(*p))
                p++;
            if (*p) {
                *p++ = '\0';
                while (*p && nasm_isspace(*p))
                    *p++ = '\0';
            }

            attrs++;
        }

        ieee_idx = 1;
        for (seg = seghead; seg; seg = seg->next) {
            ieee_idx++;
            if (!strcmp(seg->name, name)) {
                if (attrs > 0 && pass == 1)
                    nasm_error(ERR_WARNING, "segment attributes specified on"
                          " redeclaration of segment: ignoring");
                if (seg->use32)
                    *bits = 32;
                else
                    *bits = 16;
                return seg->index;
            }
        }

        *segtail = seg = nasm_malloc(sizeof(*seg));
        seg->next = NULL;
        segtail = &seg->next;
        seg->index = seg_alloc();
        seg->ieee_index = ieee_idx;
        any_segs = true;
        seg->name = NULL;
        seg->currentpos = 0;
        seg->align = 1;         /* default */
        seg->use32 = *bits == 32;       /* default to user spec */
        seg->combine = CMB_PUBLIC;      /* default */
        seg->pubhead = NULL;
        seg->pubtail = &seg->pubhead;
        seg->data = NULL;
        seg->fptr = NULL;
        seg->lochead = NULL;
        seg->loctail = &seg->lochead;

        /*
         * Process the segment attributes.
         */
        p = name;
        while (attrs--) {
            p += strlen(p);
            while (!*p)
                p++;

            /*
             * `p' contains a segment attribute.
             */
            if (!nasm_stricmp(p, "private"))
                seg->combine = CMB_PRIVATE;
            else if (!nasm_stricmp(p, "public"))
                seg->combine = CMB_PUBLIC;
            else if (!nasm_stricmp(p, "common"))
                seg->combine = CMB_COMMON;
            else if (!nasm_stricmp(p, "use16"))
                seg->use32 = false;
            else if (!nasm_stricmp(p, "use32"))
                seg->use32 = true;
            else if (!nasm_strnicmp(p, "align=", 6)) {
                seg->align = readnum(p + 6, &rn_error);
                if (seg->align == 0)
                    seg->align = 1;
                if (rn_error) {
                    seg->align = 1;
                    nasm_error(ERR_NONFATAL, "segment alignment should be"
                          " numeric");
                }
                switch ((int)seg->align) {
                case 1:        /* BYTE */
                case 2:        /* WORD */
                case 4:        /* DWORD */
                case 16:       /* PARA */
                case 256:      /* PAGE */
                case 8:
                case 32:
                case 64:
                case 128:
                    break;
                default:
                    nasm_error(ERR_NONFATAL, "invalid alignment value %d",
                          seg->align);
                    seg->align = 1;
                    break;
                }
            } else if (!nasm_strnicmp(p, "absolute=", 9)) {
                seg->align = SEG_ABS + readnum(p + 9, &rn_error);
                if (rn_error)
                    nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
                          " attribute should be numeric");
            }
        }

        ieee_seg_needs_update = seg;
        if (seg->align >= SEG_ABS)
            define_label(name, NO_SEG, seg->align - SEG_ABS,
			 NULL, false, false);
        else
            define_label(name, seg->index + 1, 0L, NULL, false, false);
        ieee_seg_needs_update = NULL;

        if (seg->use32)
            *bits = 32;
        else
            *bits = 16;
        return seg->index;
    }
}
Exemple #8
0
int stdscan(void *private_data, struct tokenval *tv)
{
    char ourcopy[MAX_KEYWORD + 1], *r, *s;

    (void)private_data;         /* Don't warn that this parameter is unused */

    stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
    if (!*stdscan_bufptr)
        return tv->t_type = TOKEN_EOS;

    /* we have a token; either an id, a number or a char */
    if (isidstart(*stdscan_bufptr) ||
        (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
        /* now we've got an identifier */
        bool is_sym = false;
        int token_type;

        if (*stdscan_bufptr == '$') {
            is_sym = true;
            stdscan_bufptr++;
        }

        r = stdscan_bufptr++;
        /* read the entire buffer to advance the buffer pointer but... */
        while (isidchar(*stdscan_bufptr))
            stdscan_bufptr++;

        /* ... copy only up to IDLEN_MAX-1 characters */
        tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r < IDLEN_MAX ?
                                     stdscan_bufptr - r : IDLEN_MAX - 1);

        if (is_sym || stdscan_bufptr - r > MAX_KEYWORD)
            return tv->t_type = TOKEN_ID;       /* bypass all other checks */

        for (s = tv->t_charptr, r = ourcopy; *s; s++)
            *r++ = nasm_tolower(*s);
        *r = '\0';
        /* right, so we have an identifier sitting in temp storage. now,
         * is it actually a register or instruction name, or what? */
        token_type = nasm_token_hash(ourcopy, tv);

        if (likely(!(tv->t_flag & TFLAG_BRC))) {
            /* most of the tokens fall into this case */
            return token_type;
        } else {
            return tv->t_type = TOKEN_ID;
        }
    } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
        /*
         * It's a $ sign with no following hex number; this must
         * mean it's a Here token ($), evaluating to the current
         * assembly location, or a Base token ($$), evaluating to
         * the base of the current segment.
         */
        stdscan_bufptr++;
        if (*stdscan_bufptr == '$') {
            stdscan_bufptr++;
            return tv->t_type = TOKEN_BASE;
        }
        return tv->t_type = TOKEN_HERE;
    } else if (isnumstart(*stdscan_bufptr)) {   /* now we've got a number */
        bool rn_error;
        bool is_hex = false;
        bool is_float = false;
        bool has_e = false;
        char c;

        r = stdscan_bufptr;

        if (*stdscan_bufptr == '$') {
            stdscan_bufptr++;
            is_hex = true;
        }

        for (;;) {
            c = *stdscan_bufptr++;

            if (!is_hex && (c == 'e' || c == 'E')) {
                has_e = true;
                if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-') {
                    /*
                     * e can only be followed by +/- if it is either a
                     * prefixed hex number or a floating-point number
                     */
                    is_float = true;
                    stdscan_bufptr++;
                }
            } else if (c == 'H' || c == 'h' || c == 'X' || c == 'x') {
                is_hex = true;
            } else if (c == 'P' || c == 'p') {
                is_float = true;
                if (*stdscan_bufptr == '+' || *stdscan_bufptr == '-')
                    stdscan_bufptr++;
            } else if (isnumchar(c) || c == '_')
                ; /* just advance */
            else if (c == '.')
                is_float = true;
            else
                break;
        }
        stdscan_bufptr--;       /* Point to first character beyond number */

        if (has_e && !is_hex) {
            /* 1e13 is floating-point, but 1e13h is not */
            is_float = true;
        }

        if (is_float) {
            tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
            return tv->t_type = TOKEN_FLOAT;
        } else {
            r = stdscan_copy(r, stdscan_bufptr - r);
            tv->t_integer = readnum(r, &rn_error);
            stdscan_pop();
            if (rn_error) {
                /* some malformation occurred */
                return tv->t_type = TOKEN_ERRNUM;
            }
            tv->t_charptr = NULL;
            return tv->t_type = TOKEN_NUM;
        }
    } else if (*stdscan_bufptr == '\'' || *stdscan_bufptr == '"' ||
               *stdscan_bufptr == '`') {
        /* a quoted string */
        char start_quote = *stdscan_bufptr;
        tv->t_charptr = stdscan_bufptr;
        tv->t_inttwo = nasm_unquote(tv->t_charptr, &stdscan_bufptr);
        if (*stdscan_bufptr != start_quote)
            return tv->t_type = TOKEN_ERRSTR;
        stdscan_bufptr++;       /* Skip final quote */
        return tv->t_type = TOKEN_STR;
    } else if (*stdscan_bufptr == '{') {
        /* now we've got a decorator */
        int token_len;

        stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);

        r = ++stdscan_bufptr;
        /*
         * read the entire buffer to advance the buffer pointer
         * {rn-sae}, {rd-sae}, {ru-sae}, {rz-sae} contain '-' in tokens.
         */
        while (isbrcchar(*stdscan_bufptr))
            stdscan_bufptr++;

        token_len = stdscan_bufptr - r;

        /* ... copy only up to DECOLEN_MAX-1 characters */
        tv->t_charptr = stdscan_copy(r, token_len < DECOLEN_MAX ?
                                        token_len : DECOLEN_MAX - 1);

        stdscan_bufptr = nasm_skip_spaces(stdscan_bufptr);
        /* if brace is not closed properly or token is too long  */
        if ((*stdscan_bufptr != '}') || (token_len > MAX_KEYWORD)) {
            nasm_error(ERR_NONFATAL,
                       "invalid decorator token inside braces");
            return tv->t_type = TOKEN_INVALID;
        }

        stdscan_bufptr++;       /* skip closing brace */

        for (s = tv->t_charptr, r = ourcopy; *s; s++)
            *r++ = nasm_tolower(*s);
        *r = '\0';

        /* right, so we have a decorator sitting in temp storage. */
        nasm_token_hash(ourcopy, tv);

        /* handle tokens inside braces */
        return stdscan_handle_brace(tv);
    } else if (*stdscan_bufptr == ';') {
        /* a comment has happened - stay */
        return tv->t_type = TOKEN_EOS;
    } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_SHR;
    } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_SHL;
    } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_SDIV;
    } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_SMOD;
    } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_EQ;
    } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_NE;
    } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_NE;
    } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_LE;
    } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_GE;
    } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_DBL_AND;
    } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_DBL_XOR;
    } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
        stdscan_bufptr += 2;
        return tv->t_type = TOKEN_DBL_OR;
    } else                      /* just an ordinary char */
        return tv->t_type = (uint8_t)(*stdscan_bufptr++);
}
Exemple #9
0
int main(int argc, char **argv)
{
    char buffer[INSN_MAX * 2], *p, *ep, *q;
    char outbuf[256];
    char *pname = *argv;
    char *filename = NULL;
    uint32_t nextsync, synclen, initskip = 0L;
    int lenread;
    int32_t lendis;
    bool autosync = false;
    int bits = 16, b;
    bool eof = false;
    iflag_t prefer;
    bool rn_error;
    int64_t offset;
    FILE *fp;

    nasm_ctype_init();
    nasm_set_verror(ndisasm_verror);
    iflag_clear_all(&prefer);

    offset = 0;
    init_sync();

    while (--argc) {
        char *v, *vv, *p = *++argv;
        if (*p == '-' && p[1]) {
            p++;
            while (*p)
                switch (nasm_tolower(*p)) {
                case 'a':      /* auto or intelligent sync */
                case 'i':
                    autosync = true;
                    p++;
                    break;
                case 'h':
                    fputs(help, stderr);
                    return 0;
                case 'r':
                case 'v':
                    fprintf(stderr,
                            "NDISASM version %s compiled on %s\n",
			    nasm_version, nasm_date);
                    return 0;
                case 'u':	/* -u for -b 32, -uu for -b 64 */
		    if (bits < 64)
			bits <<= 1;
                    p++;
                    break;
                case 'b':      /* bits */
                    v = p[1] ? p + 1 : --argc ? *++argv : NULL;
                    if (!v) {
                        fprintf(stderr, "%s: `-b' requires an argument\n",
                                pname);
                        return 1;
                    }
		    b = strtoul(v, &ep, 10);
		    if (*ep || !(bits == 16 || bits == 32 || bits == 64)) {
                        fprintf(stderr, "%s: argument to `-b' should"
                                " be 16, 32 or 64\n", pname);
                    } else {
			bits = b;
		    }
                    p = "";     /* force to next argument */
                    break;
                case 'o':      /* origin */
                    v = p[1] ? p + 1 : --argc ? *++argv : NULL;
                    if (!v) {
                        fprintf(stderr, "%s: `-o' requires an argument\n",
                                pname);
                        return 1;
                    }
                    offset = readnum(v, &rn_error);
                    if (rn_error) {
                        fprintf(stderr,
                                "%s: `-o' requires a numeric argument\n",
                                pname);
                        return 1;
                    }
                    p = "";     /* force to next argument */
                    break;
                case 's':      /* sync point */
                    v = p[1] ? p + 1 : --argc ? *++argv : NULL;
                    if (!v) {
                        fprintf(stderr, "%s: `-s' requires an argument\n",
                                pname);
                        return 1;
                    }
                    add_sync(readnum(v, &rn_error), 0L);
                    if (rn_error) {
                        fprintf(stderr,
                                "%s: `-s' requires a numeric argument\n",
                                pname);
                        return 1;
                    }
                    p = "";     /* force to next argument */
                    break;
                case 'e':      /* skip a header */
                    v = p[1] ? p + 1 : --argc ? *++argv : NULL;
                    if (!v) {
                        fprintf(stderr, "%s: `-e' requires an argument\n",
                                pname);
                        return 1;
                    }
                    initskip = readnum(v, &rn_error);
                    if (rn_error) {
                        fprintf(stderr,
                                "%s: `-e' requires a numeric argument\n",
                                pname);
                        return 1;
                    }
                    p = "";     /* force to next argument */
                    break;
                case 'k':      /* skip a region */
                    v = p[1] ? p + 1 : --argc ? *++argv : NULL;
                    if (!v) {
                        fprintf(stderr, "%s: `-k' requires an argument\n",
                                pname);
                        return 1;
                    }
                    vv = strchr(v, ',');
                    if (!vv) {
                        fprintf(stderr,
                                "%s: `-k' requires two numbers separated"
                                " by a comma\n", pname);
                        return 1;
                    }
                    *vv++ = '\0';
                    nextsync = readnum(v, &rn_error);
                    if (rn_error) {
                        fprintf(stderr,
                                "%s: `-k' requires numeric arguments\n",
                                pname);
                        return 1;
                    }
                    synclen = readnum(vv, &rn_error);
                    if (rn_error) {
                        fprintf(stderr,
                                "%s: `-k' requires numeric arguments\n",
                                pname);
                        return 1;
                    }
                    add_sync(nextsync, synclen);
                    p = "";     /* force to next argument */
                    break;
                case 'p':      /* preferred vendor */
                    v = p[1] ? p + 1 : --argc ? *++argv : NULL;
                    if (!v) {
                        fprintf(stderr, "%s: `-p' requires an argument\n",
                                pname);
                        return 1;
                    }
                    if (!strcmp(v, "intel")) {
                        iflag_clear_all(&prefer); /* default */
                    } else if (!strcmp(v, "amd")) {
                        iflag_clear_all(&prefer);
                        iflag_set(&prefer, IF_AMD);
                        iflag_set(&prefer, IF_3DNOW);
                    } else if (!strcmp(v, "cyrix")) {
                        iflag_clear_all(&prefer);
                        iflag_set(&prefer, IF_CYRIX);
                        iflag_set(&prefer, IF_3DNOW);
                    } else if (!strcmp(v, "idt") ||
                               !strcmp(v, "centaur") ||
                               !strcmp(v, "winchip")) {
                        iflag_clear_all(&prefer);
                        iflag_set(&prefer, IF_3DNOW);
                    } else {
                        fprintf(stderr,
                                "%s: unknown vendor `%s' specified with `-p'\n",
                                pname, v);
                        return 1;
                    }
                    p = "";     /* force to next argument */
                    break;
                default:       /*bf */
                    fprintf(stderr, "%s: unrecognised option `-%c'\n",
                            pname, *p);
                    return 1;
                }
        } else if (!filename) {
            filename = p;
        } else {
            fprintf(stderr, "%s: more than one filename specified\n",
                    pname);
            return 1;
        }
    }

    if (!filename) {
        fprintf(stderr, help, pname);
        return 0;
    }

    if (strcmp(filename, "-")) {
        fp = fopen(filename, "rb");
        if (!fp) {
            fprintf(stderr, "%s: unable to open `%s': %s\n",
                    pname, filename, strerror(errno));
            return 1;
        }
    } else
        fp = stdin;

    if (initskip > 0)
        skip(initskip, fp);

    /*
     * This main loop is really horrible, and wants rewriting with
     * an axe. It'll stay the way it is for a while though, until I
     * find the energy...
     */

    p = q = buffer;
    nextsync = next_sync(offset, &synclen);
    do {
        uint32_t to_read = buffer + sizeof(buffer) - p;
	if ((nextsync || synclen) &&
	    to_read > nextsync - offset - (p - q))
            to_read = nextsync - offset - (p - q);
        if (to_read) {
            lenread = fread(p, 1, to_read, fp);
            if (lenread == 0)
                eof = true;     /* help along systems with bad feof */
        } else
            lenread = 0;
        p += lenread;
        if ((nextsync || synclen) &&
	    (uint32_t)offset == nextsync) {
            if (synclen) {
                fprintf(stdout, "%08"PRIX64"  skipping 0x%"PRIX32" bytes\n",
			offset, synclen);
                offset += synclen;
                skip(synclen, fp);
            }
            p = q = buffer;
            nextsync = next_sync(offset, &synclen);
        }
        while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
            lendis = disasm((uint8_t *)q, INSN_MAX, outbuf, sizeof(outbuf),
			    bits, offset, autosync, &prefer);
            if (!lendis || lendis > (p - q)
                || ((nextsync || synclen) &&
		    (uint32_t)lendis > nextsync - offset))
                lendis = eatbyte((uint8_t *) q, outbuf, sizeof(outbuf), bits);
            output_ins(offset, (uint8_t *) q, lendis, outbuf);
            q += lendis;
            offset += lendis;
        }
        if (q >= buffer + INSN_MAX) {
            uint8_t *r = (uint8_t *) buffer, *s = (uint8_t *) q;
            int count = p - q;
            while (count--)
                *r++ = *s++;
            p -= (q - buffer);
            q = buffer;
        }
    } while (lenread > 0 || !(eof || feof(fp)));

    if (fp != stdin)
        fclose(fp);

    return 0;
}
Exemple #10
0
long
consread(Chan *c, void *va, long count, vlong offset)
{
	int i, n, ch, eol;
	Pointer m;
	char *p, buf[64];

	if(c->qid.type & QTDIR)
		return devdirread(c, va, count, contab, nelem(contab), devgen);

	switch((ulong)c->qid.path) {
	default:
		error(Egreg);
	case Qsysctl:
		return readstr(offset, va, count, VERSION);
	case Qsysname:
		if(ossysname == nil)
			return 0;
		return readstr(offset, va, count, ossysname);
	case Qrandom:
		return randomread(va, count);
	case Qnotquiterandom:
		pseudoRandomBytes(va, count);
		return count;
	case Qpin:
		p = "pin set";
		if(up->env->pgrp->pin == Nopin)
			p = "no pin";
		return readstr(offset, va, count, p);
	case Qhostowner:
		return readstr(offset, va, count, eve);
	case Qhoststdin:
		return read(0, va, count);	/* should be pread */
	case Quser:
		return readstr(offset, va, count, up->env->user);
	case Qjit:
		snprint(buf, sizeof(buf), "%d", cflag);
		return readstr(offset, va, count, buf);
	case Qtime:
		snprint(buf, sizeof(buf), "%.lld", timeoffset + osusectime());
		return readstr(offset, va, count, buf);
	case Qdrivers:
		p = malloc(READSTR);
		if(p == nil)
			error(Enomem);
		n = 0;
		for(i = 0; devtab[i] != nil; i++)
			n += snprint(p+n, READSTR-n, "#%C %s\n", devtab[i]->dc,  devtab[i]->name);
		n = readstr(offset, va, count, p);
		free(p);
		return n;
	case Qmemory:
		return poolread(va, count, offset);

	case Qnull:
		return 0;
	case Qmsec:
		return readnum(offset, va, count, osmillisec(), NUMSIZE);
	case Qcons:
		qlock(&kbd.q);
		if(waserror()){
			qunlock(&kbd.q);
			nexterror();
		}

		if(dflag)
			error(Enonexist);

		while(!qcanread(lineq)) {
			qread(kbdq, &kbd.line[kbd.x], 1);
			ch = kbd.line[kbd.x];
			if(kbd.raw){
				qiwrite(lineq, &kbd.line[kbd.x], 1);
				continue;
			}
			eol = 0;
			switch(ch) {
			case '\b':
				if(kbd.x)
					kbd.x--;
				break;
			case 0x15:
				kbd.x = 0;
				break;
			case '\n':
			case 0x04:
				eol = 1;
			default:
				kbd.line[kbd.x++] = ch;
				break;
			}
			if(kbd.x == sizeof(kbd.line) || eol){
				if(ch == 0x04)
					kbd.x--;
				qwrite(lineq, kbd.line, kbd.x);
				kbd.x = 0;
			}
		}
		n = qread(lineq, va, count);
		qunlock(&kbd.q);
		poperror();
		return n;
	case Qscancode:
		if(offset == 0)
			return readstr(0, va, count, gkscanid);
		else
			return qread(gkscanq, va, count);
	case Qkeyboard:
		return qread(gkbdq, va, count);
	case Qpointer:
		m = mouseconsume();
		n = sprint(buf, "m%11d %11d %11d %11lud ", m.x, m.y, m.b, m.msec);
		if (count < n)
			n = count;
		memmove(va, buf, n);
		return n;
	case Qkprint:
		rlock(&kprintq.l);
		if(waserror()){
			runlock(&kprintq.l);
			nexterror();
		}
		n = qread(kprintq.q, va, count);
		poperror();
		runlock(&kprintq.l);
		return n;
	case Qsnarf: 
		if(offset == 0) {
			free(c->aux);
			c->aux = clipread();
		}
		if(c->aux == nil)
			return 0;
		return readstr(offset, va, count, c->aux);
	}
}
Exemple #11
0
static long
sdread(Chan *c, void *a, long n, vlong off)
{
	char *p, *e, *buf;
	SDpart *pp;
	SDunit *unit;
	SDev *sdev;
	ulong offset;
	int i, l, m, status;

	offset = off;
	switch(TYPE(c->qid)){
	default:
		error(Eperm);
	case Qtopctl:
		m = 64*1024;	/* room for register dumps */
		p = buf = malloc(m);
		if(p == nil)
			error(Enomem);
		e = p + m;
		qlock(&devslock);
		for(i = 0; i < nelem(devs); i++){
			sdev = devs[i];
			if(sdev && sdev->ifc->rtopctl)
				p = sdev->ifc->rtopctl(sdev, p, e);
		}
		qunlock(&devslock);
		n = readstr(off, a, n, buf);
		free(buf);
		return n;

	case Qtopdir:
	case Qunitdir:
		return devdirread(c, a, n, 0, 0, sdgen);

	case Qctl:
		sdev = sdgetdev(DEV(c->qid));
		if(sdev == nil)
			error(Enonexist);

		unit = sdev->unit[UNIT(c->qid)];
		m = 16*1024;	/* room for register dumps */
		p = malloc(m);
		if(p == nil)
			error(Enomem);
		l = snprint(p, m, "inquiry %.48s\n",
			(char*)unit->inquiry+8);
		qlock(&unit->ctl);
		/*
		 * If there's a device specific routine it must
		 * provide all information pertaining to night geometry
		 * and the garscadden trains.
		 */
		if(unit->dev->ifc->rctl)
			l += unit->dev->ifc->rctl(unit, p+l, m-l);
		if(unit->sectors == 0)
			sdinitpart(unit);
		if(unit->sectors){
			if(unit->dev->ifc->rctl == nil)
				l += snprint(p+l, m-l,
					"geometry %llud %lud\n",
					unit->sectors, unit->secsize);
			pp = unit->part;
			for(i = 0; i < unit->npart; i++){
				if(pp->valid)
					l += snprint(p+l, m-l,
						"part %s %llud %llud\n",
						pp->name, pp->start, pp->end);
				pp++;
			}
		}
		qunlock(&unit->ctl);
		decref(&sdev->r);
		l = readstr(offset, a, n, p);
		free(p);
		return l;

	case Qraw:
		sdev = sdgetdev(DEV(c->qid));
		if(sdev == nil)
			error(Enonexist);

		unit = sdev->unit[UNIT(c->qid)];
		qlock(&unit->raw);
		if(waserror()){
			qunlock(&unit->raw);
			decref(&sdev->r);
			nexterror();
		}
		if(unit->state == Rawdata){
			unit->state = Rawstatus;
			i = sdrio(unit->req, a, n);
		}
		else if(unit->state == Rawstatus){
			status = unit->req->status;
			unit->state = Rawcmd;
			free(unit->req);
			unit->req = nil;
			i = readnum(0, a, n, status, NUMSIZE);
		} else
			i = 0;
		qunlock(&unit->raw);
		decref(&sdev->r);
		poperror();
		return i;

	case Qpart:
		return sdbio(c, 0, a, n, off);
	}
}
Exemple #12
0
static long
consread(Chan *c, void *va, long n, vlong offset)
{
	int send;
	char *p, buf[64], ch;

	if(c->qid.type & QTDIR)
		return devdirread(c, va, n, contab, nelem(contab), devgen);

	switch((ulong)c->qid.path) {
	default:
		error(Egreg);

	case Qsysctl:
		return readstr(offset, va, n, VERSION);

	case Qsysname:
		if(ossysname == nil)
			return 0;
		return readstr(offset, va, n, ossysname);

	case Qrandom:
		return randomread(va, n);

	case Qnotquiterandom:
		genrandom(va, n);
		return n;

	case Qhostowner:
		return readstr(offset, va, n, eve);

	case Qhoststdin:
		return read(0, va, n);	/* should be pread */

	case Quser:
		return readstr(offset, va, n, up->env->user);

	case Qjit:
		snprint(buf, sizeof(buf), "%d", cflag);
		return readstr(offset, va, n, buf);

	case Qtime:
		snprint(buf, sizeof(buf), "%.lld", timeoffset + osusectime());
		return readstr(offset, va, n, buf);

	case Qdrivers:
		return devtabread(c, va, n, offset);

	case Qmemory:
		return poolread(va, n, offset);

	case Qnull:
		return 0;

	case Qmsec:
		return readnum(offset, va, n, osmillisec(), NUMSIZE);

	case Qcons:
		qlock(&kbd.q);
		if(waserror()){
			qunlock(&kbd.q);
			nexterror();
		}

		if(dflag)
			error(Enonexist);

        /* register this proc's read interest in the channel */
        /* this probably won't be done here.  the read/readn requests 
         * are going to be different watcher callbacks
         */
        
        /* the following waits for a signal from the keyboard handler */
        uv_mutex_lock(&line_lock);
		uv_cond_wait(&line_ready, &line_lock);
		n = qread(lineq, va, n);
        uv_mutex_unlock(&line_lock);
		qunlock(&kbd.q);
		poperror();
		return n;

	case Qscancode:
		if(offset == 0)
			return readstr(0, va, n, gkscanid);
		return qread(gkscanq, va, n);

	case Qkeyboard:
		return qread(gkbdq, va, n);

	case Qkprint:
		rlock(&kprintq.l);
		if(waserror()){
			runlock(&kprintq.l);
			nexterror();
		}
		n = qread(kprintq.q, va, n);
		poperror();
		runlock(&kprintq.l);
		return n;
	}
}
Exemple #13
0
static long
progread(Chan *c, void *va, long n, vlong offset)
{
	int i;
	Prog *p;
	Osenv *o;
	Mntwalk *mw;
	ulong grpid;
	char *a = va;
	Progctl *ctl;
	char mbuf[64], timebuf[12];
	char flag[10];

	if(c->qid.type & QTDIR)
		return devdirread(c, a, n, 0, 0, proggen);

	switch(QID(c->qid)){
	case Qdbgctl:
		ctl = c->aux;
		return qread(ctl->q, va, n);
	case Qstatus:
		acquire();
		p = progpid(PID(c->qid));
		if(p == nil || p->state == Pexiting || p->R.M == H) {
			release();
			snprint(up->genbuf, sizeof(up->genbuf), "%8lud %8d %10s %s %10s %5dK %s",
				PID(c->qid),
				0,
				eve,
				progtime(0, timebuf, timebuf+sizeof(timebuf)),
				progstate[Pexiting],
				0,
				"[$Sys]");
			return readstr(offset, va, n, up->genbuf);
		}
		modstatus(&p->R, mbuf, sizeof(mbuf));
		o = p->osenv;
		snprint(up->genbuf, sizeof(up->genbuf), "%8d %8d %10s %s %10s %5dK %s",
			p->pid,
			p->group!=nil? p->group->id: 0,
			o->user,
			progtime(p->ticks, timebuf, timebuf+sizeof(timebuf)),
			progstate[p->state],
			progsize(p),
			mbuf);
		release();
		return readstr(offset, va, n, up->genbuf);
	case Qwait:
		return qread(c->aux, va, n);
	case Qns:
		acquire();
		if(waserror()){
			release();
			nexterror();
		}
		p = progpid(PID(c->qid));
		if(p == nil)
			error(Ethread);
		mw = c->aux;
		if(mw->cddone){
			poperror();
			release();
			return 0;
		}
		o = p->osenv;
		mntscan(mw, o->pgrp);
		if(mw->mh == 0) {
			mw->cddone = 1;
			i = snprint(a, n, "cd %s\n", o->pgrp->dot->name->s);
			poperror();
			release();
			return i;
		}
		int2flag(mw->cm->mflag, flag);
		if(strcmp(mw->cm->to->name->s, "#M") == 0){
			i = snprint(a, n, "mount %s %s %s %s\n", flag,
				mw->cm->to->mchan->name->s,
				mw->mh->from->name->s, mw->cm->spec? mw->cm->spec : "");
		}else
			i = snprint(a, n, "bind %s %s %s\n", flag,
				mw->cm->to->name->s, mw->mh->from->name->s);
		poperror();
		release();
		return i;
	case Qnsgrp:
		acquire();
		p = progpid(PID(c->qid));
		if(p == nil) {
			release();
			error(Ethread);
		}
		grpid = ((Osenv *)p->osenv)->pgrp->pgrpid;
		release();
		return readnum(offset, va, n, grpid, NUMSIZE);
	case Qpgrp:
		acquire();
		p = progpid(PID(c->qid));
		if(p == nil) {
			release();
			error(Ethread);
		}
		grpid = p->group!=nil? p->group->id: 0;
		release();
		return readnum(offset, va, n, grpid, NUMSIZE);
	case Qstack:
		acquire();
		p = progpid(PID(c->qid));
		if(p == nil || p->state == Pexiting) {
			release();
			error(Ethread);
		}
		if(p->state == Pready) {
			release();
			error(Estopped);
		}
		n = progstack(&p->R, p->state, va, n, offset);
		release();
		return n;
	case Qheap:
		acquire();
		if(waserror()){
			release();
			nexterror();
		}
		n = progheap(c->aux, va, n, offset);
		if(n == -1)
			error(Emisalign);
		poperror();
		release();
		return n;
	case Qfd:
		acquire();
		if(waserror()) {
			release();
			nexterror();
		}
		p = progpid(PID(c->qid));
		if(p == nil)
			error(Ethread);
		o = p->osenv;
		n = progfds(o, va, n, offset);
		poperror();
		release();
		return n;
	case Qexception:
		acquire();
		p = progpid(PID(c->qid));
		if(p == nil) {
			release();
			error(Ethread);
		}
		if(p->exstr == nil)
			up->genbuf[0] = 0;
		else
			snprint(up->genbuf, sizeof(up->genbuf), p->exstr);
		release();
		return readstr(offset, va, n, up->genbuf);
	}
	error(Egreg);
	return 0;
}
Exemple #14
0
/*
    Perform the actions associated with praying at an altar and giving a
    donation.
*/
void act_donation_pray(void)
{
	unsigned long k,temp ;

    while (1)
        {
        lprcat("\n\n");
        cursor(1,24);
        cltoeoln();
        cursor(1,23);
        cltoeoln();
        lprcat("how much do you donate? ");
        k = readnum((long)c[GOLD]);

	lprcat("\n");

        /* make giving zero gold equivalent to 'just pray'ing.  Allows player to
           'just pray' in command mode, without having to add yet another command.
        */
        if (k == 0)
            {
            act_just_pray();
            return;
            }

        if (c[GOLD] >= k)
            {
            temp = c[GOLD] / 10 ;
            c[GOLD] -= k;
            bottomline();

            /* if player gave less than 10% of _original_ gold, make a monster
            */
            if (k < temp || k < rnd(50))
                {
                createmonster(makemonst(level+1));
                c[AGGRAVATE] += 200;
                return;
                }
            if (rnd(101) > 50)
                {
                act_prayer_heard();
                return;
                }
            if (rnd(43) == 5)
                {
                if (c[WEAR])
                    lprcat("You feel your armor vibrate for a moment");
                enchantarmor();
                return;
                }
            if (rnd(43) == 8)
                {
                if (c[WIELD])
                    lprcat("You feel your weapon vibrate for a moment");
                enchweapon();
                return;
                }

            lprcat("Thank You.");
            return ;
            }

        /* Player donates more gold than they have.  Loop back around so
           player can't escape the altar for free.
        */
        lprcat("You don't have that much!");
        }
}
Exemple #15
0
long
netifread(Netif *nif, Chan *c, void *a, long n, vlong off)
{
	int i;
	Netfile *f;
	char *p, *op, *e;
	long offset;

	if(c->qid.type & QTDIR)
		return devdirread(c, a, n, (Dirtab*)nif, 0, netifgen);

	offset = off;
	switch(NETTYPE(c->qid.path)){
	case Ndataqid:
		f = nif->f[NETID(c->qid.path)];
		return qread(f->iq, a, n);
	case Nctlqid:
		return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE);
	case Nstatqid:
		p = op = malloc(READSTR);
		if(p == nil)
			return 0;
		e = p + READSTR;
		p = seprint(p, e, "in: %llud\n", nif->inpackets);
		p = seprint(p, e, "link: %d\n", nif->link);
		p = seprint(p, e, "out: %llud\n", nif->outpackets);
		p = seprint(p, e, "crc errs: %d\n", nif->crcs);
		p = seprint(p, e, "overflows: %d\n", nif->overflows);
		p = seprint(p, e, "input overflows: %d\n", nif->inoverflows);
		p = seprint(p, e, "output overflows: %d\n", nif->outoverflows);
		p = seprint(p, e, "loopback frames: %d\n", nif->loopbacks);
		p = seprint(p, e, "framing errs: %d\n", nif->frames);
		p = seprint(p, e, "buffer errs: %d\n", nif->buffs);
		p = seprint(p, e, "output errs: %d\n", nif->oerrs);
		p = seprint(p, e, "prom: %d\n", nif->prom);
		p = seprint(p, e, "mbps: %d\n", nif->mbps);
		p = seprint(p, e, "limit: %d\n", nif->limit);
		p = seprint(p, e, "addr: ");
		for(i = 0; i < nif->alen; i++)
			p = seprint(p, e, "%2.2ux", nif->addr[i]);
		p = seprint(p, e, "\n");
		seprint(p, e, "oq len: %d\n", qblen(nif->oq));
		n = readstr(offset, a, n, op);
		free(op);
		return n;
	case N3statqid:
		f = nif->f[NETID(c->qid.path)];
		p = op = malloc(READSTR);
		if(p == nil)
			return 0;
		e = p + READSTR;
		p = seprint(p, e, "in qlen: %ud\n", qblen(f->iq));
		seprint(p, e, "input overflows: %ud\n", f->inoverflows);
		n = readstr(offset, a, n, op);
		free(op);
		return n;
	case Naddrqid:
		p = op = malloc(READSTR);
		if(p == nil)
			return 0;
		e = p + READSTR;
		for(i = 0; i < nif->alen; i++)
			p = seprint(p, e, "%2.2ux", nif->addr[i]);
		n = readstr(offset, a, n, op);
		free(op);
		return n;
	case Ntypeqid:
		f = nif->f[NETID(c->qid.path)];
		return readnum(offset, a, n, f->type, NUMSIZE);
	case Nifstatqid:
		return 0;
	case Nmtuqid:
		snprint(up->genbuf, sizeof(up->genbuf), "%11.ud %11.ud %11.ud\n", nif->minmtu, nif->mtu, nif->maxmtu);
		return readstr(offset, a, n, up->genbuf);
	case Nmaxmtuqid:
		snprint(up->genbuf, sizeof(up->genbuf), "%d", nif->maxmtu);
		return readstr(offset, a, n, up->genbuf);
	}
	error(Ebadarg);
	return -1;	/* not reached */
}
Exemple #16
0
int main(int argc, char **argv) {
    unsigned char buffer[INSN_MAX * 2], *p, *q;
    char outbuf[256];
    char *pname = *argv;
    char *filename = NULL;
    unsigned long nextsync, synclen, initskip = 0L;
    int lenread, lendis;
    int autosync = FALSE;
    int bits = 16;
    int eof = FALSE;
    int rn_error;
    long offset;
    FILE *fp;

    offset = 0;
    init_sync();

    while (--argc) {
	char *v, *vv, *p = *++argv;
	if (*p == '-') {
	    p++;
	    while (*p) switch (tolower(*p)) {
	      case 'a':		       /* auto or intelligent sync */
	      case 'i':
		autosync = TRUE;
		p++;
		break;
	      case 'h':
		fprintf(stderr, help);
		return 0;
	      case 'r':
		fprintf(stderr, "NDISASM version " NASM_VER "\n");
		return 0;
	      case 'u':		       /* USE32 */
		bits = 32;
		p++;
		break;
	      case 'b':		       /* bits */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-b' requires an argument\n", pname);
		    return 1;
		}
		if (!strcmp(v, "16"))
		    bits = 16;
		else if (!strcmp(v, "32"))
		    bits = 32;
		else {
		    fprintf(stderr, "%s: argument to `-b' should"
			    " be `16' or `32'\n", pname);
		}
		p = "";		       /* force to next argument */
		break;
	      case 'o':		       /* origin */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-o' requires an argument\n", pname);
		    return 1;
		}
		offset = readnum (v, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-o' requires a numeric argument\n",
			    pname);
		    return 1;
		}
		p = "";		       /* force to next argument */
		break;
	      case 's':		       /* sync point */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-s' requires an argument\n", pname);
		    return 1;
		}
		add_sync (readnum (v, &rn_error), 0L);
		if (rn_error) {
		    fprintf(stderr, "%s: `-s' requires a numeric argument\n",
			    pname);
		    return 1;
		}
		p = "";		       /* force to next argument */
		break;
	      case 'e':		       /* skip a header */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-e' requires an argument\n", pname);
		    return 1;
		}
		initskip = readnum (v, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-e' requires a numeric argument\n",
			    pname);
		    return 1;
		}
		p = "";		       /* force to next argument */
		break;
	      case 'k':		       /* skip a region */
		v = p[1] ? p+1 : --argc ? *++argv : NULL;
		if (!v) {
		    fprintf(stderr, "%s: `-k' requires an argument\n", pname);
		    return 1;
		}
		vv = strchr(v, ',');
		if (!vv) {
		    fprintf(stderr, "%s: `-k' requires two numbers separated"
			    " by a comma\n", pname);
		    return 1;
		}
		*vv++ = '\0';
		nextsync = readnum (v, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-k' requires numeric arguments\n",
			    pname);
		    return 1;
		}
		synclen = readnum (vv, &rn_error);
		if (rn_error) {
		    fprintf(stderr, "%s: `-k' requires numeric arguments\n",
			    pname);
		    return 1;
		}
		add_sync (nextsync, synclen);
		p = "";		       /* force to next argument */
		break;
	    }
	} else if (!filename) {
	    filename = p;
	} else {
	    fprintf(stderr, "%s: more than one filename specified\n", pname);
	    return 1;
	}
    }

    if (!filename) {
	fprintf(stderr, help, pname);
	return 0;
    }

    fp = fopen(filename, "rb");
    if (!fp) {
	fprintf(stderr, "%s: unable to open `%s': %s\n",
		pname, filename, strerror(errno));
	return 1;
    }
    if (initskip > 0)
	skip (initskip, fp);

    /*
     * This main loop is really horrible, and wants rewriting with
     * an axe. It'll stay the way it is for a while though, until I
     * find the energy...
     */

    p = q = buffer;
    nextsync = next_sync (offset, &synclen);
    do {
	unsigned long to_read = buffer+sizeof(buffer)-p;
	if (to_read > nextsync-offset-(p-q))
	    to_read = nextsync-offset-(p-q);
	lenread = fread (p, 1, to_read, fp);
	if (lenread == 0)
	    eof = TRUE;		       /* help along systems with bad feof */
	p += lenread;
	if (offset == (long)nextsync) {
	    if (synclen) {
		printf("%08lX  skipping 0x%lX bytes\n", offset, synclen);
		offset += synclen;
		skip (synclen, fp);
	    }
	    p = q = buffer;
	    nextsync = next_sync (offset, &synclen);
	}
	while (p > q && (p - q >= INSN_MAX || lenread == 0)) {
	    lendis = disasm (q, outbuf, bits, offset, autosync);
	    if (!lendis || lendis > (p - q) ||
		lendis > (int)(nextsync-offset))
		lendis = eatbyte (q, outbuf);
	    output_ins (offset, q, lendis, outbuf);
	    q += lendis;
	    offset += lendis;
	}
	if (q >= buffer+INSN_MAX) {
	    unsigned char *r = buffer, *s = q;
	    int count = p - q;
	    while (count--)
		*r++ = *s++;
	    p -= (q - buffer);
	    q = buffer;
	}
    } while (lenread > 0 || !(eof || feof(fp)));
    fclose (fp);
    return 0;
}
Exemple #17
0
static long
consread(Chan *c, void *buf, long n, vlong off)
{
	ulong l;
	Mach *mp;
	char *b, *bp;
	char tmp[256];		/* must be >= 18*NUMSIZE (Qswap) */
	int i, k, id;
	vlong offset = off;
	extern char configfile[];

	if(n <= 0)
		return n;

	switch((ulong)c->qid.path){
	case Qdir:
		return devdirread(c, buf, n, consdir, nelem(consdir), devgen);

	case Qcons:
		error(Egreg);

	case Qcputime:
		k = offset;
		if(k >= 6*NUMSIZE)
			return 0;
		if(k+n > 6*NUMSIZE)
			n = 6*NUMSIZE - k;
		/* easiest to format in a separate buffer and copy out */
		for(i=0; i<6 && NUMSIZE*i<k+n; i++){
			l = up->time[i];
			if(i == TReal)
				l = MACHP(0)->ticks - l;
			l = TK2MS(l);
			readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
		}
		memmove(buf, tmp+k, n);
		return n;

	case Qkmesg:
		/*
		 * This is unlocked to avoid tying up a process
		 * that's writing to the buffer.  kmesg.n never 
		 * gets smaller, so worst case the reader will
		 * see a slurred buffer.
		 */
		if(off >= kmesg.n)
			n = 0;
		else{
			if(off+n > kmesg.n)
				n = kmesg.n - off;
			memmove(buf, kmesg.buf+off, n);
		}
		return n;
		
	case Qkprint:
		return qread(kprintoq, buf, n);

	case Qpgrpid:
		return readnum((ulong)offset, buf, n, up->pgrp->pgrpid, NUMSIZE);

	case Qpid:
		return readnum((ulong)offset, buf, n, up->pid, NUMSIZE);

	case Qppid:
		return readnum((ulong)offset, buf, n, up->parentpid, NUMSIZE);

	case Qtime:
		return readtime((ulong)offset, buf, n);

	case Qbintime:
		return readbintime(buf, n);

	case Qhostowner:
		return readstr((ulong)offset, buf, n, eve);

	case Qhostdomain:
		return readstr((ulong)offset, buf, n, hostdomain);

	case Quser:
		return readstr((ulong)offset, buf, n, up->user);

	case Qnull:
		return 0;

	case Qconfig:
		return readstr((ulong)offset, buf, n, configfile);

	case Qsysstat:
		b = smalloc(conf.nmach*(NUMSIZE*11+1) + 1);	/* +1 for NUL */
		bp = b;
		for(id = 0; id < 32; id++) {
			if(active.machs & (1<<id)) {
				mp = MACHP(id);
				readnum(0, bp, NUMSIZE, id, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->load, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE,
					(mp->perf.avg_inidle*100)/mp->perf.period,
					NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE,
					(mp->perf.avg_inintr*100)/mp->perf.period,
					NUMSIZE);
				bp += NUMSIZE;
				*bp++ = '\n';
			}
		}
		if(waserror()){
			free(b);
			nexterror();
		}
		n = readstr((ulong)offset, buf, n, b);
		free(b);
		poperror();
		return n;

	case Qswap:
		snprint(tmp, sizeof tmp,
			"%lud memory\n"
			"%d pagesize\n"
			"%lud kernel\n"
			"%lud/%lud user\n"
			"%lud/%lud swap\n"
			"%lud/%lud kernel malloc\n"
			"%lud/%lud kernel draw\n",
			conf.npage*BY2PG,
			BY2PG,
			conf.npage-conf.upages,
			palloc.user-palloc.freecount, palloc.user,
			conf.nswap-swapalloc.free, conf.nswap,
			mainmem->cursize, mainmem->maxsize,
			imagmem->cursize, imagmem->maxsize);

		return readstr((ulong)offset, buf, n, tmp);

	case Qsysname:
		if(sysname == nil)
			return 0;
		return readstr((ulong)offset, buf, n, sysname);

	case Qrandom:
		return randomread(buf, n);

	case Qdrivers:
		b = smalloc(READSTR);
		k = 0;
		for(i = 0; devtab[i] != nil; i++)
			k += snprint(b+k, READSTR-k, "#%C %s\n",
				devtab[i]->dc, devtab[i]->name);
		if(waserror()){
			free(b);
			nexterror();
		}
		n = readstr((ulong)offset, buf, n, b);
		poperror();
		free(b);
		return n;

	case Qzero:
		memset(buf, 0, n);
		return n;
	
	case Qmordor:
		error("one does not simply read from mordor");
		return 0;

	case Qosversion:
		snprint(tmp, sizeof tmp, "2000");
		n = readstr((ulong)offset, buf, n, tmp);
		return n;

	default:
		print("consread %#llux\n", c->qid.path);
		error(Egreg);
	}
	return -1;		/* never reached */
}
Exemple #18
0
long
netifread(struct ether *nif, struct chan *c, void *a, long n,
	  uint32_t offset)
{
	int i, j;
	struct netfile *f;
	char *p;

	if (c->qid.type & QTDIR)
		return devdirread(c, a, n, (struct dirtab *)nif, 0, netifgen);

	switch (NETTYPE(c->qid.path)) {
		case Ndataqid:
			f = nif->f[NETID(c->qid.path)];
			return qread(f->in, a, n);
		case Nctlqid:
			return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE);
		case Nstatqid:
			p = kzmalloc(READSTR, 0);
			if (p == NULL)
				return 0;
			j = snprintf(p, READSTR, "in: %d\n", nif->inpackets);
			j += snprintf(p + j, READSTR - j, "link: %d\n", nif->link);
			j += snprintf(p + j, READSTR - j, "out: %d\n", nif->outpackets);
			j += snprintf(p + j, READSTR - j, "crc errs: %d\n", nif->crcs);
			j += snprintf(p + j, READSTR - j, "overflows: %d\n",
						  nif->overflows);
			j += snprintf(p + j, READSTR - j, "soft overflows: %d\n",
						  nif->soverflows);
			j += snprintf(p + j, READSTR - j, "framing errs: %d\n",
						  nif->frames);
			j += snprintf(p + j, READSTR - j, "buffer errs: %d\n", nif->buffs);
			j += snprintf(p + j, READSTR - j, "output errs: %d\n", nif->oerrs);
			j += snprintf(p + j, READSTR - j, "prom: %d\n", nif->prom);
			j += snprintf(p + j, READSTR - j, "mbps: %d\n", nif->mbps);
			j += snprintf(p + j, READSTR - j, "addr: ");
			for (i = 0; i < nif->alen; i++)
				j += snprintf(p + j, READSTR - j, "%02.2x", nif->addr[i]);
			j += snprintf(p + j, READSTR - j, "\n");
			j += snprintf(p + j, READSTR - j, "feat: ");
			if (nif->feat & NETF_IPCK)
				j += snprintf(p + j, READSTR - j, "ipck ");
			if (nif->feat & NETF_UDPCK)
				j += snprintf(p + j, READSTR - j, "udpck ");
			if (nif->feat & NETF_TCPCK)
				j += snprintf(p + j, READSTR - j, "tcppck ");
			if (nif->feat & NETF_PADMIN)
				j += snprintf(p + j, READSTR - j, "padmin ");
			if (nif->feat & NETF_SG)
				j += snprintf(p + j, READSTR - j, "sg ");
			if (nif->feat & NETF_TSO)
				j += snprintf(p + j, READSTR - j, "tso ");
			if (nif->feat & NETF_LRO)
				j += snprintf(p + j, READSTR - j, "lro ");
			snprintf(p + j, READSTR - j, "\n");
			n = readstr(offset, a, n, p);
			kfree(p);
			return n;
		case Naddrqid:
			p = kzmalloc(READSTR, 0);
			if (p == NULL)
				return 0;
			j = 0;
			for (i = 0; i < nif->alen; i++)
				j += snprintf(p + j, READSTR - j, "%02.2x", nif->addr[i]);
			n = readstr(offset, a, n, p);
			kfree(p);
			return n;
		case Ntypeqid:
			f = nif->f[NETID(c->qid.path)];
			return readnum(offset, a, n, f->type, NUMSIZE);
		case Nifstatqid:
			return 0;
	}
	error(Ebadarg);
	return -1;	/* not reached */
}
Exemple #19
0
/*
	function to drop an object
 */
void dropobj (void)
{
	int i, pitflag=0;
	char *p;
	long amt;

	p = &item[playerx][playery];
	while (1) {
		if ((i = whatitem("drop"))==ESC)  
			return;
		if (i=='*') 
			showstr(); 
		else {
			/* drop some gold */
			if (i=='.')	{
				if (*p == OPIT) pitflag=1;
				if (*p && !pitflag) { 
				lprcat("\nThere's something here already!"); 
						return; 
				}
				lprcat("\n\n");
				cl_dn(1,23);
				lprcat("How much gold do you drop? ");
				if ((amt=readnum((long)c[GOLD])) <= 0) return;
				if (amt>c[GOLD]) { 
					lprcat("\nYou don't have that much!"); 
					return; 
				}
				if (amt<=32767) { 
					*p=OGOLDPILE; 
					i=(int)amt; 
				}
				else if (amt<=327670L) { 
					*p=ODGOLD; 
					i=(int)amt/10; 
					amt = 10L*i; 
				}
				else if (amt<=3276700L) { 
					*p=OMAXGOLD; 
					i=(int)amt/100; 
					amt = 100L*i; 
				}
				else if (amt<=32767000L) { 
					*p=OKGOLD; 
					i=(int)amt/1000; 
					amt = 1000L*i; 
				}
				else { 
					*p=OKGOLD; 
					i=(int)32767; 
					amt = 32767000L; 
				}
				c[GOLD] -= amt; 
				lprintf("You drop %d gold piece%s.",(long)amt,(amt==1)?"":"s");
				if (pitflag) {
					*p = OPIT;
					lprcat("\nThe gold disappears down the pit.");
				} else
					iarg[playerx][playery]=i; 
				bottomgold();
				know[playerx][playery]=0; 
				dropflag=1;  
				return;
			}
			drop_object(i-'a');
			return;
		}
	}
}
Exemple #20
0
static long
consread(Chan *c, void *va, long n, vlong offset)
{
	int send;
	char buf[64], ch;

	if(c->qid.type & QTDIR)
		return devdirread(c, va, n, contab, nelem(contab), devgen);

	switch((ulong)c->qid.path) {
	default:
		error(Egreg);

	case Qsysctl:
		return readstr(offset, va, n, VERSION);

	case Qsysname:
		if(ossysname == nil)
			return 0;
		return readstr(offset, va, n, ossysname);

	case Qrandom:
		return randomread(va, n);

	case Qnotquiterandom:
		genrandom(va, n);
		return n;

	case Qhostowner:
		return readstr(offset, va, n, eve);

	case Qhoststdin:
		return read(0, va, n);	/* should be pread */

	case Quser:
		return readstr(offset, va, n, up->env->user);

	case Qjit:
		snprint(buf, sizeof(buf), "%d", cflag);
		return readstr(offset, va, n, buf);

	case Qtime:
		snprint(buf, sizeof(buf), "%.lld", timeoffset + osusectime());
		return readstr(offset, va, n, buf);

	case Qdrivers:
		return devtabread(c, va, n, offset);

	case Qmemory:
		return poolread(va, n, offset);

	case Qnull:
		return 0;

	case Qmsec:
		return readnum(offset, va, n, osmillisec(), NUMSIZE);

	case Qcons:
		qlock(&kbd.q);
		if(waserror()){
			qunlock(&kbd.q);
			nexterror();
		}

		if(dflag)
			error(Enonexist);

		while(!qcanread(lineq)) {
			if(qread(kbdq, &ch, 1) == 0)
				continue;
			send = 0;
			if(ch == 0){
				/* flush output on rawoff -> rawon */
				if(kbd.x > 0)
					send = !qcanread(kbdq);
			}else if(kbd.raw){
				kbd.line[kbd.x++] = ch;
				send = !qcanread(kbdq);
			}else{
				switch(ch){
				case '\b':
					if(kbd.x)
						kbd.x--;
					break;
				case 0x15:
					kbd.x = 0;
					break;
				case 0x04:
					send = 1;
					break;
				case '\n':
					send = 1;
				default:
					kbd.line[kbd.x++] = ch;
					break;
				}
			}
			if(send || kbd.x == sizeof kbd.line){
				qwrite(lineq, kbd.line, kbd.x);
				kbd.x = 0;
			}
		}
		n = qread(lineq, va, n);
		qunlock(&kbd.q);
		poperror();
		return n;

	case Qscancode:
		if(offset == 0)
			return readstr(0, va, n, gkscanid);
		return qread(gkscanq, va, n);

	case Qkeyboard:
		return qread(gkbdq, va, n);

	case Qkprint:
		rlock(&kprintq.l);
		if(waserror()){
			runlock(&kprintq.l);
			nexterror();
		}
		n = qread(kprintq.q, va, n);
		poperror();
		runlock(&kprintq.l);
		return n;
	}
}
Exemple #21
0
static int
processor(uint8_t *f, int fd, size_t l)
{
	uint8_t	frag[33];
	size_t	start, off;
	ssize_t	scan;
	int	rc = EXIT_SUCCESS;
	int	stop = 0;
	char	ch = 0;

	if (stdin_nobuf()) {
		return EXIT_FAILURE;
	}

	while (1) {
		if (0xa != ch) {
			printf("\n> ");
		}

		ch = fgetc(stdin);
		printf("%c", ch);

		switch (ch) {
		case '+':
			if (readnum(&start)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			printf("%lx\n", off+start);
			break;
		case '-':
			if (readnum(&start)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			printf("%lx\n", start-off);
			break;
		case 'e':
			if (readnum(&off)) {
				printf("\nINVALID OFFSET\n");
				break;
			}

			if (-1 == expand(&f, fd, l, off)) {
				stop = 1;
				rc = EXIT_FAILURE;
				break;
			}

			l += off;
			printf("\nEXTENDED %lu BYTES\n", (long unsigned)off);
			break;
		case 'f':
			if (readnum(&start) || (start >= l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			if (-1 == read_fragment(frag, off)) {
				printf("\nINVALID FRAGMENT\n");
				break;
			}

			printf("\nFIND FRAG FROM %lx SIZE %lu\n", start, off);
			find_frag(f+start, l-start, start, frag, off);
			break;
		case 'i':
			if (readnum(&start) || (start >= l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (-1 == expand(&f, fd, l, 1)) {
				stop = 1;
				rc = EXIT_FAILURE;
				break;
			}
			l++;

			if (-1 == shift_by(f+start, l-start)) {
				stop = 1;
				rc = EXIT_FAILURE;
				break;
			}
			break;
		case 'l':
			printf(" %lx\n", l);
			break;
		case 0x4:
		case 'q':
			printf("\nQUIT\n");
			stop++;
			break;

		case 'r':
			if (readnum(&start) || (start >=l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off) || ((start+off) > l)) {
				printf("\nINVALID OFFSET\n");
				rc = EXIT_FAILURE;
				break;
			}

			if (0 == off) {
				off = (l - start);
			}

			printhex(f+start, off, start);
			break;
		case 's':
			if (readnum(&start) || (start >= l)) {
				printf("\nINVALID START\n");
				break;
			}

			if (readnum(&off)) {
				printf("\nINVALID LENGTH\n");
				break;
			}

			if (-1 == read_fragment(frag, off)) {
				printf("\nINVALID FRAGMENT\n");
				break;
			}

			printf("\nSCAN FRAG FROM %lx SIZE %lu\n", start, off);
			while (-1 != (scan = find_frag(f+start, l-start,
					    start, frag, off))) {
				start += scan + 1;
			}
			break;
		case 'w':
			if (readnum(&start) || (start >=l)) {
				printf("\nINVALID START\n");
				break;
			}

			writehex(f+start, l-start);
			break;
		}


		if (stop) {
			break;
		}
	}

	return rc;
}
Exemple #22
0
static int32_t
consread(Chan *c, void *buf, int32_t n, int64_t off)
{
	Proc *up = externup();
	uint64_t l;
	Mach *mp;
	char *b, *bp, *s, *e;
	char tmp[512];		/* Qswap is 381 bytes at clu */
	int i, k, id;
	int32_t offset;


	if(n <= 0)
		return n;

	offset = off;
	switch((uint32_t)c->qid.path){
	case Qdir:
		return devdirread(c, buf, n, consdir, nelem(consdir), devgen);

	case Qcons:
		error(Egreg);

	case Qcputime:
		k = offset;
		if(k >= 6*NUMSIZE)
			return 0;
		if(k+n > 6*NUMSIZE)
			n = 6*NUMSIZE - k;
		/* easiest to format in a separate buffer and copy out */
		for(i=0; i<6 && NUMSIZE*i<k+n; i++){
			l = up->time[i];
			if(i == TReal)
				l = sys->ticks - l;
			l = TK2MS(l);
			readnum(0, tmp+NUMSIZE*i, NUMSIZE, l, NUMSIZE);
		}
		memmove(buf, tmp+k, n);
		return n;

	case Qkmesg:
		/*
		 * This is unlocked to avoid tying up a process
		 * that's writing to the buffer.  kmesg.n never
		 * gets smaller, so worst case the reader will
		 * see a slurred buffer.
		 */
		if(off >= kmesg.n)
			n = 0;
		else{
			if(off+n > kmesg.n)
				n = kmesg.n - off;
			memmove(buf, kmesg.buf+off, n);
		}
		return n;

	case Qkprint:
		error(Egreg);

	case Qpgrpid:
		return readnum(offset, buf, n, up->pgrp->pgrpid, NUMSIZE);

	case Qpid:
		return readnum(offset, buf, n, up->pid, NUMSIZE);

	case Qppid:
		return readnum(offset, buf, n, up->parentpid, NUMSIZE);

	case Qtime:
		return readtime(offset, buf, n);

	case Qbintime:
		return readbintime(buf, n);

	case Qhostowner:
		return readstr(offset, buf, n, eve);

	case Qhostdomain:
		return readstr(offset, buf, n, hostdomain);

	case Quser:
		return readstr(offset, buf, n, up->user);

	case Qnull:
		return 0;

	case Qsysstat:
		n = MACHMAX*(NUMSIZE*11+2+1);
		b = smalloc(n + 1);	/* +1 for NUL */
		bp = b;
		e = bp + n;
		for(id = 0; id < MACHMAX; id++)
			if((mp = sys->machptr[id]) != nil && mp->online){
				readnum(0, bp, NUMSIZE, mp->machno, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->cs, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->intr, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->syscall, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->pfault, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->tlbfault, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, mp->tlbpurge, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, sys->load, NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE,
					(mp->perf.avg_inidle*100)/mp->perf.period,
					NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE,
					(mp->perf.avg_inintr*100)/mp->perf.period,
					NUMSIZE);
				bp += NUMSIZE;
				readnum(0, bp, NUMSIZE, 0, NUMSIZE); /* sched # */
				bp += NUMSIZE;
				bp = strecpy(bp, e, rolename[mp->NIX.nixtype]);
				*bp++ = '\n';
			}
		if(waserror()){
			free(b);
			nexterror();
		}
		n = readstr(offset, buf, n, b);
		free(b);
		poperror();
		return n;

	case Qswap:
		tmp[0] = 0;
		s = seprintpagestats(tmp, tmp + sizeof tmp);
		s = seprintphysstats(s, tmp + sizeof tmp);
		b = buf;
		l = s - tmp;
		i = readstr(offset, b, l, tmp);
		b += i;
		n -= i;
		if(offset > l)
			offset -= l;
		else
			offset = 0;

		return i + mallocreadsummary(c, b, n, offset);

	case Qsysname:
		if(sysname == nil)
			return 0;
		return readstr(offset, buf, n, sysname);

	case Qrandom:
		return randomread(buf, n);

	case Qurandom:
		return urandomread(buf, n);

	case Qdrivers:
		return devtabread(c, buf, n, off);

	case Qzero:
		memset(buf, 0, n);
		return n;

	case Qosversion:
		snprint(tmp, sizeof tmp, "2000");
		n = readstr(offset, buf, n, tmp);
		return n;

	case Qdebug:
		s = seprint(tmp, tmp + sizeof tmp, "locks %lu\n", lockstats.locks);
		s = seprint(s, tmp + sizeof tmp, "glare %lu\n", lockstats.glare);
		s = seprint(s, tmp + sizeof tmp, "inglare %lu\n", lockstats.inglare);
		s = seprint(s, tmp + sizeof tmp, "qlock %lu\n", qlockstats.qlock);
		seprint(s, tmp + sizeof tmp, "qlockq %lu\n", qlockstats.qlockq);
		return readstr(offset, buf, n, tmp);
		break;

	case Qsyscall:
		snprint(tmp, sizeof tmp, "%s", printallsyscalls ? "on" : "off");
		return readstr(offset, buf, n, tmp);
		break;
	default:
		print("consread %#llx\n", c->qid.path);
		error(Egreg);
	}
	return -1;		/* never reached */
}
Exemple #23
0
static void elf_deflabel(char *name, long segment, long offset,
                         int is_global, char *special)
{
    int pos = strslen;
    struct Symbol *sym;
    int special_used = FALSE;

#if defined(DEBUG) && DEBUG>2
    fprintf(stderr,
            " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
            name, segment, offset, is_global, special);
#endif
    if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
        /*
         * This is a NASM special symbol. We never allow it into
         * the ELF symbol table, even if it's a valid one. If it
         * _isn't_ a valid one, we should barf immediately.
         */
        if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
            strcmp(name, "..got") && strcmp(name, "..plt") &&
            strcmp(name, "..sym"))
            error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
        return;
    }

    if (is_global == 3) {
        struct Symbol **s;
        /*
         * Fix up a forward-reference symbol size from the first
         * pass.
         */
        for (s = &fwds; *s; s = &(*s)->nextfwd)
            if (!strcmp((*s)->name, name)) {
                struct tokenval tokval;
                expr *e;
                char *p = special;

                while (*p && !isspace(*p))
                    p++;
                while (*p && isspace(*p))
                    p++;
                stdscan_reset();
                stdscan_bufptr = p;
                tokval.t_type = TOKEN_INVALID;
                e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
                if (e) {
                    if (!is_simple(e))
                        error(ERR_NONFATAL, "cannot use relocatable"
                              " expression as symbol size");
                    else
                        (*s)->size = reloc_value(e);
                }

                /*
                 * Remove it from the list of unresolved sizes.
                 */
                nasm_free((*s)->name);
                *s = (*s)->nextfwd;
                return;
            }
        return;                 /* it wasn't an important one */
    }

    saa_wbytes(strs, name, (long)(1 + strlen(name)));
    strslen += 1 + strlen(name);

    sym = saa_wstruct(syms);

    sym->strpos = pos;
    sym->type = is_global ? SYM_GLOBAL : 0;
    sym->size = 0;
    if (segment == NO_SEG)
        sym->section = SHN_ABS;
    else {
        int i;
        sym->section = SHN_UNDEF;
        if (nsects == 0 && segment == def_seg) {
            int tempint;
            if (segment != elf_section_names(".text", 2, &tempint))
                error(ERR_PANIC,
                      "strange segment conditions in ELF driver");
            sym->section = nsects;
        } else {
            for (i = 0; i < nsects; i++)
                if (segment == sects[i]->index) {
                    sym->section = i + 1;
                    break;
                }
        }
    }

    if (is_global == 2) {
        sym->size = offset;
        sym->value = 0;
        sym->section = SHN_COMMON;
        /*
         * We have a common variable. Check the special text to see
         * if it's a valid number and power of two; if so, store it
         * as the alignment for the common variable.
         */
        if (special) {
            int err;
            sym->value = readnum(special, &err);
            if (err)
                error(ERR_NONFATAL, "alignment constraint `%s' is not a"
                      " valid number", special);
            else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
                error(ERR_NONFATAL, "alignment constraint `%s' is not a"
                      " power of two", special);
        }
        special_used = TRUE;
    } else
        sym->value = (sym->section == SHN_UNDEF ? 0 : offset);

    if (sym->type == SYM_GLOBAL) {
        /*
         * There's a problem here that needs fixing.
         * If sym->section == SHN_ABS, then the first line of the
         * else section causes a core dump, because its a reference
         * beyond the end of the section array.
         * This behaviour is exhibited by this code:
         *     GLOBAL crash_nasm
         *     crash_nasm equ 0
         *
         * I'm not sure how to procede, because I haven't got the
         * first clue about how ELF works, so I don't know what to
         * do with it. Furthermore, I'm not sure what the rest of this
         * section of code does. Help?
         *
         * For now, I'll see if doing absolutely nothing with it will
         * work...
         */
        if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
            bsym = raa_write(bsym, segment, nglobs);
        } else if (sym->section != SHN_ABS) {
            /*
             * This is a global symbol; so we must add it to the linked
             * list of global symbols in its section. We'll push it on
             * the beginning of the list, because it doesn't matter
             * much which end we put it on and it's easier like this.
             *
             * In addition, we check the special text for symbol
             * type and size information.
             */
            sym->next = sects[sym->section - 1]->gsyms;
            sects[sym->section - 1]->gsyms = sym;

            if (special) {
                int n = strcspn(special, " ");

                if (!nasm_strnicmp(special, "function", n))
                    sym->type |= SYM_FUNCTION;
                else if (!nasm_strnicmp(special, "data", n) ||
                         !nasm_strnicmp(special, "object", n))
                    sym->type |= SYM_DATA;
                else
                    error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
                          n, special);
                if (special[n]) {
                    struct tokenval tokval;
                    expr *e;
                    int fwd = FALSE;
                    char *saveme = stdscan_bufptr;      /* bugfix? fbk 8/10/00 */

                    while (special[n] && isspace(special[n]))
                        n++;
                    /*
                     * We have a size expression; attempt to
                     * evaluate it.
                     */
                    stdscan_reset();
                    stdscan_bufptr = special + n;
                    tokval.t_type = TOKEN_INVALID;
                    e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
                                 NULL);
                    if (fwd) {
                        sym->nextfwd = fwds;
                        fwds = sym;
                        sym->name = nasm_strdup(name);
                    } else if (e) {
                        if (!is_simple(e))
                            error(ERR_NONFATAL, "cannot use relocatable"
                                  " expression as symbol size");
                        else
                            sym->size = reloc_value(e);
                    }
                    stdscan_bufptr = saveme;    /* bugfix? fbk 8/10/00 */
                }
                special_used = TRUE;
            }
        }
        sym->globnum = nglobs;
        nglobs++;
    } else
        nlocals++;

    if (special && !special_used)
        error(ERR_NONFATAL, "no special symbol features supported here");
}
Exemple #24
0
int32_t
netifread(Netif *nif, Chan *c, void *a, int32_t n, int64_t off)
{
	Proc *up = externup();
	int i, j;
	Netfile *f;
	char *p;
	int32_t offset;

	if(c->qid.type & QTDIR)
		return devdirread(c, a, n, (Dirtab*)nif, 0, netifgen);

	offset = off;
	switch(NETTYPE(c->qid.path)){
	case Ndataqid:
		f = nif->f[NETID(c->qid.path)];
		return qread(f->iq, a, n);
	case Nctlqid:
		return readnum(offset, a, n, NETID(c->qid.path), NUMSIZE);
	case Nstatqid:
		p = malloc(READSTR);
		if(p == nil)
			error(Enomem);
		j = snprint(p, READSTR, "in: %llu\n", nif->inpackets);
		j += snprint(p+j, READSTR-j, "link: %d\n", nif->link);
		j += snprint(p+j, READSTR-j, "out: %llu\n", nif->outpackets);
		j += snprint(p+j, READSTR-j, "crc errs: %llu\n", nif->crcs);
		j += snprint(p+j, READSTR-j, "overflows: %llu\n", nif->overflows);
		j += snprint(p+j, READSTR-j, "soft overflows: %llu\n", nif->soverflows);
		j += snprint(p+j, READSTR-j, "framing errs: %llu\n", nif->frames);
		j += snprint(p+j, READSTR-j, "buffer errs: %llu\n", nif->buffs);
		j += snprint(p+j, READSTR-j, "output errs: %llu\n", nif->oerrs);
		j += snprint(p+j, READSTR-j, "prom: %d\n", nif->prom);
		j += snprint(p+j, READSTR-j, "mbps: %d\n", nif->mbps);
		j += snprint(p+j, READSTR-j, "addr: ");
		for(i = 0; i < nif->alen; i++)
			j += snprint(p+j, READSTR-j, "%2.2x", nif->addr[i]);
		snprint(p+j, READSTR-j, "\n");
		n = readstr(offset, a, n, p);
		free(p);
		return n;
	case Naddrqid:
		p = malloc(READSTR);
		j = 0;
		for(i = 0; i < nif->alen; i++)
			j += snprint(p+j, READSTR-j, "%2.2x", nif->addr[i]);
		n = readstr(offset, a, n, p);
		free(p);
		return n;
	case Ntypeqid:
		f = nif->f[NETID(c->qid.path)];
		return readnum(offset, a, n, f->type, NUMSIZE);
	case Nifstatqid:
		return 0;
	case Nmtuqid:
		snprint(up->genbuf, sizeof up->genbuf, "%11.u %11.u %11.u\n", nif->minmtu, nif->mtu, nif->maxmtu);
		return readstr(offset, a, n, up->genbuf);
	}
	error(Ebadarg);
	return -1;	/* not reached */
}
Exemple #25
0
static long
consread(Chan *c, void *va, long n, vlong offset)
{
	int send;
	char *p, buf[64], ch;
	FILE * battery;
	FILE * brightness;
	int size;

	if(c->qid.type & QTDIR)
		return devdirread(c, va, n, contab, nelem(contab), devgen);

	switch((ulong)c->qid.path) {
	default:
		error(Egreg);

	case Qsysctl:
		return readstr(offset, va, n, VERSION);

	case Qsysname:
		if(ossysname == nil)
			return 0;
		return readstr(offset, va, n, ossysname);

	case Qrandom:
		return randomread(va, n);

	case Qnotquiterandom:
		genrandom(va, n);
		return n;

	case Qhostowner:
		return readstr(offset, va, n, eve);

	case Qhoststdin:
		return read(0, va, n);	/* should be pread */

	case Quser:
		return readstr(offset, va, n, up->env->user);

	case Qjit:
		snprint(buf, sizeof(buf), "%d", cflag);
		return readstr(offset, va, n, buf);

	case Qtime:
		snprint(buf, sizeof(buf), "%.lld", timeoffset + osusectime());
		return readstr(offset, va, n, buf);

	case Qdrivers:
		return devtabread(c, va, n, offset);

	case Qmemory:
		return poolread(va, n, offset);

	case Qnull:
		return 0;

	case Qmsec:
		return readnum(offset, va, n, osmillisec(), NUMSIZE);

	case Qcons:
		qlock(&kbd.q);
		if(waserror()){
			qunlock(&kbd.q);
			nexterror();
		}

		if(dflag)
			error(Enonexist);

		while(!qcanread(lineq)) {
			if(qread(kbdq, &ch, 1) == 0)
				continue;
			send = 0;
			if(ch == 0){
				/* flush output on rawoff -> rawon */
				if(kbd.x > 0)
					send = !qcanread(kbdq);
			}else if(kbd.raw){
				kbd.line[kbd.x++] = ch;
				send = !qcanread(kbdq);
			}else{
				switch(ch){
				case '\b':
					if(kbd.x)
						kbd.x--;
					break;
				case 0x15:
					kbd.x = 0;
					break;
				case 0x04:
					send = 1;
					break;
				case '\n':
					send = 1;
				default:
					kbd.line[kbd.x++] = ch;
					break;
				}
			}
			if(send || kbd.x == sizeof kbd.line){
				qwrite(lineq, kbd.line, kbd.x);
				kbd.x = 0;
			}
		}
		n = qread(lineq, va, n);
		qunlock(&kbd.q);
		poperror();
		return n;

	case Qscancode:
		if(offset == 0)
			return readstr(0, va, n, gkscanid);
		return qread(gkscanq, va, n);

	case Qkeyboard:
		return qread(gkbdq, va, n);

	case Qevents:
		return qread(c->aux, va, n);

	case Qkprint:
		rlock(&kprintq.l);
		if(waserror()){
			runlock(&kprintq.l);
			nexterror();
		}
		n = qread(kprintq.q, va, n);
		poperror();
		runlock(&kprintq.l);
		return n;
	case Qbattery:
		if(type == 's')
			battery = fopen("/sys/class/power_supply/battery/capacity", "r");
		else if(type == 'c')
			battery = fopen("/sys/class/power_supply/max17042-0/capacity", "r");
		else
			battery = fopen("/sys/class/power_supply/battery/capacity", "r");
		size = fread(buf, 1, sizeof(buf), battery);
		fclose(battery);
		buf[size - 1] = '\0';
		return readstr(offset, va, n, buf);
	case Qtype:
		if(type == 's')
			strncpy(buf, "nexus s", sizeof(buf));
		else if(type == 'c')
			strncpy(buf, "nook color", sizeof(buf));
		else if(type == 'e')
			strncpy(buf, "emulator", sizeof(buf));
		else
			strncpy(buf, "nexus s", sizeof(buf));
		return readstr(offset, va, n, buf);
	case Qbrightness:
		if(type == 'c')
			brightness = fopen("/sys/devices/platform/omap_pwm_led/leds/lcd-backlight/brightness", "r");
		else if(type == 'e')
			return;
		else
			brightness = fopen("/sys/class/backlight/s5p_bl/brightness", "r");
		size = fread(buf, 1, sizeof(buf), brightness);
		fclose(brightness);
		buf[size - 1] = '\0';
		return readstr(offset, va, n, buf);
	}
}