UNIXworkcode

1 /******************************************************************************* 2 * * 3 * server_common.c -- Nirvana Editor common server stuff * 4 * * 5 * Copyright (C) 1999 Mark Edel * 6 * * 7 * This is free software; you can redistribute it and/or modify it under the * 8 * terms of the GNU General Public License as published by the Free Software * 9 * Foundation; either version 2 of the License, or (at your option) any later * 10 * version. In addition, you may distribute versions of this program linked to * 11 * Motif or Open Motif. See README for details. * 12 * * 13 * This software is distributed in the hope that it will be useful, but WITHOUT * 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * 16 * more details. * 17 * * 18 * You should have received a copy of the GNU General Public License along with * 19 * software; if not, write to the Free Software Foundation, Inc., 59 Temple * 20 * Place, Suite 330, Boston, MA 02111-1307 USA * 21 * * 22 * Nirvana Text Editor * 23 * November, 1995 * 24 * * 25 * Written by Mark Edel * 26 * * 27 *******************************************************************************/ 28 #include <stdio.h> 29 #include <Xm/Xm.h> 30 #include <sys/types.h> 31 #include <sys/param.h> 32 #include "nedit.h" 33 #include "server_common.h" 34 #include "../util/utils.h" 35 #include "../util/nedit_malloc.h" 36 37 /* 38 * Create the server property atoms for the server with serverName. 39 * Atom names are generated as follows: 40 * 41 * XNEDIT_SERVER_EXISTS_<host_name>_<user>_<server_name> 42 * XNEDIT_SERVER_REQUEST_<host_name>_<user>_<server_name> 43 * 44 * <server_name> is the name that can be set by the user to allow 45 * for multiple servers to run on the same display. <server_name> 46 * defaults to "" if not supplied by the user. 47 * 48 * <user> is the user name of the current user. 49 */ 50 void CreateServerPropertyAtoms(const char *serverName, 51 Atom *serverExistsAtomReturn, 52 Atom *serverRequestAtomReturn) 53 { 54 const char *userName = GetUserName(); 55 const char *hostName = GetNameOfHost(); 56 57 size_t serverNameLen = strlen(serverName); 58 size_t userNameLen = strlen(userName); 59 size_t hostNameLen = strlen(hostName); 60 61 size_t propNameMaxLen = 32 + serverNameLen + userNameLen + hostNameLen; 62 char *propName = NEditMalloc(propNameMaxLen); 63 64 snprintf(propName, propNameMaxLen, "XNEDIT_SERVER_EXISTS_%s_%s_%s", hostName, userName, serverName); 65 *serverExistsAtomReturn = XInternAtom(TheDisplay, propName, False); 66 snprintf(propName, propNameMaxLen, "XNEDIT_SERVER_REQUEST_%s_%s_%s", hostName, userName, serverName); 67 *serverRequestAtomReturn = XInternAtom(TheDisplay, propName, False); 68 69 NEditFree(propName); 70 } 71 72 /* 73 * Create the individual property atoms for each file being 74 * opened by the server with serverName. This atom is used 75 * by nc to monitor if the file has been closed. 76 * 77 * Atom names are generated as follows: 78 * 79 * XNEDIT_FILE_<host_name>_<user>_<server_name>_<path> 80 * 81 * <server_name> is the name that can be set by the user to allow 82 * for multiple servers to run on the same display. <server_name> 83 * defaults to "" if not supplied by the user. 84 * 85 * <user> is the user name of the current user. 86 * 87 * <path> is the path of the file being edited. 88 */ 89 Atom CreateServerFileOpenAtom(const char *serverName, 90 const char *path) 91 { 92 const char *userName = GetUserName(); 93 const char *hostName = GetNameOfHost(); 94 95 size_t serverNameLen = strlen(serverName); 96 size_t userNameLen = strlen(userName); 97 size_t hostNameLen = strlen(hostName); 98 size_t pathLen = strlen(path); 99 100 size_t propNameMaxLen = 32 + serverNameLen + userNameLen + hostNameLen + pathLen; 101 char *propName = NEditMalloc(propNameMaxLen); 102 103 Atom atom; 104 105 snprintf(propName, propNameMaxLen, "XNEDIT_FILE_%s_%s_%s_%s_WF_OPEN", hostName, userName, serverName, path); 106 atom = XInternAtom(TheDisplay, propName, False); 107 108 NEditFree(propName); 109 110 return(atom); 111 } 112 113 Atom CreateServerFileClosedAtom(const char *serverName, 114 const char *path, 115 Bool only_if_exist) 116 { 117 const char *userName = GetUserName(); 118 const char *hostName = GetNameOfHost(); 119 Atom atom; 120 121 size_t serverNameLen = strlen(serverName); 122 size_t userNameLen = strlen(userName); 123 size_t hostNameLen = strlen(hostName); 124 size_t pathLen = strlen(path); 125 126 size_t propNameMaxLen = 32 + serverNameLen + userNameLen + hostNameLen + pathLen; 127 char *propName = NEditMalloc(propNameMaxLen); 128 129 snprintf(propName, propNameMaxLen, "XNEDIT_FILE_%s_%s_%s_%s_WF_CLOSED", hostName, userName, serverName, path); 130 atom = XInternAtom(TheDisplay, propName, only_if_exist); 131 NEditFree(propName); 132 return(atom); 133 } 134 135 /* 136 * Delete all File atoms that belong to this server (as specified by 137 * <host_name>_<user>_<server_name>). 138 */ 139 void DeleteServerFileAtoms(const char* serverName, Window rootWindow) 140 { 141 const char *userName = GetUserName(); 142 const char *hostName = GetNameOfHost(); 143 144 size_t serverNameLen = strlen(serverName); 145 size_t userNameLen = strlen(userName); 146 size_t hostNameLen = strlen(hostName); 147 148 size_t propNamePrefixMaxLen = 32 + serverNameLen + userNameLen + hostNameLen; 149 char *propNamePrefix = NEditMalloc(propNamePrefixMaxLen); 150 151 int length = snprintf(propNamePrefix, propNamePrefixMaxLen, "XNEDIT_FILE_%s_%s_%s_", hostName, userName, serverName); 152 153 int nProperties; 154 Atom* atoms = XListProperties(TheDisplay, rootWindow, &nProperties); 155 if (atoms != NULL) { 156 int i; 157 for (i = 0; i < nProperties; i++) { 158 /* XGetAtomNames() is more efficient, but doesn't exist in X11R5. */ 159 char *name = XGetAtomName(TheDisplay, atoms[i]); 160 if (name != NULL && strncmp(propNamePrefix, name, length) == 0) { 161 XDeleteProperty(TheDisplay, rootWindow, atoms[i]); 162 } 163 XFree(name); 164 } 165 XFree((char*)atoms); 166 } 167 168 NEditFree(propNamePrefix); 169 } 170