static corto_ll cortotool_waitForChanges(corto_pid pid, corto_ll files, corto_ll changed) { corto_int32 i = 0; if (changed) { corto_ll_free(changed); changed = NULL; } do { corto_sleep(0, 50000000); /* Check if process is still running */ if (pid) { if ((retcode = corto_proccheck(pid, NULL))) { break; } } i++; /* Only check files every second */ if (!(i % 20)) { changed = cortotool_getModified(files, changed); } else { continue; } }while (!changed || (pid && retcode)); return changed; }
static corto_ll cortotool_getModified(corto_ll files, corto_ll changed) { corto_int32 i = 0; corto_ll libs = NULL; if (changed) { corto_ll_free(changed); changed = NULL; } if (files) { corto_iter iter = corto_ll_iter(files); while (corto_iter_hasNext(&iter)) { struct stat attr; corto_fileMonitor *mon = corto_iter_next(&iter); if (stat(mon->file, &attr) < 0) { printf("corto: failed to stat '%s' (%s)\n", mon->file, strerror(errno)); } if (mon->mtime) { if (mon->mtime != attr.st_mtime) { cortotool_addChangedLibrary(&libs, mon->lib); } } mon->mtime = attr.st_mtime; i++; } } return libs; }
static void cdiff_file_writeElements(FILE *f, corto_ll elements) { corto_iter it = corto_ll_iter(elements); bool prevIsNl = false; while (corto_iter_hasNext(&it)) { cdiff_elem *el = corto_iter_next(&it); if (el->header) { prevIsNl = cdiff_file_writeElement(f, el->header, prevIsNl); } if (el->body) { prevIsNl = cdiff_file_writeElement(f, el->body, prevIsNl); } if (el->id) free(el->id); free(el); } corto_ll_free(elements); }
/* Deserialize string in object */ corto_string corto_string_deser(corto_string str, corto_string_deser_t* data) { corto_char *ptr; corto_bool createdNew = FALSE; { corto_id buffer; corto_char *bptr, ch; /* Parse typename that potentially precedes string */ bptr = buffer; ptr = str; while((ch = *ptr) && (ch != '{')) { if (!((ch == ' ') || (ch == '\n') || (ch == '\t'))) { *bptr = ch; bptr++; } ptr++; } *bptr = '\0'; /* If no type is found, reset ptr */ if ((ch != '{') || (ptr == str)) { ptr = str; } else { corto_object type; /* If no out is provided create an object of the specified type */ if ((bptr != buffer)) { type = corto_resolve(NULL, buffer); if (type) { if (data->type && (type != data->type)) { corto_seterr( "type of object ('%s') does not match string ('%s')", corto_fullpath(NULL, data->type), corto_fullpath(NULL, type)); corto_release(type); goto error; } if (corto_instanceof(corto_type(corto_type_o), type)) { if (!data->out) { data->out = corto_declare(type); createdNew = TRUE; } data->type = type; } else { corto_seterr("'%s' is not a type", buffer); corto_release(type); goto error; } corto_release(type); } else { corto_seterr("unknown type '%s'", buffer); goto error; } } else { type = data->type; } if (type && (corto_type(type)->kind == CORTO_PRIMITIVE)) { ptr++; } } } data->current = 0; data->index = NULL; data->ptr = data->out; data->anonymousObjects = NULL; data->allocValue = NULL; data->allocUdata = NULL; if (data->out && data->isObject) { if (!data->type) { data->type = corto_typeof(data->out); } else if (!corto_instanceofType(corto_typeof(data->out), data->type)) { corto_seterr("object '%s' of type '%s' is not an instance of type '%s'", corto_fullpath(NULL, data->out), corto_fullpath(NULL, corto_typeof(data->out)), corto_fullpath(NULL, data->type)); goto error; } } if (!data->type) { corto_seterr("no type provided for '%s'", str); goto error; } if (data->type->kind == CORTO_PRIMITIVE) { ptr = corto_string_deserParse(ptr, NULL, data); } else { ptr = corto_string_deserParseScope(ptr, NULL, data); if (ptr) { ptr ++; } } if (!ptr) { if (!corto_lasterr()) { corto_seterr("failed to deserialize '%s'", str); } else { corto_seterr("failed to deserialize '%s': %s", str, corto_lasterr()); } goto error; } if (data->anonymousObjects) { corto_ll_free(data->anonymousObjects); } return ptr; error: if (data->out) { if (createdNew) { corto_release(data->out); } data->out = NULL; } return NULL; }
/* Free index */ void corto_string_deserFreeIndex(corto_string_deser_t* data) { if (data->index) { corto_ll_walk(data->index, corto_string_deserWalkIndex, NULL); corto_ll_free(data->index); } }
/* Find existing parts in the code that must not be overwritten. */ static corto_ll cdiff_parseLegacy(char *code) { char *ptr = NULL; corto_ll result = corto_ll_new(); ptr = code; while((ptr = strchr(ptr, '$'))) { cdiff_elemKind kind = CDIFF_TEXT; if (!memcmp(ptr, "$begin", 6)) { ptr += 6; kind = CDIFF_FUNCTION_LEGACY; } else if (!memcmp(ptr, "$header", 7)) { ptr += 7; } else { ptr ++; continue; } /* Find begin of identifier */ if (*ptr == '(') { char *endptr; /* Find end of identifier */ endptr = strstr(ptr, ") */"); if (endptr) { corto_id identifier; /* Copy identifier string */ *endptr = '\0'; endptr += 3; if (strlen(ptr) >= sizeof(corto_id)) { corto_seterr( "%s: identifier of code-snippet exceeds %d characters", sizeof(corto_id)); goto error; } strcpy(identifier, ptr + 1); ptr = endptr + 1; /* Find $end */ endptr = strstr(ptr, "$end"); endptr -= 3; /* include comment begin token */ if (endptr) { cdiff_elem* el; corto_string src; if (kind != CDIFF_TEXT) { endptr[0] = '}'; endptr[1] = '\n'; endptr[2] = '\0'; endptr += 3; } else { endptr[0] = '\n'; endptr[1] = '\0'; endptr += 2; } src = corto_strdup(ptr); if(strstr(src, "$begin")) { corto_seterr("code-snippet '%s' contains nested $begin (did you forget an $end?)", identifier); corto_dealloc(src); goto error; } el = corto_alloc(sizeof(cdiff_elem)); el->kind = kind; el->id = corto_strdup(identifier); el->header = NULL; el->body = src; corto_ll_append(result, el); ptr = endptr + 1; } else { corto_warning("generator: missing $end after $begin(%s)", identifier); } } else { corto_warning("generator: missing ')' after $begin/$header"); } } else { corto_warning("generator: missing '(' after $begin/$header"); } } return result; error: if (result) { corto_ll_free(result); } printf("error!\n"); return NULL; }