00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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];
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];
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);
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;
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)
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
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,
00418 "Applications",
00419 &applications
00420 );
00421 if(ret != ERROR_SUCCESS)
00422 {
00423 return -1;
00424 }
00425
00426 ret = RegOpenKey(
00427 applications,
00428 "iexplore.exe",
00429 &iexplore
00430 );
00431 if(ret != ERROR_SUCCESS)
00432 {
00433 RegCloseKey(applications);
00434 return -1;
00435 }
00436
00437 ret = RegOpenKey(
00438 iexplore,
00439 "shell",
00440 &shell
00441 );
00442 if(ret != ERROR_SUCCESS)
00443 {
00444 RegCloseKey(applications);
00445 RegCloseKey(iexplore);
00446 return -1;
00447 }
00448
00449 ret = RegOpenKey(
00450 shell,
00451 "open",
00452 &open
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,
00464 "command",
00465 &command
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,
00478 "",
00479 NULL,
00480 &type,
00481 (unsigned char *) buf,
00482 &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 ret = (int) CreateProcess( NULL, cmd
00508 , NULL, NULL
00509 , FALSE, CREATE_NO_WINDOW
00510 , NULL, NULL
00511 , &si, &pi);
00512 return ret;
00513 }
00514 #endif
00515
00516 int rlsystem(const char *command)
00517 {
00518 #ifdef RLWIN32
00519 return mysystem(command);
00520 #else
00521 return system(command);
00522 #endif
00523 }
00524
00525 int rlSubmitPvserver(const char *env, const char *path, const char *pvs, const char *options)
00526 {
00527 if(env == NULL || path == NULL || pvs == NULL) return -1;
00528 rlString command,cd;
00529 const char *cptr;
00530
00531 #ifdef __VMS
00532 cptr = env;
00533 cd = cptr;
00534 cd += path;
00535 command = "spawn/nowait ";
00536 command += cd;
00537 command += pvs;
00538 command += " ";
00539 if(options != NULL) command += options;
00540 command += " -cd=";
00541 command += cd;
00542 #else
00543 cptr = getenv(env);
00544 if(cptr == NULL) { printf("rlSubmitPvserver:ERROR env=%s is not set\n", env); return -2; }
00545 cd += cptr;
00546 cd += path;
00547 command = "\"";
00548 command += cd;
00549 #ifdef RLWIN32
00550 command += "\\";
00551 #else
00552 command += "/";
00553 #endif
00554 command += pvs;
00555 command += "\" ";
00556 if(options != NULL) command += options;
00557 command += " \"-cd=";
00558 command += cd;
00559 command += "\"";
00560 #endif
00561
00562 #ifdef RLUNIX
00563 command += " &";
00564 #endif
00565
00566
00567 return rlsystem(command.text());
00568
00569 }
00570
00571 int rlBrowser(const char *htmlfile)
00572 {
00573 char buf[4096];
00574 int ret = 0;
00575
00576 #ifdef RLUNIX
00577 sprintf(buf,"konqueror %s &", htmlfile);
00578 ret = system(buf);
00579 #endif
00580 #ifdef RLWIN32
00581 if(get_iexplore(buf) < 0) return -1;
00582 strcat(buf," ");
00583 strcat(buf,htmlfile);
00584 ret = mysystem(buf);
00585 #endif
00586 return ret;
00587 }
00588
00589 int rlOption(const char *string, const char *option)
00590 {
00591 const char *cptr;
00592
00593 cptr = strstr(string,option);
00594 if(cptr == NULL) return 0;
00595 return 1;
00596 }
00597
00598 int rlIntOption(const char *string, const char *option, int def)
00599 {
00600 const char *cptr;
00601 int ret;
00602
00603 cptr = strstr(string,option);
00604 if(cptr == NULL) return def;
00605
00606 cptr = strstr(cptr,"=");
00607 if(cptr == NULL) return def;
00608
00609 sscanf(cptr,"=%d",&ret);
00610 return ret;
00611 }
00612
00613 float rlFloatOption(const char *string, const char *option, float def)
00614 {
00615 const char *cptr;
00616 float ret;
00617
00618 cptr = strstr(string,option);
00619 if(cptr == NULL) return def;
00620
00621 cptr = strstr(cptr,"=");
00622 if(cptr == NULL) return def;
00623
00624 sscanf(cptr,"=%f",&ret);
00625 return ret;
00626 }
00627
00628 const char *rlTextOption(const char *string, const char *option, const char *def)
00629 {
00630 int i = 0;
00631 const char *cptr;
00632 char quote;
00633 static char ret[rl_PRINTF_LENGTH];
00634
00635 cptr = strstr(string,option);
00636 if(cptr == NULL) return def;
00637
00638 cptr = strstr(cptr,"=");
00639 if(cptr == NULL) return def;
00640
00641 cptr++;
00642 quote = *cptr;
00643 cptr++;
00644 while(i < ((int) sizeof(ret)-1))
00645 {
00646 if(cptr[i] == quote || cptr[i] == '\0') break;
00647 ret[i] = cptr[i];
00648 i++;
00649 }
00650 ret[i] = '\0';
00651 return ret;
00652 }
00653
00654 int rlCopyTextfile(const char *source, const char *destination)
00655 {
00656 FILE *fin, *fout;
00657
00658 if(source == NULL || destination == NULL) return -1;
00659 fin = fopen(source,"r");
00660 if(fin == NULL)
00661 {
00662 printf("rlCopyTextfile: could not read %s\n",source);
00663 return -1;
00664 }
00665 fout = fopen(destination,"w");
00666 if(fout == NULL)
00667 {
00668 fclose(fin);
00669 printf("rlCopyTextfile: could not write %s\n",destination);
00670 return -1;
00671 }
00672
00673 char *line = new char[256*256];
00674 while(fgets(line,sizeof(line)-1,fin) != NULL)
00675 {
00676 fprintf(fout,"%s",line);
00677 }
00678 delete [] line;
00679
00680 fclose(fin);
00681 fclose(fout);
00682 return 0;
00683 }
00684
00685 int rlupper(char *str)
00686 {
00687 if(str == NULL) return -1;
00688 while(*str != '\0')
00689 {
00690 *str = toupper(*str);
00691 str++;
00692 }
00693 return 0;
00694 }
00695
00696 int rllower(char *str)
00697 {
00698 if(str == NULL) return -1;
00699 while(*str != '\0')
00700 {
00701 *str = tolower(*str);
00702 str++;
00703 }
00704 return 0;
00705 }
00706
00707 int rlStartsWith(const char *str, const char *startstr)
00708 {
00709 int ret;
00710 if(str == NULL || startstr == NULL) return 0;
00711 ret = strncmp(str,startstr,strlen(startstr));
00712 if(ret == 0) return 1;
00713 return 0;
00714 }