char * PyOS_StdioReadline(char *prompt) { size_t n; char *p; n = 100; if ((p = PyMem_MALLOC(n)) == NULL) return NULL; fflush(stdout); #ifndef RISCOS if (prompt) fprintf(stderr, "%s", prompt); #else if (prompt) { if(Py_RISCOSWimpFlag) fprintf(stderr, "\x0cr%s\x0c", prompt); else fprintf(stderr, "%s", prompt); } #endif fflush(stderr); switch (my_fgets(p, (int)n, stdin)) { case 0: /* Normal case */ break; case 1: /* Interrupt */ PyMem_FREE(p); return NULL; case -1: /* EOF */ case -2: /* Error */ default: /* Shouldn't happen */ *p = '\0'; break; } #ifdef MPW /* Hack for MPW C where the prompt comes right back in the input */ /* XXX (Actually this would be rather nice on most systems...) */ n = strlen(prompt); if (strncmp(p, prompt, n) == 0) memmove(p, p + n, strlen(p) - n + 1); #endif n = strlen(p); while (n > 0 && p[n-1] != '\n') { size_t incr = n+2; p = PyMem_REALLOC(p, n + incr); if (p == NULL) return NULL; if (incr > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "input line too long"); } if (my_fgets(p+n, (int)incr, stdin) != 0) break; n += strlen(p+n); } return PyMem_REALLOC(p, n+1); }
char * PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) { size_t n; char *p, *pr; n = 100; if ((p = (char *)PyMem_MALLOC(n)) == NULL) return NULL; fflush(sys_stdout); if (prompt) fprintf(stderr, "%s", prompt); fflush(stderr); switch (my_fgets(p, (int)n, sys_stdin)) { case 0: /* Normal case */ break; case 1: /* Interrupt */ PyMem_FREE(p); return NULL; case -1: /* EOF */ case -2: /* Error */ default: /* Shouldn't happen */ *p = '\0'; break; } n = strlen(p); while (n > 0 && p[n-1] != '\n') { size_t incr = n+2; if (incr > INT_MAX) { PyMem_FREE(p); PyErr_SetString(PyExc_OverflowError, "input line too long"); return NULL; } pr = (char *)PyMem_REALLOC(p, n + incr); if (pr == NULL) { PyMem_FREE(p); PyErr_NoMemory(); return NULL; } p = pr; if (my_fgets(p+n, (int)incr, sys_stdin) != 0) break; n += strlen(p+n); } pr = (char *)PyMem_REALLOC(p, n+1); if (pr == NULL) { PyMem_FREE(p); PyErr_NoMemory(); return NULL; } return pr; }
char * PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) { size_t n; char *p; n = 100; if ((p = (char *)PyMem_MALLOC(n)) == NULL) return NULL; fflush(sys_stdout); #ifndef RISCOS if (prompt) fprintf(stderr, "%s", prompt); #else if (prompt) { if(Py_RISCOSWimpFlag) fprintf(stderr, "\x0cr%s\x0c", prompt); else fprintf(stderr, "%s", prompt); } #endif fflush(stderr); switch (my_fgets(p, (int)n, sys_stdin)) { case 0: /* Normal case */ break; case 1: /* Interrupt */ PyMem_FREE(p); return NULL; case -1: /* EOF */ case -2: /* Error */ default: /* Shouldn't happen */ *p = '\0'; break; } n = strlen(p); while (n > 0 && p[n-1] != '\n') { size_t incr = n+2; p = (char *)PyMem_REALLOC(p, n + incr); if (p == NULL) return NULL; if (incr > INT_MAX) { PyErr_SetString(PyExc_OverflowError, "input line too long"); } if (my_fgets(p+n, (int)incr, sys_stdin) != 0) break; n += strlen(p+n); } return (char *)PyMem_REALLOC(p, n+1); }
DEFINEFN void vinfo_array_shrink(PsycoObject* po, vinfo_t* vi, int ncount) { vinfo_array_t* array = vi->array; int i = array->count; if (i <= ncount) return; while (i > ncount) { vinfo_t* v1 = array->items[--i]; if (v1 != NULL) { array->items[i] = NULL; vinfo_decref(v1, po); } } if (ncount == 0) array = NullArray; else { array = PyMem_REALLOC(array, sizeof(int) + ncount * sizeof(vinfo_t*)); if (array == NULL) OUT_OF_MEMORY(); array->count = ncount; } vi->array = array; }
int addlabel(labellist *ll, int type, const unsigned char *str, size_t str_length) { int i; label *lb; for (i = 0; i < ll->ll_nlabels; i++) { if (ll->ll_label[i].lb_type == type && ll->ll_label[i].lb_str_length == str_length && memcmp(ll->ll_label[i].lb_str, str, str_length) == 0) return i; } ll->ll_label = (label *) PyMem_REALLOC( ll->ll_label, sizeof(label) * (ll->ll_nlabels + 1) ); if (ll->ll_label == NULL) Py_FatalError("no mem to resize labellist in addlabel"); lb = &ll->ll_label[ll->ll_nlabels++]; lb->lb_type = type; lb->lb_str = str; lb->lb_str_length = str_length; return lb - ll->ll_label; }
DEFINEFN vinfo_array_t* array_grow1(vinfo_array_t* array, int ncount) { int i = array->count; extra_assert(ncount > i); if (i == 0) array = PyMem_MALLOC(sizeof(int) + ncount * sizeof(vinfo_t*)); else array = PyMem_REALLOC(array, sizeof(int) + ncount * sizeof(vinfo_t*)); if (array == NULL) OUT_OF_MEMORY(); array->count = ncount; while (i<ncount) array->items[i++] = NULL; return array; }
void addarc(dfa *d, int from, int to, int lbl) { state *s; arc *a; assert(0 <= from && from < d->d_nstates); assert(0 <= to && to < d->d_nstates); s = &d->d_state[from]; s->s_arc = (arc *)PyMem_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1)); if (s->s_arc == NULL) Py_FatalError("no mem to resize arc list in addarc"); a = &s->s_arc[s->s_narcs++]; a->a_lbl = lbl; a->a_arrow = to; }
int addstate(dfa *d) { state *s; d->d_state = (state *) PyMem_REALLOC(d->d_state, sizeof(state) * (d->d_nstates + 1)); if (d->d_state == NULL) Py_FatalError("no mem to resize state in addstate"); s = &d->d_state[d->d_nstates++]; s->s_narcs = 0; s->s_arc = NULL; s->s_lower = 0; s->s_upper = 0; s->s_accel = NULL; s->s_accept = 0; return s - d->d_state; }
char * PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) { size_t n = 1000; char *p = (char *)PyMem_MALLOC(n); char *q; if (p == NULL) return NULL; fprintf(stderr, "%s", prompt); q = fgets(p, n, sys_stdin); if (q == NULL) { *p = '\0'; return p; } n = strlen(p); if (n > 0 && p[n-1] != '\n') p[n-1] = '\n'; return (char *)PyMem_REALLOC(p, n+1); }
dfa * adddfa(grammar *g, int type, const unsigned char *name, size_t name_length) { dfa *d; g->g_dfa = (dfa *) PyMem_REALLOC( g->g_dfa, sizeof(dfa) * (g->g_ndfas + 1) ); if (g->g_dfa == NULL) Py_FatalError("no mem to resize dfa in adddfa"); d = &g->g_dfa[g->g_ndfas++]; d->d_type = type; d->d_name = name; d->d_name_length = name_length; d->d_nstates = 0; d->d_state = NULL; d->d_initial = -1; d->d_first = NULL; return d; /* Only use while fresh! */ }
static char * translate_newlines(const char *s, int exec_input, struct tok_state *tok) { int skip_next_lf = 0; size_t needed_length = strlen(s) + 2, final_length; char *buf, *current; char c = '\0'; buf = PyMem_MALLOC(needed_length); if (buf == NULL) { tok->done = E_NOMEM; return NULL; } for (current = buf; *s; s++, current++) { c = *s; if (skip_next_lf) { skip_next_lf = 0; if (c == '\n') { c = *++s; if (!c) break; } } if (c == '\r') { skip_next_lf = 1; c = '\n'; } *current = c; } /* If this is exec input, add a newline to the end of the string if there isn't one already. */ if (exec_input && c != '\n') { *current = '\n'; current++; } *current = '\0'; final_length = current - buf + 1; if (final_length < needed_length && final_length) /* should never fail */ buf = PyMem_REALLOC(buf, final_length); return buf; }
static int tok_nextc(register struct tok_state *tok) { for (;;) { if (tok->cur != tok->inp) { return Py_CHARMASK(*tok->cur++); /* Fast path */ } if (tok->done != E_OK) return EOF; if (tok->fp == NULL) { char *end = strchr(tok->inp, '\n'); if (end != NULL) end++; else { end = strchr(tok->inp, '\0'); if (end == tok->inp) { tok->done = E_EOF; return EOF; } } if (tok->start == NULL) tok->buf = tok->cur; tok->line_start = tok->cur; tok->lineno++; tok->inp = end; return Py_CHARMASK(*tok->cur++); } if (tok->prompt != NULL) { char *newtok = PyOS_Readline(stdin, stdout, tok->prompt); if (tok->nextprompt != NULL) tok->prompt = tok->nextprompt; if (newtok == NULL) tok->done = E_INTR; else if (*newtok == '\0') { PyMem_FREE(newtok); tok->done = E_EOF; } #if !defined(PGEN) && defined(Py_USING_UNICODE) else if (tok_stdin_decode(tok, &newtok) != 0) PyMem_FREE(newtok); #endif else if (tok->start != NULL) { size_t start = tok->start - tok->buf; size_t oldlen = tok->cur - tok->buf; size_t newlen = oldlen + strlen(newtok); char *buf = tok->buf; buf = (char *)PyMem_REALLOC(buf, newlen+1); tok->lineno++; if (buf == NULL) { PyMem_FREE(tok->buf); tok->buf = NULL; PyMem_FREE(newtok); tok->done = E_NOMEM; return EOF; } tok->buf = buf; tok->cur = tok->buf + oldlen; tok->line_start = tok->cur; strcpy(tok->buf + oldlen, newtok); PyMem_FREE(newtok); tok->inp = tok->buf + newlen; tok->end = tok->inp + 1; tok->start = tok->buf + start; } else { tok->lineno++; if (tok->buf != NULL) PyMem_FREE(tok->buf); tok->buf = newtok; tok->line_start = tok->buf; tok->cur = tok->buf; tok->line_start = tok->buf; tok->inp = strchr(tok->buf, '\0'); tok->end = tok->inp + 1; } } else { int done = 0; Py_ssize_t cur = 0; char *pt; if (tok->start == NULL) { if (tok->buf == NULL) { tok->buf = (char *) PyMem_MALLOC(BUFSIZ); if (tok->buf == NULL) { tok->done = E_NOMEM; return EOF; } tok->end = tok->buf + BUFSIZ; } if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf), tok) == NULL) { tok->done = E_EOF; done = 1; } else { tok->done = E_OK; tok->inp = strchr(tok->buf, '\0'); done = tok->inp[-1] == '\n'; } } else { cur = tok->cur - tok->buf; if (decoding_feof(tok)) { tok->done = E_EOF; done = 1; } else tok->done = E_OK; } tok->lineno++; /* Read until '\n' or EOF */ while (!done) { Py_ssize_t curstart = tok->start == NULL ? -1 : tok->start - tok->buf; Py_ssize_t curvalid = tok->inp - tok->buf; Py_ssize_t newsize = curvalid + BUFSIZ; char *newbuf = tok->buf; newbuf = (char *)PyMem_REALLOC(newbuf, newsize); if (newbuf == NULL) { tok->done = E_NOMEM; tok->cur = tok->inp; return EOF; } tok->buf = newbuf; tok->inp = tok->buf + curvalid; tok->end = tok->buf + newsize; tok->start = curstart < 0 ? NULL : tok->buf + curstart; if (decoding_fgets(tok->inp, (int)(tok->end - tok->inp), tok) == NULL) { /* Break out early on decoding errors, as tok->buf will be NULL */ if (tok->decoding_erred) return EOF; /* Last line does not end in \n, fake one */ strcpy(tok->inp, "\n"); } tok->inp = strchr(tok->inp, '\0'); done = tok->inp[-1] == '\n'; } if (tok->buf != NULL) { tok->cur = tok->buf + cur; tok->line_start = tok->cur; /* replace "\r\n" with "\n" */ /* For Mac leave the \r, giving a syntax error */ pt = tok->inp - 2; if (pt >= tok->buf && *pt == '\r') { *pt++ = '\n'; *pt = '\0'; tok->inp = pt; } } } if (tok->done != E_OK) { if (tok->prompt != NULL) PySys_WriteStderr("\n"); tok->cur = tok->inp; return EOF; } } /*NOTREACHED*/ }
void * PyMem_Realloc(void *p, size_t nbytes) { /* See comment near MALLOC_ZERO_RETURNS_NULL in pyport.h. */ return PyMem_REALLOC(p, nbytes ? nbytes : 1); }
void * PyMem_Realloc(void *p, size_t nbytes) { return PyMem_REALLOC(p, nbytes); }
static void calcfirstset(grammar *g, dfa *d) { int i, j; state *s; arc *a; int nsyms; int *sym; int nbits; static bitset dummy; bitset result; int type; dfa *d1; label *l0; #ifndef NDEBUG printf("Calculate FIRST set for '%.*s'\n", (int)d->d_name_length, d->d_name); #endif if (dummy == NULL) dummy = newbitset(1); if (d->d_first == dummy) { fprintf(stderr, "Left-recursion for '%.*s'\n", (int)d->d_name_length, d->d_name); return; } if (d->d_first != NULL) { fprintf(stderr, "Re-calculating FIRST set for '%.*s' ???\n", (int)d->d_name_length, d->d_name); } d->d_first = dummy; l0 = g->g_ll.ll_label; nbits = g->g_ll.ll_nlabels; result = newbitset(nbits); sym = (int *)PyMem_MALLOC(sizeof(int)); if (sym == NULL) Py_FatalError("no mem for new sym in calcfirstset"); nsyms = 1; sym[0] = findlabel(&g->g_ll, d->d_type, UC(NULL), 0); s = &d->d_state[d->d_initial]; for (i = 0; i < s->s_narcs; i++) { a = &s->s_arc[i]; for (j = 0; j < nsyms; j++) { if (sym[j] == a->a_lbl) break; } if (j >= nsyms) { /* New label */ sym = (int *)PyMem_REALLOC(sym, sizeof(int) * (nsyms + 1)); if (sym == NULL) Py_FatalError( "no mem to resize sym in calcfirstset"); sym[nsyms++] = a->a_lbl; type = l0[a->a_lbl].lb_type; if (ISNONTERMINAL(type)) { d1 = PyGrammar_FindDFA(g, type); if (d1->d_first == dummy) { fprintf(stderr, "Left-recursion below '%.*s'\n", (int)d->d_name_length, d->d_name); } else { if (d1->d_first == NULL) calcfirstset(g, d1); mergebitset(result, d1->d_first, nbits); } } else if (ISTERMINAL(type)) { addbit(result, a->a_lbl); } } } d->d_first = result; PyMem_FREE(sym); }