TWindow *pz_new_textview_window(char *filename) { TWindow *ret; char *buf = NULL; char tmp[4096]; FILE *fp; long len, cs; if ((fp = fopen(filename, "r")) == NULL) { pz_perror(filename); return TTK_MENU_DONOTHING; } fseek(fp, 0L, SEEK_END); len = ftell(fp); rewind(fp); if (len == 0) { cs = 0; while (fgets(tmp, 4096, fp) != NULL) { len = buf ? strlen(buf) : 0; if (len + (long)strlen(tmp) > cs) { cs += 8192; if ((buf = realloc(buf, cs)) == NULL) { pz_error(_("Memory allocation failed")); fclose(fp); return TTK_MENU_DONOTHING; } } strncpy(buf + len, tmp, cs - len); } if (buf == NULL) { pz_message(_("Empty file")); fclose(fp); return TTK_MENU_DONOTHING; } } else { cs = len; if ((buf = malloc(len + 1)) == NULL) { pz_error(_("Memory allocation failed")); fclose(fp); return TTK_MENU_DONOTHING; } while (cs > 0) cs -= fread(buf + (len - cs), 1, cs, fp); *(buf + len) = '\0'; } fclose(fp); ret = ttk_new_window(); ret->data = 0x12345678; ttk_add_widget(ret, ttk_new_textarea_widget(ret->w, ret->h, buf, ttk_textfont, ttk_text_height(ttk_textfont)+2)); ttk_window_title(ret, strrchr(filename, '/')? strrchr(filename, '/') + 1:filename); free(buf); /* strdup'd in textarea */ return ret; }
PzWindow *new_mymodule_window() { static int calledyet = 0; PzWindow *ret; FILE *fp; if (!calledyet) { calledyet++; pz_message ("Select again to see the fun stuff."); return TTK_MENU_DONOTHING; } image = ttk_load_image (pz_module_get_datapath (module, "image.png")); if (!image) { pz_error ("Could not load %s: %s", pz_module_get_datapath (module, "image.png"), strerror (errno)); return TTK_MENU_DONOTHING; } ttk_surface_get_dimen (image, &imgw, &imgh); fp = fopen (pz_module_get_datapath (module, "message.txt"), "r"); if (!fp) { pz_warning ("Could not read from %s: %s.", pz_module_get_datapath (module, "message.txt"), strerror (errno)); text = (char *)strdup ("Hi! I forgot to supply a message!"); } else { long len; fseek (fp, 0, SEEK_END); len = ftell (fp); fseek (fp, 0, SEEK_SET); text = malloc (len + 1); fread (text, 1, len, fp); text[len] = 0; } ret = pz_new_window ("MyModule", PZ_WINDOW_NORMAL); pz_add_widget (ret, draw_mymodule, event_mymodule); return pz_finish_window (ret); }
static TWindow *browser_pipe_exec (ttk_menu_item *item) { int len; char *buf = '\0'; char *execline = item->data; char st_buf[512]; FILE *fp; if((fp = popen(execline, "r")) == NULL) { pz_perror(execline); return TTK_MENU_UPONE; } len = 0; while(fgets(st_buf, 512, fp) != NULL) { buf = (char *)realloc(buf, ((buf == '\0' ? 0 : strlen(buf)) + 512) * sizeof(char)); if(buf == NULL) { pz_error(_("malloc failed")); return TTK_MENU_UPONE; } if(len == 0) { strncpy(buf, st_buf, 512); len = 1; } else strncat(buf, st_buf, 512); } pclose(fp); TWindow *ret; if(buf=='\0') { pz_message (_("No output.")); ret = TTK_MENU_DONOTHING; } else { ttk_show_window (pz_create_stringview(buf, _("Pipe Output"))); ret = TTK_MENU_REPLACE; } free(buf); return ret; }
void pz_execv(const char *path, char *const argv[]) { #ifdef IPOD static const char *const vcs[] = {"/dev/vc/%d", "/dev/tty%d", 0}; int i, tty0_fd, ttyfd, curvt, fd, status, oldvt = 0; struct vt_stat vtstate; pid_t pid; ttyfd = -1; /* open our VT */ if ((tty0_fd = open("/dev/tty0", O_WRONLY, 0)) < 0) if ((tty0_fd = open("/dev/vc/0", O_WRONLY, 0)) < 0) tty0_fd = dup(0); /* STDIN is a VT? */ /* find available VT */ ioctl(tty0_fd, VT_OPENQRY, &curvt); if (curvt <= 0) { pz_error("No available VTs."); goto err; } for (i = 0; vcs[i] && (ttyfd < 0); ++i) { char vtpath[12]; sprintf(vtpath, vcs[i], curvt); ttyfd = open(vtpath, O_RDWR); } if (ttyfd < 0) { pz_error("No available TTYs."); goto err; } /* switch to the correct vt */ if (ioctl(ttyfd, VT_GETSTATE, &vtstate) == 0) oldvt = vtstate.v_active; /* trick SDL and the kernel both at the same time! */ if (ioctl(tty0_fd, KDSETMODE, KD_TEXT) < 0) { pz_error("Can't set graphics."); goto err; } if (ioctl(ttyfd, VT_ACTIVATE, curvt)) { pz_perror("child VT_ACTIVATE"); goto err; } if (ioctl(ttyfd, VT_WAITACTIVE, curvt)) { pz_perror("child VT_WAITACTIVE"); goto err; } /* Kill MPD music player */ if (mpd_available()) kill_mpd(); switch(pid = vfork()) { case -1: /* error */ perror("vfork"); goto err; case 0: /* child */ close(tty0_fd); close(ttyfd); if(setsid() < 0) { perror("setsid"); _exit(1); } close(0); if((fd = open("/dev/console", O_RDWR)) == -1) { perror("/dev/console"); _exit(1); } if(dup(fd) == -1) { perror("stdin dup"); _exit(1); } close(1); if(dup(fd) == -1) { perror("stdout dup"); _exit(1); } close(2); if(dup(fd) == -1) { perror("stderr dup"); _exit(1); } execv(path, argv); fprintf(stderr, _("Exec failed! (Check Permissions)\n")); _exit(1); break; default: /* parent */ waitpid(pid, &status, 0); sleep(5); if (oldvt > 0) { if (ioctl(ttyfd, VT_ACTIVATE, oldvt)) { perror("parent VT_ACTIVATE"); goto err; } if(ioctl(ttyfd, VT_WAITACTIVE, oldvt)) { perror("parent VT_WAITACTIVE"); goto err; } } close(ttyfd); ttyfd = -1; if (ioctl(tty0_fd, KDSETMODE, KD_GRAPHICS) < 0) pz_error("Can't reset graphics."); if (ioctl(tty0_fd, VT_DISALLOCATE, curvt)) { perror("VT_DISALLOCATE"); // happens sometimes, no biggy goto err; } break; } /* init MPD music player */ if (mpd_available()) init_mpd(); err: close(tty0_fd); if (ttyfd >= 0) close(ttyfd); #else pz_message(argv[0]); #endif /* IPOD */ }