// Try, very hard, to put everything in the multicol into two columns // so that the total height is at most htavail. void multicol::compose(int defonly) { int i; stream cd; if (!nonempty()) { setheight(0); return; } scratch.freeall(); // fill scratch with everything destined // for either column for (cd = definite; cd.more(); cd.advance()) scratch.append(cd.current()); if (!defonly) for (cd = *(currpage->stage); cd.more(); cd.advance()) if (cd.current()->numcol() == 2) scratch.append(cd.current()); scratch.restoreall(); // in particular, floatables' goals int rawht = scratch.rawht(); int halfheight = (int)(coltol*rawht); // choose a goal height int maxht = defonly ? halfheight : htavail; secondtry: for (i = 0; i < 2; i++) column[i].freeall(); leftblocked = 0; cd = scratch; while (cd.more()) { queue ministage; // for the minimally acceptable chunks ministage.freeall(); // that are to be added to either column while (cd.more() && !cd.current()->issentinel()) { ministage.enqueue(cd.current()); cd.advance(); } choosecol(&ministage, maxht); if (cd.more() && cd.current()->issentinel()) cd.advance(); // past sentinel } if (height() > htavail && maxht != htavail) { // We tried to balance the columns, but // the result was too tall. Go back // and try again with the less ambitious // goal of fitting the space available. maxht = htavail; goto secondtry; } for (i = 0; i < 2; i++) { movefloats(&(column[i]), ((double) column[i].rawht())/currpage->pagesize); trimspace(&(column[i])); } if (dbg & 32) { printf("#multicol::compose: htavail %d maxht %d dv %d\n", htavail, maxht, height()); dump(); } if (defonly) stretch(height()); }
// Report an upper bound on how tall the current two-column object is. // The (possibly composed) heights of the two columns give a crude upper // bound on the total height. If the result is more than the height // available for the two-column object, then the columns are each // composed to give a better estimate of their heights. int multicol::height() { int retval = max(column[0].height(), column[1].height()); if (retval < htavail) return retval; for (int i = 0; i < 2; i++) { movefloats(&(column[i]), ((double) column[i].height())/currpage->pagesize); trimspace(&(column[i])); } return max(column[0].height(), column[1].height()); }
// If r were added to s, would the height of the composed result be at most maxht? int wouldfit(range *r, stream *s, int maxht) { if (r->rawht() + s->rawht() <= maxht) return 1; // the conservative test succeeded stream scratch; // local playground for costly test for (stream cd = *s; cd.more(); cd.advance()) scratch.append(cd.current()); scratch.append(r); movefloats(&scratch, ((double) scratch.rawht())/maxht); trimspace(&scratch); int retval = scratch.height() <= maxht; scratch.freeall(); return retval; }
// If s1 were added to s, would the height of the composed result be at most maxht? // The computational structure is similar to that above. int wouldfit(stream *s1, stream *s, int maxht) { stream cd; if (s1->rawht() + s->rawht() <= maxht) return 1; stream scratch; for (cd = *s; cd.more(); cd.advance()) scratch.append(cd.current()); for (cd = *s1; cd.more(); cd.advance()) scratch.append(cd.current()); movefloats(&scratch, ((double) scratch.rawht())/maxht); trimspace(&scratch); int retval = scratch.height() <= maxht; scratch.freeall(); return retval; }
int _readline(struct rls *rls) { int flags; struct termios termios; int i, j; int fkey; char *kb; int ihist; int rc; char **tab; char *p; int off = 0; if (isatty(rls->fdin)) set_terminal(rls->fdin, &termios); memset(rls->kbuffer, 0, rls->maxbuflen); rls->pos = 0; /* flags = fcntl(0, F_GETFL); fcntl(0, F_SETFL, flags); */ flags = 0; ihist = 0; kb = rls->kb; rc = 0; do { if (off >= rc) { memset(kb, 0, sizeof(rls->kb)); rc = read(rls->fdin, kb, sizeof(rls->kb)); if (rc <= 0) { //usleep(1); continue; } off = 0; } else { memmove(kb, kb + off, rc - off); rc -= off; } #if 0 printf("\n%2.2x - %2.2x - %2.2x - %2.2x - %2.2x - %2.2x - %2.2x - %2.2x\n", kb[0], kb[1], kb[2], kb[3], kb[4], kb[5], kb[6], kb[6]); fflush(stdout); #endif if (kb[0] == ESC && kb[1] == ESC_PAD) { fkey = kb[2] | (kb[3] << 8); if (kb[2] == KEY_UP) { off = 3; if (flags == 0) { /* set history-mode */ flags = 1; ihist = rls->hist_total; if (rls->pos != 0) strcpy(rls->history[rls->maxhistnum], rls->kbuffer); else rls->history[rls->maxhistnum][0] = '\0'; } if (ihist == 0) continue; i = findhistory(rls, 0 - ihist); if (i == -1) continue; ihist = i; /* while (rls->pos-- > 0) */ i = strlen(rls->kbuffer); while (rls->pos++ < i) vprint(rls->fdout, " ", 1); while (i-- > 0) vprint(rls->fdout, "\b \b", 3); memset(rls->kbuffer, 0, rls->maxbuflen); strcpy(rls->kbuffer, rls->history[ihist]); rls->pos = strlen(rls->kbuffer); vprint(rls->fdout, rls->kbuffer, rls->pos); continue; } if (kb[2] == KEY_DOWN){ off = 3; if (flags == 0) continue; if ((ihist + 1) >= rls->hist_total) { i = strlen(rls->kbuffer); while (rls->pos++ < i) vprint(rls->fdout, " ", 1); while (i-- > 0) vprint(rls->fdout, "\b \b", 3); memset(rls->kbuffer, 0, rls->maxbuflen); rls->pos = 0; flags = 0; continue; } //printf("ihist = %d, rls->hist_total = %d\n", // ihist, rls->hist_total); i = findhistory(rls, ihist); if (i == -1) continue; ihist = i; i = strlen(rls->kbuffer); while (rls->pos++ < i) vprint(rls->fdout, " ", 1); while (i-- > 0) vprint(rls->fdout, "\b \b", 3); memset(rls->kbuffer, 0, rls->maxbuflen); strcpy(rls->kbuffer, rls->history[ihist]); rls->pos = strlen(rls->kbuffer); vprint(rls->fdout, rls->kbuffer, rls->pos); continue; } if (kb[2] == KEY_RIGHT) { off = 3; if (rls->pos < strlen(rls->kbuffer)) vprint(rls->fdout, &(rls->kbuffer[rls->pos++]), 1); continue; } if (kb[2] == KEY_LEFT) { off = 3; if (rls->pos > 0) { vprint(rls->fdout, "\b", 1); rls->pos --; } continue; } if (fkey == KEY_HOME) { off = 4; while (rls->pos > 0) { vprint(rls->fdout, "\b", 1); rls->pos --; } continue; } if (fkey == KEY_END) { off = 4; while (rls->pos < strlen(rls->kbuffer)) { vprint(rls->fdout, &(rls->kbuffer[rls->pos++]), 1); } continue; } } flags = 0; /* 'enter' */ if (kb[0] == LF || kb[0] == CR) { off = 1; trimspace(rls->kbuffer); rls->pos = strlen(rls->kbuffer); if (rls->pos == 0) break; if (rls->hist_total == rls->maxhistnum) { memmove(rls->history[0], rls->history[1], rls->maxbuflen * (rls->maxhistnum - 1)); rls->hist_total--; } strcpy(rls->history[rls->hist_total++], rls->kbuffer); break; } if (kb[0] == CTRLC) { off = 1; rls->pos = 0; rls->kbuffer[0] = '\0'; break; } if (kb[0] == '\t') { off = 1; if (rls->tab_callback == NULL) continue; p= NULL; if (rls->pos != 0) { p = rls->kbuffer + rls->pos; while (p > rls->kbuffer) { if (*(p - 1) == ' ') break; p--; } } tab = rls->tab_callback(rls->kbuffer, p); if (tab == NULL) continue; /* only one */ if (*tab != NULL && *(tab + 1) == NULL) { for (i = 0; i < strlen(p); i++) vprint(rls->fdout, "\b \b", 3); i = strlen(*tab); vprint(rls->fdout, *tab, i); if (p - rls->kbuffer + i < rls->maxbuflen) { strcpy(p, *tab); rls->pos = strlen(rls->kbuffer); } free(*tab); free(tab); continue; } /* more than one */ vprint(rls->fdout, "\n", 1); i = 0; while (*(tab + i)) { vprint(rls->fdout, *(tab + i), strlen(*(tab + i))); vprint(rls->fdout, " ", 1); free(*(tab + i)); i++; } vprint(rls->fdout, "\n", 1); free(tab); vprint(rls->fdout, rls->prompt, strlen(rls->prompt)); vprint(rls->fdout, rls->kbuffer, rls->pos); continue; } /* backspace */ if ((kb[0] == BACKSP0)|| (kb[0] == BACKSP1)) { off = 1; if (rls->pos > 0) { i = strlen(rls->kbuffer); j = rls->pos; while (j < i) { rls->kbuffer[j - 1] = rls->kbuffer[j]; j++; } rls->kbuffer[j - 1] = '\0'; rls->pos--; vprint(rls->fdout, "\b", 1); vprint(rls->fdout, &rls->kbuffer[rls->pos], strlen(&rls->kbuffer[rls->pos])); vprint(rls->fdout, " \b", 2); for (i = 0; i < strlen(rls->kbuffer) - rls->pos; i++) vprint(rls->fdout, "\b", 1); } continue; } /* normal key */ off = 1; i = kb[0]; if (isprint(i)) { if (rls->pos < strlen(rls->kbuffer) - 1) { j = strlen(rls->kbuffer); /* avoid overflow */ if (j < rls->maxbuflen - 1) { while (j > rls->pos) { rls->kbuffer[j] = rls->kbuffer[j-1]; j--; } rls->kbuffer[rls->pos] = kb[0]; //rls->kbuffer[rls->pos + 1] = '\0'; vprint(rls->fdout, &rls->kbuffer[rls->pos], strlen(&rls->kbuffer[rls->pos])); for (j = 0; j < strlen(rls->kbuffer) - rls->pos - 1; j++) vprint(rls->fdout, "\b", 1); } } else { rls->kbuffer[rls->pos] = kb[0]; rls->kbuffer[rls->pos + 1] = '\0'; vprint(rls->fdout, &kb[0], 1); } rls->pos++; } } while (kb[0] != CTRLP); if (isatty(rls->fdin)) reset_terminal(rls->fdin, &termios); return (rls->pos > 0 ? 1 : 0); }
HEGGSERVICECLIENTCONFIG eggServiceClientConfig_new(char *configfile) { char *socketfile = NULL; unsigned short port = 0; char *ip = NULL; unsigned long n_line_sz = 0; char* lp_line_str = EGG_NULL; FILE *fp; if (!(fp = fopen(configfile, "r"))) { /* fprintf(stderr, "%s:%d:%s: ERR read [%s] %s\n", */ /* __FILE__, __LINE__, __func__, configfile, strerror(errno)); */ eggPrtLog_error("eggServiceClientConfig", "%s:%d:%s: ERR read [%s] %s\n", __FILE__, __LINE__, __func__, configfile, strerror(errno)); return NULL; } while(getline(&lp_line_str, &n_line_sz, fp) != -1) { trimspace(lp_line_str); if (strncasecmp(lp_line_str, "port=", 5) == 0) { long prt; prt = strtol(lp_line_str+5, NULL, 10); if (prt > 0) { port = prt; } } else if (strncasecmp(lp_line_str, "ip=", 3) == 0) { free(ip); ip = strdup(lp_line_str+3); assert(ip); } else if (strncasecmp(lp_line_str, "socket=", 7) == 0) { free(socketfile); socketfile = strdup(lp_line_str+7); assert(socketfile); } else { ; } } free(lp_line_str); fclose(fp); if (!(port != 0 && ip && ip[0])) { free(ip); ip = NULL; port = 0; } if (port == 0 && socketfile == NULL) { /* fprintf(stderr, "%s:%d:%s NO port Or socketfile\n", */ /* __FILE__, __LINE__, __func__); */ eggPrtLog_error("eggServiceClientConfig", "%s:%d:%s NO port Or socketfile\n", __FILE__, __LINE__, __func__); return NULL; } HEGGSERVICECLIENTCONFIG hEggServiceClientConfig; hEggServiceClientConfig = (HEGGSERVICECLIENTCONFIG) malloc(sizeof(eggServiceClientConfig)); hEggServiceClientConfig->socketfile = socketfile; hEggServiceClientConfig->port = port; hEggServiceClientConfig->ip = ip; return hEggServiceClientConfig; }