Exemplo n.º 1
0
/**
tList*	awk_tList(char* str, char cc)

文字列を区切り文字で区切って,各項目をリストのキー部に入れて返す.@n
項目が存在しない場合でもリストは返す(キー部の bufが NULL).

@param  str  処理する文字列
@param  cc   項目の区切り文字
@return 項目をキー部に入れたリスト.項目が存在しない場合でもリストは返す.
@see cawk_tList
*/
tList*	awk_tList(char* str, char cc)
{
	int    nn = 1;
	char*  item;
	tList* lp = NULL;

	if (str==NULL) return NULL;

	lp = add_tList_node_bystr(NULL, 1, 0, NULL, NULL, NULL, 0);

	// first item
	item = awk(str, cc, nn);
	if (item==NULL) return lp;
	lp->ldat.key = make_Buffer_bystr(item);

	//
	item = awk(str, cc, ++nn);
	while (item!=NULL) {
		lp = add_tList_node_bystr(lp, nn, 0, item, NULL, NULL, 0);
		free(item);
		item = awk(str, cc, ++nn);
	}

	if (lp!=NULL) lp = find_tList_top(lp);
	return lp;
}
Exemplo n.º 2
0
struct passwd* getnisnam(char* usrid)
{
	struct passwd* pw;
	char*  buf;
	char*  nis;

	nis = get_nis_passwdf(usrid);
	if (nis==NULL) return NULL;
	pw = (struct passwd*)malloc(sizeof(struct passwd));
	if (pw==NULL) {
		free(nis);
		return NULL;
	}

	pw->pw_name   = awk(nis, ':', 1);
	pw->pw_passwd = awk(nis, ':', 2);
	pw->pw_gecos  = awk(nis, ':', 5);
	pw->pw_dir	  = awk(nis, ':', 6);
	pw->pw_shell  = awk(nis, ':', 7);

	buf = awk(nis, ':', 3);
	if (buf!=NULL) pw->pw_uid = atoi(buf);
	free(buf);

	buf = awk(nis, ':', 4);
	if (buf!=NULL) pw->pw_gid = atoi(buf);
	free(buf);

	free(nis);
	return pw;
}
Exemplo n.º 3
0
/*
 * mainline for awk
 */
int
main(int argc, char *argv[])
{
	wchar_t *ap;
	char *cmd;

	cmd = argv[0];
	_cmdname = cmd;

	linebuf = emalloc(NLINE * sizeof (wchar_t));

	/*
	 * At this point only messaging should be internationalized.
	 * numbers are still scanned as in the Posix locale.
	 */
	(void) setlocale(LC_ALL, "");
	(void) setlocale(LC_NUMERIC, "C");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	awkvarinit();
	/* running = 1; */
	while (argc > 1 && *argv[1] == '-') {
		void *save_ptr = NULL;
		ap = mbstowcsdup(&argv[1][1]);
		if (ap == NULL)
			break;
		if (*ap == '\0') {
			free(ap);
			break;
		}
		save_ptr = (void *) ap;
		++argv;
		--argc;
		if (*ap == '-' && ap[1] == '\0')
			break;
		for (; *ap != '\0'; ++ap) {
			switch (*ap) {
#ifdef DEBUG
			case 'd':
				dflag = 1;
				continue;

#endif
			case 'f':
				if (argc < 2) {
					(void) fprintf(stderr,
				gettext("Missing script file\n"));
					return (1);
				}
				*progfilep++ = argv[1];
				--argc;
				++argv;
				continue;

			case 'F':
				if (ap[1] == '\0') {
					if (argc < 2) {
						(void) fprintf(stderr,
				gettext("Missing field separator\n"));
						return (1);
					}
					ap = mbstowcsdup(argv[1]);
					--argc;
					++argv;
				} else
					++ap;
				strescape(ap);
				strassign(varFS, linebuf, FALLOC,
				    wcslen(linebuf));
				break;

			case 'v': {
				wchar_t *vp;
				wchar_t *arg;

				if (argc < 2) {
					(void) fprintf(stderr,
		gettext("Missing variable assignment\n"));
					return (1);
				}
				arg = mbconvert(argv[1]);
				/*
				 * Ensure the variable expression
				 * is valid (correct form).
				 */
				if (((vp = wcschr(arg, '=')) != NULL) &&
				    isclvar(arg)) {
					*vp = '\0';
					strescape(vp+1);
					strassign(vlook(arg), linebuf,
					    FALLOC|FSENSE,
					    wcslen(linebuf));
					*vp = '=';
				} else {
					(void) fprintf(stderr, gettext(
					    "Invalid form for variable "
					    "assignment: %S\n"), arg);
					return (1);
				}
				--argc;
				++argv;
				continue;
			}

			default:
				(void) fprintf(stderr,
				gettext("Unknown option \"-%S\"\n"), ap);
				return (usage());
			}
			break;
		}
		if (save_ptr)
			free(save_ptr);
	}
	if (progfilep == &progfiles[0]) {
		if (argc < 2)
			return (usage());
		filename = "[command line]";	/* BUG: NEEDS TRANSLATION */
		progptr = mbstowcsdup(argv[1]);
		proglen = wcslen(progptr);
		--argc;
		++argv;
	}

	argv[0] = cmd;

	awkarginit(argc, argv);

	/* running = 0; */
	(void) yyparse();

	lineno = 0;
	/*
	 * Ok, done parsing, so now activate the rest of the nls stuff, set
	 * the radix character.
	 */
	(void) setlocale(LC_ALL, "");
	radixpoint = *localeconv()->decimal_point;
	awk();
	/* NOTREACHED */
	return (0);
}
Exemplo n.º 4
0
void
crackdf(Dfile *df, Biobuf *b, uint len, char *dpath)
{
	char *name;
	char *field[3];
	char path[512];
	int n, inc, ok, npath;
	Symbol **l, *dp, *next;
	File *f, *ef;
	Dir *d;

	inc = 32;
	df->flen = inc;
	df->file = emalloc(df->flen*sizeof(File));
	df->nfile = 0;

	df->hlen = 1 + len/8;
	df->dhash = emalloc(df->hlen*sizeof(Symbol*));

	l = nil;
	while((n = awk(b, field, 3)) > 0){
		if(n != 2)
			continue;

		name = field[1];
		switch(*field[0]){
		case 'F':
			if(df->flen == df->nfile){
				df->flen += inc;
				df->file = realloc(df->file, df->flen*sizeof(File));
				if(df->file == nil)
					fatal(mallocerr);
				memset(&df->file[df->nfile], 0, inc*sizeof(File));
			}
			f = &df->file[df->nfile++];
			f->name = strdup(name);
			l = &f->ref;
			/* fall through and define as a symbol */
		case 'D':
			if(l == nil)
				continue;
			newsym(name, df->nfile-1, &(df->dhash[shash(name, df->hlen)]));
			break;
		case 'R':
			if(l == nil)
				continue;
			newsym(name, 0, l);
			break;
		}
	}

	ef = &df->file[df->nfile];

	/* stat the files to get sizes */
	npath = strlen(dpath);
	if(npath+1+1 >= sizeof path)
		fatal(Etoolong);
	memmove(path, dpath, npath+1);	/* include NUL */
	name = strrchr(path, '/') + 1;
	for(f = df->file; f < ef; f++){
		n = strlen(f->name);
		if(npath+1+n+3+1 > sizeof path)
			fatal(Etoolong);
		memmove(name, f->name, n+1);	/* include NUL */
		ok = access(path, AEXIST);
		if(ok < 0){
			strcpy(name+n, ".Z");
			ok = access(path, AEXIST);
			if(ok < 0){
				strcpy(name+n, ".gz");
				ok = access(path, AEXIST);
			}
		}
		if(ok >= 0){
			free(f->name);
			f->name = strdup(name);
			if(f->name == nil)
				fatal(mallocerr);
		}
		d = dirstat(path);
		if(d){
			f->len = d->length;
			f->mode = d->mode;
			f->mtime = d->mtime;
			free(d);
		}else{
			f->len = 0;
			f->mode = 0;
			f->mtime = 0;
		}
		f->fd = -1;
	}

	/* resolve all file references */
	for(f = df->file; f < ef; f++)
		dfresolve(df, f-df->file);

	/* free the referenced symbols, don't need them anymore */
	for(f = df->file; f < ef; f++){
		f->tarlen += 2*Tblocksize;	/* tars trailing 0 blocks */
		for(dp = f->ref; dp != nil; dp = next){
			next = dp->next;
			free(dp);
		}
		f->ref = nil;
	}
}