/* ** fr_getc ** ** read one byte from a file ** ** INPUT : *filehandle - pointer to a file descriptor ** RETURN: -1 if error, otherwise the read character. */ int fr_getc ( F_FILE * filehandle ) { int rc; if( xSemaphoreTake( fs_lock_semaphore, F_MAX_LOCK_WAIT_TICKS ) == pdPASS ) { rc = fn_getc( filehandle ); xSemaphoreGive( fs_lock_semaphore ); } else { rc = -1; } return rc; }
/* * kw_expand -- expand src into dst with given n value for $n (or $N) * * n == -1 means expand src into a reglob * if gz is true, include ".gz" extension * * returns true if template contains $n or $N (implying rotation of files) */ boolean_t kw_expand(struct fn *src, struct fn *dst, int n, boolean_t gz) { int c; char buf[MAXDIGITS]; boolean_t hasn = B_FALSE; struct fn *kw = fn_new(NULL); char *ptr; struct tm *gmt_tm = localtime(&Now); while ((c = fn_getc(src)) != '\0') switch (c) { case '.': case '(': case ')': case '^': case '+': case '{': case '}': /* when building an re, escape with a backslash */ if (n < 0) fn_putc(dst, '\\'); fn_putc(dst, c); break; case '?': /* when building an re, change '?' to a single dot */ if (n < 0) fn_putc(dst, '.'); break; case '*': /* when building an re, change '*' to ".*" */ if (n < 0) fn_putc(dst, '.'); fn_putc(dst, '*'); break; case '$': /* '$' marks the start of a keyword */ switch (c = fn_getc(src)) { case '$': /* double '$' stands for a single '$' */ if (n < 0) fn_putc(dst, '\\'); fn_putc(dst, '$'); break; case '#': /* * $# expands to nothing, but forces an end * of keyword, allow juxtaposition of a * keyword with lower-case characters */ break; case 'n': case 'N': if (c == 'N' || !islower(fn_peekc(src))) { /* * we've found $n or $N, if we're * building an re, build one that * matches a number, otherwise * expand the keyword to the n * passed in to this function */ hasn = B_TRUE; if (n < 0) fn_puts(dst, "([0-9]+)$0"); else { (void) snprintf(buf, MAXDIGITS, "%d", (c == 'n') ? n : n + 1); fn_puts(dst, buf); } break; } /*FALLTHROUGH*/ default: /* gather up the keyword name */ fn_renew(kw, NULL); fn_putc(kw, c); while (islower(fn_peekc(src))) fn_putc(kw, fn_getc(src)); /* lookup keyword */ if ((ptr = (char *)lut_lookup(Keywords, fn_s(kw))) == NULL) { /* nope, copy it unexpanded */ if (n < 0) fn_putc(dst, '\\'); fn_putc(dst, '$'); fn_putfn(dst, kw); } else fn_puts(dst, ptr); } break; case '%': /* * % sequence for strftime(), if we're building * an re, we take our best guess at the re for * this sequence, otherwise we pass it to strftime() */ if (n < 0) { /* * the regex for a percent sequence is * usually just ".*" unless it is one * of the common cases we know about * that are numeric. in those cases, we * tighten up the regex to just match digits. * * while it is gross that we embed knowledge * of strftime() sequences here, they are * specified in a standard so aren't * expected to change often, and it *really* * cuts down on the possibility that we'll * expire a file that isn't an old log file. */ if ((c = fn_getc(src)) == 'E' || c == 'O') { c = fn_getc(src); fn_puts(dst, ".*"); } else switch (c) { case 'd': case 'g': case 'G': case 'H': case 'I': case 'j': case 'm': case 'M': case 'S': case 'u': case 'U': case 'V': case 'w': case 'W': case 'y': case 'Y': /* pure numeric cases */ fn_puts(dst, "[0-9]+"); break; case 'e': case 'k': case 'l': /* possible space then num */ fn_puts(dst, " *[0-9]+"); break; case 'D': /* %m/%d/%y */ /* adds slashes! */ fn_puts(dst, "[0-9]+/[0-9]+/[0-9]+"); break; case 'R': /* %H:%M */ fn_puts(dst, "[0-9]+:[0-9]+"); break; case 'T': /* %H:%M:%S */ fn_puts(dst, "[0-9]+:[0-9]+:[0-9]+"); break; default: fn_puts(dst, ".*"); } } else { char tbuf[4]; /* copy % sequence to tbuf */ tbuf[0] = '%'; tbuf[1] = fn_getc(src); if (tbuf[1] == 'E' || tbuf[1] == 'O') { /* "extended" sequence */ tbuf[2] = fn_getc(src); tbuf[3] = '\0'; } else tbuf[2] = '\0'; if (strftime(buf, MAXDIGITS, tbuf, gmt_tm) == 0) /* just copy %x */ fn_puts(dst, tbuf); else fn_puts(dst, buf); } break; default: /* nothing special, just copy it */ fn_putc(dst, c); } if (gz) { if (n < 0) fn_puts(dst, "(\\.gz){0,1}"); else fn_puts(dst, ".gz"); } fn_free(kw); return (hasn); }