📄 htp.c
字号:
if(StoreVariable(&varstore, htmlMarkup->attrib[ctr].name, htmlMarkup->attrib[ctr].value, VAR_TYPE_SET_MACRO, flag, NULL, NULL) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "unable to add variable to context for include file"); CloseFile(&incfile); DestroyVariableStore(&varstore); return MARKUP_ERROR; } } /* push this onto the context and use it for the include file */ PushVariableStoreContext(task->varstore, &varstore); topVarstore = &varstore; } else { topVarstore = task->varstore; } /* build a new task structure */ newTask.infile = &incfile; newTask.outfile = task->outfile; newTask.varstore = topVarstore; newTask.sourceFilename = task->sourceFilename; /* informational message for the user */ HtpMsg(MSG_INFO, task->infile, "including file \"%s\"", fullPathname); /* process the new input file */ result = ProcessTask(&newTask); /* pop the local context */ if(topVarstore == &varstore) { assert(PeekVariableStoreContext(topVarstore) == topVarstore); PopVariableStoreContext(topVarstore); DestroyVariableStore(&varstore); } CloseFile(&incfile); /* if the new file did not process, return an error, otherwise discard */ /* the markup */ return (result == TRUE) ? DISCARD_MARKUP : MARKUP_ERROR;}uint FileProcessor(TASK *task, HTML_MARKUP *htmlMarkup, char **newPlaintext){ const char *attribName; const char *attribValue; const char *externalName; struct tm *timeNow; struct stat fileStat; /* if a NAME attribute is found, and it contains a value, use the */ /* ExternalFileProcessor to create the plaintext (this function only */ /* reports output file's time, date, name) */ if(IsAttributeInMarkup(htmlMarkup, "NAME")) { if((externalName = MarkupAttributeValue(htmlMarkup, "NAME")) != NULL) { return ExternalFileProcessor(task, htmlMarkup, externalName, newPlaintext); } } /* if NAME attribute not in markup, or no external filename specified, */ /* only one attribute can be used: NAME, SIZE, TIME, DATE , or INCLUDE */ /* (the exception being EXECUTE and TEMPLATE) */ if(IsAttributeInMarkup(htmlMarkup, "EXECUTE") == TRUE) { return FileExecuteProcessor(task, htmlMarkup); } if(IsAttributeInMarkup(htmlMarkup, "TEMPLATE") == TRUE) { return FileTemplateProcessor(task, htmlMarkup); } if(IsAttributeInMarkup(htmlMarkup, "INCLUDE") == TRUE) { return FileIncludeProcessor(task, htmlMarkup); } if(htmlMarkup->attribCount != 1) { HtpMsg(MSG_ERROR, task->infile, "improper FILE syntax"); return MARKUP_ERROR; } /* get the attribute */ attribName = htmlMarkup->attrib[0].name; attribValue = htmlMarkup->attrib[0].value; /* act on the attribute */ if(stricmp(attribName, "SEARCH") == 0) { /* set the include search path to what was specified */ if(attribValue != NULL) { StringCopy(searchPath, attribValue, SEARCH_PATH_SIZE); } else { /* search path is cleared */ searchPath[0] = NUL; } return DISCARD_MARKUP; } else { /* NAME, TIME, DATE or bad tag */ /* first, allocate some space for the (possibly) new markup */ if((*newPlaintext = AllocMemory(MAX_TIME_DATE_SIZE)) == NULL) { HtpMsg(MSG_ERROR, task->infile, "unable to allocate memory for expansion"); return MARKUP_ERROR; } /* get the input files time, in case the attribute is TIME or DATE */ if(stat(task->sourceFilename, &fileStat) != 0) { HtpMsg(MSG_ERROR, task->infile, "unable to get information for file \"%s\"\n", task->infile->filename); return MARKUP_ERROR; } timeNow = localtime(&fileStat.st_mtime); if(stricmp(attribName, "TIME") == 0) { if(attribValue != NULL) { strftime(*newPlaintext, MAX_TIME_DATE_SIZE, attribValue, timeNow); } else { strftime(*newPlaintext, MAX_TIME_DATE_SIZE, "%I:%M:%S %p", timeNow); } HtpMsg(MSG_INFO, task->outfile, "adding local time"); } else if(stricmp(attribName, "DATE") == 0) { if(attribValue != NULL) { strftime(*newPlaintext, MAX_TIME_DATE_SIZE, attribValue, timeNow); } else { strftime(*newPlaintext, MAX_TIME_DATE_SIZE, "%a %b %d, %Y", timeNow); } HtpMsg(MSG_INFO, task->outfile, "adding local date"); } else if(stricmp(attribName, "NAME") == 0) { StringCopy(*newPlaintext, task->outfile->name, MAX_TIME_DATE_SIZE); HtpMsg(MSG_INFO, task->outfile, "adding output filename"); } else { /* no appropriate tags found */ HtpMsg(MSG_ERROR, task->infile, "invalid FILE tag attribute \"%s\"", attribName); /* free the allocated plaintext buffer */ FreeMemory(*newPlaintext); *newPlaintext = NULL; return MARKUP_ERROR; } } /* the new plaintext has been created */ return NEW_MARKUP;} uint SetProcessor(TASK *task, HTML_MARKUP *htmlMarkup, char **newPlaintext){ char *name; char *value; uint ctr; uint flag; HTML_ATTRIBUTE *attrib; UNREF_PARAM(newPlaintext); /* have to declare at least one macro, but more are acceptable */ if(htmlMarkup->attribCount == 0) { HtpMsg(MSG_ERROR, task->infile, "incomplete macro declaration"); return MARKUP_ERROR; } attrib = &htmlMarkup->attrib[0]; /* walk the list and add each macro to the variable store */ for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++) { /* get a private copy of the macro name */ if((name = DuplicateString(attrib->name)) == NULL) { HtpMsg(MSG_ERROR, task->infile, "unable to store macro's value (out of memory?)"); return MARKUP_ERROR; } value = attrib->value; flag = (attrib->quoted) ? VAR_FLAG_QUOTED : VAR_FLAG_NONE; /* put the new variable into the store */ if(StoreVariable(task->varstore, name, value, VAR_TYPE_SET_MACRO, flag, NULL, NULL) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "unable to store macro \"%s\" (out of memory?)", name); FreeMemory(name); return MARKUP_ERROR; } if(value != NULL) { HtpMsg(MSG_INFO, task->infile, "macro \"%s\" assigned value \"%s\"", name, value); } else { HtpMsg(MSG_INFO, task->infile, "macro \"%s\" created with null value", name); } FreeMemory(name); attrib++; } return DISCARD_MARKUP;} uint UnsetProcessor(TASK *task, HTML_MARKUP *htmlMarkup, char **newPlaintext){ uint ctr; uint type; const char *name; UNREF_PARAM(newPlaintext); /* have to specify at least one macro to remove, but more are acceptable */ if(htmlMarkup->attribCount == 0) { HtpMsg(MSG_ERROR, task->infile, "UNSET tag improperly specified"); } /* walk the attributes list */ for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++) { name = htmlMarkup->attrib[ctr].name; /* verify that the variable exists */ if(VariableExists(task->varstore, name) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "bad macro name \"%s\"", name); return MARKUP_ERROR; } /* remove it from the variable store if its not a DEF macro */ type = GetVariableType(task->varstore, name); if((type == VAR_TYPE_SET_MACRO) || (type == VAR_TYPE_BLOCK_MACRO)) { RemoveVariable(task->varstore, name); } HtpMsg(MSG_INFO, task->infile, "variable \"%s\" removed", name); } return DISCARD_MARKUP;}uint UseProcessor(TASK *task, HTML_MARKUP *htmlMarkup, char **newPlaintext){ char *name; const char *value; TEXTFILE incfile; int result; uint type; VARSTORE varstore; VARSTORE *topVarstore; uint ctr; uint flag; /* must declare at least 1 attribute, the macro name */ if(htmlMarkup->attribCount == 0) { HtpMsg(MSG_ERROR, task->infile, "macro declaration not complete"); return MARKUP_ERROR; } /* a variable reference should NOT include a new declaration */ if(htmlMarkup->attrib[0].value != NULL) { HtpMsg(MSG_ERROR, task->infile, "improper USE syntax"); return MARKUP_ERROR; } /* get a private copy of the name variable, this function will modify */ /* its own copy */ if((name = DuplicateString(htmlMarkup->attrib[0].name)) == NULL) { HtpMsg(MSG_ERROR, task->infile, "unable to expand macro (out of memory?)"); return MARKUP_ERROR; } /* verify the macro exists */ if(VariableExists(task->varstore, name) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "macro %s has not been declared", name); FreeMemory(name); return MARKUP_ERROR; } /* get the value of the macro */ value = GetVariableValue(task->varstore, name); /* get the type of macro */ type = GetVariableType(task->varstore, name); if(type == VAR_TYPE_INTERNAL) { /* oof ... the user picked a variable name we use internally */ /* !! a fix is to use both type and name as a key to get variable */ /* out of hash, and therefore the name is not the only identifier */ /* this will have to wait for later */ HtpMsg(MSG_ERROR, task->infile, "reserved variable name \"%s\" used ... please use different name in HTP file", name); FreeMemory(name); return MARKUP_ERROR; } if(type == VAR_TYPE_DEF_MACRO) { /* nope */ HtpMsg(MSG_ERROR, task->infile, "illegal to dereference a DEF macro with USE"); FreeMemory(name); return MARKUP_ERROR; } assert((type == VAR_TYPE_SET_MACRO) || (type == VAR_TYPE_BLOCK_MACRO)); /* if more than one parameter is on the USE tag, then assume they are */ /* local variables for the macro */ if(htmlMarkup->attribCount > 1) { if(type == VAR_TYPE_SET_MACRO) { /* nope, not yet */ HtpMsg(MSG_ERROR, task->infile, "macro parameters can only be used for BLOCK macros"); FreeMemory(name); return MARKUP_ERROR; } /* create a local variable store */ if(InitializeVariableStore(&varstore) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "unable to initialize local context for macro"); FreeMemory(name); return MARKUP_ERROR; } /* add each additional parameter to the local varstore */ for(ctr = 1; ctr < htmlMarkup->attribCount; ctr++) { flag = (htmlMarkup->attrib[ctr].quoted == TRUE) ? VAR_FLAG_QUOTED : VAR_FLAG_NONE; if(StoreVariable(&varstore, htmlMarkup->attrib[ctr].name, htmlMarkup->attrib[ctr].value, VAR_TYPE_SET_MACRO, flag, NULL, NULL) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "unable to add variable to block's local context"); DestroyVariableStore(&varstore); FreeMemory(name); return MARKUP_ERROR; } } /* make this variable store the topmost context */ PushVariableStoreContext(task->varstore, &varstore); topVarstore = &varstore; } else { topVarstore = task->varstore; } if(type == VAR_TYPE_SET_MACRO) { /* if NULL, then the macro was declared with no value, this is okay, */ /* just don't do anything */ if(value == NULL) { FreeMemory(name); return DISCARD_MARKUP; } /* allocate the new plaintext buffer and copy in the macro value */ if((*newPlaintext = DuplicateString(value)) == NULL) { HtpMsg(MSG_ERROR, task->infile, "unable to allocate memory for macro expansion"); FreeMemory(name); return MARKUP_ERROR; } HtpMsg(MSG_INFO, task->infile, "macro \"%s\" dereferenced", name); FreeMemory(name); return NEW_MARKUP; } else if(type == VAR_TYPE_BLOCK_MACRO) { /* !! magic number */ char blockName[128]; TASK newTask; /* if NULL, big-time error */ if(value == NULL) { HtpMsg(MSG_ERROR, task->infile, "block macro \"%s\" incorrectly stored, fatal internal error", name); if(topVarstore == &varstore) { assert(PeekVariableStoreContext(topVarstore) == topVarstore); PopVariableStoreContext(topVarstore); DestroyVariableStore(&varstore); } FreeMemory(name); exit(1); } /* build the block macro name (printed in place of the temporary */ /* filename in all messages regarding it) */ sprintf(blockName, "Block macro \"%s\"", name); /* block macro value is the name of a temporary file containing */ /* macro contents */ if(OpenFile(blockName, value, "r", &incfile) == FALSE) { HtpMsg(MSG_ERROR, task->infile, "unable to open temporary file \"%s\" for block macro \"%s\"", value, name); FreeMemory(name); return MARKUP_ERROR; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -