util  Diff

Differences From Artifact [afa1600fae]:

To Artifact [c9d2d3baea]:


   142    142   bool run;
   143    143   
   144    144   struct termios initial_state;
   145    145   
   146    146   void sigusr(int a) { if (global -> op == mode_kill) run = false; }
   147    147   void sigterm(int a) { run = false; }
   148    148   
   149         -enum res register_window(const char* id, bool weak) {
          149  +void spawn(pid_t ssha, const char* const sockn) {
          150  +	if (ssha = fork()) {
          151  +		char pid_s_buf[16];
          152  +		char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf));
          153  +		while (access(sockn, F_OK));
          154  +			// avoid nasty race condition
          155  +		setenv("SSH_AGENT_PID", pid_s, true);
          156  +		setenv("SSH_AUTH_SOCK", sockn, true);
          157  +		global -> agent = ssha;
          158  +	} else {
          159  +		close(1); close(0);
          160  +		execlp("ssh-agent","ssh-agent","-D", "-a",sockn,0);
          161  +	}
          162  +}
          163  +
          164  +enum res register_window(const char* const id, bool weak, const char* const name) {
   150    165   	// the id field denotes the path to the shared memory
   151    166   	// in use, and allows the user to have multiple
   152    167   	// contexts by creating aliases to the binary
   153    168   	int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600);
   154    169   	ftruncate(fd, sizeof(struct signal));
   155    170   	if (fd == -1) return fail_shm;
   156    171   	
................................................................................
   202    217   		}
   203    218   		XCloseDisplay(xdpy);
   204    219   	} else {
   205    220   		// now we start ssh-agent and set the proper environment
   206    221   		// variables
   207    222   		pid_t ssha;
   208    223   
   209         -		/* messy part */ {
          224  +		if (name == NULL) {
          225  +			/* messy part */ 
   210    226   			const char* tmp; //tmpdir defined?
   211    227   			if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp";
   212    228   			size_t tmpsz = strlen(tmp);
   213    229   			
   214    230   			char sockn[tmpsz + 1 + sizeof "ssh."
   215    231   				+ 11];
   216    232   			strcpy(sockn, tmp);
................................................................................
   225    241   			// assuming asciiā€¦
   226    242   			for(uint8_t*i=rndid;i<rndid+11;++i) {
   227    243   				*i = '0' + (*i % (25 * 2 + 10));
   228    244   				if (*i > '9') *i += 7;
   229    245   				if (*i > 'Z') *i += ('a' - 'Z');
   230    246   			}
   231    247   
   232         -			if (ssha = fork()) {
   233         -				char pid_s_buf[16];
   234         -				char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf));
   235         -				while (access(sockn, F_OK));
   236         -					// avoid nasty race condition
   237         -				setenv("SSH_AGENT_PID", pid_s, true);
   238         -				setenv("SSH_AUTH_SOCK", sockn, true);
   239         -				s -> agent = ssha;
   240         -			} else {
   241         -				close(1); close(0);
   242         -				execlp("ssh-agent","ssh-agent","-D", "-a",sockn,0);
   243         -			}
   244         -		}
   245         -		
          248  +			spawn(ssha, sockn);
          249  +		} else spawn(ssha, name);
          250  +	
   246    251   		pid_t sad;
   247    252   		int p;
   248    253   		if (sad = fork()) {
   249    254   			int added;
   250    255   			waitpid(sad, &added, 0);
   251    256   			if (added == 0) {
   252    257   				if (weak == false) {
................................................................................
   296    301   	XSync(dpy,false);
   297    302   	return ok;
   298    303   }
   299    304   
   300    305   int main(int sz, char** argv) {
   301    306   	enum mode op = mode_go;
   302    307   	bool init_weak = false;
          308  +	const char* init_named = NULL;
   303    309   
   304    310   	for(int i = 1; i<sz; ++i) {
   305    311   		char* v = argv[i];
   306    312   		if (*v != '-') return bad(fail_arg);
   307    313   		char* opt = v + 1;
          314  +		bool seen_string_arg = false;
   308    315   	read_opt:
   309    316   		switch(*opt) {
   310    317   			case 'a': op = mode_register; break;
   311    318   			case 'k': op = mode_kill; break;
   312    319   			case 'l': op = mode_lock; break;
   313    320   			case 'h': op = mode_usage; break;
   314    321   			case 'w': init_weak = true; break;
          322  +			case 'n': if (i == sz -1 || seen_string_arg)
          323  +						  return bad(fail_arg);
          324  +					  init_named = argv[i+1];
          325  +					  seen_string_arg = true;
          326  +					  ++ i; break;
   315    327   			default: return bad(fail_opt);
   316    328   		}
   317    329   		if(opt[1] != 0) { ++opt; goto read_opt; }
   318    330   	}
   319    331   
   320    332   	size_t nsz;
   321    333   	const char* basename = argv[0], *p;
................................................................................
   326    338   	char shid[nsz + sizeof shmem_prefix + 0];
   327    339   	strncpy(shid,shmem_prefix,sizeof shmem_prefix);
   328    340   	strncpy(shid + sizeof shmem_prefix - 1, basename, nsz);
   329    341   
   330    342   	if (op == mode_go) {
   331    343   		int fd;
   332    344   		if ((fd = shm_open(shid, O_RDWR, 0600)) == -1) {
   333         -			execlp("urxvtc", "urxvtc", "-bg", "[80]#4b0024",
   334         -					"-e", argv[0], (init_weak?"-aw":"-a"), 0);
          345  +			const char* args[] = {
          346  +				"urxvtc", "-bg", "[90]#4b0024",
          347  +				          "-e",  argv[0],
          348  +						  (init_weak?"-aw":"-a"), 0, 0, 0};
          349  +
          350  +			const uint8_t argsz = sizeof args/sizeof(const char*);
          351  +			if (init_named != NULL) {
          352  +				args[argsz - 3] = "-n"; // im sorry
          353  +				args[argsz - 2] = init_named;
          354  +			}
          355  +
          356  +			execvp("urxvtc", (char* const*)args);
   335    357   		} else {
   336    358   			struct signal*s = mmap(0, sizeof(struct signal),
   337    359   					PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
   338    360   			return bad(activate_window(s->wid));
   339    361   		}
   340    362   	} else if (op == mode_register)
   341         -		return bad(register_window(shid,init_weak));
          363  +		return bad(register_window(shid,init_weak,init_named));
   342    364   	else if (op == mode_kill)
   343    365   		return bad(kill_window(shid));
   344    366   	else if (op == mode_usage) {
   345    367   		write(1,"\e[1musage:\e[0m ",15);
   346    368   		write(1, argv[0], strlen(argv[0]));
   347    369   		write(1, " [-aklw [arg]]\n",16);
   348    370   		return fail_parse;
   349    371   	}
   350    372   }
   351    373