TTrdItfAPI* plugin_wrapper::create( const char *psetupfile,ADDLOG plogfunc ) { if ( fcreate!=NULL ) { try { return fcreate(psetupfile,plogfunc); } catch(...) { return NULL; } } return NULL; }
void main(void) { Tree *t; File *hello, *goodbye, *world; t = mktree(); hello = fcreate(t->root, "hello", CHDIR|0777); assert(hello != nil); goodbye = fcreate(t->root, "goodbye", CHDIR|0777); assert(goodbye != nil); world = fcreate(hello, "world", 0666); assert(world != nil); world = fcreate(goodbye, "world", 0666); assert(world != nil); fdump(t->root, 0); fremove(world); fdump(t->root, 0); }
static int build_dictionary(void) { ucell lfa = 0; unsigned int i; /* we need a temporary place for latest outside the dictionary */ latest = &lfa; /* starting a new dictionary: clear dicthead */ dicthead = 0; #ifdef CONFIG_DEBUG_DICTIONARY printk("building dictionary, %d primitives.\nbuilt words:", sizeof(wordnames) / sizeof(void *)); #endif for (i = 0; i < sizeof(wordnames) / sizeof(void *); i++) { if (strlen(wordnames[i]) != 0) { fcreate((char *) wordnames[i], i); #ifdef CONFIG_DEBUG_DICTIONARY printk(" %s", wordnames[i]); #endif } } #ifdef CONFIG_DEBUG_DICTIONARY printk(".\n"); #endif /* get last/latest and state */ state = buildvariable("state", 0); last = buildvariable("forth-last", 0); latest = buildvariable("latest", 0); *latest = target_ucell(pointer2cell(latest)-2*sizeof(cell)); base=buildvariable("base", 10); buildconstant("/c", sizeof(u8)); buildconstant("/w", sizeof(u16)); buildconstant("/l", sizeof(u32)); buildconstant("/n", sizeof(ucell)); buildconstant("/x", sizeof(u64)); reveal(); if (verbose) { printk("Dictionary initialization finished.\n"); } return 0; }
int main(int argc, char* argv[]) { int fh, fh1; char c, n=0; char s[]="00", alphabet[]="0123456789abcdef"; print("Binary file to 'C' - char array converter by PowerMike (2:5064/21.25)\n\r"); print("Use: bin2c.exe <infile> <outfile> <array name>\n\r"); if (argc<4) { print("Need more parameters!"); return 10; } fh = fopen(argv[1], read); if (fh == NULL) { print ("File open error!"); return 10; } fh1 = fcreate(argv[2], 0); if (fh1 == NULL) { print ("File create error!"); return 10; } fwrite("char ",5,fh1); fwrite(argv[3], length(argv[3]), fh1); fwrite("[] =\n{\n ",9,fh1); while (fread(&c,1,fh)==1) { if (n!=0) fwrite (", ",2,fh1); if (n++>=12) { fwrite(" \\\n ",5,fh1); n=1; } fwrite ("0x",2,fh1); s[1]=alphabet[c%16]; s[0]=alphabet[c/16]; fwrite (&s,2,fh1); } fwrite("\n};\n",4,fh1); fclose(fh); fclose(fh1); print("Done.\n\r\n"); return 0; }
/* * This should be called to backup our identification + IP into flash */ void dtp_backup_id(void) { auto int offset, len, retval; auto unsigned long temp; auto File f, *F; F = &f; main_id.top = 0xa5; main_id._my_ip_addr = my_ip_addr; main_id._sin_mask = sin_mask; main_id.gate_ip = lookup_gateway(); /* backup: main_id */ printf("dtp_backup_id()...\n"); if(fcreate(F, NextBackupFile)) { /* Error! File allready exists? */ goto backup_error; } /* write out the new version number */ BackupVersion++; printf("\tBackupVersion == %ld\n",BackupVersion); if(4 != fwrite(F, (char *)&BackupVersion, 4)) { goto backup_error; } /* write out the length of the config table */ temp = sizeof(main_id); printf("\tsizeof(main_id) == %ld\n",temp); if(4 != fwrite(F, (char *)&temp, 4)) { goto backup_error; } /* write out the actual config table */ offset = 0; len = sizeof(main_id); while(offset < len) { retval = fwrite(F, (char *)&(main_id.top) + offset, len - offset); if(retval < 1) { /* error - no room */ goto backup_error; } offset += retval; } /* write out the length of the spec table */ temp = SSPEC_MAXSPEC * sizeof(ServerSpec); printf("\tsizeof(ServerSpec[]) == %ld\n",temp); if(4 != fwrite(F, (char *)&temp, 4)) { goto backup_error; } /* write the actual spec table */ offset = 0; len = SSPEC_MAXSPEC * sizeof(ServerSpec); while(offset < len) { retval = fwrite(F, (char *)&(server_spec[0]) + offset, len - offset); if(retval < 1) { /* error - no room */ goto backup_error; } offset += retval; } fclose(F); /* remove the old file, if it exists */ if(255 == NextBackupFile) { fdelete(254); NextBackupFile = 254; } else { fdelete(255); NextBackupFile = 255; } return; backup_error: fclose(F); /* should we format the fs here? */ printf("ERROR: No room to backup config table in flash FS!\n"); return; }
int main() { int rc; // Return code from filesystem functions File f1; // Demo file handle static char buf[1024]; // General-purpose buffer. if (!LX_2_USE) { printf("The specified device (LX# %d) does not exist. Change LX_2_USE.\n", (int)LX_2_USE); exit(1); } else printf("Using device LX# %d...\n", (int)LX_2_USE); /* * Step 1: initialize the filesystem. A real application must * decide whether to format a new flash device, or just start * up an existing filesystem. Formatting should not normally * be needed unless, say, a user commands it. A factory-fresh * flash device will usually be all erased so there is no need * to format. Likewise, a flash with completely random data * will be recognised as such and automatically formatted. */ rc = fs_init(0,0); if (rc) { printf("Could not initialize filesystem, error number %d\n", errno); exit(2); } /* * Step 2: format the filesystem if requested. Note that formatting * must be done _after_ the call to fs_init(). * * This demo initially asks whether the flash should be formatted * via the stdio connection. */ printf("Do you want to:\n"); printf(" <enter> re-use existing filesystem -or-\n"); printf(" F <enter> format the filesystem logical extent?\n"); gets(buf); if (toupper(buf[0]) == 'F') { rc = lx_format(LX_2_USE, 0); if (rc) { printf("Format failed, error code %d\n", errno); exit(3); } } /* * Step 3: open a file for writing if it already exists, or * create it if it does not. Note that creation automatically * opens the file for writing if it does not exist. If it * does exist, then EEXIST will be returned in errno. */ printf("Capacity of LX# %d is approximately %ld\n", (int)LX_2_USE, fs_get_lx_size(LX_2_USE, 0, 0)); fs_set_lx(LX_2_USE, LX_2_USE); rc = fcreate(&f1, MY_FILE_NAME); if (rc && errno == EEXIST) { printf("File %d exists: shall I delete it? (y/n)\n", (int)MY_FILE_NAME); gets(buf); if (toupper(buf[0]) == 'Y') { fdelete(MY_FILE_NAME); rc = fcreate(&f1, MY_FILE_NAME); } else rc = fopen_wr(&f1, MY_FILE_NAME); } if (rc) { printf("Couldn't create/open file %d: errno = %d\n", (int)MY_FILE_NAME, errno); exit(3); } do_it_again: /* * Seek to the end of the file then print the current length. */ fseek(&f1, 0, SEEK_END); printf("File length is %ld\n", ftell(&f1)); /* * Add some data to the end. */ strcpy(buf, "Test pattern ________"); rc = fwrite(&f1, buf, strlen(buf)); if (rc < strlen(buf)) { printf("Append operation failed, error code %d\n", errno); // but we keep going... } printf("After append operation...\n"); print_file(&f1); /* * Seek backwards and overwrite something. */ fseek(&f1, -7, SEEK_END); sprintf(buf, "%ld", ftell(&f1)); rc = fwrite(&f1, buf, strlen(buf)); if (rc < strlen(buf)) { printf("Overwrite operation failed, error code %d\n", errno); // but we keep going... } printf("After overwrite operation...\n"); print_file(&f1); /* * Delete some data from the start. */ rc = fshift(&f1, DELETE_AMOUNT, buf); if (rc == 0) { printf("Shift operation failed, error code %d\n", errno); // but we keep going... } printf("After shifting out %d chars...\n", rc); print_file(&f1); buf[rc] = 0; printf("...and the deleted data was '%s'\n", buf); /* * Another round, anyone? */ printf("Are you having too much fun? (y/n)\n"); gets(buf); if (toupper(buf[0]) == 'Y') goto do_it_again; return 0; }
static int interpret_source(char *fil) { FILE *f; char tib[160]; cell num; char *test; const ucell SEMIS = (ucell)findword("(semis)"); const ucell LIT = (ucell)findword("(lit)"); const ucell DOBRANCH = (ucell)findword("dobranch"); if ((f = fopen_include(fil)) == NULL) { printk("error while loading source file '%s'\n", fil); errors++; exit(1); } /* FIXME: We should read this file at * once. No need to get it char by char */ while (!feof(f)) { xt_t res; parse_word(f, tib); /* if there is actually no word, we continue right away */ if (strlen(tib) == 0) { continue; } /* Checking for builtin words that are needed to * bootstrap the forth base dictionary. */ if (!strcmp(tib, "(")) { parse(f, tib, ')'); continue; } if (!strcmp(tib, "\\")) { parse(f, tib, '\n'); continue; } if (!strcmp(tib, ":")) { parse_word(f, tib); #ifdef CONFIG_DEBUG_INTERPRETER printk("create colon word %s\n\n", tib); #endif fcreate(tib, DOCOL); /* see dict.h for DOCOL and other CFA ids */ *state = (ucell) (-1); continue; } if (!strcmp(tib, ";")) { #ifdef CONFIG_DEBUG_INTERPRETER printk("finish colon definition\n\n"); #endif writecell((cell)SEMIS); *state = (ucell) 0; reveal(); continue; } if (!strcasecmp(tib, "variable")) { parse_word(f, tib); #ifdef CONFIG_DEBUG_INTERPRETER printk("defining variable %s\n\n", tib); #endif buildvariable(tib, 0); reveal(); continue; } if (!strcasecmp(tib, "constant")) { parse_word(f, tib); #ifdef CONFIG_DEBUG_INTERPRETER printk("defining constant %s\n\n", tib); #endif buildconstant(tib, POP()); reveal(); continue; } if (!strcasecmp(tib, "value")) { parse_word(f, tib); #ifdef CONFIG_DEBUG_INTERPRETER printk("defining value %s\n\n", tib); #endif buildconstant(tib, POP()); reveal(); continue; } if (!strcasecmp(tib, "defer")) { parse_word(f, tib); #ifdef CONFIG_DEBUG_INTERPRETER printk("defining defer word %s\n\n", tib); #endif builddefer(tib); reveal(); continue; } if (!strcasecmp(tib, "include")) { parse_word(f, tib); #ifdef CONFIG_DEBUG_INTERPRETER printk("including file %s\n\n", tib); #endif interpret_source(tib); continue; } if (!strcmp(tib, "[']")) { xt_t xt; parse_word(f, tib); xt = findword(tib); if (*state == 0) { #ifdef CONFIG_DEBUG_INTERPRETER printk ("writing address of %s to stack\n\n", tib); #endif PUSH_xt(xt); } else { #ifdef CONFIG_DEBUG_INTERPRETER printk("writing lit, addr(%s) to dict\n\n", tib); #endif writecell(LIT); /* lit */ writecell((cell)xt); } continue; /* we have no error detection here */ } if (!strcasecmp(tib, "s\"")) { int cnt; cell loco; cnt = parse(f, tib, '"'); #ifdef CONFIG_DEBUG_INTERPRETER printk("compiling string %s\n", tib); #endif loco = dicthead + (6 * sizeof(cell)); writecell(LIT); writecell(pointer2cell(dict) + loco); writecell(LIT); writecell((ucell)cnt); writecell(DOBRANCH); loco = cnt + sizeof(cell) - 1; loco &= ~(sizeof(cell) - 1); writecell(loco); memcpy(dict + dicthead, tib, cnt); dicthead += cnt; paddict(sizeof(cell)); continue; } /* look if tib is in dictionary. */ /* should the dictionary be searched before the builtins ? */ res = findword(tib); if (res) { u8 flags = read_byte((u8*)cell2pointer(res) - sizeof(cell) - 1); #ifdef CONFIG_DEBUG_INTERPRETER printk("%s is 0x%" FMT_CELL_x "\n", tib, (ucell) res); #endif if (!(*state) || (flags & 3)) { #ifdef CONFIG_DEBUG_INTERPRETER printk("executing %s, %" FMT_CELL_d " (flags: %s %s)\n", tib, res, (flags & 1) ? "immediate" : "", (flags & 2) ? "compile-only" : ""); #endif PC = (ucell)res; enterforth(res); } else { #ifdef CONFIG_DEBUG_INTERPRETER printk("writing %s to dict\n\n", tib); #endif writecell((cell)res); } continue; } /* if not look if it's a number */ if (tib[0] == '-') num = strtoll(tib, &test, read_ucell(base)); else num = strtoull(tib, &test, read_ucell(base)); if (*test != 0) { /* what is it?? */ printk("%s:%d: %s is not defined.\n\n", srcfilenames[cursrc - 1], srclines[cursrc - 1], tib); errors++; #ifdef CONFIG_DEBUG_INTERPRETER continue; #else return -1; #endif } if (*state == 0) { #ifdef CONFIG_DEBUG_INTERPRETER printk("pushed %" FMT_CELL_x " to stack\n\n", num); #endif PUSH(num); } else { #ifdef CONFIG_DEBUG_INTERPRETER printk("writing lit, %" FMT_CELL_x " to dict\n\n", num); #endif writecell(LIT); /* lit */ writecell(num); } } fclose(f); cursrc--; return 0; }
static void builddefer(const char *name) { fcreate(name, DODFR); /* see dict.h for DODFR and other CFA ids */ writecell((ucell)0); writecell((ucell)findword("(semis)")); }
static void buildconstant(const char *name, cell defval) { fcreate(name, DOCON); /* see dict.h for DOCON and other CFA ids */ writecell(defval); }
static ucell *buildvariable(const char *name, cell defval) { fcreate(name, DOVAR); /* see dict.h for DOVAR and other CFA ids */ writecell(defval); return (ucell *) (dict + dicthead - sizeof(cell)); }