int main(int argc, char *argv[]) { #define MAXBUF 4095 char buf[ MAXBUF+1 ]; int iarg, ibuf, ichr; bool debug = FALSE; CL_ERR_DESC err_code; char *p1 = NULL; char pid[MAXBUF]; char *database = ERx(""); char *user = ERx(""); char *xmlfile = ERx(""); char sql_fname[LO_NM_LEN + 1]; char *work_dir = NULL; char directory[MAX_LOC + 1]; char *tmp_dir = NULL; char tmp_buf[MAX_LOC + 1]; char subdir_buf[MAX_LOC + 1]; char sql_loc_buf[MAX_LOC + 1]; char *progname; LOCATION tmp_dir_loc; LOCATION tmp_subdir_loc; LOCATION tmp_buff_loc; LOCATION curr_loc; LOCATION sql_file_loc; char *password = ERx(""); char *groupid = ERx(""); ARGRET rarg; i4 pos; LOCATION xmlfile_loc; FILE *xmlfile_read; STATUS stat = FAIL; char dbuf[256]; char encode[32]; u_i4 tmppid; TM_STAMP tm_stamp; /* Tell EX this is an ingres tool. */ (void) EXsetclient(EX_INGRES_TOOL); /* Call IIUGinit to initialize character set attribute table */ if ( IIUGinit() != OK) PCexit(FAIL); progname = ERget(F_XM0006_IMPXML); FEcopyright(progname, ERx("2001")); /* ** Get arguments from command line */ /* required parameters */ if (FEutaopen(argc, argv, ERx("xmlimport")) != OK) PCexit(FAIL); /* database name is required */ if (FEutaget(ERx("database"), 0, FARG_PROMPT, &rarg, &pos) != OK) PCexit(FAIL); database = rarg.dat.name; if (FEutaget(ERx("xmlfile"), 0, FARG_PROMPT, &rarg, &pos) != OK) PCexit(FAIL); xmlfile = rarg.dat.name; if (FEutaget(ERx("user"), 0, FARG_FAIL, &rarg, &pos) == OK) user = rarg.dat.name; if (FEutaget(ERx("password"), 0, FARG_FAIL, &rarg, &pos) == OK) { char *IIUIpassword(); if ((password = IIUIpassword(ERx("-P"))) == NULL) { FEutaerr(BADARG, 1, ERx("")); PCexit(FAIL); } } if (FEutaget(ERx("groupid"), 0, FARG_FAIL, &rarg, &pos) == OK) groupid = rarg.dat.name; if (FEutaget(ERx("debug"), 0, FARG_FAIL, &rarg, &pos) == OK) debug = TRUE; ibuf = STlength(buf); /* b121678: pid is no longer based on process id, but it's ** a random number instead. */ PCpid(&tmppid); TMget_stamp(&tm_stamp); MHsrand2(tmppid * tm_stamp.tms_usec); STprintf(pid, "%x", MHrand2()); #ifdef xDEBUG SIprintf(" the pid is: %s \n", pid); #endif /* create the sql file */ /* Avoid a name like "foo.xml.sql" on VMS, use pid.sql instead */ STcopy(pid, sql_fname); STcat(sql_fname, ".sql"); /* ** create in the temp location a directory ** with the name pid. set this directory ** as the working directory for impxml */ NMloc (TEMP, PATH, NULL, &tmp_dir_loc); /* make a location for TMP loc */ /* print location name */ LOcopy (&tmp_dir_loc, tmp_buf, &tmp_buff_loc); LOtos (&tmp_buff_loc, &tmp_dir); #ifdef xDEBUG SIprintf ("temploc: %s \n", tmp_dir); #endif /* make a subdir location with filename, pid */ STcopy (pid, subdir_buf); /* Initialize result loc so that everyone is happy */ LOcopy (&tmp_dir_loc, sql_loc_buf, &sql_file_loc); /* Generate location for temp subdirectory */ if (LOfaddpath (&tmp_dir_loc, subdir_buf, &sql_file_loc) != OK) { IIUGerr(E_XM0007_Locname_Failed, UG_ERR_FATAL, 2, tmp_dir, subdir_buf); /* NOTREACHED */ } /* print the location name */ LOcopy (&sql_file_loc, tmp_buf, &tmp_buff_loc); LOtos (&tmp_buff_loc, &work_dir); #ifdef xDEBUG SIprintf ("work dir loc: %s \n", work_dir); #endif /* create the subdir */ if (LOcreate (&sql_file_loc) != OK) { IIUGerr(E_XM0008_Create_Temp_Dir, UG_ERR_ERROR, 1, work_dir); PCexit(FAIL); } STcopy(work_dir, directory); #ifdef xDEBUG SIprintf ("sql file name: %s \n", sql_fname); SIprintf ("xml file name: %s \n", xmlfile); #endif /* Execute the command impxml */ STprintf (buf, ERx( "impxml -d=\"%s\" -o=\"%s\" " ), directory, sql_fname); /* encoding? */ if ( (LOfroms(PATH & FILENAME, xmlfile, &xmlfile_loc) != OK) || (SIopen(&xmlfile_loc, "r", &xmlfile_read) != OK) || (xmlfile_read == NULL) ) { IIUGerr(E_XM0009_Cannot_Open_File, UG_ERR_ERROR, 1, xmlfile); PCexit(FAIL); } /* scan XML declaration for encoding, if any */ if (stat = SIgetrec(dbuf, sizeof(dbuf) - 1, xmlfile_read) == OK) { char *d = dbuf; i4 i = 0; for (d = dbuf; d != (dbuf + sizeof(dbuf)); d++) { if (MEcmp(d, ERx("encoding="), sizeof(ERx("encoding=")) - 1) == 0) { d += sizeof(ERx("encoding=")); while (MEcmp (d, "\'", sizeof(char)) && MEcmp(d, "\"", sizeof(char))) MEcopy(d++, sizeof(char), &encode[i++]); encode[i++] = MIN_CHAR; encode[i] = EOS; STcat(buf, ERx("-x=")); STcat(buf, encode); break; } } } else if (stat != ENDFILE) { /* unable to read file, report error */ IIUGerr(E_XM000A_Cannot_Read_File, UG_ERR_ERROR, 1, xmlfile); PCexit(FAIL); } stat = SIclose(xmlfile_read); STcat(buf, xmlfile); #ifdef xDEBUG SIprintf ( " query send: %s \n", buf); #endif /* Execute the command. */ if( PCcmdline((LOCATION *) NULL, buf, PC_WAIT, (LOCATION *)NULL, &err_code) != OK ) { if (!debug) LOdelete(&sql_file_loc); PCexit(FAIL); } /* ** we should run the sql script ** sql dbname < new_filename */ /* save the current location */ LOcopy(&sql_file_loc, tmp_buf, &curr_loc); /* make a full location path to the location first */ LOfroms(FILENAME, sql_fname, &tmp_buff_loc); LOstfile(&tmp_buff_loc, &curr_loc); LOcopy (&curr_loc, tmp_buf, &tmp_buff_loc); LOtos (&tmp_buff_loc, &tmp_dir); #ifdef xDEBUG SIprintf ("sql file is: %s \n", tmp_dir); #endif /* No space between < and input file for VMS */ STprintf(buf, ERx( "sql -s %s %s %s %s <%s" ), database, user, password, groupid, tmp_dir); #ifdef xDEBUG SIprintf (" query send: %s \n", buf); #endif /* ** Execute the command. */ if( PCcmdline((LOCATION *) NULL, buf, PC_WAIT, (LOCATION *)NULL, &err_code) != OK ) { if (!debug) LOdelete(&sql_file_loc); PCexit(FAIL); } /* ** Delete the location */ if (!debug) LOdelete(&sql_file_loc); PCexit(OK); }
/* ** pp_open() -- open a file for reading. The use of the include path list ** is used to resolve files that may not be in the current default ** location. */ static STATUS pp_open( FILE **input, char *filepath, bool def_dir ) { LOCATION loc; LOCATION loctest; char buf[ MAX_LOC + 1 ]; char bufdev[ MAX_LOC + 1 ]; char bufpath[ MAX_LOC + 1 ]; char bufsave[ MAX_LOC + 1 ]; char buftest[ MAX_LOC + 1 ]; STATUS sts; IPATH *ipath; bool found = FALSE; bool file_has_path, dir_has_path; /* ** Check to see if we should first look in the default directory, ** this directory will be the first entry in the Include list. */ if (def_dir == TRUE) ipath = &ihead; else ipath = ihead.next; /* Convert the filename to open to a LOCATION */ STcopy(filepath, bufsave); if (OK != (sts = LOfroms( PATH & FILENAME, bufsave, &loc )) ) return (sts); /* ** Search for the file in each directory in the include list. ** No expansion possible if there are no include directories. */ while (ipath != NULL && found == FALSE) { /* Initialize loctest so LOaddpath will work */ LOcopy(&loc, buftest, &loctest); /* Is there a device (VMS) and/or a path for the directory? */ STcopy(ERx(""), bufdev); STcopy(ERx(""), bufpath); sts = LOdetail(ipath->pathloc, bufdev, bufpath, buf, buf, buf); if (sts != OK) break; if (STlength(bufdev) + STlength(bufpath) > 0 ) dir_has_path = TRUE; else dir_has_path = FALSE; /* Don't mess with the filename if it was an absolute path */ /* or if this include directory path is empty */ if (LOisfull(&loctest) == FALSE && dir_has_path == TRUE ) { /* Is there a path for the file? */ sts = LOdetail(&loctest, buf, bufpath, buf, buf, buf); if (sts != OK) break; file_has_path = (STlength(bufpath) > 0 ? TRUE : FALSE); /* ** If there's no path component then copy in the ** next prefix, else append it. ** If LOaddpath succeeds, the result will also have ** the original filename. If it fails then drop out. ** LOaddpath can fail if loctest is an absolute path, ** but we already checked for that. */ if ( file_has_path ) { sts =LOaddpath(ipath->pathloc, &loc, &loctest); } else { LOcopy(ipath->pathloc, buftest, &loctest); /* ** If we can't set the filename from loc, ** then something is wrong, and will continue ** to be wrong. Get out of the loop. */ sts = LOstfile(&loc, &loctest); } } /* ** If all of the filename manipulations have succeeded ** then check for the file's existance. */ if (sts == OK && LOexist(&loctest) == OK) { found = TRUE; LOcopy(&loctest, buf, &loc); } /* Prepare for the next path in the include list */ ipath = ipath->next; } /* ** If the file existed then return back the result from the open, ** otherwise all of our efforts were a bust, return back a failure. */ if (found) return ( SIfopen(&loc, ERx("r"), SI_TXT, MAX_LINE, input) ); else return (FAIL); }
i4 main( i4 argc, char * argv[]) { char c='\0'; char *tz_file=NULL; char *tz_name=NULL; char *tz_def; char tzname[TM_MAX_TZNAME+1]; char tzfile[MAX_LOC+1]; char ioptarg[MAX_LOC + 1]; char tz_pmvalue[MAX_LOC + 1]; char tz_pmname[MAX_LOC + 1]; char *p, *ip, *pm_value; char *tm_tztype; char chr='/'; char *out_file; i4 ioptind=1, i; char *tm_tztime; i4 timecnt, tempi, temptype, temptz; char buf[sizeof(TM_TZ_CB)+TM_MAX_TIMECNT*(sizeof(i4)+1)]; FILE *fid; LOCATION loc_root, tmp_loc; STATUS status=OK; TM_TZ_CB *tm_tz_cb; struct timevect time_v; appname = argv[0]; if( TMtz_getopt(argc, argv, "n:name:f:file", &ioptind, ioptarg, &c) == OK) { switch (c) { case 'f': tz_file = ioptarg; break; case 'n': tz_name = ioptarg; break; default: break; } } else { TMtz_usage(); PCexit(FAIL); } if( tz_file == NULL) { if( tz_name == NULL) { /* Get II_TIMEZONE_NAME value */ NMgtAt(ERx("II_TIMEZONE_NAME"), &tz_def); if (!tz_def || !(*tz_def)) { SIprintf("%s: %s_TIMEZONE_NAME is not set\n", appname, SystemVarPrefix); PCexit(FAIL); } STncpy(tzname, tz_def, TM_MAX_TZNAME); tzname[ TM_MAX_TZNAME ] = EOS; } else { STncpy(tzname, tz_name, TM_MAX_TZNAME); tzname[ TM_MAX_TZNAME ] = EOS; } PMinit(); if( PMload( NULL, (PM_ERR_FUNC *)NULL) != OK) { SIprintf("%s: Error loading PM %s_CONFIG/config.dat file\n", appname, SystemVarPrefix); PCexit(FAIL); } /* Get timezone file name */ STprintf( tz_pmname, ERx("%s.*.tz.%s"), SystemCfgPrefix, tzname); if( PMget( tz_pmname, &pm_value) != OK) { SIprintf("%s: Error locating %s in PM config.dat file\n", appname, tz_pmname); PCexit(FAIL); } do { if((status = NMloc(FILES, PATH, ERx("zoneinfo"), &loc_root)) != OK) break; #if defined(conf_BUILD_ARCH_32_64) && defined(BUILD_ARCH64) if((status = LOfaddpath(&loc_root, ERx("lp64"), &loc_root)) != OK) break; #endif #if defined(conf_BUILD_ARCH_64_32) && defined(BUILD_ARCH32) { /* ** Reverse hybrid support must be available in ALL ** 32bit binaries */ char *rhbsup; NMgtAt("II_LP32_ENABLED", &rhbsup); if ( (rhbsup && *rhbsup) && ( !(STbcompare(rhbsup, 0, "ON", 0, TRUE)) || !(STbcompare(rhbsup, 0, "TRUE", 0, TRUE)))) status = LOfaddpath(&loc_root, "lp32", &loc_root); } #endif /* reverse hybrid */ STcopy( pm_value, tz_pmvalue); /* ** Compose the directory path */ for( p = tz_pmvalue, ip = tz_pmvalue; (p = STchr(ip, chr)) != NULL;) { *p = EOS; if((status = LOfaddpath(&loc_root, ip, &loc_root)) != OK) break; ip = CMnext(p); } /* ** Add file name to the directory path */ if((status = LOfroms(FILENAME, ip, &tmp_loc)) != OK) break; status = LOstfile( &tmp_loc, &loc_root); } while( FALSE); if( status != OK) { SIprintf("%s: Error composing timezone file name for %s\n", appname, tz_pmvalue); PCexit(FAIL); } } else { STcopy("<unknown>", tzname); STncpy( tzfile, tz_file, MAX_LOC); tzfile[ MAX_LOC ] = EOS; if( LOfroms(FILENAME&PATH, tzfile, &loc_root) != OK) { SIprintf("%s: Error composing timezone file name for %s\n", appname, tz_pmvalue); PCexit(FAIL); } } /* ** Now open the timezone information file */ do { if((status = SIfopen( &loc_root, ERx("r"), SI_VAR, sizeof buf, &fid)) != OK) break; status = SIread(fid, sizeof buf, &i, buf); status = SIclose(fid); } while(FALSE); if( status != OK) { LOtos( &loc_root, &out_file); SIprintf("%s: Error opening %s for timezone %s\n", appname, out_file, tzname); PCexit(FAIL); } tm_tz_cb = (TM_TZ_CB *)&buf; I4ASSIGN_MACRO( tm_tz_cb->timecnt, timecnt); /* Make sure the input file has correct file size */ if( timecnt > TM_MAX_TIMECNT || timecnt < 0 || i != sizeof(TM_TZ_CB) + timecnt*(sizeof(i4)+1)) { LOtos( &loc_root, &out_file); SIprintf( "%s: Invalid file format for timezone file %s for timezone %s\n", appname, out_file, tzname); SIprintf( " File size: %d, Expected file size: %d, time periods: %d\n", i, sizeof(TM_TZ_CB) + timecnt*(sizeof(i4)+1), timecnt); PCexit(FAIL); } /* Now we are all set to display the content of timezone information file */ LOtos( &loc_root, &out_file); SIprintf("\n\n"); SIprintf("timezone name: %s\n", tzname); SIprintf("timezone file: %s\n", out_file); SIprintf("-------------------------------------"); SIprintf("-------------------------------------\n"); if(timecnt == 0) { I4ASSIGN_MACRO( tm_tz_cb->tzinfo[0].gmtoff, tempi); SIprintf(" Fixed GMT offset (secs): %d\n", tempi); } else { SIprintf("\tPeriod Begin"); SIprintf("\t\tGMT offset\n"); SIprintf("\t(YYYY_MM_DD HH:MM)"); SIprintf("\t(Minute)\n\n"); tm_tztype = buf + sizeof(TM_TZ_CB); tm_tztime = tm_tztype + timecnt; i=0; while( i < timecnt) { I4ASSIGN_MACRO( *tm_tztime, tempi); /* Adjust for timezone */ if( i == 0) temptype = (i4)tm_tztype[i+1]; else temptype = (i4)tm_tztype[i-1]; I4ASSIGN_MACRO( tm_tz_cb->tzinfo[temptype].gmtoff, temptz); /* Get real timezone */ tempi += temptz; temptype = (i4)tm_tztype[i]; I4ASSIGN_MACRO( tm_tz_cb->tzinfo[temptype].gmtoff, temptz); TMtz_cvtime( tempi, &time_v); SIprintf("\t%04d_%02d_%02d %02d:%02d\t%d\t%s\n", time_v.tm_year+1900, time_v.tm_mon+1, time_v.tm_mday, time_v.tm_hour, time_v.tm_min, temptz/60, tm_tz_cb->tzlabel + tm_tz_cb->tzinfo[temptype].abbrind); tm_tztime += sizeof(i4); i++; } } PCexit(OK); }