/* * prtail(): pad page with empty lines (if required) and print page trailer * if requested * * cnt number of lines of padding needed * incomp was a '\n' missing from last line output */ int prtail(int cnt, int incomp) { if (nohead) { /* * only pad with no headers when incomplete last line */ if (incomp && ((dspace && (putchar('\n') == EOF)) || (putchar('\n') == EOF))) { pfail(); return(1); } /* * but honor the formfeed request */ if (formfeed) { if (putchar('\f') == EOF) { pfail(); return(1); } } return(0); } /* * if double space output two \n */ if (dspace) cnt *= 2; /* * if an odd number of lines per page, add an extra \n */ if (addone) ++cnt; /* * pad page */ if (formfeed) { if ((incomp && (putchar('\n') == EOF)) || (putchar('\f') == EOF)) { pfail(); return(1); } return(0); } cnt += TAILLEN; while (--cnt >= 0) { if (putchar('\n') == EOF) { pfail(); return(1); } } return(0); }
FILE* safe_open(const char* path, const char* default_path, const char* md){ FILE* f = NULL; if(path) f = fopen(path, md); if (!f) { if (default_path) f = fopen(default_path, md); pfail(false, "Не удалось открыть файл по заданному пути (%s)", path); if (!f) pfail(true, "Не удалось открыть файл с помощью запасного пути (%s). Сворачиваемся...", default_path); } return f; };
sqInt sqFileSetPosition(SQFile *f, squeakFileOffsetType position) { /* Set the file's read/write head to the given position. */ if (!sqFileValid(f)) return interpreterProxy->success(false); if (f->isStdioStream) { pentry(sqFileSetPosition); /* support one character of pushback for stdio streams. */ if (!f->writable && f->lastChar != EOF) { squeakFileOffsetType currentPos = f->lastChar == EOF ? 0 : 1; if (currentPos == position) return pexit(1); if (currentPos - 1 == position) { ungetc(f->lastChar, getFile(f)); f->lastChar = EOF; return pexit(1); } } pfail(); return interpreterProxy->success(false); } fseek(getFile(f), position, SEEK_SET); f->lastOp = UNCOMMITTED; return 1; }
/* * prhead(): prints the top of page header * * buf buffer with time field (and offset) * cnt number of chars in buf * fname fname field for header * pagcnt page number */ int prhead(char *buf, const char *fname, int pagcnt) { int ips = 0; int ops = 0; if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) { pfail(); return(1); } /* * posix is not clear if the header is subject to line length * restrictions. The specification for header line format * in the spec clearly does not limit length. No pr currently * restricts header length. However if we need to truncate in * a reasonable way, adjust the length of the printf by * changing HDFMT to allow a length max as an argument to printf. * buf (which contains the offset spaces and time field could * also be trimmed * * note only the offset (if any) is processed for tab expansion */ if (offst && otln(buf, offst, &ips, &ops, -1)) return(1); (void)printf(HDFMT,buf+offst, fname, pagcnt); return(0); }
unsigned long safe_input_int(const char* str){ size_t i, refs, s = strlen(str), ndg = 0, start_from = -1; unsigned long d = 0; const char *reference = "4294967295"; refs = strlen(reference); for (i = 0; i < s; ++i) if (str[i] == '-') { if (!ndg) fail("Извините, но отрицательные числа не поддрерживаются."); else fail("Ошибка чтения - знак в неожиданном месте."); } else if (str[i] < 0 || !isdigit(str[i])) fail("Ошибка чтения - неизвестный или неподдерживаемый символ"); else { if (start_from == -1) start_from = i; ++ndg; } if (ndg>refs || (ndg == refs&&strcmp(str + start_from, reference) > 0)) pfail(true, "Переполнение буфера памяти для числа: %s", str); d = atol(str + start_from); return d; }
void pass3 () { struct dirinfo *dnp; int nd; int change; /* Mark all the directories that can be found from the root. */ inodestate[ROOTINO] |= DIR_REF; do { change = 0; for (nd = 0; nd < dirarrayused; nd++) { dnp = dirsorted[nd]; if (dnp->i_parent && inodestate[dnp->i_parent] == (DIRECTORY | DIR_REF) && inodestate[dnp->i_number] == DIRECTORY) { inodestate[dnp->i_number] |= DIR_REF; change = 1; } } } while (change); /* Check for orphaned directories */ for (nd = 0; nd < dirarrayused; nd++) { dnp = dirsorted[nd]; if (dnp->i_parent == 0) { if (inodestate[dnp->i_number] & DIR_REF) errexit ("ORPHANED DIR MARKED WITH CONNECT"); pinode (0, dnp->i_number, "UNREF"); if ((preen || reply ("RECONNECT")) && linkup (dnp->i_number, dnp->i_dotdot)) { dnp->i_parent = dnp->i_dotdot = lfdir; pfix ("RECONNECTED"); } else pfail (0); } } }
/* * otln(): output a line of data. (Supports unlimited length lines) * output is optionally contracted to tabs * * buf: output buffer with data * cnt: number of chars of valid data in buf * svips: buffer input column position (for large lines) * svops: buffer output column position (for large lines) * mor: output line not complete in this buf; more data to come. * 1 is more, 0 is complete, -1 is no \n's */ int otln(char *buf, int cnt, int *svips, int *svops, int mor) { int ops; /* last col output */ int ips; /* last col in buf examined */ int gap = ogap; int tbps; char *endbuf; if (ogap) { /* * contracting on output */ endbuf = buf + cnt; ops = *svops; ips = *svips; while (buf < endbuf) { /* * count number of spaces and ochar in buffer */ if (*buf == ' ') { ++ips; ++buf; continue; } /* * simulate ochar processing */ if (*buf == ochar) { ips += gap - (ips % gap); ++buf; continue; } /* * got a non space char; contract out spaces */ while (ips - ops > 1) { /* * use as many ochar as will fit */ if ((tbps = ops + gap - (ops % gap)) > ips) break; if (putchar(ochar) == EOF) { pfail(); return(1); } ops = tbps; } while (ops < ips) { /* * finish off with spaces */ if (putchar(' ') == EOF) { pfail(); return(1); } ++ops; } /* * output non space char */ if (putchar(*buf++) == EOF) { pfail(); return(1); } ++ips; ++ops; } if (mor > 0) { /* * if incomplete line, save position counts */ *svops = ops; *svips = ips; return(0); } if (mor < 0) { while (ips - ops > 1) { /* * use as many ochar as will fit */ if ((tbps = ops + gap - (ops % gap)) > ips) break; if (putchar(ochar) == EOF) { pfail(); return(1); } ops = tbps; } while (ops < ips) { /* * finish off with spaces */ if (putchar(' ') == EOF) { pfail(); return(1); } ++ops; } return(0); } } else { /* * output is not contracted */ if (cnt && (fwrite(buf, sizeof(char), cnt, stdout) <= 0)) { pfail(); return(1); } if (mor != 0) return(0); } /* * process line end and double space as required */ if ((putchar('\n') == EOF) || (dspace && (putchar('\n') == EOF))) { pfail(); return(1); } return(0); }
/* * prtail(): pad page with empty lines (if required) and print page trailer * if requested * * cnt number of lines of padding needed * incomp was a '\n' missing from last line output * * prtail() can now be invoked unconditionally, with the notion that if * we haven't printed a header, there is no need for a trailer */ int prtail(int cnt, int incomp) { /* * if were's skipping to page N or haven't put out anything yet just exit */ if (skipping || beheaded == 0) return (0); beheaded = 0; /* * if noheaders, only terminate an incomplete last line */ if (nohead) { if (incomp) { if (dspace) if (putchar('\n') == EOF) { pfail(); return(1); } if (putchar('\n') == EOF) { pfail(); return(1); } } /* * but honor the formfeed request */ if (formfeed) if (putchar(OUTFF) == EOF) { pfail(); return(1); } } else { /* * if double space output two \n * * XXX this all seems bogus, why are we doing it here??? * page length is in terms of output lines and only the input is * supposed to be double spaced... otln() users should be doing * something like linect+=(dspace ? 2:1). */ if (dspace) cnt *= 2; /* * if an odd number of lines per page, add an extra \n */ if (addone) ++cnt; /* * either put out a form-feed or pad page with blanks */ if (formfeed) { if (incomp) if (putchar('\n') == EOF) { pfail(); return(1); } if (putchar(OUTFF) == EOF) { pfail(); return(1); } } else { if (incomp) cnt++; cnt += TAILLEN; while (--cnt >= 0) { if (putchar('\n') == EOF) { pfail(); return(1); } } } } return(0); }
void test_spi_flash(void) { struct spi_slave spi; struct spi_flash flash; unsigned int addr, i; int ret; /* We use a 512 byte buf for verifications below because of limited RAM */ uint8_t buf[512]; ptest(); /* Enable PIO0.2 for CS */ LPC_GPIO0->DIR |= (1<<2); /* Configure SPI */ spi.master = &SPI0; spi.speed = 24000000; spi.mode = 0; spi.cs_pin = 2; spi.cs_active_high = false; debug_printf(STR_TAB "Press enter to start SPI flash test...\n"); uart_getc(); uart_putc('\n'); /* Probe for the flash */ debug_printf(STR_TAB "Probing for SPI flash chip...\n"); passert(spi_flash_probe(&flash, &spi) == 0); pokay("Found SPI flash!"); pokay(" JEDEC ID: 0x%08x", flash.params->jedec_id); pokay(" Name: %s", flash.params->name); pokay(" Sector Size: %d", flash.params->sector_size); pokay(" Capacity: %d", flash.params->capacity); pokay(" Flags: %04x", flash.params->flags); debug_printf(STR_TAB "Testing invalid argument checks of SPI flash functions\n"); /* Make sure not sector size mod address and length fail */ passert(spi_flash_erase(&flash, 0x5, 4096) == SPI_FLASH_ERROR_ARGS); passert(spi_flash_erase(&flash, 0, 4095) == SPI_FLASH_ERROR_ARGS); /* Make sure program out of bounds fails */ passert(spi_flash_write(&flash, 0x3, NULL, flash.params->capacity) == SPI_FLASH_ERROR_ARGS); /* Erase two sectors */ debug_printf(STR_TAB "Erasing lower two sectors...\n"); passert(spi_flash_erase(&flash, 0x0, 4096*2) == 0); /* Verify they are all 0xff */ debug_printf(STR_TAB "Verifying lower two sectors are blank...\n"); for (addr = 0; addr < 4096*2; addr += sizeof(buf)) { if ((ret = spi_flash_read(&flash, addr, buf, sizeof(buf))) != 0) { pfail("Error with spi_flash_read(): %d", ret); passert(false); } for (i = 0; i < sizeof(buf); i++) { if (buf[i] != 0xff) { pfail("Memory is not blank at address 0x%06x! got 0x%02x", addr+i, buf[i]); passert(false); } } } pokay("Lower two sectors are blank"); debug_printf(STR_TAB "Starting write/read/verify test vector tests...\n"); /* Write test vector 1 to 0x003 - 0x04e inclusive (75 bytes) */ passert(spi_flash_write(&flash, 0x03, test_sf_vector1, sizeof(test_sf_vector1)) == 0); pokay("Wrote test vector 1 to 0x003-0x04e"); /* Verify test vector 1 data from 0x000 - 0x0ff */ passert(spi_flash_read(&flash, 0x0, buf, sizeof(buf)) == 0); passert(memcmp(buf, test_sf_vector_blank, 3) == 0); passert(memcmp(buf+3, test_sf_vector1, sizeof(test_sf_vector1)) == 0); passert(memcmp(buf+3+sizeof(test_sf_vector1), test_sf_vector_blank, 0x100-3-sizeof(test_sf_vector1)) == 0); pokay("Read test vector 1 verified 0x000-0x0ff"); /* Write test vector 2 to 0x100 - 0x1ff inclusive (1 page == 256 bytes) */ passert(spi_flash_write(&flash, 0x100, test_sf_vector2, sizeof(test_sf_vector2)) == 0); pokay("Wrote test vector 2 to 0x100-0x1ff"); /* Verify test vector 1 again */ debug_printf(STR_TAB "Verifying test vector 1 again\n"); passert(spi_flash_read(&flash, 0x0, buf, sizeof(buf)) == 0); passert(memcmp(buf, test_sf_vector_blank, 3) == 0); passert(memcmp(buf+3, test_sf_vector1, sizeof(test_sf_vector1)) == 0); passert(memcmp(buf+3+sizeof(test_sf_vector1), test_sf_vector_blank, 0x100-3-sizeof(test_sf_vector1)) == 0); pokay("Read test vector 1 verified 0x000-0x0ff"); /* Verify test vector 2 */ passert(spi_flash_read(&flash, 0x100, buf, sizeof(buf)) == 0); passert(memcmp(buf, test_sf_vector2, sizeof(test_sf_vector2)) == 0); pokay("Read test vector 2 verified 0x100-0x1ff"); /* Write test vector 3 to 0x200 - 0x301 inclusive (1 page, 1 byte == 257 bytes) */ passert(spi_flash_write(&flash, 0x200, test_sf_vector3, sizeof(test_sf_vector3)) == 0); pokay("Wrote test vector 3 to 0x200-0x301"); /* Verify test vector 3 */ passert(spi_flash_read(&flash, 0x200, buf, sizeof(buf)) == 0); passert(memcmp(buf, test_sf_vector3, sizeof(test_sf_vector3)) == 0); passert(memcmp(buf+sizeof(test_sf_vector3), test_sf_vector_blank, 0x100-1) == 0); pokay("Read test vector 3 verified 0x200-0x3ff"); /* Write test vector 4 to 0x37f - 0x5ff inclusive (2.5 pages == 640 bytes) */ passert(spi_flash_write(&flash, 0x37f, test_sf_vector4, sizeof(test_sf_vector4)) == 0); pokay("Wrote test vector 4 to 0x37f-0x5ff"); /* Verify test vector 4 */ /* First 512 bytes */ passert(spi_flash_read(&flash, 0x37f, buf, sizeof(buf)) == 0); passert(memcmp(buf, test_sf_vector4, 512) == 0); /* Last 128 bytes (and 384 blank bytes) */ passert(spi_flash_read(&flash, 0x37f+512, buf, sizeof(buf)) == 0); passert(memcmp(buf, test_sf_vector4+512, 128) == 0); passert(memcmp(buf+128, test_sf_vector_blank, 384) == 0); pokay("Read test vector 4 verified 0x37f-0x5ff"); debug_printf(STR_TAB "Erasing entire chip... Press enter to continue."); uart_getc(); uart_putc('\n'); /* Erase chip */ passert(spi_flash_erase(&flash, 0x0, flash.params->capacity) == 0); /* Verify lower 256kB is blank */ debug_printf(STR_TAB "Verifying lower 256kB is blank...\n"); for (addr = 0; addr < 256*1024; addr += sizeof(buf)) { if ((ret = spi_flash_read(&flash, addr, buf, sizeof(buf))) != 0) { pfail("Error with spi_flash_read(): %d", ret); passert(false); } for (i = 0; i < sizeof(buf); i++) { if (buf[i] != 0xff) { pfail("Memory is not blank at address 0x%06x! got 0x%02x", addr+i, buf[i]); passert(false); } } } pokay("Lower 256kB is blank"); debug_printf(STR_TAB "Starting pseudorandom data test... Press enter to continue."); uart_getc(); uart_putc('\n'); /* Write 256kB pseudorandom data */ debug_printf(STR_TAB "Writing 256kB pseudorandom data\n"); srand(0xdeadbeef); for (addr = 0; addr < 256*1024; addr += sizeof(buf)) { for (i = 0; i < sizeof(buf); i++) buf[i] = rand(); if (spi_flash_write(&flash, addr, buf, sizeof(buf)) != 0) { pfail("Error with spi_flash_write(): %d", ret); passert(false); } } pokay("Wrote 256kB pseudorandom data"); /* Verify 256kB pseudorandom data */ debug_printf(STR_TAB "Verifying 256kB pseudorandom data\n"); srand(0xdeadbeef); for (addr = 0; addr < 256*1024; addr += sizeof(buf)) { if ((ret = spi_flash_read(&flash, addr, buf, sizeof(buf))) != 0) { pfail("Error with spi_flash_read(): %d", ret); passert(false); } for (i = 0; i < sizeof(buf); i++) { uint8_t x = rand(); if (buf[i] != x) { pfail("Pseudorandom data mismatch at address 0x%06x! expected 0x%02x, got 0x%02x", addr+i, x, buf[i]); passert(false); } } } pokay("Pseudorandom data matches!"); }
void pass4() { ino_t number; /* True if any reconnect attempt failed, in which case we don't try again. */ int reconn_failed = 0; for (number = ROOTINO; number < maxino; number++) { if (linkfound[number] && inodestate[number] != UNALLOC) { if (linkcount[number] != linkfound[number]) { pinode (0, number, "LINK COUNT %d SHOULD BE %d IN", linkcount[number], linkfound[number]); if (preen || reply ("ADJUST")) { struct dinode dino; getinode (number, &dino); dino.di_nlink = linkfound[number]; write_inode (number, &dino); pfix ("ADJUSTED"); } } } else if (linkfound[number] && inodestate[number] == UNALLOC) { /* This can't happen because we never count links to unallocated nodes. */ errexit ("LINK RECORDED FOR UNALLOCATED NODE"); } else if (!linkfound[number] && inodestate[number] != UNALLOC) { /* No links to allocated node. If the size is zero, then we want to clear it; if the size is positive, then we want to reattach in. */ struct dinode dino; pinode (0, number, "UNREF"); getinode (number, &dino); if (dino.di_size && !reconn_failed) { /* This can't happen for dirctories because pass 3 should already have reset them up. */ if ((DI_MODE (&dino) & IFMT) == IFDIR) errexit ("NO LINKS TO NONZERO DIRECTORY"); if (preen || reply ("RECONNECT")) reconn_failed = !linkup (number, -1); if (! reconn_failed) pfix ("RECONNECTED"); if (preen && reconn_failed) pfail ("RECONNECT FAILED"); } if (dino.di_size == 0 || reconn_failed) { if (reconn_failed && !preen) /* If preening, the previous call to problem is still active (more likely the failure was too severe, and exited). */ problem (0, "RECONNECT FAILED"); if (preen || reply ("CLEAR")) { inodestate[number] = UNALLOC; clear_inode (number, &dino); pfix ("CLEARED"); } } } } }
void test_mcp23008(void) { struct mcp23008 mcp; int state; ptest(); debug_printf(STR_TAB "Press enter to start MCP23008 test..."); uart_getc(); uart_putc('\n'); /* Probe for the I/O expander */ debug_printf(STR_TAB "Probing for MCP23008...\n"); passert(mcp23008_probe(&mcp, &I2C0, 0x20) == 0); pokay("Found MCP23008!"); debug_printf(STR_TAB "Initializing P4-P7 to outputs with value 1\n"); passert(mcp23008_reg_write(&mcp, MCP_REG_OLAT, 0xf0) == 0); passert(mcp23008_reg_write(&mcp, MCP_REG_IODIR, 0x0f) == 0); passert(mcp23008_reg_write(&mcp, MCP_REG_IPOL, 0x00) == 0); debug_printf(STR_TAB "Press enter to turn on LED1..."); uart_getc(); uart_putc('\n'); passert(mcp23008_reg_write(&mcp, MCP_REG_GPIO, ~(1<<4) & 0xf0) == 0); do { pinteract("LED1 on?"); } while(0); debug_printf(STR_TAB "Press enter to turn on LED2..."); uart_getc(); uart_putc('\n'); passert(mcp23008_reg_write(&mcp, MCP_REG_GPIO, ~(1<<5) & 0xf0) == 0); do { pinteract("LED2 on?"); } while(0); debug_printf(STR_TAB "Press enter to turn on LED3..."); uart_getc(); uart_putc('\n'); passert(mcp23008_reg_write(&mcp, MCP_REG_GPIO, ~(1<<6) & 0xf0) == 0); do { pinteract("LED3 on?"); } while(0); debug_printf(STR_TAB "Press enter to turn on LED4..."); uart_getc(); uart_putc('\n'); passert(mcp23008_reg_write(&mcp, MCP_REG_GPIO, ~(1<<7) & 0xf0) == 0); do { pinteract("LED4 on?"); } while(0); debug_printf(STR_TAB "Press enter to turn on all LEDs..."); uart_getc(); uart_putc('\n'); passert(mcp23008_reg_write(&mcp, MCP_REG_GPIO, 0x00) == 0); do { pinteract("All LEDs on?"); } while(0); debug_printf(STR_TAB "Polling GPIOs\n"); state = 0; while (1) { uint8_t data; if (state == 0) { debug_printf(STR_TAB "Waiting for button 1 press...\n"); state++; } else if (state == 2) { debug_printf(STR_TAB "Waiting for button 2 press...\n"); state++; } else if (state == 4) { debug_printf(STR_TAB "Waiting for switch 1 on...\n"); state++; } else if (state == 6) { debug_printf(STR_TAB "Waiting for switch 2 on...\n"); state++; } else if (state == 8) { break; } if (mcp23008_reg_read(&mcp, MCP_REG_GPIO, &data) < 0) { pfail("mcp23008_reg_read() failed"); passert(false); } if (state == 1 && !(data & (1<<2))) { pokay("Got button 1 press!"); state++; } else if (state == 3 && !(data & (1<<3))) { pokay("Got button 2 press!"); state++; } else if (state == 5 && !(data & (1<<0))) { pokay("Got switch 1 on!"); state++; } else if (state == 7 && !(data & (1<<1))) { pokay("Got switch 2 on!"); state++; } delay_ms(25); } }
void test_spi(void) { struct spi_slave spi; uint8_t txbuf[1024], rxbuf[1024]; unsigned int i; ptest(); /* Enable GPIO12 / PIO1.0 for CS */ LPC_IOCON->R_PIO1_0 = 0x1; LPC_GPIO1->DIR |= (1<<0); /* Setup SPI slave */ spi.master = &SPI0; spi.cs_pin = 12; spi.cs_active_high = false; spi.speed = 1000000; spi.mode = 0; _test_spi_transfer(&spi, "SPI mode=0, speed=1e6, cs active low"); spi.mode = 1; _test_spi_transfer(&spi, "SPI mode=1, speed=1e6, cs active low"); spi.mode = 2; _test_spi_transfer(&spi, "SPI mode=2, speed=1e6, cs active low"); spi.mode = 3; _test_spi_transfer(&spi, "SPI mode=3, speed=1e6, cs active low"); spi.mode = 0; spi.cs_active_high = true; _test_spi_transfer(&spi, "SPI mode=0, speed=1e6, cs active high"); spi.mode = 0; spi.cs_active_high = false; spi.speed = 6000000; _test_spi_transfer(&spi, "SPI mode=0, speed=6e6, cs active low"); spi.speed = 12000000; _test_spi_transfer(&spi, "SPI mode=0, speed=12e6, cs active low"); spi.speed = 24000000; _test_spi_transfer(&spi, "SPI mode=0, speed=24e6, cs active low"); debug_printf("\n"); debug_printf(STR_TAB "Generating pseudorandom txbuf:\n"); srand(0xdeadbeef); for (i = 0; i < sizeof(txbuf); i++) { txbuf[i] = (uint8_t)rand(); rxbuf[i] = 0xff; debug_printf("%02x ", txbuf[i]); } debug_printf("\n"); spi.speed = 1000000; debug_printf(STR_TAB "Please tie MISO and MOSI for loopback test and press enter..."); uart_getc(); uart_putc('\n'); spi_setup(&spi); spi_select(&spi); spi_transfer(&spi, txbuf, rxbuf, sizeof(txbuf)); spi_deselect(&spi); if (memcmp(txbuf, rxbuf, sizeof(txbuf)) == 0) pokay("SPI mode=0, speed=1e6 loopback success!"); else { pfail("SPI mode=0, speed=1e6 loopback failed! Got back:"); for (i = 0; i < sizeof(rxbuf); i++) debug_printf("%02x ", rxbuf[i]); debug_printf("\n"); } }