static void unlit(char *file, FILE *istream, FILE *ostream) { line last, this=START; int linesread=0; int defnsread=0; do { last = this; this = readline(istream, ostream); linesread++; if (this==DEFN) defnsread++; if (last==DEFN && this==TEXT) complain(file, linesread-1, MISSINGBLANK); if (last==TEXT && this==DEFN) complain(file, linesread, MISSINGBLANK); if (this==END) complain(file, linesread, SPURIOUSENDCODE); if (this == BEGIN) { /* start of code, copy to end */ char lineb[1000]; for(;;) { if (fgets(lineb, sizeof lineb, istream) == NULL) { complain(file, linesread, MISSINGENDCODE); exit(1); } linesread++; if (strncmp(lineb,ENDCODE,LENENDCODE) == 0) { myputc('\n', ostream); break; } fputs(lineb, ostream); } defnsread++; } #if defined(PSEUDOCODE) if (this == PSEUDO) { char lineb[1000]; for(;;) { if (fgets(lineb, sizeof lineb, istream) == NULL) { complain(file, linesread, MISSINGENDPSEUDOCODE); exit(1); } linesread++; myputc('\n', ostream); if (strncmp(lineb,ENDPSEUDOCODE,LENENDPSEUDOCODE) == 0) { break; } } } #endif if (this == SHEBANG) { myputc('\n', ostream); } } while(this!=ENDFILE); if (defnsread==0) complain(file,linesread,EMPTYSCRIPT); }
static void save_keystring(struct uih_context *uih, const char *name) { if (!first) myputc(' '); else first = 0; myputc('\''); myputs(name); }
static void save_float(struct uih_context *uih, number_t number) { if (!first) myputc(' '); else first = 0; #ifdef HAVE_LONG_DOUBLE /*20 should be enought to specify 64digit number :) */ #ifdef USE_XLDIO x_ldout((long double) number, 20, uih->savec->file); #else { char s[256]; sprintf(s, "%.20LG", (long double) number); myputs(s); } #endif #else { char s[256]; sprintf(s, "%.20G", (double) number); myputs(s); } #endif }
static void save_string(struct uih_context *uih, const char *text) { int i = 0; if (!first) myputc(' '); else first = 0; myputc('"'); while (text[i] != 0) { if (text[i] == '"') myputc('\\'); myputc(text[i]); i++; } myputc('"'); }
static void save_float2(struct uih_context *uih, number_t number, int places) { char fs[10]; if (!first) myputc(' '); else first = 0; if (places < 0) places = 0; if (places > 20) places = 20; #ifdef HAVE_LONG_DOUBLE #ifdef USE_XLDIO fs[0] = 0; /* Avoid warning */ x_ldout((long double) number, places, uih->savec->file); #else { char s[256]; sprintf(fs, "%%.%iLG", places); sprintf(s, fs, (long double) number); myputs(s); } #endif #else { char s[256]; sprintf(fs, "%%.%iG", places); sprintf(s, fs, (double) number); myputs(s); } #endif }
void myputs(const char *str) { while(*str){ myputc(*str); str++; } }
/* P u t p i p e * * put an index into the pipeline. */ void putpipe(unsigned u, int flag) { static unsigned short flags, *flagp; static int n = 0; /* number of flags in flags */ if (pnum == 0) { /* if we should write to stdout */ myputc(u); /* index will be the char value */ return; } if (n == 0) { /* if we need to reserve a flag entry */ flags = 0; flagp = opbuf + opbufind; opbufind++; } opbuf[opbufind++] = u; /* add index to buffer */ flags = (flags << 1) | flag; /* add firstonly flag */ if (++n >= 15) { /* if block of 15 indices */ n = 0; *flagp = flags; /* insert flags entry */ if (opbufind >= BUFSZ_2) { /* if pipe out buffer full */ opbufind = 0; if (write(fdout, (char *) opbuf, BUFSZ) != BUFSZ) die("bad pipe write\n"); } } }
static void save_keyword (struct uih_context *uih, CONST char *name) { if (!first) myputc (' '); else first = 0; myputs (name); }
static void save_onoff(struct uih_context *uih, int number) { if (!first) myputc(' '); else first = 0; myputs(number ? "#t" : "#f"); }
void myputs(const char *str) { /* 发送字符串 */ while(*str) { myputc(*str); str++; } }
static void save_int(struct uih_context *uih, int number) { char s[256]; if (!first) myputc(' '); else first = 0; sprintf(s, "%i", number); myputs(s); }
void send_impulses(transmit_modes tx_mode) { /* * Send all saved impulses via the given transmit mode */ if (tx_mode == uart) { unsigned int i; for (i = 0; i < current_impulse; i++) { // myputc((impulseData[i].year >> 12) + 0x30); // Send BCD as ASCII // myputc(((impulseData[i].year >> 8) & 0xf) + 0x30); myputs("20"); myputc(((impulseData[i].year >> 4) & 0xf) + 0x30); myputc((impulseData[i].year & 0xf) + 0x30); myputc((impulseData[i].mon >> 4) + 0x30); myputc((impulseData[i].mon & 0xf) + 0x30); myputc((impulseData[i].day >> 4) + 0x30); myputc((impulseData[i].day & 0xf) + 0x30); // myputc((impulseData[i].dow >> 4) + 0x30); // myputc((impulseData[i].dow & 0xf) + 0x30); myputc((impulseData[i].hour >> 4) + 0x30); myputc((impulseData[i].hour & 0xf) + 0x30); myputc((impulseData[i].min >> 4) + 0x30); myputc((impulseData[i].min & 0xf) + 0x30); myputc((impulseData[i].sec >> 4) + 0x30); myputc((impulseData[i].sec & 0xf) + 0x30); myputs("\r\n"); } } else if (tx_mode == cc2500) {
static void start_save(struct uih_context *uih, const char *name) { if (!changed && !uih->savec->firsttime) { char s[256]; sprintf(s, "\n(usleep %i)\n", tl_lookup_timer(uih->savec->timer)); myputs(s); tl_reset_timer(uih->savec->timer); } changed = 1; myputc('('); myputs(name); first = 0; }
void myputs(char *string1) { char mychar; int j; j=0; do { mychar=string1[j]; if(mychar!=0) { myputc(mychar); j++; } } while(mychar !=0); return; }
/* Flushes the bit output buffer and the byte output buffer */ void bitoflush() { register unsigned int outbyte; while (bits < 32) { outbyte = reg >> 24; myputc(outbyte, out); if ( outbyte == 0xff ) { bits += 7; reg <<= 7; reg &= ~(1<<(8*sizeof(reg)-1)); /* stuff a 0 at MSB */ } else { bits += 8; reg <<= 8; } } flushbuff(out); bitoinit(); }
int main() { uart_init(); myputc('1'); myputc('2'); myputc('3'); myputc('4'); myputc('5'); myputc('6'); myputs("hello world!\n"); led_test(); return 0; }
void _myprintf(void (*myputc)(char),const char* str,va_list arp) //自制printf底层 { unsigned char f,r,fl=0,l=3,lt,jj; //默认留3位小数,小数可留0~7位 unsigned char i,j,w,lp; unsigned long v; char c, d, s[16], *p; int res, chc, cc,ll; int kh,kl,pow=1; double k; for (cc=res=0;cc!=-1;res+=cc) //解析格式化字符串,且输出 { c = *str++; //每一轮取一个字符 //---------------------------------//读取到'\0',结束程序 if (c == 0) break; //---------------------------------//读取到非'%'符号时 if (c != '%') { myputc(c); continue; } //---------------------------------//读取到'%'符号时 w=f=0; k=0; lp=0; c=*str++; //越过'%',读其格式 if (c == '0') { //%0,0填充 f = 1; c = *str++; //f0填充标记位置1,读取下一个字符 } else if (c == '-') { //%-,左对齐(左边填空格) f = 2; c = *str++; //f左对齐标记位置1,读取下一个字符 } else if (c == '.') { //%.3f表示留3位小数 fl=1;c=*str++; } while (((c)>='0')&&((c)<='9')) { //"%030",将30转换为数字 if(fl==1){ lp=lp*10+c-'0'; c=*str++; } else{ w=w*10+c-'0'; c=*str++; } //将数字读完 } if(fl==1) l=(lp>7)? 7:lp; if (c == 'l' || c == 'L') { //%ld等长形数 f |= 4; c = *str++; //f长形标记位置1,读下一个字符 } if (!c) break; //如果此时无字符,结束输出 //---------------------------------//处理格式化标识符(d,s,c,x,o,f) d = c; if (((c)>='a')&&((c)<='z')) d -= 0x20; //如果是小写,划归成大写处理 switch (d) { //分类%*的情况 case 'S' : /* String */ p = va_arg(arp,char*); //取字符串变量 for(j=0;p[j];j++); //长度计算 ll=j; chc = 0; if (!(f&2)) { //不用左对齐,左边就要补空格%06s while (j++ < w) { myputc(' '); chc+=1; } } jj=0; while (p[jj]!='\0') { myputc(p[jj]); jj++; } chc+=ll; while (j++ < w) //左对齐,左边就不用空格,右边填空格%-06s { myputc(' '); chc+=1; } cc = chc; continue; case 'C' : { /* Character */ myputc((char)va_arg(arp,int)); continue; } case 'F' : //默认保留3位小数 { /* double(64位)/float(32位) */ k=va_arg(arp,double); if(k<0){ l|=8; k*=-1; //负数置低位1 } kh=(int)k; //整数小数分离 pow=1; lt=l&7; while((lt-1)>=0){ pow*=10; lt--; } kl=(int)(pow*(k-kh)); i=0; lt=l&7; while(lt--){ //存入小数部分 if(kl){ d=(char)(kl%10); //按10进制取余,即取个位,化归为字符处理 kl/=10; //递进 s[i++]='0'+d; //倒叙存入s } else s[i++]='0'; } s[i++]='.'; //加入小数点 do{ //存入整数部分 d=(char)(kh%10); //按10进制取余,即取个位,化归为字符处理 kh/=10; //递进 s[i++]='0'+d; //倒叙存入s }while(kh && i<sizeof s /sizeof s[0]); if (l&8)s[i++]='-'; //添加符号(注意低位在前) fl=0; goto PRT; } case 'B' : /* Binary */ r = 2; break; case 'O' : /* Octal */ r = 8; break; case 'D' : /* Signed decimal */ case 'U' : /* Unsigned decimal */ r = 10; break; case 'X' : /* Hexdecimal */ r = 16; break; default:{ /* Unknown type (pass-through) */ myputc(c); cc=1; continue; } } /* Get an argument and put it in numeral */ /*取变量 是%ld型? 有则按long取变量 没有l但有d则按int取变量 没有l也没有d则先按unsigned int取变量在做处理 */ v =(f&4)?(unsigned long)va_arg(arp, long):((d=='D')?(unsigned long)(long)va_arg(arp, int):(unsigned long)va_arg(arp, unsigned int)); if (d == 'D' && (v & 0x80000000)) { //是%d,且符号位为1(负数) //只有int型有增幅之分 v = 0 - v; //化归为正数处理 f |= 8; //f符号标记位置1 } i = 0; do { //数字解析成字符 d=(char)(v%r); //按进制r取余,即取个位,化归为字符处理 v/=r; //递进 if(d>9)d+=(c=='x')?0x27:0x07; //判读,不是10进制而是16进制时,d欲先加一个间隔跳转至'A'(大写X)或'a'(小写X) s[i++]=d+'0'; //化归为字符串处理(注意低位在前) } while (v && i<sizeof s /sizeof s[0]); //(i<sizeof s /sizeof s[0])防止含0类数字误判 if (f & 8) s[i++] = '-'; //添加符号(注意低位在前) PRT: j=i; d=(f&1)?'0':' '; //判断0填充还是左对齐 while (!(f&2)&&j++<w) { myputc(d); } do { myputc(s[--i]); }while(i); while (j++ < w) { myputc(d); } } }
line readline(FILE *istream, FILE *ostream) { int c, c1; char buf[100]; int i; c = egetc(istream); if (c==EOF) return ENDFILE; if ( c == '#' ) { if ( ignore_shebang ) { c1 = egetc(istream); if ( c1 == '!' ) { while (c=egetc(istream), !isLineTerm(c)) ; return SHEBANG; } myputc(c, ostream); c=c1; } if ( leavecpp ) { myputc(c, ostream); while (c=egetc(istream), !isLineTerm(c)) myputc(c,ostream); myputc('\n',ostream); return HASH; } } if (c==DEFNCHAR) { myputc(' ',ostream); while (c=egetc(istream), !isLineTerm(c)) myputc(c,ostream); myputc('\n',ostream); return DEFN; } if (!crunchnl) myputc('\n',ostream); while (isWhitespace(c)) c=egetc(istream); if (isLineTerm(c)) return BLANK; i = 0; buf[i++] = c; while (c=egetc(istream), !isLineTerm(c)) if (i < sizeof buf - 1) buf[i++] = c; while(i > 0 && isspace(buf[i-1])) i--; buf[i] = 0; if (strcmp(buf, BEGINCODE) == 0) return BEGIN; if (strcmp(buf, ENDCODE) == 0) return END; #ifdef PSEUDOCODE else if (strcmp(buf, BEGINPSEUDOCODE) == 0) return PSEUDO; #endif else return TEXT; }
int myvprintf(const char *fmt, va_list ap) { size_t strlen(const char *); union { intmax_t i; uintmax_t u; long double f; void *p; } u; int flags, width, precision = -1, i; char c, buf[256], *bufp, *bufb; enum { ALT = 1, ZERO = 2, LEFT = 4, SIGN = 8, SPACE = 16, }; while ((c = *fmt++)) { if (c != '%') { myputc(c); continue; } else { c = *fmt++; goto FLAGS; } FLAGS: flags = 0; do { switch (c) { case '#': flags |= ALT; break; case '0': flags |= ZERO; break; case '+': flags |= SIGN; break; case '-': flags |= LEFT; break; case ' ': flags |= SPACE; break; default: goto WIDTH; } } while ((c = *fmt++)); WIDTH: width = 0; do { switch (c) { case '*': width = va_arg(ap, int); c = *fmt++; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': width = width * 10 + c - '0'; break; default: goto PRECISION; } } while ((c = *fmt++)); PRECISION: if (c == '.') { precision = 0; while ((c = *fmt++)) { switch (c) { case '*': precision = va_arg(ap, int); c = *fmt++; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': precision = precision * 10 + c - '0'; break; default: goto LENGTH; } } } LENGTH: switch (c) { case 'h': if (fmt[1] == 'h') { fmt++; switch (c = *fmt++) { case 'd': case 'i': u.i = va_arg(ap, int); goto SPECIFIER; case 'u': case 'o': case 'x': case 'X': u.u = va_arg(ap, unsigned int); goto SPECIFIER; default: return -1; } } else { switch (c = *fmt++) {
int main (int argc, char **argv) { char *ctype, *s1, *s2, *s3, *boundary, *eof, *name; Sfio_t *fp; int state, boundaryn, eofn, atnl, i, n; char c1, c2; if (!(ctype = getenv ("CONTENT_TYPE"))) error (3, "cannot find CONTENT_TYPE env. variable"); if (!(s1 = strchr (ctype, ';'))) error (3, "no field divider"); *s1++ = 0; if (!(s2 = strstr (s1, "boundary="))) error (3, "no boundary field"); s2 += strlen ("boundary="); boundary = strdup (sfprints ("--%s", s2)); boundaryn = strlen (boundary); eof = strdup (sfprints ("%s--", boundary)); eofn = strlen (eof); state = 0; name = NULL; fp = NULL; init (eofn); for (;;) { next: if (state == 0) { /* before eof or boundary */ if (ensurechar ('\n') == -1) error (3, "cannot find NL"); s1 = getcurr (); if (strncmp (s1, eof, eofn) == 0) break; if (strncmp (s1, boundary, boundaryn) == 0) { if (s1[boundaryn] == '\r' && s1[boundaryn + 1] == '\n') advance (boundaryn + 2); else if (s1[boundaryn] == '\n') advance (boundaryn + 1); else error (3, "no CR+NL or NL after boundary"); } else error (3, "boundary or eof not found"); binmode = 0; state = 1; } else if (state == 1) { /* at header section */ if (ensurechar ('\n') == -1) error (3, "cannot find NL"); s1 = getcurr (); if (*s1 == '\r' && *(s1 + 1) == '\n') { advance (2); state = 2; continue; } else if (*s1 == '\n') { advance (1); state = 2; continue; } s3 = strchr (s1, '\n'); c1 = *s3, *s3 = 0; if (strstr (s1, "Content-Type: ")) binmode = 1; if ((s2 = strstr (s1, " name=\""))) { name = s2 + 7; if (!(s2 = strchr (name, '"'))) error (3, "cannot find matching \" for name"); c2 = *s2; *s2 = 0; name = strdup (name); *s2 = c2; if (!(fp = sfopen (NULL, name, "a"))) error (3, "sfopen for name %s failed", name); } *s3 = c1; advance (s3 - s1 + 1); } else if (state == 2) { atnl = 1; wrotenl = 0; for (;;) { next2: ensure (eofn); if ((n = getsize ()) < eofn) error (3, "unexpected EOF"); s1 = getcurr (); if (atnl == 1 && strncmp (s1, boundary, boundaryn) == 0) { if (count2 > 1 && buf2[0] != '\r') sfputc (fp, buf2[0]); if (!binmode && !wrotenl) sfputc (fp, '\n'); count2 = 0; sfclose (fp); state = 0; goto next; } atnl = 0; for (i = 0; i < n; i++) { myputc (fp, s1[i]); if (s1[i] == '\n') { atnl = 1; advance (i + 1); goto next2; } } advance (n); } } } return 0; }
/* Main function starts */ int main() { clock_t begin, end; int option, n, temp; const char *a="Writing byte by byte\n"; const char *b="Writing using buffers\n"; unsigned long i, bytes_to_write = BYTES_TO_READ_WRITE, bytes_to_read = BYTES_TO_READ_WRITE; char ch; while(1) { printf("\n 1 - Write without buffering \n 2 - Write with buffering"); printf("\n 3 - Read without buffering \n 4 - Read with buffering"); printf(("\n 5 - Exit \n Enter the option: ")); scanf("%d", &option); switch(option) { case 1: /* Write without buffer */ begin = clock(); for (i=0;i<bytes_to_write;i++) { ch = a[i%strlen(a)]; mywritec(ch); } end = clock(); printf("\n Time to write without buffering: %f secs\n",(double)(end - begin)/CLOCKS_PER_SEC); break; case 2: /* Write with buffer */ printf("\n Enter the buffer size in bytes: "); scanf("%d", &n); mywritebufsetup(n); begin = clock(); for (i=0;i<bytes_to_write;i++) { ch = b[i%strlen(b)]; myputc(ch); } mywriteflush(); end = clock(); printf("\n Time to write with buffering: %f secs\n",(double)(end - begin)/CLOCKS_PER_SEC); break; case 3: /* Read without buffer */ fd_read = open(READABLE_FILE, O_RDONLY); if(fd_read < 0) { printf("\n Error opening the readable file \n"); return 1; } begin = clock(); for (i=0;i<bytes_to_read;i++) { /* you may need to modify this slightly to print the character received and also check for end of file */ if(myreadc() == -1) { printf("\n End of file \n"); break; } } end = clock(); if(close(fd_read)) { printf("\n Error while closing the file \n "); } printf("\n Time to read without buffering: %f secs\n",(double)(end - begin)/CLOCKS_PER_SEC); break; case 4: /* Read with buffer */ printf("\n Enter the buffer size in bytes: "); scanf("%d", &n); myreadbufsetup(n); fd_read = open(READABLE_FILE, O_RDONLY); if(fd_read < 0) { printf("\n Error opening the readable file \n"); return 1; } begin = clock(); for (i=0;i<bytes_to_read;i++) { /* you may need to modify this slightly to print the character received and also check for end of file */ if(mygetc() == -1) { printf("\n End of file \n"); break; } } end = clock(); if(close(fd_read)) { printf("\n Error while closing the file \n "); } printf("\n Time to read with buffering: %f secs\n",(double)(end - begin)/CLOCKS_PER_SEC); break; default: return 0; } } }
static void stop_save(struct uih_context *uih) { myputc(')'); myputc('\n'); }