/* * Push a copy of a line onto the undo stack. Push a patch so we can * later fix up any references to this line that might already be in the * stack. When the undo happens, the later pops (i.e. those lines still * in the stack) will point at the _original_ (which will by then be on the * redo stack) instead of at the copy, which will have just been popped, * unless we fix them by popping and using the patch. */ int copy_for_undo(LINE *lp) { int status = FALSE; LINE *nlp; TRACE2((T_CALLED "copy_for_undo(%p)\n", lp)); if (needundocleanup) preundocleanup(); if (liscopied(lp)) { status = TRUE; } else if ((nlp = copyline(lp)) == 0) { status = ABORT; } else { /* take care of the normal undo stack */ pushline(nlp, BACKSTK(curbp)); make_undo_patch(lp, nlp); lsetcopied(lp); setupuline(lp); FORWDOT(curbp).l = lp; FORWDOT(curbp).o = DOT.o; status = TRUE; } return2Code(status); }
int main(void) { char buffer[MINLENGTH]; int status = 0; while (status != -1) { status = readbuff(buffer); if (status == 1) status = copyline(buffer); } return 0; }
main(){ char line[MINLENGTH]; int status =0; while(status != -1){ status = getlne(line); if(status == 1) status = copyline(line); } return 0; }
main() { int len; /* current line length */ int max = 0; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ while ((len = Getline(line, MAXLINE)) > 0) if (len > max) { max = len; copyline(longest, line); } if (max> 0) /* there was a line */ printf("\n\nThe line : %swas the longest.\n\n", longest); return 0; }
/* ==================================== */ scene * copyscene(scene * s) /* ==================================== */ #undef F_NAME #define F_NAME "copyscene" { scene * sc; int32_t i, nobj = s->nobj; object *obj; sc = (scene *)calloc(1,sizeof(scene)); if (sc == NULL) { fprintf(stderr, "%s : malloc failed\n", F_NAME); return NULL; } sc->nobj = nobj; sc->tabobj = (object **)calloc(1,nobj * sizeof(object *)); if (sc->tabobj == NULL) { fprintf(stderr, "%s: malloc failed\n", F_NAME); return NULL; } for (i = 0; i < nobj; i++) { obj = s->tabobj[i]; switch (obj->objtype) { case OBJTYPE_LINE: sc->tabobj[i] = copyline(obj); break; case OBJTYPE_CLOSEDLINE: sc->tabobj[i] = copyclosedline(obj); break; case OBJTYPE_SPLINE: sc->tabobj[i] = copyspline(obj); break; case OBJTYPE_CLOSEDSPLINE: sc->tabobj[i] = copyclosedspline(obj); break; default: fprintf(stderr, "%s: bad object typs: %d\n", F_NAME, obj->objtype); return 0; } // switch (obj->objtype) } // for (i = 0; i < nobj; i++) return sc; } // copyscene()
main() { int initlen,trimlen; /* current line lengths */ int max = 0; /* maximum length seen so far */ char initline[MAXLINE]; /* current input line as typed */ char revline[MAXLINE]; /* current input line reversed */ char longline[MAXLINE]; /* longest line saved here */ while ((trimlen = initlen = Getline(initline, MAXLINE)) > 0) { trimlen = trimline(initline, initlen); reverseline(initline, revline, trimlen); if (trimlen > max) { max = trimlen; copyline(longline, revline); } if (trimlen >= MINPRINT) { printf("\n\nThe trimmed & reversed line : \n%s\nwas long enough to print (>= %d characters)\n" "specifically %d characters.\n\n", revline, MINPRINT, trimlen); if (initlen > MAXLINE) printf("Maximum untrimmed line length exceeded (%d charaters), excess " "input lost.\n\n", MAXLINE); } if (trimlen < initlen) printf("Initial line trimmed & reversed from %d to %d characters by removal of trailing white space\n\n",initlen,trimlen); if (trimlen == 0) printf("Line rejected. Nothing other than whitespace characters\n\n"); } if (max > 0) { printf("\n\nThe line : \n%s\nwas the longest (%d characters).\n\n", longline, max); if (max > MAXLINE) printf("Maximum line length exceeded (%d charaters), excess input lost.\n\n", MAXLINE); } else printf("\n\nNo input???? So no maxline to print.\n\n"); return 0; }
int split_patch(struct stream f, struct stream *f1, struct stream *f2) { struct stream r1, r2; int chunks = 0; char *cp, *end; int state = 0; int acnt = 0, bcnt = 0; int a, b, c, d; int lineno = 0; char before[100], after[100]; char func[100]; f1->body = f2->body = NULL; r1.body = xmalloc(f.len); r2.body = xmalloc(f.len); r1.len = r2.len = 0; func[0] = 0; cp = f.body; end = f.body+f.len; while (cp < end) { /* state: * 0 not in a patch * 1 first half of context * 2 second half of context * 3 unified */ lineno++; switch (state) { case 0: if (sscanf(cp, "@@ -%99s +%99s @@%99[^\n]", before, after, func) >= 2) { int ok = 1; if (sscanf(before, "%d,%d", &a, &b) == 2) acnt = b; else if (sscanf(before, "%d", &a) == 1) acnt = 1; else ok = 0; if (sscanf(after, "%d,%d", &c, &d) == 2) bcnt = d; else if (sscanf(after, "%d", &c) == 1) bcnt = 1; else ok = 0; if (ok) state = 3; else state = 0; } else if (sscanf(cp, "*** %d,%d ****", &a, &b) == 2) { acnt = b-a+1; state = 1; } else if (sscanf(cp, "--- %d,%d ----", &c, &d) == 2) { bcnt = d-c+1; state = 2; } else sscanf(cp, "***************%99[^\n]", func); skip_eol(&cp, end); if (state == 1 || state == 3) { char *f; char buf[20]; buf[0] = 0; chunks++; sprintf(buf+1, "%5d %5d %5d", chunks, a, acnt); memcpy(r1.body+r1.len, buf, 18); r1.len += 18; f = func; while (*f == ' ') f++; if (*f) { r1.body[r1.len++] = ' '; strcpy(r1.body + r1.len, f); r1.len += strlen(f); } r1.body[r1.len++] = '\n'; r1.body[r1.len++] = '\0'; } if (state == 2 || state == 3) { char buf[20]; buf[0] = 0; sprintf(buf+1, "%5d %5d %5d\n", chunks, c, bcnt); memcpy(r2.body+r2.len, buf, 20); r2.len += 20; } if (state) func[0] = 0; break; case 1: if ((*cp == ' ' || *cp == '!' || *cp == '-' || *cp == '+') && cp[1] == ' ') { cp += 2; copyline(&r1, &cp, end); acnt--; if (acnt == 0) state = 0; } else { fprintf(stderr, "%s: bad context patch at line %d\n", Cmd, lineno); return 0; } break; case 2: if ((*cp == ' ' || *cp == '!' || *cp == '-' || *cp == '+') && cp[1] == ' ') { cp += 2; copyline(&r2, &cp, end); bcnt--; if (bcnt == 0) state = 0; } else { fprintf(stderr, "%s: bad context patch/2 at line %d\n", Cmd, lineno); return 0; } break; case 3: if (*cp == ' ') { char *cp2; cp++; cp2 = cp; copyline(&r1, &cp, end); copyline(&r2, &cp2, end); acnt--; bcnt--; } else if (*cp == '-') { cp++; copyline(&r1, &cp, end); acnt--; } else if (*cp == '+') { cp++; copyline(&r2, &cp, end); bcnt--; } else { fprintf(stderr, "%s: bad unified patch at line %d\n", Cmd, lineno); return 0; } if (acnt <= 0 && bcnt <= 0) state = 0; break; } } if (r1.len > f.len || r2.len > f.len) abort(); *f1 = r1; *f2 = r2; return chunks; }
/* * extract parts of a "diff3 -m" or "wiggle -m" output */ int split_merge(struct stream f, struct stream *f1, struct stream *f2, struct stream *f3) { int state = 0; char *cp, *end; struct stream r1, r2, r3; f1->body = NULL; f2->body = NULL; r1.body = xmalloc(f.len); r2.body = xmalloc(f.len); r3.body = xmalloc(f.len); r1.len = r2.len = r3.len = 0; cp = f.body; end = f.body+f.len; while (cp < end) { /* state: * 0 not in conflict * 1 in file 1 of conflict * 2 in file 2 of conflict * 3 in file 3 of conflict * 4 in file 2 but expecting 1/3 next * 5 in file 1/3 */ int len = end-cp; switch (state) { case 0: if (len >= 8 && strncmp(cp, "<<<<<<<", 7) == 0 && (cp[7] == ' ' || cp[7] == '\n') ) { char *peek; state = 1; skip_eol(&cp, end); /* diff3 will do something a bit strange in * the 1st and 3rd sections are the same. * it reports * <<<<<<< * 2nd * ======= * 1st and 3rd * >>>>>>> * Without a ||||||| at all. * so to know if we are in '1' or '2', skip forward * having a peek. */ peek = cp; while (peek < end) { if (end-peek >= 8 && (peek[7] == ' ' || peek[7] == '\n')) { if (strncmp(peek, "|||||||", 7) == 0 || strncmp(peek, ">>>>>>>", 7) == 0) break; else if (strncmp(peek, "=======", 7) == 0) { state = 4; break; } } skip_eol(&peek, end); } } else { char *cp2 = cp; copyline(&r1, &cp2, end); cp2 = cp; copyline(&r2, &cp2, end); copyline(&r3, &cp, end); } break; case 1: if (len >= 8 && strncmp(cp, "|||||||", 7) == 0 && (cp[7] == ' ' || cp[7] == '\n') ) { state = 2; skip_eol(&cp, end); } else copyline(&r1, &cp, end); break; case 2: if (len >= 8 && strncmp(cp, "=======", 7) == 0 && (cp[7] == ' ' || cp[7] == '\n') ) { state = 3; skip_eol(&cp, end); } else copyline(&r2, &cp, end); break; case 3: if (len >= 8 && strncmp(cp, ">>>>>>>", 7) == 0 && (cp[7] == ' ' || cp[7] == '\n') ) { state = 0; skip_eol(&cp, end); } else copyline(&r3, &cp, end); break; case 4: if (len >= 8 && strncmp(cp, "=======", 7) == 0 && (cp[7] == ' ' || cp[7] == '\n') ) { state = 5; skip_eol(&cp, end); } else copyline(&r2, &cp, end); break; case 5: if (len >= 8 && strncmp(cp, ">>>>>>>", 7) == 0 && (cp[7] == ' ' || cp[7] == '\n') ) { state = 0; skip_eol(&cp, end); } else { char *t = cp; copyline(&r1, &t, end); copyline(&r3, &cp, end); } break; } } if (cp > end) abort(); *f1 = r1; *f2 = r2; *f3 = r3; return state == 0; }
/************************************************ * fetch_logfile( char *logfilename, char *bookmarkfile, int updbm_flag ) ************************************************ */ int fetch_logfile( char *logfile, char *bmfile, int updbm_flag ) { bookmark obm, nbm; /* old, new bookmark */ char *ibuf = NULL; /* input buf (--> the logfile) */ size_t ilen = 0; char *ipt = NULL; char *bmpt = NULL; /* points to first new char if bm exists */ char *obuf = NULL; /* output buf (filled backwards) */ int opos; /* first used char pos in obuf */ /* for CONV_NAGIOS3 */ int lastlinepos = 0; /* pos of beginning of lastline in obuf */ int lastlinelen = 0; /* len of lastline, excl. trailing '\' 'n' */ int r; /* write's returncode */ int i; int fd = -1; struct stat sb; char *lgfile = NULL; int llen = 0; int done = 0; if( read_bookmark( bmfile, &obm ) ) return RET_ERROR; if( (fd=open( logfile, O_RDONLY )) == -1 ) { perr( "open", logfile, errno ); return RET_ERROR; } if( fstat( fd, &sb ) == -1 ) { perr( "stat", logfile, errno ); close( fd ); return RET_ERROR; } nbm.last = (size_t) sb.st_size; nbm.mtime = sb.st_mtime; nbm.inode = sb.st_ino; /* something changed meanwhile ? */ if( obm.mtime==nbm.mtime && obm.inode==nbm.inode && obm.last==nbm.last ) { if( conv_G & CONV_OKMSG ) r = write( STDOUT_FILENO, OK_MESSAGE "\n", sizeof( OK_MESSAGE ) ); close( fd ); return RET_OK; } /*****************/ obuf = malloc( fetchlen_G+1 ); if( obuf == NULL ) { perr( "malloc", NULL, errno ); close( fd ); return RET_ERROR; } opos = fetchlen_G; *(obuf + opos) = '\0'; /* dummy: opos -> first used char in obuf */ if( conv_G & CONV_NEWLINE ) { /* when using CONV_NEWLINE the obuf is filled up like this: 1. init: write '\\' 'n' '\n' to the end (3 chars) 2. fill (copyline() ) by first prepend line contents and then prepend '\\' 'n' result: An additional '\\' 'n' at the beginning else 1. fill (copyline() ) by first prepend newline and then prepend line contents */ *(obuf + --opos) = '\n'; *(obuf + --opos) = 'n'; *(obuf + --opos) = '\\'; } lgfile = (char*)malloc( strlen(logfile) + sizeof(".X") ); if( lgfile == NULL ) { free( obuf ); perr( "malloc", NULL, errno ); close( fd ); return RET_ERROR; } /* read in all logfiles and backward fill obuf upto fetchlen_G chars */ for( i=-1; i<10; i++ ) { /* i==-1: logfile without suffix, else i==logfile suffix */ if( i==-1 ) { /* lgfile is already open and sb contains the stat */ strcpy( lgfile, logfile ); }else{ sprintf( lgfile, "%s.%1d", logfile, i ); if( (fd=open( lgfile, O_RDONLY )) == -1 ) { if( errno==ENOENT && i==0 ) { continue; /* some logrotator start with .1 */ }else if( errno==ENOENT && i>0 ) { break; }else{ perr( "open", lgfile, errno ); free( obuf ); free( lgfile ); return RET_ERROR; } } if( fstat( fd, &sb ) == -1 ) { perr( "stat", lgfile, errno ); free( obuf ); free( lgfile ); close( fd ); return RET_ERROR; } } ilen = (size_t) sb.st_size; if( ilen == 0 ) { close( fd ); if( obm.inode == sb.st_ino ) break; continue; } ibuf = mmap( NULL, ilen, PROT_READ, MAP_SHARED, fd, (off_t)0 ); if( ibuf == MAP_FAILED ) { perr( "mmap", lgfile, errno ); free( obuf ); free( lgfile ); close( fd ); return RET_ERROR; } #ifdef HAS_MADVISE if( madvise( ibuf, ilen, MADV_RANDOM ) ) { perr( "madvise", NULL, errno ); free( obuf ); free( lgfile ); close( fd ); munmap( ibuf, ilen ); return RET_ERROR; } #endif /* check for old bookmark */ bmpt = NULL; if( obm.inode == sb.st_ino ) { bmpt = ibuf+obm.last; } /* scan backwards for lines but the first */ done = 0; for( llen=1,ipt=ibuf+ilen-2; ipt>=ibuf; llen++,ipt-- ) { if( *ipt=='\n' ) { if( ipt+1<bmpt ) { done=1; break; } opos = copyline( opos, obuf, ipt+1, llen ); if( opos==0 ) { done=1; break; } llen = 0; } } /* copy first line ? */ if( ipt+1==ibuf && done==0 ) { if( ipt+1<bmpt ) { done=1; } else{ opos = copyline( opos, obuf, ipt+1, llen ); } if( opos==0 ) { done=1; } } munmap( ibuf, ilen ); close( fd ); if( done ) break; if( bmpt ) break; /* processed a bookmarked file? --> finito */ } if( updbm_flag ) { if( write_bookmark( bmfile, &nbm ) ) return RET_ERROR; } /* if in Nagios3 mode: prepend short message (the last line fetched) */ if( conv_G & CONV_NAGIOS3 ) { /* Nagios2 --> Nagios3 changed accepted format for plugin output. * Nagios2 accepted a line containing one or more '\'+'n' as a single * line. Nagios3 now supports multiline output, and as a result, lines * containing '\'+'n' are now handled as multiline messages (as well as * messages having '\n'). The new multiline format is: * SHORT_MESSAGE | OPTIONAL_PERFORMANCE_DATA * LONG_MESSAGE_LINE_1 * LONG_MESSAGE_LINE_2 * ... * LONG_MESSAGE_LINE_N * * In Nagios3 mode fetchlog copies LONG_MESSAGE_LINE_N as SHORT_MESSAGE * and leaves OPTIONAL_PERFORMANCE_DATA empty */ int oidx = fetchlen_G - 4; /* in obuf: last char, last line */ lastlinepos = oidx + 1; /* fallback value: empty line at end */ lastlinelen = 0; /* fallback value: empty line at end */ /* determine lastlinepos and lastlinelen */ while( oidx > 0 ) { if( *(obuf + oidx) == 'n' && *(obuf + oidx -1) == '\\' ) { lastlinepos = oidx + 1; lastlinelen = fetchlen_G - lastlinepos - 3; /* 3: \ n \n */ break; } oidx--; } /* case: obuf has enough room for SHORT_MESSAGE */ if( lastlinelen + 1 <= opos ) { /* +1 = '|' */ *(obuf + --opos) = '|'; memmove( obuf+opos-lastlinelen, obuf+lastlinepos, lastlinelen ); opos -= lastlinelen; /* case: obuf too small: SHORT_MESSAGE and fetched messages overlap, * but not LONG_MESSAGE_LINE_N */ }else if( lastlinelen + 6 <= lastlinepos ) { /* +6 = '|\n...' */ memmove( obuf, obuf+lastlinepos, lastlinelen ); *(obuf + lastlinelen + 0 ) = '|'; *(obuf + lastlinelen + 1 ) = '\\'; *(obuf + lastlinelen + 2 ) = 'n'; *(obuf + lastlinelen + 3 ) = '.'; *(obuf + lastlinelen + 4 ) = '.'; if( *(obuf + lastlinelen + 5 ) != '\\' ) { *(obuf + lastlinelen + 5 ) = '.'; } opos = 0; /* case: obuf too small: SHORT_MESSAGE and fetched messages overlap, * including LONG_MESSAGE_LINE_N */ }else{ memmove( obuf+lastlinepos+2, obuf+lastlinepos, lastlinelen ); opos = lastlinepos + 2; } } /* only return a message if there is something to print */ if( ((conv_G & CONV_NEWLINE)==0 && fetchlen_G-opos==0 ) || ((conv_G & CONV_NEWLINE)!=0 && ( ((conv_G & CONV_NAGIOS3)== 0 && fetchlen_G-opos==3 ) || ((conv_G & CONV_NAGIOS3)!= 0 && fetchlen_G-opos==4 ) ) ) ) { if( conv_G & CONV_OKMSG ) { r = write( STDOUT_FILENO, OK_MESSAGE "\n", sizeof( OK_MESSAGE ) ); } i = RET_OK; }else{ r = write( STDOUT_FILENO, obuf+opos,fetchlen_G-opos); i = RET_NEWMSG; } free( obuf ); free( lgfile ); return i; } /* fetch_logfile() */
int main(int argc, char** argv) { int i = 0, ret, failCount = 0, passCount = 0, disabledCount = 0, failed, verbose = 0; int expectedRC; FILE* f = fopen( #ifdef _WIN32 /* hack for easier running from vs */ "..\\" #endif "tests.twok", "rb"); char* src = testdata; char* dest, *desc; if (argc >= 2 && strcmp(argv[1], "--http-repl") == 0) { twokHttpRepl(getExternFunc); return 0; } ret = /* warning suppress */ (int)fread(src, 1, 1<<24, f); if (argc >= 2 && strcmp(argv[1], "-v") == 0) { argc--; argv++; verbose = 1; } for (;; ++i) { if (src[0] != '#' || src[1] != '#' || src[2] != '#') { print("expecting ### line in tests\n"); exit(1); } src += 4; expectedRC = strtol(src, &src, 0); desc = description; src += 1; copyline(&desc, &src); *(desc - 1) = 0; if (strcmp(description, "END") == 0) { print("%d/%d tests passed (+%d disabled)\n", passCount, passCount + failCount, disabledCount); break; } dest = curtest; while (!(src[0] == '#' && src[1] == '#' && src[2] == '#')) copyline(&dest, &src); *dest = 0; if ((argc == 2 && atoi(argv[1]) != i) || (argc == 3 && (atoi(argv[1]) > i || atoi(argv[2]) < i))) continue; if (strncmp(description, "DISABLED", 8) == 0) { disabledCount++; continue; } print("[%3d %20s %s]: ", i, description, expectedRC == -1 ? "err" : " "); ret = twokRun(curtest, getExternFunc); failed = ret != expectedRC || (expectedRC == -1 && strstr(C.errorText, description) == NULL); print("%s\n", failed ? "FAILED": "ok"); failCount += failed; passCount += !failed; if (failed || verbose) { print("\n------------------------\n%s------------------------\n", curtest); print("rc=%d, want=%d, desc='%s'\n%s%s\n\n", ret, expectedRC, description, C.errorText[0] ? "Error:\n" : "", C.errorText); } } return failCount; }