1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 |
- /*
- * CDE - Common Desktop Environment
- *
- * Copyright (c) 1993-2012, The Open Group. All rights reserved.
- *
- * These libraries and programs are free software; you can
- * redistribute them and/or modify them under the terms of the GNU
- * Lesser General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * These libraries and programs are distributed in the hope that
- * they will be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Lesser General Public License for more
- * details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with these libraries and programs; if not, write
- * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
- * Floor, Boston, MA 02110-1301 USA
- */
- #include <stdio.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <Dt/Icon.h>
- #include <stdlib.h>
- #include "dtcreate.h"
- #include "cmnrtns.h"
- #include "fileio.h"
- #include "parser.h"
- #include "ErrorDialog.h"
- #define COMMENT_CHAR '#'
- #define MAX_KEY 10000
- #define MAX_KEY_SIZE 5
- #define MAX_HEADER_LINES 13
- /*****************************************************************************/
- /* */
- /* CalculateMagicCookie */
- /* */
- /* description: Calculates a integer (the "key" value) which reflects the */
- /* contents of the given file from the current file-offset */
- /* to the end of file. */
- /* Comment lines (those starting with '!' or '#') are ignored */
- /* in calculating the key value. */
- /* */
- /* input: pointer to FILE */
- /* output: integer - magic cookie */
- /* */
- /*****************************************************************************/
- int CalculateMagicCookie(FILE *fp)
- {
- #define PRIME1 2999
- #define PRIME2 353
- #define PRIME3 113
- long save_offset, file_id;
- int c, prev_c, char_sum, nline, nword, nchar, inword;
- /**************************************************************************/
- /* Since we will be traversing the file, we save the file-offset */
- /* so that we can restore it before returning */
- /**************************************************************************/
- save_offset = ftell( fp );
- inword = FALSE;
- char_sum = nline = nword = nchar = 0;
- prev_c = '0'; /* This is arbitrary, but '0' not '\0' IS intended */
- while ( (c = fgetc( fp )) != EOF ) {
- if ( c == COMMENT_CHAR ) {
- while ( (c = fgetc( fp )) != EOF && c != '\n' )
- ; /* skip characters to end of line */
- } else {
- char_sum += (c * (prev_c%10));
- prev_c = c;
- ++nchar;
- if (c == '\n') {
- ++nline;
- }
- if ( c == ' ' || c == '\n' || c == '\t' ) {
- inword = FALSE;
- } else if ( inword == FALSE ) {
- inword = TRUE;
- ++nword;
- }
- }
- }
- file_id = PRIME1 * nline + PRIME2 * nword + PRIME3 * nchar + char_sum;
- #ifdef DEBUG
- printf( "nline = %d nword = %d nchar = %d char_sum = %d\n",
- nline, nword, nchar, char_sum );
- printf( "file_id = %ld\n", file_id );
- #endif
- /**************************************************************************/
- /* We restore the file-offset to its previous value */
- /**************************************************************************/
- (void) fseek( fp, save_offset, 0 );
- return ( file_id % MAX_KEY );
- }
- /****************************************************************************/
- /* */
- /* SetCookie - find the location in the file to write the magic cookie */
- /* and update the file with the magic cookie. */
- /* */
- /* input - file pointer */
- /* output - none */
- /* */
- /****************************************************************************/
- void SetCookie(FILE *fp)
- {
- int iCookie;
- long lSave_offset;
- char buffer[256];
- char *keystr;
- char *retbuf;
- fpos_t filepos;
- char *mc_pos;
- char *mc_clrpos;
- int i;
- int size;
- /**************************************************************************/
- /* Save file pointer initial setting */
- /**************************************************************************/
- lSave_offset = ftell (fp);
- /**************************************************************************/
- /* Set file pointer to beginning of file */
- /**************************************************************************/
- fseek(fp, 0, SEEK_SET);
- /**************************************************************************/
- /* Calculate magic cookie */
- /**************************************************************************/
- iCookie = CalculateMagicCookie(fp);
- #ifdef DEBUG
- printf("Magic Cookie: %d\n", iCookie);
- #endif
- /**************************************************************************/
- /* Find location in header and store cookie */
- /**************************************************************************/
- keystr = (char *)NULL;
- retbuf = buffer;
- i = 0;
- while ( !keystr && retbuf && (i < MAX_HEADER_LINES) ) {
- fgetpos(fp, &filepos);
- retbuf = fgets(buffer, 80, fp);
- keystr = strstr (buffer, "$KEY:");
- i++;
- }
- if (!keystr) {
- #ifdef DEBUG
- printf("No '$KEY' keyword in file. Magic Cookie not written to file.\n");
- #endif
- return;
- }
- mc_pos = strchr(keystr, ':');
- if (mc_pos) {
- mc_pos += 2;
- sprintf(mc_pos, "%i", iCookie);
- size = strlen(mc_pos);
- if (size < MAX_KEY_SIZE) {
- mc_clrpos = mc_pos + size;
- for (i=size; i < MAX_KEY_SIZE; i++) {
- *mc_clrpos = ' ';
- mc_clrpos++;
- }
- }
- }
- fsetpos (fp, &filepos);
- fputs (buffer, fp);
- /**************************************************************************/
- /* Set file pointer back to initial setting */
- /**************************************************************************/
- fseek (fp, lSave_offset, SEEK_SET);
- return;
- }
- /****************************************************************************/
- /* */
- /* CheckCookie - check if the magic cookie exists in the file and is the */
- /* correct value. */
- /* */
- /* input - file pointer */
- /* output - boolean indicating if correct value or not, TRUE = good */
- /* */
- /****************************************************************************/
- Boolean CheckCookie(FILE *fp)
- {
- int iNewCookie, iOldCookie;
- long lSave_offset;
- char buffer[256];
- char *msgPtr, *errPtr;
- char *keystr;
- char *retbuf;
- fpos_t filepos;
- char *mc_pos;
- int i;
- Boolean bGoodCookie = TRUE;
- /**************************************************************************/
- /* Save file pointer initial setting */
- /**************************************************************************/
- lSave_offset = ftell (fp);
- /**************************************************************************/
- /* Set file pointer to beginning of file */
- /**************************************************************************/
- fseek (fp, 0, SEEK_SET);
- /**************************************************************************/
- /* Calculate magic cookie */
- /**************************************************************************/
- iNewCookie = CalculateMagicCookie(fp);
- #ifdef DEBUG
- printf("New Magic Cookie: %d\n", iNewCookie);
- #endif
- /**************************************************************************/
- /* Find location in header of old cookie and retrieve it. */
- /**************************************************************************/
- keystr = (char *)NULL;
- retbuf = buffer;
- i = 0;
- while ( !keystr && retbuf && (i < MAX_HEADER_LINES) ) {
- fgetpos(fp, &filepos);
- retbuf = fgets(buffer, 80, fp);
- keystr = strstr (buffer, "$KEY:");
- i++;
- }
- if (!keystr) {
- #ifdef DEBUG
- printf("No '$KEY' keyword in file header. File is invalid.\n");
- #endif
- msgPtr = GETMESSAGE(5, 67,
- "Create Action can only edit files that were created\n\
- and edited by Create Action.");
- errPtr = XtNewString(msgPtr);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- bGoodCookie = FALSE;
- fseek (fp, lSave_offset, SEEK_SET);
- return(bGoodCookie);
- }
- mc_pos = strchr(keystr, ':');
- mc_pos += 2;
- sscanf(mc_pos, "%i", &iOldCookie);
- #ifdef DEBUG
- printf("Old Magic Cookie: %d\n", iOldCookie);
- #endif
- /**************************************************************************/
- /* Check if cookies are the same. */
- /**************************************************************************/
- #ifdef DEBUG
- if (iOldCookie == iNewCookie) {
- printf("Cookies are good = %i\n", iNewCookie);
- }
- #endif
- if (iOldCookie != iNewCookie) {
- #ifdef DEBUG
- printf("Cookies are bad; n=%i, o=%i\n", iNewCookie, iOldCookie);
- #endif
- msgPtr = GETMESSAGE(5, 66,
- "Could not load the file because it has been altered by some\n\
- other means other than Create Action. This could have been caused\n\
- by modifying the file with a text editor or some other tool.");
- errPtr = XtNewString(msgPtr);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- bGoodCookie = FALSE;
- }
- /**************************************************************************/
- /* Set file pointer back to initial setting */
- /**************************************************************************/
- fseek (fp, lSave_offset, SEEK_SET);
- return(bGoodCookie);
- }
- /*****************************************************************************/
- /* */
- /* MakeExecString */
- /* */
- /* return codes: */
- /* */
- /* ptr good */
- /* 0 bad */
- /* */
- /*****************************************************************************/
- char * MakeExecString(char *pszCmd, char *pszPrompt)
- {
- char *pszExecString;
- char *pArgSrc = (char *)NULL;
- char *pArgDst = (char *)NULL;
- char *pTmp;
- char pszArgStr[10];
- int size = 0;
- int cArgs = 0;
- int val;
- Boolean bDidPrompt = FALSE;
- #define ARGSIZE 7 /* sizeof "%Arg_n%" */
- pArgSrc = pszCmd;
- while (pArgSrc = strchr(pArgSrc, '$')) {
- #ifdef DEBUG
- printf("%d pArgSrc = %s\n", cArgs, pArgSrc);
- #endif
- pArgSrc++;
- val = *pArgSrc;
- switch (val) {
- case '1': case '2': case '3': case '4': case '5':
- case '6': case '7': case '8': case '9': case '*':
- cArgs++;
- break;
- }
- }
- #ifdef DEBUG
- printf("Args found = %d\n", cArgs);
- #endif
- size = 1;
- size += cArgs * ARGSIZE;
- size += strlen(pszCmd);
- if (pszPrompt) {
- size += strlen(pszPrompt) + 2;
- }
- pszExecString = (char *)XtMalloc(size);
- strcpy(pszExecString, pszCmd);
- pArgSrc = pszCmd;
- pTmp = pszExecString;
- while (pArgSrc = strchr(pArgSrc, '$')) {
- pArgDst = strchr(pTmp, '$');
- pTmp = pArgDst;
- *pArgDst = '\0';
- pTmp++;
- val = *pTmp;
- switch (val) {
- case '1': case '2': case '3': case '4': case '5':
- case '6': case '7': case '8': case '9': case '*':
- pArgSrc+=2;
- if (*pTmp == '*') {
- strcat(pszExecString, "%Args");
- } else {
- sprintf(pszArgStr, "%%Arg_%c", *pTmp);
- strcat(pszExecString, pszArgStr);
- }
- if ((pszPrompt) && (!bDidPrompt)) {
- strcat(pszExecString, "\"");
- strcat(pszExecString, pszPrompt);
- strcat(pszExecString, "\"");
- bDidPrompt = TRUE;
- }
- strcat(pszExecString, "%");
- break;
- }
- if (*pArgSrc) {
- strcat(pszExecString, pArgSrc);
- pArgSrc++;
- }
- }
- #ifdef DEBUG
- printf("EXEC_STRING = '%s'\n", pszExecString);
- #endif
- return(pszExecString);
- }
- /*****************************************************************************/
- /* */
- /* WriteDefinitionFile */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 could not open file */
- /* */
- /*****************************************************************************/
- ushort WriteDefinitionFile(char *pszFile, ActionData *pAD)
- {
- FILE *fp = (FILE *)NULL;
- int i;
- FiletypeData *pFtD;
- char buffer[MAXBUFSIZE];
- char *ptr;
- char *msgPtr, *fmtPtr, *errPtr;
- if (!pszFile)
- return 1;
- /***************************************************************************/
- /* Open action and filetypes definition file for writing. */
- /***************************************************************************/
- if ((fp = fopen(pszFile, "w+")) == NULL) {
- #ifdef DEBUG
- printf("Can not open file.\n"); /* debug */
- #endif
- msgPtr = GETMESSAGE(5, 55, "Could not create the following file:");
- fmtPtr = "%s\n %s";
- errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
- strlen(pszFile) + 1) * sizeof(char));
- sprintf(errPtr, fmtPtr, msgPtr, pszFile);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- return(1);
- }
- #ifdef DEBUG
- printf("database file = %s\n", pszFile); /* debug */
- #endif
- /***************************************************************************/
- /* Write comment area of action and filetype definition file, including */
- /* revision number and magic cookie. */
- /***************************************************************************/
- fprintf(fp, "######################################################################\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# Common Desktop Environment (CDE)\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# Action and DataType Definition File\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# Generated by the CreateAction tool\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# $Revision: 1.0\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# $KEY: nnnnn \n");
- fprintf(fp, "#\n");
- fprintf(fp, "######################################################################\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# WARNING:\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# This file was generated by the CDE CreateAction tool.\n");
- fprintf(fp, "# If this file is modified by some other tool, such as vi,\n");
- fprintf(fp, "# the CreateAction tool will no longer be able to load and\n");
- fprintf(fp, "# update this file. For this reason, changes to this file\n");
- fprintf(fp, "# should be handled through CreateAction whenever possible.\n");
- fprintf(fp, "#\n");
- fprintf(fp, "######################################################################\n");
- fprintf(fp, "\n");
- /***************************************************************************/
- /* Write the main Action definition. */
- /***************************************************************************/
- ptr = GetCoreName(pszFile);
- fprintf(fp, "ACTION %s\n", ptr);
- free(ptr);
- fprintf(fp, "{\n");
- fprintf(fp, " LABEL %s\n", pAD->pszName);
- fprintf(fp, " TYPE COMMAND\n");
- ptr = MakeExecString(AD.pszCmd, AD.pszPrompt);
- if (ptr) {
- fprintf(fp, " EXEC_STRING %s\n", ptr);
- XtFree(ptr);
- }
- if (pAD->pszIcon) {
- #ifdef OLDICON
- fprintf(fp, " LARGE_ICON %s.m\n", pAD->pszIcon);
- fprintf(fp, " SMALL_ICON %s.t\n", pAD->pszIcon);
- #else
- if (ActionHasIcon()) {
- fprintf(fp, " ICON %s\n", pAD->pszName);
- } else {
- fprintf(fp, " ICON %s\n", pAD->pszIcon);
- }
- #endif
- }
- #ifdef DEBUG
- printf("CA flags = %X\n", pAD->fsFlags);
- #endif
- if (pAD->fsFlags & CA_WT_TERM) fprintf(fp, " WINDOW_TYPE TERMINAL\n");
- else if (pAD->fsFlags & CA_WT_PERMTERM) fprintf(fp, " WINDOW_TYPE PERM_TERMINAL\n");
- else if (pAD->fsFlags & CA_WT_SHELLTERM) fprintf(fp, " WINDOW_TYPE SHELL_TERMINAL\n");
- else fprintf(fp, " WINDOW_TYPE NO_STDIO\n");
- if ((pAD->papFiletypes) && (pAD->fsFlags & CA_DF_ONLYFTFILES)) {
- fprintf(fp, " ARG_TYPE %s", (pAD->papFiletypes[0])->pszName);
- for (i=1; i < pAD->cFiletypes; i++) {
- pFtD = pAD->papFiletypes[i];
- fprintf(fp, ",%s", pFtD->pszName);
- }
- fprintf(fp, "\n");
- }
- if (pAD->pszHelp) {
- fprintf(fp, " DESCRIPTION %s\n", pAD->pszHelp);
- }
- fprintf(fp, "}\n");
- fprintf(fp, "\n");
- /***************************************************************************/
- /* Write the filetype definitions. */
- /***************************************************************************/
- if (pAD->papFiletypes) {
- for (i=0; i < pAD->cFiletypes; i++) {
- pFtD = pAD->papFiletypes[i];
- /***********************************************************************/
- /* Write the data attributes declaration. */
- /***********************************************************************/
- fprintf(fp, "DATA_ATTRIBUTES %s\n", pFtD->pszName);
- fprintf(fp, "{\n");
- fprintf(fp, " ACTIONS Open");
- if (pFtD->pszPrintCmd) {
- fprintf(fp, ",Print");
- }
- fprintf(fp, "\n");
- if (pFtD->pszIcon) {
- #ifdef OLDICON
- fprintf(fp, " LARGE_ICON %s.m\n", pFtD->pszIcon);
- fprintf(fp, " SMALL_ICON %s.t\n", pFtD->pszIcon);
- #else
- if (FiletypeHasIcon(pFtD)) {
- fprintf(fp, " ICON %s\n", pFtD->pszName);
- } else {
- fprintf(fp, " ICON %s\n", pFtD->pszIcon);
- }
- #endif
- }
- if (pFtD->pszHelp) {
- fprintf(fp, " DESCRIPTION %s\n", pFtD->pszHelp);
- }
- fprintf(fp, "}\n");
- fprintf(fp, "\n");
- /***********************************************************************/
- /* Write the data criteria declaration. */
- /***********************************************************************/
- fprintf(fp, "DATA_CRITERIA %sA\n", pFtD->pszName);
- fprintf(fp, "{\n");
- fprintf(fp, " DATA_ATTRIBUTES_NAME %s\n", pFtD->pszName);
- if (pFtD->pszPermissions) {
- fprintf(fp, " MODE %s\n", pFtD->pszPermissions);
- }
- if (pFtD->pszPattern) {
- fprintf(fp, " PATH_PATTERN %s\n", pFtD->pszPattern);
- }
- if (pFtD->pszContents) {
- if (pFtD->fsFlags & CA_FT_CNTSTRING)
- strcpy(buffer, "string");
- else if (pFtD->fsFlags & CA_FT_CNTBYTE)
- strcpy(buffer, "byte");
- else if (pFtD->fsFlags & CA_FT_CNTSHORT)
- strcpy(buffer, "short");
- else if (pFtD->fsFlags & CA_FT_CNTLONG)
- strcpy(buffer, "long");
- fprintf(fp, " CONTENT %d %s %s\n", pFtD->sStart, buffer, pFtD->pszContents);
- }
- fprintf(fp, "}\n");
- fprintf(fp, "\n");
- /***********************************************************************/
- /* Write the Open MAP action. */
- /***********************************************************************/
- fprintf(fp, "ACTION Open\n");
- fprintf(fp, "{\n");
- fprintf(fp, " ARG_TYPE %s\n", pFtD->pszName);
- fprintf(fp, " TYPE MAP\n");
- fprintf(fp, " MAP_ACTION %s\n", pAD->pszName);
- fprintf(fp, " LABEL %s\n", GETMESSAGE(7, 30, "Open"));
- fprintf(fp, "}\n");
- fprintf(fp, "\n");
- /***********************************************************************/
- /* Write the Print filetype action and MAP action. */
- /***********************************************************************/
- if (pFtD->pszPrintCmd) {
- /*********************************************************************/
- /* First write the print filetype action. */
- /*********************************************************************/
- fprintf(fp, "ACTION %s_PRINT\n", pFtD->pszName);
- fprintf(fp, "{\n");
- fprintf(fp, " TYPE COMMAND\n");
- ptr = MakeExecString(pFtD->pszPrintCmd, NULL);
- if (ptr) {
- fprintf(fp, " EXEC_STRING %s\n", ptr);
- /*fprintf(fp, " EXEC_STRING %s\n", pFtD->pszPrintCmd);*/
- XtFree(ptr);
- }
- fprintf(fp, " WINDOW_TYPE NO_STDIO\n");
- fprintf(fp, "}\n");
- fprintf(fp, "\n");
- /*********************************************************************/
- /* Now writhe the Print MAP action. */
- /*********************************************************************/
- fprintf(fp, "ACTION Print\n");
- fprintf(fp, "{\n");
- fprintf(fp, " ARG_TYPE %s\n", pFtD->pszName);
- fprintf(fp, " TYPE MAP\n");
- fprintf(fp, " MAP_ACTION %s_PRINT\n", pFtD->pszName);
- fprintf(fp, " LABEL %s\n", GETMESSAGE(7, 31, "Print"));
- fprintf(fp, "}\n");
- fprintf(fp, "\n");
- }
- }
- }
- SetCookie(fp);
- if (fp)
- fclose(fp);
- chmod(pszFile, 0644);
- return(0);
- }
- /*****************************************************************************/
- /* */
- /* WriteActionFile */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 could not open file */
- /* */
- /*****************************************************************************/
- ushort WriteActionFile(ActionData *pAD)
- {
- FILE *fp = (FILE *)NULL;
- char *pszFile;
- char *newName;
- char *pszEnvVar;
- char *msgPtr, *fmtPtr, *errPtr;
- /***************************************************************************/
- /* Open action file for writing. */
- /***************************************************************************/
- newName = ReplaceSpaces(AD.pszName);
- pszEnvVar = getenv("HOME");
- pszFile = NULL;
- if ( pszEnvVar && strlen(pszEnvVar) ) {
- pszFile = XtMalloc(strlen(pszEnvVar) + strlen(newName) + 10);
- if (pszFile) sprintf(pszFile, "%s/%s", pszEnvVar, newName);
- } else {
- pszFile = XtMalloc(strlen(newName) + 10);
- if (pszFile) sprintf(pszFile, "/tmp/%s", newName);
- }
- free(newName);
- /***************************************************************************/
- /* Open action file for writing. */
- /***************************************************************************/
- if (pszFile == NULL || (fp = fopen(pszFile, "w")) == NULL) {
- #ifdef DEBUG
- printf("Can not open file.\n"); /* debug */
- #endif
- msgPtr = GETMESSAGE(5, 55, "Could not create the following file:");
- fmtPtr = "%s\n %s";
- errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
- strlen(pszFile) + 1) * sizeof(char));
- sprintf(errPtr, fmtPtr, msgPtr, pszFile);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- XtFree(pszFile);
- return(1);
- }
- #ifdef DEBUG
- printf("action file = %s\n", pszFile); /* debug */
- #endif
- /***************************************************************************/
- /* Write action file contents. */
- /***************************************************************************/
- fprintf(fp, "################################################\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# action\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# Common Desktop Environment (CDE)\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# $TOG: fileio.c /main/9 1998/10/26 17:18:19 mgreess $\n");
- fprintf(fp, "#\n");
- fprintf(fp, "################################################\n");
- fprintf(fp, "\n");
- fprintf(fp, "# This file represents a CDE Dt action. The contents of the file do\n");
- fprintf(fp, "# not matter; however, the file must have executable permission and the\n");
- fprintf(fp, "# name of the file must match an entry in the action database (*.dt files).\n");
- fprintf(fp, "#\n");
- fprintf(fp, "# For more information on Dt actions, see the CDE Dt manuals.\n");
- fprintf(fp, "\n");
- fprintf(fp, "echo \"Common Desktop Environment Dt action\"\n");
- fprintf(fp, "\n");
- fclose(fp);
- chmod(pszFile, 0755);
- return(0);
- }
- /******************************************************************************/
- /* check_file_exists - check if the file exists that is passed in, if not */
- /* display error dialog. */
- /* */
- /* INPUT: filename - name of file to check if it exists or not. */
- /* */
- /* OUTPUT: TRUE - file exists */
- /* FALSE - file does not exist */
- /* */
- /******************************************************************************/
- Boolean check_file_exists (char *pszFileName)
- {
- FILE *exists;
- /***************************************/
- /* see if file exists */
- /***************************************/
- exists = fopen (pszFileName, "r");
- /***************************************/
- /* if exists, return good return code */
- /***************************************/
- if (exists) {
- fclose (exists);
- return(TRUE);
- } else {
- #ifdef DEBUG
- printf("File '%s' does not exist!\n", pszFileName);
- #endif
- return(FALSE);
- }
- }
- /*****************************************************************************/
- /* */
- /* OpenDefinitionFile */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 error */
- /* */
- /*****************************************************************************/
- int OpenDefinitionFile(char *pszFile, ActionData *pAD)
- {
- FILE *fp = (FILE *)NULL;
- int rc = 1;
- char *msgPtr, *fmtPtr, *errPtr;
- /***************************************************************************/
- /* Open action and filetypes definition file for reading. */
- /***************************************************************************/
- if ((fp = fopen(pszFile, "r+")) == NULL) {
- #ifdef DEBUG
- printf("Can not open file.\n"); /* debug */
- #endif
- msgPtr = GETMESSAGE(5, 60, "Could not open the following file:");
- fmtPtr = "%s\n %s";
- errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
- strlen(pszFile) + 1) * sizeof(char));
- sprintf(errPtr, fmtPtr, msgPtr, pszFile);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- return(1);
- }
- #ifdef DEBUG
- printf("Opened database file = %s\n", pszFile); /* debug */
- #endif
- /***************************************************************************/
- /* If magic cookie is good, then parse the file. */
- /***************************************************************************/
- if (CheckCookie(fp)) {
- #ifdef DEBUG
- printf("Cookie good, call parse code\n");
- #endif
- rc = GetActionData(fp, pAD);
- /************************************************************************/
- /* If error, display error message. */
- /************************************************************************/
- if (rc) {
- msgPtr = GETMESSAGE(5, 90, "Could not parse the following file:");
- fmtPtr = "%s\n %s";
- errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
- strlen(pszFile) + 1) * sizeof(char));
- sprintf(errPtr, fmtPtr, msgPtr, pszFile);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- fclose(fp);
- return(1);
- }
- }
- fclose(fp);
- return(rc);
- }
- /******************************************************************************/
- /* CheckDirtyIcon - check if icon has a DirtyBit set */
- /* */
- /* INPUT: Icon Widget - widget id of icon gadget to check */
- /* */
- /* OUTPUT: TRUE - dirty bit is set */
- /* FALSE - dirty bit is not set */
- /* */
- /******************************************************************************/
- Boolean CheckDirtyIcon (Widget wid)
- {
- IconData *pIconData;
- pIconData = GetIconDataFromWid(wid);
- if (pIconData) {
- if ( (pIconData->pmDirtyBit) || (pIconData->bmDirtyBit) ) {
- return(True);
- }
- }
- return(False);
- }
- /*****************************************************************************/
- /* */
- /* CreateIconName */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 could not open file */
- /* */
- /*****************************************************************************/
- char * CreateIconName(char *pszDirName, char *pszBaseName, enum icon_size_range enumSize, char *pszType, Boolean bIsMask)
- {
- char *pszName;
- char pszSize[MAX_EXT_SIZE];
- switch (enumSize) {
- case Large_Icon :
- strcpy(pszSize, LARGE_EXT);
- break;
- case Medium_Icon :
- strcpy(pszSize, MEDIUM_EXT);
- break;
- case Small_Icon :
- strcpy(pszSize, SMALL_EXT);
- break;
- case Tiny_Icon :
- strcpy(pszSize, TINY_EXT);
- break;
- case None_Selected :
- pszSize[0] = 0;
- }
- if (bIsMask) {
- strcat(pszSize, "_m");
- }
- if (pszDirName) {
- pszName = XtMalloc(strlen(pszDirName) + strlen(pszBaseName) + strlen(pszSize) + strlen(pszType) + 1);
- if (pszName) {
- sprintf(pszName, "%s%s%s%s", pszDirName, pszBaseName, pszSize, pszType);
- }
- } else {
- pszName = XtMalloc(strlen(pszBaseName) + strlen(pszSize) + strlen(pszType) + 1);
- if (pszName) {
- sprintf(pszName, "%s%s%s", pszBaseName, pszSize, pszType);
- }
- }
- return(pszName);
- }
- /*****************************************************************************/
- /* */
- /* CopyFile */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 could not open file */
- /* */
- /*****************************************************************************/
- ushort CopyFile(char *pszSource, char *pszDest)
- {
- int fdSource;
- int fdDest;
- int len;
- int rlen;
- ushort usRc = 0;
- char *buffer;
- char *msgPtr, *fmtPtr, *errPtr;
- fdSource = open(pszSource, O_RDONLY);
- if (fdSource == -1) {
- msgPtr = GETMESSAGE(5, 60, "Could not open the following file:");
- fmtPtr = "%s\n %s";
- errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
- strlen(pszSource) + 1) * sizeof(char));
- sprintf(errPtr, fmtPtr, msgPtr, pszSource);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- usRc = 1;
- } else {
- /************************************************************/
- /* Find out how big a buffer is needed */
- /************************************************************/
- len = lseek(fdSource, 0, SEEK_END);
- (void) lseek(fdSource, 0, SEEK_SET);
- buffer = XtMalloc((Cardinal) (len + 1));
- if ((rlen = read(fdSource, buffer, len)) != len) {
- /*********************************************************/
- /* Didn't read whole file! */
- /*********************************************************/
- } else {
- fdDest = creat(pszDest, 0644);
- /*fdDest = open(pszDest, O_WRONLY);*/
- if (fdDest == -1) {
- msgPtr = GETMESSAGE(5, 55, "Could not create the following file:");
- fmtPtr = "%s\n %s";
- errPtr = XtMalloc((strlen(msgPtr) + strlen(fmtPtr) +
- strlen(pszDest) + 1) * sizeof(char));
- sprintf(errPtr, fmtPtr, msgPtr, pszDest);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- usRc = 1;
- } else {
- if ((rlen = write(fdDest, buffer, len)) != len) {
- /***************************************************/
- /* Didn't write the whole file! */
- /***************************************************/
- }
- close(fdDest);
- }
- }
- XtFree(buffer);
- close(fdSource);
- }
- return(usRc);
- }
- /*****************************************************************************/
- /* */
- /* CreateDirName */
- /* */
- /* return codes: */
- /* */
- /* NULL error has occurred */
- /* not NULL name of directory to store icons */
- /* */
- /*****************************************************************************/
- char * CreateDirName(void)
- {
- char *pszNewName = (char *)NULL;
- char *pszDirName = (char *)NULL;
- char *pszEnvVar;
- char *msgPtr, *errPtr;
- pszEnvVar = getenv("HOME");
- if ( pszEnvVar && strlen(pszEnvVar) ) {
- pszDirName = XtMalloc(strlen(pszEnvVar) + strlen("/.dt/icons/") + 1);
- sprintf(pszDirName, "%s/.dt/icons/", pszEnvVar);
- } else {
- msgPtr = GETMESSAGE(5, 170, "The HOME environment variable is not set.\n\
- The HOME environment variable must be set in order\n\
- for this action to be saved.");
- errPtr = XtNewString(msgPtr);
- display_error_message(CreateActionAppShell, errPtr);
- XtFree(errPtr);
- }
- return(pszDirName);
- }
- /*****************************************************************************/
- /* */
- /* CopyFilesFromWid */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 could not open file */
- /* */
- /*****************************************************************************/
- ushort CopyFilesFromWid(ActionData *pAD, Widget wid)
- {
- IconData *pIconData;
- char *pszDirName = (char *)NULL;
- Boolean bHaveError = FALSE;
- char *pszIconName = (char *)NULL;
- char *pszNewName = (char *)NULL;
- char *pszMaskName = (char *)NULL;
- pszDirName = CreateDirName();
- if (!pszDirName) bHaveError = True;
- pIconData = GetIconDataFromWid(wid);
- pszNewName = ReplaceSpaces(pAD->pszName);
- if ((!bHaveError) && (pIconData)) {
- pszIconName = CreateIconName(pszDirName, pszNewName, pIconData->size, PIXMAP_EXT, FALSE);
- if ((!pszIconName) || (CopyFile(pIconData->pmFileName, pszIconName))) {
- bHaveError = True;
- }
- if (pszIconName) XtFree(pszIconName);
- }
- if ((!bHaveError) && (pIconData)) {
- pszIconName = CreateIconName(pszDirName, pszNewName, pIconData->size, BITMAP_EXT, FALSE);
- if ((!pszIconName) || (CopyFile(pIconData->bmFileName, pszIconName))) {
- bHaveError = True;
- }
- if (pszIconName) XtFree(pszIconName);
- /**********************************************/
- /* Now see if it has a mask file. */
- /**********************************************/
- if (!bHaveError) {
- pszMaskName = CreateMaskName(pIconData->bmFileName);
- if ((pszMaskName) && (check_file_exists(pszMaskName))) {
- #ifdef DEBUG
- printf("Mask file exists = %s\n", pszMaskName); /* debug */
- #endif
- pszIconName = CreateIconName(pszDirName, pszNewName, pIconData->size, BITMAP_EXT, TRUE);
- if ((!pszIconName) || (CopyFile(pszMaskName, pszIconName))) {
- bHaveError = True;
- }
- if (pszIconName) XtFree(pszIconName);
- XtFree(pszMaskName);
- }
- }
- }
- if (pszNewName) XtFree(pszNewName);
- if (pszDirName) XtFree(pszDirName);
- if (bHaveError) {
- return(1);
- } else {
- return(0);
- }
- }
- /******************************************************************************/
- /* ActionHasIcon - check if any of the main action's icons have dirty bit set */
- /* */
- /* INPUT: none */
- /* */
- /* OUTPUT: TRUE - action has icon */
- /* FALSE - no icon for this action */
- /* */
- /******************************************************************************/
- Boolean ActionHasIcon (void)
- {
- if ( CheckDirtyIcon(CA_LRG_IconGadget) ||
- CheckDirtyIcon(CA_MED_IconGadget) ||
- CheckDirtyIcon(CA_TINY_IconGadget) ) {
- return(True);
- } else {
- return(False);
- }
- }
- /******************************************************************************/
- /* FiletypeHasIcon - check if the filetype has modified icons. */
- /* */
- /* INPUT: Filetype data structure pointer */
- /* */
- /* OUTPUT: TRUE - action has icon */
- /* FALSE - no icon for this action */
- /* */
- /******************************************************************************/
- Boolean FiletypeHasIcon (FiletypeData *pFtD)
- {
- if ( (pFtD->pszMedPmIcon) ||
- (pFtD->pszMedBmIcon) ||
- (pFtD->pszTinyPmIcon) ||
- (pFtD->pszTinyBmIcon) ) {
- return(True);
- } else {
- return(False);
- }
- }
- /*****************************************************************************/
- /* */
- /* CopyIconFiles */
- /* */
- /* return codes: */
- /* */
- /* 0 successful */
- /* 1 could not open file */
- /* */
- /*****************************************************************************/
- ushort CopyIconFiles(ActionData *pAD)
- {
- Boolean bHaveError = FALSE;
- FiletypeData *pFtD;
- int i;
- char *pszDirName = (char *)NULL;
- char *pszNewIconName;
- char *pszNewIconMask;
- char *pszOldIconName;
- char *ptr;
- char buffer[MAXFILENAME];
- #define CIF_ERROR 1
- #define CIF_SUCCESS 0
- if (ActionHasIcon()) {
- if ( !bHaveError && (CopyFilesFromWid(pAD, CA_LRG_IconGadget) != 0) ) {
- bHaveError = TRUE;
- }
- if ( !bHaveError && (CopyFilesFromWid(pAD, CA_MED_IconGadget) != 0) ) {
- bHaveError = TRUE;
- }
- if ( !bHaveError && (CopyFilesFromWid(pAD, CA_TINY_IconGadget) != 0) ) {
- bHaveError = TRUE;
- }
- }
- if (!bHaveError && (pAD->papFiletypes)) {
- pszDirName = CreateDirName();
- for (i=0; i < pAD->cFiletypes; i++) {
- pFtD = pAD->papFiletypes[i];
- if (FiletypeHasIcon(pFtD)) {
- FIND_ICONGADGET_ICON(pFtD->pszIcon, pszOldIconName, DtMEDIUM);
- if (pszOldIconName) {
- ptr = strstr(pszOldIconName, MEDIUM_EXT);
- if (ptr) *ptr = '\0';
- }
- /*****************************************************************/
- /* Medium Pixmap */
- /*****************************************************************/
- pszNewIconName = CreateIconName(pszDirName, pFtD->pszName, Medium_Icon, PIXMAP_EXT, FALSE);
- if (pszNewIconName) {
- if (pFtD->pszMedPmIcon) {
- CopyFile(pFtD->pszMedPmIcon, pszNewIconName);
- } else {
- ptr = CreateIconName((char *)NULL, pszOldIconName, Medium_Icon, PIXMAP_EXT, FALSE);
- if (ptr) {
- CopyFile(ptr, pszNewIconName);
- XtFree(ptr);
- }
- }
- if (pszNewIconName) XtFree(pszNewIconName);
- }
- /*****************************************************************/
- /* Medium Bitmap */
- /*****************************************************************/
- pszNewIconName = CreateIconName(pszDirName, pFtD->pszName, Medium_Icon, BITMAP_EXT, FALSE);
- pszNewIconMask = CreateIconName(pszDirName, pFtD->pszName, Medium_Icon, BITMAP_EXT, TRUE);
- if (pszNewIconName) {
- if (pFtD->pszMedBmIcon) {
- CopyFile(pFtD->pszMedBmIcon, pszNewIconName);
- /****************************/
- /* Now do the mask file. */
- /****************************/
- sprintf(buffer, "%s_m", pFtD->pszMedBmIcon);
- if (pszNewIconMask && check_file_exists(buffer)) {
- CopyFile(buffer, pszNewIconMask);
- }
- } else {
- ptr = CreateIconName((char *)NULL, pszOldIconName, Medium_Icon, BITMAP_EXT, FALSE);
- if (ptr) {
- CopyFile(ptr, pszNewIconName);
- XtFree(ptr);
- }
- /****************************/
- /* Now do the mask file. */
- /****************************/
- ptr = CreateIconName((char *)NULL, pszOldIconName, Medium_Icon, BITMAP_EXT, TRUE);
- if (ptr && check_file_exists(ptr) && pszNewIconMask) {
- CopyFile(ptr, pszNewIconMask);
- XtFree(ptr);
- }
- }
- if (pszNewIconName) XtFree(pszNewIconName);
- if (pszNewIconMask) XtFree(pszNewIconMask);
- }
- /*****************************************************************/
- /* Tiny Pixmap */
- /*****************************************************************/
- pszNewIconName = CreateIconName(pszDirName, pFtD->pszName, Tiny_Icon, PIXMAP_EXT, FALSE);
- if (pszNewIconName) {
- if (pFtD->pszTinyPmIcon) {
- CopyFile(pFtD->pszTinyPmIcon, pszNewIconName);
- } else {
- ptr = CreateIconName((char *)NULL, pszOldIconName, Tiny_Icon, PIXMAP_EXT, FALSE);
- if (ptr) {
- CopyFile(ptr, pszNewIconName);
- XtFree(ptr);
- }
- }
- if (pszNewIconName) XtFree(pszNewIconName);
- }
- /*****************************************************************/
- /* Tiny Bitmap */
- /*****************************************************************/
- pszNewIconName = CreateIconName(pszDirName, pFtD->pszName, Tiny_Icon, BITMAP_EXT, FALSE);
- pszNewIconMask = CreateIconName(pszDirName, pFtD->pszName, Tiny_Icon, BITMAP_EXT, TRUE);
- if (pszNewIconName) {
- if (pFtD->pszTinyBmIcon) {
- CopyFile(pFtD->pszTinyBmIcon, pszNewIconName);
- /****************************/
- /* Now do the mask file. */
- /****************************/
- sprintf(buffer, "%s_m", pFtD->pszTinyBmIcon);
- if (pszNewIconMask && check_file_exists(buffer)) {
- CopyFile(buffer, pszNewIconMask);
- }
- } else {
- ptr = CreateIconName((char *)NULL, pszOldIconName, Tiny_Icon, BITMAP_EXT, FALSE);
- if (ptr) {
- CopyFile(ptr, pszNewIconName);
- XtFree(ptr);
- }
- /****************************/
- /* Now do the mask file. */
- /****************************/
- ptr = CreateIconName((char *)NULL, pszOldIconName, Tiny_Icon, BITMAP_EXT, TRUE);
- if (ptr && check_file_exists(ptr) && pszNewIconMask) {
- CopyFile(ptr, pszNewIconMask);
- XtFree(ptr);
- }
- }
- if (pszNewIconName) XtFree(pszNewIconName);
- if (pszNewIconMask) XtFree(pszNewIconMask);
- }
- }
- }
- }
- return(CIF_SUCCESS);
- }
|