void _pf_init_args(int argc, char **argv, _pf_String *retProg, _pf_Array *retArgs, char *environ[]) /* Set up command line arguments. */ { _pf_Array args; int stringTypeId = _pf_find_string_type_id(); int i; *retProg = _pf_string_from_const(argv[0]); struct _pf_string **a; args = _pf_dim_array(argc-1, stringTypeId); a = (struct _pf_string **)(args->elements); for (i=1; i<argc; ++i) a[i-1] = _pf_string_from_const(argv[i]); *retArgs = args; cl_env = environ; }
void pf_keyIn(_pf_Stack *stack) /* Get next key press. */ { char buf[2]; buf[0] = rawKeyIn(); buf[1] = 0; stack[0].String = _pf_string_from_const(buf); }
void pf_lineIn(_pf_Stack *stack) /* Get next line of input from stdin. In general * you do not want to mix calls to keyIn and lineIn, * since keyIn is unbuffered and lineIn is buffered. * Returns nil at EOF. */ { char buf[256]; int i, c; FILE *f = stdin; for (i=0; i<sizeof(buf)-1; ++i) { c = fgetc(f); if (c < 0) { if (i == 0) stack[0].String = NULL; else { buf[i] = 0; stack[0].String = _pf_string_from_const(buf); } return; } buf[i] = c; if (c == '\n') { buf[i] = 0; stack[0].String = _pf_string_dupe(buf, i); return; } } /* Well, looks like it's a pretty long string! * Let's convert to dyString based capture rather * than using the fixed size buffer on stack. */ { struct dyString *dy = dyStringNew(512); struct _pf_string *string; dyStringAppendN(dy, buf, i); for (;;) { c = fgetc(f); if (c < 0) break; dyStringAppendC(dy, c); if (c == '\n') break; } string = _pf_string_new(dy->string, dy->bufSize); string->size = dy->stringSize; dyStringCannibalize(&dy); stack[0].String = string; } }
void _pf_cm_seriousError_asString(_pf_Stack *stack) /* Get error message. */ { struct _pf_error *err = stack[0].v; struct dyString *dy = dyStringNew(0); while (err != NULL) { dyStringAppend(dy, err->message->s); err = err->next; if (err != NULL) dyStringAppendC(dy, '\n'); } stack[0].String = _pf_string_from_const(dy->string); dyStringFree(&dy); }
void _pf_cm_file_readLine(_pf_Stack *stack) /* Read next line from file. */ { struct file *file = stack[0].v; FILE *f = file->f; struct dyString *dy = file->dy; int c; dyStringClear(dy); for (;;) { c = fgetc(f); if (c == EOF) break; dyStringAppendC(dy, c); if (c == '\n') break; } stack[0].String = _pf_string_from_const(dy->string); }
void pf_getEnvArray(_pf_Stack *stack) /* Return an array of string containing the environment * int var=val format. We'll leave it to paraFlow * library to turn this into a hash. */ { int stringTypeId = _pf_find_string_type_id(); int envSize = 0; int i; struct _pf_string **a; _pf_Array envArray; for (i=0; ; ++i) { if (cl_env[i] == NULL) break; envSize += 1; } envArray = _pf_dim_array(envSize, stringTypeId); a = (struct _pf_string **)(envArray->elements); for (i=0; i<envSize; ++i) a[i] = _pf_string_from_const(cl_env[i]); stack[0].Array = envArray; }
static struct _pf_error *errorNewFromC(char *message, char *source, int code) /* Create error object around C style strings. */ { return errorNew(_pf_string_from_const(message), _pf_string_from_const(source), code); }