/* RedirectCmdDone(): * As each command completes, this redirection mechanism must be notified. * When this function is called (by docmd() after the command function has * completed), if the RedirectFile[] array is populated, it transfers the * buffer to the specified file; if the array is empty, then no TFS action * is taken. * The file specified in RedirectFile[] may have additional commas in it. * They allow the filename to contain flags and info field. */ void RedirectionCmdDone(void) { if (RedirectState != REDIRECT_UNINITIALIZED) { RedirectState = REDIRECT_IDLE; if (RedirectFile[0]) { char *comma, *info, *flags; comma = info = flags = (char *)0; comma = strchr(RedirectFile,','); if (comma) { *comma = 0; flags = comma+1; comma = strchr(flags,','); if (comma) { *comma = 0; info = comma+1; } } tfsadd(RedirectFile,info,flags,(uchar *)RedirectBase, (int)(RedirectPtr-RedirectBase)); RedirectFile[0] = 0; RedirectPtr = RedirectBase; } } }
/* fatfsGet(): * Retreive a file from the FATFS and place it in the specified destination. * The destination may be either a TFS filename or a hex address * (indicated by a leading "0x"). * * Input: * char *fatfspath * Pointer to FATFS file and path. * char *dest * Pointer to TFS filename or a addr,len string. * The expected "addr,len" syntax is 0xADDR,LEN or 0xADDR,0xLEN. */ static int fatfsGet(char *fatfile, char *dest) { FILEINFO fi; char *p, tfs; uint32_t i; if (DFS_OpenFile(&vi, (uint8_t *)fatfile, DFS_READ, sector, &fi)) { printf("error opening file\n"); return(-1); } if (strncmp(dest,"0x",2) == 0) { tfs = 0; p = (char *)strtoul(dest,0,0); } else { tfs = 1; p = fatfs_tmp_space; } DFS_ReadFile(&fi, sector, (uint8_t *)p, &i, fi.filelen); if (i != fi.filelen) { printf("read %d, expected %d\n",i,fi.filelen); return(-1); } if (tfs) { #if INCLUDE_TFS int tfserr; char *flags, *info; flags = info = (char *)0; if ((flags = strchr(dest,','))) { *flags++ = 0; if ((info = strchr(flags,','))) *info++ = 0; } tfserr = tfsadd(dest,info,flags,(uchar *)p,fi.filelen); if (tfserr != TFS_OKAY) { printf("TFS error: %s\n",tfserrmsg(tfserr)); return(-1); } #else printf("TFS not built in\n"); return(-1); #endif } return(0); }
/* tfsclose(): * If the file was opened for reading only, then just close out the * entry in the tfsSlots table. If the file was opened for creation, * then add it to the tfs list. Note the additional argument is * only needed for tfsclose() of a newly created file. * info = additional text describing the file. * MONLIB NOTICE: this function is accessible through monlib.c. */ int tfsclose(int fd,char *info) { int err; struct tfsdat *tdat; if (!info) info = ""; if (tfsTrace > 0) printf("tfsclose(%d,%s)\n",fd,info); if ((fd < 0) || (fd >= TFS_MAXOPEN)) return(TFSERR_BADARG); tdat = &tfsSlots[fd]; if (tdat->offset == -1) return(TFSERR_BADFD); /* Mark the file as closed by setting the offset to -1. * Note that this is done prior to potentially calling tfsadd() so * that tfsadd() will not think the file is opened and reject the add... */ tdat->offset = -1; /* If the file was opened for creation or append, and the hwp * (high-water-point) is greater than zero, then add it now. * * Note regarding hwp==0... * In cases where a non-existent file is opened for creation, * but then nothing is written to the file, the hwp value will * be zero; hence, no need to call tfsadd(). */ if ((tdat->flagmode & (TFS_CREATE | TFS_APPEND)) && (tdat->hwp > 0)) { char buf[16]; err = tfsadd(tdat->hdr.name, info, tfsflagsbtoa(tdat->flagmode,buf), tdat->base, tdat->hwp); if (err != TFS_OKAY) { printf("%s: %s\n",tdat->hdr.name,tfserrmsg(err)); return(err); } } return(TFS_OKAY); }
void tfslog(int action, char *string) { #if TFS_CHANGELOG_SIZE static char *tfslogaction[] = { "ADD", "DEL", "IPM", " ON", "OFF" }; extern void *setTmpMaxUsrLvl(); static char buf[TFS_CHANGELOG_SIZE]; TFILE *tfp; int (*fptr)(); char *eol, *eob, *logaction, tbuf[32]; int newfsize, fsize, lsize, tfd, err, len, tbuflen; switch(action) { case TFSLOG_ADD: /* Return here if logging is off, */ case TFSLOG_DEL: /* or this tfslog() call is on the */ case TFSLOG_IPM: /* TFS_CHANGELOG_FILE itself. */ if (!tfsLogging || !strcmp(string,TFS_CHANGELOG_FILE)) return; break; case TFSLOG_ON: if (tfsLogging == 1) return; tfsLogging = 1; break; case TFSLOG_OFF: if (tfsLogging == 0) return; tfsLogging = 0; break; } /* Force the getUsrLvl() function to return MAX: */ fptr = (int(*)())setTmpMaxUsrLvl(); logaction = tfslogaction[action]; tfp = tfsstat(TFS_CHANGELOG_FILE); tfsGetAtime(0,tbuf,sizeof(tbuf)); tbuflen = strlen(tbuf); if (tfp) { tfd = tfsopen(TFS_CHANGELOG_FILE,TFS_RDONLY,0); fsize = tfsread(tfd,buf,TFS_CHANGELOG_SIZE); tfsclose(tfd,0); newfsize = (fsize+strlen(logaction)+strlen(string)+3); if (tbuflen) newfsize += tbuflen + 3; eob = buf + fsize; /* If newfsize is greater than the maximum size the file is * allowed to grow, then keep removing the first line * (oldest entry) until new size is within the limit... */ if (newfsize > TFS_CHANGELOG_SIZE) { lsize = 0; eol = buf; while ((newfsize-lsize) > TFS_CHANGELOG_SIZE) { while((*eol != '\r') && (*eol != '\n')) eol++; while((*eol == '\r') || (*eol == '\n')) eol++; lsize = eol-buf; } fsize -= lsize; newfsize -= lsize; eob -= lsize; memcpy(buf,eol,fsize); } if (tbuflen) sprintf(eob,"%s: %s @ %s\n",logaction,string,tbuf); else sprintf(eob,"%s: %s\n",logaction,string); err = _tfsunlink(TFS_CHANGELOG_FILE); if (err < 0) printf("%s: %s\n",TFS_CHANGELOG_FILE, (char *)tfsctrl(TFS_ERRMSG,err,0)); err = tfsadd(TFS_CHANGELOG_FILE,0,"u3",buf,newfsize); if (err < 0) printf("%s: %s\n",TFS_CHANGELOG_FILE, (char *)tfsctrl(TFS_ERRMSG,err,0)); } else { if (tbuflen) len = sprintf(buf,"%s: %s @ %s\n",logaction,string,tbuf); else len = sprintf(buf,"%s: %s\n",logaction,string); err = tfsadd(TFS_CHANGELOG_FILE,0,"u3",buf,len); if (err < 0) printf("%s: %s\n",TFS_CHANGELOG_FILE, (char *)tfsctrl(TFS_ERRMSG,err,0)); } /* Restore the original getUsrLvl() functionality: */ clrTmpMaxUsrLvl(fptr); #endif }
/* envToExec(): Create a file of "set" commands that can be run to recreate the current environment. Changed Oct 2008 to eliminate use of getAppRamStart(). */ int envToExec(char *filename) { int err, vartot, size, rc; char *buf, *bp, *cp; register struct s_shell *sp; sp = shell_vars; vartot = size = rc = 0; /* First go through the list to see how much space we need * to allocate... */ while(1) { if (validEnvToExecVar(sp->name)) { size += strlen(sp->name) + 6; cp = sp->val; while(*cp) { if (*cp == '$') size++; size++; cp++; } size += 3; vartot++; } if (sp->next != (struct s_shell *)0) sp = sp->next; else break; } if (size == 0) return(0); /* Now that we know the space needed (stored in 'size' variable), * allocate it and build the new file in that space, then use tfsadd() * to create the file... */ vartot = 0; sp = shell_vars; buf = bp = (char *)env_alloc(size); while(1) { /* Note: if this code changes, then the code above that is used to * allocate the buffer size may also need to change... */ if (validEnvToExecVar(sp->name)) { bp += sprintf(bp,"set %s \"",sp->name); cp = sp->val; while(*cp) { if (*cp == '$') *bp++ = '\\'; *bp++ = *cp++; } *bp++ = '\"'; *bp++ = '\n'; *bp = 0; vartot++; } if (sp->next != (struct s_shell *)0) sp = sp->next; else break; } if (vartot > 0) { err = tfsadd(filename,"envsetup","e",(unsigned char *)buf,strlen(buf)); if (err != TFS_OKAY) { printf("%s: %s\n",filename,(char *)tfsctrl(TFS_ERRMSG,err,0)); rc = -1; } } env_free(buf); return(rc); }