Example #1
0
File: ej.c Project: jhbsz/LC4
void
do_ej(char *file, webs_t stream, ...)
{
    FILE *fp;
    int c;
    char pattern[512], *asp = NULL, *func = NULL, *end = NULL;
#define DO_EJ_PARA_MAX  8
    /* At most 'DO_EJ_PARA_MAX' parameters are allowed.
     * DO_EJ_PARA_MAX could not be more than 10 because $10 will be recognized
     * as $1.
     */
    char paras[DO_EJ_PARA_MAX][SHORT_BUF_LEN];
    int len = 0;
    int i = 0;
    va_list ap;
    char *para;

    if (!(fp = fopen(file, "r")))
        return;

    /* NOTE: va_arg() doesn't correctly return NULL if there is no parameter.
     * Therefore, we put an empty string as the last parameter for every
     * do_ej() function.
     * eg., do_ej("file.asp, "");
     */
    va_start(ap, stream);
    para = va_arg(ap, char *);
    while (para && *para) {
        strcpy(paras[i], para);
        i++;
        if (i >= DO_EJ_PARA_MAX) {
            break;
        }
        para = va_arg(ap, char *);
    } 
    
    /* i indicates the number of parameters for replacement. */
    while ((c = getc(fp)) != EOF) {
        if (c == '$') {
            c = getc(fp);
            if (c == EOF) {
                break;
            }
            if ((c - '0') < i && c - '0' >= 0) {
                /* $0 ~ $i */
                strcpy(&pattern[len], paras[c - '0']);
                len += strlen(paras[c - '0']);
            } else {
                /* Not what we want to replace. */
                sprintf(&pattern[len], "$%c", c);
                len += 2;
            }
            pattern[len] = '\0';
            continue;
        }
        /* Add to pattern space */
        pattern[len++] = c;
        pattern[len] = '\0';
        if (len == (sizeof(pattern) - 1))
            goto release;

        /* Look for <% ... */

        /* EZP: If "<%" appears, it will store in the "pattern buffer". */
       
        if (!asp && !strncmp(pattern, "<%", len)) {
            if (len == 2)
                asp = pattern + 2;
            continue;
        }

        /* Look for ... %> */
        if (asp) {
            if (unqstrstr(asp, "%>")) {
                /* The iterator could be ended before the string "%>". */
                for (func = asp; func < &pattern[len - 3]; func = end) {
                    /* Skip initial whitespace */
                    for (; isspace((int) *func); func++);
                    if (!(end = unqstrstr(func, ";")))
                        break;
                    *end++ = '\0';
                    /* 
                     * Either if allowed to print or if "do_print_end()", 
                     * let's execute the ej function. 
                     */
                    if (ej_print || !strncmp(func, "do_print_end", 
                                strlen("do_print_end"))) {
                        call(func, stream);
                    }
                }
                asp = NULL;
                len = 0;
            }
            continue;
        }
release:
        /* Release pattern space */
        if (ej_print) {
            if (wfputs(pattern, stream) == EOF) {
                printf("wfputs(pattern, stream) Error : code %d\n", ferror(stream));
                break;
            }
        }
        len = 0;
    }

    fclose(fp);
}
Example #2
0
static void do_ej_s(int (*get) (void), webs_t stream)	// jimmy, https, 8/4/2003
{
	int c, ret;
	char *pattern, *asp = NULL, *func = NULL, *end = NULL;
	int len = 0;
	memdebug_enter();
	FILE *backup_fp = s_fp;
	int backup_filecount = s_filecount;
	unsigned char *backup_filebuffer = s_filebuffer;
	unsigned int backup_filelen = s_filelen;

	pattern = (char *)safe_malloc(PATTERN_BUFFER + 1);
	while ((c = get()) != EOF) {
		/* Add to pattern space */
		pattern[len++] = c;
		pattern[len] = '\0';
		if (len == (PATTERN_BUFFER - 1))
			goto release;

		if (!asp) {
			char pat = pattern[0];
			if (pat == '{') {
				ret = decompress(stream, pattern, len);
				if (ret) {
					if (len == 3)
						len = 0;
					continue;
				}
			}
			/* Look for <% ... */
			if (pat == 0x3c) {
				if (len == 1)
					continue;
				if (pattern[1] == 0x25) {
					asp = pattern + 2;
					continue;
				}
			}
			pat = pattern[len - 1];
			if (pat == '{' || pat == 0x3c) {
				pattern[len - 1] = '\0';
				wfputs(pattern, stream);	//jimmy, https, 8/4/2003
				pattern[0] = pat;
				len = 1;
			}
			continue;
		} else {
			if (unqstrstr(asp, "%>")) {
				for (func = asp; func < &pattern[len]; func = end) {
					/* Skip initial whitespace */
					for (; isspace((int)*func); func++) ;
					if (!(end = uqstrchr(func, ';')))
						break;
					*end++ = '\0';
					/* Call function */
					backup_filecount = s_filecount;
					global_handle = call(global_handle, func, stream);
					// restore pointers
					s_fp = backup_fp;
					s_filebuffer = backup_filebuffer;
					s_filecount = backup_filecount;
					s_filelen = backup_filelen;
				}
				asp = NULL;
				len = 0;
			}
			continue;
		}

	      release:
		/* Release pattern space */
		wfputs(pattern, stream);	//jimmy, https, 8/4/2003
		len = 0;
	}
	if (len)
		wfputs(pattern, stream);	//jimmy, https, 8/4/2003

#ifndef MEMLEAK_OVERRIDE
	if (global_handle)
		dlclose(global_handle);
	global_handle = NULL
#endif
	    free(pattern);
	memdebug_leave();
}
Example #3
0
//------------------------------------------
int CFS_Stdio::Puts(const wchar* string, FSFILE* fp){
	assert(fp->pFileData!=NULL);
	int ret = wfputs(string, (FILE*)fp->pFileData);
	fp->nOffset = ftell((FILE*)fp->pFileData);
	return ret;
}