static int cmd_order_x(globalstate *gstate, const char *name, ...) { va_list ap; char *p; const char **names; int i; names = gstate->statics->order_names; if (names != NULL) { if ((i = string_index(name, names)) == -1) { /* check the alternate list */ va_start(ap, name); p = va_arg(ap, char *); while (p != NULL) { if ((i = string_index(p, names)) != -1) { gstate->order_index = i; return CMD_REFRESH; } p = va_arg(ap, char *); } message_error(" Sort order not recognized"); } else {
static int cmd_order(globalstate *gstate) { char tmpbuf[MAX_COLS]; int i; if (gstate->statics->order_names != NULL) { message_prompt("Column to sort: "); if (readline(tmpbuf, sizeof(tmpbuf), No) > 0) { if ((i = string_index(tmpbuf, gstate->statics->order_names)) == -1) { message_error(" Sort order \"%s\" not recognized", tmpbuf); } else { gstate->order_index = i; return CMD_REFRESH; } } } return CMD_OK; }
void server_server(char *data) { char *server; server = string_index(data + 1, 0); printf("[+] server: new server <%s>\n", server); server_new(server); free(server); }
void process(struct processor *p_uncast, const char *filename, struct stack *s, struct makefile *m) { struct processor_pdfcrop *p; void *c; int cachedir_index; char *cachedir; char *infile; /* We need access to the real structure, get it safely */ p = talloc_get_type(p_uncast, struct processor_pdfcrop); /* Makes a new context */ c = talloc_new(p); /* Finds the original filename */ cachedir_index = string_index(filename, ".tek_cache/"); if (cachedir_index == -1) { fprintf(stderr, "Bad cachedir for image\n"); return; } cachedir = talloc_strdup(c, filename); cachedir[cachedir_index + strlen(".tex_cache/")] = '\0'; infile = talloc_strdup(c, filename); infile[cachedir_index] = '\0'; strcat(infile, filename + strlen(cachedir)); infile[strlen(infile) - 4] = '\0'; TALLOC_FREE(cachedir); cachedir = talloc_strndup(c, filename, basename_len(filename)); /* Creates the target to build the image */ makefile_create_target(m, filename); makefile_start_deps(m); makefile_add_dep(m, infile); makefile_end_deps(m); makefile_start_cmds(m); makefile_nam_cmd(m, "echo -e \"PDFCROP\\t%s\"", infile); makefile_add_cmd(m, "mkdir -p \"%s\" >& /dev/null || true", cachedir); makefile_add_cmd(m, "pdfcrop \"%s\" \"%s\" >& /dev/null", infile, filename); makefile_end_cmds(m); /* Cleans up all the memory allocated by this code. */ TALLOC_FREE(c); }
/* parent has child * child in parent */ int obj_in(Object child, Object parent) { switch (TM_TYPE(parent)) { case TYPE_LIST: { return (list_index(GET_LIST(parent), child) != -1); } case TYPE_STR: { if (TM_TYPE(child) != TYPE_STR) return 0; return string_index(GET_STR_OBJ(parent), GET_STR_OBJ(child), 0) != -1; } case TYPE_DICT: { DictNode* node = dict_get_node(GET_DICT(parent), child); if (node == NULL) { return 0; } return 1; } } return 0; }
int main() { string *s = string_init(); string *o; string *subs; string *q = string_init_cstring("quit"); string_read_line(s); while(string_cmp(s, q)) { string_print(s); string_reset(s); string_read_line(s); } string_append(s, q); o = string_cat(s, q); string_print(s); string_print(o); string_append_cstring(o, "hello"); string_append_char(o, 'J'); string_print(o); subs = string_substring(o, 5, 6); printf("--\n"); printf("%c\n", string_get(s, 5)); printf("%d\n", string_index(o, 'o')); string_print(subs); printf("--\n"); o = string_free(o); s = string_free(s); q = string_free(q); subs = string_free(subs); return 0; }
int main(int argc, char **argv) { int i; char *input, *output, *last; FILE *inf, *otf; char buf[BUF_SIZE]; input = NULL; last = NULL; output = NULL; for (i = 1; i < argc; i++) { if (last != NULL) { if (strcmp(last, "-o") == 0) { output = argv[i]; last = NULL; } else if (strcmp(last, "-i") == 0) { input = argv[i]; last = NULL; } } else last = argv[i]; } if ((input == NULL) || (output == NULL)) { fprintf(stderr, "Specify both -i and -o\n"); return 1; } inf = fopen(input, "r"); otf = fopen(output, "w"); while (fgets(buf, BUF_SIZE, inf) != NULL) { if (buf[0] == '%') { } else if (string_index(buf, "\\usepackage{graphicx}") != -1) { fputs(buf, otf); fputs("\\usepackage{grffile}\n", otf); } else if (string_index(buf, "\\includegraphics") != -1) { size_t index; /* Removes all the optional agruments */ index = string_index(buf, "\\includegraphics"); index += strlen("\\includegraphics"); while ((buf[index] != '}') && (buf[index] != '\0')) index++; /* Ensures that the code is properly formed */ if (buf[index] == '\0') { fprintf(stderr, "Bad includegraphics\n"); return 1; } /* Appends ".pdf" to the filename */ if (fwrite(buf, 1, index, otf) != index) abort(); fputs(".pdf", otf); fputs(buf + index, otf); } else if (string_index(buf, "\\input") != -1) { size_t index, oindex; /* Removes all the optional agruments */ index = string_index(buf, "\\input"); index += strlen("\\input"); while ((buf[index] != '}') && (buf[index] != '\0')) index++; /* Ensures that the code is properly formed */ if (buf[index] == '\0') { fprintf(stderr, "Bad includegraphics\n"); return 1; } /* Appends ".pdf" to the filename */ oindex = index; if (strncmp(buf + index - 4, ".tex", 4) == 0) oindex = index - 4; if (strncmp(buf + index - 5, ".stex", 5) == 0) oindex = index - 5; if (fwrite(buf, 1, oindex, otf) != oindex) abort(); fputs(".stex", otf); fputs(buf + index, otf); } else { /* There was no special processor to process this file */ fputs(buf, otf); } } fclose(inf); fclose(otf); return 0; }
int main(int argc, char *argv[]) { char *env_top; char **preset_argv; int preset_argc = 0; void *mask; int need_mini = 1; struct statics statics; globalstate *gstate; /* get our name */ if (argc > 0) { if ((myname = strrchr(argv[0], '/')) == 0) { myname = argv[0]; } else { myname++; } } /* binary compatibility check */ #ifdef HAVE_UNAME { struct utsname uts; if (uname(&uts) == 0) { if (strcmp(uts.machine, UNAME_HARDWARE) != 0) { fprintf(stderr, "%s: incompatible hardware platform\n", myname); exit(EX_UNAVAILABLE); } } } #endif /* initialization */ gstate = (globalstate *)calloc(1, sizeof(globalstate)); gstate->statics = &statics; time_mark(NULL); /* preset defaults for various options */ gstate->show_usernames = Yes; gstate->topn = DEFAULT_TOPN; gstate->delay = DEFAULT_DELAY; gstate->fulldraw = Yes; gstate->use_color = Yes; gstate->interactive = Maybe; /* preset defaults for process selection */ gstate->pselect.idle = Yes; gstate->pselect.system = No; gstate->pselect.fullcmd = No; gstate->pselect.command = NULL; gstate->pselect.uid = -1; gstate->pselect.mode = 0; /* use a large buffer for stdout */ #ifdef HAVE_SETVBUF setvbuf(stdout, stdoutbuf, _IOFBF, BUFFERSIZE); #else #ifdef HAVE_SETBUFFER setbuffer(stdout, stdoutbuf, BUFFERSIZE); #endif #endif /* get preset options from the environment */ if ((env_top = getenv("TOP")) != NULL) { preset_argv = argparse(env_top, &preset_argc); preset_argv[0] = myname; do_arguments(gstate, preset_argc, preset_argv); } /* process arguments */ do_arguments(gstate, argc, argv); #ifdef ENABLE_COLOR /* If colour has been turned on read in the settings. */ env_top = getenv("TOPCOLOURS"); if (!env_top) { env_top = getenv("TOPCOLORS"); } /* must do something about error messages */ color_env_parse(env_top); color_activate(gstate->use_color); #endif /* in order to support forward compatability, we have to ensure that the entire statics structure is set to a known value before we call machine_init. This way fields that a module does not know about will retain their default values */ memzero((void *)&statics, sizeof(statics)); statics.boottime = -1; /* call the platform-specific init */ if (machine_init(&statics) == -1) { exit(EX_SOFTWARE); } /* create a helper list of sort order names */ gstate->order_namelist = string_list(statics.order_names); /* look up chosen sorting order */ if (gstate->order_name != NULL) { int i; if (statics.order_names == NULL) { message_error(" This platform does not support arbitrary ordering"); } else if ((i = string_index(gstate->order_name, statics.order_names)) == -1) { message_error(" Sort order `%s' not recognized", gstate->order_name); message_error(" Recognized sort orders: %s", gstate->order_namelist); } else { gstate->order_index = i; } } /* initialize extensions */ init_username(); /* initialize termcap */ gstate->smart_terminal = screen_readtermcap(gstate->interactive); /* determine interactive state */ if (gstate->interactive == Maybe) { gstate->interactive = smart_terminal; } /* if displays were not specified, choose an appropriate default */ if (gstate->displays == 0) { gstate->displays = gstate->smart_terminal ? Infinity: 1; } /* we don't need a mini display when delay is less than 2 seconds or when we are not on a smart terminal */ if (gstate->delay <= 1 || !smart_terminal) { need_mini = 0; } /* set constants for username/uid display */ if (gstate->show_usernames) { gstate->header_text = format_header("USERNAME"); gstate->get_userid = username; } else { gstate->header_text = format_header(" UID "); gstate->get_userid = itoa7; } gstate->pselect.usernames = gstate->show_usernames; /* initialize display */ if ((gstate->max_topn = display_init(&statics)) == -1) { fprintf(stderr, "%s: can't allocate sufficient memory\n", myname); exit(EX_OSERR); } /* check for infinity and for overflowed screen */ if (gstate->topn == Infinity) { gstate->topn = INT_MAX; } else if (gstate->topn > gstate->max_topn) { #if 0 message_error(" This terminal can only display %d processes", gstate->max_topn); #endif } #ifdef ENABLE_COLOR /* producing a list of color tags is easy */ if (gstate->show_tags) { color_dump(stdout); exit(EX_OK); } #endif /* hold all signals while we initialize the screen */ mask = hold_signals(); screen_init(); /* set the signal handlers */ set_signals(); /* longjmp re-entry point */ /* set the jump buffer for long jumps out of signal handlers */ if (setjmp(jmp_int) != 0) { /* this is where we end up after processing sigwinch or sigtstp */ /* tell display to resize its buffers, and get the new length */ if ((gstate->max_topn = display_resize()) == -1) { /* thats bad */ quit(EX_OSERR); /*NOTREACHED*/ } /* set up for a full redraw, and get the current line count */ gstate->fulldraw = Yes; /* safe to release the signals now */ release_signals(mask); } else { /* release the signals */ release_signals(mask); /* some systems require a warmup */ /* always do a warmup for batch mode */ if (gstate->interactive == 0 || statics.flags.warmup) { struct system_info system_info; struct timeval timeout; time_mark(&(gstate->now)); get_system_info(&system_info); (void)get_process_info(&system_info, &gstate->pselect, 0); timeout.tv_sec = 1; timeout.tv_usec = 0; select(0, NULL, NULL, NULL, &timeout); /* if we've warmed up, then we can show good states too */ gstate->show_cpustates = Yes; need_mini = 0; } } /* main loop */ while ((gstate->displays == -1) || (--gstate->displays > 0)) { do_display(gstate); if (gstate->interactive) { if (need_mini) { do_minidisplay(gstate); need_mini = 0; } do_command(gstate); } else { do_wait(gstate); } } /* do one last display */ do_display(gstate); quit(EX_OK); /* NOTREACHED */ return 1; /* Keep compiler quiet. */ }
void process(struct processor *p_uncast, const char *filename, struct stack *s, struct makefile *m) { struct processor_svgtex *p; void *c; int cachedir_index; char *cachedir; char *infile; char *cachename; char *origfile; /* We need access to the real structure, get it safely */ p = talloc_get_type(p_uncast, struct processor_svgtex); /* Makes a new context */ c = talloc_new(p); /* Finds the original filename */ cachedir_index = string_index(filename, ".tek_cache/"); if (cachedir_index == -1) { fprintf(stderr, "Bad cachedir for image\n"); return; } cachedir = talloc_strdup(c, filename); cachedir[cachedir_index + strlen(".tex_cache/")] = '\0'; infile = talloc_strdup(c, filename); infile[cachedir_index] = '\0'; strcat(infile, filename + strlen(cachedir)); infile[strlen(infile) - 8] = '\0'; TALLOC_FREE(cachedir); cachedir = talloc_strndup(c, filename, basename_len(filename)); cachename = talloc_strndup(c, filename, strlen(filename) - 8); origfile = infile; while (strstr(infile, "/") != NULL) infile = infile + 1; /* Creates the target to build the LaTeX file for importing. */ makefile_create_target(m, filename); makefile_start_deps(m); makefile_add_dep(m, cachename); makefile_end_deps(m); makefile_start_cmds(m); makefile_nam_cmd(m, "echo -e \"SVGTEX\\t%s\"", infile); makefile_add_cmd(m, "mkdir -p \"%s\" >& /dev/null || true", cachedir); makefile_add_cmd(m, "cd .tek_cache; svgtexpp \"%s/%s\" > /dev/null", strstr(cachedir, "/")+1, infile); makefile_end_cmds(m); /* This one is necessary for pandoc. */ makefile_create_target(m, cachename); makefile_start_deps(m); makefile_add_dep(m, origfile); makefile_end_deps(m); makefile_start_cmds(m); makefile_nam_cmd(m, "echo -e \"IMGCP\\t%s\"", origfile); makefile_add_cmd(m, "mkdir -p \"%s\" >& /dev/null || true", cachedir); makefile_add_cmd(m, "cp \"%s\" \"%s\"", origfile, cachename); makefile_end_cmds(m); /* Also generate the relevant PDF */ { char *outname; outname = talloc_asprintf(c, "%.*s.svg.pdf", (int)(strlen(cachename)-8), cachename); makefile_create_target(m, outname); makefile_start_deps(m); makefile_add_dep(m, cachename); makefile_end_deps(m); makefile_start_cmds(m); makefile_nam_cmd(m, "echo -e \"RSVGC\\t%s\"", infile); makefile_add_cmd(m, "mkdir -p \"%s\" >& /dev/null || true", cachedir); makefile_add_cmd(m, "rsvg-convert \"%s\" -f pdf -o \"%s\"", infile, outname); makefile_end_cmds(m); } /* Cleans up all the memory allocated by this code. */ TALLOC_FREE(c); }
nemo_main() { stream denstr, velstr, outstr, tabstr; real center[2], cospa, sinpa, cosi, sini, cost, costmin, x, y, xt, yt, r, den; real vr, wt, frang, dx, dy, xmin, ymin, rmin, rmax, ave, tmp, rms; real sincosi, cos2i, tga, dmin, dmax, dval, dr, area, fsum1, fsum2; int i, j, k, nx, ny, ir, nring, nundf, nout, nang, nsum; string outmode; int mode = -1; velstr = stropen(getparam("in"),"r"); read_image(velstr,&velptr); nx = Nx(velptr); ny = Ny(velptr); if (hasvalue("out")) { outmode = getparam("mode"); mode = string_index(outmodes, outmode); if (mode < 0) error("Illegal mode=%s [%d], valid:",outmode,mode,outmodes); warning("New out= mode mode=%s [%d]",outmode,mode); Qout = TRUE; outstr = stropen(getparam("out"),"w"); copy_image(velptr,&outptr); } if (hasvalue("den")) { Qden = TRUE; denstr = stropen(getparam("den"),"r"); read_image(denstr,&denptr); } else if (mode==2) error("Need den="); if (hasvalue("tab")) { Qtab = TRUE; tabstr = stropen(getparam("tab"),"w"); } nrad = nemoinpd(getparam("radii"),rad,MAXRING); if (nrad < 2) error("got no rings (%d), use radii=",nrad); nring = nrad-1; if (hasvalue("center")) { if (nemoinpd(getparam("center"),center,2) != 2) error("not enuf values for center=, need 2"); xpos = center[0]; ypos = center[1]; } else { xpos = (Nx(velptr)-1.0)/2.0; ypos = (Ny(velptr)-1.0)/2.0; } pa = getdparam("pa"); inc = getdparam("inc"); vsys = getdparam("vsys"); undf = getdparam("blank"); frang = getdparam("frang"); cospa = cos(pa*PI/180.0); sinpa = sin(pa*PI/180.0); sini = sin(inc*PI/180.0); cosi = cos(inc*PI/180.0); costmin = sin(frang*PI/180.0); sincosi = sini*cosi; cos2i = cosi*cosi; for (i=0; i<nring; i++) pixe[i] = vsum[i] = vsqu[i] = wsum[i] = 0; nundf = nout = nang = 0; ymin = Ymin(velptr); xmin = Xmin(velptr); dx = Dx(velptr); dx = ABS(dx); dx = -dx; dy = Dy(velptr); dy = ABS(dy); rmin = -nx*dx*10.0; rmax = 0.0; dprintf(0,"Map %d x %d pixels, size %g x %g\n", Nx(velptr), Ny(velptr), -dx*Nx(velptr), dy*Ny(velptr)); dprintf(0,"Pixel size: %g x %g\n",-dx, dy); dmin = dmax = vsys; /* loop over the map, accumulating data for fitting process */ for (j=0; j<ny; j++) { y = (j-ypos)*dy; for (i=0; i<nx; i++) { if (MapValue(velptr,i,j) == undf) { nundf++; if (Qout) MapValue(outptr,i,j) = undf; continue; } x = (i-xpos)*dx; yt = x*sinpa + y*cospa; /* major axis now along Y */ xt = x*cospa - y*sinpa; /* minor axis along X */ xt /= cosi; /* deproject to the circle */ r = sqrt(xt*xt+yt*yt); /* radius in the disk */ rmin = MIN(r,rmin); rmax = MAX(r,rmax); ir = ring_index(nrad,rad,r); dprintf(2,"r=%g ir=%d (x,y)=%g,%g (xt,yt)=%g,%g\n", r,ir,x,y,xt,yt); if (ir < 0) { nout++; continue; } cost = yt/r; dval = MapValue(velptr,i,j); if (mode==1) dval /= r; else if (mode==2) dval *= MapValue(denptr,i,j) / r; if (outptr) { if (ABS(cost) > costmin) { MapValue(outptr,i,j) = dval; if (dval > dmax) dmax = dval; if (dval < dmin) dmin = dval; } else { MapValue(outptr,i,j) = undf; } } /* now some ring accumulation, remnant of the velfit fitting */ if (ABS(cost) > costmin) { pixe[ir] += 1; wsum[ir] += 1.0; vsum[ir] += dval; vsqu[ir] += dval*dval; } else nang++; } /* i */ } /* j */ /* write output map(s), if needed */ if (Qout) { dprintf(0,"Data min/max = %g %g\n",dmin,dmax); MapMin(outptr) = dmin; MapMax(outptr) = dmax; write_image(outstr,outptr); } /* report on the rings */ if (Qtab) { fprintf(tabstr,"# r I rms I_sum1 I_sum2 i_ring Npoints\n"); fsum1 = 0.0; /* this will count rings with average values */ fsum2 = 0.0; /* this will count up flux whenever it fell in a ring */ nsum = 0; for (i=0; i<nring; i++) { if (wsum[i] == 0.0) continue; nsum++; r = 0.5*(rad[i] + rad[i+1]); dr = rad[i+1] - rad[i]; area = PI*(sqr(rad[i+1]) - sqr(rad[i])); ave = vsum[i]/wsum[i]; rms = vsqu[i]/wsum[i]-ave*ave; if (rms < 0) rms=0.0; rms = sqrt(rms); fsum1 += ave*area; fsum2 += vsum[i]; fprintf(tabstr,"%g %g %g %g %g %d %d\n", r,ave,rms,fsum1,fsum2,i+1,pixe[i]); } } dprintf(0,"Nundf=%d/%d Nout=%d Nang=%d (sum=%d)\n", nundf,nx*ny,nout,nang,nout+nundf+nang); dprintf(0,"Rmin/max = %g %g\n",rmin,rmax); }
int main(int argc, char **argv) { int i; char *input, *output, *last; FILE *inf, *otf; char buf[BUF_SIZE]; input = NULL; last = NULL; output = NULL; for (i = 1; i < argc; i++) { if (last != NULL) { if (strcmp(last, "-o") == 0) { output = argv[i]; last = NULL; } else if (strcmp(last, "-i") == 0) { input = argv[i]; last = NULL; } } else last = argv[i]; } if ((input == NULL) || (output == NULL)) { fprintf(stderr, "Specify both -i and -o\n"); return 1; } inf = fopen(input, "r"); otf = fopen(output, "w"); while (fgets(buf, BUF_SIZE, inf) != NULL) { if (strncmp(buf, "\\documentclass{", strlen("\\documentclass{")) == 0) continue; else if (strncmp(buf, "\\usepackage{", strlen("\\usepackage{")) == 0) continue; else if (strncmp(buf, "\\begin{docume", strlen("\\begin{docume")) == 0) continue; else if (strncmp(buf, "\\end{document", strlen("\\end{document")) == 0) continue; else if (string_index(buf, "\\includegraphics") != -1) { size_t index; /* Removes all the optional agruments */ index = string_index(buf, "\\includegraphics"); index += strlen("\\includegraphics"); while ((buf[index] != '}') && (buf[index] != '\0')) index++; /* Ensures that the code is properly formed */ if (buf[index] == '\0') { fprintf(stderr, "Bad includegraphics\n"); return 1; } /* Appends ".pdf" to the filename */ if (fwrite(buf, 1, index, otf) != index) abort(); fputs(".pdf", otf); fputs(buf + index, otf); } else fputs(buf, otf); } fclose(inf); fclose(otf); return 0; }
void process(struct processor *p_uncast, const char *filename, struct stack *s, struct makefile *m) { struct processor_pdfcopy *p; void *c; int cachedir_index; char *cachedir; char *infile; char *procfile; /* We need access to the real structure, get it safely */ p = talloc_get_type(p_uncast, struct processor_pdfcopy); /* Makes a new context */ c = talloc_new(p); /* Finds the original filename */ cachedir_index = string_index(filename, ".tek_cache/"); if (cachedir_index == -1) { fprintf(stderr, "Bad cachedir for image\n"); return; } cachedir = talloc_strdup(c, filename); cachedir[cachedir_index + strlen(".tex_cache/")] = '\0'; infile = talloc_strdup(c, filename); infile[cachedir_index] = '\0'; strcat(infile, filename + strlen(cachedir)); infile[strlen(infile) - 4] = '\0'; TALLOC_FREE(cachedir); cachedir = talloc_strndup(c, filename, basename_len(filename)); /* Figure out if we're supposed to copy the file or we're supposed * to generate it. */ procfile = talloc_asprintf(c, "%s.proc", infile); if (access(procfile, X_OK) == 0) { /* Generate the file manually. */ makefile_create_target(m, filename); makefile_start_deps(m); makefile_add_dep(m, procfile); makefile_end_deps(m); makefile_start_cmds(m); makefile_nam_cmd(m, "echo -e \"PDFGEN\\t%s\"", infile); makefile_add_cmd(m, "mkdir -p \"%s\" >& /dev/null || true", cachedir); makefile_add_cmd(m, "./\"%s\" > \"%s\"", procfile, filename); makefile_end_cmds(m); } else { /* Just copy the file over. */ makefile_create_target(m, filename); makefile_start_deps(m); makefile_add_dep(m, infile); makefile_end_deps(m); makefile_start_cmds(m); makefile_nam_cmd(m, "echo -e \"PDFCOPY\\t%s\"", infile); makefile_add_cmd(m, "mkdir -p \"%s\" >& /dev/null || true", cachedir); makefile_add_cmd(m, "cp \"%s\" \"%s\" >& /dev/null", infile, filename); makefile_end_cmds(m); } /* Cleans up all the memory allocated by this code. */ TALLOC_FREE(c); }