/** * Detect token Expression * @param state * @param token * @return */ int scpiLex_ProgramExpression(lex_state_t * state, scpi_token_t * token) { token->ptr = state->pos; if (!iseos(state) && ischr(state, '(')) { state->pos++; skipProgramExpression(state); if (!iseos(state) && ischr(state, ')')) { state->pos++; token->len = state->pos - token->ptr; } else { token->len = 0; } } if ((token->len > 0)) { token->type = SCPI_TOKEN_PROGRAM_EXPRESSION; } else { token->type = SCPI_TOKEN_UNKNOWN; state->pos = token->ptr; token->len = 0; } return token->len; }
/** * Detect token Block Data * @param state * @param token * @return */ int scpiLex_ArbitraryBlockProgramData(lex_state_t * state, scpi_token_t * token) { int i; int arbitraryBlockLength = 0; const char * ptr = state->pos; int validData = -1; token->ptr = state->pos; if (skipChr(state, '#')) { if (!iseos(state) && isNonzeroDigit(state->pos[0])) { /* Get number of digits */ i = state->pos[0] - '0'; state->pos++; for (; i > 0; i--) { if (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) { arbitraryBlockLength *= 10; arbitraryBlockLength += (state->pos[0] - '0'); state->pos++; } else { break; } } if (i == 0) { state->pos += arbitraryBlockLength; if ((state->buffer + state->len) >= (state->pos)) { token->ptr = state->pos - arbitraryBlockLength; token->len = arbitraryBlockLength; validData = 1; } else { validData = 0; } } else if (iseos(state)) { validData = 0; } } else if (iseos(state)) { validData = 0; } } if (validData == 1) { /* valid */ token->type = SCPI_TOKEN_ARBITRARY_BLOCK_PROGRAM_DATA; } else if (validData == 0) { /* incomplete */ token->type = SCPI_TOKEN_UNKNOWN; token->len = 0; state->pos = state->buffer + state->len; } else { /* invalid */ token->type = SCPI_TOKEN_UNKNOWN; state->pos = token->ptr; token->len = 0; } return token->len + (token->ptr - ptr); }
/** * Skip program mnemonic [a-z][a-z0-9_]* * @param state * @return */ static int skipProgramMnemonic(lex_state_t * state) { const char * startPos = state->pos; if (!iseos(state) && isalpha(state->pos[0])) { state->pos++; while (!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) { state->pos++; } } if (iseos(state)) { return (state->pos - startPos) * SKIP_INCOMPLETE; } else { return (state->pos - startPos) * SKIP_OK; } }
static void skipQuoteProgramData(lex_state_t * state, char quote) { while (!iseos(state)) { if (isascii7bit(state->pos[0]) && !ischr(state, quote)) { state->pos++; } else if (ischr(state, quote)) { state->pos++; if (!iseos(state) && ischr(state, quote)) { state->pos++; } else { state->pos--; break; } } } }
/** * Detect token nondecimal number * @param state * @param token * @return */ int scpiLex_NondecimalNumericData(lex_state_t * state, scpi_token_t * token) { int someNumbers = 0; token->ptr = state->pos; if (skipChr(state, '#')) { if (!iseos(state)) { if (isH(state->pos[0])) { state->pos++; someNumbers = skipHexNum(state); token->type = SCPI_TOKEN_HEXNUM; } else if (isQ(state->pos[0])) { state->pos++; someNumbers = skipOctNum(state); token->type = SCPI_TOKEN_OCTNUM; } else if (isB(state->pos[0])) { state->pos++; someNumbers = skipBinNum(state); token->type = SCPI_TOKEN_BINNUM; } } } if (someNumbers) { token->ptr += 2; // ignore number prefix token->len = state->pos - token->ptr; } else { token->type = SCPI_TOKEN_UNKNOWN; state->pos = token->ptr; token->len = 0; } return token->len > 0 ? token->len + 2 : 0; }
/** * Skip colon * @param state * @return */ static int skipColon(lex_state_t * state) { if (!iseos(state) && ischr(state, ':')) { state->pos++; return SKIP_OK; } else { return SKIP_NONE; } }
static int skipBinNum(lex_state_t * state) { int someNumbers = 0; while (!iseos(state) && isbdigit(state->pos[0])) { state->pos++; someNumbers++; } return someNumbers; }
/* 7.7.4 <NONDECIMAL NUMERIC PROGRAM DATA> */ static int skipHexNum(lex_state_t * state) { int someNumbers = 0; while (!iseos(state) && isxdigit((uint8_t)(state->pos[0]))) { state->pos++; someNumbers++; } return someNumbers; }
/** * Skip exact character chr or nothing * @param state * @param chr * @return */ static int skipChr(lex_state_t * state, char chr) { if (!iseos(state) && ischr(state, chr)) { state->pos++; return SKIP_OK; } else { return SKIP_NONE; } }
/** * Skip slash or dot * @param state * @return */ static int skipSlashDot(lex_state_t * state) { if (!iseos(state) && (ischr(state, '/') | ischr(state, '.'))) { state->pos++; return SKIP_OK; } else { return SKIP_NONE; } }
/** * Skip any character from 'a'-'Z' * @param state * @return */ static int skipAlpha(lex_state_t * state) { int someLetters = 0; while (!iseos(state) && isalpha(state->pos[0])) { state->pos++; someLetters++; } return someLetters; }
/** * Skip plus or minus * @param state * @return */ static int skipPlusmn(lex_state_t * state) { if (!iseos(state) && isplusmn(state->pos[0])) { state->pos++; return SKIP_OK; } else { return SKIP_NONE; } }
/** * Skip decimal digit * @param state * @return */ static int skipDigit(lex_state_t * state) { if (!iseos(state) && isdigit(state->pos[0])) { state->pos++; return SKIP_OK; } else { return SKIP_NONE; } }
/*------------------------------------------------- main ----- | Function main | | Purpose: Reads input from STDIN using getchar(), iterates over | each character and determines whether the character | denotes the end of a sentence or word. After reaching | EOF, prints output of flesch kincaid algorithm results | and exits. | | | Parameters: argc (IN) -- Doesn't really do anything with this | argv (IN) -- Doesn't really do anything with this. | | | Returns: Only success *-------------------------------------------------------------------*/ int main(int argc, char ** argv) { context = logging_init("prog07"); int word_cnt = 0; int sent_cnt = 0; int syll_cnt = 0; char buffer[MAX_BUFFER]; buffer[0] = '\0'; debug("Does this work"); char c; boolean eos = false; while ((c = getchar()) != EOF) { if (iseos(c)) { eos = true; sent_cnt++; } if (iseow(c) || eos) { // reset the buffer, this is a word if (strlen(buffer) > 0) { int syllcnt = 0; syllcnt = syll_count(buffer); debug("Counted %d syllables for %s\n", syllcnt, buffer); word_cnt++; syll_cnt++; } buffer[0] = '\0'; } else { // Keep reading the buffer concat(buffer, c); } } if (strlen(buffer) > 0) { int syllcnt = 0; syllcnt = syll_count(buffer); debug("Counted %d syllables for %s\n", syllcnt, buffer); word_cnt++; syll_cnt++; } // Never got the last end of sentence character if (!eos) { sent_cnt++; } debug("mspw = %.2f", mspw(syll_cnt, word_cnt)); debug("mwps = %.2f", mwps(word_cnt, sent_cnt)); printf("%d sentences\n", sent_cnt); printf("%d words\n", word_cnt); printf("%d syllables\n", syll_cnt); printf("%.2f is the text’s grade level\n", 0.39 * mwps(word_cnt, sent_cnt) + 11.8 * mspw(syll_cnt, word_cnt) - 15.5); logging_dest(context); return EXIT_SUCCESS; }
/** * Skip all whitespaces * @param state * @return */ static int skipWs(lex_state_t * state) { int someSpace = 0; while (!iseos(state) && isws(state->pos[0])) { state->pos++; someSpace++; } return someSpace; }
/** * Detect token "Character program data" * @param state * @param token * @return */ int scpiLex_CharacterProgramData(lex_state_t * state, scpi_token_t * token) { token->ptr = state->pos; if (!iseos(state) && isalpha(state->pos[0])) { state->pos++; while (!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) { state->pos++; } } token->len = state->pos - token->ptr; if (token->len > 0) { token->type = SCPI_TOKEN_PROGRAM_MNEMONIC; } else { token->type = SCPI_TOKEN_UNKNOWN; } return token->len; }
/** * Detect token String data * @param state * @param token * @return */ int scpiLex_StringProgramData(lex_state_t * state, scpi_token_t * token) { token->ptr = state->pos; if (!iseos(state)) { if (ischr(state, '"')) { state->pos++; token->type = SCPI_TOKEN_DOUBLE_QUOTE_PROGRAM_DATA; skipDoubleQuoteProgramData(state); if (!iseos(state) && ischr(state, '"')) { state->pos++; token->len = state->pos - token->ptr; } else { state->pos = token->ptr; } } else if (ischr(state, '\'')) { state->pos++; token->type = SCPI_TOKEN_SINGLE_QUOTE_PROGRAM_DATA; skipSingleQuoteProgramData(state); if (!iseos(state) && ischr(state, '\'')) { state->pos++; token->len = state->pos - token->ptr; } else { state->pos = token->ptr; } } } token->len = state->pos - token->ptr; if ((token->len > 0)) { //token->ptr++; //token->len -= 2; } else { token->type = SCPI_TOKEN_UNKNOWN; state->pos = token->ptr; token->len = 0; } return token->len > 0 ? token->len : 0; }
static int skipExponent(lex_state_t * state) { int someNumbers = 0; if (!iseos(state) && isE(state->pos[0])) { state->pos++; skipWs(state); skipPlusmn(state); someNumbers = skipNumbers(state); } return someNumbers; }
/** * Skip command program header \*<PROGRAM MNEMONIC> * @param state * @return */ static int skipCommonProgramHeader(lex_state_t * state) { int res; if (skipStar(state)) { res = skipProgramMnemonic(state); if (res == SKIP_NONE && iseos(state)) { return SKIP_INCOMPLETE; } else if (res <= SKIP_INCOMPLETE) { return SKIP_OK; } else if (res >= SKIP_OK) { return SKIP_OK; } else { return SKIP_INCOMPLETE; } } return SKIP_NONE; }
static int createFrameFile(struct frameset *f, FILE * f1, Buffer *current, int level, int force_reload) { int r, c, t_stack; URLFile f2; #ifdef USE_M17N wc_ces charset, doc_charset; #endif char *d_target, *p_target, *s_target, *t_target; ParsedURL *currentURL, base; MySignalHandler(*volatile prevtrap) (SIGNAL_ARG) = NULL; int flag; if (f == NULL) return -1; if (level == 0) { if (SETJMP(AbortLoading) != 0) { TRAP_OFF; return -1; } TRAP_ON; f->name = "_top"; } if (level > 7) { fputs("Too many frameset tasked.\n", f1); return -1; } if (level == 0) { fprintf(f1, "<html><head><title>%s</title></head><body>\n", html_quote(current->buffername)); fputs("<table hborder width=\"100%\">\n", f1); } else fputs("<table hborder>\n", f1); currentURL = f->currentURL ? f->currentURL : ¤t->currentURL; for (r = 0; r < f->row; r++) { fputs("<tr valign=top>\n", f1); for (c = 0; c < f->col; c++) { union frameset_element frame; struct frameset *f_frameset; int i = c + r * f->col; char *p = ""; int status = R_ST_NORMAL; Str tok = Strnew(); int pre_mode = 0; int end_tag = 0; frame = f->frame[i]; if (frame.element == NULL) { fputs("<td>\n</td>\n", f1); continue; } fputs("<td", f1); if (frame.element->name) fprintf(f1, " id=\"_%s\"", html_quote(frame.element->name)); if (!r) fprintf(f1, " width=\"%s\"", f->width[c]); fputs(">\n", f1); flag = 0; if (force_reload) { flag |= RG_NOCACHE; if (frame.element->attr == F_BODY) unloadFrame(frame.body); } switch (frame.element->attr) { default: /* FIXME: gettextize? */ fprintf(f1, "Frameset \"%s\" frame %d: type unrecognized", html_quote(f->name), i + 1); break; case F_UNLOADED: if (!frame.body->name && f->name) { frame.body->name = Sprintf("%s_%d", f->name, i)->ptr; } fflush(f1); f_frameset = frame_download_source(frame.body, currentURL, current->baseURL, flag); if (f_frameset) { deleteFrame(frame.body); f->frame[i].set = frame.set = f_frameset; goto render_frameset; } /* fall through */ case F_BODY: init_stream(&f2, SCM_LOCAL, NULL); if (frame.body->source) { fflush(f1); examineFile(frame.body->source, &f2); } if (f2.stream == NULL) { frame.body->attr = F_UNLOADED; if (frame.body->flags & FB_NO_BUFFER) /* FIXME: gettextize? */ fprintf(f1, "Open %s with other method", html_quote(frame.body->url)); else if (frame.body->url) /* FIXME: gettextize? */ fprintf(f1, "Can't open %s", html_quote(frame.body->url)); else /* FIXME: gettextize? */ fprintf(f1, "This frame (%s) contains no src attribute", frame.body->name ? html_quote(frame.body->name) : "(no name)"); break; } parseURL2(frame.body->url, &base, currentURL); p_target = f->name; s_target = frame.body->name; t_target = "_blank"; d_target = TargetSelf ? s_target : t_target; #ifdef USE_M17N charset = WC_CES_US_ASCII; if (current->document_charset != WC_CES_US_ASCII) doc_charset = current->document_charset; else doc_charset = DocumentCharset; #endif t_stack = 0; if (frame.body->type && !strcasecmp(frame.body->type, "text/plain")) { Str tmp; fprintf(f1, "<pre>\n"); while ((tmp = StrmyUFgets(&f2))->length) { tmp = convertLine(NULL, tmp, HTML_MODE, &charset, doc_charset); fprintf(f1, "%s", html_quote(tmp->ptr)); } fprintf(f1, "</pre>\n"); UFclose(&f2); break; } do { int is_tag = FALSE; char *q; struct parsed_tag *tag; do { if (*p == '\0') { Str tmp = StrmyUFgets(&f2); if (tmp->length == 0) break; tmp = convertLine(NULL, tmp, HTML_MODE, &charset, doc_charset); p = tmp->ptr; } read_token(tok, &p, &status, 1, status != R_ST_NORMAL); } while (status != R_ST_NORMAL); if (tok->length == 0) continue; if (tok->ptr[0] == '<') { if (tok->ptr[1] && REALLY_THE_BEGINNING_OF_A_TAG(tok->ptr)) is_tag = TRUE; else if (!(pre_mode & (RB_PLAIN | RB_INTXTA | RB_SCRIPT | RB_STYLE))) { p = Strnew_m_charp(tok->ptr + 1, p, NULL)->ptr; tok = Strnew_charp("<"); } } if (is_tag) { if (pre_mode & (RB_PLAIN | RB_INTXTA | RB_SCRIPT | RB_STYLE)) { q = tok->ptr; if ((tag = parse_tag(&q, FALSE)) && tag->tagid == end_tag) { if (pre_mode & RB_PLAIN) { fputs("</PRE_PLAIN>", f1); pre_mode = 0; end_tag = 0; goto token_end; } pre_mode = 0; end_tag = 0; goto proc_normal; } if (strncmp(tok->ptr, "<!--", 4) && (q = strchr(tok->ptr + 1, '<'))) { tok = Strnew_charp_n(tok->ptr, q - tok->ptr); p = Strnew_m_charp(q, p, NULL)->ptr; status = R_ST_NORMAL; } is_tag = FALSE; } else if (pre_mode & RB_INSELECT) { q = tok->ptr; if ((tag = parse_tag(&q, FALSE))) { if ((tag->tagid == end_tag) || (tag->tagid == HTML_N_FORM)) { if (tag->tagid == HTML_N_FORM) fputs("</SELECT>", f1); pre_mode = 0; end_tag = 0; goto proc_normal; } if (t_stack) { switch (tag->tagid) { case HTML_TABLE: case HTML_N_TABLE: CASE_TABLE_TAG: fputs("</SELECT>", f1); pre_mode = 0; end_tag = 0; goto proc_normal; } } } } } proc_normal: if (is_tag) { char *q = tok->ptr; int j, a_target = 0; ParsedURL url; if (!(tag = parse_tag(&q, FALSE))) goto token_end; switch (tag->tagid) { case HTML_TITLE: fputs("<!-- title:", f1); goto token_end; case HTML_N_TITLE: fputs("-->", f1); goto token_end; case HTML_BASE: /* "BASE" is prohibit tag */ if (parsedtag_get_value(tag, ATTR_HREF, &q)) { q = url_encode(remove_space(q), NULL, charset); parseURL(q, &base, NULL); } if (parsedtag_get_value(tag, ATTR_TARGET, &q)) { if (!strcasecmp(q, "_self")) d_target = s_target; else if (!strcasecmp(q, "_parent")) d_target = p_target; else d_target = url_quote_conv(q, charset); } Strshrinkfirst(tok, 1); Strshrink(tok, 1); fprintf(f1, "<!-- %s -->", html_quote(tok->ptr)); goto token_end; case HTML_META: if (parsedtag_get_value(tag, ATTR_HTTP_EQUIV, &q) && !strcasecmp(q, "refresh")) { if (parsedtag_get_value(tag, ATTR_CONTENT, &q) ) { Str s_tmp = NULL; int refresh_interval = getMetaRefreshParam(q, &s_tmp); if (s_tmp) { q = html_quote(s_tmp->ptr); fprintf(f1, "Refresh (%d sec) <a href=\"%s\">%s</a>\n", refresh_interval, q, q); } } } #ifdef USE_M17N if (UseContentCharset && parsedtag_get_value(tag, ATTR_HTTP_EQUIV, &q) && !strcasecmp(q, "Content-Type") && parsedtag_get_value(tag, ATTR_CONTENT, &q) && (q = strcasestr(q, "charset")) != NULL) { q += 7; SKIP_BLANKS(q); if (*q == '=') { wc_ces c; q++; SKIP_BLANKS(q); if ((c = wc_guess_charset(q, 0)) != 0) { doc_charset = c; charset = WC_CES_US_ASCII; } } } #endif /* fall thru, "META" is prohibit tag */ case HTML_HEAD: case HTML_N_HEAD: case HTML_BODY: case HTML_N_BODY: case HTML_DOCTYPE: /* prohibit_tags */ Strshrinkfirst(tok, 1); Strshrink(tok, 1); fprintf(f1, "<!-- %s -->", html_quote(tok->ptr)); goto token_end; case HTML_TABLE: t_stack++; break; case HTML_N_TABLE: t_stack--; if (t_stack < 0) { t_stack = 0; Strshrinkfirst(tok, 1); Strshrink(tok, 1); fprintf(f1, "<!-- table stack underflow: %s -->", html_quote(tok->ptr)); goto token_end; } break; CASE_TABLE_TAG: /* table_tags MUST be in table stack */ if (!t_stack) { Strshrinkfirst(tok, 1); Strshrink(tok, 1); fprintf(f1, "<!-- %s -->", html_quote(tok->ptr)); goto token_end; } break; case HTML_SELECT: pre_mode = RB_INSELECT; end_tag = HTML_N_SELECT; break; case HTML_TEXTAREA: pre_mode = RB_INTXTA; end_tag = HTML_N_TEXTAREA; break; case HTML_SCRIPT: pre_mode = RB_SCRIPT; end_tag = HTML_N_SCRIPT; break; case HTML_STYLE: pre_mode = RB_STYLE; end_tag = HTML_N_STYLE; break; case HTML_LISTING: pre_mode = RB_PLAIN; end_tag = HTML_N_LISTING; fputs("<PRE_PLAIN>", f1); goto token_end; case HTML_XMP: pre_mode = RB_PLAIN; end_tag = HTML_N_XMP; fputs("<PRE_PLAIN>", f1); goto token_end; case HTML_PLAINTEXT: pre_mode = RB_PLAIN; end_tag = MAX_HTMLTAG; fputs("<PRE_PLAIN>", f1); goto token_end; default: break; } for (j = 0; j < TagMAP[tag->tagid].max_attribute; j++) { switch (tag->attrid[j]) { case ATTR_SRC: case ATTR_HREF: case ATTR_ACTION: if (!tag->value[j]) break; tag->value[j] = url_encode(remove_space(tag->value[j]), &base, charset); tag->need_reconstruct = TRUE; parseURL2(tag->value[j], &url, &base); if (url.scheme == SCM_UNKNOWN || #ifndef USE_W3MMAILER url.scheme == SCM_MAILTO || #endif url.scheme == SCM_MISSING) break; a_target |= 1; tag->value[j] = parsedURL2Str(&url)->ptr; parsedtag_set_value(tag, ATTR_REFERER, parsedURL2Str(&base)->ptr); #ifdef USE_M17N if (tag->attrid[j] == ATTR_ACTION && charset != WC_CES_US_ASCII) parsedtag_set_value(tag, ATTR_CHARSET, wc_ces_to_charset (charset)); #endif break; case ATTR_TARGET: if (!tag->value[j]) break; a_target |= 2; if (!strcasecmp(tag->value[j], "_self")) { parsedtag_set_value(tag, ATTR_TARGET, s_target); } else if (!strcasecmp(tag->value[j], "_parent")) { parsedtag_set_value(tag, ATTR_TARGET, p_target); } break; case ATTR_NAME: case ATTR_ID: if (!tag->value[j]) break; parsedtag_set_value(tag, ATTR_FRAMENAME, s_target); break; } } if (a_target == 1) { /* there is HREF attribute and no TARGET * attribute */ parsedtag_set_value(tag, ATTR_TARGET, d_target); } if (parsedtag_need_reconstruct(tag)) tok = parsedtag2str(tag); Strfputs(tok, f1); } else { if (pre_mode & RB_PLAIN) fprintf(f1, "%s", html_quote(tok->ptr)); else if (pre_mode & RB_INTXTA) fprintf(f1, "%s", html_quote(html_unquote(tok->ptr))); else Strfputs(tok, f1); } token_end: Strclear(tok); } while (*p != '\0' || !iseos(f2.stream)); if (pre_mode & RB_PLAIN) fputs("</PRE_PLAIN>\n", f1); else if (pre_mode & RB_INTXTA) fputs("</TEXTAREA></FORM>\n", f1); else if (pre_mode & RB_INSELECT) fputs("</SELECT></FORM>\n", f1); else if (pre_mode & (RB_SCRIPT | RB_STYLE)) { if (status != R_ST_NORMAL) fputs(correct_irrtag(status)->ptr, f1); if (pre_mode & RB_SCRIPT) fputs("</SCRIPT>\n", f1); else if (pre_mode & RB_STYLE) fputs("</STYLE>\n", f1); } while (t_stack--) fputs("</TABLE>\n", f1); UFclose(&f2); break; case F_FRAMESET: render_frameset: if (!frame.set->name && f->name) { frame.set->name = Sprintf("%s_%d", f->name, i)->ptr; } createFrameFile(frame.set, f1, current, level + 1, force_reload); break; } fputs("</td>\n", f1); } fputs("</tr>\n", f1); } fputs("</table>\n", f1); if (level == 0) { fputs("</body></html>\n", f1); TRAP_OFF; } return 0; }
/** * Private export of iseos * @param state * @return */ int scpiLex_IsEos(lex_state_t * state) { return iseos(state); }
static void skipProgramExpression(lex_state_t * state) { while (!iseos(state) && isProgramExpression(state->pos[0])) { state->pos++; } }