// fname indica la ruta y denominación del archivo void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn) { FILE *f; int row, col; register struct ent **pp; int pid; info("Writing file \"%s\"...", fname); if ((f = openfile(fname, &pid, NULL)) == (FILE *)0) { error ("Can't create file \"%s\"", fname); return; } struct ent * ent = go_end(); if (rn > ent->row) rn = ent->row; //if (cn > ent->col) cn = ent->col; for (row = r0; row <= rn; row++) { for (pp = ATBL(tbl, row, col = c0); col <= cn; col++, pp++) { if (*pp) { char * s; if ((*pp)->flags & is_valid) { if ((*pp)->cellerror) { (void) fprintf (f, "%*s", fwidth[col], ((*pp)->cellerror == CELLERROR ? "ERROR" : "INVALID")); } else if ((*pp)->format) { char field[FBUFLEN]; if (*((*pp)->format) == 'd') { // formato fecha time_t v = (time_t) ((*pp)->v); strftime(field, sizeof(field), ((*pp)->format)+1, localtime(&v)); } else { // formato numerico format((*pp)->format, precision[col], (*pp)->v, field, sizeof(field)); } ltrim(field, ' '); unspecial(f, field, coldelim); } else { //eng number format char field[FBUFLEN] = ""; (void) engformat(realfmt[col], fwidth[col], precision[col], (*pp)->v, field, sizeof(field)); ltrim(field, ' '); unspecial(f, field, coldelim); } } if ((s = (*pp)->label)) { ltrim(s, ' '); unspecial(f, s, coldelim); } } if (col < cn) (void) fprintf(f,"%c", coldelim); } (void) fprintf(f,"\n"); } closefile(f, pid, 0); if (! pid) { info("File \"%s\" written", fname); } }
static void unsetspec(struct tbl *vp) { /* * AT&T ksh man page says OPTIND, OPTARG and _ lose special * meaning, but OPTARG does not (still set by getopts) and _ is * also still set in various places. Don't know what AT&T does * for HISTSIZE, HISTFILE. Unsetting these in AT&T ksh does not * loose the 'specialness': IFS, COLUMNS, PATH, TMPDIR */ switch (special(vp->name)) { #ifdef __OS2__ case V_BEGINLIBPATH: case V_ENDLIBPATH: case V_LIBPATHSTRICT: setextlibpath(vp->name, ""); return; #endif #if HAVE_PERSISTENT_HISTORY case V_HISTFILE: sethistfile(NULL); return; #endif case V_IFS: set_ifs(TC_IFSWS); break; case V_PATH: afree(path, APERM); strdupx(path, def_path, APERM); /* clear tracked aliases */ flushcom(true); break; #ifndef MKSH_NO_CMDLINE_EDITING case V_TERM: x_initterm(null); return; #endif case V_TMPDIR: /* should not become unspecial */ if (tmpdir) { afree(tmpdir, APERM); tmpdir = NULL; } break; case V_LINENO: case V_RANDOM: case V_SECONDS: case V_TMOUT: /* AT&T ksh leaves previous value in place */ unspecial(vp->name); break; } }
static void unsetspec(struct tbl *vp) { /* * AT&T ksh man page says OPTIND, OPTARG and _ lose special * meaning, but OPTARG does not (still set by getopts) and _ is * also still set in various places. Don't know what AT&T does * for HISTSIZE, HISTFILE. Unsetting these in AT&T ksh does not * loose the 'specialness': IFS, COLUMNS, PATH, TMPDIR */ switch (special(vp->name)) { #if HAVE_PERSISTENT_HISTORY case V_HISTFILE: sethistfile(NULL); return; #endif case V_IFS: setctypes(TC_IFSWS, C_IFS); ifs0 = ' '; break; case V_PATH: if (path) afree(path, APERM); strdupx(path, def_path, APERM); /* clear tracked aliases */ flushcom(true); break; case V_TMPDIR: /* should not become unspecial */ if (tmpdir) { afree(tmpdir, APERM); tmpdir = NULL; } break; case V_LINENO: case V_RANDOM: case V_SECONDS: case V_TMOUT: /* AT&T ksh leaves previous value in place */ unspecial(vp->name); break; } }
int main(int argc, char *argv[]) { bool removeTempC = true; char c; while(--argc > 1) { ++argv; if ((*argv)++[0] == '-') { while ((c = (*argv)++[0]) != '\0') { switch (c) { case 'r': removeTempC = false; break; default: printf("Incorrect optional argument %c.\n", c); break; } } } } /* There must be one argument, that is a file name */ if (argc != 1) { fprintf(stderr, "Incorrect number of arguments.\nusage: lcomp [-r] file.L\n"); return 1; } /* It must have the extension '.L' */ char *filename = argv[1]; int i; for (i=0; filename[i+1] != '\0'; i++); // Go to the end of the filename if (!(i && filename[i] == 'L' && filename[i-1] == '.')) { fprintf(stderr, "Invalid .L filename.\n"); return 1; } /* Open the source .L file */ FILE *sfile; if ((sfile = fopen(filename, "r")) == NULL) { fprintf(stderr, "Can't open %s.\n", filename); return 1; } /* Create the .c file to be written to */ FILE *ctemp; char *tempName = strcat(filename, "temp.c"); if ((ctemp = fopen(tempName, "w")) == NULL) { fprintf(stderr, "Couldn't create temp .c file\n"); return 1; } char data[MAXLINE]; fread(data, sizeof(char), MAXLINE, sfile); unspecial(data, MAXLINE); for (i=0; data[i+1] != EOF; i++); // Go to end of data data[i] = '\0'; // Write to .c file fprintf(ctemp, "#include <stdio.h>\n" "int main(int argc, char *argv[]) {\n"); fprintf(ctemp, "printf(\"%s\");", data); fprintf(ctemp, "\nreturn 0;}"); fclose(ctemp); fclose(sfile); char *args[] = {"/usr/bin/gcc", tempName, "-std=c11", "-o", "hello", NULL}; runcmd("/usr/bin/gcc", args); if (removeTempC) { //char *rmargs[] = {"/bin/rm", "-f", tempName, NULL}; //runcmd("/bin/rm", rmargs); int rem = remove(tempName); if (rem != 0) { fprintf(stderr, "Failed to remove temp file\n"); } } return 0; }
static void setspec(struct tbl *vp) { mksh_ari_u num; char *s; int st; switch ((st = special(vp->name))) { #if HAVE_PERSISTENT_HISTORY case V_HISTFILE: sethistfile(str_val(vp)); return; #endif case V_IFS: setctypes(s = str_val(vp), C_IFS); ifs0 = *s; return; case V_PATH: if (path) afree(path, APERM); s = str_val(vp); strdupx(path, s, APERM); /* clear tracked aliases */ flushcom(true); return; case V_TMPDIR: if (tmpdir) { afree(tmpdir, APERM); tmpdir = NULL; } /* * Use tmpdir iff it is an absolute path, is writable * and searchable and is a directory... */ { struct stat statb; s = str_val(vp); /* LINTED use of access */ if (mksh_abspath(s) && access(s, W_OK|X_OK) == 0 && stat(s, &statb) == 0 && S_ISDIR(statb.st_mode)) strdupx(tmpdir, s, APERM); } return; /* common sub-cases */ case V_COLUMNS: case V_LINES: if (vp->flag & IMPORT) { /* do not touch */ unspecial(vp->name); vp->flag &= ~SPECIAL; return; } /* FALLTHROUGH */ case V_HISTSIZE: case V_LINENO: case V_OPTIND: case V_RANDOM: case V_SECONDS: case V_TMOUT: vp->flag &= ~SPECIAL; if (getint(vp, &num, false) == -1) { s = str_val(vp); if (st != V_RANDOM) errorf("%s: %s: %s", vp->name, "bad number", s); num.u = hash(s); } vp->flag |= SPECIAL; break; default: /* do nothing, do not touch vp at all */ return; } /* process the singular parts of the common cases */ switch (st) { case V_COLUMNS: if (num.i >= MIN_COLS) x_cols = num.i; break; case V_HISTSIZE: sethistsize(num.i); break; case V_LINENO: /* The -1 is because line numbering starts at 1. */ user_lineno = num.u - (mksh_uari_t)current_lineno - 1; break; case V_LINES: if (num.i >= MIN_LINS) x_lins = num.i; break; case V_OPTIND: getopts_reset((int)num.i); break; case V_RANDOM: /* * mksh R39d+ no longer has the traditional repeatability * of $RANDOM sequences, but always retains state */ rndset((unsigned long)num.u); break; case V_SECONDS: { struct timeval tv; mksh_TIME(tv); seconds = tv.tv_sec - num.i; } break; case V_TMOUT: ksh_tmout = num.i >= 0 ? num.i : 0; break; } }