static int modfile_move( INSTANCE * my, int * params ) { int r = file_move( string_get( params[0] ), string_get( params[1] ) ) ; string_discard( params[1] ) ; string_discard( params[0] ) ; return r ; }
static int modregex_regex (INSTANCE * my, int * params) { const char * reg = string_get(params[0]); const char * str = string_get(params[1]); int result = -1; unsigned n; struct re_pattern_buffer pb; struct re_registers re; int start[16]; int end[16]; int * regex_reg; /* Alloc the pattern resources */ memset (&pb, 0, sizeof(pb)); memset (&re, 0, sizeof(re)); pb.buffer = malloc(4096); pb.allocated = 4096; pb.fastmap = malloc(256); pb.regs_allocated = 16; re.num_regs = 16; re.start = start; re.end = end; re_syntax_options = RE_SYNTAX_POSIX_MINIMAL_EXTENDED | REG_ICASE; /* Match the regex */ if (re_compile_pattern (reg, strlen(reg), &pb) == 0) { result = re_search (&pb, str, strlen(str), 0, strlen(str), &re); if (result != -1) { /* Fill the regex_reg global variables */ regex_reg = (int *) &GLODWORD( mod_regex, REGEX_REG); for (n = 0 ; n < 16 && n <= pb.re_nsub ; n++) { string_discard (regex_reg[n]); regex_reg[n] = string_newa (str + re.start[n], re.end[n] - re.start[n]); string_use (regex_reg[n]); } } } /* Free the resources */ free (pb.buffer); free (pb.fastmap); string_discard(params[0]); string_discard(params[1]); return result; }
void instance_destroy( INSTANCE * r ) { INSTANCE * father, * bigbro, * smallbro; int n ; LOCDWORD( r, STATUS ) = STATUS_RUNNING; if ( instance_destroy_hook_count ) for ( n = 0; n < instance_destroy_hook_count; n++ ) instance_destroy_hook_list[n]( r ); /* Actualiza la cuenta de referencia de las variables tipo string */ for ( n = 0 ; n < r->proc->string_count ; n++ ) string_discard( PRIDWORD( r, r->proc->strings[n] ) ) ; /* Strings privadas */ for ( n = 0 ; n < r->proc->pubstring_count ; n++ ) string_discard( PUBDWORD( r, r->proc->pubstrings[n] ) ) ; /* Strings publicas */ for ( n = 0 ; n < local_strings ; n++ ) string_discard( LOCDWORD( r, localstr[n] ) ) ; /* Strings locales */ /* Actualiza árbol de jerarquias */ bigbro = instance_get( LOCDWORD( r, BIGBRO ) ) ; /* Tengo hermano mayor? */ if ( bigbro ) LOCDWORD( bigbro, SMALLBRO ) = LOCDWORD( r, SMALLBRO ) ; /* El hermano menor de mi hermano mayor es mi hermano menor */ smallbro = instance_get( LOCDWORD( r, SMALLBRO ) ) ; /* Tengo hermano menor? */ if ( smallbro ) LOCDWORD( smallbro, BIGBRO ) = LOCDWORD( r, BIGBRO ) ; /* El hermano mayor de mi hermano menor es mi hermano mayor */ father = instance_get( LOCDWORD( r, FATHER ) ) ; /* Tengo padre? */ if ( father && instance_get( LOCDWORD( father, SON ) ) == r ) LOCDWORD( father, SON ) = LOCDWORD( r, BIGBRO ); /* Si tengo padre y soy el hijo menor, mi hermano mayor pasa a ser el menor hijo de mi padre */ /* Quita la instancia de la lista */ if ( r->prev ) r->prev->next = r->next ; if ( r->next ) r->next->prev = r->prev ; if ( first_instance == r ) first_instance = r->next ; /* Remove the instance from all hash lists */ instance_remove_from_list_by_id( r, LOCDWORD( r, PROCESS_ID ) ); instance_remove_from_list_by_instance( r ); instance_remove_from_list_by_type( r, LOCDWORD( r, PROCESS_TYPE ) ); instance_remove_from_list_by_priority( r ); if ( r->stack ) free( r->stack ) ; if ( r->locdata ) free( r->locdata ) ; if ( r->pubdata ) free( r->pubdata ) ; if ( r->pridata ) free( r->pridata ) ; free( r ) ; }
static int moddir_rm( INSTANCE * my, int * params ) { const char * d = string_get( params[ 0 ] ) ; int ret = dir_deletefile( d ); string_discard( params[ 0 ] ) ; return ( ret ) ; }
static int modfile_file( INSTANCE * my, int * params ) { char buffer[1025] ; int str = string_new( "" ) ; file * f ; int l; f = file_open( string_get( params[0] ), "rb" ) ; string_discard( params[0] ) ; if ( f ) { while ( !file_eof( f ) ) { l = file_read( f, buffer, sizeof( buffer ) - 1 ) ; buffer[l] = '\0' ; if ( l ) { string_concat( str, buffer ) ; buffer[0] = '\0' ; } else break; } file_close( f ) ; } string_use( str ) ; return str ; }
static int moddir_mkdir( INSTANCE * my, int * params ) { const char * d = string_get( params[ 0 ] ) ; int ret = dir_create( d ) ; string_discard( params[ 0 ] ) ; return ( ret ) ; }
static int modsay_say_fast( INSTANCE * my, int * params ) { /* Show debugging info also in stdout */ printf( "%s\n", string_get( params[0] ) ); string_discard( params[0] ) ; return 1 ; }
static int moddir_glob( INSTANCE * my, int * params ) { const char * path = string_get( params[ 0 ] ); static __DIR_ST * dh = NULL; int result; if ( dh && strcmp( dh->path, path ) ) { dir_close( dh ); dh = NULL; } if ( !dh ) dh = dir_open( path ); string_discard( params[ 0 ] ); if ( !dh ) { result = string_new( "" ); string_use( result ); return ( result ); } return ( __moddir_read( dh ) ) ; }
static int modfile_fputs( INSTANCE * my, int * params ) { char * str = ( char * ) string_get( params[1] ); int r = file_puts(( file * )params[0], str ) ; if ( str[strlen( str )-1] != '\n' ) file_puts(( file * )params[0], "\r\n" ) ; /* int r = file_puts ((file *)params[0], string_get(params[1])) ; */ string_discard( params[1] ) ; return r ; }
static int modsys_exec( INSTANCE * my, int * params ) { int mode = params[0]; char * filename = ( char * ) string_get( params[1] ); int argc = params[2]; char ** argv; int n = 0; #ifndef WIN32 pid_t child; #endif int status = -1; // fill argv argv = ( char ** ) calloc( argc + 2, sizeof( char * ) ); argv[0] = filename; for ( n = 0; n < argc; n++ ) argv[n + 1] = ( char * ) string_get((( int * )( params[3] ) )[n] ); // Execute program #ifdef WIN32 status = spawnvp( mode, filename, ( const char ** )argv ); #else if (( child = fork() ) == -1 ) { //Error status = -1 ; } else if ( child == 0 ) { execvp( filename, ( const char ** )argv ); exit(-1); } else { /* father */ switch ( mode ) { case _P_WAIT: if ( waitpid( child, &status, WUNTRACED ) != child ) status = -1; else status = (int)(char)WEXITSTATUS(status); break; case _P_NOWAIT: status = child; break; } } #endif // Free resources string_discard( params[1] ); if ( argv ) free( argv ); return ( status ) ; }
cStr *build_path(char *fname, struct stat * sbuf, Int nodir) { Int len = strlen(fname); cStr *str = NULL; if (len == 0) THROWN((file_id, "No file specified.")) #ifdef RESTRICTIVE_FILES if (strstr(fname, "../") || strstr(fname, "/..") || !strcmp(fname, "..")) THROWN((perm_id, "Filename \"%s\" is not legal.", fname)) str = string_from_chars(c_dir_root, strlen(c_dir_root)); str = string_addc(str, '/'); str = string_add_chars(str, fname, len); #else if (*fname != '/') { str = string_from_chars(c_dir_root, strlen(c_dir_root)); str = string_addc(str, '/'); str = string_add_chars(str, fname, len); } else { str = string_from_chars(fname, len); } #endif if (sbuf != NULL) { if (stat(str->s, sbuf) < 0) { cthrow(file_id, "Cannot find file \"%s\".", str->s); string_discard(str); return NULL; } if (nodir) { if (S_ISDIR(sbuf->st_mode)) { cthrow(directory_id, "\"%s\" is a directory.", str->s); string_discard(str); return NULL; } } } return str; }
static int modproc_exit_1( INSTANCE * my, int * params ) { printf( string_get( params[0] ) ); printf( "\n" ); fflush( stdout ); string_discard( params[0] ); exit_value = 0; must_exit = 1 ; return 1 ; }
static int modfile_fopen( INSTANCE * my, int * params ) { static char * ops[] = { "rb0", "r+b0", "wb0", "rb", "wb6" } ; int r ; if ( params[1] < 0 || params[1] > 4 ) params[0] = 0 ; r = ( int ) file_open( string_get( params[0] ), ops[params[1]] ) ; string_discard( params[0] ) ; return r ; }
static int __moddir_read(__DIR_ST * dh ) { __DIR_FILEINFO_ST * dif; char buffer[ 20 ]; int result; dif = dir_read( dh ); if ( !dif ) { result = string_new( "" ); string_use( result ); return ( result ); } /* discard previous strings values */ string_discard( GLODWORD( mod_dir, FILE_NAME ) ); string_discard( GLODWORD( mod_dir, FILE_PATH ) ); string_discard( GLODWORD( mod_dir, FILE_CREATED ) ); string_discard( GLODWORD( mod_dir, FILE_MODIFIED ) ); string_discard( GLODWORD( mod_dir, FILE_ACCESSED ) ); string_discard( GLODWORD( mod_dir, FILE_STATECHG ) ); GLODWORD( mod_dir, FILE_NAME ) = string_new( dif->filename ); string_use( GLODWORD( mod_dir, FILE_NAME ) ); GLODWORD( mod_dir, FILE_PATH ) = string_new( dif->fullpath ); string_use( GLODWORD( mod_dir, FILE_PATH ) ); GLODWORD( mod_dir, FILE_DIRECTORY ) = dif->attributes & DIR_FI_ATTR_DIRECTORY ? 1 : 0; GLODWORD( mod_dir, FILE_HIDDEN ) = dif->attributes & DIR_FI_ATTR_HIDDEN ? 1 : 0; GLODWORD( mod_dir, FILE_READONLY ) = dif->attributes & DIR_FI_ATTR_READONLY ? 1 : 0; GLODWORD( mod_dir, FILE_SIZE ) = dif->size; /* Store file times */ #ifdef _WIN32 strftime( buffer, 20, "%d/%m/%Y %H:%M:S", &dif->mtime ); GLODWORD( mod_dir, FILE_CREATED ) = string_new( buffer ); string_use( GLODWORD( mod_dir, FILE_CREATED ) ); #else GLODWORD( mod_dir, FILE_CREATED ) = string_new( "" ); string_use( GLODWORD( mod_dir, FILE_CREATED ) ); #endif strftime( buffer, 20, "%d/%m/%Y %H:%M:S", &dif->crtime ); GLODWORD( mod_dir, FILE_MODIFIED ) = string_new( buffer ); string_use( GLODWORD( mod_dir, FILE_MODIFIED ) ); strftime( buffer, 20, "%d/%m/%Y %H:%M:S", &dif->atime ); GLODWORD( mod_dir, FILE_ACCESSED ) = string_new( buffer ); string_use( GLODWORD( mod_dir, FILE_ACCESSED ) ); #ifndef _WIN32 strftime( buffer, 20, "%d/%m/%Y %H:%M:S", &dif->ctime ); GLODWORD( mod_dir, FILE_STATECHG ) = string_new( buffer ); string_use( GLODWORD( mod_dir, FILE_STATECHG ) ); #else GLODWORD( mod_dir, FILE_STATECHG ) = string_new( "" ); string_use( GLODWORD( mod_dir, FILE_STATECHG ) ); #endif /* Return */ result = GLODWORD( mod_dir, FILE_NAME ); string_use( result ); return result; }
static int modfile_load( INSTANCE * my, int * params ) { file * fp ; const char * filename ; int result = 0 ; filename = string_get( params[0] ) ; if ( !filename ) return 0 ; fp = file_open( filename, "rb0" ) ; if ( fp ) { result = loadtypes( fp, ( void * )params[1], ( void * )params[2], params[3], 0 ); file_close( fp ) ; } string_discard( params[0] ) ; return result ; }
static int modsys_getenv( INSTANCE * my, int * params ) { char *e ; int str ; if (( e = getenv( string_get( params[0] ) ) ) ) { str = string_new( e ) ; } else { str = string_new( "" ) ; } string_discard( params[0] ) ; string_use( str ) ; return str ; }
/* // -------------------------------------------------------------------- // // NOTE: If you send the object along, it is assumed it is the CORRECT // object bound to this function, sending the wrong object can cause // problems. // */ void file_discard(filec_t * file, Obj * obj) { filec_t **fp, *f; /* clear the object's file variable */ if (obj == NULL) { if (file->objnum != INV_OBJNUM) { Obj *obj = cache_retrieve(file->objnum); if (obj != NULL) { obj->file = NULL; cache_discard(obj); } } } else { obj->file = NULL; } /* pull it out of the 'files' list */ fp = &files; while (*fp) { f = *fp; if (f->objnum == file->objnum) { if (!f->f.closed) close_file(f); *fp = f->next; break; } else { fp = &f->next; } } /* toss the file proper */ string_discard(file->path); efree(file); }
int modtime_ftime( INSTANCE * my, int * params ) { char buffer[128] ; char * format ; struct tm * t ; int ret ; time_t tim ; char * base ; #ifdef _WIN32 /* aux buffer to make all changes... */ char aux[128] ; unsigned char pos ; #endif format = base = strdup( string_get( params[0] ) ) ; string_discard( params[0] ) ; #ifdef _WIN32 /* Addapting win32 strftime formats to linux formats */ /* HEAVY PATCH... :( */ pos = 0 ; while ( *format && pos < 127 ) { switch ( *format ) { case '%': /* MIGHT NEED CONVERSION... */ aux[pos] = *format ; pos++ ; format++ ; switch ( *format ) { case 'e': aux[pos++] = '#' ; aux[pos] = 'd' ; break ; case 'l': aux[pos++] = '#' ; aux[pos] = 'I' ; break ; case 'k': aux[pos++] = '#' ; aux[pos] = 'H' ; break ; case 'P': aux[pos] = 'p' ; break ; case 'C': aux[pos++] = '%' ; aux[pos++] = *format ; aux[pos++] = '%' ; aux[pos] = 'Y' ; break ; case 'u': aux[pos++] = '%' ; aux[pos++] = *format ; aux[pos++] = '%' ; aux[pos] = 'w' ; break ; case '%': //MUST BE %%%% TO KEEP 2 IN POSTPROCESS aux[pos++] = '%' ; aux[pos++] = '%' ; aux[pos] = '%' ; break ; default: aux[pos] = *format ; break ; } break ; default: aux[pos] = *format ; break ; } format++ ; pos++ ; } aux[pos] = 0 ; format = aux ; #endif tim = ( time_t ) params[1] ; t = localtime( &tim ) ; strftime( buffer, sizeof( buffer ), format, t ) ; #ifdef _WIN32 /* win32 postprocess */ aux[0] = '\0' ; format = buffer ; pos = 0 ; while ( *format ) { switch ( *format ) { case '%': format++ ; switch ( *format ) { case 'u': format++ ; if ( *format == '0' ) *format = '7' ; aux[pos] = *format ; break ; case 'C': format++ ; aux[pos++] = *format ; format++ ; aux[pos] = *format ; format++ ; format++ ; break ; default: aux[pos] = *format ; break ; } break ; default: aux[pos] = *format ; break ; } format++ ; pos++; } aux[pos] = '\0' ; strcpy( buffer, aux ) ; #endif ret = string_new( buffer ) ; string_use( ret ) ; free( base ) ; return ret ; }
/* void catch_signal(int sig, int code, struct sigcontext *scp) { */ void catch_signal(int sig) { char *sptr; cStr *sigstr; cData arg1; Bool do_shutdown = NO; signal(sig, catch_signal); sptr = sig_name(sig); sigstr = string_from_chars(sptr, strlen(sptr)); write_err("Caught signal %d: %S", sig, sigstr); string_discard(sigstr); /* figure out what to do */ switch (sig) { #ifdef __UNIX__ case SIGHUP: atomic = NO; handle_connection_output(); flush_files(); #endif #ifndef __MSVC__ case SIGUSR2: /* let the db do what it wants from here */ break; case SIGUSR1: { cData *d; cList *l; /* First cancel all preempted and suspended tasks */ l = vm_list(); for (d = list_first(l); d; d = list_next(l, d)) { /* boggle */ if (d->type != INTEGER) continue; vm_cancel(d->u.val); } list_discard(l); /* now cancel the current task if it is valid */ if (vm_lookup(task_id) != NULL) { vm_cancel(task_id); } /* jump back to the main loop */ longjmp(main_jmp, 1); break; } #endif case SIGILL: /* lets panic and hopefully shutdown without frobbing the db */ panic(sig_name(sig)); break; case SIGTERM: if (running) { write_err("*** Attempting normal shutdown ***"); running = NO; /* jump back to the main loop, ignore any current tasks; *drip*, *drip*, leaky */ longjmp(main_jmp, 1); } else { panic(sig_name(sig)); } break; default: do_shutdown = YES; break; } /* only pass onto the db if we are 'executing' */ if (!running) return; /* send a message to the system object */ arg1.type = SYMBOL; arg1.u.symbol = ident_get(sptr); vm_task(SYSTEM_OBJNUM, signal_id, 1, &arg1); if (do_shutdown) running = NO; }
static int moddir_open( INSTANCE * my, int * params ) { int result = ( int ) dir_open( string_get( params[ 0 ] ) ); string_discard( params[ 0 ] ); return result; }
static int modregex_regex_replace (INSTANCE * my, int * params) { const char * reg = string_get(params[0]); const char * rep = string_get(params[1]); const char * str = string_get(params[2]); unsigned reg_len = strlen(reg); unsigned str_len = strlen(str); unsigned rep_len = strlen(rep); char * replacement; unsigned replacement_len; int fixed_replacement = strchr(rep, '\\') ? 0:1; struct re_pattern_buffer pb; struct re_registers re; int start[16]; int end[16]; unsigned startpos = 0; unsigned nextpos; int regex_filled = 0; char * result = 0; unsigned result_allocated = 0; int result_string = 0; unsigned n; int * regex_reg; /* Alloc a buffer for the resulting string */ result = malloc(128); result_allocated = 128; *result = 0; /* Alloc the pattern resources */ memset (&pb, 0, sizeof(pb)); memset (&re, 0, sizeof(re)); pb.buffer = malloc(4096); pb.allocated = 4096; pb.used = 0; pb.fastmap = malloc(256); pb.translate = NULL; pb.fastmap_accurate = 0; pb.regs_allocated = 16; re.start = start; re.end = end; re_syntax_options = RE_SYNTAX_POSIX_MINIMAL_EXTENDED; /* Run the regex */ if (re_compile_pattern (reg, reg_len, &pb) == 0) { startpos = 0; while (startpos < str_len) { nextpos = re_search (&pb, str, str_len, startpos, str_len - startpos, &re); if ((int)nextpos < 0) break; /* Fill the REGEX_REG global variables */ if (regex_filled == 0) { regex_filled = 1; regex_reg = (int *)&GLODWORD( mod_regex, REGEX_REG); for (n = 0 ; n < 16 && n <= pb.re_nsub ; n++) { string_discard (regex_reg[n]); regex_reg[n] = string_newa (str + re.start[n], re.end[n] - re.start[n]); string_use (regex_reg[n]); } } /* Prepare the replacement string */ if (fixed_replacement == 0) { int total_length = rep_len; const char * bptr; char * ptr; /* Count the size */ ptr = strchr(rep, '\\'); while (ptr) { if (ptr[1] >= '0' && ptr[1] <= '9') total_length += re.end[ptr[1]-'0'] - re.start[ptr[1]-'0'] - 2; ptr = strchr(ptr+1, '\\'); } /* Fill the replacement string */ replacement = calloc (total_length+1, 1); bptr = rep; ptr = strchr(rep, '\\'); while (ptr) { if (ptr[1] >= '0' && ptr[1] <= '9') { strncpy (replacement+strlen(replacement), bptr, ptr-bptr); strncpy (replacement+strlen(replacement), str + re.start[ptr[1]-'0'], re.end[ptr[1]-'0'] - re.start[ptr[1]-'0']); bptr = ptr+2; } ptr = strchr (ptr+1, '\\'); } strcat (replacement, bptr); replacement_len = strlen(replacement); } else { replacement = (char *)rep; replacement_len = rep_len; } /* Fill the resulting string */ if (result_allocated < strlen(result)+(nextpos-startpos)+1+replacement_len) { result_allocated += ((nextpos-startpos+1+replacement_len) & ~127) + 128; result = realloc(result, result_allocated); } result[strlen(result)+(nextpos-startpos)] = 0; memcpy (result + strlen(result), str+startpos, nextpos-startpos); strcat (result, replacement); if (fixed_replacement == 0) free (replacement); /* Continue the search */ startpos = nextpos+re_match(&pb, str, str_len, nextpos, 0); if (startpos < nextpos) break; if (startpos == nextpos) startpos++; } } /* Copy remaining characters */ nextpos = str_len; if (result_allocated < strlen(result)+(nextpos-startpos)+1) { result_allocated += ((nextpos-startpos+1) & ~127) + 128; result = realloc(result, result_allocated); } result[strlen(result)+(nextpos-startpos)] = 0; memcpy (result + strlen(result), str+startpos, nextpos-startpos); /* Free resources */ free (pb.buffer); free (pb.fastmap); string_discard(params[0]); string_discard(params[1]); string_discard(params[2]); /* Return the new string */ result_string = string_new(result); string_use(result_string); free(result); return result_string; }
static int modregex_split (INSTANCE * my, int * params) { const char * reg = string_get(params[0]); const char * str = string_get(params[1]); int * result_array = (int *)params[2]; int result_array_size = params[3]; int count = 0; int pos, lastpos = 0; struct re_pattern_buffer pb; struct re_registers re; int start[16]; int end[16]; /* Alloc the pattern resources */ memset (&pb, 0, sizeof(pb)); memset (&re, 0, sizeof(re)); pb.buffer = malloc(4096); pb.allocated = 4096; pb.fastmap = malloc(256); pb.regs_allocated = 16; re.num_regs = 16; re.start = start; re.end = end; re_syntax_options = RE_SYNTAX_POSIX_MINIMAL_EXTENDED; /* Match the regex */ if (re_compile_pattern (reg, strlen(reg), &pb) == 0) { for (;;) { pos = re_search (&pb, str, strlen(str), lastpos, strlen(str), &re); if (pos == -1) break; *result_array = string_newa (str + lastpos, pos-lastpos); string_use(*result_array); result_array++; count++; result_array_size--; if (result_array_size == 0) break; lastpos = pos + re_match (&pb, str, strlen(str), pos, 0); if (lastpos < pos) break; if (lastpos == pos) lastpos++; } if (result_array_size > 0) { *result_array = string_new (str + lastpos); string_use (*result_array); count++; } } /* Free the resources */ free (pb.buffer); free (pb.fastmap); string_discard(params[0]); string_discard(params[1]); return count; }
static int modfile_exists( INSTANCE * my, int * params ) { int r = file_exists( string_get( params[0] ) ) ; string_discard( params[0] ) ; return r ; }