/* has_metadata - parse document and report whether metadata is present */ gboolean has_metadata(char *text, int extensions) { gboolean hasMeta; element *result; element *references; element *notes; element *labels; GString *formatted_text; formatted_text = preformat_text(text); references = parse_references(formatted_text->str, extensions); notes = parse_notes(formatted_text->str, extensions, references); labels = parse_labels(formatted_text->str, extensions, references, notes); result = parse_metadata_only(formatted_text->str, extensions); hasMeta = FALSE; if (result != NULL) { if (result->children != NULL) { hasMeta = TRUE; free_element_list(result); } else { free_element(result); } } free_element_list(references); free_element_list(labels); return hasMeta; }
/* markdown_to_gstring - convert markdown text to the output format specified. * Returns a GString, which must be freed after use using g_string_free(). */ GString * markdown_to_g_string(char *text, int extensions, int output_format) { element *result; element *references; element *notes; element *labels; GString *formatted_text; GString *out; out = g_string_new(""); formatted_text = preformat_text(text); if (output_format == OPML_FORMAT) { result = parse_markdown_for_opml(formatted_text->str, extensions); } else { references = parse_references(formatted_text->str, extensions); notes = parse_notes(formatted_text->str, extensions, references); labels = parse_labels(formatted_text->str, extensions, references, notes); result = parse_markdown_with_metadata(formatted_text->str, extensions, references, notes, labels); result = process_raw_blocks(result, extensions, references, notes, labels); } g_string_free(formatted_text, TRUE); print_element_list(out, result, output_format, extensions); free_element_list(result); if (output_format != OPML_FORMAT) { free_element_list(references); free_element_list(labels); } return out; }
int interpret_program(tvm_program_t* p, char* filename, tvm_memory_t* pMemory) { int i; FILE* pFile = NULL; int source_length = 0; char* source = NULL; tvm_lexer_t* lexer = NULL; /* Attempt to open the file. If the file cannot be opened, try once more. */ if(filename) for(i = 0; i < 2; i++) if(!pFile) pFile = tvm_fopen(filename, ".vm", "r"); if(!pFile) { printf("File was not found, or does not exist. Unable to interpret.\n"); return 1; } source_length = tvm_flength(pFile); source = malloc(source_length); tvm_fcopy(source, source_length, pFile); lexer = lexer_create(); lex(lexer, source); free(source); fclose(pFile); if(parse_labels(p, (const char***)lexer->tokens) != 0) return 1; for(i = 0; lexer->tokens[i]; i++) { p->instr = (int*)realloc(p->instr, sizeof(int) * (i + 2)); p->instr[i] = 0; p->args = (int***)realloc(p->args, sizeof(int**) * (i + 2)); p->args[i] = (int**)calloc(MAX_ARGS, sizeof(int*)); parse_instruction(p, (const char**)lexer->tokens[i], pMemory); } lexer_destroy(lexer); p->args[i] = NULL; p->instr[i] = -0x1; return 0; }
/* extract_metadata_value - parse document and return value of specified metadata key (e.g. "LateX Mode")/ Returns a null-terminated string, which must be freed after use. */ char * extract_metadata_value(char *text, int extensions, char *key) { char *value; element *result; element *references; element *notes; element *labels; GString *formatted_text; formatted_text = preformat_text(text); references = parse_references(formatted_text->str, extensions); notes = parse_notes(formatted_text->str, extensions, references); labels = parse_labels(formatted_text->str, extensions, references, notes); result = parse_metadata_only(formatted_text->str, extensions); value = metavalue_for_key(key, result); free_element_list(result); free_element_list(references); free_element_list(labels); return value; }
/* markdown_to_gstring - convert markdown text to the output format specified. * Returns a GString, which must be freed after use using g_string_free(). */ GString * markdown_to_g_string(char *text, int extensions, int output_format) { element *result; element *references; element *notes; element *labels; GString *formatted_text; GString *out; out = g_string_new(""); formatted_text = preformat_text(text); references = parse_references(formatted_text->str, extensions); notes = parse_notes(formatted_text->str, extensions, references); labels = parse_labels(formatted_text->str, extensions, references, notes); if (output_format == OPML_FORMAT) { result = parse_markdown_for_opml(formatted_text->str, extensions); } else { result = parse_markdown_with_metadata(formatted_text->str, extensions, references, notes, labels); result = process_raw_blocks(result, extensions, references, notes, labels); } g_string_free(formatted_text, TRUE); if (result == NULL) { /* The parsing was aborted */ g_string_append(out,"MultiMarkdown was unable to parse this file."); } else { print_element_list(out, result, output_format, extensions); } free_element_list(result); free_element_list(references); free_element_list(labels); return out; }
/* ========================================================================= */ struct query *ParseURLString(struct query *q, char *urlstring) { char *qstring = NULL; char *paramlist = NULL; /* Let's start with sanity checks. */ if ( NULL == q ) { if ( NULL == (q = (struct query *)malloc(sizeof(struct query))) ) { error_msg("ERROR: Unable to malloc for query processing."); return(NULL); } } /* Addume no parse errors to start */ q->parse_failure = UP_ERROR_NONE; /* Set other values to NONE - they will be overwritten */ q->tree = TREE_NONE; q->format = FORMAT_NONE; /* Handle the NULL URI case */ /* This means that we only want to insure that the q is allocated and do not care what the actual "query" is. (Likely the user asked for OPTIONS) */ if ( NULL == urlstring ) return(q); /* Handle the empty URL case */ if ( (urlstring[0] == 0) || ((urlstring[0] == '/') && (urlstring[1] == 0)) ) { q->parse_failure = UP_ERROR_NONE; q->tree = TREE_DEFAULT; q->format = FORMAT_DEFAULT; q->label_list[0] = 0; return(q); } /* If not empty, then it must be 4 chars - at a minimum */ /* /root, /xml, /json, /cvs, /perf/json.... */ if ( strlen(urlstring) < 4 ) { q->parse_failure |= UP_ERROR_SHORT; return(q); } /* Is this an absoluteURI or not? */ if ( *urlstring != '/' ) { /* We are not dealing with the "traditional" URL string. This is likely a absoluteURI (see RFC2616) string. */ /* Bite off expected 'chunks' of the URI */ if (( urlstring[0] == 'h' ) && ( urlstring[1] == 't' ) && ( urlstring[2] == 't' ) && ( urlstring[3] == 'p' )) { urlstring += 4; } /* [http]://[server_addr...] */ if ((urlstring[0] == ':' ) && (urlstring[1] == '/' ) && (urlstring[2] == '/' )) { urlstring += 3; } /* Walk up to the "traditional" URI string */ while (( *urlstring != 0 ) && ( *urlstring != '/' )) urlstring++; if ( *urlstring != '/' ) { q->parse_failure |= UP_ERROR_MAL; return(q); } } while ( *urlstring != 0 ) { if ( *urlstring == '/' ) { if ( NULL == qstring ) qstring = urlstring; } if ( *urlstring == '?' ) { *urlstring = 0; urlstring++; /* String should not end with a '?' */ if ( *urlstring == 0 ) { q->parse_failure |= UP_ERROR_MAL; return(q); } if ( NULL == paramlist ) paramlist = urlstring; } urlstring++; } if(parse_query(q, qstring)) return(q); if(parse_labels(q, paramlist)) return(q); return(q); }