/// @param[in] str: User supplied command line /// /// @return 1 The ruturn code indicates a command matched. /// @return 0 if no rules matched MEMSPACE int user_tests(char *str) { int res; int len; char *ptr; long p1, p2; time_t t; ptr = skipspaces(str); if ((len = token(ptr,"help")) ) { ptr += len; user_help(); return(1); } if ((len = token(ptr,"setdate")) ) { ptr += len; ptr = skipspaces(str); setdate_r(ptr); return(1); } if ((len = token(ptr,"time")) ) { t = time(0); DEBUG_PRINTF("TIME:%s\n", ctime(&t)); return(1); } return(0); }
static void rcc_set_callname(const char *s) { free (callname); callname = NULL; nargs = 0; callname = trim (strdup (skipspaces (s))); pushargs = !((!strcmp (s, "goto")) || (!strcmp (s, "break"))); }
static void rcc_element(REgg *egg, char *str) { REggEmit *e = egg->remit; char *p = strrchr (str, ','); int num, num2; if (CTX) { nargs = 0; if (mode == GOTO) mode = NORMAL; // XXX while (p) { *p = '\0'; p = (char *)skipspaces (p+1); rcc_pusharg (egg, p); p = strrchr (str, ','); } if (callname) rcc_pusharg (egg, str); else if (mode == NORMAL) { if (!atoi (str)) { if (dstvar == NULL) /* return string */ dstvar = strdup (".fix0"); rcc_pushstr (egg, str, 1); } } } else { switch (mode) { case ALIAS: e->equ (egg, dstvar, str); R_FREE (dstvar); mode = NORMAL; break; case SYSCALL: syscalls[nsyscalls].name = strdup (dstvar); syscalls[nsyscalls].arg = strdup (str); nsyscalls++; R_FREE (dstvar); break; case GOTO: elem[elem_n] = 0; e->jmp (egg, elem, 0); break; default: p = strchr (str, ','); if (p) { *p='\0'; num2 = atoi (p+1); } else num2 = 0; num = atoi (str) + num2; stackframe = num; stackfixed = num2; if (mode != NAKED) e->frame (egg, stackframe+stackfixed); } elem[0] = 0; elem_n = 0; } }
static int parseinlinechar(REgg *egg, char c) { static int inlinectr = 0; /* skip until '{' */ if (c == '{') { /* XXX: repeated code!! */ rcc_context (egg, 1); inlinectr++; if (inlinectr==1) return 0; } else if (inlinectr == 0) return 0; /* capture body */ if (c == '}') { /* XXX: repeated code!! */ if (CTX < 2) { rcc_context (egg, -1); slurp = 0; mode = NORMAL; inlinectr = 0; if (!dstvar && dstval == syscallbody) { dstval = NULL; return 1; } else /* register */ if (dstval != NULL && dstvar != NULL) { dstval[ndstval]='\0'; //printf(" /* END OF INLINE (%s)(%s) */\n", dstvar, dstval); inlines[ninlines].name = strdup (skipspaces (dstvar)); inlines[ninlines].body = strdup (skipspaces (dstval)); ninlines++; R_FREE (dstvar); R_FREE (dstval); return 1; } eprintf ("Parse error\n"); } } dstval[ndstval++] = c; dstval[ndstval]=0; return 0; }
gettoken(void) { int token; skipspaces(); lexeme[1] = 0; if (token = isvar()) return token; if (token = iscte()) return token; if (token = isaddop()) return token; if (token = ismulop()) return token; return lexeme[0] = token = getc(stdin); }
void AddDisks(VM& vm) { char* pCommandLine = GetCommandLine(); char *p = skipword(pCommandLine); p = skipspaces(p); while (p != null && *p != 0) { char* q = skipword(p); if (q != null && *q != 0) { *q = 0; q++; } if (*p == '"') p++; if (strlen(p) > 0 && p[strlen(p)-1] == '"') p[strlen(p)-1] = 0; if (strlen(p) > 0) { if (!vm.Disks.AddDisk(p)) vm.printf("failed to add disk \"%s\"\n", p); else vm.printf("disk%d \"%s\"\n", vm.Disks.GetCount()-1, p); } if (q != null && *q != 0) p = skipspaces(q); else p = null; } }
int rarvm_assemble (Bitbuf *b, const char *c) { char* arg0 = NULL; char* arg1 = NULL; int opnum; char *p, *str = strdup (skipspaces (c)); p = strchr (str, ' '); if (p) { *p = 0; arg0 = p+1; SKIPSPACES (arg0); arg1 = strchr (arg0, ','); if (arg1) *arg1++ = 0; } opnum = opcode_num (str); if (opnum>=0 && opnum<=7) { bitadd (b, 0, 1); bitadd (b, opnum, 3); } else if (opnum>=8 && opnum<=39) { bitadd (b, 1, 1); bitadd (b, opnum+24, 5); } else { free (str); fprintf (stderr, "Oops. unsupported opcode\n"); return 0; } if (1 && opcodes[opnum].flags & T_BYTE) { bitadd (b, 0, 1); } if (opcodes[opnum].flags & 1) { SKIPSPACES (arg0); if (!assemble_arg (b, arg0)) { free (str); return 0; } if (opcodes[opnum].flags & 2) { SKIPSPACES (arg1); if (!assemble_arg (b, arg1)) return 0; } } free (str); return b->bits; }
void cmd_write (char *cp) { int rc; Line *line; Position beg_write, end_write; out_file = NULL; linecount = 0; /* Get filename */ out_name = cp; cp = uptospace (cp); if (*cp != 0) { *(cp ++) = 0; cp = skipspaces (cp); } /* Get optional range. If not specified, use the whole current buffer. */ if (*cp != 0) { rc = range_multiple (cp, &cp, write_range, out_name); if (rc == 0) eoltest (cp); } else { for (line = buffer_first_line (cur_position.buffer); line != NULL; line = line_next (line)) { rc = write_range (out_name, cur_position.buffer, line); if (rc != 0) break; } } /* Close output file */ if (out_file == NULL) outerr (0, "no file created\n"); else { if (linecount == 0) outerr (strlen (out_name), "no lines written to %s\n", out_name); else outerr (12 + strlen (out_name), "%u line%s written to %s\n", linecount, (linecount == 1) ? "" : "s", out_name); if (fclose (out_file) < 0) outerr (strlen (out_name) + strlen (strerror (errno)), "error closing file %s: %s\n", out_name, strerror (errno)); out_file = NULL; } }
static char *getline(char *buf, int buf_size, FILE *f, int strip_comments) { for (;;) { char *p; int len; if (!fgets(buf, buf_size, f)) return NULL; len = strlen(buf); if (len > 0 && buf[len - 1] == '\n') { buf[len - 1] = '\0'; } p = skipspaces(buf); if (*p == 26) { /* handle obsolete DOS ctrl-Z marker */ return NULL; } if (strip_comments && (*p == '\0' || *p == '#')) continue; return p; } }
/* This function returns a boolean that tells if the string is only a * comment */ static int iscomment(char *string) { const char comments[] = { '#',';', '%', '\0' }; const char *cmtchar = comments; int iscomment = 0; if (string == NULL) return(1); string = skipspaces(string); while(*cmtchar != '\0') { if(*string == *cmtchar) { iscomment = 1; break; } cmtchar++; } return(iscomment); }
/* TODO: split this function into several ones..quite long fun */ static void rcc_next(REgg *egg) { const char *ocn; REggEmit *e = egg->remit; char *str = NULL, *p, *ptr, buf[64]; int i; if (setenviron) { elem[elem_n - 1] = 0; r_sys_setenv (setenviron, elem); R_FREE (setenviron); return; } if (includefile) { char *p, *q, *path; // TODO: add support for directories elem[elem_n-1] = 0; path = find_include (elem, includefile); if (!path) { eprintf ("Cannot find include file '%s'\n", elem); return; } free (includefile); includefile = NULL; rcc_reset_callname (); p = q = r_file_slurp (path, NULL); if (p) { int oline = ++line; elem[0] = 0; // TODO: this must be a separate function elem_n = 0; line = 0; for (; *p; p++) r_egg_lang_parsechar (egg, *p); free (q); line = oline; } else { eprintf ("Cannot find '%s'\n", path); } free (path); return; } docall = 1; if (callname) { if (!strcmp (callname, "goto")) { if (nargs != 1) { eprintf ("Invalid number of arguments for goto()\n"); return; } e->jmp (egg, ctxpush[CTX], 0); rcc_reset_callname (); return; } if (!strcmp (callname, "break")) { e->trap (egg); rcc_reset_callname (); return; } ptr = strchr (callname, '='); if (ptr) { *ptr = '\0'; //ocn = ptr+1; // what is the point of this? } ocn = skipspaces (callname); if (!ocn) return; str = r_egg_mkvar (egg, buf, ocn, 0); if (!str) { eprintf ("Cannot mkvar\n"); return; } if (*ocn=='.') e->call (egg, str, 1); if (!strcmp (str, "while")) { char var[128]; if (lastctxdelta >= 0) exit (eprintf ("ERROR: Unsupported while syntax\n")); sprintf (var, "__begin_%d_%d_%d\n", nfunctions, CTX, nestedi[CTX-1]); e->while_end (egg, var); //get_frame_label (1)); #if 0 eprintf ("------------------------------------------ lastctx: %d\n", lastctxdelta); // TODO: the pushvar is required for the if(){}while(); constructions //char *pushvar = ctxpush[context+nbrackets-1]; /* TODO: support to compare more than one expression (LOGICAL OR) */ rcc_printf (" pop %%eax\n"); rcc_printf (" cmp $0, %%eax\n"); // XXX MUST SUPPORT != 0 COMPARE HERE /* TODO : Simplify!! */ //if (pushvar) // printf(" push %s /* wihle push */\n", pushvar); if (lastctxdelta<0) rcc_printf (" jnz %s\n", get_frame_label (1)); else rcc_printf (" jnz %s\n", get_frame_label (0)); //if (pushvar) // printf(" pop %%"R_AX" /* while pop */\n"); #endif nargs = 0; } else { for (i = 0; i < nsyscalls; i++) { if (!strcmp (str, syscalls[i].name)) { p = syscallbody; e->comment (egg, "set syscall args"); e->syscall_args (egg, nargs); docall = 0; e->comment (egg, "syscall"); r_egg_lang_parsechar (egg, '\n'); /* FIX parsing issue */ if (p) { for (; *p; p++) r_egg_lang_parsechar (egg, *p); } else { char *q, *s = e->syscall (egg, nargs); if (s) { for (q=s; *q; q++) r_egg_lang_parsechar (egg, *q); free (s); } else eprintf ("Cannot get @syscall payload\n"); } docall = 0; break; } } if (docall) for (i = 0; i < ninlines; i++) { if (!strcmp (str, inlines[i].name)) { p = inlines[i].body; docall = 0; e->comment (egg, "inline"); r_egg_lang_parsechar (egg, '\n'); /* FIX parsing issue */ for (; *p; p++) r_egg_lang_parsechar (egg, *p); docall = 0; break; } } if (docall) { e->comment (egg, "call in mode %d", mode); e->call (egg, str, 0); } } if (nargs > 0) e->restore_stack (egg, nargs*e->size); if (ocn) { // Used to call .var0() /* XXX: Probably buggy and wrong */ *buf = 0; free (str); str = r_egg_mkvar (egg, buf, ocn, 0); if (*buf) e->get_result (egg, buf); //else { eprintf("external symbol %s\n", ocn); } } /* store result of call */ if (dstvar) { if (mode != NAKED) { *buf = 0; free (str); str = r_egg_mkvar (egg, buf, dstvar, 0); if (*buf == 0) eprintf ("Cannot resolve variable '%s'\n", dstvar); else e->get_result (egg, buf); } R_FREE (dstvar); } rcc_reset_callname (); } else { int vs = 'l'; char type, *eq, *ptr = elem; elem[elem_n] = '\0'; ptr = (char*)skipspaces (ptr); if (*ptr) { eq = strchr (ptr, '='); if (eq) { char str2[64], *p, ch = *(eq-1); *eq = '\0'; eq = (char*) skipspaces (eq+1); p = r_egg_mkvar (egg, str2, ptr, 0); vs = varsize; if (is_var (eq)) { eq = r_egg_mkvar (egg, buf, eq, 0); if (varxs=='*') e->load (egg, eq, varsize); else /* XXX this is a hack .. must be integrated with pusharg */ if (varxs=='&') e->load_ptr (egg, eq); if (eq) { free (eq); eq = NULL; } type = ' '; } else type = '$'; vs = 'l'; // XXX: add support for != 'l' size e->mathop (egg, ch, vs, type, eq, p); free(p); } else { if (!strcmp (ptr, "break")) { // handle 'break;' e->trap (egg); rcc_reset_callname (); } else { e->mathop (egg, '=', vs, '$', ptr, NULL); } } } } free (str); }
int main (int argc, char *argv[]) { setvbuf(stdout, NULL, _IONBF, 0); // need this to see i/o at all char *buf, *init_name, *output_name, *p; const char *cmdpnt; FILE *init_file; int i, len, rdonly, recover, siz; String *cmdstr; uLong sts; if (argc > 0) pn = argv[0]; fprintf (stderr, "Copyright (C) 2001,2002,2003,2004 Mike Rieker, Beverly, MA USA\n" "Version 2004-06-10, EDT comes with ABSOLUTELY NO WARRANTY\n" "EXPECT it to FAIL when someone's HeALTh or PROpeRTy is at RISk\n\n"); fflush (stderr); /* Parse command line */ init_name = NULL; input_name = NULL; journal_file = NULL; journal_name = NULL; output_name = NULL; rdonly = 0; recover = 0; recover_file = NULL; recover_name = NULL; for (i = 1; i < argc; i ++) { /* -init <file> : process the given initialization file */ if (strcasecmp (argv[i], "-init") == 0) { if (++ i >= argc) goto usage; if (argv[i][0] == '-') goto usage; if (init_name != NULL) goto usage; init_name = argv[i]; continue; } /* -journal <file> : write journal to specified file */ /* by default, it gets written to <output_file>.edtj */ if (strcasecmp (argv[i], "-journal") == 0) { if (++ i >= argc) goto usage; if (argv[i][0] == '-') goto usage; journal_name = argv[i]; continue; } /* -noinit : don't process default init file */ if (strcasecmp (argv[i], "-noinit") == 0) { if (init_name != NULL) goto usage; init_name = ""; continue; } /* -output <file> : write output to specified file */ /* by default, it gets written to <input_file> */ if (strcasecmp (argv[i], "-output") == 0) { if (++ i >= argc) goto usage; if (argv[i][0] == '-') goto usage; output_name = argv[i]; continue; } /* -readonly : don't write an output file */ if (strcasecmp (argv[i], "-readonly") == 0) { if (output_name == input_name) output_name = NULL; rdonly = 1; continue; } /* -recover [<file>] : process recovery from file */ /* by default, recovery is processed from <input_file>.edtj */ if (strcasecmp (argv[i], "-recover") == 0) { recover = 1; if (i + 1 == argc) continue; if (argv[i+1][0] == '-') continue; if ((input_name != NULL) || (i + 2 <= argc)) recover_name = argv[++i]; continue; } /* No more options */ if (argv[i][0] == '-') goto usage; /* first and only parameter <file> : input filename */ if (input_name == NULL) { input_name = argv[i]; if (!rdonly) output_name = input_name; continue; } goto usage; } /* Open recovery file */ if (recover && (input_name == NULL)) { fprintf (stderr, "no input file specified to recover\n"); return (-1); } if (recover && (recover_name == NULL)) recover_name = os_makejnlname (input_name); if (recover_name != NULL) { recover_file = fopen (recover_name, "r"); if (recover_file == NULL) { fprintf (stderr, "error opening recovery file %s: %s\n", recover_name, strerror (errno)); return (-1); } } /* Create journal file */ if ((output_name != NULL) && (journal_name == NULL)) journal_name = os_makejnlname (output_name); if (journal_name != NULL) { journal_file = os_crenewfile (journal_name); if (journal_file == NULL) { fprintf (stderr, "error creating journal file %s: %s\n", journal_name, strerror (errno)); return (-1); } } /* Initialize os dependent routines. No using stdin/stdout/stderr from now on. */ os_initialization (); /* If input file was given, read it into a buffer and mark that buffer for writing on exit */ if (input_name != NULL) { p = input_name; if (rdonly) { p = malloc (strlen (input_name) + 12); strcpy (p, "-readonly "); strcat (p, input_name); } cmd_open (p); if (p != input_name) free (p); cur_position.line = buffer_first_line (cur_position.buffer); cur_position.offset = 0; } /* Otherwise, allocate initial 'MAIN' buffer with no lines in it */ else { cur_position.buffer = buffer_create (4, "MAIN"); cur_position.line = NULL; cur_position.offset = 0; } /* Either way, that is the one used by the EXIT command */ main_buffer = cur_position.buffer; if (output_name != NULL) buffer_setfile (main_buffer, output_name); /* Process initialization file */ if (init_name == NULL) init_name = os_defaultinitname (); /* if no -noinit or -init, get default name */ if ((init_name != NULL) && (init_name[0] != 0)) { init_file = fopen (init_name, "r"); /* try to open init file */ if (init_file != NULL) { siz = 256; /* start with 256 byte buffer */ buf = malloc (siz); len = 0; /* don't have anything in it */ while (fgets (buf + len, siz - len, init_file) != NULL) { /* read onto end of what's there */ len += strlen (buf + len); /* get total length */ if (len == 0) break; /* stop if nothing there (eof?) */ if (buf[len-1] != '\n') { /* check for line terminator */ siz += 256; /* didn't get the whole thing, increase buffer */ buf = realloc (buf, siz); /* ... then loop to read more of the line */ } else { buf[--len] = 0; /* got terminator, remove it from buffer */ cmdpnt = skipspaces (buf); /* skip leading spaces */ if (*cmdpnt != 0) ln_command (cmdpnt); /* process command line */ len = 0; /* empty line buffer for next read */ } } fclose (init_file); free (buf); } else if (errno != ENOENT) { outerr (strlen (init_name) + strlen (strerror (errno)), "error opening init file %s: %s\n", init_name, strerror (errno)); exit (-1); } } /* Type out line at current position to begin with */ cmd_type ("."); /* Read and process commands until eof */ i = 0; while (1) { cmdstr = jnl_readprompt ("\r\n*"); if (cmdstr == NULL) { /* prompt then read command line */ if (++ i < 3) outerr (0, "use either EXIT or QUIT to terminate\n"); else { outerr (12, "%d EOF's in a row or fatal terminal error, exiting ...\n", i); cmd_exit (""); exit (-1); } continue; } i = 0; for (cmdpnt = string_getval (cmdstr); *cmdpnt != 0; cmdpnt ++) if (*cmdpnt > ' ') break; if (*cmdpnt == 0) cmd_type (".+1"); /* blank line means 'type .+1' */ else ln_command (cmdpnt); /* process command */ string_delete (cmdstr); /* free the command string off */ } /* Bad command line parameter */ usage: fprintf (stderr, "usage: %s [-init <init_file>] [-journal <journal_output>] [-noinit] [-output <output_file>] [-readonly] [-recover [<journal_input>]] [<input_file>]\n", pn); return (-1); }
static void rcc_context(REgg *egg, int delta) { REggEmit *emit = egg->remit; char str[64]; if (CTX>31 || CTX <0) return; nestedi[CTX]++; if (callname && CTX>0) {// && delta>0) { // set_nested (callname); //eprintf (" - - - - - - - set nested d=%d c=%d (%s)\n", delta, context-1, callname); //shownested(); } CTX += delta; lastctxdelta = delta; if (CTX == 0 && delta < 0) { if (mode != NAKED) emit->frame_end (egg, stackframe+stackfixed, nbrackets); if (mode == NORMAL) /* XXX : commenting this makes hello.r unhappy! TODO: find a cleaner alternative */ stackframe = 0; mode = NORMAL; } else { /* conditional block */ //eprintf ("Callname is (%s)\n", callname); const char *elm = skipspaces (elem); const char *cn = callname; //if (nested[context-1]) #if 0 if (delta<0 && context>0) { eprintf ("close bracket foo!!!\n"); shownested (); cn = strdup (nested[context-1]); eprintf ("STATEMENT cn=(%s) idx=%d (%s)\n", cn, context-1, nested[context-1]); eprintf ("CNTXXXPUSH (%s)\n", ctxpush[context-1]); #if 0 if (!strcmp (cn, "while")) { emit->while_end (egg, get_frame_label (context-1)); //char *var = get_frame_label (0); //emit->jmp (egg, var, 0); return; } #endif } #endif //eprintf ("ELEM (%s)\n", elm); //eprintf ("END BLOCK %d, (%s)\n", context, nested[context-1]); //eprintf ("CN = (%s) %d (%s) delta=%d\n", cn, context, nested[context-1], delta); if (cn) { //if (callname) { // handle 'foo() {' /* TODO: this must be an array */ char *b, *g, *e, *n; emit->comment (egg, "cond frame %s (%s)", cn, elm); /* TODO: simplify with a single for */ b = strchr (elem, '<'); /* below */ g = strchr (elem, '>'); /* greater */ e = strchr (elem, '='); /* equal */ n = strchr (elem, '!'); /* negate */ if (!strcmp (cn, "while")) { char lab[128]; sprintf (lab, "__begin_%d_%d_%d", nfunctions, CTX-1, nestedi[CTX-1]); emit->get_while_end (egg, str, ctxpush[CTX-1], lab); //get_frame_label (2)); //get_frame_label (2)); //eprintf ("------ (%s)\n", ctxpush[context-1]); // free (endframe); // XXX: endframe is deprecated, must use set_nested only if (delta>0) { set_nested (egg, str); } rcc_set_callname ("if"); // append 'if' body } if (!strcmp (cn, "if")) { //emit->branch (egg, b, g, e, n, varsize, get_end_frame_label (egg)); // HACK HACK :D sprintf (str, "__end_%d_%d_%d", nfunctions, CTX-1, nestedi[CTX-1]); nestede[CTX-1] = strdup (str); sprintf (str, "__end_%d_%d_%d", nfunctions, CTX, nestedi[CTX-1]); emit->branch (egg, b, g, e, n, varsize, str); if (CTX>0) { /* XXX .. */ } rcc_reset_callname (); } //else eprintf ("Unknown statement (%s)(%s)\n", cn, elem); } // handle '{ ..' } }
static void rcc_fun(REgg *egg, const char *str) { char *ptr, *ptr2; REggEmit *e = egg->remit; str = skipspaces (str); if (CTX) { ptr = strchr (str, '='); if (ptr) { *ptr++ = '\0'; free (dstvar); dstvar = strdup (skipspaces (str)); ptr2 = (char *)skipspaces (ptr); if (*ptr2) rcc_set_callname (skipspaces (ptr)); } else { str = skipspaces (str); rcc_set_callname (skipspaces (str)); egg->remit->comment (egg, "rcc_fun %d (%s)", CTX, callname); } } else { ptr = strchr (str, '@'); if (ptr) { *ptr++ = '\0'; mode = NORMAL; if (strstr (ptr, "env")) { //eprintf ("SETENV (%s)\n", str); free (setenviron); setenviron = strdup (skipspaces (str)); slurp = 0; } else if (strstr (ptr, "fastcall")) { /* TODO : not yet implemented */ } else if (strstr (ptr, "syscall")) { if (*str) { mode = SYSCALL; dstvar = strdup (skipspaces (str)); } else { mode = INLINE; free (syscallbody); syscallbody = malloc (4096); // XXX hardcoded size dstval = syscallbody; R_FREE (dstvar); ndstval = 0; *syscallbody = '\0'; } } else if (strstr (ptr, "include")) { free (includefile); includefile = strdup (skipspaces (str)); slurp = 0; } else if (strstr (ptr, "alias")) { mode = ALIAS; dstvar = strdup (skipspaces (str)); } else if (strstr (ptr, "data")) { mode = DATA; ndstval = 0; dstvar = strdup (skipspaces (str)); dstval = malloc (4096); } else if (strstr (ptr, "naked")) { mode = NAKED; free (dstvar); dstvar = strdup (skipspaces (str)); dstval = malloc (4096); ndstval = 0; r_egg_printf (egg, "%s:\n", str); } else if (strstr (ptr, "inline")) { mode = INLINE; free (dstvar); dstvar = strdup (skipspaces (str)); dstval = malloc (4096); ndstval = 0; } else { // naked label if (*ptr) r_egg_printf (egg, "\n.%s %s\n", ptr, str); r_egg_printf (egg, "%s:\n", str); } } else { //e->jmp (egg, ctxpush[context], 0); if (CTX>0) { // WTF? eprintf ("LABEL %d\n", CTX); r_egg_printf (egg, "\n%s:\n", str); } else { if (!strcmp (str, "goto")) { mode = GOTO; } else { // call() // or maybe jmp? e->call (egg, str, 0); } } } } }
R_API char *r_egg_mkvar(REgg *egg, char *out, const char *_str, int delta) { int i, idx, len, qi; char *oldstr = NULL, *str = NULL, foo[32], *q, *ret = NULL; delta += stackfixed; // XXX can be problematic if (!_str) return NULL; /* fix segfault, but not badparsing */ /* XXX memory leak */ ret = str = oldstr = strdup (skipspaces (_str)); //if (num || str[0]=='0') { sprintf(out, "$%d", num); ret = out; } if ( (q = strchr (str, ':')) ) { *q = '\0'; qi = atoi (q+1); varsize = (qi==1)? 'b': 'l'; } else varsize='l'; if (*str == '*' || *str == '&') { varxs = *str; str++; } else varxs = 0; if (str[0] == '.') { REggEmit *e = egg->remit; idx = atoi (str+4) + delta + e->size; if (!strncmp (str+1, "ret", 3)) { strcpy (out, e->retvar); } else if (!strncmp (str+1, "fix", 3)) { e->get_var (egg, 0, out, idx-stackfixed); //sprintf(out, "%d(%%"R_BP")", -(atoi(str+4)+delta+R_SZ-stackfixed)); } else if (!strncmp (str+1, "var", 3)) { e->get_var (egg, 0, out, idx); //sprintf(out, "%d(%%"R_BP")", -(atoi(str+4)+delta+R_SZ)); } else if (!strncmp (str+1, "arg", 3)) { if (str[4]) { if (stackframe == 0) { e->get_var (egg, 1, out, 4); //idx-4); } else { e->get_var (egg, 2, out, idx+4); } } else { /* TODO: return size of syscall */ if (callname) { for (i=0; i<nsyscalls; i++) if (!strcmp (syscalls[i].name, callname)) { free (oldstr); return strdup(syscalls[i].arg); } eprintf ("Unknown arg for syscall '%s'\n", callname); } else eprintf ("NO CALLNAME '%s'\n", callname); } } else if (!strncmp (str+1, "reg", 3)) { // XXX: can overflow if out is small if (attsyntax) snprintf (out, 32, "%%%s", e->regs (egg, atoi (str+4))); else snprintf (out, 32, "%s", e->regs (egg, atoi (str+4))); } else { out = str; /* TODO: show error, invalid var name? */ eprintf ("Something is really wrong\n"); } ret = strdup(out); free (oldstr); } else if (*str=='"' || *str=='\'') { int mustfilter = *str=='"'; /* TODO: check for room in stackfixed area */ str++; len = strlen (str)-1; if (!stackfixed || stackfixed <len) eprintf ("WARNING: No room in the static stackframe! (%d must be %d)\n", stackfixed, len); str[len]='\0'; snprintf (foo, sizeof (foo)-1, ".fix%d", nargs*16); /* XXX FIX DELTA !!!1 */ free(dstvar); dstvar = strdup (skipspaces (foo)); rcc_pushstr (egg, str, mustfilter); ret = r_egg_mkvar (egg, out, foo, 0); free (oldstr); } return ret; }
int main(int argc, char **argv) { char *buf = NULL; size_t bufsize = 0; FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL; utf8proc_uint8_t src[1024]; check(f != NULL, "error opening GraphemeBreakTest.txt"); while (getline(&buf, &bufsize, f) > 0) { size_t bi = 0, si = 0; lineno += 1; if (lineno % 100 == 0) printf("checking line %zd...\n", lineno); if (buf[0] == '#') continue; while (buf[bi]) { bi = skipspaces(buf, bi); if (buf[bi] == '/') { /* grapheme break */ src[si++] = '/'; bi++; } else if (buf[bi] == '+') { /* no break */ bi++; } else if (buf[bi] == '#') { /* start of comments */ break; } else { /* hex-encoded codepoint */ bi += encode((char*) (src + si), buf + bi) - 1; while (src[si]) ++si; /* advance to NUL termination */ } } if (si && src[si-1] == '/') --si; /* no break after final grapheme */ src[si] = 0; /* NUL-terminate */ if (si) { utf8proc_uint8_t utf8[1024]; /* copy src without 0xff grapheme separators */ size_t i = 0, j = 0; utf8proc_ssize_t glen; utf8proc_uint8_t *g; /* utf8proc_map grapheme results */ while (i < si) { if (src[i] != '/') utf8[j++] = src[i++]; else i++; } glen = utf8proc_map(utf8, j, &g, UTF8PROC_CHARBOUND); if (glen == UTF8PROC_ERROR_INVALIDUTF8) { /* the test file contains surrogate codepoints, which are only for UTF-16 */ printf("line %zd: ignoring invalid UTF-8 codepoints\n", lineno); } else { check(glen >= 0, "utf8proc_map error = %s", utf8proc_errmsg(glen)); for (i = 0; i <= glen; ++i) if (g[i] == 0xff) g[i] = '/'; /* easier-to-read output (/ is not in test strings) */ check(!strcmp((char*)g, (char*)src), "grapheme mismatch: \"%s\" instead of \"%s\"", (char*)g, (char*)src); } free(g); } } fclose(f); printf("Passed tests after %zd lines!\n", lineno); return 0; }
void cmd_show (char *cp) { char c, curbf, dirty, *p; const char *fname, *name; int l, rf; Buffer *buffer; uLong lines; if (*cp == 0) goto usage; do { p = uptospace (cp); /* Show buffer info */ if (strncasecmp ("buffers", cp, p - cp) == 0) { outfmt (0, "\nBuffers:\n"); l = 0; for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) { name = buffer_name (buffer); if (strlen (name) > l) l = strlen (name); } for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) { name = buffer_name (buffer); fname = buffer_filename (buffer); lines = buffer_linecount (buffer); rf = (buffer_getreadfile (buffer) != NULL); curbf = (buffer == cur_position.buffer) ? '>' : ' '; dirty = buffer_dirty (buffer, -1) ? '*' : ' '; outfmt (strlen (name) + 16, " %c %c %*.*s: %5u%c line%c", curbf, dirty, l, l, name, lines, rf ? '+' : ' ', (lines == 1) ? ' ' : 's'); if (fname != NULL) outfmt (strlen (fname), " => %s", fname); if (buffer == main_buffer) outstr (" (main buffer)"); outchr ('\n'); } continue; } /* Show info about files */ if (strncasecmp ("files", cp, p - cp) == 0) { outfmt (0, "\nFiles:\n"); outfmt (strlen (help_name), " Help: %s\n", help_name); outfmt (strlen (journal_name), " Journal: %s\n", journal_name[0] == 0 ? "<none>" : journal_name); continue; } /* Show keypad definitions */ if (strncasecmp ("keypad", cp, p - cp) == 0) { show_keypad (); continue; } goto usage; } while (*(cp = skipspaces (p)) != 0); return; usage: outerr (0, "specify BUFFERS, FILES, KEYPAD\n"); }
static DB_playItem_t * load_pls (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname, int *pabort, int (*cb)(DB_playItem_t *it, void *data), void *user_data) { trace ("load_pls %s\n", fname); const char *slash = strrchr (fname, '/'); DB_FILE *fp = deadbeef->fopen (fname); if (!fp) { trace ("failed to open file %s\n", fname); return NULL; } int sz = deadbeef->fgetlength (fp); deadbeef->rewind (fp); uint8_t *buffer = malloc (sz); if (!buffer) { deadbeef->fclose (fp); trace ("failed to allocate %d bytes to read the file %s\n", sz, fname); return NULL; } deadbeef->fread (buffer, 1, sz, fp); deadbeef->fclose (fp); // 1st line must be "[playlist]" const uint8_t *p = buffer; const uint8_t *end = buffer+sz; if (strncasecmp (p, "[playlist]", 10)) { trace ("file %s doesn't begin with [playlist]\n", fname); free (buffer); return NULL; } p += 10; p = skipspaces (p, end); if (p >= end) { trace ("file %s finished before numberofentries had been read\n", fname); free (buffer); return NULL; } // fetch all tracks char uri[1024] = ""; char title[1024] = ""; char length[20] = ""; int lastidx = -1; while (p < end) { p = skipspaces (p, end); if (p >= end) { break; } if (end-p < 6) { break; } const uint8_t *e; int n; if (!strncasecmp (p, "file", 4)) { int idx = atoi (p + 4); if (uri[0] && idx != lastidx && lastidx != -1) { DB_playItem_t *it = pls_insert_file (plt, after, fname, uri, pabort, cb, user_data, title, length); if (it) { after = it; } if (pabort && *pabort) { free (buffer); return after; } uri[0] = 0; title[0] = 0; length[0] = 0; } lastidx = idx; p += 4; while (p < end && *p != '=') { p++; } p++; while (p < end && *p <= 0x20) { p++; } if (p >= end) { break; } e = p; while (e < end && *e >= 0x20) { e++; } n = e-p; n = min (n, sizeof (uri)-1); memcpy (uri, p, n); uri[n] = 0; trace ("uri: %s\n", uri); trace ("uri%d=%s\n", idx, uri); p = ++e; } else if (!strncasecmp (p, "title", 5)) { int idx = atoi (p + 5); if (uri[0] && idx != lastidx && lastidx != -1) { trace ("title%d\n", idx); DB_playItem_t *it = pls_insert_file (plt, after, fname, uri, pabort, cb, user_data, title, length); if (it) { after = it; } if (pabort && *pabort) { free (buffer); return after; } uri[0] = 0; title[0] = 0; length[0] = 0; } lastidx = idx; p += 5; while (p < end && *p != '=') { p++; } p++; while (p < end && *p <= 0x20) { p++; } if (p >= end) { break; } e = p; while (e < end && *e >= 0x20) { e++; } n = e-p; n = min (n, sizeof (title)-1); memcpy (title, p, n); title[n] = 0; trace ("title%d=%s\n", idx, title); p = ++e; } else if (!strncasecmp (p, "length", 6)) { int idx = atoi (p + 6); if (uri[0] && idx != lastidx && lastidx != -1) { trace ("length%d\n", idx); DB_playItem_t *it = pls_insert_file (plt, after, fname, uri, pabort, cb, user_data, title, length); if (it) { after = it; } if (pabort && *pabort) { free (buffer); return after; } uri[0] = 0; title[0] = 0; length[0] = 0; } lastidx = idx; p += 6; // skip = while (p < end && *p != '=') { p++; } p++; if (p >= end) { break; } e = p; while (e < end && *e >= 0x20) { e++; } n = e-p; n = min (n, sizeof (length)-1); memcpy (length, p, n); trace ("length%d=%s\n", idx, length); } else { trace ("pls: skipping unrecognized entry in pls file: %s\n", p); e = p; while (e < end && *e >= 0x20) { e++; } } while (e < end && *e < 0x20) { e++; } p = e; } if (uri[0]) { DB_playItem_t *it = pls_insert_file (plt, after, fname, uri, pabort, cb, user_data, title, length); if (it) { after = it; } } free (buffer); return after; }
void cmd_exit (char *cp) { const char *bn, *fn, *ynanswer; Buffer *buffer; int prompt, saveflag, ynflag; String *ynstring; /* Check for -save qualifier - it inhibits deleting the journal file */ saveflag = 0; if ((strncasecmp (cp, "-save", 5) == 0) && (cp[5] <= ' ')) { if ((journal_name == NULL) || (journal_name[0] == 0)) { outerr (0, "there is no journal file to save\n"); return; } saveflag = 1; cp = skipspaces (cp + 5); } /* Maybe use a different name for main buffer */ if (*cp != 0) buffer_setfile (main_buffer, cp); /* Don't bother if recovery mode, we want them to be able to do more. They can re-type exit command */ if (recover_file != NULL) { outerr (0, "EXIT command ignored in recovery mode\n"); return; } /* Check for buffers that have filename of "" (opened -readonly) but were modified */ prompt = 0; for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) { fn = buffer_filename (buffer); if (fn == NULL) continue; if (fn[0] != 0) continue; if (buffer_dirty (buffer, -1)) { bn = buffer_name (buffer); if (!prompt) outerr (0, "\n"); outerr (strlen (bn), "buffer %s was modified but has no output file\n", bn); prompt = 1; } } if (prompt) { do { ynstring = jnl_readprompt ("\r\n do you still want to exit (yes or no)? "); if (ynstring == NULL) return; ynflag = -1; ynanswer = string_getval (ynstring); if (strcasecmp (ynanswer, "no") == 0) ynflag = 0; if (strcasecmp (ynanswer, "yes") == 0) ynflag = 1; string_delete (ynstring); } while (ynflag < 0); if (!ynflag) return; } /* Write the entire contents of all the buffers that have files and that have been modified to their respective files */ for (buffer = NULL; (buffer = buffer_next (buffer)) != NULL;) { fn = buffer_filename (buffer); /* get output filename */ if (fn == NULL) continue; /* if none, don't try to write */ bn = buffer_name (buffer); /* ok, write it out */ if (!buffer_dirty (buffer, -1)) { outerr (strlen (fn) + strlen (bn), "not writing %s from =%s because it is unmodified\n", fn, bn); continue; } outerr (strlen (fn) + strlen (bn), "writing %s from =%s: ", fn, bn); if (!write_file (fn, buffer_first_line (buffer), buffer_last_line (buffer))) { outerr (strlen (fn), "output file %s not written, not exiting\n", fn); /* if error, don't exit */ return; } buffer_dirty (buffer, 0); } /* Write successful, maybe delete journal file and terminate process */ jnl_close (!saveflag); output (); exit (0); }
static DB_playItem_t * load_m3u (ddb_playlist_t *plt, DB_playItem_t *after, const char *fname, int *pabort, int (*cb)(DB_playItem_t *it, void *data), void *user_data) { const char *slash = strrchr (fname, '/'); trace ("enter pl_insert_m3u\n"); // skip all empty lines and comments DB_FILE *fp = deadbeef->fopen (fname); if (!fp) { trace ("failed to open file %s\n", fname); return NULL; } int sz = deadbeef->fgetlength (fp); trace ("loading m3u...\n"); uint8_t *membuffer = malloc (sz); if (!membuffer) { deadbeef->fclose (fp); trace ("failed to allocate %d bytes to read the file %s\n", sz, fname); return NULL; } uint8_t *buffer = membuffer; deadbeef->fread (buffer, 1, sz, fp); deadbeef->fclose (fp); if (sz >= 3 && buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) { buffer += 3; sz -= 3; } int line = 0; int read_extm3u = 0; const uint8_t *p = buffer; const uint8_t *end = buffer+sz; const uint8_t *e; int length = -1; char title[1000] = ""; char artist[1000] = ""; while (p < end) { line++; p = skipspaces (p, end); if (p >= end) { break; } if (*p == '#') { if (line == 1) { if (end - p >= 7 && !strncmp (p, "#EXTM3U", 7)) { read_extm3u = 1; } } else if (read_extm3u) { if (end - p >= 8 && !strncmp (p, "#EXTINF:", 8)) { length = -1; memset (title, 0, sizeof (title)); memset (artist, 0, sizeof (artist)); p += 8; e = p; while (e < end && *e >= 0x20) { e++; } int n = e-p; uint8_t nm[n+1]; memcpy (nm, p, n); nm[n] = 0; length = atoi (nm); char *c = nm; while (*c && *c != ',') { c++; } if (*c == ',') { c++; const char *dash = NULL; const char *newdash = strstr (c, " - "); while (newdash) { dash = newdash; newdash = strstr (newdash+3, " - "); } if (dash) { strncpy (title, dash+3, sizeof (title)-1); title[sizeof(title)-1] = 0; int l = dash - c; strncpy (artist, c, min(l, sizeof (artist))); artist[sizeof(artist)-1] = 0; } else { strncpy (title, c, sizeof (title)-1); title[sizeof(title)-1] = 0; } trace ("title: %s, artist: %s\n", title, artist); } } } while (p < end && *p >= 0x20) { p++; } if (p >= end) { break; } continue; } e = p; while (e < end && *e >= 0x20) { e++; } int n = e-p; uint8_t nm[n+1]; memcpy (nm, p, n); nm[n] = 0; if (title[0]) { const char *cs = deadbeef->junk_detect_charset (title); if (cs) { char tmp[1000]; if (deadbeef->junk_iconv (title, strlen (title), tmp, sizeof (tmp), cs, "utf-8") >= 0) { strcpy (title, tmp); } } } if (artist[0]) { const char *cs = deadbeef->junk_detect_charset (artist); if (cs) { char tmp[1000]; if (deadbeef->junk_iconv (artist, strlen (artist), tmp, sizeof (tmp), cs, "utf-8") >= 0) { strcpy (artist, tmp); } } } DB_playItem_t *it = NULL; int is_fullpath = 0; if (nm[0] == '/') { is_fullpath = 1; } else { uint8_t *p = strstr (nm, "://"); if (p) { p--; while (p >= nm) { if (*p < 'a' && *p > 'z') { break; } p--; } if (p < nm) { is_fullpath = 1; } } } if (is_fullpath) { // full path trace ("pl_insert_m3u: adding file %s\n", nm); it = deadbeef->plt_insert_file2 (0, plt, after, nm, pabort, cb, user_data); if (it) { if (length >= 0 && deadbeef->pl_get_item_duration (it) < 0) { deadbeef->plt_set_item_duration (plt, it, length); } if (title[0]) { deadbeef->pl_add_meta (it, "title", title); } if (artist[0]) { deadbeef->pl_add_meta (it, "artist", artist); } } // reset title/artist, to avoid them from being reused in the next track memset (title, 0, sizeof (title)); memset (artist, 0, sizeof (artist)); } else { int l = strlen (nm); char fullpath[slash - fname + l + 2]; memcpy (fullpath, fname, slash - fname + 1); strcpy (fullpath + (slash - fname + 1), nm); trace ("pl_insert_m3u: adding file %s\n", fullpath); it = deadbeef->plt_insert_file2 (0, plt, after, fullpath, pabort, cb, user_data); } if (it) { after = it; } if (pabort && *pabort) { free (membuffer); return after; } p = e; if (p >= end) { break; } } trace ("leave pl_insert_m3u\n"); free (membuffer); return after; }
ConditionalReader::ConditionalReader(PClip _child, const char* filename, const char _varname[], bool _show, IScriptEnvironment* env) : GenericVideoFilter(_child), show(_show), variableName(_varname), mode(MODE_UNKNOWN), offset(0), stringcache(0) { FILE * f; char *line = 0; int lines; if ((f = fopen(filename, "rb")) == NULL) env->ThrowError("ConditionalReader: Could not open file '%s'.", filename); lines = 0; try { while ((line = readline(f)) != NULL) { char *ptr; int fields; lines++; /* We skip spaces */ ptr = skipspaces(line); /* Skip coment lines or empty lines */ if(iscomment(ptr) || *ptr == '\0') { free(line); line = 0; continue; } if (mode == MODE_UNKNOWN) { // We have not recieved a mode - We expect type. char* keyword = ptr; ptr = findspace(ptr); if (*ptr) { *ptr++ = '\0'; if (!lstrcmpi(keyword, "type")) { /* We skip spaces */ char* type = skipspaces(ptr); ptr = findspace(type); *ptr = '\0'; if (!lstrcmpi(type, "int")) { mode = MODE_INT; intVal = new int[vi.num_frames]; } else if (!lstrcmpi(type, "float")) { mode = MODE_FLOAT; floatVal = new float[vi.num_frames]; } else if (!lstrcmpi(type, "bool")) { mode = MODE_BOOL; boolVal = new bool[vi.num_frames]; } else if (!lstrcmpi(type, "string")) { mode = MODE_STRING; stringVal = new const char*[vi.num_frames]; } else { ThrowLine("ConditionalReader: Unknown 'Type' specified in line %d", lines, env); }// end if compare type SetRange(0, vi.num_frames-1, AVSValue()); }// end if compare keyword }// end if fields } else { // We have a defined mode and allocated the values. char* keyword = ptr; char* type = findspace(keyword); if (*type) *type++ = '\0'; if (!lstrcmpi(keyword, "default")) { AVSValue def = ConvertType(type, lines, env); SetRange(0, vi.num_frames-1, def); } else if (!lstrcmpi(keyword, "offset")) { fields = sscanf(type, "%d", &offset); if (fields != 1) ThrowLine("ConditionalReader: Could not read Offset in line %d", lines, env); } else if (keyword[0] == 'R' || keyword[0] == 'r') { // Range int start; int stop; type = skipspaces(type); fields = sscanf(type, "%d", &start); type = findspace(type); type = skipspaces(type); fields += sscanf(type, "%d", &stop); type = findspace(type); if (!*type || fields != 2) ThrowLine("ConditionalReader: Could not read Range in line %d", lines, env); if (start > stop) ThrowLine("ConditionalReader: The Range start frame is after the end frame in line %d", lines, env); AVSValue set = ConvertType(type+1, lines, env); SetRange(start, stop, set); } else if (keyword[0] == 'I' || keyword[0] == 'i') { // Interpolate if (mode == MODE_BOOL) ThrowLine("ConditionalReader: Cannot Interpolate booleans in line %d", lines, env); if (mode == MODE_STRING) ThrowLine("ConditionalReader: Cannot Interpolate strings in line %d", lines, env); type = skipspaces(type); int start; int stop; char start_value[64]; char stop_value[64]; fields = sscanf(type, "%d %d %63s %63s", &start, &stop, start_value, stop_value); if (fields != 4) ThrowLine("ConditionalReader: Could not read Interpolation range in line %d", lines, env); if (start > stop) ThrowLine("ConditionalReader: The Interpolation start frame is after the end frame in line %d", lines, env); start_value[63] = '\0'; AVSValue set_start = ConvertType(start_value, lines, env); stop_value[63] = '\0'; AVSValue set_stop = ConvertType(stop_value, lines, env); const int range = stop-start; const double diff = (set_stop.AsFloat() - set_start.AsFloat()) / range; for (int i = 0; i<=range; i++) { const double n = i * diff + set_start.AsFloat(); SetFrame(i+start, (mode == MODE_FLOAT) ? AVSValue(n) : AVSValue((int)(n+0.5))); } } else { int cframe; fields = sscanf(keyword, "%d", &cframe); if (*type && fields == 1) { AVSValue set = ConvertType(type, lines, env); SetFrame(cframe, set); } else { ThrowLine("ConditionalReader: Do not understand line %d", lines, env); } } } // End we have defined type free(line); line = 0; }// end while still some file left to read. } catch (...) { if (line) free(line); fclose(f); CleanUp(); throw; } /* We are done with the file */ fclose(f); if (mode == MODE_UNKNOWN) env->ThrowError("ConditionalReader: Type was not defined!"); }
ConditionalReader::ConditionalReader(PClip _child, const char* filename, const char _varname[], bool _show, IScriptEnvironment* env) : GenericVideoFilter(_child), show(_show), variableName(_varname) { FILE * f; char *line; int lines; if ((f = fopen(filename, "rb")) == NULL) env->ThrowError("ConditionalReader: Could not open file '%s'.", filename); lines = 0; mode = MODE_UNKNOWN; while ((line = readline(f)) != NULL) { char *ptr; int fields; lines++; /* We skip spaces */ ptr = skipspaces(line); /* Skip coment lines or empty lines */ if(iscomment(ptr) || *ptr == '\0') { free(line); continue; } if (mode == MODE_UNKNOWN) { // We have not recieved a mode - We expect type. char keyword [1024]; char type [1024]; fields = sscanf(ptr,"%1023s %1023s", keyword, type); if (fields) { if (!strcasecmp((const char*)keyword, "type")) { if (!strcasecmp((const char*)type, "int")) { mode = MODE_INT; intVal = new int[vi.num_frames]; } else if (!strcasecmp((const char*)type, "float")) { mode = MODE_FLOAT; floatVal = new float[vi.num_frames]; } else if (!strcasecmp((const char*)type, "bool")) { mode = MODE_BOOL; boolVal = new bool[vi.num_frames]; } else { ThrowLine("ConditionalReader: Unknown 'type' specified in line %d", lines, env); }// end if compare type }// end if compare keyword }// end if fields } else { // We have a defined mode and allocated the values. char keyword [1024]; char type [1024]; fields = sscanf(ptr,"%1023s %1023s", keyword, type); if (!strcasecmp((const char*)keyword, "default")) { AVSValue def = ConvertType((const char*)type, lines, env); SetRange(0, vi.num_frames-1, def); free(line); continue; } // end if "default" if (ptr[0] == 'R' || ptr[0] == 'r') { // Range ptr++; ptr = skipspaces(ptr); int start; int stop; char value [64]; fields = sscanf(ptr, "%d %d %63s", &start, &stop, value); if (fields != 3) ThrowLine("ConditionalReader: Could not read range in line %d", lines, env); if (start > stop) ThrowLine("ConditionalReader: The start frame is after the end frame in line %d", lines, env); AVSValue set = ConvertType((const char*)value, lines, env); SetRange(start, stop, set); } else if (ptr[0] == 'I' || ptr[0] == 'i') { // Interpolate if (mode == MODE_BOOL) ThrowLine("ConditionalReader: Cannot interpolate booleans in line %d", lines, env); ptr++; ptr = skipspaces(ptr); int start; int stop; char start_value [64]; char stop_value [64]; fields = sscanf(ptr, "%d %d %63s %63s", &start, &stop, start_value, stop_value); if (fields != 4) ThrowLine("ConditionalReader: Could not read interpolation range in line %d", lines, env); if (start > stop) ThrowLine("ConditionalReader: The start frame is after the end frame in line %d", lines, env); AVSValue set_start = ConvertType((const char*)start_value, lines, env); AVSValue set_stop = ConvertType((const char*)stop_value, lines, env); int range = stop-start; double diff = set_stop.AsFloat() - set_start.AsFloat(); for (int i = 0; i<=range; i++) { double where = (double)(i)/(double)range; double n = where * diff + set_start.AsFloat(); SetFrame(i+start, (mode == MODE_FLOAT) ? AVSValue(n) : AVSValue((int) n)); } } else { char value [64]; int cframe; fields = sscanf(ptr, "%d %63s", &cframe, value); if (fields == 2) { AVSValue set = ConvertType((const char*)value, lines, env); SetFrame(cframe, set); } else { AVXLOG_INFO("ConditionalReader: Ignored line %d.\n", lines); } } } // End we have defined type free(line); }// end while still some file left to read. /* We are done with the file */ fclose(f); if (mode == MODE_UNKNOWN) env->ThrowError("ConditionalReader: Mode was not defined!"); }
void cmd_set (char *cp) { Buffer *buffer; char c, *p; int i, v; /* Set autoshift count */ if (i = matchkeyword (cp, "autoshift", 1)) { if (cp[i] > ' ') goto usage; cp = skipspaces (cp + i); v = strtol (cp, &p, 10); if ((p == cp) || !eoltest (p)) goto usage; autoshift = v; return; } /* Set lfs to hide or show */ if (i = matchkeyword (cp, "lfs", 1)) { if (cp[i] > ' ') goto usage; cp = skipspaces (cp + i); if (i = matchkeyword (cp, "hide", 1)) showlfs = 0; else if (i = matchkeyword (cp, "show", 1)) showlfs = 1; else goto usage; if (!eoltest (cp + i)) goto usage; return; } /* Set numbers to hide or show */ if (i = matchkeyword (cp, "numbers", 1)) { if (cp[i] > ' ') goto usage; cp = skipspaces (cp + i); if (i = matchkeyword (cp, "auto", 1)) shownums = -1; else if (i = matchkeyword (cp, "hide", 1)) shownums = 0; else if (i = matchkeyword (cp, "show", 1)) shownums = 1; else goto usage; if (!eoltest (cp + i)) goto usage; return; } /* Set search to exact or generic */ if (i = matchkeyword (cp, "search", 1)) { if (cp[i] > ' ') goto usage; cp = skipspaces (cp + i); if (i = matchkeyword (cp, "exact", 1)) { xstrstr = strstr; xstrncmp = strncmp; } else if (i = matchkeyword (cp, "generic", 1)) { xstrstr = strcasestr; xstrncmp = strncasecmp; } else goto usage; if (!eoltest (cp + i)) goto usage; return; } /* Set buffer attributes (-output filename or -readonly) */ if (*cp == '=') { for (p = ++ cp; (c = *cp) != 0; cp ++) if (strchr (bufnamechars, c) == NULL) break; if (cp == p) goto usage; if (*cp > ' ') goto usage; buffer = buffer_create (cp - p, p); cp = skipspaces (cp); if ((cp[7] <= ' ') && (strncasecmp (cp, "-output", 7) == 0)) { cp = skipspaces (cp + 7); p = uptospace (cp); if (*p != 0) goto usage; buffer_setfile (buffer, cp); return; } if ((cp[9] <= ' ') && (strncasecmp (cp, "-readonly", 9) == 0)) { cp = skipspaces (cp + 9); if (*cp != 0) goto usage; buffer_setfile (buffer, NULL); return; } goto usage; } usage: outerr (0, "set autoshift <count>\n\n"); outerr (0, "set lfs {hide | show}\n\n"); outerr (0, "set numbers {auto | hide | show}\n\n"); outerr (0, "set search {exact | generic}\n\n"); outerr (0, "set =<buffer> -output <filename>\n"); outerr (0, " -readonly\n\n"); }
static int cmd_parseopt(int argc, const char **argv, const char *prefix) { static int keep_dashdash = 0, stop_at_non_option = 0; static char const * const parseopt_usage[] = { N_("git rev-parse --parseopt [options] -- [<args>...]"), NULL }; static struct option parseopt_opts[] = { OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, N_("keep the `--` passed as an arg")), OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option, N_("stop parsing after the " "first non-option argument")), OPT_END(), }; struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; const char **usage = NULL; struct option *opts = NULL; int onb = 0, osz = 0, unb = 0, usz = 0; strbuf_addstr(&parsed, "set --"); argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, PARSE_OPT_KEEP_DASHDASH); if (argc < 1 || strcmp(argv[0], "--")) usage_with_options(parseopt_usage, parseopt_opts); /* get the usage up to the first line with a -- on it */ for (;;) { if (strbuf_getline(&sb, stdin, '\n') == EOF) die("premature end of input"); ALLOC_GROW(usage, unb + 1, usz); if (!strcmp("--", sb.buf)) { if (unb < 1) die("no usage string given before the `--' separator"); usage[unb] = NULL; break; } usage[unb++] = strbuf_detach(&sb, NULL); } /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */ while (strbuf_getline(&sb, stdin, '\n') != EOF) { const char *s; struct option *o; if (!sb.len) continue; ALLOC_GROW(opts, onb + 1, osz); memset(opts + onb, 0, sizeof(opts[onb])); o = &opts[onb++]; s = strchr(sb.buf, ' '); if (!s || *sb.buf == ' ') { o->type = OPTION_GROUP; o->help = xstrdup(skipspaces(sb.buf)); continue; } o->type = OPTION_CALLBACK; o->help = xstrdup(skipspaces(s)); o->value = &parsed; o->flags = PARSE_OPT_NOARG; o->callback = &parseopt_dump; while (s > sb.buf && strchr("*=?!", s[-1])) { switch (*--s) { case '=': o->flags &= ~PARSE_OPT_NOARG; break; case '?': o->flags &= ~PARSE_OPT_NOARG; o->flags |= PARSE_OPT_OPTARG; break; case '!': o->flags |= PARSE_OPT_NONEG; break; case '*': o->flags |= PARSE_OPT_HIDDEN; break; } } if (s - sb.buf == 1) /* short option only */ o->short_name = *sb.buf; else if (sb.buf[1] != ',') /* long option only */ o->long_name = xmemdupz(sb.buf, s - sb.buf); else { o->short_name = *sb.buf; o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); } } strbuf_release(&sb); /* put an OPT_END() */ ALLOC_GROW(opts, onb + 1, osz); memset(opts + onb, 0, sizeof(opts[onb])); argc = parse_options(argc, argv, prefix, opts, usage, (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) | (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) | PARSE_OPT_SHELL_EVAL); strbuf_addf(&parsed, " --"); sq_quote_argv(&parsed, argv, 0); puts(parsed.buf); return 0; }
int fscanf(FILE* file,const char* format, ...) { int res; void* arg; int i; int c; int contflag; int longflag; int sign; long long number; long double rnumber; char* str; res=0; arg=&format; arg+=sizeof(const char*); while (*format!='\0') { if (*format!='%') { c=fgetc(file); if (c!=*format) { fungetc(c,file); return -1; } format++; continue; } contflag=1; longflag=0; while (*format && contflag) { switch(*format) { case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': format++; continue; break; case 'l': if (longflag==0) longflag=1; else longflag=2; format++; break; case 'L': longflag=2; format++; break; case 'f': case 'd': case 'c': case 's': case '%': contflag=0; break; default: contflag=0; } } if (contflag) break; switch(*format) { case '%': c=fgetc(file); if (c!='%') { ungetc(c,file); return -1; } res--; break; case 'd': number=0; sign=1; skipspaces(file); c=fgetc(file); if (c=='-') { sign=-1; }else if (c!='+') ungetc(c,file); contflag=0; while(1) { c=fgetc(file); if (c>='0' && c<='9') { contflag++; number=number*10+(c-'0'); }else break; } ungetc(c,file); if (!contflag) return res; if (longflag<=1) { *((int*)arg)=number; arg+=sizeof(int); }else { *((long long*)arg)=number; arg+=sizeof(long long); } break; case 'c': c=fgetc(file); if (c==EOF) return res; *((char*)arg)=c; arg+=sizeof(char); break; case 's': skipspaces(file); contflag=0; str=*((char**)arg); arg+=sizeof(char*); while(1) { c=fgetc(file); if (c==EOF || c==' ' || c=='\n' || c=='\r') { ungetc(c,file); break; } *str=c; str++; contflag++; } if (!contflag) return res; break; case 'f': skipspaces(file); // TODO: read real numbers rnumber=0; switch (longflag) { case 0: *((float*)arg)=rnumber; arg+=sizeof(float); break; case 1: *((double*)arg)=rnumber; arg+=sizeof(double); break; case 2: *((long double*)arg)=rnumber; arg+=sizeof(long double); break; default: return res; } break; default: break; } format++; res++; } return res; }
/* rtoken - read the next CsToken */ static int rtoken(CsCompiler *c) { int ch,ch2; /* check the next character */ for (;;) switch (ch = skipspaces(c)) { case EOF: return T_EOF; case '\"': return getstring(c); case '`': return getstring(c, '`'); case '\'': return getcharacter(c); case '<': switch (ch = getch(c)) { case '=': return T_LE; case '<': if ((ch = getch(c)) == '=') return T_SHLEQ; else if( ch == '<' ) { if ((ch = getch(c)) == '=') return T_USHLEQ; c->savedChar = ch; return T_USHL; } c->savedChar = ch; return T_SHL; default: c->savedChar = ch; return '<'; } case '=': if ((ch = getch(c)) == '=') { if ((ch = getch(c)) == '=') { return T_EQ_STRONG; } c->savedChar = ch; return T_EQ; } c->savedChar = ch; return '='; case '!': if ((ch = getch(c)) == '=') { if ((ch = getch(c)) == '=') { return T_NE_STRONG; } c->savedChar = ch; return T_NE; } c->savedChar = ch; return '!'; case '>': switch (ch = getch(c)) { case '=': return T_GE; case '>': if ((ch = getch(c)) == '=') return T_SHREQ; else if( ch == '>' ) { if ((ch = getch(c)) == '=') return T_USHREQ; c->savedChar = ch; return T_USHR; } c->savedChar = ch; return T_SHR; default: c->savedChar = ch; return '>'; } case '&': switch (ch = getch(c)) { case '&': return T_AND; case '=': return T_ANDEQ; default: c->savedChar = ch; return '&'; } case '%': switch (ch = getch(c)) { case '>': return getoutputstring(c); case '=': return T_REMEQ; case '~': return T_RCDR; default: c->savedChar = ch; return '%'; } case '|': switch (ch = getch(c)) { case '|': return T_OR; case '=': return T_OREQ; default: c->savedChar = ch; return '|'; } case '^': if ((ch = getch(c)) == '=') return T_XOREQ; c->savedChar = ch; return '^'; case '+': switch (ch = getch(c)) { case '+': return T_INC; case '=': return T_ADDEQ; default: c->savedChar = ch; return '+'; } case '-': switch (ch = getch(c)) { case '-': return T_DEC; case '=': return T_SUBEQ; default: c->savedChar = ch; return '-'; } case '~': switch (ch = getch(c)) { case '/': return T_CAR; case '%': return T_CDR; default: c->savedChar = ch; return '~'; } case '*': if ((ch = getch(c)) == '=') return T_MULEQ; c->savedChar = ch; return '*'; case '/': switch (ch = getch(c)) { case '=': return T_DIVEQ; case '/': while ((ch = getch(c)) != EOF) if (ch == '\n') break; break; case '*': ch = ch2 = EOF; for (; (ch2 = getch(c)) != EOF; ch = ch2) if (ch == '*' && ch2 == '/') break; break; case '~': return T_RCAR; default: c->savedChar = ch; return '/'; } break; case '.': if ((ch = getch(c)) != EOF && is_digit(ch)) { c->savedChar = ch; return getnumber(c,'.'); } else if (ch == '.') { c->t_token[0] = '.'; c->t_token[1] = '.'; c->t_token[2] = '\0'; return T_DOTDOT; } else { c->savedChar = ch; c->t_token[0] = '.'; c->t_token[1] = '\0'; return '.'; } break; case 1: return T_DOTDOT; case '0': switch (ch = getch(c)) { case 'x': case 'X': return getradixnumber(c,16); case 'b': case 'B': return getradixnumber(c,2); default: c->savedChar = ch; if (ch >= '0' && ch <= '7') return getradixnumber(c,8); else return getnumber(c,'0'); } break; case '#': return getsymbol(c); default: if (is_digit(ch)) return getnumber(c,ch); else if (isidchar(ch)) return getid(c,ch); else { c->t_token[0] = ch; c->t_token[1] = '\0'; return ch; } } }
static void handle_cp(FILE *f0, const char *name, const char *fname) { char line[1024]; char *p, *q; int table[256]; int min_code, max_code, c1, c2, i, nb, j; char name_id[256]; char iso_name[256]; char alias_list[256]; char includename[256]; int has_iso_name, has_alias_list; int eol_char = 10; int base; FILE *f = f0; const char *filename = fname; /* name_id is name with - changed into _ */ pstrcpy(name_id, sizeof(name_id), name); for (p = name_id; *p != '\0'; p++) { if (*p == '-') *p = '_'; } pstrcpy(iso_name, sizeof(iso_name), name); pstrcpy(alias_list, sizeof(alias_list), ""); has_iso_name = has_alias_list = 0; for (i = 0; i < 256; i++) { table[i] = i; } nb = 0; for (;;) { if (!(p = getline(line, sizeof(line), f, 0)) || *p == '[' || !strcasecmp(p, "# compatibility")) { if (f == f0) break; fclose(f); f = f0; filename = fname; continue; } if (*p == '\0' || p[0] == '#') continue; if (!memcmp(p, "include ", 8)) { pstrcpy(includename, sizeof(includename), filename); base = get_basename_offset(includename); pstrcpy(includename + base, sizeof(includename) - base, skipspaces(p + 8)); f = fopen(includename, "r"); if (f == NULL) { fprintf(stderr, "%s: cannot open %s\n", name, includename); f = f0; } filename = includename; continue; } if (p[0] != '0' || (p[1] != 'x' && p[1] != 'X')) { if (!has_iso_name) { pstrcpy(iso_name, sizeof(iso_name), p); has_iso_name = 1; continue; } if (!has_alias_list) { pstrcpy(alias_list, sizeof(alias_list), p); has_alias_list = 1; continue; } if (!strcmp(iso_name, p) || !strcmp(alias_list, p)) continue; if (!isdigit((unsigned char)*p)) { fprintf(stderr, "%s: ignoring line: %s\n", filename, p); continue; } } c1 = strtol(p, (char **)&p, 16); if (!isspace((unsigned char)*p)) { /* ignore ranges such as "0x20-0x7e idem" */ continue; } p = skipspaces(p); if (*p == '\0' || *p == '#') { /* unknown */ /* continue; */ } c2 = strtol(p, (char **)&p, 16); if (c1 >= 256) { fprintf(stderr, "%s: ERROR %d %d\n", filename, c1, c2); continue; } table[c1] = c2; nb++; } if (table[10] != 10) { if (table[0x25] == 0x0A) { /* EBCDIC file */ eol_char = 0x25; } else { fprintf(stderr, "%s: warning: newline is not preserved\n", filename); } } min_code = 0x7fffffff; max_code = -1; for (i = 0; i < 256; i++) { if (table[i] != i) { if (i < min_code) min_code = i; if (i > max_code) max_code = i; } } printf("\n" "/*----------------------------------------------------------------\n" " * filename: %s\n" " * iso_name: %s\n" " * name: %s\n" " * id: %s\n" " */\n\n", filename, iso_name, name, name_id); if (max_code != -1) { printf("static const unsigned short table_%s[%d] = {\n", name_id, max_code - min_code + 1); j = 0; for (i = min_code; i <= max_code; i++) { if ((j & 7) == 0) printf(" "); printf(" 0x%04x,", table[i]); if ((j++ & 7) == 7) printf("\n"); } if ((j & 7) != 0) printf("\n"); printf("};\n\n"); } printf("static QECharset charset_%s = {\n" " \"%s\",\n", name_id, name); printf(" \""); { const char *sep = ""; for (q = alias_list;;) { if ((p = strchr(q, '"')) == NULL || (q = strchr(++p, '"')) == NULL) break; *q++ = '\0'; if (strcmp(name, p)) { printf("%s%s", sep, p); sep = "|"; } } } printf("\",\n"); printf(" decode_8bit_init,\n" " decode_8bit,\n" " encode_8bit,\n" " charset_get_pos_8bit,\n" " charset_get_chars_8bit,\n" " charset_goto_char_8bit,\n" " charset_goto_line_8bit,\n" " .char_size = 1,\n" " .variable_size = 0,\n" " .table_alloc = 1,\n" " .eol_char = %d,\n" " .min_char = %d,\n" " .max_char = %d,\n" " .private_table = table_%s,\n" "};\n\n", eol_char, min_code, max_code, name_id); add_init(" qe_register_charset(&charset_"); add_init(name_id); add_init(");\n"); }
animationC::animationC(char* name, char* str) { skeFrameT *t, *f, **h; double total = 0.0; char* c; int p; Hang(); Name = name; FramesCnt = 0; FirstFrame = NULL; h = &FirstFrame; str = skipspaces(str); for(c = str; *c; c = skipspaces(c)) { // allocate // *h = (skeFrameT*)calloc(1, sizeof(skeFrameT)); if(*h == NULL) goto ERROR; f = *h; h = &f->next; FramesCnt++; // read duration // f->duration = strtod(c, &c); if(f->duration <= 0.0) goto ERROR; total += f->duration; c = skipspaces(c); // read values // for(p = 0; *c && *c != '\n'; p++) { if(p >= JOINTS_CNT) goto ERROR; c = skeValueRead(f->joint + p, c); if(c == NULL) goto ERROR; c = skipspaces(c); } // skip terminating character // if(*c == '\n') c++; } // normalize frames, so that the whole animation lasts 1.0 // for(f = FirstFrame; f; f = f->next) f->duration /= total; // close frames loop and return successfully // *h = FirstFrame; ERROR: ; /* // free everything // for(f = ani->first_frame; f; f = t) { t = f->next; free(f); }*/ }