|
rllib
1
|
00001 /*************************************************************************** 00002 rlcutil.cpp - description 00003 ------------------- 00004 begin : Wed Dec 11 2002 00005 copyright : (C) 2002 by Rainer Lehrig 00006 email : lehrig@t-online.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This library is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * 00013 * published by the Free Software Foundation * 00014 * * 00015 ***************************************************************************/ 00016 #include "rlcutil.h" 00017 #include "rlstring.h" 00018 #include <stdio.h> 00019 #include <stdlib.h> 00020 #include <string.h> 00021 #include <stdarg.h> 00022 #include <signal.h> 00023 00024 #ifdef RLUNIX 00025 #include <sys/stat.h> 00026 #include <sys/types.h> 00027 #include <unistd.h> 00028 #include <fcntl.h> 00029 #include <dirent.h> 00030 #endif 00031 00032 #ifdef __VMS 00033 #include <descrip.h> 00034 #include <rmsdef.h> 00035 #include <ssdef.h> 00036 #include <iodef.h> 00037 #include <unixio.h> 00038 #include <file.h> 00039 #include <lib$routines.h> 00040 #endif 00041 00042 #ifdef RLWIN32 00043 #include <windows.h> 00044 #include <winreg.h> 00045 #include <fcntl.h> 00046 #include <io.h> 00047 #include <sys\types.h> 00048 #include <sys\stat.h> 00049 #endif 00050 00051 int rlDebugPrintfState = 0; 00052 00053 int rlSetDebugPrintf(int state) 00054 { 00055 if(state == 0) rlDebugPrintfState = 0; 00056 else rlDebugPrintfState = 1; 00057 return 0; 00058 } 00059 00060 int rlDebugPrintf(const char *format, ...) 00061 { 00062 int ret; 00063 char message[rl_PRINTF_LENGTH]; // should be big enough 00064 00065 if(rlDebugPrintfState == 0) return 0; 00066 va_list ap; 00067 va_start(ap,format); 00068 ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap); 00069 va_end(ap); 00070 if(ret < 0) return ret; 00071 return printf("%s",message); 00072 } 00073 00074 int rlLastLinePrintf(const char *format, ...) 00075 { 00076 int ret,i; 00077 char message[rl_PRINTF_LENGTH]; // should be big enough 00078 00079 va_list ap; 00080 va_start(ap,format); 00081 ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap); 00082 va_end(ap); 00083 if(ret < 0) return ret; 00084 message[79] = '\0'; 00085 printf("%c[80D",27); // move 80 columns left 00086 printf("%s",message); 00087 for(i=strlen(message); i<80; i++) printf(" "); 00088 return 80; 00089 } 00090 00091 #ifdef RLUNIX 00092 //------------------------------------------------------------------------ 00093 int rlexec(const char *command) 00094 { 00095 char *buf; 00096 char *arg[512]; 00097 int i,iarg,ret; 00098 00099 buf = new char [strlen(command)+1]; 00100 strcpy(buf,command); 00101 iarg = 0; 00102 i = 0; 00103 while(buf[i] != '\0') 00104 { 00105 if(buf[i] == '\"') 00106 { 00107 i++; 00108 arg[iarg++] = &buf[i]; 00109 while(buf[i] != '\"' && buf[i] != '\0') i++; 00110 if(buf[i] == '\0') break; 00111 buf[i] = '\0'; 00112 } 00113 else if(buf[i] != ' ' || i == 0) 00114 { 00115 arg[iarg++] = &buf[i]; 00116 while(buf[i] != ' ' && buf[i] != '\0') i++; 00117 if(buf[i] == '\0') break; 00118 buf[i] = '\0'; 00119 } 00120 i++; 00121 } 00122 arg[iarg] = NULL; 00123 00124 ret = execvp(arg[0],arg); 00125 delete [] buf; 00126 return ret; 00127 } 00128 #endif 00129 00130 //------------------------------------------------------------------------ 00131 const char *rlpass(const char *p) 00132 { 00133 static char ret[4*16+8]; 00134 char buf[80]; 00135 int i,val; 00136 00137 ret[0] = '\0'; 00138 for(i=0; p[i] != '\0' && i<=16; i++) 00139 { 00140 val = p[i] * 16; 00141 sprintf(buf,"%04X",val); 00142 strcat(ret,buf); 00143 } 00144 00145 return ret; 00146 } 00147 00148 //------------------------------------------------------------------------ 00149 char *rlstrncpy(char *dest, const char *source, int n) 00150 { 00151 int i; 00152 00153 for(i=0; i<n; i++) 00154 { 00155 if(source[i] == '\0') break; 00156 dest[i] = source[i]; 00157 } 00158 dest[i] = '\0'; 00159 return dest; 00160 } 00161 00162 //------------------------------------------------------------------------ 00163 int rlvsnprintf(char *text, int len, const char *format, va_list ap) 00164 { 00165 int ret; 00166 00167 #ifdef RLWIN32 00168 if(format == NULL || format[0] == '\0') 00169 { 00170 text[0] = '\0'; 00171 return 1; 00172 } 00173 ret = _vsnprintf(text, len, format, ap); 00174 #endif 00175 #ifdef __VMS 00176 static char vms_is_deprecated[rl_PRINTF_LENGTH]; 00177 if(format == NULL || format[0] == '\0') 00178 { 00179 text[0] = '\0'; 00180 return 1; 00181 } 00182 ret = vsprintf(vms_is_deprecated, format, ap); 00183 rlstrncpy(text,vms_is_deprecated,len); 00184 #endif 00185 #ifdef RLUNIX 00186 if(format == NULL || format[0] == '\0') 00187 { 00188 text[0] = '\0'; 00189 return 1; 00190 } 00191 ret = vsnprintf(text, len, format, ap); 00192 #endif 00193 return ret; 00194 } 00195 00196 //------------------------------------------------------------------------ 00197 int rlsnprintf(char *text, int len, const char *format, ...) 00198 { 00199 int ret; 00200 00201 va_list ap; 00202 va_start(ap,format); 00203 #ifdef RLWIN32 00204 ret = _vsnprintf(text, len, format, ap); 00205 #endif 00206 #ifdef __VMS 00207 static char vms_is_deprecated[rl_PRINTF_LENGTH]; 00208 ret = vsprintf(vms_is_deprecated, format, ap); 00209 rlstrncpy(text,vms_is_deprecated,len); 00210 #endif 00211 #ifdef RLUNIX 00212 ret = vsnprintf(text, len, format, ap); 00213 #endif 00214 va_end(ap); 00215 return ret; 00216 } 00217 00218 //------------------------------------------------------------------------ 00219 static void *rlsigtermarg = NULL; 00220 static void (*rlUserSigtermHandler)(void *arg) = NULL; 00221 00222 static void rlSigtermHandler(int sig) 00223 { 00224 printf("entering rlSigtermHandler sig=%d\n",sig); 00225 if(sig == SIGTERM && rlUserSigtermHandler != NULL) rlUserSigtermHandler(rlsigtermarg); 00226 #ifdef RLUNIX 00227 fflush(stdout); 00228 fsync(fileno(stdout)); 00229 #endif 00230 exit(0); 00231 } 00232 00233 void rlSetSigtermHandler(void (*handler)(void *arg), void *arg) 00234 { 00235 rlsigtermarg = arg; 00236 rlUserSigtermHandler = handler; 00237 signal(SIGTERM,rlSigtermHandler); 00238 } 00239 00240 //------------------------------------------------------------------------ 00241 const char *rlFindFile(const char *pattern, int *context) 00242 { 00243 static char freturn[512]; 00244 00245 #ifdef RLUNIX 00246 static DIR *dirp; 00247 static struct dirent *dp; 00248 00249 if(*context == 0) dirp = opendir("."); 00250 *context = 1; 00251 00252 while((dp = readdir(dirp)) != NULL) 00253 { 00254 if(dp->d_name[0] == '.') ; 00255 else if(strstr(dp->d_name,pattern) != NULL) 00256 { 00257 strcpy(freturn,dp->d_name); 00258 return freturn; 00259 } 00260 } 00261 closedir(dirp); 00262 return NULL; 00263 #endif 00264 00265 #ifdef __VMS 00266 int i,ret; 00267 static char file[512] = ""; 00268 static char wildcard[80]; 00269 struct dsc$descriptor_s dwildcard; 00270 struct dsc$descriptor_s dfreturn; 00271 00272 strcpy(wildcard,pattern); 00273 00274 dwildcard.dsc$w_length = strlen(wildcard); 00275 dwildcard.dsc$a_pointer = wildcard; 00276 dwildcard.dsc$b_dtype = DSC$K_DTYPE_T; 00277 dwildcard.dsc$b_class = DSC$K_CLASS_S; 00278 00279 dfreturn.dsc$w_length = sizeof(freturn); 00280 dfreturn.dsc$a_pointer = &freturn[0]; 00281 dfreturn.dsc$b_dtype = DSC$K_DTYPE_T; 00282 dfreturn.dsc$b_class = DSC$K_CLASS_S; 00283 00284 ret = LIB$FIND_FILE(&dwildcard,&dfreturn,context,0,0,0,0); 00285 if (ret == RMS$_NMF) return NULL; // no more files found 00286 else if(ret != RMS$_NORMAL) return NULL; 00287 else if(strcmp(freturn,file) == 0) { file[0] = '\0'; return NULL; } 00288 i=0; 00289 while(freturn[i] > ' ')i++; 00290 freturn[i] = '\0'; 00291 return freturn; 00292 #endif 00293 00294 #ifdef RLWIN32 00295 static WIN32_FIND_DATA wfd; 00296 static HANDLE hFindFile; 00297 00298 if(*context == 0) // find first 00299 { 00300 *context = 1; 00301 hFindFile = FindFirstFile(pattern,&wfd); 00302 if(hFindFile == INVALID_HANDLE_VALUE) return NULL; 00303 else strcpy(freturn,(const char *) &wfd.cFileName); 00304 } 00305 else // subsequent find 00306 { 00307 if(FindNextFile(hFindFile,&wfd) == TRUE) strcpy(freturn,(const char *) &wfd.cFileName); 00308 else { FindClose(hFindFile); return NULL; } 00309 } 00310 return freturn; 00311 #endif 00312 } 00313 00314 //------------------------------------------------------------------------ 00315 const char *rlGetInifile(const char *name) 00316 { 00317 static char buf[1024]; 00318 00319 #ifdef RLUNIX 00320 strcpy(buf,getenv("HOME")); 00321 strcat(buf,"/."); 00322 #endif 00323 #ifdef __VMS 00324 strcpy(buf,"sys$login:"); 00325 #endif 00326 #ifdef RLWIN32 00327 ExpandEnvironmentStrings("%USERPROFILE%",buf,sizeof(buf)-1); 00328 if(strcmp(buf,"%USERPROFILE%") == 0) strcpy(buf,"C:"); 00329 strcat(buf,"\\");; 00330 #endif 00331 00332 strcat(buf,name); 00333 return buf; 00334 } 00335 00336 int rlSwapShort(int val) 00337 { 00338 return (val & 0x0ff)*256 + val/256; 00339 } 00340 00341 //------------------------------------------------------------------------ 00342 int rlEib1(int command) 00343 { 00344 char buf[80]; 00345 00346 sprintf(buf,"buscommand -eib1 %d",command); 00347 return system(buf); 00348 } 00349 00350 int rlEib2(int command) 00351 { 00352 char buf[80]; 00353 00354 sprintf(buf,"buscommand -eib2 %d",command); 00355 return system(buf); 00356 } 00357 00358 int rlLon1(int command) 00359 { 00360 char buf[80]; 00361 00362 sprintf(buf,"buscommand -lon1 %d",command); 00363 return system(buf); 00364 } 00365 00366 int rlLon2(int command) 00367 { 00368 char buf[80]; 00369 00370 sprintf(buf,"buscommand -lon2 %d",command); 00371 return system(buf); 00372 } 00373 00374 int rlProfibus1(int command) 00375 { 00376 char buf[80]; 00377 00378 sprintf(buf,"buscommand -profibus1 %d",command); 00379 return system(buf); 00380 } 00381 00382 int rlProfibus2(int command) 00383 { 00384 char buf[80]; 00385 00386 sprintf(buf,"buscommand -profibus2 %d",command); 00387 return system(buf); 00388 } 00389 00390 int rlCan1(int command) 00391 { 00392 char buf[80]; 00393 00394 sprintf(buf,"buscommand -can1 %d",command); 00395 return system(buf); 00396 } 00397 00398 int rlCan2(int command) 00399 { 00400 char buf[80]; 00401 00402 sprintf(buf,"buscommand -can2 %d",command); 00403 return system(buf); 00404 } 00405 00406 #ifdef RLWIN32 00407 static int get_iexplore(char *buf) 00408 { 00409 HKEY applications,iexplore,shell,open,command; 00410 long ret; 00411 unsigned long size,type; 00412 char *cptr; 00413 00414 buf[0] = '\0'; 00415 00416 ret = RegOpenKey( 00417 HKEY_CLASSES_ROOT, // handle to open key 00418 "Applications", // address of name of subkey to open 00419 &applications // address of handle to open key 00420 ); 00421 if(ret != ERROR_SUCCESS) 00422 { 00423 return -1; 00424 } 00425 00426 ret = RegOpenKey( 00427 applications, // handle to open key 00428 "iexplore.exe", // address of name of subkey to open 00429 &iexplore // address of handle to open key 00430 ); 00431 if(ret != ERROR_SUCCESS) 00432 { 00433 RegCloseKey(applications); 00434 return -1; 00435 } 00436 00437 ret = RegOpenKey( 00438 iexplore, // handle to open key 00439 "shell", // address of name of subkey to open 00440 &shell // address of handle to open key 00441 ); 00442 if(ret != ERROR_SUCCESS) 00443 { 00444 RegCloseKey(applications); 00445 RegCloseKey(iexplore); 00446 return -1; 00447 } 00448 00449 ret = RegOpenKey( 00450 shell, // handle to open key 00451 "open", // address of name of subkey to open 00452 &open // address of handle to open key 00453 ); 00454 if(ret != ERROR_SUCCESS) 00455 { 00456 RegCloseKey(applications); 00457 RegCloseKey(iexplore); 00458 RegCloseKey(shell); 00459 return -1; 00460 } 00461 00462 ret = RegOpenKey( 00463 open, // handle to open key 00464 "command", // address of name of subkey to open 00465 &command // address of handle to open key 00466 ); 00467 if(ret != ERROR_SUCCESS) 00468 { 00469 RegCloseKey(applications); 00470 RegCloseKey(iexplore); 00471 RegCloseKey(shell); 00472 RegCloseKey(open); 00473 return -1; 00474 } 00475 00476 ret = RegQueryValueEx( 00477 command, // handle to key to query 00478 "", // address of name of value to query 00479 NULL, // reserved 00480 &type, // address of buffer for value type 00481 (unsigned char *) buf, // address of data buffer 00482 &size // address of data buffer size 00483 ); 00484 00485 RegCloseKey(applications); 00486 RegCloseKey(iexplore); 00487 RegCloseKey(shell); 00488 RegCloseKey(open); 00489 RegCloseKey(command); 00490 00491 if(ret != ERROR_SUCCESS) return -1; 00492 00493 cptr = strstr(buf," %1"); 00494 if(cptr != NULL) *cptr = '\0'; 00495 00496 return 0; 00497 } 00498 00499 static int mysystem(const char *command) 00500 { 00501 int ret; 00502 STARTUPINFO si = { sizeof(si)}; 00503 PROCESS_INFORMATION pi; 00504 char cmd[4096]; 00505 00506 //strcpy(cmd,command); 00507 ExpandEnvironmentStrings(command,cmd,sizeof(cmd)-1); 00508 ret = (int) CreateProcess( NULL, cmd 00509 , NULL, NULL 00510 , FALSE, CREATE_NO_WINDOW 00511 , NULL, NULL 00512 , &si, &pi); 00513 return ret; 00514 } 00515 #endif 00516 00517 int rlsystem(const char *command) 00518 { 00519 #ifdef RLWIN32 00520 return mysystem(command); 00521 #else 00522 return system(command); 00523 #endif 00524 } 00525 00526 int rlSubmitPvserver(const char *env, const char *path, const char *pvs, const char *options) 00527 { 00528 if(env == NULL || path == NULL || pvs == NULL) return -1; 00529 rlString command,cd; 00530 const char *cptr; 00531 00532 #ifdef __VMS 00533 cptr = env; 00534 cd = cptr; 00535 cd += path; 00536 command = "spawn/nowait "; 00537 command += cd; 00538 command += pvs; 00539 command += " "; 00540 if(options != NULL) command += options; 00541 command += " -cd="; 00542 command += cd; 00543 #else 00544 cptr = getenv(env); 00545 if(cptr == NULL) { printf("rlSubmitPvserver:ERROR env=%s is not set\n", env); return -2; } 00546 cd += cptr; 00547 cd += path; 00548 command = "\""; 00549 command += cd; 00550 #ifdef RLWIN32 00551 command += "\\"; 00552 #else 00553 command += "/"; 00554 #endif 00555 command += pvs; 00556 command += "\" "; 00557 if(options != NULL) command += options; 00558 command += " \"-cd="; 00559 command += cd; 00560 command += "\""; 00561 #endif 00562 00563 #ifdef RLUNIX 00564 command += " &"; 00565 #endif 00566 00567 //printf("command=%s\n", command.text()); 00568 return rlsystem(command.text()); 00569 //example: rlSubmitPvserver("HOME","/temp/murx","pvs","-exit_on_bind_error -exit_after_last_client_terminates"); 00570 } 00571 00572 int rlBrowser(const char *htmlfile) 00573 { 00574 char buf[4096]; 00575 int ret = 0; 00576 00577 #ifdef RLUNIX 00578 sprintf(buf,"konqueror %s &", htmlfile); 00579 ret = system(buf); 00580 #endif 00581 #ifdef RLWIN32 00582 if(get_iexplore(buf) < 0) return -1; 00583 strcat(buf," "); 00584 strcat(buf,htmlfile); 00585 ret = mysystem(buf); 00586 #endif 00587 return ret; 00588 }//------------------------------------------------------------------------ 00589 00590 int rlOption(const char *string, const char *option) 00591 { 00592 const char *cptr; 00593 00594 cptr = strstr(string,option); 00595 if(cptr == NULL) return 0; 00596 return 1; 00597 } 00598 00599 int rlIntOption(const char *string, const char *option, int def) 00600 { 00601 const char *cptr; 00602 int ret; 00603 00604 cptr = strstr(string,option); 00605 if(cptr == NULL) return def; 00606 00607 cptr = strstr(cptr,"="); 00608 if(cptr == NULL) return def; 00609 00610 sscanf(cptr,"=%d",&ret); 00611 return ret; 00612 } 00613 00614 float rlFloatOption(const char *string, const char *option, float def) 00615 { 00616 const char *cptr; 00617 float ret; 00618 00619 cptr = strstr(string,option); 00620 if(cptr == NULL) return def; 00621 00622 cptr = strstr(cptr,"="); 00623 if(cptr == NULL) return def; 00624 00625 sscanf(cptr,"=%f",&ret); 00626 return ret; 00627 } 00628 00629 const char *rlTextOption(const char *string, const char *option, const char *def) 00630 { 00631 int i = 0; 00632 const char *cptr; 00633 char quote; 00634 static char ret[rl_PRINTF_LENGTH]; 00635 00636 cptr = strstr(string,option); 00637 if(cptr == NULL) return def; 00638 00639 cptr = strstr(cptr,"="); 00640 if(cptr == NULL) return def; 00641 00642 cptr++; 00643 quote = *cptr; 00644 cptr++; 00645 while(i < ((int) sizeof(ret)-1)) 00646 { 00647 if(cptr[i] == quote || cptr[i] == '\0') break; 00648 ret[i] = cptr[i]; 00649 i++; 00650 } 00651 ret[i] = '\0'; 00652 return ret; 00653 } 00654 00655 int rlCopyTextfile(const char *source, const char *destination) 00656 { 00657 FILE *fin, *fout; 00658 00659 if(source == NULL || destination == NULL) return -1; 00660 fin = fopen(source,"r"); 00661 if(fin == NULL) 00662 { 00663 printf("rlCopyTextfile: could not read %s\n",source); 00664 return -1; 00665 } 00666 fout = fopen(destination,"w"); 00667 if(fout == NULL) 00668 { 00669 fclose(fin); 00670 printf("rlCopyTextfile: could not write %s\n",destination); 00671 return -1; 00672 } 00673 00674 char *line = new char[256*256]; 00675 while(fgets(line,sizeof(line)-1,fin) != NULL) 00676 { 00677 fprintf(fout,"%s",line); 00678 } 00679 delete [] line; 00680 00681 fclose(fin); 00682 fclose(fout); 00683 return 0; 00684 } 00685 00686 int rlupper(char *str) 00687 { 00688 if(str == NULL) return -1; 00689 while(*str != '\0') 00690 { 00691 *str = toupper(*str); 00692 str++; 00693 } 00694 return 0; 00695 } 00696 00697 int rllower(char *str) 00698 { 00699 if(str == NULL) return -1; 00700 while(*str != '\0') 00701 { 00702 *str = tolower(*str); 00703 str++; 00704 } 00705 return 0; 00706 } 00707 00708 int rlStartsWith(const char *str, const char *startstr) 00709 { 00710 int ret; 00711 if(str == NULL || startstr == NULL) return 0; 00712 ret = strncmp(str,startstr,strlen(startstr)); 00713 if(ret == 0) return 1; 00714 return 0; 00715 }
1.7.5.1