home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / slfinsta.zip / instsup.c < prev    next >
C/C++ Source or Header  |  2000-03-26  |  12KB  |  522 lines

  1. /* $Id: instsup.c,v 1.1 2000/03/27 04:52:55 ktk Exp $ */
  2.  
  3. /*
  4.  *  instsup.c (c) 1999,2000 Brian Smith
  5.  */
  6.  
  7. #define INCL_WIN       /* Window Manager Functions     */
  8. #define INCL_DOS
  9. #define INCL_BASE
  10. #include <os2.h>
  11. #include <stdio.h>
  12. #include <stddef.h>
  13. #include <stdlib.h>
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #include <fcntl.h>
  17. #include <process.h>
  18. #include <sys/types.h>
  19. #include "install.h"
  20.  
  21. HWND hwndMLE = 0;
  22. extern char *INSTALLER_TITLE;
  23. extern char tempPath[], installdir[], csfile[], bufile[], bootdrive[], instlog[];
  24. extern int installstate, success;
  25.  
  26. FILE *self;
  27.  
  28. char *replaceem(char *orig);
  29.  
  30. /* This should return the current color depth */
  31. ULONG color_depth(void)
  32. {
  33.     HDC hdc = WinOpenWindowDC(HWND_DESKTOP);
  34.     LONG colors;
  35.  
  36.     DevQueryCaps(hdc, CAPS_COLORS, 1, &colors);
  37.     DevCloseDC(hdc);
  38.     return colors;
  39. }
  40.  
  41. /*
  42.  * Call the reboot vector.
  43.  */
  44. void reboot(void)
  45. {
  46. #define SYSFUNC               0xD5
  47. #define REBOOT                0xAB
  48. #define REBOOTDEV             "\\DEV\\DOS$"
  49.  
  50.     APIRET rc;
  51.     HFILE  hREBOOT;
  52.     ULONG  ulAction;
  53.  
  54.     rc = DosOpen(REBOOTDEV,
  55.                  &hREBOOT,
  56.                  &ulAction,
  57.                  0L,
  58.                  FILE_NORMAL,
  59.                  FILE_OPEN,
  60.                  OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
  61.                  0L);
  62.     if (rc == 0)
  63.     {
  64.         DosDevIOCtl(hREBOOT,
  65.                     SYSFUNC,
  66.                     REBOOT,
  67.                     NULL,
  68.                     0L,
  69.                     NULL,
  70.                     NULL,
  71.                     0L,
  72.                     NULL);
  73.         DosClose(hREBOOT);
  74.     }
  75. }
  76.  
  77. /*
  78.  * Display an informational dialog box to the user with the given text.
  79.  */
  80. int mesg(char *format, ...) {
  81.     va_list args;
  82.     char outbuf[4096];
  83.  
  84.     va_start(args, format);
  85.     vsprintf(outbuf, format, args);
  86.     va_end(args);
  87.  
  88.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, outbuf, INSTALLER_TITLE, 0, MB_OK | MB_INFORMATION | MB_MOVEABLE);
  89.  
  90.     return strlen(outbuf);
  91. }
  92.  
  93. int checktext(char *text, char *buffer, int buflen)
  94. {
  95.     int z, len = strlen(text);
  96.  
  97.     for(z=0;z<(buflen-len);z++)
  98.     {
  99.         if(memcmp(text, &buffer[z], len) == 0)
  100.             return z;
  101.     }
  102.     return -1;
  103.  
  104. }
  105.  
  106. /*
  107.  * Returns the offset withing the executable to the specified text.
  108.  */
  109. long findtext(char *text)
  110. {
  111.     char buffer[512];
  112.     int offset;
  113.     unsigned long curpos = 0;
  114.  
  115.     fseek(self, 0, SEEK_SET);
  116.     fread(buffer, 1, 512, self);
  117.     if((offset = checktext(text, buffer, 512)) > -1)
  118.         return offset;
  119.     while(!feof(self))
  120.     {
  121.         memcpy(buffer, &buffer[256], 256);
  122.         fread(&buffer[256], 1, 256, self);
  123.         curpos += 256;
  124.         if((offset = checktext(text, buffer, 512)) > -1)
  125.             return offset+curpos;
  126.  
  127.     }
  128.     return -1;
  129. }
  130.  
  131. /* We encode archive search text so we don't get confused
  132.  * by the string table - I was using LXLite to take care
  133.  * of this problem on OS/2 but in portable code this may
  134.  * not be an option. */
  135. char *decode(char *input)
  136. {
  137.     char    *result;
  138.     int    i = 0;
  139.  
  140.     result = (char *)malloc(strlen(input) / 2 + 1);
  141.  
  142.     while (input[0] && input[1])
  143.     {
  144.         result[i] = ((input[0] - 0x41) << 4) | (input[1] - 0x41);
  145.         input += 2;
  146.         i++;
  147.     }
  148.     result[i] = '\0';
  149.  
  150.     return result;
  151. }
  152.  
  153. /*
  154.  * Window Procedure for the viewer window.
  155.  */
  156. MRESULT    EXPENTRY ViewWndProc(HWND hWnd, ULONG msg, MPARAM mp1,    MPARAM mp2)
  157.  
  158. {
  159.     SWP swp;
  160.  
  161.     int th, wh, ww;
  162.     static int bw, bh;
  163.  
  164.     switch ( msg )
  165.     {
  166.     case WM_CREATE:
  167.         bw = WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER);
  168.         bh = WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
  169.         break;
  170.     case WM_SIZE:
  171.         WinQueryWindowPos(hWnd, &swp);
  172.         wh = swp.cy;
  173.         ww = swp.cx;
  174.         swp.cy = 0;
  175.         WinQueryWindowPos(WinWindowFromID(hWnd, FID_TITLEBAR), &swp);
  176.         th = swp.cy;
  177.         WinSetWindowPos(hwndMLE, NULLHANDLE, bw, bh, ww, wh - th, SWP_MOVE | SWP_SIZE);
  178.         return(WinDefWindowProc(hWnd, msg, mp1, mp2));
  179.         break;
  180.     default :
  181.         return(WinDefWindowProc(hWnd, msg, mp1, mp2));
  182.     }
  183.     return(0L);
  184. }
  185.  
  186. /*
  187.  * Opens a Window which contains the text specfied by given filename.
  188.  */
  189. void viewfile(char *filename, HAB localhab)
  190. {
  191.     HWND hwndFrame, /*hwndMLE,*/ hwndClient;
  192.     ULONG flStyle = FCF_MINMAX | FCF_SYSMENU | FCF_TITLEBAR |
  193.         FCF_SIZEBORDER | FCF_SHELLPOSITION | FCF_TASKLIST;
  194.     PPRESPARAMS ppp;
  195.     char deffont[] = "9.Warpsans";
  196.  
  197.     WinRegisterClass(localhab, "VIEW", ViewWndProc, CS_SIZEREDRAW, 32);
  198.  
  199.     hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  200.                                   WS_VISIBLE | WS_MAXIMIZED,
  201.                                   &flStyle,
  202.                                   "VIEW",
  203.                                   filename,
  204.                                   0L,
  205.                                   NULLHANDLE,
  206.                                   0L,
  207.                                   &hwndClient);
  208.  
  209.     ppp = malloc((sizeof(ULONG) * 3) + strlen(deffont) + 1);
  210.     ppp->cb = (sizeof(ULONG) * 2) + strlen(deffont) + 1;
  211.     ppp->aparam[0].id = PP_FONTNAMESIZE;
  212.     ppp->aparam[0].cb = strlen(deffont)+1;
  213.     memcpy(&ppp->aparam[0].ab, deffont, strlen(deffont)+1);
  214.  
  215.     hwndMLE = WinCreateWindow(hwndFrame,
  216.                               WC_MLE,
  217.                               "",
  218.                               WS_VISIBLE | MLS_VSCROLL | MLS_WORDWRAP | MLS_READONLY | MLS_BORDER,
  219.                               0,0,100,100,
  220.                               hwndFrame,
  221.                               HWND_TOP,
  222.                               999L,
  223.                               NULL,
  224.                               ppp);
  225.  
  226.     WinSetWindowPos(hwndFrame, HWND_TOP, 0,0,0,0,SWP_MAXIMIZE | SWP_ZORDER);
  227.     WinSetWindowPos(hwndMLE, HWND_TOP, 0,0,0,0,SWP_SHOW);
  228.  
  229.     if(!hwndMLE)
  230.     {
  231.         mesg("Error %lu while creating MLE window.", WinGetLastError(localhab));
  232.     }
  233.     else
  234.     {
  235.         FILE *f;
  236.         char buffer[1024];
  237.         ULONG bytes, point = -1;
  238.  
  239.         if((f = fopen(filename, "rb")) != NULL)
  240.         {
  241.             WinSendMsg(hwndMLE, MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(1024L));
  242.             while(!feof(f))
  243.             {
  244.                 bytes = fread(buffer, 1, 1024, f);
  245.                     WinSendMsg(hwndMLE, MLM_IMPORT, MPFROMP(&point), MPFROMLONG(bytes));
  246.             }
  247.             fclose(f);
  248.         }
  249.  
  250.     }
  251. }
  252.  
  253. /*
  254.  * Removes any carriage returns or line feeds from the buffer.
  255.  */
  256. void stripcrlf(char *buffer)
  257. {
  258.     int z, len = strlen(buffer);
  259.  
  260.     for(z=0;z<len;z++)
  261.     {
  262.         if(buffer[z] == '\r' || buffer[z] == '\n')
  263.         {
  264.             buffer[z] = 0;
  265.             return;
  266.         }
  267.     }
  268. }
  269.  
  270. /*
  271.  * Returns the space free on a given drive... where 0 is A: in MB
  272.  */
  273. unsigned long drivefree(int drive)
  274. {
  275.     ULONG   aulFSInfoBuf[40] = {0};
  276.     APIRET  rc               = NO_ERROR;
  277.     double    bytesFree;
  278.  
  279.     DosError(FERR_DISABLEHARDERR);
  280.     rc = DosQueryFSInfo(drive,
  281.                         FSIL_ALLOC,
  282.                         (PVOID)aulFSInfoBuf,
  283.                         sizeof(aulFSInfoBuf));
  284.  
  285.     DosError(FERR_ENABLEHARDERR);
  286.     if (rc != NO_ERROR)
  287.         return 0;
  288.  
  289.     bytesFree = (double)aulFSInfoBuf[3] * (double)aulFSInfoBuf[1] * (USHORT)aulFSInfoBuf[4];
  290.     return (unsigned long)(bytesFree / (1024.0 * 1024.0));
  291. }
  292.  
  293.  
  294. /*
  295.  * Display a fatal error message and set the abort flag in case we are in a secondary thread.
  296.  */
  297. void error(char *format, ...) {
  298.     va_list args;
  299.     char errstring[1024];
  300.  
  301.     va_start(args, format);
  302.     vsprintf(errstring, format, args);
  303.     va_end(args);
  304.  
  305.     if(installstate != ABORTED)
  306.     {
  307.         success=1;
  308.         installstate=ABORTED;
  309.     }
  310.     WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, errstring, INSTALLER_TITLE, 0, MB_OK | MB_ERROR | MB_MOVEABLE | MB_SYSTEMMODAL);
  311. }
  312.  
  313. void setdrivedir(char *drivedir)
  314. {
  315.     DosSetDefaultDisk((int)(drivedir[0]-'A'+1));
  316.     DosSetCurrentDir(drivedir);
  317. }
  318.  
  319. /*
  320.  * Make the TEMP directory the current directory, or the root directory of the boot drive.
  321.  */
  322. void settempdir(void)
  323. {
  324.     char *envdir = getenv("TMP");
  325.     int    len;
  326.  
  327.     if (!envdir)
  328.         envdir = getenv("TEMP");
  329.     if (!envdir)
  330.         envdir = replaceem("%BOOTDRIVE%:\\");
  331.     strcpy(tempPath,envdir);
  332.     len = strlen(tempPath);
  333.     if (len > 3 && tempPath[len-1] == '\\')
  334.         tempPath[len-1] = 0;
  335.     strupr(tempPath);
  336.     setdrivedir(tempPath);
  337. }
  338.  
  339. void getbootdrive(void)
  340. {
  341.     ULONG   ulSysInfo;
  342.  
  343.     DosQuerySysInfo(QSV_BOOT_DRIVE,
  344.             QSV_BOOT_DRIVE,
  345.             (PVOID)&ulSysInfo,
  346.             sizeof(ULONG));
  347.  
  348.     bootdrive[0]=installdir[0]=instlog[0]=csfile[0]=bufile[0]=(char)('A'+(ulSysInfo-1));
  349. }
  350. /*
  351.  * Start an application using CMD.EXE.
  352.  */
  353. int cmdrun(char *progname)
  354. {
  355.     STARTDATA SData       = {0};
  356.     PSZ       PgmTitle    = "",
  357.               PgmName     = "CMD.EXE";
  358.     APIRET    rc          = NO_ERROR;
  359.     PID       pid         = 0;
  360.     ULONG     ulSessID    = 0;
  361.     CHAR      achObjBuf[256] = {0};
  362.     HQUEUE hqQueue;
  363.     REQUESTDATA rdRequest;
  364.     ULONG ulSzData;
  365.     BYTE bPriority;
  366.     PVOID pvData;
  367.  
  368.     SData.Length  = sizeof(STARTDATA);
  369.     SData.Related = SSF_RELATED_CHILD;
  370.     SData.FgBg    = SSF_FGBG_BACK;
  371.     SData.TraceOpt = SSF_TRACEOPT_NONE;
  372.     SData.PgmTitle = PgmTitle;
  373.     SData.PgmName = PgmName;
  374.     SData.PgmInputs = progname;
  375.  
  376.     SData.TermQ = "\\QUEUES\\CHILD.QUE";
  377.     SData.Environment = 0;
  378.     SData.InheritOpt = SSF_INHERTOPT_SHELL;
  379.     SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
  380.     SData.IconFile = 0;
  381.     SData.PgmHandle = 0;
  382.  
  383.     SData.PgmControl = SSF_CONTROL_INVISIBLE;
  384.     SData.Reserved = 0;
  385.     SData.ObjectBuffer = achObjBuf;
  386.     SData.ObjectBuffLen = (ULONG) sizeof(achObjBuf);
  387.  
  388.     if(!(rc = DosCreateQueue(&hqQueue, QUE_FIFO | QUE_CONVERT_ADDRESS,"\\QUEUES\\CHILD.QUE")))
  389.     {
  390.         if(!(rc = DosStartSession(&SData, &ulSessID, &pid)))
  391.             if(!(rc=DosReadQueue(hqQueue, &rdRequest, &ulSzData, &pvData, 0, 0, &bPriority, 0)))
  392.                 DosFreeMem(pvData);
  393.         DosCloseQueue(hqQueue);
  394.     }
  395.  
  396.     if (rc != NO_ERROR) {
  397.         mesg("Error %d while attempting to run %s!", rc, progname);
  398.         return 1;
  399.     }
  400.     return NO_ERROR;
  401. }
  402.  
  403. /*
  404.  * Makes a folder on the desktop.
  405.  */
  406. void MakeFolder(char Title[], char Icon[], char dest[], char id[], char setup[])
  407. {
  408.     char szArg[200];
  409.  
  410.     memset(szArg,0,sizeof(szArg));
  411.  
  412.     if ((Icon != NULL) && (strlen(Icon) != 0))
  413.     {
  414.         strcat(szArg,"ICONFILE=");
  415.         strcat(szArg,Icon);
  416.     }
  417.  
  418.     if ((id != NULL) && (strlen(id) != 0))
  419.     {
  420.         strcat(szArg,";OBJECTID=");
  421.         strcat(szArg,id);
  422.     }
  423.  
  424.     if ((setup != NULL) && (strlen(setup) != 0))
  425.     {
  426.         strcat(szArg,";");
  427.         strcat(szArg,setup);
  428.     }
  429.  
  430.     WinCreateObject("WPFolder",Title,szArg,dest,CO_REPLACEIFEXISTS);
  431. }
  432.  
  433. /*
  434.  * Makes a Program object on the desktop.
  435.  */
  436. void MakeProgram(char Title[], char Program[], char Icon[], char dest[], char id[], char setup[])
  437. {
  438.     char szArg[200];
  439.  
  440.     memset(szArg,0,sizeof(szArg));
  441.  
  442.     strcat(szArg,"EXENAME=");
  443.     strcat(szArg,Program);
  444.  
  445.     if ((Icon != NULL) && (strlen(Icon) != 0))
  446.     {
  447.         strcat(szArg,";ICONFILE=");
  448.         strcat(szArg,Icon);
  449.     }
  450.  
  451.     if ((id != NULL) && (strlen(id) != 0))
  452.     {
  453.         strcat(szArg,";OBJECTID=");
  454.         strcat(szArg,id);
  455.     }
  456.  
  457.     if ((setup != NULL) && (strlen(setup) != 0))
  458.     {
  459.         strcat(szArg,";");
  460.         strcat(szArg,setup);
  461.     }
  462.  
  463.     WinCreateObject("WPProgram",Title,szArg,dest,CO_REPLACEIFEXISTS);
  464. }
  465. /*
  466.  * Makes a user defined object on the desktop.
  467.  */
  468. void MakeObject(char Title[], char oclass[], char dest[], char id[], char setup[])
  469. {
  470.     char szArg[200];
  471.  
  472.     memset(szArg,0,sizeof(szArg));
  473.  
  474.     if ((oclass == NULL) || (strlen(oclass) == 0))
  475.         return;
  476.  
  477.     if ((id != NULL) && (strlen(id) != 0))
  478.     {
  479.         strcat(szArg,"OBJECTID=");
  480.         strcat(szArg,id);
  481.     }
  482.  
  483.     if ((setup != NULL) && (strlen(setup) != 0))
  484.     {
  485.         if ((id != NULL) && (strlen(id) != 0))
  486.             strcat(szArg,";");
  487.         strcat(szArg,setup);
  488.     }
  489.  
  490.     WinCreateObject(oclass,Title,szArg,dest,CO_REPLACEIFEXISTS);
  491. }
  492. /*
  493.  * Makes a shadow on the desktop.
  494.  */
  495. void MakeShadow(char Title[], char reference[], char dest[], char id[])
  496. {
  497.     char szArg[400];
  498.  
  499.     memset(szArg,0,sizeof(szArg));
  500.  
  501.     strcpy(szArg,"SHADOWID=");
  502.     strcat(szArg,reference);
  503.     if ((id != NULL) && (strlen(id) != 0))
  504.     {
  505.         strcat(szArg,";OBJECTID=");
  506.         strcat(szArg,id);
  507.     }
  508.     strcat(szArg,";");
  509.     WinCreateObject("WPShadow",Title,szArg,dest,CO_REPLACEIFEXISTS);
  510. }
  511.  
  512. void PM_backslash(
  513.     char *s)
  514. {
  515.     unsigned int pos = strlen(s);
  516.     if (s[pos-1] != '\\') {
  517.         s[pos] = '\\';
  518.         s[pos+1] = '\0';
  519.         }
  520. }
  521.  
  522.