📄 yabinst.c
字号:
/*
Install-Program for yabasic
written by Marc-Oliver Ihm in 1996.
Date of last change:
*/
#define DOLC "February 23, 1999"
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (the file is named "COPYING");
if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*----------- includes -------------*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <shlobj.h>
#include <shellapi.h>
#include <commctrl.h>
#include <ole2.h>
#include "resource.h"
/*------------ defines -------------------*/
/* names and symbols for basic */
#define BASIC_NAME "Yabasic"
#define BASIC_DOKU "Yabasic"
#define BASIC_EXE "yabasic.exe"
#define BASIC_EXTENSION ".yab"
#define BASIC_VERFILE "yabver.txt"
#define BASIC_DEFVERSION 2.3
#define BASIC_ICON "yabico.ico"
#define BASIC_DEMO "yabdemo"
#define BASIC_README "readme.txt"
#define BASIC_LICENSE "copying.txt"
#define BASIC_SETUP "setup.exe"
#define BASIC_LOG "yabasic.log"
/* headings for messageboxes */
#define INSTALL_HEADING " Install yabasic !"
#define REMOVE_HEADING " Remove yabasic !"
/* basic-defaults */
#define DEFAULTPATH "c:\\yabasic\\"
#define DEFAULTFONT "swiss13"
#define DEFAULTGEOMETRY "+10+10"
/* shortcuts for end-message */
#define INSTALL_CANCELLED 1
#define INSTALL_ABORTED 2
#define INSTALL_SUCCESS 3
#define INSTALL_FAILURE 4
#define REMOVE_SUCCESS 6
#define REMOVE_FAILURE 7
#define REMOVE_CANCELLED 8
#define SILENT 9
/* shortcuts for registry */
#define ROOT HKEY_CLASSES_ROOT
#define LOCAL HKEY_LOCAL_MACHINE
#define SOFT "SOFTWARE\\"
#define UNINSTALL "SOFTWARE\\MICROSOFT\\WINDOWS\\CURRENTVERSION\\UNINSTALL\\"
/* operationmodes for My...() functions */
#define INSTALL 1
#define REMOVE 2
/* defines for files() */
#define RESET 1
#define NEXT 2
/* shortcut for standard Message Box Style */
#define MB_STYLE MB_OK|MB_SYSTEMMODAL|MB_ICONINFORMATION
/* standard string length */
#define SSLEN 300
#if !defined(TRUE)
#define TRUE (1==1)
#endif
#ifndef FALSE
#define FALSE (1!=1)
#endif
/*------------ global types -----------------------*/
typedef struct linkinfo {
int folder; /* registry key */
char *link; /* name of link */
char *file; /* name of file */
char *desc; /* description of link */
char *icon; /* name of icon for link */
int removeonly; /* true, if icon should be removed but not installed */
} LINKINFO;
/*------------ global variables -------------------*/
char *currentpath; /* current path */
char *installpath; /* path, where to install */
char *temppath; /* path to store temporary files */
char logs[10000]; /* string to compose log-message */
int here; /* install in current path ? */
int install; /* install or remove ? */
int copied; /* true if copy in temp-directory */
int total_progress; /* number of steps to advance progress counter */
HINSTANCE this_instance; /* instance */
float newversion; /* version to be installed */
/*------------ prototypes -------------------*/
/* My...() functions */
int MyLinks(int); /* add or remove shell links */
int MyFiles(int); /* copy or delete files */
int MyRegs(int); /* add or delete entries to or from registry */
/* dialog callbacks */
BOOL CALLBACK pathdialog(HWND,UINT,WPARAM,LPARAM);/* choose installpath */
BOOL CALLBACK progressdialog(HWND,UINT,WPARAM,LPARAM);/* show progress */
UINT CALLBACK HookProc(HWND,UINT,WPARAM,LPARAM); /* hook for save as */
/* registry manipulation */
int delreg(HKEY,char *,char *); /* delete keys from Registry */
char *getreg(char *); /* get keys from Registry */
int putreg(HKEY,char *,char *,char *); /* put keys into Registry */
/* shell link functions */
/* Create link: */
HRESULT CreateShellLink(LINKINFO *,char *);
/* delete a shell link */
HRESULT DeleteShellLink(LINKINFO *);
/* functions dealing with progress bar */
void progress(char *); /* show progress */
/* miscellanous functions */
void end(int); /* display message and terminate */
char *app(char *trail); /* append trail to installpath */
int Copy(char *,char *,int); /* copy files */
char *brushup(char *); /* change to upper case, add slash */
char *enumfiles(int); /* give filenames, one after another */
LINKINFO *enumlinks(int); /* give back links, one after the other */
void logit(char *); /* write text to logfile */
int MyMessage(HWND,LPCSTR,LPCSTR,UINT); /* wrapper for MessageBox() */
/*------------ main program --------------*/
int WINAPI WinMain(HINSTANCE _this_instance,
HINSTANCE prev_instance,
LPSTR commandline,
int win_state)
{
float oldversion; /* version already installed */
FILE *verfile; /* file with version */
int cancel; /* return value of dialog */
int success; /* return value of My..() functions */
char string[SSLEN]; /* multi-purpose string */
/* copy intance information to global variable */
this_instance=_this_instance;
/* get path for temporary files */
GetTempPath(SSLEN,string);
temppath=brushup(string);
/* write to log */
sprintf(logs,"--Temppath: '%s'\n",temppath);
logit(logs);
sprintf(logs,"--Commandline='%s'\n",commandline);
logit(logs);
/* 'parse' commandline */
install=TRUE;
if (!strcmp(commandline,"remove")) install=FALSE;
copied=FALSE;
if (!strncmp(commandline,"copied",6)) {
install=FALSE;
copied=TRUE;
}
/* get current path */
GetCurrentDirectory(SSLEN,string);
currentpath=brushup(string);
/* write to log */
sprintf(logs,"--Currentpath: '%s'\n",currentpath);
logit(logs);
if (install) { /* install yabasic */
char *ver; /* string containing installed version */
/* set advance for progresscount */
total_progress=27;
/* get and check versions ... */
/* get new version from file */
sprintf(string,"%s%s",currentpath,BASIC_VERFILE);
verfile=fopen(string,"r");
if (!verfile || !fscanf(verfile,"%f",&newversion) ||
newversion<BASIC_DEFVERSION) {
newversion=(float)BASIC_DEFVERSION;
sprintf(logs,"--Couldn't retrieve new version,"
" using %5.2f instead\n",newversion);
logit(logs);
}
else {
sprintf(logs,"--New version: %5.2f\n",newversion);
logit(logs);
}
if (verfile) fclose(verfile);
/* get old version */
ver=getreg("version");
oldversion=0;
if (ver) sscanf(ver,"%f",&oldversion);
/* get confirmation */
sprintf(string,"This will install\n Yabasic, Version %g,\ndo You want to proceed ?",newversion);
if (MyMessage(NULL,string,INSTALL_HEADING,
MB_YESNO | MB_ICONQUESTION | MB_SYSTEMMODAL)==IDNO)
end(INSTALL_CANCELLED);
/* check versions */
if (oldversion>newversion) {
sprintf(string,"A newer Version of "BASIC_NAME
" has already been installed:\n"
" \talready installed: \t%5.2f\n"
" \tto be installed: \t%5.2f\n"
"Shall the installation proceed, superseeding the\n"
"existing version ?",oldversion,newversion);
if (MyMessage(NULL,string,INSTALL_HEADING,
MB_YESNO | MB_ICONQUESTION | MB_SYSTEMMODAL)==IDNO)
end(INSTALL_CANCELLED);
}
/* get path */
installpath=getreg("path");
if (installpath==NULL) installpath=DEFAULTPATH;
cancel=!DialogBox((HANDLE)this_instance,
MAKEINTRESOURCE(IDD_PATHDIALOG),
(HWND)NULL,(DLGPROC)pathdialog);
if (cancel) end(INSTALL_CANCELLED);
/* brush up path */
installpath=brushup(installpath);
here=!strcmp(currentpath,installpath);
/* write to log */
sprintf(logs,"--Installpath: '%s'\n",installpath);
logit(logs);
/* check for disk-space */
{
DWORD spc,bps,frcl,tncl;
float total;
int answer;
sprintf(string,"%c:/",*installpath);
GetDiskFreeSpace(string,&spc,&bps,&frcl,&tncl);
total=(float)frcl*spc*bps/1024;
if (total<1024) {
sprintf(string,"Free disk space is only %g kB!\n"
"Proceed anyway ?",total);
answer=MyMessage(NULL,string,INSTALL_HEADING,
MB_YESNO | MB_SYSTEMMODAL | MB_ICONINFORMATION);
if (answer==IDNO) end(INSTALL_ABORTED);
}
}
/* make entry in the registry */
success=MyRegs(INSTALL);
if (!success) {
MyMessage(NULL,"Failed to make entries in the Registry !",
INSTALL_HEADING,MB_STYLE);
end(INSTALL_FAILURE);
}
/* create shell links */
success=MyLinks(INSTALL);
if (!success) {
MyMessage(NULL,"Failed to add entry to the start-Menu !",
INSTALL_HEADING,MB_STYLE);
end(INSTALL_FAILURE);
}
/* create directory */
progress("Creating directory.");
CreateDirectory(installpath,NULL);
/* copy files */
progress("Copying files.");
success=MyFiles(INSTALL);
if (!success) {
MyMessage(NULL,"Couldn't copy files !",
INSTALL_HEADING,MB_STYLE);
end(INSTALL_FAILURE);
}
/* installation successfull ! */
end(INSTALL_SUCCESS);
}
else { /* remove yabasic */
if (!copied)
if (MyMessage(NULL,"Really remove yabasic ?",REMOVE_HEADING,
MB_YESNO | MB_ICONQUESTION | MB_SYSTEMMODAL)==IDNO)
end(REMOVE_CANCELLED);
/* get installpath */
installpath=getreg("path");
installpath=brushup(installpath);
if (installpath==NULL) {
MyMessage(NULL,"Could not find installation path !",
REMOVE_HEADING,MB_STYLE);
end(REMOVE_FAILURE);
}
/* write to log */
sprintf(logs,"--Installpath: '%s'\n",installpath);
logit(logs);
/* set advance for progresscount */
total_progress=9;
here=!strcmp(currentpath,installpath);
{/* remove files */
HANDLE from;
DWORD pid;
STARTUPINFO start;
PROCESS_INFORMATION proc;
if (!copied) {
char to[SSLEN];
char *from;
char string[SSLEN]; /* multi purpose string */
/* change registry, to point to new location */
sprintf(string,"%s %s",to,"remove");
putreg(LOCAL,UNINSTALL BASIC_NAME,"UninstallString",string);
GetTempPath(SSLEN,to);
strcat(to,BASIC_SETUP);
from=app(BASIC_SETUP);
CopyFile(from,to,FALSE);
MyFiles(REMOVE);
GetStartupInfo(&start);
pid=GetCurrentProcessId();
sprintf(string,"%s copied %d",to,pid);
if (!CreateProcess(NULL,string,NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,
NULL,NULL,&start,&proc)) {
MyMessage(NULL,"Couldn't hand over",REMOVE_HEADING,MB_STYLE);
end(REMOVE_FAILURE);
}
end(SILENT);
}
/* from now on: copied */
/* get process id */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -