void cqsort (canon_info_t *cn, canon_line_t v[], int left, int right) { int i, last; canon_line_t line1, line2; if (left >= right) return; swap (cn, v, left, (left + right)/2); last = left; for (i = left + 1; i <= right; i++) { if (cn->lio == CANON_MEM) { if (v[i].wt < v[left].wt) swap (cn, v, ++last, i); } else { lget (i, &line1, cn); lget (left, &line2, cn); if (line1.wt < line2.wt) swap (cn, v, ++last, i); } } swap (cn, v, left, last); cqsort (cn, v, left, last - 1); cqsort (cn, v, last + 1, right); }
int main() { printf("Hello"); unsigned char b; for(lset(b,1);lget(b)<=(grid*grid);lset(b,(lget(b)+1))) for(rset(b,1);rget(b)<=(grid*grid);rset(b,(rget(b)+1))) if(lget(b)%grid != rget(b)%grid) printf("A=%d, B=%d\n", lget(b),rget(b)); return 0; }
void swap (canon_info_t *cn, canon_line_t v[], int i, int j) { canon_line_t temp, line2; if (cn->lio == CANON_MEM) { temp = v[i]; v[i] = v[j]; v[j] = temp; } else { lget (i, &temp, cn); lget (j, &line2, cn); lput (j, &temp, cn); lput (i, &line2, cn); } }
void clock_reset() { Uint32 i, t = SDL_GetTicks(); int count; count = lcount(_clocks); for(i=0;i<count;i++) ((sclock*)lget(_clocks, i))->ticks = t; }
void set_skip_attr (canon_info_t *ci, parse_info_t *pi) { canon_line_t lrec; if (ci->lio == CANON_DISK) { lget (pi->num_lines, &lrec, ci); lrec.skip_attr = 1; lput (pi->num_lines, &lrec, ci); } else lineptr[pi->num_lines].skip_attr = 1; }
void clock_pause(int enable) { Uint32 i, t; int count; if(enable) { _pause = SDL_GetTicks(); } else { t = SDL_GetTicks() - _pause; count = lcount(_clocks); for(i=0;i<count;i++) ((sclock*)lget(_clocks, i))->ticks += t; } }
/* * Print the attribute identifier at the beginning of the line */ void canonicalize_key_attr (parse_info_t *obj, canon_info_t *canon_info, int lncont) { char buf[64]; int num_spaces; canon_line_t lrec; /* fprintf (stderr,"canonicalize_key_attr: attr=(%s)\n", attr_name[obj->curr_attr]); */ if (!canon_info->do_canon) return; num_spaces = ATTR_ID_LENGTH - strlen (attr_name[obj->curr_attr]) - 1; if (num_spaces > 0) sprintf(buf, "%%s:%%b%d", num_spaces); else sprintf(buf, "%%s:"); parse_buf_add (canon_info, buf, attr_name[obj->curr_attr]); /* if we are doing line continuation then exit * as we have already done the next part */ if (lncont) return; /* init the curr attribute type and count for this field */ if (canon_info->lio == CANON_MEM) { lineptr[obj->num_lines].attr = obj->curr_attr; lineptr[obj->num_lines].count = obj->attrs[obj->curr_attr] + 1; } else { lget (obj->num_lines, &lrec, canon_info); lrec.attr = obj->curr_attr; lrec.count = obj->attrs[obj->curr_attr] + 1; lput (obj->num_lines, &lrec, canon_info); } }
static void diag(const char *exe) { static const uint8_t elf[] = {0x7f, 'E', 'L', 'F'}; static const uint8_t shebang[] = {'#','!'}; static int diag_depth; struct stat st; const uint8_t *mm; const char *itrp = NULL; map_file(exe, PROT_READ, MAP_SHARED, &st, (void **)&mm); exit_if(!((S_IXUSR|S_IXGRP|S_IXOTH) & st.st_mode), "\"%s\" is not executable", exe) if(st.st_size >= sizeof(shebang) && !memcmp(mm, shebang, sizeof(shebang))) { const uint8_t *nl; int maxlen = MIN(PATH_MAX, st.st_size - sizeof(shebang)); /* TODO(vc): EOF-terminated shebang lines are technically possible */ exit_if(!(nl = memchr(&mm[sizeof(shebang)], '\n', maxlen)), "Shebang line too long"); pexit_if(!(itrp = strndup((char *)&mm[sizeof(shebang)], (nl - mm) - 2)), "Failed to dup interpreter path"); } else if(st.st_size >= sizeof(elf) && !memcmp(mm, elf, sizeof(elf))) { uint64_t (*lget)(const uint8_t *) = NULL; uint32_t (*iget)(const uint8_t *) = NULL; uint16_t (*sget)(const uint8_t *) = NULL; const void *phoff = NULL, *phesz = NULL, *phecnt = NULL; const uint8_t *ph = NULL; int i, phreloff, phrelsz; exit_if(mm[ELF_VERSION] != 1, "Unsupported ELF version: %hhx", mm[ELF_VERSION]); /* determine which accessors to use and where */ if(mm[ELF_BITS] == ELF_BITS_32) { if(mm[ELF_ENDIAN] == ELF_ENDIAN_LITL) { lget = le32_lget; sget = le_sget; iget = le_iget; } else if(mm[ELF_ENDIAN] == ELF_ENDIAN_BIG) { lget = be32_lget; sget = be_sget; iget = be_iget; } phoff = &mm[ELF32_PHT_OFF]; phesz = &mm[ELF32_PHTE_SIZE]; phecnt = &mm[ELF32_PHTE_CNT]; phreloff = ELF32_PHE_OFF; phrelsz = ELF32_PHE_SIZE; } else if(mm[ELF_BITS] == ELF_BITS_64) { if(mm[ELF_ENDIAN] == ELF_ENDIAN_LITL) { lget = le64_lget; sget = le_sget; iget = le_iget; } else if(mm[ELF_ENDIAN] == ELF_ENDIAN_BIG) { lget = be64_lget; sget = be_sget; iget = be_iget; } phoff = &mm[ELF64_PHT_OFF]; phesz = &mm[ELF64_PHTE_SIZE]; phecnt = &mm[ELF64_PHTE_CNT]; phreloff = ELF64_PHE_OFF; phrelsz = ELF64_PHE_SIZE; } exit_if(!lget, "Unsupported ELF format"); if(!phoff) /* program header may be absent, don't make it an error */ return; /* TODO(vc): sanity checks on values before using them */ for(ph = &mm[lget(phoff)], i = 0; i < sget(phecnt); i++, ph += sget(phesz)) { if(iget(ph) == ELF_PT_INTERP) { itrp = strndup((char *)&mm[lget(&ph[phreloff])], lget(&ph[phrelsz])); break; } } } else { exit_if(1, "Unsupported file type"); } exit_if(!itrp, "Unable to determine interpreter for \"%s\"", exe); exit_if(*itrp != '/', "Path must be absolute: \"%s\"", itrp); exit_if(++diag_depth > MAX_DIAG_DEPTH, "Excessive interpreter recursion, giving up"); diag(itrp); }
void print_pc() { char str[1024]; uint8_t binary[32]; uint32_t i; uint32_t len; const char *name = NULL; if ((shoe.pc >= 0x40000000) && (shoe.pc < 0x50000000)) { uint32_t i, addr = shoe.pc % (shoe.physical_rom_size); for (i=0; macii_rom_symbols[i].name; i++) { if (macii_rom_symbols[i].addr > addr) { break; } name = macii_rom_symbols[i].name; } } else if (sr_s()) { // these symbols are only meaningful in supervisor mode coff_symbol *symb = coff_find_func(shoe.coff, shoe.pc); if (symb && strlen(symb->name)) name = symb->name; } else { if ((shoe.pc >= 0x10000000) && (shoe.pc < 0x20000000)) { uint32_t i, addr = shoe.pc % (shoe.physical_rom_size); for (i=0; macii_rom_symbols[i].name; i++) { if (macii_rom_symbols[i].addr > addr) { break; } name = macii_rom_symbols[i].name; } } /*else { coff_symbol *symb = coff_find_func(shoe.launch, shoe.pc); if (symb) name = symb->name; }*/ } const uint16_t old_abort = shoe.abort; shoe.suppress_exceptions = 1; for (i=0; i<32; i++) { binary[i] = (uint8_t) lget(shoe.pc+i, 1); } disassemble_inst(binary, shoe.pc, str, &len); printf("*0x%08x %s [ ", shoe.pc, name ? name : ""); for (i=0; i<len; i+=2) { printf("%02x%02x ", binary[i], binary[i+1]); } printf("] %s\n", str); shoe.abort = old_abort; shoe.suppress_exceptions = 0; if (name && strcmp(name, "ui_ioctl")==0) printregs(); }
/* Our memory parse buffer is not large enough to hold the * canonicalized object, flush it to disk. Function opens up * a temp parse file 'flushfn' to hold the object. Each of * the lines in the memory buffer are copied to the disk buffer. * * Return: * pointer to the disk streams pointer. */ FILE *flush_parse_buffer (canon_info_t *cn) { char *p; int i, fd; FILE *fp; canon_line_t lrec; if (cn->fd != NULL) { fprintf (dfile, "Overflow buffer called a second time. Abort!\n"); exit (0); } strcpy (cn->flushfn, cn->flushfntmpl); fd = mkstemp (cn->flushfn); if ((fp = fdopen (fd, "w+")) == NULL) { fprintf (stderr, "Cannot open disk parse buffer (%s). Abort!\n", cn->flushfn); exit (0); } if (verbose) fprintf (dfile, "*****flush buffer to disk****\n:%s:\n",cn->buffer); /* Make sure last char is a NULL byte, we * could be in the middle of a string */ *cn->bufp = '\0'; if (cn->lio == CANON_DISK) fseek (cn->lfd, 0, SEEK_SET); for (i = 0, p = cn->buffer; p != NULL && p < cn->bufp; i++) { cn->flinep = ftell (fp); if (cn->lio == CANON_MEM) lineptr[i].fpos = cn->flinep; else { lget (i, &lrec, cn); lrec.fpos = cn->flinep; if (verbose) fprintf (dfile, "flush buf line pos (%ld)\n", lrec.fpos); lput (i, &lrec, cn); } fputs (p, fp); if ((p = strchr (p, '\0')) != NULL) p++; } if (verbose) fprintf (dfile, "flush_parse_buffer () number of lines flushed to disk (%d)\n", i); /* set the begining of line pointer (ie, cn->flinep) */ if (cn->linep == cn->bufp) { /* we are at the beginning of a line */ cn->flinep = ftell (fp); if (verbose) fprintf (dfile, "flush buf: we are at the beginning of a line...\n"); if (cn->lio == CANON_MEM) lineptr[i].fpos = cn->flinep; else { lget (i, &lrec, cn); lrec.fpos = cn->flinep; if (verbose) fprintf (dfile, "flush buf line pos (%ld)\n", lrec.fpos); lput (i, &lrec, cn); } } cn->io = CANON_DISK; cn->buf_space_left = 0; return fp; }
void rpsl_lncont (parse_info_t *obj, canon_info_t *canon_info, int add_line_feed) { int i, j = 0, state = 0; char c, d; canon_line_t lrec; /* if (verbose) */ fprintf (dfile, "1 error-pos (%d) line-len (%d) num_lines (%d) \n", obj->error_pos, obj->eline_len, obj->num_lines); if (!canon_info->do_canon) return; /* Reset the parse buffer pointer to the beginning of * the line to overwrite any fragment of the canonicalized * line we have already parsed. */ dump_current_canon_line (canon_info); /* reset the number of lines */ if (canon_info->lio == CANON_MEM) lineptr[obj->num_lines].lines = obj->elines; else { lget (obj->num_lines, &lrec, canon_info); lrec.lines = obj->elines; lput (obj->num_lines, &lrec, canon_info); } if (fseek (canon_info->efd, 0, SEEK_SET) != EOF) { /* put in the attribute ID */ canonicalize_key_attr (obj, canon_info, 1); for (i = 0; i < obj->eline_len && (c = fgetc (canon_info->efd)) != EOF; i++) { if (state == 0) { /* initial state, skip past attr */ if (c == ':') state = 1; } else if (state == 1) { /* at the ATTR_ID_LEN, * get rid of spaces */ if (c == ' ' || c == '\t') continue; parse_buf_add (canon_info, "%c", &c); state = 2; } else if (state == 2) { /* print non-blank characters */ if (c == ' ' || c == '\t') { /* go to string trim state */ j = 1; state = 3; continue; } if (c == '\n') state = 4; parse_buf_add (canon_info, "%c", &c); } else if (state == 3) { /* string trim, remove trailing blanks */ if (c == ' ' || c == '\t') { j++; continue; } if (c == '\n') state = 4; else { /* print non-blank char and spaces */ d = ' '; for (; j > 0; j--) parse_buf_add (canon_info, "%c", &d); state = 2; } parse_buf_add (canon_info, "%c", &c); } else if (state == 4) { /* print line continuation char * and pad with space */ parse_buf_add (canon_info, "%c", &c); c = ' '; for (j = 1;(ATTR_ID_LENGTH - j) > 0; j++) parse_buf_add (canon_info, "%c", &c); state = 1; } } } else { parse_buf_add (canon_info, "%s", "INTERNAL ERROR: could not read error line!\n"); add_line_feed = 0; } if (add_line_feed) parse_buf_add (canon_info, "\n"); /* terminate the line */ parse_buf_add (canon_info, "%z"); return; }
/* Write out the error causing line verbatim (ie, as the user typed it in) and put * the error symbol '<?>' next to the error causing token. The error line is in * the file pointed to by 'canon_info->efd'. Because of line continuation '\' and * as-in/as-out, the line is saved to file as it can be arbitrarily large. The * basic steps here are to reset the canonicalize buffer back to the beginning or * the line and write over any partially canonicalized line with the error line. * Next the position within the line to put the error symbol is calculated and then * the error line is written to the canonicalize buffer. * * Return: * void */ void add_canonical_error_line (parse_info_t *obj, canon_info_t *canon_info, int add_line_feed) { int i, j, k, l; char c; canon_line_t lrec; /* if (verbose) */ fprintf (dfile, "1 error-pos (%d) line-len (%d) num_lines (%d) \n", obj->error_pos, obj->eline_len, obj->num_lines); if (!canon_info->do_canon) return; /* Reset the parse buffer pointer to the beginning of * the line to overwrite any fragment of the canonicalized * line we have already parsed. Want to write the error * line verbatim with the error symbol at the error token * position. */ dump_current_canon_line (canon_info); /* reset the number of lines */ if (canon_info->lio == CANON_MEM) lineptr[obj->num_lines].lines = obj->elines; else { lget (obj->num_lines, &lrec, canon_info); lrec.lines = obj->elines; lput (obj->num_lines, &lrec, canon_info); } /* Make the error symbol '<?>' abut the nearest token. * Find the position j to put '<?>'. Remember, because * of '\' line continuation, we can have many '\n's in * a single error line. */ if (fseek (canon_info->efd, 0, SEEK_SET) != EOF) { j = l = -1; i = obj->error_pos; k = 0; for (;j < 0 || l < 0 || (l <= j); k++) { if (k >= obj->eline_len || (c = fgetc (canon_info->efd)) == EOF || c == '\0') break; else if (c != ' ' && c != '\t' && c != '\n') j = k; else if ((j >= 0 && (j == (k - 1)) && k == i) || k > i) l = k; } if (j >= 0) j++; else j = 0; /* JW debug */ fprintf (dfile, "\nerror_line (): error_pos (%d) j (%d) l (%d) k (%d)\n", obj->error_pos, j, l, k); fseek (canon_info->efd, 0, SEEK_SET); for (i = 0; i < obj->eline_len && (c = fgetc (canon_info->efd)) != EOF; i++) { if (i % 10 == 0) fprintf (dfile, "|"); fprintf (dfile, "%c", c); } fprintf (dfile, "\n i (%d)\n", i); /* JW end debug */ /* write out the error line, inserting '<?>' at position j */ fseek (canon_info->efd, 0, SEEK_SET); for (i = 0; i < obj->eline_len && (c = fgetc (canon_info->efd)) != EOF; i++) { if (i == j) parse_buf_add (canon_info, "%s", ERROR_TOKEN); parse_buf_add (canon_info, "%c", &c); } } else { parse_buf_add (canon_info, "%s", "INTERNAL ERROR: could not read error line!\n"); add_line_feed = 0; } if (add_line_feed) parse_buf_add (canon_info, "\n"); /* terminate the line */ parse_buf_add (canon_info, "%z"); return; }